-
Notifications
You must be signed in to change notification settings - Fork 4
Adding Support For Non-Validators #393
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
17 commits
Select commit
Hold shift + click to select a range
5d8c632
add non-validator support
samliok f17ba1c
update comment
samliok 2bc67a8
better threshold
samliok 874fab9
merge conflicts
samliok b320960
merge conflicts
samliok 6af298b
verbose if/else
samliok 19815fd
send replication not digest request
samliok d4259c9
eligible signers
samliok 7e19ed3
improving logging and removing helper
samliok 7ff7b66
nextSeqToCommit, add check + naming
samliok 15a3968
remove unused digest map
samliok 1641a4d
garbage collect
samliok 676851b
received in one
samliok ef7a6d9
sealing info update to not have epoch
samliok 471908c
Merge branch 'main' into non-validator
yacovm b55de42
consolidate files
samliok 90dd295
remove digest requests
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
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,218 @@ | ||
| // Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. | ||
| // See the file LICENSE for licensing terms. | ||
|
|
||
| package nonvalidator | ||
|
samliok marked this conversation as resolved.
|
||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/ava-labs/simplex/common" | ||
| "github.com/ava-labs/simplex/testutil" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| var testNodes = common.Nodes{ | ||
| {Id: common.NodeID{1}, Weight: 1}, | ||
| {Id: common.NodeID{2}, Weight: 1}, | ||
| {Id: common.NodeID{3}, Weight: 1}, | ||
| {Id: common.NodeID{4}, Weight: 1}, | ||
| } | ||
|
|
||
| var genesis = testutil.NewTestBlock(common.ProtocolMetadata{ | ||
| Seq: 0, | ||
| Round: 0, | ||
| Epoch: 0, | ||
| }, common.Blacklist{}) | ||
|
|
||
| type messageInfo struct { | ||
| msg *common.Message | ||
| from common.NodeID | ||
| } | ||
|
|
||
| // testChain is a helper that book-keeps the current chain-tip, alongside any | ||
| // blocks and finalizations indexed on the chain. It allows easy creation of block and sealing blocks. | ||
| // It also manages validator sets, and the SignatureAggregatorCreator required by non-validators to verify finalizations. | ||
| type testChain struct { | ||
| *testutil.InMemStorage | ||
| t *testing.T | ||
|
|
||
| // seq, epoch, prev, sealingBlockHash defines the current tip of testChain. | ||
| // the next block uses seq + 1, epoch, prevDigest = digest, etc... | ||
| seq uint64 | ||
| epoch uint64 | ||
| digest common.Digest | ||
| sealingBlockHash common.Digest | ||
|
|
||
| validatorSets map[uint64]common.Nodes | ||
| } | ||
|
|
||
| func (c *testChain) String() string { | ||
| return fmt.Sprintf("TestChain: Current Epoch: %d, Current Seq: %d", c.epoch, c.seq) | ||
| } | ||
|
|
||
| // newSeededChain returns a testChain whose storage is indexed through seq=lastSeq: | ||
| // | ||
| // seq 0 — genesis (epoch 0) | ||
| // seq 1 — sealing block opening epoch 1 (validatorSet = testNodes) | ||
| // seq 2..lastSeq — epoch 1 blocks | ||
| func newSeededChain(t *testing.T, nodes common.Nodes, lastSeq uint64) *testChain { | ||
| require.GreaterOrEqual(t, lastSeq, uint64(1), "lastSeq must be >= 1 (0 is genesis, 1 is the first epoch's sealing block)") | ||
|
|
||
| tc := &testChain{ | ||
| InMemStorage: testutil.NewInMemStorage(), | ||
| t: t, | ||
| digest: genesis.Digest, | ||
| validatorSets: make(map[uint64]common.Nodes), | ||
| seq: 0, // set for clarity | ||
| epoch: 0, | ||
| } | ||
| require.NoError(t, tc.Index(context.Background(), genesis, common.Finalization{})) | ||
| tc.validatorSets[0] = nodes | ||
|
|
||
| // firstSimplex comes after genesis | ||
| sealingBlock := tc.appendFirstSimplexAfterGenesis(nodes) | ||
| finalization := tc.newFinalization(sealingBlock) | ||
| require.NoError(t, tc.Index(context.Background(), sealingBlock, finalization)) | ||
|
|
||
| for tc.seq < lastSeq { | ||
| b := tc.appendBlock() | ||
| finalization := tc.newFinalization(b) | ||
|
|
||
| require.NoError(t, tc.Index(context.Background(), b, finalization)) | ||
| } | ||
|
|
||
| return tc | ||
| } | ||
|
|
||
| // newSnowToSimplexChain returns a testChain whose storage is indexed through seq=lastSnowSeq: | ||
| func newSnowToSimplexChain(t *testing.T, lastSnowSeq uint64) *testChain { | ||
| require.GreaterOrEqual(t, lastSnowSeq, uint64(1), "genesis must be indexed") | ||
|
|
||
| tc := &testChain{ | ||
| InMemStorage: testutil.NewInMemStorage(), | ||
| t: t, | ||
| digest: genesis.Digest, | ||
| validatorSets: make(map[uint64]common.Nodes), | ||
| seq: 0, | ||
| epoch: 0, | ||
| } | ||
|
|
||
| // genesis | ||
| require.NoError(t, tc.Index(context.Background(), genesis, common.Finalization{})) | ||
|
|
||
| for tc.seq < lastSnowSeq { | ||
| b := tc.appendBlock() | ||
| require.NoError(t, tc.Index(context.Background(), b, common.Finalization{})) | ||
| } | ||
|
|
||
| require.Equal(t, tc.seq, lastSnowSeq) | ||
| return tc | ||
| } | ||
|
|
||
| // appendBlock advances the chain by one non-sealing block in the current | ||
| // epoch. The block is constructed but NOT indexed. | ||
| func (tc *testChain) appendBlock() *testutil.TestBlock { | ||
| tc.seq++ | ||
| block := newBlock(tc.seq, tc.epoch, tc.digest) | ||
| tc.digest = block.Digest | ||
| return block | ||
| } | ||
|
|
||
| func (tc *testChain) newFinalization(b common.VerifiedBlock) common.Finalization { | ||
| nodes, ok := tc.validatorSets[b.BlockHeader().Epoch] | ||
| require.True(tc.t, ok, "Validator set expected to have before creating a new finalization", b.BlockHeader().Epoch, b.BlockHeader().Seq) | ||
| sigAgg := tc.signatureAggregatorCreator(nodes) | ||
| finalization, _ := testutil.NewFinalizationRecord(tc.t, sigAgg, b, nodes.NodeIDs()) | ||
|
|
||
| return finalization | ||
| } | ||
|
|
||
| // appendSealing advances the chain by one sealing block announcing nextEpoch | ||
| // and validatorSet. The sealing block's metadata.Epoch is the current epoch; | ||
| // subsequent appendBlock calls live in nextEpoch. Not indexed. | ||
| func (tc *testChain) appendSealing(validatorSet common.Nodes) *sealingTestBlock { | ||
| tc.seq++ | ||
|
|
||
| block := newSealingTestBlock(tc.seq, tc.epoch, tc.digest, &common.SealingBlockInfo{ | ||
| ValidatorSet: validatorSet, | ||
| PrevSealingBlockHash: tc.sealingBlockHash, | ||
| }) | ||
| tc.digest = block.Digest | ||
| tc.epoch = tc.seq | ||
| tc.validatorSets[tc.epoch] = validatorSet | ||
| tc.sealingBlockHash = block.Digest | ||
| return block | ||
| } | ||
|
|
||
| // the first simplex block must return sealing block information | ||
| func (tc *testChain) appendFirstSimplexAfterGenesis(validatorSet common.Nodes) *sealingTestBlock { | ||
| lastBlock, _, err := tc.Retrieve(tc.seq) | ||
| tc.seq++ | ||
| firstEverEpoch := tc.seq | ||
| require.NoError(tc.t, err) | ||
|
|
||
| block := newSealingTestBlock(tc.seq, firstEverEpoch, tc.digest, &common.SealingBlockInfo{ | ||
| ValidatorSet: validatorSet, | ||
| PrevSealingBlockHash: lastBlock.BlockHeader().Digest, | ||
| }) | ||
| tc.digest = block.Digest | ||
| tc.epoch = firstEverEpoch | ||
| tc.sealingBlockHash = block.Digest | ||
| tc.validatorSets[firstEverEpoch] = validatorSet | ||
| return block | ||
| } | ||
|
|
||
| func (tc *testChain) signatureAggregatorCreator(nodes []common.Node) common.SignatureAggregator { | ||
| isQuorumFunc := func(signatures []common.NodeID) bool { | ||
| count := 0 | ||
| nodeSet := make(map[string]struct{}) | ||
| for _, node := range nodes { | ||
| nodeSet[node.Id.String()] = struct{}{} | ||
| } | ||
|
|
||
| for _, sig := range signatures { | ||
| if _, ok := nodeSet[sig.String()]; ok { | ||
| count++ | ||
| } | ||
| } | ||
|
|
||
| return count >= common.Quorum(len(nodes)) | ||
| } | ||
| return &testutil.TestSignatureAggregator{ | ||
| IsQuorumFunc: isQuorumFunc, | ||
| N: len(nodes), | ||
| } | ||
| } | ||
|
|
||
| // addEpochs adds sealing blocks at epochs, and normal blocks in between | ||
| func (tc *testChain) addEpochs(epochs ...uint64) { | ||
| // ensure that the new epoch we are adding is not already indexed | ||
| require.Greater(tc.t, epochs[0], tc.seq) | ||
|
|
||
| for _, epoch := range epochs { | ||
| for tc.seq < epoch-1 { | ||
| b := tc.appendBlock() | ||
| finalization := tc.newFinalization(b) | ||
| require.NoError(tc.t, tc.Index(context.Background(), b, finalization)) | ||
| } | ||
| validatorSet, ok := tc.validatorSets[tc.epoch] | ||
| require.True(tc.t, ok) | ||
|
|
||
| newNodes := append(validatorSet, common.Node{ | ||
| Id: common.NodeID{byte(epoch)}, | ||
| Weight: 1, | ||
| }) | ||
| sealing := tc.appendSealing(newNodes) | ||
| finalization := tc.newFinalization(sealing) | ||
| require.NoError(tc.t, tc.Index(context.Background(), sealing, finalization)) | ||
| } | ||
| } | ||
|
|
||
| func (tc *testChain) nodes() common.Nodes { | ||
| latestValidatorSet, ok := tc.validatorSets[tc.epoch] | ||
| require.True(tc.t, ok) | ||
|
|
||
| return latestValidatorSet | ||
| } | ||
Oops, something went wrong.
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.