@@ -27,6 +27,7 @@ import (
2727 "github.com/XinFinOrg/XDPoSChain/core/types"
2828 "github.com/XinFinOrg/XDPoSChain/crypto"
2929 "github.com/XinFinOrg/XDPoSChain/log"
30+ "github.com/XinFinOrg/XDPoSChain/params"
3031 "github.com/XinFinOrg/XDPoSChain/rlp"
3132 "github.com/XinFinOrg/XDPoSChain/trie"
3233)
@@ -69,6 +70,9 @@ type StateDB struct {
6970 // Per-transaction access list
7071 accessList * accessList
7172
73+ // Transient storage
74+ transientStorage transientStorage
75+
7276 // Journal of state modifications. This is the backbone of
7377 // Snapshot and RevertToSnapshot.
7478 journal journal
@@ -117,6 +121,7 @@ func New(root common.Hash, db Database) (*StateDB, error) {
117121 logs : make (map [common.Hash ][]* types.Log ),
118122 preimages : make (map [common.Hash ][]byte ),
119123 accessList : newAccessList (),
124+ transientStorage : newTransientStorage (),
120125 }, nil
121126}
122127
@@ -406,6 +411,35 @@ func (s *StateDB) Suicide(addr common.Address) bool {
406411 return true
407412}
408413
414+ // SetTransientState sets transient storage for a given account. It
415+ // adds the change to the journal so that it can be rolled back
416+ // to its previous value if there is a revert.
417+ func (s * StateDB ) SetTransientState (addr common.Address , key , value common.Hash ) {
418+ prev := s .GetTransientState (addr , key )
419+ if prev == value {
420+ return
421+ }
422+
423+ s .journal = append (s .journal , transientStorageChange {
424+ account : & addr ,
425+ key : key ,
426+ prevalue : prev ,
427+ })
428+
429+ s .setTransientState (addr , key , value )
430+ }
431+
432+ // setTransientState is a lower level setter for transient storage. It
433+ // is called during a revert to prevent modifications to the journal.
434+ func (s * StateDB ) setTransientState (addr common.Address , key , value common.Hash ) {
435+ s .transientStorage .Set (addr , key , value )
436+ }
437+
438+ // GetTransientState gets transient storage for a given account.
439+ func (s * StateDB ) GetTransientState (addr common.Address , key common.Hash ) common.Hash {
440+ return s .transientStorage .Get (addr , key )
441+ }
442+
409443//
410444// Setting, updating & deleting state object methods.
411445//
@@ -577,6 +611,9 @@ func (s *StateDB) Copy() *StateDB {
577611 // However, it doesn't cost us much to copy an empty list, so we do it anyway
578612 // to not blow up if we ever decide copy it in the middle of a transaction
579613 state .accessList = s .accessList .Copy ()
614+
615+ state .transientStorage = s .transientStorage .Copy ()
616+
580617 return state
581618}
582619
@@ -638,9 +675,10 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
638675 return s .trie .Hash ()
639676}
640677
641- // Prepare sets the current transaction hash and index and block hash which is
642- // used when the EVM emits new state logs.
643- func (s * StateDB ) Prepare (thash common.Hash , ti int ) {
678+ // SetTxContext sets the current transaction hash and index which are
679+ // used when the EVM emits new state logs. It should be invoked before
680+ // transaction execution.
681+ func (s * StateDB ) SetTxContext (thash common.Hash , ti int ) {
644682 s .thash = thash
645683 s .txIndex = ti
646684}
@@ -717,33 +755,39 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error)
717755 return root , err
718756}
719757
720- // PrepareAccessList handles the preparatory steps for executing a state transition with
721- // regards to both EIP-2929 and EIP-2930:
758+ // Prepare handles the preparatory steps for executing a state transition with.
759+ // This method must be invoked before state transition.
722760//
761+ // Berlin fork:
723762// - Add sender to access list (2929)
724763// - Add destination to access list (2929)
725764// - Add precompiles to access list (2929)
726765// - Add the contents of the optional tx access list (2930)
727766//
728- // This method should only be called if Yolov3/Berlin/2929+2930 is applicable at the current number.
729- func (s * StateDB ) PrepareAccessList (sender common.Address , dst * common.Address , precompiles []common.Address , list types.AccessList ) {
730- // Clear out any leftover from previous executions
731- s .accessList = newAccessList ()
732-
733- s .AddAddressToAccessList (sender )
734- if dst != nil {
735- s .AddAddressToAccessList (* dst )
736- // If it's a create-tx, the destination will be added inside evm.create
737- }
738- for _ , addr := range precompiles {
739- s .AddAddressToAccessList (addr )
740- }
741- for _ , el := range list {
742- s .AddAddressToAccessList (el .Address )
743- for _ , key := range el .StorageKeys {
744- s .AddSlotToAccessList (el .Address , key )
767+ // Potential EIPs:
768+ // - Reset transient storage(1153)
769+ func (s * StateDB ) Prepare (rules params.Rules , sender common.Address , dst * common.Address , precompiles []common.Address , list types.AccessList ) {
770+ if rules .IsEIP1559 {
771+ // Clear out any leftover from previous executions
772+ s .accessList = newAccessList ()
773+
774+ s .AddAddressToAccessList (sender )
775+ if dst != nil {
776+ s .AddAddressToAccessList (* dst )
777+ // If it's a create-tx, the destination will be added inside evm.create
778+ }
779+ for _ , addr := range precompiles {
780+ s .AddAddressToAccessList (addr )
781+ }
782+ for _ , el := range list {
783+ s .AddAddressToAccessList (el .Address )
784+ for _ , key := range el .StorageKeys {
785+ s .AddSlotToAccessList (el .Address , key )
786+ }
745787 }
746788 }
789+ // Reset transient storage at the beginning of transaction execution
790+ s .transientStorage = newTransientStorage ()
747791}
748792
749793// AddAddressToAccessList adds the given address to the access list
0 commit comments