diff options
author | obscuren <geffobscura@gmail.com> | 2014-07-17 20:53:27 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-07-17 20:53:27 +0800 |
commit | 0415e4a637296539e7a5c09282b7aee19268e599 (patch) | |
tree | c403eac0c3d4cda7aef003bba658f7b39881cdb5 | |
parent | ed3424ff75b396360990725afc124326dea4ab45 (diff) | |
download | dexon-0415e4a637296539e7a5c09282b7aee19268e599.tar dexon-0415e4a637296539e7a5c09282b7aee19268e599.tar.gz dexon-0415e4a637296539e7a5c09282b7aee19268e599.tar.bz2 dexon-0415e4a637296539e7a5c09282b7aee19268e599.tar.lz dexon-0415e4a637296539e7a5c09282b7aee19268e599.tar.xz dexon-0415e4a637296539e7a5c09282b7aee19268e599.tar.zst dexon-0415e4a637296539e7a5c09282b7aee19268e599.zip |
Fixed coinbase copy in state
-rw-r--r-- | ethchain/state.go | 3 | ||||
-rw-r--r-- | ethchain/state_manager.go | 4 | ||||
-rw-r--r-- | ethchain/state_object.go | 9 | ||||
-rw-r--r-- | ethchain/state_transition.go | 30 | ||||
-rw-r--r-- | ethchain/vm.go | 2 |
5 files changed, 29 insertions, 19 deletions
diff --git a/ethchain/state.go b/ethchain/state.go index 6b849296a..9748da1bc 100644 --- a/ethchain/state.go +++ b/ethchain/state.go @@ -61,6 +61,7 @@ func (self *State) UpdateStateObject(stateObject *StateObject) { addr := stateObject.Address() ethutil.Config.Db.Put(ethcrypto.Sha3Bin(stateObject.Script()), stateObject.Script()) + fmt.Printf("balance %v %p\n", stateObject.Amount, stateObject) self.trie.Update(string(addr), string(stateObject.RlpEncode())) @@ -174,7 +175,7 @@ func (s *State) Reset() { func (s *State) Sync() { // Sync all nested states for _, stateObject := range s.stateObjects { - s.UpdateStateObject(stateObject) + //s.UpdateStateObject(stateObject) if stateObject.state == nil { continue diff --git a/ethchain/state_manager.go b/ethchain/state_manager.go index a0568c4cd..0d4b8ac55 100644 --- a/ethchain/state_manager.go +++ b/ethchain/state_manager.go @@ -120,7 +120,9 @@ func (self *StateManager) ProcessTransactions(coinbase *StateObject, state *Stat done: for i, tx := range txs { txGas := new(big.Int).Set(tx.Gas) - st := NewStateTransition(coinbase, tx, state, block) + + cb := state.GetStateObject(coinbase.Address()) + st := NewStateTransition(cb, tx, state, block) //fmt.Printf("#%d\n", i+1) err = st.TransitionState() if err != nil { diff --git a/ethchain/state_object.go b/ethchain/state_object.go index cf37586fc..a4225991a 100644 --- a/ethchain/state_object.go +++ b/ethchain/state_object.go @@ -80,6 +80,7 @@ func NewStateObject(addr []byte) *StateObject { object := &StateObject{address: address, Amount: new(big.Int), gasPool: new(big.Int)} object.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, "")) object.storage = make(Storage) + object.gasPool = new(big.Int) return object } @@ -183,7 +184,7 @@ func (self *StateObject) Sync() { fmt.Printf("%x %x %x\n", self.Address(), []byte(key), value.Bytes()) }) */ - //fmt.Printf("%x @:%x\n", self.Address(), self.state.Root()) + fmt.Printf("%x @:%x\n", self.Address(), self.state.Root()) } func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value { @@ -197,13 +198,13 @@ func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value { func (c *StateObject) AddAmount(amount *big.Int) { c.SetAmount(new(big.Int).Add(c.Amount, amount)) - statelogger.DebugDetailf("%x: #%d %v (+ %v)\n", c.Address(), c.Nonce, c.Amount, amount) + statelogger.Debugf("%x: #%d %v (+ %v)\n", c.Address(), c.Nonce, c.Amount, amount) } func (c *StateObject) SubAmount(amount *big.Int) { c.SetAmount(new(big.Int).Sub(c.Amount, amount)) - statelogger.DebugDetailf("%x: #%d %v (- %v)\n", c.Address(), c.Nonce, c.Amount, amount) + statelogger.Debugf("%x: #%d %v (- %v)\n", c.Address(), c.Nonce, c.Amount, amount) } func (c *StateObject) SetAmount(amount *big.Int) { @@ -266,6 +267,7 @@ func (self *StateObject) Copy() *StateObject { stateObject.script = ethutil.CopyBytes(self.script) stateObject.initScript = ethutil.CopyBytes(self.initScript) stateObject.storage = self.storage.Copy() + stateObject.gasPool.Set(self.gasPool) return stateObject } @@ -324,6 +326,7 @@ func (c *StateObject) RlpDecode(data []byte) { c.Amount = decoder.Get(1).BigInt() c.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, decoder.Get(2).Interface())) c.storage = make(map[string]*ethutil.Value) + c.gasPool = new(big.Int) c.ScriptHash = decoder.Get(3).Bytes() diff --git a/ethchain/state_transition.go b/ethchain/state_transition.go index 8cface9e8..8ed528c9f 100644 --- a/ethchain/state_transition.go +++ b/ethchain/state_transition.go @@ -42,7 +42,7 @@ func (self *StateTransition) Coinbase() *StateObject { return self.cb } - self.cb = self.state.GetAccount(self.coinbase) + self.cb = self.state.GetOrNewStateObject(self.coinbase) return self.cb } func (self *StateTransition) Sender() *StateObject { @@ -50,7 +50,7 @@ func (self *StateTransition) Sender() *StateObject { return self.sen } - self.sen = self.state.GetAccount(self.tx.Sender()) + self.sen = self.state.GetOrNewStateObject(self.tx.Sender()) return self.sen } @@ -63,7 +63,7 @@ func (self *StateTransition) Receiver() *StateObject { return self.rec } - self.rec = self.state.GetAccount(self.tx.Recipient) + self.rec = self.state.GetOrNewStateObject(self.tx.Recipient) return self.rec } @@ -174,13 +174,16 @@ func (self *StateTransition) TransitionState() (err error) { return } - /* FIXME - * If tx goes TO "0", goes OOG during init, reverse changes, but initial endowment should happen. The ether is lost forever - */ - var snapshot *State + if sender.Amount.Cmp(self.value) < 0 { + return fmt.Errorf("Insufficient funds to transfer value. Req %v, has %v", self.value, sender.Amount) + } + var snapshot *State // If the receiver is nil it's a contract (\0*32). if tx.CreatesContract() { + // Subtract the (irreversible) amount from the senders account + sender.SubAmount(self.value) + snapshot = self.state.Copy() // Create a new state object for the contract @@ -189,16 +192,17 @@ func (self *StateTransition) TransitionState() (err error) { if receiver == nil { return fmt.Errorf("Unable to create contract") } + + // Add the amount to receivers account which should conclude this transaction + receiver.AddAmount(self.value) } else { receiver = self.Receiver() - } - // Transfer value from sender to receiver - if err = self.transferValue(sender, receiver); err != nil { - return - } + // Subtract the amount from the senders account + sender.SubAmount(self.value) + // Add the amount to receivers account which should conclude this transaction + receiver.AddAmount(self.value) - if snapshot == nil { snapshot = self.state.Copy() } diff --git a/ethchain/vm.go b/ethchain/vm.go index 58d1bee89..4fdf8b31a 100644 --- a/ethchain/vm.go +++ b/ethchain/vm.go @@ -456,7 +456,7 @@ func (vm *Vm) RunClosure(closure *Closure) (ret []byte, err error) { case BYTE: require(2) val, th := stack.Popn() - if th.Cmp(big.NewInt(32)) < 0 { + if th.Cmp(big.NewInt(32)) < 0 && th.Cmp(big.NewInt(int64(len(val.Bytes())))) < 0 { byt := big.NewInt(int64(val.Bytes()[th.Int64()])) stack.Push(byt) |