aboutsummaryrefslogtreecommitdiffstats
path: root/ethchain
diff options
context:
space:
mode:
Diffstat (limited to 'ethchain')
-rw-r--r--ethchain/block_chain.go3
-rw-r--r--ethchain/state_manager.go93
-rw-r--r--ethchain/state_object.go21
-rw-r--r--ethchain/transaction_pool.go29
4 files changed, 119 insertions, 27 deletions
diff --git a/ethchain/block_chain.go b/ethchain/block_chain.go
index 5e6ce46e1..8cede2403 100644
--- a/ethchain/block_chain.go
+++ b/ethchain/block_chain.go
@@ -259,7 +259,7 @@ func (bc *BlockChain) GetChain(hash []byte, amount int) []*Block {
func AddTestNetFunds(block *Block) {
for _, addr := range []string{
- "8a40bfaa73256b60764c1bf40675a99083efb075",
+ "51ba59315b3a95761d0863b05ccc7a7f54703d99",
"e4157b34ea9615cfbde6b4fda419828124b70c78",
"1e12515ce3e0f817a4ddef9ca55788a1d66bd2df",
"6c386a4b26f73c802f34673f7248bb118f97424a",
@@ -273,7 +273,6 @@ func AddTestNetFunds(block *Block) {
account.Amount = ethutil.Big("1606938044258990275541962092341162602522202993782792835301376") //ethutil.BigPow(2, 200)
block.state.UpdateStateObject(account)
}
- log.Printf("%x\n", block.RlpEncode())
}
func (bc *BlockChain) setLastBlock() {
diff --git a/ethchain/state_manager.go b/ethchain/state_manager.go
index 6fb664e3d..9631b55fe 100644
--- a/ethchain/state_manager.go
+++ b/ethchain/state_manager.go
@@ -108,17 +108,92 @@ func (sm *StateManager) MakeStateObject(state *State, tx *Transaction) *StateObj
return nil
}
+func (self *StateManager) ProcessTransaction(tx *Transaction, coinbase *StateObject, state *State, toContract bool) (gas *big.Int, err error) {
+ fmt.Printf("state root before update %x\n", state.Root())
+ defer func() {
+ if r := recover(); r != nil {
+ ethutil.Config.Log.Infoln(r)
+ err = fmt.Errorf("%v", r)
+ }
+ }()
+
+ gas = new(big.Int)
+ addGas := func(g *big.Int) { gas.Add(gas, g) }
+ addGas(GasTx)
+
+ // Get the sender
+ sender := state.GetAccount(tx.Sender())
+
+ if sender.Nonce != tx.Nonce {
+ err = NonceError(tx.Nonce, sender.Nonce)
+ return
+ }
+
+ sender.Nonce += 1
+ defer func() {
+ //state.UpdateStateObject(sender)
+ // Notify all subscribers
+ self.Ethereum.Reactor().Post("newTx:post", tx)
+ }()
+
+ txTotalBytes := big.NewInt(int64(len(tx.Data)))
+ txTotalBytes.Div(txTotalBytes, ethutil.Big32)
+ addGas(new(big.Int).Mul(txTotalBytes, GasSStore))
+
+ rGas := new(big.Int).Set(gas)
+ rGas.Mul(gas, tx.GasPrice)
+
+ // Make sure there's enough in the sender's account. Having insufficient
+ // funds won't invalidate this transaction but simple ignores it.
+ totAmount := new(big.Int).Add(tx.Value, rGas)
+ if sender.Amount.Cmp(totAmount) < 0 {
+ state.UpdateStateObject(sender)
+ err = fmt.Errorf("[TXPL] Insufficient amount in sender's (%x) account", tx.Sender())
+ return
+ }
+
+ coinbase.BuyGas(gas, tx.GasPrice)
+ state.UpdateStateObject(coinbase)
+
+ // Get the receiver
+ receiver := state.GetAccount(tx.Recipient)
+
+ // Send Tx to self
+ if bytes.Compare(tx.Recipient, tx.Sender()) == 0 {
+ // Subtract the fee
+ sender.SubAmount(rGas)
+ } else {
+ // Subtract the amount from the senders account
+ sender.SubAmount(totAmount)
+
+ fmt.Printf("state root after sender update %x\n", state.Root())
+
+ // Add the amount to receivers account which should conclude this transaction
+ receiver.AddAmount(tx.Value)
+ state.UpdateStateObject(receiver)
+
+ fmt.Printf("state root after receiver update %x\n", state.Root())
+ }
+
+ state.UpdateStateObject(sender)
+
+ ethutil.Config.Log.Infof("[TXPL] Processed Tx %x\n", tx.Hash())
+
+ return
+}
+
// Apply transactions uses the transaction passed to it and applies them onto
// the current processing state.
-func (sm *StateManager) ApplyTransactions(state *State, block *Block, txs []*Transaction) ([]*Receipt, []*Transaction) {
+func (sm *StateManager) ApplyTransactions(coinbase []byte, state *State, block *Block, txs []*Transaction) ([]*Receipt, []*Transaction) {
// Process each transaction/contract
var receipts []*Receipt
var validTxs []*Transaction
var ignoredTxs []*Transaction // Transactions which go over the gasLimit
totalUsedGas := big.NewInt(0)
+
for _, tx := range txs {
- usedGas, err := sm.ApplyTransaction(state, block, tx)
+ usedGas, err := sm.ApplyTransaction(coinbase, state, block, tx)
if err != nil {
if IsNonceErr(err) {
continue
@@ -146,7 +221,7 @@ func (sm *StateManager) ApplyTransactions(state *State, block *Block, txs []*Tra
return receipts, validTxs
}
-func (sm *StateManager) ApplyTransaction(state *State, block *Block, tx *Transaction) (totalGasUsed *big.Int, err error) {
+func (sm *StateManager) ApplyTransaction(coinbase []byte, state *State, block *Block, tx *Transaction) (totalGasUsed *big.Int, err error) {
/*
Applies transactions to the given state and creates new
state objects where needed.
@@ -164,8 +239,9 @@ func (sm *StateManager) ApplyTransaction(state *State, block *Block, tx *Transac
totalGasUsed = big.NewInt(0)
snapshot := state.Snapshot()
+ ca := state.GetAccount(coinbase)
// Apply the transaction to the current state
- gas, err = sm.Ethereum.TxPool().ProcessTransaction(tx, state, false)
+ gas, err = sm.ProcessTransaction(tx, ca, state, false)
addTotalGas(gas)
if tx.CreatesContract() {
@@ -249,7 +325,7 @@ func (sm *StateManager) ProcessBlock(state *State, parent, block *Block, dontRea
}
// Process the transactions on to current block
- sm.ApplyTransactions(state, parent, block.Transactions())
+ sm.ApplyTransactions(block.Coinbase, state, parent, block.Transactions())
// Block validation
if err := sm.ValidateBlock(block); err != nil {
@@ -357,13 +433,6 @@ func CalculateBlockReward(block *Block, uncleLength int) *big.Int {
base.Add(base, UncleInclusionReward)
}
- lastCumulGasUsed := big.NewInt(0)
- for _, r := range block.Receipts() {
- usedGas := new(big.Int).Sub(r.CumulativeGasUsed, lastCumulGasUsed)
- usedGas.Add(usedGas, r.Tx.GasPrice)
- base.Add(base, usedGas)
- }
-
return base.Add(base, BlockReward)
}
diff --git a/ethchain/state_object.go b/ethchain/state_object.go
index 3e9c6df40..a1dd531de 100644
--- a/ethchain/state_object.go
+++ b/ethchain/state_object.go
@@ -108,10 +108,14 @@ func (c *StateObject) ReturnGas(gas, price *big.Int, state *State) {
func (c *StateObject) AddAmount(amount *big.Int) {
c.SetAmount(new(big.Int).Add(c.Amount, amount))
+
+ ethutil.Config.Log.Debugf("%x: #%d %v (+ %v)", c.Address(), c.Nonce, c.Amount, amount)
}
func (c *StateObject) SubAmount(amount *big.Int) {
c.SetAmount(new(big.Int).Sub(c.Amount, amount))
+
+ ethutil.Config.Log.Debugf("%x: #%d %v (- %v)", c.Address(), c.Nonce, c.Amount, amount)
}
func (c *StateObject) SetAmount(amount *big.Int) {
@@ -129,6 +133,17 @@ func (c *StateObject) ConvertGas(gas, price *big.Int) error {
return nil
}
+func (self *StateObject) BuyGas(gas, price *big.Int) error {
+ rGas := new(big.Int).Set(gas)
+ rGas.Mul(gas, price)
+
+ self.AddAmount(rGas)
+
+ // TODO Do sub from TotalGasPool
+ // and check if enough left
+ return nil
+}
+
// Returns the address of the contract/account
func (c *StateObject) Address() []byte {
return c.address
@@ -153,14 +168,14 @@ func (c *StateObject) RlpEncode() []byte {
root = ""
}
- return ethutil.Encode([]interface{}{c.Amount, c.Nonce, root, ethutil.Sha3Bin(c.script)})
+ return ethutil.Encode([]interface{}{c.Nonce, c.Amount, root, ethutil.Sha3Bin(c.script)})
}
func (c *StateObject) RlpDecode(data []byte) {
decoder := ethutil.NewValueFromBytes(data)
- c.Amount = decoder.Get(0).BigInt()
- c.Nonce = decoder.Get(1).Uint()
+ c.Nonce = decoder.Get(0).Uint()
+ c.Amount = decoder.Get(1).BigInt()
c.state = NewState(ethutil.NewTrie(ethutil.Config.Db, decoder.Get(2).Interface()))
c.ScriptHash = decoder.Get(3).Bytes()
diff --git a/ethchain/transaction_pool.go b/ethchain/transaction_pool.go
index 103c305fe..52c850ba3 100644
--- a/ethchain/transaction_pool.go
+++ b/ethchain/transaction_pool.go
@@ -90,9 +90,11 @@ func (pool *TxPool) addTransaction(tx *Transaction) {
pool.Ethereum.Broadcast(ethwire.MsgTxTy, []interface{}{tx.RlpData()})
}
+/*
// Process transaction validates the Tx and processes funds from the
// sender to the recipient.
func (pool *TxPool) ProcessTransaction(tx *Transaction, state *State, toContract bool) (gas *big.Int, err error) {
+ fmt.Printf("state root before update %x\n", state.Root())
defer func() {
if r := recover(); r != nil {
ethutil.Config.Log.Infoln(r)
@@ -102,6 +104,7 @@ func (pool *TxPool) ProcessTransaction(tx *Transaction, state *State, toContract
gas = new(big.Int)
addGas := func(g *big.Int) { gas.Add(gas, g) }
+ addGas(GasTx)
// Get the sender
sender := state.GetAccount(tx.Sender())
@@ -111,28 +114,37 @@ func (pool *TxPool) ProcessTransaction(tx *Transaction, state *State, toContract
return
}
+ sender.Nonce += 1
+ defer func() {
+ //state.UpdateStateObject(sender)
+ // Notify all subscribers
+ pool.Ethereum.Reactor().Post("newTx:post", tx)
+ }()
+
txTotalBytes := big.NewInt(int64(len(tx.Data)))
txTotalBytes.Div(txTotalBytes, ethutil.Big32)
addGas(new(big.Int).Mul(txTotalBytes, GasSStore))
+ rGas := new(big.Int).Set(gas)
+ rGas.Mul(gas, tx.GasPrice)
+
// Make sure there's enough in the sender's account. Having insufficient
// funds won't invalidate this transaction but simple ignores it.
- //totAmount := new(big.Int).Add(tx.Value, new(big.Int).Mul(TxFee, TxFeeRat))
- totAmount := new(big.Int).Add(tx.Value, new(big.Int).Mul(tx.Gas, tx.GasPrice))
+ totAmount := new(big.Int).Add(tx.Value, rGas)
if sender.Amount.Cmp(totAmount) < 0 {
err = fmt.Errorf("[TXPL] Insufficient amount in sender's (%x) account", tx.Sender())
return
}
+ state.UpdateStateObject(sender)
+ fmt.Printf("state root after sender update %x\n", state.Root())
// Get the receiver
receiver := state.GetAccount(tx.Recipient)
- sender.Nonce += 1
// Send Tx to self
if bytes.Compare(tx.Recipient, tx.Sender()) == 0 {
- addGas(GasTx)
// Subtract the fee
- sender.SubAmount(new(big.Int).Mul(GasTx, tx.GasPrice))
+ sender.SubAmount(rGas)
} else {
// Subtract the amount from the senders account
sender.SubAmount(totAmount)
@@ -141,17 +153,14 @@ func (pool *TxPool) ProcessTransaction(tx *Transaction, state *State, toContract
receiver.AddAmount(tx.Value)
state.UpdateStateObject(receiver)
+ fmt.Printf("state root after receiver update %x\n", state.Root())
}
- state.UpdateStateObject(sender)
-
ethutil.Config.Log.Infof("[TXPL] Processed Tx %x\n", tx.Hash())
- // Notify all subscribers
- pool.Ethereum.Reactor().Post("newTx:post", tx)
-
return
}
+*/
func (pool *TxPool) ValidateTransaction(tx *Transaction) error {
// Get the last block so we can retrieve the sender and receiver from