mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
core/type: Using uniformed receiver name
This commit is contained in:
parent
4e6f53ac33
commit
c1e955fb3b
4 changed files with 302 additions and 302 deletions
|
|
@ -39,15 +39,15 @@ type Dump struct {
|
||||||
Accounts map[string]DumpAccount `json:"accounts"`
|
Accounts map[string]DumpAccount `json:"accounts"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateDB) RawDump() Dump {
|
func (db *StateDB) RawDump() Dump {
|
||||||
dump := Dump{
|
dump := Dump{
|
||||||
Root: fmt.Sprintf("%x", self.trie.Hash()),
|
Root: fmt.Sprintf("%x", db.trie.Hash()),
|
||||||
Accounts: make(map[string]DumpAccount),
|
Accounts: make(map[string]DumpAccount),
|
||||||
}
|
}
|
||||||
|
|
||||||
it := trie.NewIterator(self.trie.NodeIterator(nil))
|
it := trie.NewIterator(db.trie.NodeIterator(nil))
|
||||||
for it.Next() {
|
for it.Next() {
|
||||||
addr := self.trie.GetKey(it.Key)
|
addr := db.trie.GetKey(it.Key)
|
||||||
var data Account
|
var data Account
|
||||||
if err := rlp.DecodeBytes(it.Value, &data); err != nil {
|
if err := rlp.DecodeBytes(it.Value, &data); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
@ -59,20 +59,20 @@ func (self *StateDB) RawDump() Dump {
|
||||||
Nonce: data.Nonce,
|
Nonce: data.Nonce,
|
||||||
Root: common.Bytes2Hex(data.Root[:]),
|
Root: common.Bytes2Hex(data.Root[:]),
|
||||||
CodeHash: common.Bytes2Hex(data.CodeHash),
|
CodeHash: common.Bytes2Hex(data.CodeHash),
|
||||||
Code: common.Bytes2Hex(obj.Code(self.db)),
|
Code: common.Bytes2Hex(obj.Code(db.db)),
|
||||||
Storage: make(map[string]string),
|
Storage: make(map[string]string),
|
||||||
}
|
}
|
||||||
storageIt := trie.NewIterator(obj.getTrie(self.db).NodeIterator(nil))
|
storageIt := trie.NewIterator(obj.getTrie(db.db).NodeIterator(nil))
|
||||||
for storageIt.Next() {
|
for storageIt.Next() {
|
||||||
account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value)
|
account.Storage[common.Bytes2Hex(db.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value)
|
||||||
}
|
}
|
||||||
dump.Accounts[common.Bytes2Hex(addr)] = account
|
dump.Accounts[common.Bytes2Hex(addr)] = account
|
||||||
}
|
}
|
||||||
return dump
|
return dump
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateDB) Dump() []byte {
|
func (db *StateDB) Dump() []byte {
|
||||||
json, err := json.MarshalIndent(self.RawDump(), "", " ")
|
json, err := json.MarshalIndent(db.RawDump(), "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("dump err", err)
|
fmt.Println("dump err", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,23 +31,23 @@ var emptyCodeHash = crypto.Keccak256(nil)
|
||||||
|
|
||||||
type Code []byte
|
type Code []byte
|
||||||
|
|
||||||
func (self Code) String() string {
|
func (c Code) String() string {
|
||||||
return string(self) //strings.Join(Disassemble(self), " ")
|
return string(c) //strings.Join(Disassemble(s), " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
type Storage map[common.Hash]common.Hash
|
type Storage map[common.Hash]common.Hash
|
||||||
|
|
||||||
func (self Storage) String() (str string) {
|
func (s Storage) String() (str string) {
|
||||||
for key, value := range self {
|
for key, value := range s {
|
||||||
str += fmt.Sprintf("%X : %X\n", key, value)
|
str += fmt.Sprintf("%X : %X\n", key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Storage) Copy() Storage {
|
func (s Storage) Copy() Storage {
|
||||||
cpy := make(Storage)
|
cpy := make(Storage)
|
||||||
for key, value := range self {
|
for key, value := range s {
|
||||||
cpy[key] = value
|
cpy[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -121,192 +121,192 @@ func newObject(db *StateDB, address common.Address, data Account) *stateObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodeRLP implements rlp.Encoder.
|
// EncodeRLP implements rlp.Encoder.
|
||||||
func (c *stateObject) EncodeRLP(w io.Writer) error {
|
func (s *stateObject) EncodeRLP(w io.Writer) error {
|
||||||
return rlp.Encode(w, c.data)
|
return rlp.Encode(w, s.data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// setError remembers the first non-nil error it is called with.
|
// setError remembers the first non-nil error it is called with.
|
||||||
func (self *stateObject) setError(err error) {
|
func (s *stateObject) setError(err error) {
|
||||||
if self.dbErr == nil {
|
if s.dbErr == nil {
|
||||||
self.dbErr = err
|
s.dbErr = err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *stateObject) markSuicided() {
|
func (s *stateObject) markSuicided() {
|
||||||
self.suicided = true
|
s.suicided = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *stateObject) touch() {
|
func (s *stateObject) touch() {
|
||||||
c.db.journal.append(touchChange{
|
s.db.journal.append(touchChange{
|
||||||
account: &c.address,
|
account: &s.address,
|
||||||
})
|
})
|
||||||
if c.address == ripemd {
|
if s.address == ripemd {
|
||||||
// Explicitly put it in the dirty-cache, which is otherwise generated from
|
// Explicitly put it in the dirty-cache, which is otherwise generated from
|
||||||
// flattened journals.
|
// flattened journals.
|
||||||
c.db.journal.dirty(c.address)
|
s.db.journal.dirty(s.address)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *stateObject) getTrie(db Database) Trie {
|
func (s *stateObject) getTrie(db Database) Trie {
|
||||||
if c.trie == nil {
|
if s.trie == nil {
|
||||||
var err error
|
var err error
|
||||||
c.trie, err = db.OpenStorageTrie(c.addrHash, c.data.Root)
|
s.trie, err = db.OpenStorageTrie(s.addrHash, s.data.Root)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.trie, _ = db.OpenStorageTrie(c.addrHash, common.Hash{})
|
s.trie, _ = db.OpenStorageTrie(s.addrHash, common.Hash{})
|
||||||
c.setError(fmt.Errorf("can't create storage trie: %v", err))
|
s.setError(fmt.Errorf("can't create storage trie: %v", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return c.trie
|
return s.trie
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetState retrieves a value from the account storage trie.
|
// GetState retrieves a value from the account storage trie.
|
||||||
func (self *stateObject) GetState(db Database, key common.Hash) common.Hash {
|
func (s *stateObject) GetState(db Database, key common.Hash) common.Hash {
|
||||||
// If we have a dirty value for this state entry, return it
|
// If we have a dirty value for this state entry, return it
|
||||||
value, dirty := self.dirtyStorage[key]
|
value, dirty := s.dirtyStorage[key]
|
||||||
if dirty {
|
if dirty {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
// Otherwise return the entry's original value
|
// Otherwise return the entry's original value
|
||||||
return self.GetCommittedState(db, key)
|
return s.GetCommittedState(db, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCommittedState retrieves a value from the committed account storage trie.
|
// GetCommittedState retrieves a value from the committed account storage trie.
|
||||||
func (self *stateObject) GetCommittedState(db Database, key common.Hash) common.Hash {
|
func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Hash {
|
||||||
// If we have the original value cached, return that
|
// If we have the original value cached, return that
|
||||||
value, cached := self.originStorage[key]
|
value, cached := s.originStorage[key]
|
||||||
if cached {
|
if cached {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
// Otherwise load the value from the database
|
// Otherwise load the value from the database
|
||||||
enc, err := self.getTrie(db).TryGet(key[:])
|
enc, err := s.getTrie(db).TryGet(key[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
self.setError(err)
|
s.setError(err)
|
||||||
return common.Hash{}
|
return common.Hash{}
|
||||||
}
|
}
|
||||||
if len(enc) > 0 {
|
if len(enc) > 0 {
|
||||||
_, content, _, err := rlp.Split(enc)
|
_, content, _, err := rlp.Split(enc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
self.setError(err)
|
s.setError(err)
|
||||||
}
|
}
|
||||||
value.SetBytes(content)
|
value.SetBytes(content)
|
||||||
}
|
}
|
||||||
self.originStorage[key] = value
|
s.originStorage[key] = value
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetState updates a value in account storage.
|
// SetState updates a value in account storage.
|
||||||
func (self *stateObject) SetState(db Database, key, value common.Hash) {
|
func (s *stateObject) SetState(db Database, key, value common.Hash) {
|
||||||
// If the new value is the same as old, don't set
|
// If the new value is the same as old, don't set
|
||||||
prev := self.GetState(db, key)
|
prev := s.GetState(db, key)
|
||||||
if prev == value {
|
if prev == value {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// New value is different, update and journal the change
|
// New value is different, update and journal the change
|
||||||
self.db.journal.append(storageChange{
|
s.db.journal.append(storageChange{
|
||||||
account: &self.address,
|
account: &s.address,
|
||||||
key: key,
|
key: key,
|
||||||
prevalue: prev,
|
prevalue: prev,
|
||||||
})
|
})
|
||||||
self.setState(key, value)
|
s.setState(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *stateObject) setState(key, value common.Hash) {
|
func (s *stateObject) setState(key, value common.Hash) {
|
||||||
self.dirtyStorage[key] = value
|
s.dirtyStorage[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateTrie writes cached storage modifications into the object's storage trie.
|
// updateTrie writes cached storage modifications into the object's storage trie.
|
||||||
func (self *stateObject) updateTrie(db Database) Trie {
|
func (s *stateObject) updateTrie(db Database) Trie {
|
||||||
tr := self.getTrie(db)
|
tr := s.getTrie(db)
|
||||||
for key, value := range self.dirtyStorage {
|
for key, value := range s.dirtyStorage {
|
||||||
delete(self.dirtyStorage, key)
|
delete(s.dirtyStorage, key)
|
||||||
|
|
||||||
// Skip noop changes, persist actual changes
|
// Skip noop changes, persist actual changes
|
||||||
if value == self.originStorage[key] {
|
if value == s.originStorage[key] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
self.originStorage[key] = value
|
s.originStorage[key] = value
|
||||||
|
|
||||||
if (value == common.Hash{}) {
|
if (value == common.Hash{}) {
|
||||||
self.setError(tr.TryDelete(key[:]))
|
s.setError(tr.TryDelete(key[:]))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Encoding []byte cannot fail, ok to ignore the error.
|
// Encoding []byte cannot fail, ok to ignore the error.
|
||||||
v, _ := rlp.EncodeToBytes(bytes.TrimLeft(value[:], "\x00"))
|
v, _ := rlp.EncodeToBytes(bytes.TrimLeft(value[:], "\x00"))
|
||||||
self.setError(tr.TryUpdate(key[:], v))
|
s.setError(tr.TryUpdate(key[:], v))
|
||||||
}
|
}
|
||||||
return tr
|
return tr
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateRoot sets the trie root to the current root hash of
|
// UpdateRoot sets the trie root to the current root hash of
|
||||||
func (self *stateObject) updateRoot(db Database) {
|
func (s *stateObject) updateRoot(db Database) {
|
||||||
self.updateTrie(db)
|
s.updateTrie(db)
|
||||||
self.data.Root = self.trie.Hash()
|
s.data.Root = s.trie.Hash()
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommitTrie the storage trie of the object to db.
|
// CommitTrie the storage trie of the object to db.
|
||||||
// This updates the trie root.
|
// This updates the trie root.
|
||||||
func (self *stateObject) CommitTrie(db Database) error {
|
func (s *stateObject) CommitTrie(db Database) error {
|
||||||
self.updateTrie(db)
|
s.updateTrie(db)
|
||||||
if self.dbErr != nil {
|
if s.dbErr != nil {
|
||||||
return self.dbErr
|
return s.dbErr
|
||||||
}
|
}
|
||||||
root, err := self.trie.Commit(nil)
|
root, err := s.trie.Commit(nil)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
self.data.Root = root
|
s.data.Root = root
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddBalance removes amount from c's balance.
|
// AddBalance removes amount from c's balance.
|
||||||
// It is used to add funds to the destination account of a transfer.
|
// It is used to add funds to the destination account of a transfer.
|
||||||
func (c *stateObject) AddBalance(amount *big.Int) {
|
func (s *stateObject) AddBalance(amount *big.Int) {
|
||||||
// EIP158: We must check emptiness for the objects such that the account
|
// EIP158: We must check emptiness for the objects such that the account
|
||||||
// clearing (0,0,0 objects) can take effect.
|
// clearing (0,0,0 objects) can take effect.
|
||||||
if amount.Sign() == 0 {
|
if amount.Sign() == 0 {
|
||||||
if c.empty() {
|
if s.empty() {
|
||||||
c.touch()
|
s.touch()
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.SetBalance(new(big.Int).Add(c.Balance(), amount))
|
s.SetBalance(new(big.Int).Add(s.Balance(), amount))
|
||||||
}
|
}
|
||||||
|
|
||||||
// SubBalance removes amount from c's balance.
|
// SubBalance removes amount from c's balance.
|
||||||
// It is used to remove funds from the origin account of a transfer.
|
// It is used to remove funds from the origin account of a transfer.
|
||||||
func (c *stateObject) SubBalance(amount *big.Int) {
|
func (s *stateObject) SubBalance(amount *big.Int) {
|
||||||
if amount.Sign() == 0 {
|
if amount.Sign() == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.SetBalance(new(big.Int).Sub(c.Balance(), amount))
|
s.SetBalance(new(big.Int).Sub(s.Balance(), amount))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *stateObject) SetBalance(amount *big.Int) {
|
func (s *stateObject) SetBalance(amount *big.Int) {
|
||||||
self.db.journal.append(balanceChange{
|
s.db.journal.append(balanceChange{
|
||||||
account: &self.address,
|
account: &s.address,
|
||||||
prev: new(big.Int).Set(self.data.Balance),
|
prev: new(big.Int).Set(s.data.Balance),
|
||||||
})
|
})
|
||||||
self.setBalance(amount)
|
s.setBalance(amount)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *stateObject) setBalance(amount *big.Int) {
|
func (s *stateObject) setBalance(amount *big.Int) {
|
||||||
self.data.Balance = amount
|
s.data.Balance = amount
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the gas back to the origin. Used by the Virtual machine or Closures
|
// Return the gas back to the origin. Used by the Virtual machine or Closures
|
||||||
func (c *stateObject) ReturnGas(gas *big.Int) {}
|
func (s *stateObject) ReturnGas(gas *big.Int) {}
|
||||||
|
|
||||||
func (self *stateObject) deepCopy(db *StateDB) *stateObject {
|
func (s *stateObject) deepCopy(db *StateDB) *stateObject {
|
||||||
stateObject := newObject(db, self.address, self.data)
|
stateObject := newObject(db, s.address, s.data)
|
||||||
if self.trie != nil {
|
if s.trie != nil {
|
||||||
stateObject.trie = db.db.CopyTrie(self.trie)
|
stateObject.trie = db.db.CopyTrie(s.trie)
|
||||||
}
|
}
|
||||||
stateObject.code = self.code
|
stateObject.code = s.code
|
||||||
stateObject.dirtyStorage = self.dirtyStorage.Copy()
|
stateObject.dirtyStorage = s.dirtyStorage.Copy()
|
||||||
stateObject.originStorage = self.originStorage.Copy()
|
stateObject.originStorage = s.originStorage.Copy()
|
||||||
stateObject.suicided = self.suicided
|
stateObject.suicided = s.suicided
|
||||||
stateObject.dirtyCode = self.dirtyCode
|
stateObject.dirtyCode = s.dirtyCode
|
||||||
stateObject.deleted = self.deleted
|
stateObject.deleted = s.deleted
|
||||||
return stateObject
|
return stateObject
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -315,69 +315,69 @@ func (self *stateObject) deepCopy(db *StateDB) *stateObject {
|
||||||
//
|
//
|
||||||
|
|
||||||
// Returns the address of the contract/account
|
// Returns the address of the contract/account
|
||||||
func (c *stateObject) Address() common.Address {
|
func (s *stateObject) Address() common.Address {
|
||||||
return c.address
|
return s.address
|
||||||
}
|
}
|
||||||
|
|
||||||
// Code returns the contract code associated with this object, if any.
|
// Code returns the contract code associated with this object, if any.
|
||||||
func (self *stateObject) Code(db Database) []byte {
|
func (s *stateObject) Code(db Database) []byte {
|
||||||
if self.code != nil {
|
if s.code != nil {
|
||||||
return self.code
|
return s.code
|
||||||
}
|
}
|
||||||
if bytes.Equal(self.CodeHash(), emptyCodeHash) {
|
if bytes.Equal(s.CodeHash(), emptyCodeHash) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
code, err := db.ContractCode(self.addrHash, common.BytesToHash(self.CodeHash()))
|
code, err := db.ContractCode(s.addrHash, common.BytesToHash(s.CodeHash()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
self.setError(fmt.Errorf("can't load code hash %x: %v", self.CodeHash(), err))
|
s.setError(fmt.Errorf("can't load code hash %x: %v", s.CodeHash(), err))
|
||||||
}
|
}
|
||||||
self.code = code
|
s.code = code
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *stateObject) SetCode(codeHash common.Hash, code []byte) {
|
func (s *stateObject) SetCode(codeHash common.Hash, code []byte) {
|
||||||
prevcode := self.Code(self.db.db)
|
prevcode := s.Code(s.db.db)
|
||||||
self.db.journal.append(codeChange{
|
s.db.journal.append(codeChange{
|
||||||
account: &self.address,
|
account: &s.address,
|
||||||
prevhash: self.CodeHash(),
|
prevhash: s.CodeHash(),
|
||||||
prevcode: prevcode,
|
prevcode: prevcode,
|
||||||
})
|
})
|
||||||
self.setCode(codeHash, code)
|
s.setCode(codeHash, code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *stateObject) setCode(codeHash common.Hash, code []byte) {
|
func (s *stateObject) setCode(codeHash common.Hash, code []byte) {
|
||||||
self.code = code
|
s.code = code
|
||||||
self.data.CodeHash = codeHash[:]
|
s.data.CodeHash = codeHash[:]
|
||||||
self.dirtyCode = true
|
s.dirtyCode = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *stateObject) SetNonce(nonce uint64) {
|
func (s *stateObject) SetNonce(nonce uint64) {
|
||||||
self.db.journal.append(nonceChange{
|
s.db.journal.append(nonceChange{
|
||||||
account: &self.address,
|
account: &s.address,
|
||||||
prev: self.data.Nonce,
|
prev: s.data.Nonce,
|
||||||
})
|
})
|
||||||
self.setNonce(nonce)
|
s.setNonce(nonce)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *stateObject) setNonce(nonce uint64) {
|
func (s *stateObject) setNonce(nonce uint64) {
|
||||||
self.data.Nonce = nonce
|
s.data.Nonce = nonce
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *stateObject) CodeHash() []byte {
|
func (s *stateObject) CodeHash() []byte {
|
||||||
return self.data.CodeHash
|
return s.data.CodeHash
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *stateObject) Balance() *big.Int {
|
func (s *stateObject) Balance() *big.Int {
|
||||||
return self.data.Balance
|
return s.data.Balance
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *stateObject) Nonce() uint64 {
|
func (s *stateObject) Nonce() uint64 {
|
||||||
return self.data.Nonce
|
return s.data.Nonce
|
||||||
}
|
}
|
||||||
|
|
||||||
// Never called, but must be present to allow stateObject to be used
|
// Never called, but must be present to allow stateObject to be used
|
||||||
// as a vm.Account interface that also satisfies the vm.ContractRef
|
// as a vm.Account interface that also satisfies the vm.ContractRef
|
||||||
// interface. Interfaces are awesome.
|
// interface. Interfaces are awesome.
|
||||||
func (self *stateObject) Value() *big.Int {
|
func (s *stateObject) Value() *big.Int {
|
||||||
panic("Value on stateObject should never be called")
|
panic("Value on stateObject should never be called")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -106,114 +106,114 @@ func New(root common.Hash, db Database) (*StateDB, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// setError remembers the first non-nil error it is called with.
|
// setError remembers the first non-nil error it is called with.
|
||||||
func (self *StateDB) setError(err error) {
|
func (db *StateDB) setError(err error) {
|
||||||
if self.dbErr == nil {
|
if db.dbErr == nil {
|
||||||
self.dbErr = err
|
db.dbErr = err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateDB) Error() error {
|
func (db *StateDB) Error() error {
|
||||||
return self.dbErr
|
return db.dbErr
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset clears out all ephemeral state objects from the state db, but keeps
|
// Reset clears out all ephemeral state objects from the state db, but keeps
|
||||||
// the underlying state trie to avoid reloading data for the next operations.
|
// the underlying state trie to avoid reloading data for the next operations.
|
||||||
func (self *StateDB) Reset(root common.Hash) error {
|
func (db *StateDB) Reset(root common.Hash) error {
|
||||||
tr, err := self.db.OpenTrie(root)
|
tr, err := db.db.OpenTrie(root)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
self.trie = tr
|
db.trie = tr
|
||||||
self.stateObjects = make(map[common.Address]*stateObject)
|
db.stateObjects = make(map[common.Address]*stateObject)
|
||||||
self.stateObjectsDirty = make(map[common.Address]struct{})
|
db.stateObjectsDirty = make(map[common.Address]struct{})
|
||||||
self.thash = common.Hash{}
|
db.thash = common.Hash{}
|
||||||
self.bhash = common.Hash{}
|
db.bhash = common.Hash{}
|
||||||
self.txIndex = 0
|
db.txIndex = 0
|
||||||
self.logs = make(map[common.Hash][]*types.Log)
|
db.logs = make(map[common.Hash][]*types.Log)
|
||||||
self.logSize = 0
|
db.logSize = 0
|
||||||
self.preimages = make(map[common.Hash][]byte)
|
db.preimages = make(map[common.Hash][]byte)
|
||||||
self.clearJournalAndRefund()
|
db.clearJournalAndRefund()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateDB) AddLog(log *types.Log) {
|
func (db *StateDB) AddLog(log *types.Log) {
|
||||||
self.journal.append(addLogChange{txhash: self.thash})
|
db.journal.append(addLogChange{txhash: db.thash})
|
||||||
|
|
||||||
log.TxHash = self.thash
|
log.TxHash = db.thash
|
||||||
log.BlockHash = self.bhash
|
log.BlockHash = db.bhash
|
||||||
log.TxIndex = uint(self.txIndex)
|
log.TxIndex = uint(db.txIndex)
|
||||||
log.Index = self.logSize
|
log.Index = db.logSize
|
||||||
self.logs[self.thash] = append(self.logs[self.thash], log)
|
db.logs[db.thash] = append(db.logs[db.thash], log)
|
||||||
self.logSize++
|
db.logSize++
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateDB) GetLogs(hash common.Hash) []*types.Log {
|
func (db *StateDB) GetLogs(hash common.Hash) []*types.Log {
|
||||||
return self.logs[hash]
|
return db.logs[hash]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateDB) Logs() []*types.Log {
|
func (db *StateDB) Logs() []*types.Log {
|
||||||
var logs []*types.Log
|
var logs []*types.Log
|
||||||
for _, lgs := range self.logs {
|
for _, lgs := range db.logs {
|
||||||
logs = append(logs, lgs...)
|
logs = append(logs, lgs...)
|
||||||
}
|
}
|
||||||
return logs
|
return logs
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddPreimage records a SHA3 preimage seen by the VM.
|
// AddPreimage records a SHA3 preimage seen by the VM.
|
||||||
func (self *StateDB) AddPreimage(hash common.Hash, preimage []byte) {
|
func (db *StateDB) AddPreimage(hash common.Hash, preimage []byte) {
|
||||||
if _, ok := self.preimages[hash]; !ok {
|
if _, ok := db.preimages[hash]; !ok {
|
||||||
self.journal.append(addPreimageChange{hash: hash})
|
db.journal.append(addPreimageChange{hash: hash})
|
||||||
pi := make([]byte, len(preimage))
|
pi := make([]byte, len(preimage))
|
||||||
copy(pi, preimage)
|
copy(pi, preimage)
|
||||||
self.preimages[hash] = pi
|
db.preimages[hash] = pi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Preimages returns a list of SHA3 preimages that have been submitted.
|
// Preimages returns a list of SHA3 preimages that have been submitted.
|
||||||
func (self *StateDB) Preimages() map[common.Hash][]byte {
|
func (db *StateDB) Preimages() map[common.Hash][]byte {
|
||||||
return self.preimages
|
return db.preimages
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddRefund adds gas to the refund counter
|
// AddRefund adds gas to the refund counter
|
||||||
func (self *StateDB) AddRefund(gas uint64) {
|
func (db *StateDB) AddRefund(gas uint64) {
|
||||||
self.journal.append(refundChange{prev: self.refund})
|
db.journal.append(refundChange{prev: db.refund})
|
||||||
self.refund += gas
|
db.refund += gas
|
||||||
}
|
}
|
||||||
|
|
||||||
// SubRefund removes gas from the refund counter.
|
// SubRefund removes gas from the refund counter.
|
||||||
// This method will panic if the refund counter goes below zero
|
// This method will panic if the refund counter goes below zero
|
||||||
func (self *StateDB) SubRefund(gas uint64) {
|
func (db *StateDB) SubRefund(gas uint64) {
|
||||||
self.journal.append(refundChange{prev: self.refund})
|
db.journal.append(refundChange{prev: db.refund})
|
||||||
if gas > self.refund {
|
if gas > db.refund {
|
||||||
panic("Refund counter below zero")
|
panic("Refund counter below zero")
|
||||||
}
|
}
|
||||||
self.refund -= gas
|
db.refund -= gas
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exist reports whether the given account address exists in the state.
|
// Exist reports whether the given account address exists in the state.
|
||||||
// Notably this also returns true for suicided accounts.
|
// Notably this also returns true for suicided accounts.
|
||||||
func (self *StateDB) Exist(addr common.Address) bool {
|
func (db *StateDB) Exist(addr common.Address) bool {
|
||||||
return self.getStateObject(addr) != nil
|
return db.getStateObject(addr) != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Empty returns whether the state object is either non-existent
|
// Empty returns whether the state object is either non-existent
|
||||||
// or empty according to the EIP161 specification (balance = nonce = code = 0)
|
// or empty according to the EIP161 specification (balance = nonce = code = 0)
|
||||||
func (self *StateDB) Empty(addr common.Address) bool {
|
func (db *StateDB) Empty(addr common.Address) bool {
|
||||||
so := self.getStateObject(addr)
|
so := db.getStateObject(addr)
|
||||||
return so == nil || so.empty()
|
return so == nil || so.empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve the balance from the given address or 0 if object not found
|
// Retrieve the balance from the given address or 0 if object not found
|
||||||
func (self *StateDB) GetBalance(addr common.Address) *big.Int {
|
func (db *StateDB) GetBalance(addr common.Address) *big.Int {
|
||||||
stateObject := self.getStateObject(addr)
|
stateObject := db.getStateObject(addr)
|
||||||
if stateObject != nil {
|
if stateObject != nil {
|
||||||
return stateObject.Balance()
|
return stateObject.Balance()
|
||||||
}
|
}
|
||||||
return common.Big0
|
return common.Big0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateDB) GetNonce(addr common.Address) uint64 {
|
func (db *StateDB) GetNonce(addr common.Address) uint64 {
|
||||||
stateObject := self.getStateObject(addr)
|
stateObject := db.getStateObject(addr)
|
||||||
if stateObject != nil {
|
if stateObject != nil {
|
||||||
return stateObject.Nonce()
|
return stateObject.Nonce()
|
||||||
}
|
}
|
||||||
|
|
@ -221,31 +221,31 @@ func (self *StateDB) GetNonce(addr common.Address) uint64 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateDB) GetCode(addr common.Address) []byte {
|
func (db *StateDB) GetCode(addr common.Address) []byte {
|
||||||
stateObject := self.getStateObject(addr)
|
stateObject := db.getStateObject(addr)
|
||||||
if stateObject != nil {
|
if stateObject != nil {
|
||||||
return stateObject.Code(self.db)
|
return stateObject.Code(db.db)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateDB) GetCodeSize(addr common.Address) int {
|
func (db *StateDB) GetCodeSize(addr common.Address) int {
|
||||||
stateObject := self.getStateObject(addr)
|
stateObject := db.getStateObject(addr)
|
||||||
if stateObject == nil {
|
if stateObject == nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
if stateObject.code != nil {
|
if stateObject.code != nil {
|
||||||
return len(stateObject.code)
|
return len(stateObject.code)
|
||||||
}
|
}
|
||||||
size, err := self.db.ContractCodeSize(stateObject.addrHash, common.BytesToHash(stateObject.CodeHash()))
|
size, err := db.db.ContractCodeSize(stateObject.addrHash, common.BytesToHash(stateObject.CodeHash()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
self.setError(err)
|
db.setError(err)
|
||||||
}
|
}
|
||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateDB) GetCodeHash(addr common.Address) common.Hash {
|
func (db *StateDB) GetCodeHash(addr common.Address) common.Hash {
|
||||||
stateObject := self.getStateObject(addr)
|
stateObject := db.getStateObject(addr)
|
||||||
if stateObject == nil {
|
if stateObject == nil {
|
||||||
return common.Hash{}
|
return common.Hash{}
|
||||||
}
|
}
|
||||||
|
|
@ -253,25 +253,25 @@ func (self *StateDB) GetCodeHash(addr common.Address) common.Hash {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetState retrieves a value from the given account's storage trie.
|
// GetState retrieves a value from the given account's storage trie.
|
||||||
func (self *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash {
|
func (db *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash {
|
||||||
stateObject := self.getStateObject(addr)
|
stateObject := db.getStateObject(addr)
|
||||||
if stateObject != nil {
|
if stateObject != nil {
|
||||||
return stateObject.GetState(self.db, hash)
|
return stateObject.GetState(db.db, hash)
|
||||||
}
|
}
|
||||||
return common.Hash{}
|
return common.Hash{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetProof returns the MerkleProof for a given Account
|
// GetProof returns the MerkleProof for a given Account
|
||||||
func (self *StateDB) GetProof(a common.Address) ([][]byte, error) {
|
func (db *StateDB) GetProof(a common.Address) ([][]byte, error) {
|
||||||
var proof proofList
|
var proof proofList
|
||||||
err := self.trie.Prove(crypto.Keccak256(a.Bytes()), 0, &proof)
|
err := db.trie.Prove(crypto.Keccak256(a.Bytes()), 0, &proof)
|
||||||
return [][]byte(proof), err
|
return [][]byte(proof), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetProof returns the StorageProof for given key
|
// GetProof returns the StorageProof for given key
|
||||||
func (self *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte, error) {
|
func (db *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte, error) {
|
||||||
var proof proofList
|
var proof proofList
|
||||||
trie := self.StorageTrie(a)
|
trie := db.StorageTrie(a)
|
||||||
if trie == nil {
|
if trie == nil {
|
||||||
return proof, errors.New("storage trie for requested address does not exist")
|
return proof, errors.New("storage trie for requested address does not exist")
|
||||||
}
|
}
|
||||||
|
|
@ -280,32 +280,32 @@ func (self *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byt
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCommittedState retrieves a value from the given account's committed storage trie.
|
// GetCommittedState retrieves a value from the given account's committed storage trie.
|
||||||
func (self *StateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash {
|
func (db *StateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash {
|
||||||
stateObject := self.getStateObject(addr)
|
stateObject := db.getStateObject(addr)
|
||||||
if stateObject != nil {
|
if stateObject != nil {
|
||||||
return stateObject.GetCommittedState(self.db, hash)
|
return stateObject.GetCommittedState(db.db, hash)
|
||||||
}
|
}
|
||||||
return common.Hash{}
|
return common.Hash{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Database retrieves the low level database supporting the lower level trie ops.
|
// Database retrieves the low level database supporting the lower level trie ops.
|
||||||
func (self *StateDB) Database() Database {
|
func (db *StateDB) Database() Database {
|
||||||
return self.db
|
return db.db
|
||||||
}
|
}
|
||||||
|
|
||||||
// StorageTrie returns the storage trie of an account.
|
// StorageTrie returns the storage trie of an account.
|
||||||
// The return value is a copy and is nil for non-existent accounts.
|
// The return value is a copy and is nil for non-existent accounts.
|
||||||
func (self *StateDB) StorageTrie(addr common.Address) Trie {
|
func (db *StateDB) StorageTrie(addr common.Address) Trie {
|
||||||
stateObject := self.getStateObject(addr)
|
stateObject := db.getStateObject(addr)
|
||||||
if stateObject == nil {
|
if stateObject == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
cpy := stateObject.deepCopy(self)
|
cpy := stateObject.deepCopy(db)
|
||||||
return cpy.updateTrie(self.db)
|
return cpy.updateTrie(db.db)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateDB) HasSuicided(addr common.Address) bool {
|
func (db *StateDB) HasSuicided(addr common.Address) bool {
|
||||||
stateObject := self.getStateObject(addr)
|
stateObject := db.getStateObject(addr)
|
||||||
if stateObject != nil {
|
if stateObject != nil {
|
||||||
return stateObject.suicided
|
return stateObject.suicided
|
||||||
}
|
}
|
||||||
|
|
@ -317,46 +317,46 @@ func (self *StateDB) HasSuicided(addr common.Address) bool {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// AddBalance adds amount to the account associated with addr.
|
// AddBalance adds amount to the account associated with addr.
|
||||||
func (self *StateDB) AddBalance(addr common.Address, amount *big.Int) {
|
func (db *StateDB) AddBalance(addr common.Address, amount *big.Int) {
|
||||||
stateObject := self.GetOrNewStateObject(addr)
|
stateObject := db.GetOrNewStateObject(addr)
|
||||||
if stateObject != nil {
|
if stateObject != nil {
|
||||||
stateObject.AddBalance(amount)
|
stateObject.AddBalance(amount)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SubBalance subtracts amount from the account associated with addr.
|
// SubBalance subtracts amount from the account associated with addr.
|
||||||
func (self *StateDB) SubBalance(addr common.Address, amount *big.Int) {
|
func (db *StateDB) SubBalance(addr common.Address, amount *big.Int) {
|
||||||
stateObject := self.GetOrNewStateObject(addr)
|
stateObject := db.GetOrNewStateObject(addr)
|
||||||
if stateObject != nil {
|
if stateObject != nil {
|
||||||
stateObject.SubBalance(amount)
|
stateObject.SubBalance(amount)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateDB) SetBalance(addr common.Address, amount *big.Int) {
|
func (db *StateDB) SetBalance(addr common.Address, amount *big.Int) {
|
||||||
stateObject := self.GetOrNewStateObject(addr)
|
stateObject := db.GetOrNewStateObject(addr)
|
||||||
if stateObject != nil {
|
if stateObject != nil {
|
||||||
stateObject.SetBalance(amount)
|
stateObject.SetBalance(amount)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateDB) SetNonce(addr common.Address, nonce uint64) {
|
func (db *StateDB) SetNonce(addr common.Address, nonce uint64) {
|
||||||
stateObject := self.GetOrNewStateObject(addr)
|
stateObject := db.GetOrNewStateObject(addr)
|
||||||
if stateObject != nil {
|
if stateObject != nil {
|
||||||
stateObject.SetNonce(nonce)
|
stateObject.SetNonce(nonce)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateDB) SetCode(addr common.Address, code []byte) {
|
func (db *StateDB) SetCode(addr common.Address, code []byte) {
|
||||||
stateObject := self.GetOrNewStateObject(addr)
|
stateObject := db.GetOrNewStateObject(addr)
|
||||||
if stateObject != nil {
|
if stateObject != nil {
|
||||||
stateObject.SetCode(crypto.Keccak256Hash(code), code)
|
stateObject.SetCode(crypto.Keccak256Hash(code), code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateDB) SetState(addr common.Address, key, value common.Hash) {
|
func (db *StateDB) SetState(addr common.Address, key, value common.Hash) {
|
||||||
stateObject := self.GetOrNewStateObject(addr)
|
stateObject := db.GetOrNewStateObject(addr)
|
||||||
if stateObject != nil {
|
if stateObject != nil {
|
||||||
stateObject.SetState(self.db, key, value)
|
stateObject.SetState(db.db, key, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -365,12 +365,12 @@ func (self *StateDB) SetState(addr common.Address, key, value common.Hash) {
|
||||||
//
|
//
|
||||||
// The account's state object is still available until the state is committed,
|
// The account's state object is still available until the state is committed,
|
||||||
// getStateObject will return a non-nil account after Suicide.
|
// getStateObject will return a non-nil account after Suicide.
|
||||||
func (self *StateDB) Suicide(addr common.Address) bool {
|
func (db *StateDB) Suicide(addr common.Address) bool {
|
||||||
stateObject := self.getStateObject(addr)
|
stateObject := db.getStateObject(addr)
|
||||||
if stateObject == nil {
|
if stateObject == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
self.journal.append(suicideChange{
|
db.journal.append(suicideChange{
|
||||||
account: &addr,
|
account: &addr,
|
||||||
prev: stateObject.suicided,
|
prev: stateObject.suicided,
|
||||||
prevbalance: new(big.Int).Set(stateObject.Balance()),
|
prevbalance: new(big.Int).Set(stateObject.Balance()),
|
||||||
|
|
@ -386,26 +386,26 @@ func (self *StateDB) Suicide(addr common.Address) bool {
|
||||||
//
|
//
|
||||||
|
|
||||||
// updateStateObject writes the given object to the trie.
|
// updateStateObject writes the given object to the trie.
|
||||||
func (self *StateDB) updateStateObject(stateObject *stateObject) {
|
func (db *StateDB) updateStateObject(stateObject *stateObject) {
|
||||||
addr := stateObject.Address()
|
addr := stateObject.Address()
|
||||||
data, err := rlp.EncodeToBytes(stateObject)
|
data, err := rlp.EncodeToBytes(stateObject)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err))
|
panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err))
|
||||||
}
|
}
|
||||||
self.setError(self.trie.TryUpdate(addr[:], data))
|
db.setError(db.trie.TryUpdate(addr[:], data))
|
||||||
}
|
}
|
||||||
|
|
||||||
// deleteStateObject removes the given object from the state trie.
|
// deleteStateObject removes the given object from the state trie.
|
||||||
func (self *StateDB) deleteStateObject(stateObject *stateObject) {
|
func (db *StateDB) deleteStateObject(stateObject *stateObject) {
|
||||||
stateObject.deleted = true
|
stateObject.deleted = true
|
||||||
addr := stateObject.Address()
|
addr := stateObject.Address()
|
||||||
self.setError(self.trie.TryDelete(addr[:]))
|
db.setError(db.trie.TryDelete(addr[:]))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve a state object given by the address. Returns nil if not found.
|
// Retrieve a state object given by the address. Returns nil if not found.
|
||||||
func (self *StateDB) getStateObject(addr common.Address) (stateObject *stateObject) {
|
func (db *StateDB) getStateObject(addr common.Address) (stateObject *stateObject) {
|
||||||
// Prefer 'live' objects.
|
// Prefer 'live' objects.
|
||||||
if obj := self.stateObjects[addr]; obj != nil {
|
if obj := db.stateObjects[addr]; obj != nil {
|
||||||
if obj.deleted {
|
if obj.deleted {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -413,9 +413,9 @@ func (self *StateDB) getStateObject(addr common.Address) (stateObject *stateObje
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the object from the database.
|
// Load the object from the database.
|
||||||
enc, err := self.trie.TryGet(addr[:])
|
enc, err := db.trie.TryGet(addr[:])
|
||||||
if len(enc) == 0 {
|
if len(enc) == 0 {
|
||||||
self.setError(err)
|
db.setError(err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var data Account
|
var data Account
|
||||||
|
|
@ -424,36 +424,36 @@ func (self *StateDB) getStateObject(addr common.Address) (stateObject *stateObje
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// Insert into the live set.
|
// Insert into the live set.
|
||||||
obj := newObject(self, addr, data)
|
obj := newObject(db, addr, data)
|
||||||
self.setStateObject(obj)
|
db.setStateObject(obj)
|
||||||
return obj
|
return obj
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StateDB) setStateObject(object *stateObject) {
|
func (db *StateDB) setStateObject(object *stateObject) {
|
||||||
self.stateObjects[object.Address()] = object
|
db.stateObjects[object.Address()] = object
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve a state object or create a new state object if nil.
|
// Retrieve a state object or create a new state object if nil.
|
||||||
func (self *StateDB) GetOrNewStateObject(addr common.Address) *stateObject {
|
func (db *StateDB) GetOrNewStateObject(addr common.Address) *stateObject {
|
||||||
stateObject := self.getStateObject(addr)
|
stateObject := db.getStateObject(addr)
|
||||||
if stateObject == nil || stateObject.deleted {
|
if stateObject == nil || stateObject.deleted {
|
||||||
stateObject, _ = self.createObject(addr)
|
stateObject, _ = db.createObject(addr)
|
||||||
}
|
}
|
||||||
return stateObject
|
return stateObject
|
||||||
}
|
}
|
||||||
|
|
||||||
// createObject creates a new state object. If there is an existing account with
|
// createObject creates a new state object. If there is an existing account with
|
||||||
// the given address, it is overwritten and returned as the second return value.
|
// the given address, it is overwritten and returned as the second return value.
|
||||||
func (self *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) {
|
func (db *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) {
|
||||||
prev = self.getStateObject(addr)
|
prev = db.getStateObject(addr)
|
||||||
newobj = newObject(self, addr, Account{})
|
newobj = newObject(db, addr, Account{})
|
||||||
newobj.setNonce(0) // sets the object to dirty
|
newobj.setNonce(0) // sets the object to dirty
|
||||||
if prev == nil {
|
if prev == nil {
|
||||||
self.journal.append(createObjectChange{account: &addr})
|
db.journal.append(createObjectChange{account: &addr})
|
||||||
} else {
|
} else {
|
||||||
self.journal.append(resetObjectChange{prev: prev})
|
db.journal.append(resetObjectChange{prev: prev})
|
||||||
}
|
}
|
||||||
self.setStateObject(newobj)
|
db.setStateObject(newobj)
|
||||||
return newobj, prev
|
return newobj, prev
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -467,8 +467,8 @@ func (self *StateDB) createObject(addr common.Address) (newobj, prev *stateObjec
|
||||||
// 2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1)
|
// 2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1)
|
||||||
//
|
//
|
||||||
// Carrying over the balance ensures that Ether doesn't disappear.
|
// Carrying over the balance ensures that Ether doesn't disappear.
|
||||||
func (self *StateDB) CreateAccount(addr common.Address) {
|
func (db *StateDB) CreateAccount(addr common.Address) {
|
||||||
new, prev := self.createObject(addr)
|
new, prev := db.createObject(addr)
|
||||||
if prev != nil {
|
if prev != nil {
|
||||||
new.setBalance(prev.data.Balance)
|
new.setBalance(prev.data.Balance)
|
||||||
}
|
}
|
||||||
|
|
@ -492,26 +492,26 @@ func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common
|
||||||
|
|
||||||
// Copy creates a deep, independent copy of the state.
|
// Copy creates a deep, independent copy of the state.
|
||||||
// Snapshots of the copied state cannot be applied to the copy.
|
// Snapshots of the copied state cannot be applied to the copy.
|
||||||
func (self *StateDB) Copy() *StateDB {
|
func (db *StateDB) Copy() *StateDB {
|
||||||
// Copy all the basic fields, initialize the memory ones
|
// Copy all the basic fields, initialize the memory ones
|
||||||
state := &StateDB{
|
state := &StateDB{
|
||||||
db: self.db,
|
db: db.db,
|
||||||
trie: self.db.CopyTrie(self.trie),
|
trie: db.db.CopyTrie(db.trie),
|
||||||
stateObjects: make(map[common.Address]*stateObject, len(self.journal.dirties)),
|
stateObjects: make(map[common.Address]*stateObject, len(db.journal.dirties)),
|
||||||
stateObjectsDirty: make(map[common.Address]struct{}, len(self.journal.dirties)),
|
stateObjectsDirty: make(map[common.Address]struct{}, len(db.journal.dirties)),
|
||||||
refund: self.refund,
|
refund: db.refund,
|
||||||
logs: make(map[common.Hash][]*types.Log, len(self.logs)),
|
logs: make(map[common.Hash][]*types.Log, len(db.logs)),
|
||||||
logSize: self.logSize,
|
logSize: db.logSize,
|
||||||
preimages: make(map[common.Hash][]byte),
|
preimages: make(map[common.Hash][]byte),
|
||||||
journal: newJournal(),
|
journal: newJournal(),
|
||||||
}
|
}
|
||||||
// Copy the dirty states, logs, and preimages
|
// Copy the dirty states, logs, and preimages
|
||||||
for addr := range self.journal.dirties {
|
for addr := range db.journal.dirties {
|
||||||
// As documented [here](https://github.com/ethereum/go-ethereum/pull/16485#issuecomment-380438527),
|
// As documented [here](https://github.com/ethereum/go-ethereum/pull/16485#issuecomment-380438527),
|
||||||
// and in the Finalise-method, there is a case where an object is in the journal but not
|
// and in the Finalise-method, there is a case where an object is in the journal but not
|
||||||
// in the stateObjects: OOG after touch on ripeMD prior to Byzantium. Thus, we need to check for
|
// in the stateObjects: OOG after touch on ripeMD prior to Byzantium. Thus, we need to check for
|
||||||
// nil
|
// nil
|
||||||
if object, exist := self.stateObjects[addr]; exist {
|
if object, exist := db.stateObjects[addr]; exist {
|
||||||
state.stateObjects[addr] = object.deepCopy(state)
|
state.stateObjects[addr] = object.deepCopy(state)
|
||||||
state.stateObjectsDirty[addr] = struct{}{}
|
state.stateObjectsDirty[addr] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
@ -519,13 +519,13 @@ func (self *StateDB) Copy() *StateDB {
|
||||||
// Above, we don't copy the actual journal. This means that if the copy is copied, the
|
// Above, we don't copy the actual journal. This means that if the copy is copied, the
|
||||||
// loop above will be a no-op, since the copy's journal is empty.
|
// loop above will be a no-op, since the copy's journal is empty.
|
||||||
// Thus, here we iterate over stateObjects, to enable copies of copies
|
// Thus, here we iterate over stateObjects, to enable copies of copies
|
||||||
for addr := range self.stateObjectsDirty {
|
for addr := range db.stateObjectsDirty {
|
||||||
if _, exist := state.stateObjects[addr]; !exist {
|
if _, exist := state.stateObjects[addr]; !exist {
|
||||||
state.stateObjects[addr] = self.stateObjects[addr].deepCopy(state)
|
state.stateObjects[addr] = db.stateObjects[addr].deepCopy(state)
|
||||||
state.stateObjectsDirty[addr] = struct{}{}
|
state.stateObjectsDirty[addr] = struct{}{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for hash, logs := range self.logs {
|
for hash, logs := range db.logs {
|
||||||
cpy := make([]*types.Log, len(logs))
|
cpy := make([]*types.Log, len(logs))
|
||||||
for i, l := range logs {
|
for i, l := range logs {
|
||||||
cpy[i] = new(types.Log)
|
cpy[i] = new(types.Log)
|
||||||
|
|
@ -533,46 +533,46 @@ func (self *StateDB) Copy() *StateDB {
|
||||||
}
|
}
|
||||||
state.logs[hash] = cpy
|
state.logs[hash] = cpy
|
||||||
}
|
}
|
||||||
for hash, preimage := range self.preimages {
|
for hash, preimage := range db.preimages {
|
||||||
state.preimages[hash] = preimage
|
state.preimages[hash] = preimage
|
||||||
}
|
}
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
// Snapshot returns an identifier for the current revision of the state.
|
// Snapshot returns an identifier for the current revision of the state.
|
||||||
func (self *StateDB) Snapshot() int {
|
func (db *StateDB) Snapshot() int {
|
||||||
id := self.nextRevisionId
|
id := db.nextRevisionId
|
||||||
self.nextRevisionId++
|
db.nextRevisionId++
|
||||||
self.validRevisions = append(self.validRevisions, revision{id, self.journal.length()})
|
db.validRevisions = append(db.validRevisions, revision{id, db.journal.length()})
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
// RevertToSnapshot reverts all state changes made since the given revision.
|
// RevertToSnapshot reverts all state changes made since the given revision.
|
||||||
func (self *StateDB) RevertToSnapshot(revid int) {
|
func (db *StateDB) RevertToSnapshot(revid int) {
|
||||||
// Find the snapshot in the stack of valid snapshots.
|
// Find the snapshot in the stack of valid snapshots.
|
||||||
idx := sort.Search(len(self.validRevisions), func(i int) bool {
|
idx := sort.Search(len(db.validRevisions), func(i int) bool {
|
||||||
return self.validRevisions[i].id >= revid
|
return db.validRevisions[i].id >= revid
|
||||||
})
|
})
|
||||||
if idx == len(self.validRevisions) || self.validRevisions[idx].id != revid {
|
if idx == len(db.validRevisions) || db.validRevisions[idx].id != revid {
|
||||||
panic(fmt.Errorf("revision id %v cannot be reverted", revid))
|
panic(fmt.Errorf("revision id %v cannot be reverted", revid))
|
||||||
}
|
}
|
||||||
snapshot := self.validRevisions[idx].journalIndex
|
snapshot := db.validRevisions[idx].journalIndex
|
||||||
|
|
||||||
// Replay the journal to undo changes and remove invalidated snapshots
|
// Replay the journal to undo changes and remove invalidated snapshots
|
||||||
self.journal.revert(self, snapshot)
|
db.journal.revert(db, snapshot)
|
||||||
self.validRevisions = self.validRevisions[:idx]
|
db.validRevisions = db.validRevisions[:idx]
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRefund returns the current value of the refund counter.
|
// GetRefund returns the current value of the refund counter.
|
||||||
func (self *StateDB) GetRefund() uint64 {
|
func (db *StateDB) GetRefund() uint64 {
|
||||||
return self.refund
|
return db.refund
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finalise finalises the state by removing the self destructed objects
|
// Finalise finalises the state by removing the db destructed objects
|
||||||
// and clears the journal as well as the refunds.
|
// and clears the journal as well as the refunds.
|
||||||
func (s *StateDB) Finalise(deleteEmptyObjects bool) {
|
func (db *StateDB) Finalise(deleteEmptyObjects bool) {
|
||||||
for addr := range s.journal.dirties {
|
for addr := range db.journal.dirties {
|
||||||
stateObject, exist := s.stateObjects[addr]
|
stateObject, exist := db.stateObjects[addr]
|
||||||
if !exist {
|
if !exist {
|
||||||
// ripeMD is 'touched' at block 1714175, in tx 0x1237f737031e40bcde4a8b7e717b2d15e3ecadfe49bb1bbc71ee9deb09c6fcf2
|
// ripeMD is 'touched' at block 1714175, in tx 0x1237f737031e40bcde4a8b7e717b2d15e3ecadfe49bb1bbc71ee9deb09c6fcf2
|
||||||
// That tx goes out of gas, and although the notion of 'touched' does not exist there, the
|
// That tx goes out of gas, and although the notion of 'touched' does not exist there, the
|
||||||
|
|
@ -584,81 +584,81 @@ func (s *StateDB) Finalise(deleteEmptyObjects bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if stateObject.suicided || (deleteEmptyObjects && stateObject.empty()) {
|
if stateObject.suicided || (deleteEmptyObjects && stateObject.empty()) {
|
||||||
s.deleteStateObject(stateObject)
|
db.deleteStateObject(stateObject)
|
||||||
} else {
|
} else {
|
||||||
stateObject.updateRoot(s.db)
|
stateObject.updateRoot(db.db)
|
||||||
s.updateStateObject(stateObject)
|
db.updateStateObject(stateObject)
|
||||||
}
|
}
|
||||||
s.stateObjectsDirty[addr] = struct{}{}
|
db.stateObjectsDirty[addr] = struct{}{}
|
||||||
}
|
}
|
||||||
// Invalidate journal because reverting across transactions is not allowed.
|
// Invalidate journal because reverting across transactions is not allowed.
|
||||||
s.clearJournalAndRefund()
|
db.clearJournalAndRefund()
|
||||||
}
|
}
|
||||||
|
|
||||||
// IntermediateRoot computes the current root hash of the state trie.
|
// IntermediateRoot computes the current root hash of the state trie.
|
||||||
// It is called in between transactions to get the root hash that
|
// It is called in between transactions to get the root hash that
|
||||||
// goes into transaction receipts.
|
// goes into transaction receipts.
|
||||||
func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
|
func (db *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
|
||||||
s.Finalise(deleteEmptyObjects)
|
db.Finalise(deleteEmptyObjects)
|
||||||
return s.trie.Hash()
|
return db.trie.Hash()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare sets the current transaction hash and index and block hash which is
|
// Prepare sets the current transaction hash and index and block hash which is
|
||||||
// used when the EVM emits new state logs.
|
// used when the EVM emits new state logs.
|
||||||
func (self *StateDB) Prepare(thash, bhash common.Hash, ti int) {
|
func (db *StateDB) Prepare(thash, bhash common.Hash, ti int) {
|
||||||
self.thash = thash
|
db.thash = thash
|
||||||
self.bhash = bhash
|
db.bhash = bhash
|
||||||
self.txIndex = ti
|
db.txIndex = ti
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StateDB) clearJournalAndRefund() {
|
func (db *StateDB) clearJournalAndRefund() {
|
||||||
s.journal = newJournal()
|
db.journal = newJournal()
|
||||||
s.validRevisions = s.validRevisions[:0]
|
db.validRevisions = db.validRevisions[:0]
|
||||||
s.refund = 0
|
db.refund = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commit writes the state to the underlying in-memory trie database.
|
// Commit writes the state to the underlying in-memory trie database.
|
||||||
func (s *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error) {
|
func (db *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error) {
|
||||||
defer s.clearJournalAndRefund()
|
defer db.clearJournalAndRefund()
|
||||||
|
|
||||||
for addr := range s.journal.dirties {
|
for addr := range db.journal.dirties {
|
||||||
s.stateObjectsDirty[addr] = struct{}{}
|
db.stateObjectsDirty[addr] = struct{}{}
|
||||||
}
|
}
|
||||||
// Commit objects to the trie.
|
// Commit objects to the trie.
|
||||||
for addr, stateObject := range s.stateObjects {
|
for addr, stateObject := range db.stateObjects {
|
||||||
_, isDirty := s.stateObjectsDirty[addr]
|
_, isDirty := db.stateObjectsDirty[addr]
|
||||||
switch {
|
switch {
|
||||||
case stateObject.suicided || (isDirty && deleteEmptyObjects && stateObject.empty()):
|
case stateObject.suicided || (isDirty && deleteEmptyObjects && stateObject.empty()):
|
||||||
// If the object has been removed, don't bother syncing it
|
// If the object has been removed, don't bother syncing it
|
||||||
// and just mark it for deletion in the trie.
|
// and just mark it for deletion in the trie.
|
||||||
s.deleteStateObject(stateObject)
|
db.deleteStateObject(stateObject)
|
||||||
case isDirty:
|
case isDirty:
|
||||||
// Write any contract code associated with the state object
|
// Write any contract code associated with the state object
|
||||||
if stateObject.code != nil && stateObject.dirtyCode {
|
if stateObject.code != nil && stateObject.dirtyCode {
|
||||||
s.db.TrieDB().InsertBlob(common.BytesToHash(stateObject.CodeHash()), stateObject.code)
|
db.db.TrieDB().InsertBlob(common.BytesToHash(stateObject.CodeHash()), stateObject.code)
|
||||||
stateObject.dirtyCode = false
|
stateObject.dirtyCode = false
|
||||||
}
|
}
|
||||||
// Write any storage changes in the state object to its storage trie.
|
// Write any storage changes in the state object to its storage trie.
|
||||||
if err := stateObject.CommitTrie(s.db); err != nil {
|
if err := stateObject.CommitTrie(db.db); err != nil {
|
||||||
return common.Hash{}, err
|
return common.Hash{}, err
|
||||||
}
|
}
|
||||||
// Update the object in the main account trie.
|
// Update the object in the main account trie.
|
||||||
s.updateStateObject(stateObject)
|
db.updateStateObject(stateObject)
|
||||||
}
|
}
|
||||||
delete(s.stateObjectsDirty, addr)
|
delete(db.stateObjectsDirty, addr)
|
||||||
}
|
}
|
||||||
// Write trie changes.
|
// Write trie changes.
|
||||||
root, err = s.trie.Commit(func(leaf []byte, parent common.Hash) error {
|
root, err = db.trie.Commit(func(leaf []byte, parent common.Hash) error {
|
||||||
var account Account
|
var account Account
|
||||||
if err := rlp.DecodeBytes(leaf, &account); err != nil {
|
if err := rlp.DecodeBytes(leaf, &account); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if account.Root != emptyState {
|
if account.Root != emptyState {
|
||||||
s.db.TrieDB().Reference(account.Root, parent)
|
db.db.TrieDB().Reference(account.Root, parent)
|
||||||
}
|
}
|
||||||
code := common.BytesToHash(account.CodeHash)
|
code := common.BytesToHash(account.CodeHash)
|
||||||
if code != emptyCode {
|
if code != emptyCode {
|
||||||
s.db.TrieDB().Reference(code, parent)
|
db.db.TrieDB().Reference(code, parent)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -168,7 +168,7 @@ func (s EIP155Signer) Hash(tx *Transaction) common.Hash {
|
||||||
// homestead rules.
|
// homestead rules.
|
||||||
type HomesteadSigner struct{ FrontierSigner }
|
type HomesteadSigner struct{ FrontierSigner }
|
||||||
|
|
||||||
func (s HomesteadSigner) Equal(s2 Signer) bool {
|
func (hs HomesteadSigner) Equal(s2 Signer) bool {
|
||||||
_, ok := s2.(HomesteadSigner)
|
_, ok := s2.(HomesteadSigner)
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
@ -185,7 +185,7 @@ func (hs HomesteadSigner) Sender(tx *Transaction) (common.Address, error) {
|
||||||
|
|
||||||
type FrontierSigner struct{}
|
type FrontierSigner struct{}
|
||||||
|
|
||||||
func (s FrontierSigner) Equal(s2 Signer) bool {
|
func (fs FrontierSigner) Equal(s2 Signer) bool {
|
||||||
_, ok := s2.(FrontierSigner)
|
_, ok := s2.(FrontierSigner)
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue