@@ -31,6 +31,7 @@ import (
3131 "github.com/ethereum/go-ethereum/crypto"
3232 "github.com/ethereum/go-ethereum/log"
3333 "github.com/ethereum/go-ethereum/metrics"
34+ "github.com/ethereum/go-ethereum/params"
3435 "github.com/ethereum/go-ethereum/rlp"
3536 "github.com/ethereum/go-ethereum/trie"
3637)
@@ -102,6 +103,9 @@ type StateDB struct {
102103 // Per-transaction access list
103104 accessList * accessList
104105
106+ // Transient storage
107+ transientStorage transientStorage
108+
105109 // Journal of state modifications. This is the backbone of
106110 // Snapshot and RevertToSnapshot.
107111 journal * journal
@@ -146,6 +150,7 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error)
146150 preimages : make (map [common.Hash ][]byte ),
147151 journal : newJournal (),
148152 accessList : newAccessList (),
153+ transientStorage : newTransientStorage (),
149154 hasher : crypto .NewKeccakState (),
150155 }
151156 if sdb .snaps != nil {
@@ -452,6 +457,35 @@ func (s *StateDB) Suicide(addr common.Address) bool {
452457 return true
453458}
454459
460+ // SetTransientState sets transient storage for a given account. It
461+ // adds the change to the journal so that it can be rolled back
462+ // to its previous value if there is a revert.
463+ func (s * StateDB ) SetTransientState (addr common.Address , key , value common.Hash ) {
464+ prev := s .GetTransientState (addr , key )
465+ if prev == value {
466+ return
467+ }
468+
469+ s .journal .append (transientStorageChange {
470+ account : & addr ,
471+ key : key ,
472+ prevalue : prev ,
473+ })
474+
475+ s .setTransientState (addr , key , value )
476+ }
477+
478+ // setTransientState is a lower level setter for transient storage. It
479+ // is called during a revert to prevent modifications to the journal.
480+ func (s * StateDB ) setTransientState (addr common.Address , key , value common.Hash ) {
481+ s .transientStorage .Set (addr , key , value )
482+ }
483+
484+ // GetTransientState gets transient storage for a given account.
485+ func (s * StateDB ) GetTransientState (addr common.Address , key common.Hash ) common.Hash {
486+ return s .transientStorage .Get (addr , key )
487+ }
488+
455489//
456490// Setting, updating & deleting state object methods.
457491//
@@ -708,6 +742,8 @@ func (s *StateDB) Copy() *StateDB {
708742 // to not blow up if we ever decide copy it in the middle of a transaction
709743 state .accessList = s .accessList .Copy ()
710744
745+ state .transientStorage = s .transientStorage .Copy ()
746+
711747 // If there's a prefetcher running, make an inactive copy of it that can
712748 // only access data but does not actively preload (since the user will not
713749 // know that they need to explicitly terminate an active copy).
@@ -880,9 +916,10 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
880916 return s .trie .Hash ()
881917}
882918
883- // Prepare sets the current transaction hash and index which are
884- // used when the EVM emits new state logs.
885- func (s * StateDB ) Prepare (thash common.Hash , ti int ) {
919+ // SetTxContext sets the current transaction hash and index which are
920+ // used when the EVM emits new state logs. It should be invoked before
921+ // transaction execution.
922+ func (s * StateDB ) SetTxContext (thash common.Hash , ti int ) {
886923 s .thash = thash
887924 s .txIndex = ti
888925}
@@ -1020,33 +1057,39 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
10201057 return root , nil
10211058}
10221059
1023- // PrepareAccessList handles the preparatory steps for executing a state transition with
1024- // regards to both EIP-2929 and EIP-2930:
1060+ // Prepare handles the preparatory steps for executing a state transition with.
1061+ // This method must be invoked before state transition.
10251062//
1063+ // Berlin fork:
10261064// - Add sender to access list (2929)
10271065// - Add destination to access list (2929)
10281066// - Add precompiles to access list (2929)
10291067// - Add the contents of the optional tx access list (2930)
10301068//
1031- // This method should only be called if Berlin/2929+2930 is applicable at the current number.
1032- func (s * StateDB ) PrepareAccessList (sender common.Address , dst * common.Address , precompiles []common.Address , list types.AccessList ) {
1033- // Clear out any leftover from previous executions
1034- s .accessList = newAccessList ()
1035-
1036- s .AddAddressToAccessList (sender )
1037- if dst != nil {
1038- s .AddAddressToAccessList (* dst )
1039- // If it's a create-tx, the destination will be added inside evm.create
1040- }
1041- for _ , addr := range precompiles {
1042- s .AddAddressToAccessList (addr )
1043- }
1044- for _ , el := range list {
1045- s .AddAddressToAccessList (el .Address )
1046- for _ , key := range el .StorageKeys {
1047- s .AddSlotToAccessList (el .Address , key )
1069+ // Potential EIPs:
1070+ // - Reset transient storage(1153)
1071+ func (s * StateDB ) Prepare (rules params.Rules , sender common.Address , dst * common.Address , precompiles []common.Address , list types.AccessList ) {
1072+ if rules .IsBerlin {
1073+ // Clear out any leftover from previous executions
1074+ s .accessList = newAccessList ()
1075+
1076+ s .AddAddressToAccessList (sender )
1077+ if dst != nil {
1078+ s .AddAddressToAccessList (* dst )
1079+ // If it's a create-tx, the destination will be added inside evm.create
1080+ }
1081+ for _ , addr := range precompiles {
1082+ s .AddAddressToAccessList (addr )
1083+ }
1084+ for _ , el := range list {
1085+ s .AddAddressToAccessList (el .Address )
1086+ for _ , key := range el .StorageKeys {
1087+ s .AddSlotToAccessList (el .Address , key )
1088+ }
10481089 }
10491090 }
1091+ // Reset transient storage at the beginning of transaction execution
1092+ s .transientStorage = newTransientStorage ()
10501093}
10511094
10521095// AddAddressToAccessList adds the given address to the access list
0 commit comments