aboutsummaryrefslogtreecommitdiffstats
path: root/state
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-03-23 23:59:09 +0800
committerobscuren <geffobscura@gmail.com>2015-03-23 23:59:09 +0800
commit0330077d76b48934ab024a309000f83c78047d8a (patch)
tree2a3ffbcd5bd941b30ed28d0eb5c30553a25324e0 /state
parentd7eaa97a297151637af090ecb05bbd6d260d90b8 (diff)
downloaddexon-0330077d76b48934ab024a309000f83c78047d8a.tar
dexon-0330077d76b48934ab024a309000f83c78047d8a.tar.gz
dexon-0330077d76b48934ab024a309000f83c78047d8a.tar.bz2
dexon-0330077d76b48934ab024a309000f83c78047d8a.tar.lz
dexon-0330077d76b48934ab024a309000f83c78047d8a.tar.xz
dexon-0330077d76b48934ab024a309000f83c78047d8a.tar.zst
dexon-0330077d76b48934ab024a309000f83c78047d8a.zip
moved state and vm to core
Diffstat (limited to 'state')
-rw-r--r--state/dump.go62
-rw-r--r--state/errors.go23
-rw-r--r--state/log.go99
-rw-r--r--state/main_test.go9
-rw-r--r--state/managed_state.go89
-rw-r--r--state/managed_state_test.go89
-rw-r--r--state/state_object.go357
-rw-r--r--state/state_test.go106
-rw-r--r--state/statedb.go325
9 files changed, 0 insertions, 1159 deletions
diff --git a/state/dump.go b/state/dump.go
deleted file mode 100644
index 712f8da1f..000000000
--- a/state/dump.go
+++ /dev/null
@@ -1,62 +0,0 @@
-package state
-
-import (
- "encoding/json"
- "fmt"
-
- "github.com/ethereum/go-ethereum/common"
-)
-
-type Account struct {
- Balance string `json:"balance"`
- Nonce uint64 `json:"nonce"`
- Root string `json:"root"`
- CodeHash string `json:"codeHash"`
- Storage map[string]string `json:"storage"`
-}
-
-type World struct {
- Root string `json:"root"`
- Accounts map[string]Account `json:"accounts"`
-}
-
-func (self *StateDB) RawDump() World {
- world := World{
- Root: common.Bytes2Hex(self.trie.Root()),
- Accounts: make(map[string]Account),
- }
-
- it := self.trie.Iterator()
- for it.Next() {
- stateObject := NewStateObjectFromBytes(common.BytesToAddress(it.Key), it.Value, self.db)
-
- account := Account{Balance: stateObject.balance.String(), Nonce: stateObject.nonce, Root: common.Bytes2Hex(stateObject.Root()), CodeHash: common.Bytes2Hex(stateObject.codeHash)}
- account.Storage = make(map[string]string)
-
- storageIt := stateObject.State.trie.Iterator()
- for storageIt.Next() {
- fmt.Println("value", storageIt.Value)
- account.Storage[common.Bytes2Hex(storageIt.Key)] = common.Bytes2Hex(storageIt.Value)
- }
- world.Accounts[common.Bytes2Hex(it.Key)] = account
- }
- return world
-}
-
-func (self *StateDB) Dump() []byte {
- json, err := json.MarshalIndent(self.RawDump(), "", " ")
- if err != nil {
- fmt.Println("dump err", err)
- }
-
- return json
-}
-
-// Debug stuff
-func (self *StateObject) CreateOutputForDiff() {
- fmt.Printf("%x %x %x %x\n", self.Address(), self.State.Root(), self.balance.Bytes(), self.nonce)
- it := self.State.trie.Iterator()
- for it.Next() {
- fmt.Printf("%x %x\n", it.Key, it.Value)
- }
-}
diff --git a/state/errors.go b/state/errors.go
deleted file mode 100644
index 5a847d38b..000000000
--- a/state/errors.go
+++ /dev/null
@@ -1,23 +0,0 @@
-package state
-
-import (
- "fmt"
- "math/big"
-)
-
-type GasLimitErr struct {
- Message string
- Is, Max *big.Int
-}
-
-func IsGasLimitErr(err error) bool {
- _, ok := err.(*GasLimitErr)
-
- return ok
-}
-func (err *GasLimitErr) Error() string {
- return err.Message
-}
-func GasLimitError(is, max *big.Int) *GasLimitErr {
- return &GasLimitErr{Message: fmt.Sprintf("GasLimit error. Max %s, transaction would take it to %s", max, is), Is: is, Max: max}
-}
diff --git a/state/log.go b/state/log.go
deleted file mode 100644
index f8aa4c08c..000000000
--- a/state/log.go
+++ /dev/null
@@ -1,99 +0,0 @@
-package state
-
-import (
- "fmt"
- "io"
-
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/rlp"
-)
-
-type Log interface {
- Address() common.Address
- Topics() []common.Hash
- Data() []byte
-
- Number() uint64
-}
-
-type StateLog struct {
- address common.Address
- topics []common.Hash
- data []byte
- number uint64
-}
-
-func NewLog(address common.Address, topics []common.Hash, data []byte, number uint64) *StateLog {
- return &StateLog{address, topics, data, number}
-}
-
-func (self *StateLog) Address() common.Address {
- return self.address
-}
-
-func (self *StateLog) Topics() []common.Hash {
- return self.topics
-}
-
-func (self *StateLog) Data() []byte {
- return self.data
-}
-
-func (self *StateLog) Number() uint64 {
- return self.number
-}
-
-/*
-func NewLogFromValue(decoder *common.Value) *StateLog {
- var extlog struct {
-
- }
-
- log := &StateLog{
- address: decoder.Get(0).Bytes(),
- data: decoder.Get(2).Bytes(),
- }
-
- it := decoder.Get(1).NewIterator()
- for it.Next() {
- log.topics = append(log.topics, it.Value().Bytes())
- }
-
- return log
-}
-*/
-
-func (self *StateLog) EncodeRLP(w io.Writer) error {
- return rlp.Encode(w, []interface{}{self.address, self.topics, self.data})
-}
-
-/*
-func (self *StateLog) RlpData() interface{} {
- return []interface{}{self.address, common.ByteSliceToInterface(self.topics), self.data}
-}
-*/
-
-func (self *StateLog) String() string {
- return fmt.Sprintf(`log: %x %x %x`, self.address, self.topics, self.data)
-}
-
-type Logs []Log
-
-/*
-func (self Logs) RlpData() interface{} {
- data := make([]interface{}, len(self))
- for i, log := range self {
- data[i] = log.RlpData()
- }
-
- return data
-}
-*/
-
-func (self Logs) String() (ret string) {
- for _, log := range self {
- ret += fmt.Sprintf("%v", log)
- }
-
- return "[" + ret + "]"
-}
diff --git a/state/main_test.go b/state/main_test.go
deleted file mode 100644
index f3d3f7e23..000000000
--- a/state/main_test.go
+++ /dev/null
@@ -1,9 +0,0 @@
-package state
-
-import (
- "testing"
-
- checker "gopkg.in/check.v1"
-)
-
-func Test(t *testing.T) { checker.TestingT(t) }
diff --git a/state/managed_state.go b/state/managed_state.go
deleted file mode 100644
index 0fcc1be67..000000000
--- a/state/managed_state.go
+++ /dev/null
@@ -1,89 +0,0 @@
-package state
-
-import (
- "sync"
-
- "github.com/ethereum/go-ethereum/common"
-)
-
-type account struct {
- stateObject *StateObject
- nstart uint64
- nonces []bool
-}
-
-type ManagedState struct {
- *StateDB
-
- mu sync.RWMutex
-
- accounts map[string]*account
-}
-
-func ManageState(statedb *StateDB) *ManagedState {
- return &ManagedState{
- StateDB: statedb,
- accounts: make(map[string]*account),
- }
-}
-
-func (ms *ManagedState) SetState(statedb *StateDB) {
- ms.mu.Lock()
- defer ms.mu.Unlock()
- ms.StateDB = statedb
-}
-
-func (ms *ManagedState) RemoveNonce(addr common.Address, n uint64) {
- if ms.hasAccount(addr) {
- ms.mu.Lock()
- defer ms.mu.Unlock()
-
- account := ms.getAccount(addr)
- if n-account.nstart <= uint64(len(account.nonces)) {
- reslice := make([]bool, n-account.nstart)
- copy(reslice, account.nonces[:n-account.nstart])
- account.nonces = reslice
- }
- }
-}
-
-func (ms *ManagedState) NewNonce(addr common.Address) uint64 {
- ms.mu.RLock()
- defer ms.mu.RUnlock()
-
- account := ms.getAccount(addr)
- for i, nonce := range account.nonces {
- if !nonce {
- return account.nstart + uint64(i)
- }
- }
- account.nonces = append(account.nonces, true)
- return uint64(len(account.nonces)) + account.nstart
-}
-
-func (ms *ManagedState) hasAccount(addr common.Address) bool {
- _, ok := ms.accounts[addr.Str()]
- return ok
-}
-
-func (ms *ManagedState) getAccount(addr common.Address) *account {
- straddr := addr.Str()
- if account, ok := ms.accounts[straddr]; !ok {
- so := ms.GetOrNewStateObject(addr)
- ms.accounts[straddr] = newAccount(so)
- } else {
- // Always make sure the state account nonce isn't actually higher
- // than the tracked one.
- so := ms.StateDB.GetStateObject(addr)
- if so != nil && uint64(len(account.nonces))+account.nstart < so.nonce {
- ms.accounts[straddr] = newAccount(so)
- }
-
- }
-
- return ms.accounts[straddr]
-}
-
-func newAccount(so *StateObject) *account {
- return &account{so, so.nonce - 1, nil}
-}
diff --git a/state/managed_state_test.go b/state/managed_state_test.go
deleted file mode 100644
index b61f59e6d..000000000
--- a/state/managed_state_test.go
+++ /dev/null
@@ -1,89 +0,0 @@
-package state
-
-import (
- "testing"
-
- "github.com/ethereum/go-ethereum/common"
-)
-
-var addr = common.BytesToAddress([]byte("test"))
-
-func create() (*ManagedState, *account) {
- ms := ManageState(&StateDB{stateObjects: make(map[string]*StateObject)})
- so := &StateObject{address: addr, nonce: 100}
- ms.StateDB.stateObjects[addr.Str()] = so
- ms.accounts[addr.Str()] = newAccount(so)
-
- return ms, ms.accounts[addr.Str()]
-}
-
-func TestNewNonce(t *testing.T) {
- ms, _ := create()
-
- nonce := ms.NewNonce(addr)
- if nonce != 100 {
- t.Error("expected nonce 100. got", nonce)
- }
-
- nonce = ms.NewNonce(addr)
- if nonce != 101 {
- t.Error("expected nonce 101. got", nonce)
- }
-}
-
-func TestRemove(t *testing.T) {
- ms, account := create()
-
- nn := make([]bool, 10)
- for i, _ := range nn {
- nn[i] = true
- }
- account.nonces = append(account.nonces, nn...)
-
- i := uint64(5)
- ms.RemoveNonce(addr, account.nstart+i)
- if len(account.nonces) != 5 {
- t.Error("expected", i, "'th index to be false")
- }
-}
-
-func TestReuse(t *testing.T) {
- ms, account := create()
-
- nn := make([]bool, 10)
- for i, _ := range nn {
- nn[i] = true
- }
- account.nonces = append(account.nonces, nn...)
-
- i := uint64(5)
- ms.RemoveNonce(addr, account.nstart+i)
- nonce := ms.NewNonce(addr)
- if nonce != 105 {
- t.Error("expected nonce to be 105. got", nonce)
- }
-}
-
-func TestRemoteNonceChange(t *testing.T) {
- ms, account := create()
- nn := make([]bool, 10)
- for i, _ := range nn {
- nn[i] = true
- }
- account.nonces = append(account.nonces, nn...)
- nonce := ms.NewNonce(addr)
-
- ms.StateDB.stateObjects[addr.Str()].nonce = 200
- nonce = ms.NewNonce(addr)
- if nonce != 200 {
- t.Error("expected nonce after remote update to be", 201, "got", nonce)
- }
- ms.NewNonce(addr)
- ms.NewNonce(addr)
- ms.NewNonce(addr)
- ms.StateDB.stateObjects[addr.Str()].nonce = 200
- nonce = ms.NewNonce(addr)
- if nonce != 204 {
- t.Error("expected nonce after remote update to be", 201, "got", nonce)
- }
-}
diff --git a/state/state_object.go b/state/state_object.go
deleted file mode 100644
index a7c20722c..000000000
--- a/state/state_object.go
+++ /dev/null
@@ -1,357 +0,0 @@
-package state
-
-import (
- "bytes"
- "fmt"
- "math/big"
-
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/crypto"
- "github.com/ethereum/go-ethereum/rlp"
- "github.com/ethereum/go-ethereum/trie"
-)
-
-type Code []byte
-
-func (self Code) String() string {
- return string(self) //strings.Join(Disassemble(self), " ")
-}
-
-type Storage map[string]*common.Value
-
-func (self Storage) String() (str string) {
- for key, value := range self {
- str += fmt.Sprintf("%X : %X\n", key, value.Bytes())
- }
-
- return
-}
-
-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 {
- // State database for storing state changes
- db common.Database
- // The state object
- State *StateDB
-
- // Address belonging to this account
- address common.Address
- // The balance of the account
- balance *big.Int
- // The nonce of the account
- nonce uint64
- // The code hash if code is present (i.e. a contract)
- codeHash []byte
- // The code for this account
- code Code
- // Temporarily initialisation code
- initCode Code
- // Cached storage (flushed when updated)
- storage Storage
- // Temporary prepaid gas, reward after transition
- prepaid *big.Int
-
- // 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
- dirty bool
-}
-
-func (self *StateObject) Reset() {
- self.storage = make(Storage)
- self.State.Reset()
-}
-
-func NewStateObject(address common.Address, db common.Database) *StateObject {
- // This to ensure that it has 20 bytes (and not 0 bytes), thus left or right pad doesn't matter.
- //address := common.ToAddress(addr)
-
- object := &StateObject{db: db, address: address, balance: new(big.Int), gasPool: new(big.Int), dirty: true}
- object.State = New(common.Hash{}, db) //New(trie.New(common.Config.Db, ""))
- object.storage = make(Storage)
- object.gasPool = new(big.Int)
- object.prepaid = new(big.Int)
-
- return object
-}
-
-func NewStateObjectFromBytes(address common.Address, data []byte, db common.Database) *StateObject {
- // TODO clean me up
- var extobject struct {
- Nonce uint64
- Balance *big.Int
- Root common.Hash
- CodeHash []byte
- }
- err := rlp.Decode(bytes.NewReader(data), &extobject)
- if err != nil {
- fmt.Println(err)
- return nil
- }
-
- object := &StateObject{address: address, db: db}
- //object.RlpDecode(data)
- object.nonce = extobject.Nonce
- object.balance = extobject.Balance
- object.codeHash = extobject.CodeHash
- object.State = New(extobject.Root, db)
- object.storage = make(map[string]*common.Value)
- object.gasPool = new(big.Int)
- object.prepaid = new(big.Int)
- object.code, _ = db.Get(extobject.CodeHash)
-
- return object
-}
-
-func (self *StateObject) MarkForDeletion() {
- self.remove = true
- self.dirty = true
- statelogger.Debugf("%x: #%d %v X\n", self.Address(), self.nonce, self.balance)
-}
-
-func (c *StateObject) getAddr(addr common.Hash) *common.Value {
- return common.NewValueFromBytes([]byte(c.State.trie.Get(addr[:])))
-}
-
-func (c *StateObject) setAddr(addr []byte, value interface{}) {
- c.State.trie.Update(addr, common.Encode(value))
-}
-
-func (self *StateObject) GetStorage(key *big.Int) *common.Value {
- return self.GetState(common.BytesToHash(key.Bytes()))
-}
-func (self *StateObject) SetStorage(key *big.Int, value *common.Value) {
- self.SetState(common.BytesToHash(key.Bytes()), value)
-}
-
-func (self *StateObject) Storage() Storage {
- return self.storage
-}
-
-func (self *StateObject) GetState(key common.Hash) *common.Value {
- strkey := key.Str()
- value := self.storage[strkey]
- if value == nil {
- value = self.getAddr(key)
-
- if !value.IsNil() {
- self.storage[strkey] = value
- }
- }
-
- return value
-}
-
-func (self *StateObject) SetState(k common.Hash, value *common.Value) {
- self.storage[k.Str()] = value.Copy()
- self.dirty = true
-}
-
-func (self *StateObject) Sync() {
- for key, value := range self.storage {
- if value.Len() == 0 {
- self.State.trie.Delete([]byte(key))
- continue
- }
-
- self.setAddr([]byte(key), value)
- }
- self.storage = make(Storage)
-}
-
-func (c *StateObject) GetInstr(pc *big.Int) *common.Value {
- if int64(len(c.code)-1) < pc.Int64() {
- return common.NewValue(0)
- }
-
- return common.NewValueFromBytes([]byte{c.code[pc.Int64()]})
-}
-
-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)
-}
-
-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)
-}
-
-func (c *StateObject) SetBalance(amount *big.Int) {
- c.balance = amount
- c.dirty = true
-}
-
-func (c *StateObject) St() Storage {
- return c.storage
-}
-
-//
-// 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.balance) > 0 {
- return fmt.Errorf("insufficient amount: %v, %v", c.balance, total)
- }
-
- c.SubBalance(total)
-
- c.dirty = true
-
- return nil
-}
-
-func (self *StateObject) SetGasPool(gasLimit *big.Int) {
- self.gasPool = new(big.Int).Set(gasLimit)
-
- statelogger.Debugf("%x: gas (+ %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)
- }
-
- self.gasPool.Sub(self.gasPool, gas)
-
- rGas := new(big.Int).Set(gas)
- rGas.Mul(rGas, price)
-
- self.dirty = true
-
- return nil
-}
-
-func (self *StateObject) RefundGas(gas, price *big.Int) {
- self.gasPool.Add(self.gasPool, gas)
-}
-
-func (self *StateObject) Copy() *StateObject {
- stateObject := NewStateObject(self.Address(), self.db)
- stateObject.balance.Set(self.balance)
- stateObject.codeHash = common.CopyBytes(self.codeHash)
- stateObject.nonce = self.nonce
- if self.State != nil {
- stateObject.State = self.State.Copy()
- }
- stateObject.code = common.CopyBytes(self.code)
- stateObject.initCode = common.CopyBytes(self.initCode)
- stateObject.storage = self.storage.Copy()
- stateObject.gasPool.Set(self.gasPool)
- stateObject.remove = self.remove
- stateObject.dirty = self.dirty
-
- return stateObject
-}
-
-func (self *StateObject) Set(stateObject *StateObject) {
- *self = *stateObject
-}
-
-//
-// Attribute accessors
-//
-
-func (self *StateObject) Balance() *big.Int {
- return self.balance
-}
-
-func (c *StateObject) N() *big.Int {
- return big.NewInt(int64(c.nonce))
-}
-
-// Returns the address of the contract/account
-func (c *StateObject) Address() common.Address {
- return c.address
-}
-
-// Returns the initialization Code
-func (c *StateObject) Init() Code {
- return c.initCode
-}
-
-func (self *StateObject) Trie() *trie.SecureTrie {
- return self.State.trie
-}
-
-func (self *StateObject) Root() []byte {
- return self.Trie().Root()
-}
-
-func (self *StateObject) Code() []byte {
- return self.code
-}
-
-func (self *StateObject) SetCode(code []byte) {
- self.code = code
- self.dirty = true
-}
-
-func (self *StateObject) SetInitCode(code []byte) {
- self.initCode = code
- self.dirty = true
-}
-
-func (self *StateObject) SetNonce(nonce uint64) {
- self.nonce = nonce
- self.dirty = true
-}
-
-func (self *StateObject) Nonce() uint64 {
- return self.nonce
-}
-
-//
-// Encoding
-//
-
-// State object encoding methods
-func (c *StateObject) RlpEncode() []byte {
- return common.Encode([]interface{}{c.nonce, c.balance, c.Root(), c.CodeHash()})
-}
-
-func (c *StateObject) CodeHash() common.Bytes {
- return crypto.Sha3(c.code)
-}
-
-func (c *StateObject) RlpDecode(data []byte) {
- decoder := common.NewValueFromBytes(data)
- c.nonce = decoder.Get(0).Uint()
- c.balance = decoder.Get(1).BigInt()
- c.State = New(common.BytesToHash(decoder.Get(2).Bytes()), c.db) //New(trie.New(common.Config.Db, decoder.Get(2).Interface()))
- c.storage = make(map[string]*common.Value)
- c.gasPool = new(big.Int)
-
- c.codeHash = decoder.Get(3).Bytes()
-
- c.code, _ = c.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
-}
diff --git a/state/state_test.go b/state/state_test.go
deleted file mode 100644
index a3d3973de..000000000
--- a/state/state_test.go
+++ /dev/null
@@ -1,106 +0,0 @@
-package state
-
-import (
- "math/big"
- "testing"
-
- checker "gopkg.in/check.v1"
-
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/ethdb"
-)
-
-type StateSuite struct {
- state *StateDB
-}
-
-var _ = checker.Suite(&StateSuite{})
-
-var toAddr = common.BytesToAddress
-
-func (s *StateSuite) TestDump(c *checker.C) {
- return
- // generate a few entries
- obj1 := s.state.GetOrNewStateObject(toAddr([]byte{0x01}))
- obj1.AddBalance(big.NewInt(22))
- obj2 := s.state.GetOrNewStateObject(toAddr([]byte{0x01, 0x02}))
- obj2.SetCode([]byte{3, 3, 3, 3, 3, 3, 3})
- obj3 := s.state.GetOrNewStateObject(toAddr([]byte{0x02}))
- obj3.SetBalance(big.NewInt(44))
-
- // write some of them to the trie
- s.state.UpdateStateObject(obj1)
- s.state.UpdateStateObject(obj2)
-
- // check that dump contains the state objects that are in trie
- got := string(s.state.Dump())
- want := `{
- "root": "6e277ae8357d013e50f74eedb66a991f6922f93ae03714de58b3d0c5e9eee53f",
- "accounts": {
- "1468288056310c82aa4c01a7e12a10f8111a0560e72b700555479031b86c357d": {
- "balance": "22",
- "nonce": 0,
- "root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
- "codeHash": "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
- "storage": {}
- },
- "a17eacbc25cda025e81db9c5c62868822c73ce097cee2a63e33a2e41268358a1": {
- "balance": "0",
- "nonce": 0,
- "root": "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
- "codeHash": "87874902497a5bb968da31a2998d8f22e949d1ef6214bcdedd8bae24cca4b9e3",
- "storage": {}
- }
- }
-}`
- if got != want {
- c.Errorf("dump mismatch:\ngot: %s\nwant: %s\n", got, want)
- }
-}
-
-func (s *StateSuite) SetUpTest(c *checker.C) {
- db, _ := ethdb.NewMemDatabase()
- s.state = New(common.Hash{}, db)
-}
-
-func TestNull(t *testing.T) {
- db, _ := ethdb.NewMemDatabase()
- state := New(common.Hash{}, db)
-
- address := common.HexToAddress("0x823140710bf13990e4500136726d8b55")
- state.NewStateObject(address)
- //value := common.FromHex("0x823140710bf13990e4500136726d8b55")
- value := make([]byte, 16)
- state.SetState(address, common.Hash{}, value)
- state.Update(nil)
- state.Sync()
- value = state.GetState(address, common.Hash{})
-}
-
-func (s *StateSuite) TestSnapshot(c *checker.C) {
- stateobjaddr := toAddr([]byte("aa"))
- storageaddr := common.Big("0")
- data1 := common.NewValue(42)
- data2 := common.NewValue(43)
-
- // get state object
- stateObject := s.state.GetOrNewStateObject(stateobjaddr)
- // set inital state object value
- stateObject.SetStorage(storageaddr, data1)
- // get snapshot of current state
- snapshot := s.state.Copy()
-
- // get state object. is this strictly necessary?
- stateObject = s.state.GetStateObject(stateobjaddr)
- // set new state object value
- stateObject.SetStorage(storageaddr, data2)
- // restore snapshot
- s.state.Set(snapshot)
-
- // get state object
- stateObject = s.state.GetStateObject(stateobjaddr)
- // get state storage value
- res := stateObject.GetStorage(storageaddr)
-
- c.Assert(data1, checker.DeepEquals, res)
-}
diff --git a/state/statedb.go b/state/statedb.go
deleted file mode 100644
index 6fcd39dbc..000000000
--- a/state/statedb.go
+++ /dev/null
@@ -1,325 +0,0 @@
-package state
-
-import (
- "bytes"
- "math/big"
-
- "github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/logger"
- "github.com/ethereum/go-ethereum/trie"
-)
-
-var statelogger = logger.NewLogger("STATE")
-
-// StateDBs within the ethereum protocol are used to store anything
-// within the merkle trie. StateDBs take care of caching and storing
-// nested states. It's the general query interface to retrieve:
-// * Contracts
-// * Accounts
-type StateDB struct {
- db common.Database
- trie *trie.SecureTrie
-
- stateObjects map[string]*StateObject
-
- refund map[string]*big.Int
-
- logs Logs
-}
-
-// Create a new state from a given trie
-func New(root common.Hash, db common.Database) *StateDB {
- trie := trie.NewSecure(root[:], db)
- return &StateDB{db: db, trie: trie, stateObjects: make(map[string]*StateObject), refund: make(map[string]*big.Int)}
-}
-
-func (self *StateDB) PrintRoot() {
- self.trie.Trie.PrintRoot()
-}
-
-func (self *StateDB) EmptyLogs() {
- self.logs = nil
-}
-
-func (self *StateDB) AddLog(log Log) {
- self.logs = append(self.logs, log)
-}
-
-func (self *StateDB) Logs() Logs {
- return self.logs
-}
-
-func (self *StateDB) Refund(address common.Address, gas *big.Int) {
- addr := address.Str()
- if self.refund[addr] == nil {
- self.refund[addr] = new(big.Int)
- }
- self.refund[addr].Add(self.refund[addr], gas)
-}
-
-// Retrieve the balance from the given address or 0 if object not found
-func (self *StateDB) GetBalance(addr common.Address) *big.Int {
- stateObject := self.GetStateObject(addr)
- if stateObject != nil {
- return stateObject.balance
- }
-
- return common.Big0
-}
-
-func (self *StateDB) AddBalance(addr common.Address, amount *big.Int) {
- stateObject := self.GetStateObject(addr)
- if stateObject != nil {
- stateObject.AddBalance(amount)
- }
-}
-
-func (self *StateDB) GetNonce(addr common.Address) uint64 {
- stateObject := self.GetStateObject(addr)
- if stateObject != nil {
- return stateObject.nonce
- }
-
- return 0
-}
-
-func (self *StateDB) GetCode(addr common.Address) []byte {
- stateObject := self.GetStateObject(addr)
- if stateObject != nil {
- return stateObject.code
- }
-
- return nil
-}
-
-func (self *StateDB) GetState(a common.Address, b common.Hash) []byte {
- stateObject := self.GetStateObject(a)
- if stateObject != nil {
- return stateObject.GetState(b).Bytes()
- }
-
- return nil
-}
-
-func (self *StateDB) SetNonce(addr common.Address, nonce uint64) {
- stateObject := self.GetStateObject(addr)
- if stateObject != nil {
- stateObject.SetNonce(nonce)
- }
-}
-
-func (self *StateDB) SetCode(addr common.Address, code []byte) {
- stateObject := self.GetStateObject(addr)
- if stateObject != nil {
- stateObject.SetCode(code)
- }
-}
-
-func (self *StateDB) SetState(addr common.Address, key common.Hash, value interface{}) {
- stateObject := self.GetStateObject(addr)
- if stateObject != nil {
- stateObject.SetState(key, common.NewValue(value))
- }
-}
-
-func (self *StateDB) Delete(addr common.Address) bool {
- stateObject := self.GetStateObject(addr)
- if stateObject != nil {
- stateObject.MarkForDeletion()
- stateObject.balance = new(big.Int)
-
- return true
- }
-
- return false
-}
-
-func (self *StateDB) IsDeleted(addr common.Address) bool {
- stateObject := self.GetStateObject(addr)
- if stateObject != nil {
- return stateObject.remove
- }
- return false
-}
-
-//
-// Setting, updating & deleting state object methods
-//
-
-// Update the given state object and apply it to state trie
-func (self *StateDB) UpdateStateObject(stateObject *StateObject) {
- //addr := stateObject.Address()
-
- if len(stateObject.CodeHash()) > 0 {
- self.db.Put(stateObject.CodeHash(), stateObject.code)
- }
-
- addr := stateObject.Address()
- self.trie.Update(addr[:], stateObject.RlpEncode())
-}
-
-// Delete the given state object and delete it from the state trie
-func (self *StateDB) DeleteStateObject(stateObject *StateObject) {
- addr := stateObject.Address()
- self.trie.Delete(addr[:])
-
- delete(self.stateObjects, addr.Str())
-}
-
-// Retrieve a state object given my the address. Nil if not found
-func (self *StateDB) GetStateObject(addr common.Address) *StateObject {
- //addr = common.Address(addr)
-
- stateObject := self.stateObjects[addr.Str()]
- if stateObject != nil {
- return stateObject
- }
-
- data := self.trie.Get(addr[:])
- if len(data) == 0 {
- return nil
- }
-
- stateObject = NewStateObjectFromBytes(addr, []byte(data), self.db)
- self.SetStateObject(stateObject)
-
- return stateObject
-}
-
-func (self *StateDB) SetStateObject(object *StateObject) {
- self.stateObjects[object.Address().Str()] = object
-}
-
-// Retrieve a state object or create a new state object if nil
-func (self *StateDB) GetOrNewStateObject(addr common.Address) *StateObject {
- stateObject := self.GetStateObject(addr)
- if stateObject == nil {
- stateObject = self.NewStateObject(addr)
- }
-
- return stateObject
-}
-
-// Create a state object whether it exist in the trie or not
-func (self *StateDB) NewStateObject(addr common.Address) *StateObject {
- //addr = common.Address(addr)
-
- statelogger.Debugf("(+) %x\n", addr)
-
- stateObject := NewStateObject(addr, self.db)
- self.stateObjects[addr.Str()] = stateObject
-
- return stateObject
-}
-
-// Deprecated
-func (self *StateDB) GetAccount(addr common.Address) *StateObject {
- return self.GetOrNewStateObject(addr)
-}
-
-//
-// Setting, copying of the state methods
-//
-
-func (s *StateDB) Cmp(other *StateDB) bool {
- return bytes.Equal(s.trie.Root(), other.trie.Root())
-}
-
-func (self *StateDB) Copy() *StateDB {
- state := New(common.Hash{}, self.db)
- state.trie = self.trie.Copy()
- for k, stateObject := range self.stateObjects {
- state.stateObjects[k] = stateObject.Copy()
- }
-
- for addr, refund := range self.refund {
- state.refund[addr] = new(big.Int).Set(refund)
- }
-
- logs := make(Logs, len(self.logs))
- copy(logs, self.logs)
- state.logs = logs
-
- return state
-}
-
-func (self *StateDB) Set(state *StateDB) {
- self.trie = state.trie
- self.stateObjects = state.stateObjects
-
- self.refund = state.refund
- self.logs = state.logs
-}
-
-func (s *StateDB) Root() common.Hash {
- return common.BytesToHash(s.trie.Root())
-}
-
-func (s *StateDB) Trie() *trie.SecureTrie {
- return s.trie
-}
-
-// Resets the trie and all siblings
-func (s *StateDB) Reset() {
- s.trie.Reset()
-
- // Reset all nested states
- for _, stateObject := range s.stateObjects {
- if stateObject.State == nil {
- continue
- }
-
- stateObject.Reset()
- }
-
- s.Empty()
-}
-
-// Syncs the trie and all siblings
-func (s *StateDB) Sync() {
- // Sync all nested states
- for _, stateObject := range s.stateObjects {
- if stateObject.State == nil {
- continue
- }
-
- stateObject.State.Sync()
- }
-
- s.trie.Commit()
-
- s.Empty()
-}
-
-func (self *StateDB) Empty() {
- self.stateObjects = make(map[string]*StateObject)
- self.refund = make(map[string]*big.Int)
-}
-
-func (self *StateDB) Refunds() map[string]*big.Int {
- return self.refund
-}
-
-func (self *StateDB) Update(gasUsed *big.Int) {
- self.refund = make(map[string]*big.Int)
-
- for _, stateObject := range self.stateObjects {
- if stateObject.dirty {
- if stateObject.remove {
- self.DeleteStateObject(stateObject)
- } else {
- stateObject.Sync()
-
- self.UpdateStateObject(stateObject)
- }
- stateObject.dirty = false
- }
- }
-}
-
-// Debug stuff
-func (self *StateDB) CreateOutputForDiff() {
- for _, stateObject := range self.stateObjects {
- stateObject.CreateOutputForDiff()
- }
-}