-
Notifications
You must be signed in to change notification settings - Fork 4
Serve verified uncommitted blocks to seq-only lookups in CachedStorage #497
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3cebdc0
add requesting verified blocks from retrieve
samliok 4e4a32f
Merge branch 'main' into fix/cachedstorage-seq-only-lookup
samliok 07c6628
cached storage test
samliok 0444b7b
remove comment and check to ensure the returned digest is equal to th…
samliok 0916cfe
add table tests
samliok 9d3f0f0
Merge branch 'main' into fix/cachedstorage-seq-only-lookup
yacovm d9cb088
fix TestInstanceNonValidatorBootstraps: route EpochAwareStorage.Index…
samliok ea55078
remove comment
samliok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,188 @@ | ||
| // Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. | ||
| // See the file LICENSE for licensing terms. | ||
|
|
||
| package simplex | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/ava-labs/simplex/avalanchego" | ||
| "github.com/ava-labs/simplex/common" | ||
| metadata "github.com/ava-labs/simplex/msm" | ||
| "github.com/ava-labs/simplex/testutil" | ||
| "github.com/ava-labs/simplex/wal" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // newTestParsedBlock builds a ParsedBlock with round and seq set to num. | ||
| func newTestParsedBlock(num uint64, payload string) *ParsedBlock { | ||
| return &ParsedBlock{ | ||
| StateMachineBlock: metadata.StateMachineBlock{ | ||
| Metadata: metadata.StateMachineMetadata{ | ||
| SimplexProtocolMetadata: common.ProtocolMetadata{ | ||
| Round: num, | ||
| Seq: num, | ||
| }, | ||
| }, | ||
| InnerBlock: &testInnerBlock{ | ||
| Height_: num, | ||
| TS: time.UnixMilli(1), | ||
| Payload: []byte(payload), | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // TestCachedStorageRetrieve asserts Retrieve against an indexed block at seq 0 | ||
| // and a verified but not yet indexed block at seq 5. A zero digest matches on | ||
| // seq alone, a non-zero digest must match the block's digest exactly. | ||
| func TestCachedStorageRetrieve(t *testing.T) { | ||
| cs := NewCachedStorage(NewMockStorage(t)) | ||
| indexedBlock := newTestParsedBlock(0, "indexed") | ||
| require.NoError(t, cs.Index(t.Context(), indexedBlock, common.Finalization{})) | ||
|
|
||
| verifiedBlockSeq := uint64(5) | ||
| verifiedBlock := newTestParsedBlock(verifiedBlockSeq, "cached") | ||
| cached := &cachedBlock{ | ||
| ParsedBlock: verifiedBlock, | ||
| cache: cs, | ||
| } | ||
| _, err := cached.Verify(t.Context(), common.OnlyVMVerifyOpt) | ||
| require.NoError(t, err) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| seq uint64 | ||
| digest common.Digest | ||
| wantBlock *ParsedBlock | ||
| wantErr error | ||
| }{ | ||
| { | ||
| name: "cached block by seq with zero digest", | ||
| seq: verifiedBlockSeq, | ||
| wantBlock: verifiedBlock, | ||
| }, | ||
| { | ||
| name: "cached block with matching digest", | ||
| seq: verifiedBlockSeq, | ||
| digest: cached.Digest(), | ||
| wantBlock: verifiedBlock, | ||
| }, | ||
| { | ||
| name: "cached block with mismatched digest", | ||
| seq: verifiedBlockSeq, | ||
| digest: common.Digest{1, 2, 3}, | ||
| wantErr: common.ErrBlockNotFound, | ||
| }, | ||
| { | ||
| name: "uncached seq falls through to storage", | ||
| seq: 0, | ||
| wantBlock: indexedBlock, | ||
| }, | ||
| { | ||
| name: "indexed block with mismatched digest", | ||
| seq: 0, | ||
| digest: common.Digest{1, 2, 3}, | ||
| wantErr: common.ErrBlockNotFound, | ||
| }, | ||
| { | ||
| name: "seq not cached or in storage", | ||
| seq: 7, | ||
| wantErr: common.ErrBlockNotFound, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, fin, err := cs.Retrieve(tt.seq, tt.digest) | ||
| if tt.wantErr != nil { | ||
| require.ErrorIs(t, err, tt.wantErr) | ||
| return | ||
| } | ||
| require.NoError(t, err) | ||
| require.Equal(t, tt.wantBlock, got) | ||
| if tt.wantBlock == verifiedBlock { | ||
| require.Nil(t, fin) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestCachedStoragePopulatedByWal asserts that a block restored from the WAL on | ||
| // startup ends up in the instance's CachedStorage, retrievable by seq before it | ||
| // is finalized and indexed. | ||
| func TestCachedStoragePopulatedByWal(t *testing.T) { | ||
| const basePChainHeight = uint64(1) | ||
|
|
||
| // Four equal-weight validators; the node under test is the first. | ||
| numNodes := 4 | ||
| validatorSet := make(metadata.NodeBLSMappings, numNodes) | ||
| for i := range numNodes { | ||
| validatorSet[i] = metadata.NodeBLSMapping{NodeID: avalanchego.NodeID{byte(i + 1)}, BLSKey: []byte{byte(i + 1)}, Weight: 1} | ||
| } | ||
| pChain := newTestPlatformChain(basePChainHeight, map[uint64]metadata.NodeBLSMappings{ | ||
| basePChainHeight: validatorSet, | ||
| }) | ||
|
|
||
| vm := newTestVM() | ||
| vm.pause() | ||
| cops := &testCryptoOps{} | ||
| genesisBlock := &testInnerBlock{Height_: 0, TS: time.Now(), Payload: []byte("genesis")} | ||
| storage := newStorageWithGenesis(t, genesisBlock) | ||
| nodeIDs := validatorSet.Nodes().NodeIDs() | ||
| comm := testutil.NewNoopComm(nodeIDs) | ||
| logger := testutil.MakeLogger(t, 1) | ||
| testWAL := testutil.NewTestWAL(t) | ||
|
|
||
| // The first Simplex block on top of the genesis block. | ||
| genesis := &ParsedBlock{StateMachineBlock: metadata.StateMachineBlock{InnerBlock: genesisBlock}} | ||
| block := newTestParsedBlock(1, "wal block") | ||
| block.Metadata.SimplexProtocolMetadata.Epoch = 1 | ||
| block.Metadata.SimplexProtocolMetadata.Prev = genesis.BlockHeader().Digest | ||
|
|
||
| blockRecord, err := common.BlockRecord(block.BlockHeader(), block.Bytes()) | ||
| require.NoError(t, err) | ||
|
|
||
| // write block record to wal | ||
| require.NoError(t, testWAL.Append(blockRecord)) | ||
|
|
||
| // notarize the block so restoring the WAL keeps it as the round in progress | ||
| quorum := common.Quorum(len(nodeIDs)) | ||
| notarizationRecord, err := testutil.NewNotarizationRecord(logger, cops.CreateSignatureAggregator(validatorSet.Nodes()), block, nodeIDs[:quorum]) | ||
| require.NoError(t, err) | ||
| require.NoError(t, testWAL.Append(notarizationRecord)) | ||
|
|
||
| config := Config{ | ||
| Logger: logger, | ||
| ID: nodeIDs[0], | ||
| VM: vm, | ||
| Storage: storage, | ||
| Sender: comm, | ||
| Broadcaster: comm, | ||
| PlatformChain: pChain, | ||
| CryptoOps: cops, | ||
| LastNonSimplexInnerBlock: genesisBlock, | ||
| WalCreator: storage.CreateWAL, | ||
| ParameterConfig: ParameterConfig{ | ||
| MaxNetworkDelay: 500 * time.Millisecond, | ||
| MaxRoundWindow: 100, | ||
| WALMaxEntryCount: 1024, | ||
| }, | ||
| WALs: []wal.DeletableWAL{testWAL}, | ||
| } | ||
| instance := NewInstance(config) | ||
| require.NoError(t, instance.Start(t.Context())) | ||
| t.Cleanup(instance.Stop) | ||
|
|
||
| // The restored block is verified asynchronously and not indexed, so poll until | ||
| // a seq-only lookup serves it from the cache. | ||
| require.Eventually(t, func() bool { | ||
| got, fin, err := instance.cs.Retrieve(1, common.Digest{}) | ||
| if err != nil || fin != nil { | ||
| return false | ||
| } | ||
| return got.BlockHeader().Digest == block.BlockHeader().Digest | ||
| }, 20*time.Second, 100*time.Millisecond) | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.