Skip to content

Commit

Permalink
Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Juliusan committed Oct 24, 2023
1 parent e044475 commit 2be0a68
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions packages/chain/statemanager/sm_gpa/state_manager_gpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ func (smT *stateManagerGPA) pruneStore(commitment *state.L1Commitment, stateInde
// to the oldest unpruned block. Usually some block chain is currently known.
// However the passed commitment might be newer and not contained in currently
// known chain of blocks. The function attempts to use currently known chain as
// much ass possible: while building new chain of blocks, it attempts to find a
// much as possible: while building new chain of blocks, it attempts to find a
// place to merge it with already known chain. After the merge it checks if the
// end of the merged chain is still what it should be.
// This function is extensively tested in `state_manager_gpa_cob_test.go` file.
Expand Down Expand Up @@ -690,25 +690,29 @@ func (smT *stateManagerGPA) updateChainOfBlocks(commitment *state.L1Commitment,
}

var err error
// Find chain of newest blocks: the ones, that are newer than currently known chain
// Find chain of newest blocks: the ones, that has larger indexes than currently known chain
if lastKnownBi != nil {
for err == nil && bi != nil && bi.blockIndex > lastKnownBi.blockIndex && smT.store.HasTrieRoot(bi.trieRoot) {
cob.AddStart(bi)
bi, err = GetPreviousBlockInfoFun(bi)
}
}
// Remove blocks from currently known chain, that have larger indexes than
// the newest block: they are older than the newest block, but on the different
// branch of the chain. TODO: Instead of removing, the blocks should probably
// be pruned.
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)``
// Try to find a place to merge newest blocks chain with currently known blocks 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
// is reached in previous cycle, trie roots should also match. In an unlikely
// is reached in previous cycles, trie roots should also match. In an unlikely
// event of chain split, each iteration of this cycle fetches one older block
// to the newest blocks chain and drops (maybe it should prune) one
// to the newest blocks chain and drops (TODO: maybe it should prune) one
// newest ("last known") block from currently known blocks chain. Hence,
// this comparison of block indexes should still hold.
if bi.blockIndex != lastKnownBi.blockIndex {
Expand All @@ -721,13 +725,12 @@ func (smT *stateManagerGPA) updateChainOfBlocks(commitment *state.L1Commitment,
_ = smT.chainOfBlocks.RemoveEnd()
lastKnownBi = GetLastKnownBlockInfoFun()
}
// Something failed when trying to construct a new chain
if err != nil {
smT.log.Errorf("Failed to obtain previous block info: %v", err)
return
}
if lastKnownBi == nil { // either there were no currently known block chain,
// or newest block chain had no common block infos
if lastKnownBi == nil { // either there were no currently known blocks chain,
// or newest blocks chain had no common block infos
// (which is very unlikely): fill the chain from the store.
for err == nil && bi != nil && smT.store.HasTrieRoot(bi.trieRoot) {
cob.AddStart(bi)
Expand All @@ -740,12 +743,12 @@ func (smT *stateManagerGPA) updateChainOfBlocks(commitment *state.L1Commitment,
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
} else if bi.trieRoot.Equals(lastKnownBi.trieRoot) { // Here is the the place to merge newest blocks chain with currently known blocks chain
// Normally newest blocks chain should contain only several (usually, 1)
// block indexes and currently known block chain should contain at least
// `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
// block infos and currently known blocks chain should contain at least
// `PruningMinStatesToKeep` block infos, but on a sudden enabling of pruning
// might contain millions of them. Therefore it is more efficient to copy
// newest blocks chain to the currently known one compared to doing it
// the other way round. Let's merge them this way.
for cob.Length() > 0 {
bi = cob.RemoveStart()
Expand Down

0 comments on commit 2be0a68

Please sign in to comment.