Merge pull request #21056 from karalabe/statedb-simpler-code

core/state: make GetCodeSize mirror GetCode implementation wise
This commit is contained in:
Péter Szilágyi 2020-05-11 11:09:25 +03:00 committed by GitHub
commit 37877e86ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 14 deletions

View File

@ -454,6 +454,23 @@ func (s *stateObject) Code(db Database) []byte {
return code return code
} }
// CodeSize returns the size of the contract code associated with this object,
// or zero if none. This methos is an almost mirror of Code, but uses a cache
// inside the database to avoid loading codes seen recently.
func (s *stateObject) CodeSize(db Database) int {
if s.code != nil {
return len(s.code)
}
if bytes.Equal(s.CodeHash(), emptyCodeHash) {
return 0
}
size, err := db.ContractCodeSize(s.addrHash, common.BytesToHash(s.CodeHash()))
if err != nil {
s.setError(fmt.Errorf("can't load code size %x: %v", s.CodeHash(), err))
}
return size
}
func (s *stateObject) SetCode(codeHash common.Hash, code []byte) { func (s *stateObject) SetCode(codeHash common.Hash, code []byte) {
prevcode := s.Code(s.db.db) prevcode := s.Code(s.db.db)
s.db.journal.append(codeChange{ s.db.journal.append(codeChange{

View File

@ -18,7 +18,6 @@
package state package state
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
@ -289,20 +288,10 @@ func (s *StateDB) GetCode(addr common.Address) []byte {
func (s *StateDB) GetCodeSize(addr common.Address) int { func (s *StateDB) GetCodeSize(addr common.Address) int {
stateObject := s.getStateObject(addr) stateObject := s.getStateObject(addr)
if stateObject == nil { if stateObject != nil {
return 0 return stateObject.CodeSize(s.db)
} }
if stateObject.code != nil { return 0
return len(stateObject.code)
}
if bytes.Equal(stateObject.CodeHash(), emptyCode[:]) {
return 0
}
size, err := s.db.ContractCodeSize(stateObject.addrHash, common.BytesToHash(stateObject.CodeHash()))
if err != nil {
s.setError(fmt.Errorf("GetCodeSize (%x) error: %v", addr[:], err))
}
return size
} }
func (s *StateDB) GetCodeHash(addr common.Address) common.Hash { func (s *StateDB) GetCodeHash(addr common.Address) common.Hash {