@ -183,8 +183,9 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
}
}
// If no live objects are available, attempt to use snapshots
// If no live objects are available, attempt to use snapshots
var (
var (
enc [ ] byte
enc [ ] byte
err error
err error
value common . Hash
)
)
if s . db . snap != nil {
if s . db . snap != nil {
start := time . Now ( )
start := time . Now ( )
@ -192,6 +193,13 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
if metrics . EnabledExpensive {
if metrics . EnabledExpensive {
s . db . SnapshotStorageReads += time . Since ( start )
s . db . SnapshotStorageReads += time . Since ( start )
}
}
if len ( enc ) > 0 {
_ , content , _ , err := rlp . Split ( enc )
if err != nil {
s . db . setError ( err )
}
value . SetBytes ( content )
}
}
}
// If the snapshot is unavailable or reading from it fails, load from the database.
// If the snapshot is unavailable or reading from it fails, load from the database.
if s . db . snap == nil || err != nil {
if s . db . snap == nil || err != nil {
@ -201,7 +209,7 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
s . db . setError ( err )
s . db . setError ( err )
return common . Hash { }
return common . Hash { }
}
}
enc , err = tr . GetStorage ( s . address , key . Bytes ( ) )
val , err : = tr . GetStorage ( s . address , key . Bytes ( ) )
if metrics . EnabledExpensive {
if metrics . EnabledExpensive {
s . db . StorageReads += time . Since ( start )
s . db . StorageReads += time . Since ( start )
}
}
@ -209,14 +217,7 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
s . db . setError ( err )
s . db . setError ( err )
return common . Hash { }
return common . Hash { }
}
}
}
value . SetBytes ( val )
var value common . Hash
if len ( enc ) > 0 {
_ , content , _ , err := rlp . Split ( enc )
if err != nil {
s . db . setError ( err )
}
value . SetBytes ( content )
}
}
s . originStorage [ key ] = value
s . originStorage [ key ] = value
return value
return value
@ -292,7 +293,8 @@ func (s *stateObject) updateTrie(db Database) (Trie, error) {
}
}
s . originStorage [ key ] = value
s . originStorage [ key ] = value
var v [ ] byte
// rlp-encoded value to be used by the snapshot
var snapshotVal [ ] byte
if ( value == common . Hash { } ) {
if ( value == common . Hash { } ) {
if err := tr . DeleteStorage ( s . address , key [ : ] ) ; err != nil {
if err := tr . DeleteStorage ( s . address , key [ : ] ) ; err != nil {
s . db . setError ( err )
s . db . setError ( err )
@ -300,9 +302,10 @@ func (s *stateObject) updateTrie(db Database) (Trie, error) {
}
}
s . db . StorageDeleted += 1
s . db . StorageDeleted += 1
} else {
} else {
trimmedVal := common . TrimLeftZeroes ( value [ : ] )
// Encoding []byte cannot fail, ok to ignore the error.
// Encoding []byte cannot fail, ok to ignore the error.
v , _ = rlp . EncodeToBytes ( common . TrimLeftZeroes ( value [ : ] ) )
snapshotVal , _ = rlp . EncodeToBytes ( trimmedVal )
if err := tr . UpdateStorage ( s . address , key [ : ] , v ) ; err != nil {
if err := tr . UpdateStorage ( s . address , key [ : ] , trimmedVal ) ; err != nil {
s . db . setError ( err )
s . db . setError ( err )
return nil , err
return nil , err
}
}
@ -317,7 +320,7 @@ func (s *stateObject) updateTrie(db Database) (Trie, error) {
s . db . snapStorage [ s . addrHash ] = storage
s . db . snapStorage [ s . addrHash ] = storage
}
}
}
}
storage [ crypto . HashData ( hasher , key [ : ] ) ] = v // v will be nil if it's deleted
storage [ crypto . HashData ( hasher , key [ : ] ) ] = snapshotVal // will be nil if it's deleted
}
}
usedStorage = append ( usedStorage , common . CopyBytes ( key [ : ] ) ) // Copy needed for closure
usedStorage = append ( usedStorage , common . CopyBytes ( key [ : ] ) ) // Copy needed for closure
}
}