Skip to content

Commit

Permalink
Some more tests and BugFixes for picking blocks for pruning algorythm
Browse files Browse the repository at this point in the history
  • Loading branch information
Juliusan committed Oct 23, 2023
1 parent e9755f4 commit e044475
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
15 changes: 11 additions & 4 deletions packages/chain/statemanager/sm_gpa/state_manager_gpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ func (smT *stateManagerGPA) commitStateDraft(stateDraft state.StateDraft) state.
stateIndex := block.StateIndex()
smT.metrics.BlockIndexCommitted(stateIndex)
if smT.pruningNeeded() {
smT.pruneStore(block.PreviousL1Commitment(), stateIndex)
smT.pruneStore(block.PreviousL1Commitment(), stateIndex-1)
}
smT.output.addBlockCommitted(stateIndex, block.L1Commitment())
return block
Expand Down Expand Up @@ -697,6 +697,12 @@ func (smT *stateManagerGPA) updateChainOfBlocks(commitment *state.L1Commitment,
bi, err = GetPreviousBlockInfoFun(bi)
}
}
if err == nil && bi != nil {
for lastKnownBi != nil && lastKnownBi.blockIndex > bi.blockIndex {
_ = smT.chainOfBlocks.RemoveEnd()
lastKnownBi = GetLastKnownBlockInfoFun()
}
}
// Try to find a place to merge newest block chain with currently known block chain: `bi.trieRoot.Equals(lastKnownBi.trieRoot)``
for err == nil && bi != nil && lastKnownBi != nil && !bi.trieRoot.Equals(lastKnownBi.trieRoot) && smT.store.HasTrieRoot(bi.trieRoot) {
// Normally, no iteration of this cycle should occur: once a common index
Expand Down Expand Up @@ -732,10 +738,12 @@ func (smT *stateManagerGPA) updateChainOfBlocks(commitment *state.L1Commitment,
return
}
smT.chainOfBlocks = cob
} else if bi == nil { // origin block has been reached
smT.chainOfBlocks = cob
} else if bi.trieRoot.Equals(lastKnownBi.trieRoot) { // Here is the the place to merge newest block chain with currently known block chain
// Normally newest blocks chain should contain only several (usually, 1)
// block indexes and currently known block chain should contain at least
// `PruningMinStatesToKeep` indexes and on a sudden enabling of pruning
// `PruningMinStatesToKeep` indexes, but on a sudden enabling of pruning
// might contain millions of them. Therefore it is more effective to copy
// newest block chain to the currently known one compared to doing it
// the other way round. Let's merge them this way.
Expand All @@ -760,8 +768,7 @@ func (smT *stateManagerGPA) updateChainOfBlocks(commitment *state.L1Commitment,
smT.log.Errorf("Failed to obtain previous block info: %v", err)
return
}
} else { // bi == nil, which means that origin block has been reached or
// smT.store.HasTrieRoot(bi.trieRoot), which means that this block
} else { // !smT.store.HasTrieRoot(bi.trieRoot), which means that this block
// has already been pruned from the store.
smT.chainOfBlocks = cob
}
Expand Down
26 changes: 22 additions & 4 deletions packages/chain/statemanager/sm_gpa/state_manager_gpa_cob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ func TestChainOfBlocksMergeAllSomeHistory(t *testing.T) {
runTestChainOfBlocks(t, log, bf, store, sm, blocksToCommit, blocksToPrune, blocksInChain, blocksExpected)
}

func TestChainOfBlocksMergeMiddleFullHistory(t *testing.T) {
totalBlocks := 15
branchFrom := 9
branchLength := 5
func testChainOfBlocksMergeMiddleFullHistory(t *testing.T, totalBlocks, branchFrom, branchLength int) {
log, bf, store, sm := initTestChainOfBlocks(t)
originalBlocks := bf.GetBlocks(totalBlocks, 1)
branchBlocks := bf.GetBlocksFrom(branchLength, 1, originalBlocks[branchFrom].L1Commitment(), 2)
Expand All @@ -143,6 +140,27 @@ func TestChainOfBlocksMergeMiddleFullHistory(t *testing.T) {
runTestChainOfBlocks(t, log, bf, store, sm, blocksToCommit, nil, blocksInChain, prependOriginBlock(bf, blocksToCommit))
}

func TestChainOfBlocksMergeMiddleFullHistory(t *testing.T) {
totalBlocks := 15
branchFrom := 9
branchLength := 5
testChainOfBlocksMergeMiddleFullHistory(t, totalBlocks, branchFrom, branchLength)
}

func TestChainOfBlocksMergeMiddleFullHistoryLonger(t *testing.T) {
totalBlocks := 15
branchFrom := 9
branchLength := 10
testChainOfBlocksMergeMiddleFullHistory(t, totalBlocks, branchFrom, branchLength)
}

func TestChainOfBlocksMergeMiddleFullHistoryShorter(t *testing.T) {
totalBlocks := 15
branchFrom := 9
branchLength := 3
testChainOfBlocksMergeMiddleFullHistory(t, totalBlocks, branchFrom, branchLength)
}

func TestChainOfBlocksMergeMiddleSomeHistory(t *testing.T) {
totalBlocks := 15
branchFrom := 9
Expand Down

0 comments on commit e044475

Please sign in to comment.