Skip to content

Commit

Permalink
avoid sharedPool effects on other modules
Browse files Browse the repository at this point in the history
  • Loading branch information
flywukong committed Mar 21, 2022
1 parent 63edb9d commit 5c2222a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2101,7 +2101,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
if parent == nil {
parent = bc.GetHeader(block.ParentHash(), block.NumberU64()-1)
}
statedb, err := state.New(parent.Root, bc.stateCache, bc.snaps)
statedb, err := state.NewWithSharedPool(parent.Root, bc.stateCache, bc.snaps)
if err != nil {
return it.index, err
}
Expand Down
14 changes: 9 additions & 5 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ func newObject(db *StateDB, address common.Address, data Account) *StateObject {
data.Root = emptyRoot
}
var storageMap *sync.Map
storageMap = nil
// Check whether the storage exist in pool, new originStorage if not exist
if db != nil {
if db != nil && db.sharedStorage != nil {
storageMap = db.GetOrInsertStorage(address)
}

Expand Down Expand Up @@ -209,11 +210,14 @@ func (s *StateObject) getStorageKey(key common.Hash) (common.Hash, bool) {
return value, true
}
// if L1 cache miss, try to get it from shared pool
val, ok := s.sharedOriginMap.Load(key)
if !ok {
return common.Hash{}, false
if s.sharedOriginMap != nil {
val, ok := s.sharedOriginMap.Load(key)
if !ok {
return common.Hash{}, false
}
return val.(common.Hash), ok
}
return val.(common.Hash), ok
return common.Hash{}, false
}

func (s *StateObject) setStorgeKey(key common.Hash, value common.Hash) {
Expand Down
12 changes: 11 additions & 1 deletion core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error)
return newStateDB(root, db, snaps)
}

// NewWithSharedPool creates a new state with sharedStorge on layer 1.5
func NewWithSharedPool(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) {
statedb, err := newStateDB(root, db, snaps)
if err != nil {
return nil, err
}
statedb.sharedStorage = NewSharedStorage()
return statedb, nil
}

func newStateDB(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) {
sdb := &StateDB{
db: db,
Expand All @@ -158,7 +168,7 @@ func newStateDB(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB,
stateObjects: make(map[common.Address]*StateObject, defaultNumOfSlots),
stateObjectsPending: make(map[common.Address]struct{}, defaultNumOfSlots),
stateObjectsDirty: make(map[common.Address]struct{}, defaultNumOfSlots),
sharedStorage: NewSharedStorage(),
sharedStorage: nil,
isPrefetchDb: false,
logs: make(map[common.Hash][]*types.Log, defaultNumOfSlots),
preimages: make(map[common.Hash][]byte),
Expand Down

0 comments on commit 5c2222a

Please sign in to comment.