From ed3424ff75b396360990725afc124326dea4ab45 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 17 Jul 2014 11:21:18 +0200 Subject: Trie fixes --- ethchain/state.go | 4 ++++ ethchain/state_manager.go | 1 + ethchain/state_object.go | 41 +++++++++++++++++++++++++++++++---------- ethchain/vm.go | 2 +- 4 files changed, 37 insertions(+), 11 deletions(-) (limited to 'ethchain') diff --git a/ethchain/state.go b/ethchain/state.go index 66c298b3c..6b849296a 100644 --- a/ethchain/state.go +++ b/ethchain/state.go @@ -76,6 +76,8 @@ func (self *State) DeleteStateObject(stateObject *StateObject) { // Retrieve a state object given my the address. Nil if not found func (self *State) GetStateObject(addr []byte) *StateObject { + addr = ethutil.Address(addr) + stateObject := self.stateObjects[string(addr)] if stateObject != nil { return stateObject @@ -204,6 +206,8 @@ func (self *State) Update() { // FIXME trie delete is broken valid, t2 := ethtrie.ParanoiaCheck(self.trie) if !valid { + statelogger.Infof("Warn: PARANOIA: Different state root during copy %x vs %x\n", self.trie.Root, t2.Root) + self.trie = t2 } } diff --git a/ethchain/state_manager.go b/ethchain/state_manager.go index 80362fa77..a0568c4cd 100644 --- a/ethchain/state_manager.go +++ b/ethchain/state_manager.go @@ -121,6 +121,7 @@ done: for i, tx := range txs { txGas := new(big.Int).Set(tx.Gas) st := NewStateTransition(coinbase, tx, state, block) + //fmt.Printf("#%d\n", i+1) err = st.TransitionState() if err != nil { switch { diff --git a/ethchain/state_object.go b/ethchain/state_object.go index 889496e91..cf37586fc 100644 --- a/ethchain/state_object.go +++ b/ethchain/state_object.go @@ -75,7 +75,7 @@ func MakeContract(tx *Transaction, state *State) *StateObject { func NewStateObject(addr []byte) *StateObject { // This to ensure that it has 20 bytes (and not 0 bytes), thus left or right pad doesn't matter. - address := ethutil.LeftPadBytes(addr, 20) + address := ethutil.Address(addr) object := &StateObject{address: address, Amount: new(big.Int), gasPool: new(big.Int)} object.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, "")) @@ -92,13 +92,6 @@ func NewContract(address []byte, Amount *big.Int, root []byte) *StateObject { return contract } -// Returns a newly created account -func NewAccount(address []byte, amount *big.Int) *StateObject { - account := &StateObject{address: address, Amount: amount, Nonce: 0} - - return account -} - func NewStateObjectFromBytes(address, data []byte) *StateObject { object := &StateObject{address: address} object.RlpDecode(data) @@ -139,17 +132,37 @@ func (self *StateObject) getStorage(k []byte) *ethutil.Value { } return value + + //return self.GetAddr(key) } func (self *StateObject) setStorage(k []byte, value *ethutil.Value) { key := ethutil.LeftPadBytes(k, 32) - //fmt.Printf("%x %v\n", key, value) self.storage[string(key)] = value.Copy() + + /* + if value.BigInt().Cmp(ethutil.Big0) == 0 { + self.state.trie.Delete(string(key)) + return + } + + self.SetAddr(key, value) + */ } func (self *StateObject) Sync() { + /* + fmt.Println("############# BEFORE ################") + self.state.EachStorage(func(key string, value *ethutil.Value) { + fmt.Printf("%x %x %x\n", self.Address(), []byte(key), value.Bytes()) + }) + fmt.Printf("%x @:%x\n", self.Address(), self.state.Root()) + fmt.Println("#####################################") + */ for key, value := range self.storage { - if value.BigInt().Cmp(ethutil.Big0) == 0 { + if value.Len() == 0 { // value.BigInt().Cmp(ethutil.Big0) == 0 { + //data := self.getStorage([]byte(key)) + //fmt.Printf("deleting %x %x 0x%x\n", self.Address(), []byte(key), data) self.state.trie.Delete(string(key)) continue } @@ -163,6 +176,14 @@ func (self *StateObject) Sync() { self.state.trie = t2 } + + /* + fmt.Println("############# AFTER ################") + self.state.EachStorage(func(key string, value *ethutil.Value) { + fmt.Printf("%x %x %x\n", self.Address(), []byte(key), value.Bytes()) + }) + */ + //fmt.Printf("%x @:%x\n", self.Address(), self.state.Root()) } func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value { diff --git a/ethchain/vm.go b/ethchain/vm.go index 3a956ee83..58d1bee89 100644 --- a/ethchain/vm.go +++ b/ethchain/vm.go @@ -195,7 +195,7 @@ func (vm *Vm) RunClosure(closure *Closure) (ret []byte, err error) { val := closure.GetStorage(x) if val.BigInt().Cmp(ethutil.Big0) == 0 && len(y.Bytes()) > 0 { mult = ethutil.Big2 - } else if !val.IsEmpty() && len(y.Bytes()) == 0 { + } else if val.BigInt().Cmp(ethutil.Big0) != 0 && len(y.Bytes()) == 0 { mult = ethutil.Big0 } else { mult = ethutil.Big1 -- cgit v1.2.3