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

Blocks: Bump min to save based on catchpoint support #5927

Merged
merged 5 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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: 15 additions & 1 deletion ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,25 @@ func (l *Ledger) notifyCommit(r basics.Round) basics.Round {
if l.archival {
// Do not forget any blocks.
minToSave = 0
} else if minCatchpointsRoundsLookback := l.calcMinCatchpointRoundsLookback(); minCatchpointsRoundsLookback > 0 {
catchpointsMinToSave := r.SubSaturate(minCatchpointsRoundsLookback)
if catchpointsMinToSave < minToSave {
minToSave = catchpointsMinToSave
}
}
gmalouf marked this conversation as resolved.
Show resolved Hide resolved

return minToSave
}

func (l *Ledger) calcMinCatchpointRoundsLookback() basics.Round {
// cfg.StoresCatchpoints checks that CatchpointInterval is positive
if !l.cfg.StoresCatchpoints() || l.cfg.CatchpointFileHistoryLength == 0 {
return 0
}

return basics.Round(2 * l.cfg.CatchpointInterval)
}

// GetLastCatchpointLabel returns the latest catchpoint label that was written to the
// database.
func (l *Ledger) GetLastCatchpointLabel() string {
Expand Down Expand Up @@ -901,7 +915,7 @@ func (l *Ledger) FlushCaches() {
// Validate uses the ledger to validate block blk as a candidate next block.
// It returns an error if blk is not the expected next block, or if blk is
// not a valid block (e.g., it has duplicate transactions, overspends some
// account, etc).
// account, etc.).
func (l *Ledger) Validate(ctx context.Context, blk bookkeeping.Block, executionPool execpool.BacklogPool) (*ledgercore.ValidatedBlock, error) {
delta, err := eval.Eval(ctx, l, blk, true, l.verifiedTxnCache, executionPool, l.tracer)
if err != nil {
Expand Down
74 changes: 74 additions & 0 deletions ledger/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3287,3 +3287,77 @@ func TestLedgerMaxBlockHistoryLookback(t *testing.T) {
require.Error(t, err)
require.Empty(t, blk)
}

func TestLedgerRetainMinOffCatchpointInterval(t *testing.T) {
partitiontest.PartitionTest(t)
// This test is to ensure that the ledger retains the minimum number of blocks off the catchpoint interval.
blocksToMake := 5000

// Cases:
// 1. Base Case: Archival = false, Stores catchpoints returns true, CatchpointFileHistoryLength = >= 1 - implies catchpoint interval > 0 - min formula
// 2. Archival = true, stores catchpoints returns false - we keep all blocks anyway
// 3. Archival = false, stores catchpoints returns false - we don't modify minToSave
// 4. Condition: Archival = false, storesCatchpoints returns true, CatchpointFileHistoryLength is -1 - keep all catchpoint files
// 5. Condition: Archival = false, storesCatchpoints returns true, CatchpointFileHistoryLength is 365 - the config default setting

catchpointIntervalBlockRetentionTestCases := []struct {
storeCatchpoints bool
archival bool
catchpointFileHistoryLength int
expectedOldestBlock basics.Round
}{
{true, false, 1, 1000}, // should use min catchpoint formula
algorandskiy marked this conversation as resolved.
Show resolved Hide resolved
{false, true, 1, 1}, // all blocks get retained, archival mode dictates
{false, false, 1, basics.Round(uint64(blocksToMake) - 2800)}, // should not modify min blocks retained based on catchpoint interval
{true, false, -1, 1000}, // should use min formula, this is the keep all catchpoints setting
{true, false, 365, 1000}, // should use min formula, this is the default setting for catchpoint file history length
}
for _, tc := range catchpointIntervalBlockRetentionTestCases {
func() {
genBalances, _, _ := ledgertesting.NewTestGenesis()
var genHash crypto.Digest
crypto.RandBytes(genHash[:])
cfg := config.GetDefaultLocal()
// set config properties based on test case
cfg.MaxBlockHistoryLookback = 0 // max block history lookback is not used in this test
if tc.storeCatchpoints {
cfg.CatchpointTracking = config.CatchpointTrackingModeStored
cfg.CatchpointInterval = 2000
} else {
cfg.CatchpointInterval = 0 // sufficient for cfg.StoresCatchpoints() to return false
}
cfg.CatchpointFileHistoryLength = tc.catchpointFileHistoryLength

l := newSimpleLedgerFull(t, genBalances, protocol.ConsensusCurrentVersion, genHash, cfg,
func(cfg *simpleLedgerCfg) { cfg.notArchival = !tc.archival })
defer l.Close()

for i := 0; i < blocksToMake; i++ {
algorandskiy marked this conversation as resolved.
Show resolved Hide resolved
eval := nextBlock(t, l)
endBlock(t, l, eval)
}

require.Equal(t, basics.Round(blocksToMake), l.Latest())

// Default for archival nodes
blockToCheckUnavailable := basics.Round(0)

if !tc.archival {
blockToCheckUnavailable = tc.expectedOldestBlock - 500
}

// make sure we can get the last `expectedOldestBlock` blocks
blkA, err := l.Block(tc.expectedOldestBlock)

require.NoError(t, err)
require.NotEmpty(t, blkA)

if blockToCheckUnavailable > 0 {
blkU, err1 := l.Block(blockToCheckUnavailable)
require.Error(t, err1)
require.Empty(t, blkU)
}
}()
}

}