File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -454,6 +454,23 @@ func (s *stateObject) Code(db Database) []byte {
454454 return code
455455}
456456
457+ // CodeSize returns the size of the contract code associated with this object,
458+ // or zero if none. This methos is an almost mirror of Code, but uses a cache
459+ // inside the database to avoid loading codes seen recently.
460+ func (s * stateObject ) CodeSize (db Database ) int {
461+ if s .code != nil {
462+ return len (s .code )
463+ }
464+ if bytes .Equal (s .CodeHash (), emptyCodeHash ) {
465+ return 0
466+ }
467+ size , err := db .ContractCodeSize (s .addrHash , common .BytesToHash (s .CodeHash ()))
468+ if err != nil {
469+ s .setError (fmt .Errorf ("can't load code size %x: %v" , s .CodeHash (), err ))
470+ }
471+ return size
472+ }
473+
457474func (s * stateObject ) SetCode (codeHash common.Hash , code []byte ) {
458475 prevcode := s .Code (s .db .db )
459476 s .db .journal .append (codeChange {
Original file line number Diff line number Diff line change 1818package state
1919
2020import (
21- "bytes"
2221 "errors"
2322 "fmt"
2423 "math/big"
@@ -289,20 +288,10 @@ func (s *StateDB) GetCode(addr common.Address) []byte {
289288
290289func (s * StateDB ) GetCodeSize (addr common.Address ) int {
291290 stateObject := s .getStateObject (addr )
292- if stateObject == nil {
293- return 0
294- }
295- if stateObject .code != nil {
296- return len (stateObject .code )
297- }
298- if bytes .Equal (stateObject .CodeHash (), emptyCode [:]) {
299- return 0
300- }
301- size , err := s .db .ContractCodeSize (stateObject .addrHash , common .BytesToHash (stateObject .CodeHash ()))
302- if err != nil {
303- s .setError (fmt .Errorf ("GetCodeSize (%x) error: %v" , addr [:], err ))
291+ if stateObject != nil {
292+ return stateObject .CodeSize (s .db )
304293 }
305- return size
294+ return 0
306295}
307296
308297func (s * StateDB ) GetCodeHash (addr common.Address ) common.Hash {
You can’t perform that action at this time.
0 commit comments