|
| 1 | +package signer |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/cometbft/cometbft/libs/protoio" |
| 8 | + cometproto "github.com/cometbft/cometbft/proto/tendermint/types" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +// createTestSignBytes creates proper Tendermint sign bytes for testing |
| 13 | +func createTestSignBytesE2E(blockHash []byte, step int8) []byte { |
| 14 | + switch step { |
| 15 | + case stepPropose: |
| 16 | + // Create a CanonicalProposal |
| 17 | + proposal := &cometproto.CanonicalProposal{ |
| 18 | + Type: cometproto.ProposalType, |
| 19 | + Height: 100, |
| 20 | + Round: 5, |
| 21 | + BlockID: &cometproto.CanonicalBlockID{ |
| 22 | + Hash: blockHash, |
| 23 | + }, |
| 24 | + } |
| 25 | + signBytes, _ := protoio.MarshalDelimited(proposal) |
| 26 | + return signBytes |
| 27 | + |
| 28 | + case stepPrevote, stepPrecommit: |
| 29 | + // Create a CanonicalVote |
| 30 | + vote := &cometproto.CanonicalVote{ |
| 31 | + Type: cometproto.SignedMsgType(step), |
| 32 | + Height: 100, |
| 33 | + Round: 5, |
| 34 | + BlockID: &cometproto.CanonicalBlockID{ |
| 35 | + Hash: blockHash, |
| 36 | + }, |
| 37 | + } |
| 38 | + signBytes, _ := protoio.MarshalDelimited(vote) |
| 39 | + return signBytes |
| 40 | + |
| 41 | + default: |
| 42 | + return nil |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// TestConsensusLockE2E tests the consensus lock functionality end-to-end |
| 47 | +// This test simulates a real scenario where a validator tries to sign conflicting blocks |
| 48 | +func TestConsensusLockE2E(t *testing.T) { |
| 49 | + // Create a sign state that simulates a validator that has already signed a PRECOMMIT |
| 50 | + // for a specific block at height 100, round 5 |
| 51 | + lockedBlockHash := []byte("locked_block_hash_123456789012345678901234567890")[:32] // Ensure exactly 32 bytes |
| 52 | + signState := &SignState{ |
| 53 | + Height: 100, |
| 54 | + Round: 5, |
| 55 | + Step: stepPrecommit, |
| 56 | + ConsensusLock: ConsensusLock{ |
| 57 | + Height: 100, |
| 58 | + Round: 5, |
| 59 | + Value: lockedBlockHash, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + // Test 1: Validator tries to sign a PROPOSAL for the same block in a later round |
| 64 | + // This should be allowed (same value) |
| 65 | + sameBlockProposal := createTestSignBytesE2E(lockedBlockHash, stepPropose) |
| 66 | + err := signState.ValidateConsensusLock(HRSKey{Height: 100, Round: 6, Step: stepPropose}, sameBlockProposal) |
| 67 | + require.NoError(t, err, "Should allow PROPOSAL for same block in later round") |
| 68 | + |
| 69 | + // Test 2: Validator tries to sign a PREVOTE for the same block in a later round |
| 70 | + // This should be allowed (same value) |
| 71 | + sameBlockPrevote := createTestSignBytesE2E(lockedBlockHash, stepPrevote) |
| 72 | + err = signState.ValidateConsensusLock(HRSKey{Height: 100, Round: 6, Step: stepPrevote}, sameBlockPrevote) |
| 73 | + require.NoError(t, err, "Should allow PREVOTE for same block in later round") |
| 74 | + |
| 75 | + // Test 3: Validator tries to sign a PROPOSAL for a different block in a later round |
| 76 | + // This should be blocked (different value) |
| 77 | + differentBlockHash := []byte("different_block_hash_123456789012345678901234567890")[:32] |
| 78 | + differentBlockProposal := createTestSignBytesE2E(differentBlockHash, stepPropose) |
| 79 | + err = signState.ValidateConsensusLock(HRSKey{Height: 100, Round: 6, Step: stepPropose}, differentBlockProposal) |
| 80 | + require.Error(t, err, "Should block PROPOSAL for different block in later round") |
| 81 | + require.True(t, IsConsensusLockViolationError(err), "Should be a consensus lock violation error") |
| 82 | + |
| 83 | + // Test 4: Validator tries to sign a PREVOTE for a different block in a later round |
| 84 | + // This should be blocked (different value) |
| 85 | + differentBlockPrevote := createTestSignBytesE2E(differentBlockHash, stepPrevote) |
| 86 | + err = signState.ValidateConsensusLock(HRSKey{Height: 100, Round: 6, Step: stepPrevote}, differentBlockPrevote) |
| 87 | + require.Error(t, err, "Should block PREVOTE for different block in later round") |
| 88 | + require.True(t, IsConsensusLockViolationError(err), "Should be a consensus lock violation error") |
| 89 | + |
| 90 | + // Test 5: Validator tries to sign a PRECOMMIT for a different block in a later round |
| 91 | + // This should be allowed (PRECOMMIT releases the lock) |
| 92 | + differentBlockPrecommit := createTestSignBytesE2E(differentBlockHash, stepPrecommit) |
| 93 | + err = signState.ValidateConsensusLock(HRSKey{Height: 100, Round: 6, Step: stepPrecommit}, differentBlockPrecommit) |
| 94 | + require.NoError(t, err, "Should allow PRECOMMIT for different block in later round (releases lock)") |
| 95 | + |
| 96 | + // Test 6: After signing a PRECOMMIT for a different block, the lock should be updated |
| 97 | + // Simulate the lock update |
| 98 | + newLock := updateConsensusLock(signState.ConsensusLock, HRSKey{Height: 100, Round: 6, Step: stepPrecommit}, differentBlockPrecommit) |
| 99 | + require.True(t, newLock.IsLocked(), "New lock should be active") |
| 100 | + require.Equal(t, int64(100), newLock.Height, "Lock should be at height 100") |
| 101 | + require.Equal(t, int64(6), newLock.Round, "Lock should be at round 6") |
| 102 | + require.Equal(t, differentBlockHash, newLock.Value, "Lock should be on the new block") |
| 103 | + |
| 104 | + // Test 7: Validator tries to sign for a different height |
| 105 | + // This should be allowed (locks are height-specific) |
| 106 | + differentHeightBytes := createTestSignBytesE2E(differentBlockHash, stepPropose) |
| 107 | + err = signState.ValidateConsensusLock(HRSKey{Height: 101, Round: 1, Step: stepPropose}, differentHeightBytes) |
| 108 | + require.NoError(t, err, "Should allow signing for different height") |
| 109 | + |
| 110 | + // Test 8: Validator tries to sign for the same height but earlier round |
| 111 | + // This should be allowed (locks only apply to later rounds) |
| 112 | + err = signState.ValidateConsensusLock(HRSKey{Height: 100, Round: 4, Step: stepPropose}, differentHeightBytes) |
| 113 | + require.NoError(t, err, "Should allow signing for earlier round") |
| 114 | +} |
| 115 | + |
| 116 | +// TestConsensusLockRealWorldScenario tests a realistic scenario |
| 117 | +func TestConsensusLockRealWorldScenario(t *testing.T) { |
| 118 | + // Scenario: Validator is locked on block A at height 100, round 5 |
| 119 | + // Network times out and moves to round 6 |
| 120 | + // New block B is proposed in round 6 |
| 121 | + // Validator should be prevented from signing block B until it signs a PRECOMMIT |
| 122 | + |
| 123 | + lockedBlockA := []byte("block_A_hash_123456789012345678901234567890")[:32] |
| 124 | + signState := &SignState{ |
| 125 | + Height: 100, |
| 126 | + Round: 5, |
| 127 | + Step: stepPrecommit, |
| 128 | + ConsensusLock: ConsensusLock{ |
| 129 | + Height: 100, |
| 130 | + Round: 5, |
| 131 | + Value: lockedBlockA, |
| 132 | + }, |
| 133 | + } |
| 134 | + |
| 135 | + // Round 6: New block B is proposed |
| 136 | + blockB := []byte("block_B_hash_123456789012345678901234567890")[:32] |
| 137 | + |
| 138 | + // Validator should NOT be able to sign PROPOSAL for block B |
| 139 | + blockBProposal := createTestSignBytesE2E(blockB, stepPropose) |
| 140 | + err := signState.ValidateConsensusLock(HRSKey{Height: 100, Round: 6, Step: stepPropose}, blockBProposal) |
| 141 | + require.Error(t, err, "Should block PROPOSAL for block B") |
| 142 | + require.True(t, IsConsensusLockViolationError(err), "Should be a consensus lock violation") |
| 143 | + |
| 144 | + // Validator should NOT be able to sign PREVOTE for block B |
| 145 | + blockBPrevote := createTestSignBytesE2E(blockB, stepPrevote) |
| 146 | + err = signState.ValidateConsensusLock(HRSKey{Height: 100, Round: 6, Step: stepPrevote}, blockBPrevote) |
| 147 | + require.Error(t, err, "Should block PREVOTE for block B") |
| 148 | + require.True(t, IsConsensusLockViolationError(err), "Should be a consensus lock violation") |
| 149 | + |
| 150 | + // Validator should be able to sign PRECOMMIT for block B (this releases the lock) |
| 151 | + blockBPrecommit := createTestSignBytesE2E(blockB, stepPrecommit) |
| 152 | + err = signState.ValidateConsensusLock(HRSKey{Height: 100, Round: 6, Step: stepPrecommit}, blockBPrecommit) |
| 153 | + require.NoError(t, err, "Should allow PRECOMMIT for block B (releases lock)") |
| 154 | + |
| 155 | + // After signing PRECOMMIT for block B, validator should be locked on block B |
| 156 | + newLock := updateConsensusLock(signState.ConsensusLock, HRSKey{Height: 100, Round: 6, Step: stepPrecommit}, blockBPrecommit) |
| 157 | + require.True(t, newLock.IsLocked(), "Should be locked on block B") |
| 158 | + require.Equal(t, blockB, newLock.Value, "Lock should be on block B") |
| 159 | + require.Equal(t, int64(6), newLock.Round, "Lock should be at round 6") |
| 160 | + |
| 161 | + // Update the signState with the new lock |
| 162 | + signState.ConsensusLock = newLock |
| 163 | + |
| 164 | + // Now validator should NOT be able to sign for block A in round 7 |
| 165 | + blockAProposal := createTestSignBytesE2E(lockedBlockA, stepPropose) |
| 166 | + err = signState.ValidateConsensusLock(HRSKey{Height: 100, Round: 7, Step: stepPropose}, blockAProposal) |
| 167 | + require.Error(t, err, "Should block PROPOSAL for block A in round 7") |
| 168 | + require.True(t, IsConsensusLockViolationError(err), "Should be a consensus lock violation") |
| 169 | +} |
| 170 | + |
| 171 | +// TestConsensusLockPerformanceE2E tests that consensus lock validation is fast enough for production use |
| 172 | +func TestConsensusLockPerformanceE2E(t *testing.T) { |
| 173 | + signState := &SignState{ |
| 174 | + Height: 100, |
| 175 | + Round: 5, |
| 176 | + Step: stepPrecommit, |
| 177 | + ConsensusLock: ConsensusLock{ |
| 178 | + Height: 100, |
| 179 | + Round: 5, |
| 180 | + Value: []byte("locked_block_hash_123456789012345678901234567890")[:32], |
| 181 | + }, |
| 182 | + } |
| 183 | + |
| 184 | + blockHash := []byte("different_block_hash_123456789012345678901234567890")[:32] |
| 185 | + blockBytes := createTestSignBytesE2E(blockHash, stepPropose) |
| 186 | + |
| 187 | + // Test that validation is fast (should complete in < 1ms per operation) |
| 188 | + start := time.Now() |
| 189 | + for i := 0; i < 10000; i++ { |
| 190 | + err := signState.ValidateConsensusLock(HRSKey{Height: 100, Round: 6, Step: stepPropose}, blockBytes) |
| 191 | + require.Error(t, err) // Should always fail due to lock violation |
| 192 | + } |
| 193 | + duration := time.Since(start) |
| 194 | + |
| 195 | + // Should complete 10,000 validations in well under 1 second |
| 196 | + require.Less(t, duration, time.Second, "Consensus lock validation should be very fast") |
| 197 | + |
| 198 | + avgTimePerOp := duration / 10000 |
| 199 | + require.Less(t, avgTimePerOp, 100*time.Microsecond, "Average time per operation should be < 100μs") |
| 200 | +} |
0 commit comments