Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Fast Copy of Trie #5221

Merged
merged 2 commits into from Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions beacon-chain/blockchain/head.go
Expand Up @@ -127,17 +127,17 @@ func (s *Service) saveHeadNoDB(ctx context.Context, b *ethpb.SignedBeaconBlock,
return errors.Wrap(err, "could not retrieve head state in DB")
}
} else {
headState, err = s.beaconDB.State(ctx, r)
if err != nil {
return errors.Wrap(err, "could not retrieve head state in DB")
s.initSyncStateLock.RLock()
cachedHeadState, ok := s.initSyncState[r]
if ok {
headState = cachedHeadState
}
s.initSyncStateLock.RUnlock()
if headState == nil {
s.initSyncStateLock.RLock()
cachedHeadState, ok := s.initSyncState[r]
if ok {
headState = cachedHeadState
headState, err = s.beaconDB.State(ctx, r)
if err != nil {
return errors.Wrap(err, "could not retrieve head state in DB")
}
s.initSyncStateLock.RUnlock()
}
}
if headState == nil {
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/blockchain/process_block.go
Expand Up @@ -229,7 +229,7 @@ func (s *Service) onBlockInitialSyncStateTransition(ctx context.Context, signed
} else {
s.initSyncStateLock.Lock()
defer s.initSyncStateLock.Unlock()
s.initSyncState[root] = postState.Copy()
s.initSyncState[root] = postState.FastCopy()
s.filterBoundaryCandidates(ctx, root, postState)
}

Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/blockchain/process_block_helpers.go
Expand Up @@ -99,7 +99,7 @@ func (s *Service) verifyBlkPreState(ctx context.Context, b *ethpb.BeaconBlock) (
}
return preState, nil // No copy needed from newly hydrated DB object.
}
return preState.Copy(), nil
return preState.FastCopy(), nil
}

// verifyBlkDescendant validates input block root is a descendant of the
Expand Down
23 changes: 23 additions & 0 deletions beacon-chain/state/state_trie.go
Expand Up @@ -177,6 +177,29 @@ func (b *BeaconState) Copy() *BeaconState {
return dst
}

// FastCopy is used to copy all the field trie contents and
// empty out the references to it in the source state. This
// is used when we have a state that we know will most
// likely not be used anytime in the future.
func (b *BeaconState) FastCopy() *BeaconState {
newState := b.Copy()
for fldIdx, fieldTrie := range b.stateFieldLeaves {
if fieldTrie.reference != nil {
fieldTrie.Lock()
fieldTrie.MinusRef()
fieldTrie.Unlock()
b.rebuildTrie[fldIdx] = true
b.dirtyFields[fldIdx] = true
b.stateFieldLeaves[fldIdx] = &FieldTrie{
field: fldIdx,
reference: &reference{1},
Mutex: new(sync.Mutex),
}
}
}
return newState
}

// HashTreeRoot of the beacon state retrieves the Merkle root of the trie
// representation of the beacon state based on the eth2 Simple Serialize specification.
func (b *BeaconState) HashTreeRoot(ctx context.Context) ([32]byte, error) {
Expand Down