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
Bound Initial Sync Cache Size #4844
Merged
Merged
Changes from 74 commits
Commits
Show all changes
83 commits
Select commit
Hold shift + click to select a range
6a60325
bound initial sync
nisdas ba918e3
Merge branch 'master' into OOMFix
nisdas 54a51c3
Merge branch 'OOMFix' of github.com:prysmaticlabs/prysm into OOMFix
rauljordan ef8b9a4
fix lint
rauljordan dc489e9
Revert "Better block attestation inclusion (#4838)"
rauljordan dc5176d
add memory pool
nisdas a8afaa3
more fixes
nisdas dfbe81a
Merge branch 'master' of https://github.com/prysmaticlabs/geth-shardi…
nisdas 386668a
revert changes
nisdas bb103b0
add hack
nisdas 66aefaa
revert hack
nisdas 026415c
push halving
nisdas 3b7258a
bring back hack
nisdas 17d1d11
Merge branch 'master' of https://github.com/prysmaticlabs/geth-shardi…
nisdas 771eca3
increase cache size
nisdas afa5074
more fixes
nisdas 9775983
more changes
nisdas 3ff5133
new fixes
nisdas ee7bc62
Merge branch 'master' of https://github.com/prysmaticlabs/geth-shardi…
nisdas d034213
add test
nisdas 56f9d1c
add reverse test
nisdas 478c495
more tests and clean up
nisdas 6556570
add helper
nisdas 7b2c0ff
more cleanup and tests
nisdas 09c92c9
fix test
nisdas 64c868c
remove code
nisdas c8bdcbe
set gc percent flag
nisdas 21d7c87
lint
nisdas 9cd5d0f
lint
nisdas 0826591
Fix comment formatting
0xKiwi 9500ab5
Fix some formatting
0xKiwi 0887bc8
inverse if statement
nisdas 7df80b7
remove debug log
nisdas ea91ad1
Apply suggestions from code review
nisdas 0fac774
Update beacon-chain/state/getters.go
nisdas b18c68d
Update beacon-chain/db/kv/state.go
nisdas b368a8b
Merge branch 'master' into OOMFix
prestonvanloon 700237d
Merge refs/heads/master into OOMFix
prylabs-bulldozer[bot] e67dd1b
Merge refs/heads/master into OOMFix
prylabs-bulldozer[bot] 2f53667
Merge refs/heads/master into OOMFix
prylabs-bulldozer[bot] 9eabd89
integrate state generator
nisdas a12826c
gaz
nisdas 34b8969
fixes
nisdas 2c55ee9
Merge branch 'master' of https://github.com/prysmaticlabs/geth-shardi…
nisdas f7b53aa
terence's review
nisdas 6cd8218
reduce bound further
nisdas c05b809
fix test
nisdas 7f495c2
separate into new files
nisdas 2dad4cd
gaz
nisdas 3704e7b
mod build file
nisdas 5b51607
add test
nisdas 2e5c6b1
revert changes
nisdas 63bd182
fix test
nisdas c11b029
Update beacon-chain/core/helpers/slot_epoch.go
nisdas 8986e9f
handle edge case
nisdas 86ca9b3
Merge branch 'OOMFix' of https://github.com/prysmaticlabs/geth-shardi…
nisdas 96b56eb
add back test
nisdas 91c18da
fix test again
nisdas d904f3b
handle edge case
nisdas 9bf5e60
Update beacon-chain/blockchain/init_sync_process_block.go
rauljordan 073a164
Update beacon-chain/blockchain/init_sync_process_block.go
rauljordan 4d2c684
Merge refs/heads/master into OOMFix
prylabs-bulldozer[bot] 402cbfa
Merge refs/heads/master into OOMFix
prylabs-bulldozer[bot] 6ef46d6
Merge refs/heads/master into OOMFix
prylabs-bulldozer[bot] 19b13d5
Merge refs/heads/master into OOMFix
prylabs-bulldozer[bot] f2407d4
Merge refs/heads/master into OOMFix
prylabs-bulldozer[bot] 564f3b1
Update beacon-chain/stategen/service_test.go
nisdas 17ded5e
Update beacon-chain/blockchain/init_sync_process_block.go
nisdas 330138f
Update beacon-chain/stategen/service.go
nisdas 21f56b1
Update beacon-chain/stategen/service.go
nisdas 2f2aa2a
raul's review
nisdas 499f672
Merge branch 'OOMFix' of https://github.com/prysmaticlabs/geth-shardi…
nisdas dd096b5
raul's review
nisdas b0ebc75
Merge branch 'master' of https://github.com/prysmaticlabs/geth-shardi…
nisdas 454113d
Merge refs/heads/master into OOMFix
prylabs-bulldozer[bot] 18c5f27
fix refs
nisdas e0d1a26
Merge refs/heads/master into OOMFix
prylabs-bulldozer[bot] f1f571a
Merge refs/heads/master into OOMFix
prylabs-bulldozer[bot] 1ea5d24
Merge branch 'master' of https://github.com/prysmaticlabs/geth-shardi…
nisdas e5ab1fc
terence's review
nisdas 0276ea4
one more fix
nisdas 2da4350
Update beacon-chain/blockchain/init_sync_process_block.go
terencechain 8c67eed
Merge refs/heads/master into OOMFix
prylabs-bulldozer[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
package blockchain | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" | ||
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" | ||
"github.com/prysmaticlabs/prysm/beacon-chain/stategen" | ||
"github.com/prysmaticlabs/prysm/shared/bytesutil" | ||
"github.com/prysmaticlabs/prysm/shared/params" | ||
) | ||
|
||
const maxCacheSize = 70 | ||
const initialSyncCacheSize = 45 | ||
const minimumCacheSize = initialSyncCacheSize / 3 | ||
|
||
func (s *Service) persistCachedStates(ctx context.Context, numOfStates int) error { | ||
oldStates := make([]*stateTrie.BeaconState, 0, numOfStates) | ||
|
||
// Add slots to the map and add epoch boundary states to the slice. | ||
for _, rt := range s.boundaryRoots[:numOfStates-minimumCacheSize] { | ||
oldStates = append(oldStates, s.initSyncState[rt]) | ||
} | ||
|
||
err := s.beaconDB.SaveStates(ctx, oldStates, s.boundaryRoots[:numOfStates-minimumCacheSize]) | ||
if err != nil { | ||
return err | ||
} | ||
for _, rt := range s.boundaryRoots[:numOfStates-minimumCacheSize] { | ||
delete(s.initSyncState, rt) | ||
} | ||
s.boundaryRoots = s.boundaryRoots[numOfStates-minimumCacheSize:] | ||
return nil | ||
} | ||
|
||
// filter out boundary candidates from our currently processed batch of states. | ||
func (s *Service) filterBoundaryCandidates(ctx context.Context, root [32]byte, postState *stateTrie.BeaconState) { | ||
// Only trigger on epoch start. | ||
if !helpers.IsEpochStart(postState.Slot()) { | ||
return | ||
} | ||
|
||
stateSlice := make([][32]byte, 0, len(s.initSyncState)) | ||
// Add epoch boundary roots to slice. | ||
for rt := range s.initSyncState { | ||
stateSlice = append(stateSlice, rt) | ||
} | ||
|
||
sort.Slice(stateSlice, func(i int, j int) bool { | ||
return s.initSyncState[stateSlice[i]].Slot() < s.initSyncState[stateSlice[j]].Slot() | ||
}) | ||
epochLength := params.BeaconConfig().SlotsPerEpoch | ||
|
||
if len(s.boundaryRoots) > 0 { | ||
// Retrieve previous boundary root. | ||
previousBoundaryRoot := s.boundaryRoots[len(s.boundaryRoots)-1] | ||
previousSlot := s.initSyncState[previousBoundaryRoot].Slot() | ||
|
||
// Round up slot number to account for skipped slots. | ||
previousSlot = helpers.RoundUpToNearestEpoch(previousSlot) | ||
if postState.Slot()-previousSlot > epochLength { | ||
targetSlot := postState.Slot() | ||
tempRoots := s.loopThroughCandidates(stateSlice, previousBoundaryRoot, previousSlot, targetSlot) | ||
s.boundaryRoots = append(s.boundaryRoots, tempRoots...) | ||
} | ||
} | ||
s.boundaryRoots = append(s.boundaryRoots, root) | ||
s.pruneOldStates() | ||
s.pruneNonBoundaryStates() | ||
} | ||
|
||
// loop-through the provided candidate roots to filter out which would be appropriate boundary roots. | ||
func (s *Service) loopThroughCandidates(stateSlice [][32]byte, previousBoundaryRoot [32]byte, | ||
previousSlot uint64, targetSlot uint64) [][32]byte { | ||
tempRoots := [][32]byte{} | ||
epochLength := params.BeaconConfig().SlotsPerEpoch | ||
|
||
rauljordan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Loop through current states to filter for valid boundary states. | ||
for i := len(stateSlice) - 1; stateSlice[i] != previousBoundaryRoot && i >= 0; i-- { | ||
currentSlot := s.initSyncState[stateSlice[i]].Slot() | ||
// Skip if the current slot is larger than the previous epoch | ||
// boundary. | ||
if currentSlot > targetSlot-epochLength { | ||
continue | ||
} | ||
tempRoots = append(tempRoots, stateSlice[i]) | ||
|
||
// Switch target slot if the current slot is greater than | ||
// 1 epoch boundary from the previously saved boundary slot. | ||
if currentSlot > previousSlot+epochLength { | ||
currentSlot = helpers.RoundUpToNearestEpoch(currentSlot) | ||
targetSlot = currentSlot | ||
continue | ||
} | ||
break | ||
} | ||
// Reverse to append the roots in ascending order corresponding | ||
// to the respective slots. | ||
tempRoots = bytesutil.ReverseBytes32Slice(tempRoots) | ||
return tempRoots | ||
} | ||
|
||
// prune for states past the current finalized checkpoint. | ||
func (s *Service) pruneOldStates() { | ||
prunedBoundaryRoots := [][32]byte{} | ||
for _, rt := range s.boundaryRoots { | ||
st, ok := s.initSyncState[rt] | ||
// Skip non-existent roots. | ||
if !ok { | ||
continue | ||
} | ||
if st.Slot() < helpers.StartSlot(s.FinalizedCheckpt().Epoch) { | ||
delete(s.initSyncState, rt) | ||
continue | ||
} | ||
prunedBoundaryRoots = append(prunedBoundaryRoots, rt) | ||
} | ||
s.boundaryRoots = prunedBoundaryRoots | ||
} | ||
|
||
// prune cache for non-boundary states. | ||
func (s *Service) pruneNonBoundaryStates() { | ||
boundaryMap := make(map[[32]byte]bool) | ||
for i := range s.boundaryRoots { | ||
boundaryMap[s.boundaryRoots[i]] = true | ||
} | ||
for rt := range s.initSyncState { | ||
if !boundaryMap[rt] { | ||
delete(s.initSyncState, rt) | ||
} | ||
} | ||
} | ||
|
||
func (s *Service) pruneOldNonFinalizedStates() { | ||
stateSlice := make([][32]byte, 0, len(s.initSyncState)) | ||
// Add epoch boundary roots to slice. | ||
for rt := range s.initSyncState { | ||
stateSlice = append(stateSlice, rt) | ||
} | ||
|
||
// Sort by slots. | ||
sort.Slice(stateSlice, func(i int, j int) bool { | ||
return s.initSyncState[stateSlice[i]].Slot() < s.initSyncState[stateSlice[j]].Slot() | ||
}) | ||
|
||
boundaryMap := make(map[[32]byte]bool) | ||
for i := range s.boundaryRoots { | ||
boundaryMap[s.boundaryRoots[i]] = true | ||
} | ||
for _, rt := range stateSlice[:initialSyncCacheSize] { | ||
if boundaryMap[rt] { | ||
continue | ||
} | ||
delete(s.initSyncState, rt) | ||
} | ||
} | ||
|
||
func (s *Service) generateState(ctx context.Context, startRoot [32]byte, endRoot [32]byte) (*stateTrie.BeaconState, error) { | ||
preState, err := s.beaconDB.State(ctx, startRoot) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if preState == nil { | ||
return nil, errors.New("finalized state does not exist in db") | ||
} | ||
|
||
endBlock, err := s.beaconDB.Block(ctx, endRoot) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if endBlock == nil { | ||
return nil, errors.New("provided block root does not have block saved in the db") | ||
} | ||
stGen := stategen.New(s.beaconDB) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you initialize this properly in node.go and pass it down to the service struct so |
||
|
||
log.Warnf("Generating missing state of slot %d and root %#x", endBlock.Block.Slot, endRoot) | ||
postState, err := stGen.GenerateState(ctx, preState, endBlock) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "could not generate state") | ||
} | ||
return postState, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this panic out of range?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if used normally without context , yes. But we only use it when we have
len(s.boundaryRoots) > 45
, so this will never happen in how it is currently used.