diff options
-rw-r--r-- | ethchain/state.go | 14 | ||||
-rw-r--r-- | ethchain/state_object.go | 26 | ||||
-rw-r--r-- | ethchain/vm.go | 16 | ||||
-rw-r--r-- | ethpub/types.go | 4 | ||||
-rw-r--r-- | ethutil/big.go | 19 | ||||
-rw-r--r-- | ethutil/common.go | 12 |
6 files changed, 58 insertions, 33 deletions
diff --git a/ethchain/state.go b/ethchain/state.go index 684b81102..3a9929ecc 100644 --- a/ethchain/state.go +++ b/ethchain/state.go @@ -1,7 +1,6 @@ package ethchain import ( - "fmt" "github.com/ethereum/eth-go/ethcrypto" "github.com/ethereum/eth-go/ethtrie" "github.com/ethereum/eth-go/ethutil" @@ -27,12 +26,6 @@ func NewState(trie *ethtrie.Trie) *State { return &State{trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest()} } -// Iterate over each storage address and yield callback -func (s *State) EachStorage(cb ethtrie.EachCallback) { - it := s.trie.NewIterator() - it.Each(cb) -} - // Retrieve the balance from the given address or 0 if object not found func (self *State) GetBalance(addr []byte) *big.Int { stateObject := self.GetStateObject(addr) @@ -214,11 +207,8 @@ func (self *State) Update() { // Debug stuff func (self *State) CreateOutputForDiff() { - for addr, stateObject := range self.stateObjects { - fmt.Printf("%x %x %x %x\n", addr, stateObject.state.Root(), stateObject.Amount.Bytes(), stateObject.Nonce) - stateObject.state.EachStorage(func(addr string, value *ethutil.Value) { - fmt.Printf("%x %x\n", addr, value.Bytes()) - }) + for _, stateObject := range self.stateObjects { + stateObject.CreateOutputForDiff() } } diff --git a/ethchain/state_object.go b/ethchain/state_object.go index 8e7b5fece..184609015 100644 --- a/ethchain/state_object.go +++ b/ethchain/state_object.go @@ -151,6 +151,24 @@ func (self *StateObject) setStorage(k []byte, value *ethutil.Value) { */ } +// Iterate over each storage address and yield callback +func (self *StateObject) EachStorage(cb ethtrie.EachCallback) { + // First loop over the uncommit/cached values in storage + for key, value := range self.storage { + // XXX Most iterators Fns as it stands require encoded values + encoded := ethutil.NewValue(value.Encode()) + cb(key, encoded) + } + + it := self.state.trie.NewIterator() + it.Each(func(key string, value *ethutil.Value) { + // If it's cached don't call the callback. + if self.storage[key] == nil { + cb(key, value) + } + }) +} + func (self *StateObject) Sync() { /* fmt.Println("############# BEFORE ################") @@ -303,6 +321,14 @@ func (c *StateObject) Init() Code { return c.initScript } +// Debug stuff +func (self *StateObject) CreateOutputForDiff() { + fmt.Printf("%x %x %x %x\n", self.Address(), self.state.Root(), self.Amount.Bytes(), self.Nonce) + self.EachStorage(func(addr string, value *ethutil.Value) { + fmt.Printf("%x %x\n", addr, value.Bytes()) + }) +} + // // Encoding // diff --git a/ethchain/vm.go b/ethchain/vm.go index 07e9a0f2c..d5f65676c 100644 --- a/ethchain/vm.go +++ b/ethchain/vm.go @@ -158,7 +158,7 @@ func (vm *Vm) RunClosure(closure *Closure) (ret []byte, err error) { switch op { case STOP, RETURN, SUICIDE: closure.object.Sync() - closure.object.state.EachStorage(func(key string, value *ethutil.Value) { + closure.object.EachStorage(func(key string, value *ethutil.Value) { value.Decode() fmt.Printf("%x %x\n", new(big.Int).SetBytes([]byte(key)).Bytes(), value.Bytes()) }) @@ -505,13 +505,17 @@ func (vm *Vm) RunClosure(closure *Closure) (ret []byte, err error) { vm.Printf(" => %v", vm.vars.Value) case CALLDATALOAD: require(1) - offset := stack.Pop().Int64() + var ( + offset = stack.Pop() + data = make([]byte, 32) + lenData = big.NewInt(int64(len(closure.Args))) + ) - data := make([]byte, 32) - if big.NewInt(int64(len(closure.Args))).Cmp(big.NewInt(offset)) >= 0 { - l := int64(math.Min(float64(offset+32), float64(len(closure.Args)))) + if lenData.Cmp(offset) >= 0 { + length := new(big.Int).Add(offset, ethutil.Big32) + length = ethutil.BigMin(length, lenData) - copy(data, closure.Args[offset:l]) + copy(data, closure.Args[offset.Int64():length.Int64()]) } vm.Printf(" => 0x%x", data) diff --git a/ethpub/types.go b/ethpub/types.go index 9e5159a4c..52ea5a6bb 100644 --- a/ethpub/types.go +++ b/ethpub/types.go @@ -215,7 +215,7 @@ func (c *PStateObject) IsContract() bool { } func (self *PStateObject) EachStorage(cb ethtrie.EachCallback) { - self.object.State().EachStorage(cb) + self.object.EachStorage(cb) } type KeyVal struct { @@ -226,7 +226,7 @@ type KeyVal struct { func (c *PStateObject) StateKeyVal(asJson bool) interface{} { var values []KeyVal if c.object != nil { - c.object.State().EachStorage(func(name string, value *ethutil.Value) { + c.object.EachStorage(func(name string, value *ethutil.Value) { values = append(values, KeyVal{name, ethutil.Bytes2Hex(value.Bytes())}) }) } diff --git a/ethutil/big.go b/ethutil/big.go index 7af6f7414..ec263b818 100644 --- a/ethutil/big.go +++ b/ethutil/big.go @@ -4,14 +4,6 @@ import ( "math/big" ) -var BigInt0 *big.Int = big.NewInt(0) - -// True -var BigTrue *big.Int = big.NewInt(1) - -// False -var BigFalse *big.Int = big.NewInt(0) - // Big pow // // Returns the power of two big integers @@ -73,3 +65,14 @@ func BigMax(x, y *big.Int) *big.Int { return x } + +// Big min +// +// Returns the minimum size big integer +func BigMin(x, y *big.Int) *big.Int { + if x.Cmp(y) >= 0 { + return y + } + + return x +} diff --git a/ethutil/common.go b/ethutil/common.go index 2fd031a20..cbd94e7ad 100644 --- a/ethutil/common.go +++ b/ethutil/common.go @@ -58,9 +58,11 @@ func CurrencyToString(num *big.Int) string { // Common big integers often used var ( - Big1 = big.NewInt(1) - Big2 = big.NewInt(2) - Big0 = big.NewInt(0) - Big32 = big.NewInt(32) - Big256 = big.NewInt(0xff) + Big1 = big.NewInt(1) + Big2 = big.NewInt(2) + Big0 = big.NewInt(0) + BigTrue = Big1 + BigFalse = Big0 + Big32 = big.NewInt(32) + Big256 = big.NewInt(0xff) ) |