core/state: unified function receiver names (#19615)

This commit is contained in:
Mohanson 2019-05-26 05:52:10 +08:00 committed by Péter Szilágyi
parent 2cd6059e51
commit 4b622b277e

View File

@ -33,23 +33,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(c), " ")
} }
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
} }
@ -123,210 +123,210 @@ 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
} }
// Track the amount of time wasted on reading the storge trie // Track the amount of time wasted on reading the storge trie
if metrics.EnabledExpensive { if metrics.EnabledExpensive {
defer func(start time.Time) { self.db.StorageReads += time.Since(start) }(time.Now()) defer func(start time.Time) { s.db.StorageReads += time.Since(start) }(time.Now())
} }
// 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 {
// Track the amount of time wasted on updating the storge trie // Track the amount of time wasted on updating the storge trie
if metrics.EnabledExpensive { if metrics.EnabledExpensive {
defer func(start time.Time) { self.db.StorageUpdates += time.Since(start) }(time.Now()) defer func(start time.Time) { s.db.StorageUpdates += time.Since(start) }(time.Now())
} }
// Update all the dirty slots in the trie // Update all the dirty slots in the 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)
// Track the amount of time wasted on hashing the storge trie // Track the amount of time wasted on hashing the storge trie
if metrics.EnabledExpensive { if metrics.EnabledExpensive {
defer func(start time.Time) { self.db.StorageHashes += time.Since(start) }(time.Now()) defer func(start time.Time) { s.db.StorageHashes += time.Since(start) }(time.Now())
} }
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
} }
// Track the amount of time wasted on committing the storge trie // Track the amount of time wasted on committing the storge trie
if metrics.EnabledExpensive { if metrics.EnabledExpensive {
defer func(start time.Time) { self.db.StorageCommits += time.Since(start) }(time.Now()) defer func(start time.Time) { s.db.StorageCommits += time.Since(start) }(time.Now())
} }
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
} }
@ -335,69 +335,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")
} }