@@ -39,10 +39,7 @@ import (
3939 "github.com/ethereum/go-ethereum/trie"
4040)
4141
42- const (
43- preLoadLimit = 128
44- defaultNumOfSlots = 100
45- )
42+ const defaultNumOfSlots = 100
4643
4744type revision struct {
4845 id int
@@ -101,6 +98,8 @@ type StateDB struct {
10198 stateObjectsPending map [common.Address ]struct {} // State objects finalized but not yet written to the trie
10299 stateObjectsDirty map [common.Address ]struct {} // State objects modified in the current execution
103100
101+ storagePool * StoragePool // sharedPool to store L1 originStorage of stateObjects
102+ writeOnSharedStorage bool // Write to the shared origin storage of a stateObject while reading from the underlying storage layer.
104103 // DB error.
105104 // State objects are used by the consensus core and VM which are
106105 // unable to deal with database-level errors. Any error that occurs
@@ -147,6 +146,16 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error)
147146 return newStateDB (root , db , snaps )
148147}
149148
149+ // NewWithSharedPool creates a new state with sharedStorge on layer 1.5
150+ func NewWithSharedPool (root common.Hash , db Database , snaps * snapshot.Tree ) (* StateDB , error ) {
151+ statedb , err := newStateDB (root , db , snaps )
152+ if err != nil {
153+ return nil , err
154+ }
155+ statedb .storagePool = NewStoragePool ()
156+ return statedb , nil
157+ }
158+
150159func newStateDB (root common.Hash , db Database , snaps * snapshot.Tree ) (* StateDB , error ) {
151160 sdb := & StateDB {
152161 db : db ,
@@ -178,6 +187,10 @@ func newStateDB(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB,
178187 return sdb , nil
179188}
180189
190+ func (s * StateDB ) EnableWriteOnSharedStorage () {
191+ s .writeOnSharedStorage = true
192+ }
193+
181194// StartPrefetcher initializes a new trie prefetcher to pull in nodes from the
182195// state trie concurrently while the state is mutated so that when we reach the
183196// commit phase, most of the needed data is already hot.
@@ -591,78 +604,6 @@ func (s *StateDB) getStateObject(addr common.Address) *StateObject {
591604 return nil
592605}
593606
594- func (s * StateDB ) TryPreload (block * types.Block , signer types.Signer ) {
595- accounts := make (map [common.Address ]bool , block .Transactions ().Len ())
596- accountsSlice := make ([]common.Address , 0 , block .Transactions ().Len ())
597- for _ , tx := range block .Transactions () {
598- from , err := types .Sender (signer , tx )
599- if err != nil {
600- break
601- }
602- accounts [from ] = true
603- if tx .To () != nil {
604- accounts [* tx .To ()] = true
605- }
606- }
607- for account := range accounts {
608- accountsSlice = append (accountsSlice , account )
609- }
610- if len (accountsSlice ) >= preLoadLimit && len (accountsSlice ) > runtime .NumCPU () {
611- objsChan := make (chan []* StateObject , runtime .NumCPU ())
612- for i := 0 ; i < runtime .NumCPU (); i ++ {
613- start := i * len (accountsSlice ) / runtime .NumCPU ()
614- end := (i + 1 ) * len (accountsSlice ) / runtime .NumCPU ()
615- if i + 1 == runtime .NumCPU () {
616- end = len (accountsSlice )
617- }
618- go func (start , end int ) {
619- objs := s .preloadStateObject (accountsSlice [start :end ])
620- objsChan <- objs
621- }(start , end )
622- }
623- for i := 0 ; i < runtime .NumCPU (); i ++ {
624- objs := <- objsChan
625- for _ , obj := range objs {
626- s .SetStateObject (obj )
627- }
628- }
629- }
630- }
631-
632- func (s * StateDB ) preloadStateObject (address []common.Address ) []* StateObject {
633- // Prefer live objects if any is available
634- if s .snap == nil {
635- return nil
636- }
637- hasher := crypto .NewKeccakState ()
638- objs := make ([]* StateObject , 0 , len (address ))
639- for _ , addr := range address {
640- // If no live objects are available, attempt to use snapshots
641- if acc , err := s .snap .Account (crypto .HashData (hasher , addr .Bytes ())); err == nil {
642- if acc == nil {
643- continue
644- }
645- data := & Account {
646- Nonce : acc .Nonce ,
647- Balance : acc .Balance ,
648- CodeHash : acc .CodeHash ,
649- Root : common .BytesToHash (acc .Root ),
650- }
651- if len (data .CodeHash ) == 0 {
652- data .CodeHash = emptyCodeHash
653- }
654- if data .Root == (common.Hash {}) {
655- data .Root = emptyRoot
656- }
657- // Insert into the live set
658- obj := newObject (s , addr , * data )
659- objs = append (objs , obj )
660- }
661- // Do not enable this feature when snapshot is not enabled.
662- }
663- return objs
664- }
665-
666607// getDeletedStateObject is similar to getStateObject, but instead of returning
667608// nil for a deleted state object, it returns the actual object with the deleted
668609// flag set. This is needed by the state journal to revert to the correct s-
@@ -828,6 +769,7 @@ func (s *StateDB) Copy() *StateDB {
828769 stateObjects : make (map [common.Address ]* StateObject , len (s .journal .dirties )),
829770 stateObjectsPending : make (map [common.Address ]struct {}, len (s .stateObjectsPending )),
830771 stateObjectsDirty : make (map [common.Address ]struct {}, len (s .journal .dirties )),
772+ storagePool : s .storagePool ,
831773 refund : s .refund ,
832774 logs : make (map [common.Hash ][]* types.Log , len (s .logs )),
833775 logSize : s .logSize ,
@@ -1626,3 +1568,7 @@ func (s *StateDB) GetDirtyAccounts() []common.Address {
16261568 }
16271569 return accounts
16281570}
1571+
1572+ func (s * StateDB ) GetStorage (address common.Address ) * sync.Map {
1573+ return s .storagePool .getStorage (address )
1574+ }
0 commit comments