From 1e8b54abfb7129fcdf4812ad01b6a7cd61e4f65d Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 22 Jul 2014 11:54:48 +0200 Subject: Refactored state, state object and vm * The State and StateObject have been moved to their own package * The VM is moved to it's own package --- ethstate/state_object.go | 339 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 339 insertions(+) create mode 100644 ethstate/state_object.go (limited to 'ethstate/state_object.go') diff --git a/ethstate/state_object.go b/ethstate/state_object.go new file mode 100644 index 000000000..d8513f37d --- /dev/null +++ b/ethstate/state_object.go @@ -0,0 +1,339 @@ +package ethstate + +import ( + "fmt" + "github.com/ethereum/eth-go/ethcrypto" + "github.com/ethereum/eth-go/ethtrie" + "github.com/ethereum/eth-go/ethutil" + "math/big" +) + +type Code []byte + +func (self Code) String() string { + return "" //strings.Join(Disassemble(self), " ") +} + +type Storage map[string]*ethutil.Value + +func (self Storage) Copy() Storage { + cpy := make(Storage) + for key, value := range self { + // XXX Do we need a 'value' copy or is this sufficient? + cpy[key] = value + } + + return cpy +} + +type StateObject struct { + // Address of the object + address []byte + // Shared attributes + Amount *big.Int + CodeHash []byte + Nonce uint64 + // Contract related attributes + state *State + Code Code + initCode Code + + storage Storage + + // Total gas pool is the total amount of gas currently + // left if this object is the coinbase. Gas is directly + // purchased of the coinbase. + gasPool *big.Int + + // Mark for deletion + // When an object is marked for deletion it will be delete from the trie + // during the "update" phase of the state transition + remove bool +} + +func (self *StateObject) Reset() { + self.storage = make(Storage) + self.state.Reset() +} + +/* +// Converts an transaction in to a state object +func MakeContract(tx *Transaction, state *State) *StateObject { + // Create contract if there's no recipient + if tx.IsContract() { + addr := tx.CreationAddress() + + contract := state.NewStateObject(addr) + contract.initCode = tx.Data + contract.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, "")) + + return contract + } + + return nil +} +*/ + +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.Address(addr) + + 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 +} + +func NewContract(address []byte, Amount *big.Int, root []byte) *StateObject { + contract := NewStateObject(address) + contract.Amount = Amount + contract.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, string(root))) + + return contract +} + +func NewStateObjectFromBytes(address, data []byte) *StateObject { + object := &StateObject{address: address} + object.RlpDecode(data) + + return object +} + +func (self *StateObject) MarkForDeletion() { + self.remove = true + statelogger.DebugDetailf("%x: #%d %v (deletion)\n", self.Address(), self.Nonce, self.Amount) +} + +func (c *StateObject) GetAddr(addr []byte) *ethutil.Value { + return ethutil.NewValueFromBytes([]byte(c.state.trie.Get(string(addr)))) +} + +func (c *StateObject) SetAddr(addr []byte, value interface{}) { + c.state.trie.Update(string(addr), string(ethutil.NewValue(value).Encode())) +} + +func (self *StateObject) GetStorage(key *big.Int) *ethutil.Value { + return self.getStorage(key.Bytes()) +} +func (self *StateObject) SetStorage(key *big.Int, value *ethutil.Value) { + self.setStorage(key.Bytes(), value) +} + +func (self *StateObject) getStorage(k []byte) *ethutil.Value { + key := ethutil.LeftPadBytes(k, 32) + + value := self.storage[string(key)] + if value == nil { + value = self.GetAddr(key) + + if !value.IsNil() { + self.storage[string(key)] = value + } + } + + return value + + //return self.GetAddr(key) +} + +func (self *StateObject) setStorage(k []byte, value *ethutil.Value) { + key := ethutil.LeftPadBytes(k, 32) + self.storage[string(key)] = value.Copy() +} + +// 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() { + for key, value := range self.storage { + 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 + } + + self.SetAddr([]byte(key), value) + } + + valid, t2 := ethtrie.ParanoiaCheck(self.state.trie) + if !valid { + statelogger.Infof("Warn: PARANOIA: Different state storage root during copy %x vs %x\n", self.state.trie.Root, t2.Root) + + self.state.trie = t2 + } +} + +func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value { + if int64(len(c.Code)-1) < pc.Int64() { + return ethutil.NewValue(0) + } + + return ethutil.NewValueFromBytes([]byte{c.Code[pc.Int64()]}) +} + +func (c *StateObject) AddAmount(amount *big.Int) { + c.SetAmount(new(big.Int).Add(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.Debugf("%x: #%d %v (- %v)\n", c.Address(), c.Nonce, c.Amount, amount) +} + +func (c *StateObject) SetAmount(amount *big.Int) { + c.Amount = amount +} + +// +// Gas setters and getters +// + +// Return the gas back to the origin. Used by the Virtual machine or Closures +func (c *StateObject) ReturnGas(gas, price *big.Int) {} +func (c *StateObject) ConvertGas(gas, price *big.Int) error { + total := new(big.Int).Mul(gas, price) + if total.Cmp(c.Amount) > 0 { + return fmt.Errorf("insufficient amount: %v, %v", c.Amount, total) + } + + c.SubAmount(total) + + return nil +} + +func (self *StateObject) SetGasPool(gasLimit *big.Int) { + self.gasPool = new(big.Int).Set(gasLimit) + + statelogger.DebugDetailf("%x: fuel (+ %v)", self.Address(), self.gasPool) +} + +func (self *StateObject) BuyGas(gas, price *big.Int) error { + if self.gasPool.Cmp(gas) < 0 { + return GasLimitError(self.gasPool, gas) + } + + rGas := new(big.Int).Set(gas) + rGas.Mul(rGas, price) + + self.AddAmount(rGas) + + return nil +} + +func (self *StateObject) RefundGas(gas, price *big.Int) { + self.gasPool.Add(self.gasPool, gas) + + rGas := new(big.Int).Set(gas) + rGas.Mul(rGas, price) + + self.Amount.Sub(self.Amount, rGas) +} + +func (self *StateObject) Copy() *StateObject { + stateObject := NewStateObject(self.Address()) + stateObject.Amount.Set(self.Amount) + stateObject.CodeHash = ethutil.CopyBytes(self.CodeHash) + stateObject.Nonce = self.Nonce + if self.state != nil { + stateObject.state = self.state.Copy() + } + stateObject.Code = ethutil.CopyBytes(self.Code) + stateObject.initCode = ethutil.CopyBytes(self.initCode) + stateObject.storage = self.storage.Copy() + stateObject.gasPool.Set(self.gasPool) + + return stateObject +} + +func (self *StateObject) Set(stateObject *StateObject) { + *self = *stateObject +} + +// +// Attribute accessors +// + +func (c *StateObject) State() *State { + return c.state +} + +func (c *StateObject) N() *big.Int { + return big.NewInt(int64(c.Nonce)) +} + +// Returns the address of the contract/account +func (c *StateObject) Address() []byte { + return c.address +} + +// Returns the initialization Code +func (c *StateObject) Init() Code { + return c.initCode +} + +// 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 +// + +// State object encoding methods +func (c *StateObject) RlpEncode() []byte { + var root interface{} + if c.state != nil { + root = c.state.trie.Root + } else { + root = "" + } + + return ethutil.Encode([]interface{}{c.Nonce, c.Amount, root, ethcrypto.Sha3Bin(c.Code)}) +} + +func (c *StateObject) RlpDecode(data []byte) { + decoder := ethutil.NewValueFromBytes(data) + + c.Nonce = decoder.Get(0).Uint() + 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.CodeHash = decoder.Get(3).Bytes() + + c.Code, _ = ethutil.Config.Db.Get(c.CodeHash) +} + +// Storage change object. Used by the manifest for notifying changes to +// the sub channels. +type StorageState struct { + StateAddress []byte + Address []byte + Value *big.Int +} -- cgit v1.2.3 From 490ca410c01a1b8076214d00c21d2edf09c24f86 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 22 Jul 2014 15:57:54 +0200 Subject: Minor improvements and fixes to the new vm structure --- ethstate/state_object.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ethstate/state_object.go') diff --git a/ethstate/state_object.go b/ethstate/state_object.go index d8513f37d..6b00c5369 100644 --- a/ethstate/state_object.go +++ b/ethstate/state_object.go @@ -11,7 +11,7 @@ import ( type Code []byte func (self Code) String() string { - return "" //strings.Join(Disassemble(self), " ") + return string(self) //strings.Join(Disassemble(self), " ") } type Storage map[string]*ethutil.Value -- cgit v1.2.3 From 32d125131f602d63f66ee7eb09439074f0b94a91 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 24 Jul 2014 12:04:15 +0200 Subject: Refactored to new state and vm --- ethstate/state_object.go | 44 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 24 deletions(-) (limited to 'ethstate/state_object.go') diff --git a/ethstate/state_object.go b/ethstate/state_object.go index 6b00c5369..ab14b8604 100644 --- a/ethstate/state_object.go +++ b/ethstate/state_object.go @@ -34,9 +34,9 @@ type StateObject struct { CodeHash []byte Nonce uint64 // Contract related attributes - state *State + State *State Code Code - initCode Code + InitCode Code storage Storage @@ -53,7 +53,7 @@ type StateObject struct { func (self *StateObject) Reset() { self.storage = make(Storage) - self.state.Reset() + self.State.Reset() } /* @@ -79,7 +79,7 @@ func NewStateObject(addr []byte) *StateObject { address := ethutil.Address(addr) object := &StateObject{address: address, Amount: new(big.Int), gasPool: new(big.Int)} - object.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, "")) + object.State = NewState(ethtrie.NewTrie(ethutil.Config.Db, "")) object.storage = make(Storage) object.gasPool = new(big.Int) @@ -89,7 +89,7 @@ func NewStateObject(addr []byte) *StateObject { func NewContract(address []byte, Amount *big.Int, root []byte) *StateObject { contract := NewStateObject(address) contract.Amount = Amount - contract.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, string(root))) + contract.State = NewState(ethtrie.NewTrie(ethutil.Config.Db, string(root))) return contract } @@ -107,11 +107,11 @@ func (self *StateObject) MarkForDeletion() { } func (c *StateObject) GetAddr(addr []byte) *ethutil.Value { - return ethutil.NewValueFromBytes([]byte(c.state.trie.Get(string(addr)))) + return ethutil.NewValueFromBytes([]byte(c.State.Trie.Get(string(addr)))) } func (c *StateObject) SetAddr(addr []byte, value interface{}) { - c.state.trie.Update(string(addr), string(ethutil.NewValue(value).Encode())) + c.State.Trie.Update(string(addr), string(ethutil.NewValue(value).Encode())) } func (self *StateObject) GetStorage(key *big.Int) *ethutil.Value { @@ -152,7 +152,7 @@ func (self *StateObject) EachStorage(cb ethtrie.EachCallback) { cb(key, encoded) } - it := self.state.trie.NewIterator() + 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 { @@ -166,18 +166,18 @@ func (self *StateObject) Sync() { 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)) + self.State.Trie.Delete(string(key)) continue } self.SetAddr([]byte(key), value) } - valid, t2 := ethtrie.ParanoiaCheck(self.state.trie) + valid, t2 := ethtrie.ParanoiaCheck(self.State.Trie) if !valid { - statelogger.Infof("Warn: PARANOIA: Different state storage root during copy %x vs %x\n", self.state.trie.Root, t2.Root) + statelogger.Infof("Warn: PARANOIA: Different state storage root during copy %x vs %x\n", self.State.Trie.Root, t2.Root) - self.state.trie = t2 + self.State.Trie = t2 } } @@ -255,11 +255,11 @@ func (self *StateObject) Copy() *StateObject { stateObject.Amount.Set(self.Amount) stateObject.CodeHash = ethutil.CopyBytes(self.CodeHash) stateObject.Nonce = self.Nonce - if self.state != nil { - stateObject.state = self.state.Copy() + if self.State != nil { + stateObject.State = self.State.Copy() } stateObject.Code = ethutil.CopyBytes(self.Code) - stateObject.initCode = ethutil.CopyBytes(self.initCode) + stateObject.InitCode = ethutil.CopyBytes(self.InitCode) stateObject.storage = self.storage.Copy() stateObject.gasPool.Set(self.gasPool) @@ -274,10 +274,6 @@ func (self *StateObject) Set(stateObject *StateObject) { // Attribute accessors // -func (c *StateObject) State() *State { - return c.state -} - func (c *StateObject) N() *big.Int { return big.NewInt(int64(c.Nonce)) } @@ -289,12 +285,12 @@ func (c *StateObject) Address() []byte { // Returns the initialization Code func (c *StateObject) Init() Code { - return c.initCode + return c.InitCode } // Debug stuff func (self *StateObject) CreateOutputForDiff() { - fmt.Printf("%x %x %x %x\n", self.Address(), self.state.Root(), self.Amount.Bytes(), self.Nonce) + 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()) }) @@ -307,8 +303,8 @@ func (self *StateObject) CreateOutputForDiff() { // State object encoding methods func (c *StateObject) RlpEncode() []byte { var root interface{} - if c.state != nil { - root = c.state.trie.Root + if c.State != nil { + root = c.State.Trie.Root } else { root = "" } @@ -321,7 +317,7 @@ func (c *StateObject) RlpDecode(data []byte) { c.Nonce = decoder.Get(0).Uint() c.Amount = decoder.Get(1).BigInt() - c.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, decoder.Get(2).Interface())) + c.State = NewState(ethtrie.NewTrie(ethutil.Config.Db, decoder.Get(2).Interface())) c.storage = make(map[string]*ethutil.Value) c.gasPool = new(big.Int) -- cgit v1.2.3 From 1f9894c0845a5259adbfd30fe3a86631e6403b8d Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 30 Jul 2014 00:31:15 +0200 Subject: Old code removed and renamed amount to balance --- ethstate/state_object.go | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) (limited to 'ethstate/state_object.go') diff --git a/ethstate/state_object.go b/ethstate/state_object.go index ab14b8604..5932fbee6 100644 --- a/ethstate/state_object.go +++ b/ethstate/state_object.go @@ -2,10 +2,11 @@ package ethstate import ( "fmt" + "math/big" + "github.com/ethereum/eth-go/ethcrypto" "github.com/ethereum/eth-go/ethtrie" "github.com/ethereum/eth-go/ethutil" - "math/big" ) type Code []byte @@ -30,7 +31,7 @@ type StateObject struct { // Address of the object address []byte // Shared attributes - Amount *big.Int + Balance *big.Int CodeHash []byte Nonce uint64 // Contract related attributes @@ -78,7 +79,7 @@ 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.Address(addr) - object := &StateObject{address: address, Amount: new(big.Int), gasPool: new(big.Int)} + object := &StateObject{address: address, Balance: new(big.Int), gasPool: new(big.Int)} object.State = NewState(ethtrie.NewTrie(ethutil.Config.Db, "")) object.storage = make(Storage) object.gasPool = new(big.Int) @@ -86,9 +87,9 @@ func NewStateObject(addr []byte) *StateObject { return object } -func NewContract(address []byte, Amount *big.Int, root []byte) *StateObject { +func NewContract(address []byte, balance *big.Int, root []byte) *StateObject { contract := NewStateObject(address) - contract.Amount = Amount + contract.Balance = balance contract.State = NewState(ethtrie.NewTrie(ethutil.Config.Db, string(root))) return contract @@ -103,7 +104,7 @@ func NewStateObjectFromBytes(address, data []byte) *StateObject { func (self *StateObject) MarkForDeletion() { self.remove = true - statelogger.DebugDetailf("%x: #%d %v (deletion)\n", self.Address(), self.Nonce, self.Amount) + statelogger.DebugDetailf("%x: #%d %v (deletion)\n", self.Address(), self.Nonce, self.Balance) } func (c *StateObject) GetAddr(addr []byte) *ethutil.Value { @@ -190,19 +191,19 @@ 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)) + c.SetBalance(new(big.Int).Add(c.Balance, amount)) - statelogger.Debugf("%x: #%d %v (+ %v)\n", c.Address(), c.Nonce, c.Amount, amount) + statelogger.Debugf("%x: #%d %v (+ %v)\n", c.Address(), c.Nonce, c.Balance, amount) } func (c *StateObject) SubAmount(amount *big.Int) { - c.SetAmount(new(big.Int).Sub(c.Amount, amount)) + c.SetBalance(new(big.Int).Sub(c.Balance, amount)) - statelogger.Debugf("%x: #%d %v (- %v)\n", c.Address(), c.Nonce, c.Amount, amount) + statelogger.Debugf("%x: #%d %v (- %v)\n", c.Address(), c.Nonce, c.Balance, amount) } -func (c *StateObject) SetAmount(amount *big.Int) { - c.Amount = amount +func (c *StateObject) SetBalance(amount *big.Int) { + c.Balance = amount } // @@ -213,8 +214,8 @@ func (c *StateObject) SetAmount(amount *big.Int) { func (c *StateObject) ReturnGas(gas, price *big.Int) {} func (c *StateObject) ConvertGas(gas, price *big.Int) error { total := new(big.Int).Mul(gas, price) - if total.Cmp(c.Amount) > 0 { - return fmt.Errorf("insufficient amount: %v, %v", c.Amount, total) + if total.Cmp(c.Balance) > 0 { + return fmt.Errorf("insufficient amount: %v, %v", c.Balance, total) } c.SubAmount(total) @@ -247,12 +248,12 @@ func (self *StateObject) RefundGas(gas, price *big.Int) { rGas := new(big.Int).Set(gas) rGas.Mul(rGas, price) - self.Amount.Sub(self.Amount, rGas) + self.Balance.Sub(self.Balance, rGas) } func (self *StateObject) Copy() *StateObject { stateObject := NewStateObject(self.Address()) - stateObject.Amount.Set(self.Amount) + stateObject.Balance.Set(self.Balance) stateObject.CodeHash = ethutil.CopyBytes(self.CodeHash) stateObject.Nonce = self.Nonce if self.State != nil { @@ -290,7 +291,7 @@ func (c *StateObject) Init() Code { // Debug stuff func (self *StateObject) CreateOutputForDiff() { - fmt.Printf("%x %x %x %x\n", self.Address(), self.State.Root(), self.Amount.Bytes(), self.Nonce) + fmt.Printf("%x %x %x %x\n", self.Address(), self.State.Root(), self.Balance.Bytes(), self.Nonce) self.EachStorage(func(addr string, value *ethutil.Value) { fmt.Printf("%x %x\n", addr, value.Bytes()) }) @@ -309,14 +310,14 @@ func (c *StateObject) RlpEncode() []byte { root = "" } - return ethutil.Encode([]interface{}{c.Nonce, c.Amount, root, ethcrypto.Sha3Bin(c.Code)}) + return ethutil.Encode([]interface{}{c.Nonce, c.Balance, root, ethcrypto.Sha3Bin(c.Code)}) } func (c *StateObject) RlpDecode(data []byte) { decoder := ethutil.NewValueFromBytes(data) c.Nonce = decoder.Get(0).Uint() - c.Amount = decoder.Get(1).BigInt() + c.Balance = 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) -- cgit v1.2.3 From 3debeb7236d2c8474fa9049cc91dc26bf1040b3f Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 4 Aug 2014 10:38:18 +0200 Subject: ethtrie.NewTrie => ethtrie.New --- ethstate/state_object.go | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) (limited to 'ethstate/state_object.go') diff --git a/ethstate/state_object.go b/ethstate/state_object.go index 5932fbee6..309e3e762 100644 --- a/ethstate/state_object.go +++ b/ethstate/state_object.go @@ -57,30 +57,12 @@ func (self *StateObject) Reset() { self.State.Reset() } -/* -// Converts an transaction in to a state object -func MakeContract(tx *Transaction, state *State) *StateObject { - // Create contract if there's no recipient - if tx.IsContract() { - addr := tx.CreationAddress() - - contract := state.NewStateObject(addr) - contract.initCode = tx.Data - contract.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, "")) - - return contract - } - - return nil -} -*/ - 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.Address(addr) object := &StateObject{address: address, Balance: new(big.Int), gasPool: new(big.Int)} - object.State = NewState(ethtrie.NewTrie(ethutil.Config.Db, "")) + object.State = NewState(ethtrie.New(ethutil.Config.Db, "")) object.storage = make(Storage) object.gasPool = new(big.Int) @@ -90,7 +72,7 @@ func NewStateObject(addr []byte) *StateObject { func NewContract(address []byte, balance *big.Int, root []byte) *StateObject { contract := NewStateObject(address) contract.Balance = balance - contract.State = NewState(ethtrie.NewTrie(ethutil.Config.Db, string(root))) + contract.State = NewState(ethtrie.New(ethutil.Config.Db, string(root))) return contract } @@ -318,7 +300,7 @@ func (c *StateObject) RlpDecode(data []byte) { c.Nonce = decoder.Get(0).Uint() c.Balance = decoder.Get(1).BigInt() - c.State = NewState(ethtrie.NewTrie(ethutil.Config.Db, decoder.Get(2).Interface())) + c.State = NewState(ethtrie.New(ethutil.Config.Db, decoder.Get(2).Interface())) c.storage = make(map[string]*ethutil.Value) c.gasPool = new(big.Int) -- cgit v1.2.3 From 03ce15df4c7ef51d4373233ab5c3766282b31771 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 4 Aug 2014 10:42:40 +0200 Subject: ethstate.NewState => ethstate.New --- ethstate/state_object.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'ethstate/state_object.go') diff --git a/ethstate/state_object.go b/ethstate/state_object.go index 309e3e762..67d09edd8 100644 --- a/ethstate/state_object.go +++ b/ethstate/state_object.go @@ -62,7 +62,7 @@ func NewStateObject(addr []byte) *StateObject { address := ethutil.Address(addr) object := &StateObject{address: address, Balance: new(big.Int), gasPool: new(big.Int)} - object.State = NewState(ethtrie.New(ethutil.Config.Db, "")) + object.State = New(ethtrie.New(ethutil.Config.Db, "")) object.storage = make(Storage) object.gasPool = new(big.Int) @@ -72,7 +72,7 @@ func NewStateObject(addr []byte) *StateObject { func NewContract(address []byte, balance *big.Int, root []byte) *StateObject { contract := NewStateObject(address) contract.Balance = balance - contract.State = NewState(ethtrie.New(ethutil.Config.Db, string(root))) + contract.State = New(ethtrie.New(ethutil.Config.Db, string(root))) return contract } @@ -300,7 +300,7 @@ func (c *StateObject) RlpDecode(data []byte) { c.Nonce = decoder.Get(0).Uint() c.Balance = decoder.Get(1).BigInt() - c.State = NewState(ethtrie.New(ethutil.Config.Db, decoder.Get(2).Interface())) + c.State = New(ethtrie.New(ethutil.Config.Db, decoder.Get(2).Interface())) c.storage = make(map[string]*ethutil.Value) c.gasPool = new(big.Int) -- cgit v1.2.3 From 3f904bf3acb5779f68834ebca95825ea1990f85b Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 25 Aug 2014 11:29:42 +0200 Subject: Implemented POST --- ethstate/state_object.go | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'ethstate/state_object.go') diff --git a/ethstate/state_object.go b/ethstate/state_object.go index 67d09edd8..bf4e92583 100644 --- a/ethstate/state_object.go +++ b/ethstate/state_object.go @@ -245,6 +245,7 @@ func (self *StateObject) Copy() *StateObject { stateObject.InitCode = ethutil.CopyBytes(self.InitCode) stateObject.storage = self.storage.Copy() stateObject.gasPool.Set(self.gasPool) + stateObject.remove = self.remove return stateObject } @@ -271,6 +272,11 @@ func (c *StateObject) Init() Code { return c.InitCode } +// To satisfy ClosureRef +func (self *StateObject) Object() *StateObject { + return self +} + // Debug stuff func (self *StateObject) CreateOutputForDiff() { fmt.Printf("%x %x %x %x\n", self.Address(), self.State.Root(), self.Balance.Bytes(), self.Nonce) -- cgit v1.2.3 From d91357d00ca080c63d81570504e5c45fb15e3841 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 8 Sep 2014 00:50:04 +0200 Subject: Added GetCode method --- ethstate/state_object.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'ethstate/state_object.go') diff --git a/ethstate/state_object.go b/ethstate/state_object.go index bf4e92583..6fc0696a8 100644 --- a/ethstate/state_object.go +++ b/ethstate/state_object.go @@ -297,8 +297,12 @@ func (c *StateObject) RlpEncode() []byte { } else { root = "" } + var codeHash []byte + if len(c.Code) > 0 { + codeHash = ethcrypto.Sha3Bin(c.Code) + } - return ethutil.Encode([]interface{}{c.Nonce, c.Balance, root, ethcrypto.Sha3Bin(c.Code)}) + return ethutil.Encode([]interface{}{c.Nonce, c.Balance, root, codeHash}) } func (c *StateObject) RlpDecode(data []byte) { -- cgit v1.2.3 From 33a0dec8a157b9687ca6038f4deb011f3f1f7bdc Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 15 Sep 2014 15:42:12 +0200 Subject: Improved catching up and refactored --- ethstate/state_object.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'ethstate/state_object.go') diff --git a/ethstate/state_object.go b/ethstate/state_object.go index 6fc0696a8..be083e80a 100644 --- a/ethstate/state_object.go +++ b/ethstate/state_object.go @@ -32,7 +32,7 @@ type StateObject struct { address []byte // Shared attributes Balance *big.Int - CodeHash []byte + codeHash []byte Nonce uint64 // Contract related attributes State *State @@ -236,7 +236,7 @@ func (self *StateObject) RefundGas(gas, price *big.Int) { func (self *StateObject) Copy() *StateObject { stateObject := NewStateObject(self.Address()) stateObject.Balance.Set(self.Balance) - stateObject.CodeHash = ethutil.CopyBytes(self.CodeHash) + stateObject.codeHash = ethutil.CopyBytes(self.codeHash) stateObject.Nonce = self.Nonce if self.State != nil { stateObject.State = self.State.Copy() @@ -297,12 +297,17 @@ func (c *StateObject) RlpEncode() []byte { } else { root = "" } + + return ethutil.Encode([]interface{}{c.Nonce, c.Balance, root, c.CodeHash()}) +} + +func (c *StateObject) CodeHash() ethutil.Bytes { var codeHash []byte if len(c.Code) > 0 { codeHash = ethcrypto.Sha3Bin(c.Code) } - return ethutil.Encode([]interface{}{c.Nonce, c.Balance, root, codeHash}) + return codeHash } func (c *StateObject) RlpDecode(data []byte) { @@ -314,9 +319,9 @@ func (c *StateObject) RlpDecode(data []byte) { c.storage = make(map[string]*ethutil.Value) c.gasPool = new(big.Int) - c.CodeHash = decoder.Get(3).Bytes() + c.codeHash = decoder.Get(3).Bytes() - c.Code, _ = ethutil.Config.Db.Get(c.CodeHash) + c.Code, _ = ethutil.Config.Db.Get(c.codeHash) } // Storage change object. Used by the manifest for notifying changes to -- cgit v1.2.3 From 9d86a49a7327199c01977f3372c8adf748252c32 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 8 Oct 2014 12:06:39 +0200 Subject: Renamed Sha3Bin to Sha3 --- ethstate/state_object.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ethstate/state_object.go') diff --git a/ethstate/state_object.go b/ethstate/state_object.go index be083e80a..fe4c5f73b 100644 --- a/ethstate/state_object.go +++ b/ethstate/state_object.go @@ -304,7 +304,7 @@ func (c *StateObject) RlpEncode() []byte { func (c *StateObject) CodeHash() ethutil.Bytes { var codeHash []byte if len(c.Code) > 0 { - codeHash = ethcrypto.Sha3Bin(c.Code) + codeHash = ethcrypto.Sha3(c.Code) } return codeHash -- cgit v1.2.3 From 311c6f8a3fed5ac03ee4b442fd0f420072bc41b4 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 15 Oct 2014 17:12:26 +0200 Subject: Fixed remote Arithmetic tests --- ethstate/state_object.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'ethstate/state_object.go') diff --git a/ethstate/state_object.go b/ethstate/state_object.go index fe4c5f73b..4d2aae1a7 100644 --- a/ethstate/state_object.go +++ b/ethstate/state_object.go @@ -98,13 +98,13 @@ func (c *StateObject) SetAddr(addr []byte, value interface{}) { } func (self *StateObject) GetStorage(key *big.Int) *ethutil.Value { - return self.getStorage(key.Bytes()) + return self.GetState(key.Bytes()) } func (self *StateObject) SetStorage(key *big.Int, value *ethutil.Value) { - self.setStorage(key.Bytes(), value) + self.SetState(key.Bytes(), value) } -func (self *StateObject) getStorage(k []byte) *ethutil.Value { +func (self *StateObject) GetState(k []byte) *ethutil.Value { key := ethutil.LeftPadBytes(k, 32) value := self.storage[string(key)] @@ -117,11 +117,9 @@ func (self *StateObject) getStorage(k []byte) *ethutil.Value { } return value - - //return self.GetAddr(key) } -func (self *StateObject) setStorage(k []byte, value *ethutil.Value) { +func (self *StateObject) SetState(k []byte, value *ethutil.Value) { key := ethutil.LeftPadBytes(k, 32) self.storage[string(key)] = value.Copy() } -- cgit v1.2.3 From 70f7a0be1187cc0e487e7b95cad238c6530d29ae Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 16 Oct 2014 13:38:21 +0200 Subject: Use the state instead of the state object directly. If a state gets reset and you still hold a pointer to the previous, incorrect, state object you'll operate on the wrong object. Using the state to set/get objects and attributes you won't have this problem since the state will always have the correct object. --- ethstate/state_object.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'ethstate/state_object.go') diff --git a/ethstate/state_object.go b/ethstate/state_object.go index 4d2aae1a7..a5b7c65e9 100644 --- a/ethstate/state_object.go +++ b/ethstate/state_object.go @@ -104,6 +104,10 @@ func (self *StateObject) SetStorage(key *big.Int, value *ethutil.Value) { self.SetState(key.Bytes(), value) } +func (self *StateObject) Storage() map[string]*ethutil.Value { + return self.storage +} + func (self *StateObject) GetState(k []byte) *ethutil.Value { key := ethutil.LeftPadBytes(k, 32) -- cgit v1.2.3 From b5beb1aac11af92bfe0f3ed7560b9eb08495ed09 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 22 Oct 2014 15:22:21 +0200 Subject: added a transfer method to vm env --- ethstate/state_object.go | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'ethstate/state_object.go') diff --git a/ethstate/state_object.go b/ethstate/state_object.go index a5b7c65e9..1ba005439 100644 --- a/ethstate/state_object.go +++ b/ethstate/state_object.go @@ -31,7 +31,7 @@ type StateObject struct { // Address of the object address []byte // Shared attributes - Balance *big.Int + balance *big.Int codeHash []byte Nonce uint64 // Contract related attributes @@ -61,7 +61,7 @@ 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.Address(addr) - object := &StateObject{address: address, Balance: new(big.Int), gasPool: new(big.Int)} + object := &StateObject{address: address, balance: new(big.Int), gasPool: new(big.Int)} object.State = New(ethtrie.New(ethutil.Config.Db, "")) object.storage = make(Storage) object.gasPool = new(big.Int) @@ -71,7 +71,7 @@ func NewStateObject(addr []byte) *StateObject { func NewContract(address []byte, balance *big.Int, root []byte) *StateObject { contract := NewStateObject(address) - contract.Balance = balance + contract.balance = balance contract.State = New(ethtrie.New(ethutil.Config.Db, string(root))) return contract @@ -86,7 +86,7 @@ func NewStateObjectFromBytes(address, data []byte) *StateObject { func (self *StateObject) MarkForDeletion() { self.remove = true - statelogger.DebugDetailf("%x: #%d %v (deletion)\n", self.Address(), self.Nonce, self.Balance) + statelogger.DebugDetailf("%x: #%d %v (deletion)\n", self.Address(), self.Nonce, self.balance) } func (c *StateObject) GetAddr(addr []byte) *ethutil.Value { @@ -174,22 +174,26 @@ func (c *StateObject) GetInstr(pc *big.Int) *ethutil.Value { return ethutil.NewValueFromBytes([]byte{c.Code[pc.Int64()]}) } -func (c *StateObject) AddAmount(amount *big.Int) { - c.SetBalance(new(big.Int).Add(c.Balance, amount)) +func (c *StateObject) AddBalance(amount *big.Int) { + c.SetBalance(new(big.Int).Add(c.balance, amount)) - statelogger.Debugf("%x: #%d %v (+ %v)\n", c.Address(), c.Nonce, c.Balance, amount) + statelogger.Debugf("%x: #%d %v (+ %v)\n", c.Address(), c.Nonce, c.balance, amount) } +func (c *StateObject) AddAmount(amount *big.Int) { c.AddBalance(amount) } -func (c *StateObject) SubAmount(amount *big.Int) { - c.SetBalance(new(big.Int).Sub(c.Balance, amount)) +func (c *StateObject) SubBalance(amount *big.Int) { + c.SetBalance(new(big.Int).Sub(c.balance, amount)) - statelogger.Debugf("%x: #%d %v (- %v)\n", c.Address(), c.Nonce, c.Balance, amount) + statelogger.Debugf("%x: #%d %v (- %v)\n", c.Address(), c.Nonce, c.balance, amount) } +func (c *StateObject) SubAmount(amount *big.Int) { c.SubBalance(amount) } func (c *StateObject) SetBalance(amount *big.Int) { - c.Balance = amount + c.balance = amount } +func (self *StateObject) Balance() *big.Int { return self.balance } + // // Gas setters and getters // @@ -198,8 +202,8 @@ func (c *StateObject) SetBalance(amount *big.Int) { func (c *StateObject) ReturnGas(gas, price *big.Int) {} func (c *StateObject) ConvertGas(gas, price *big.Int) error { total := new(big.Int).Mul(gas, price) - if total.Cmp(c.Balance) > 0 { - return fmt.Errorf("insufficient amount: %v, %v", c.Balance, total) + if total.Cmp(c.balance) > 0 { + return fmt.Errorf("insufficient amount: %v, %v", c.balance, total) } c.SubAmount(total) @@ -232,12 +236,12 @@ func (self *StateObject) RefundGas(gas, price *big.Int) { rGas := new(big.Int).Set(gas) rGas.Mul(rGas, price) - self.Balance.Sub(self.Balance, rGas) + self.balance.Sub(self.balance, rGas) } func (self *StateObject) Copy() *StateObject { stateObject := NewStateObject(self.Address()) - stateObject.Balance.Set(self.Balance) + stateObject.balance.Set(self.balance) stateObject.codeHash = ethutil.CopyBytes(self.codeHash) stateObject.Nonce = self.Nonce if self.State != nil { @@ -281,7 +285,7 @@ func (self *StateObject) Object() *StateObject { // Debug stuff func (self *StateObject) CreateOutputForDiff() { - fmt.Printf("%x %x %x %x\n", self.Address(), self.State.Root(), self.Balance.Bytes(), self.Nonce) + fmt.Printf("%x %x %x %x\n", self.Address(), self.State.Root(), self.balance.Bytes(), self.Nonce) self.EachStorage(func(addr string, value *ethutil.Value) { fmt.Printf("%x %x\n", addr, value.Bytes()) }) @@ -300,7 +304,7 @@ func (c *StateObject) RlpEncode() []byte { root = "" } - return ethutil.Encode([]interface{}{c.Nonce, c.Balance, root, c.CodeHash()}) + return ethutil.Encode([]interface{}{c.Nonce, c.balance, root, c.CodeHash()}) } func (c *StateObject) CodeHash() ethutil.Bytes { @@ -316,7 +320,7 @@ func (c *StateObject) RlpDecode(data []byte) { decoder := ethutil.NewValueFromBytes(data) c.Nonce = decoder.Get(0).Uint() - c.Balance = decoder.Get(1).BigInt() + c.balance = decoder.Get(1).BigInt() c.State = New(ethtrie.New(ethutil.Config.Db, decoder.Get(2).Interface())) c.storage = make(map[string]*ethutil.Value) c.gasPool = new(big.Int) -- cgit v1.2.3