|
| 1 | +package sync_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/NethermindEth/juno/consensus/driver" |
| 8 | + "github.com/NethermindEth/juno/consensus/mocks" |
| 9 | + "github.com/NethermindEth/juno/consensus/proposal" |
| 10 | + "github.com/NethermindEth/juno/consensus/starknet" |
| 11 | + consensusSync "github.com/NethermindEth/juno/consensus/sync" |
| 12 | + "github.com/NethermindEth/juno/consensus/types" |
| 13 | + "github.com/NethermindEth/juno/core" |
| 14 | + "github.com/NethermindEth/juno/core/felt" |
| 15 | + "github.com/NethermindEth/juno/p2p/sync" |
| 16 | + "github.com/NethermindEth/juno/utils" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | + "go.uber.org/mock/gomock" |
| 19 | +) |
| 20 | + |
| 21 | +type mockSyncService struct { |
| 22 | + inCh chan sync.BlockBody // relays blocks to blockCh |
| 23 | +} |
| 24 | + |
| 25 | +func newMockSyncService(inCh chan sync.BlockBody) mockSyncService { |
| 26 | + return mockSyncService{ |
| 27 | + inCh: inCh, |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func (m *mockSyncService) SyncToChannel(ctx context.Context, blockCh chan<- sync.BlockBody) error { |
| 32 | + for { |
| 33 | + select { |
| 34 | + case <-ctx.Done(): |
| 35 | + return nil |
| 36 | + case committedBlock := <-m.inCh: |
| 37 | + blockCh <- committedBlock |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func (m *mockSyncService) recieveBlock(block sync.BlockBody) { |
| 43 | + m.inCh <- block |
| 44 | +} |
| 45 | + |
| 46 | +func TestSync(t *testing.T) { |
| 47 | + ctrl := gomock.NewController(t) |
| 48 | + defer ctrl.Finish() |
| 49 | + |
| 50 | + ctx, cancel := context.WithCancel(t.Context()) |
| 51 | + defer cancel() |
| 52 | + |
| 53 | + stateMachine := mocks.NewMockStateMachine[starknet.Value, starknet.Hash, starknet.Address](ctrl) |
| 54 | + stateMachine.EXPECT().ReplayWAL().AnyTimes().Return() // ignore WAL replay logic here |
| 55 | + stateMachine.EXPECT().ProcessStart(types.Round(0)).Return([]types.Action[starknet.Value, starknet.Hash, starknet.Address]{}) |
| 56 | + stateMachine.EXPECT().ProcessPrecommit(gomock.Any()).Times(3).Return([]types.Action[starknet.Value, starknet.Hash, starknet.Address]{}) |
| 57 | + stateMachine.EXPECT().ProcessProposal(gomock.Any()).Return( |
| 58 | + []types.Action[starknet.Value, starknet.Hash, starknet.Address]{&types.StopSync{}}, // Pretend we caught the chain head. Commit action ignored here. |
| 59 | + ) |
| 60 | + |
| 61 | + proposalCh := make(chan starknet.Proposal) |
| 62 | + prevoteCh := make(chan starknet.Prevote) |
| 63 | + precommitCh := make(chan starknet.Precommit) |
| 64 | + broadcasters := mockBroadcasters() |
| 65 | + |
| 66 | + stopSyncCh := make(chan struct{}) |
| 67 | + |
| 68 | + driver := driver.New( |
| 69 | + utils.NewNopZapLogger(), |
| 70 | + newTendermintDB(t), |
| 71 | + stateMachine, |
| 72 | + newMockBlockchain(t), |
| 73 | + mockListeners(proposalCh, prevoteCh, precommitCh), |
| 74 | + broadcasters, |
| 75 | + mockTimeoutFn, |
| 76 | + stopSyncCh, |
| 77 | + ) |
| 78 | + driver.Start() |
| 79 | + |
| 80 | + mockInCh := make(chan sync.BlockBody) |
| 81 | + mockSyncService := newMockSyncService(mockInCh) |
| 82 | + |
| 83 | + proposalStore := proposal.ProposalStore[starknet.Hash]{} |
| 84 | + |
| 85 | + consensusSyncService := consensusSync.New(&mockSyncService, proposalCh, precommitCh, getPrecommits, stopSyncCh, toValue, toHash, &proposalStore) |
| 86 | + |
| 87 | + block0 := getCommittedBlock() |
| 88 | + block0Hash := toHash(block0.Block.Hash) |
| 89 | + go func() { |
| 90 | + mockSyncService.recieveBlock(block0) |
| 91 | + }() |
| 92 | + |
| 93 | + consensusSyncService.Run(ctx) // Driver should trigger stopSyncCh and shut this service down |
| 94 | + require.NotEmpty(t, proposalStore.Get(block0Hash)) // Ensure the Driver sees the correct proposal |
| 95 | + _, stopSyncChIsOpen := <-stopSyncCh // Ensure the Driver closed this channel after catching up to the chain head |
| 96 | + require.False(t, stopSyncChIsOpen) |
| 97 | +} |
| 98 | + |
| 99 | +func getCommittedBlock() sync.BlockBody { |
| 100 | + return sync.BlockBody{ |
| 101 | + Block: &core.Block{ |
| 102 | + Header: &core.Header{ |
| 103 | + Hash: new(felt.Felt).SetUint64(1), |
| 104 | + TransactionCount: 2, |
| 105 | + EventCount: 3, |
| 106 | + SequencerAddress: new(felt.Felt).SetUint64(4), |
| 107 | + Number: 1, |
| 108 | + }, |
| 109 | + }, |
| 110 | + StateUpdate: &core.StateUpdate{ |
| 111 | + StateDiff: &core.StateDiff{}, |
| 112 | + }, |
| 113 | + NewClasses: make(map[felt.Felt]core.Class), |
| 114 | + Commitments: &core.BlockCommitments{}, |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func toValue(in *felt.Felt) starknet.Value { |
| 119 | + return starknet.Value(*in) |
| 120 | +} |
| 121 | + |
| 122 | +func toHash(in *felt.Felt) starknet.Hash { |
| 123 | + return starknet.Hash(*in) |
| 124 | +} |
| 125 | + |
| 126 | +func getPrecommits(types.Height) []types.Precommit[starknet.Hash, starknet.Address] { |
| 127 | + return []types.Precommit[starknet.Hash, starknet.Address]{ |
| 128 | + // We don't use the round since it's not present in the spec yet |
| 129 | + { |
| 130 | + MessageHeader: types.MessageHeader[starknet.Address]{ |
| 131 | + Height: types.Height(1), |
| 132 | + Sender: starknet.Address(*new(felt.Felt).SetUint64(1)), |
| 133 | + }, |
| 134 | + }, |
| 135 | + { |
| 136 | + MessageHeader: types.MessageHeader[starknet.Address]{ |
| 137 | + Height: types.Height(1), |
| 138 | + Sender: starknet.Address(*new(felt.Felt).SetUint64(2)), |
| 139 | + }, |
| 140 | + }, |
| 141 | + { |
| 142 | + MessageHeader: types.MessageHeader[starknet.Address]{ |
| 143 | + Height: types.Height(1), |
| 144 | + Sender: starknet.Address(*new(felt.Felt).SetUint64(3)), |
| 145 | + }, |
| 146 | + }, |
| 147 | + } |
| 148 | +} |
0 commit comments