|
| 1 | +import type { AztecNodeService } from '@aztec/aztec-node'; |
| 2 | +import type { TestAztecNodeService } from '@aztec/aztec-node/test'; |
| 3 | +import { EthAddress } from '@aztec/aztec.js/addresses'; |
| 4 | +import { EpochNumber, SlotNumber } from '@aztec/foundation/branded-types'; |
| 5 | +import { bufferToHex } from '@aztec/foundation/string'; |
| 6 | +import { OffenseType } from '@aztec/slasher'; |
| 7 | + |
| 8 | +import { jest } from '@jest/globals'; |
| 9 | +import fs from 'fs'; |
| 10 | +import os from 'os'; |
| 11 | +import path from 'path'; |
| 12 | +import { privateKeyToAccount } from 'viem/accounts'; |
| 13 | + |
| 14 | +import { shouldCollectMetrics } from '../fixtures/fixtures.js'; |
| 15 | +import { ATTESTER_PRIVATE_KEYS_START_INDEX, createNode } from '../fixtures/setup_p2p_test.js'; |
| 16 | +import { getPrivateKeyFromIndex } from '../fixtures/utils.js'; |
| 17 | +import { P2PNetworkTest } from './p2p_network.js'; |
| 18 | +import { awaitCommitteeExists, awaitOffenseDetected } from './shared.js'; |
| 19 | + |
| 20 | +const TEST_TIMEOUT = 600_000; // 10 minutes |
| 21 | + |
| 22 | +jest.setTimeout(TEST_TIMEOUT); |
| 23 | + |
| 24 | +const NUM_VALIDATORS = 4; |
| 25 | +const BOOT_NODE_UDP_PORT = 4500; |
| 26 | +const COMMITTEE_SIZE = NUM_VALIDATORS; |
| 27 | +const ETHEREUM_SLOT_DURATION = 8; |
| 28 | +const AZTEC_SLOT_DURATION = ETHEREUM_SLOT_DURATION * 3; |
| 29 | +const BLOCK_DURATION = 4; |
| 30 | + |
| 31 | +const DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'duplicate-proposal-slash-')); |
| 32 | + |
| 33 | +/** |
| 34 | + * Test that slashing occurs when a validator sends duplicate proposals (equivocation). |
| 35 | + * |
| 36 | + * The setup of the test is as follows: |
| 37 | + * 1. Create 4 validator nodes total: |
| 38 | + * - 2 honest validators with unique keys |
| 39 | + * - 2 "malicious" validators that share the SAME validator key but have DIFFERENT coinbase addresses |
| 40 | + * 2. The two nodes with the same key will both detect they are proposers for the same slot and naturally race to propose |
| 41 | + * 3. Since they have different coinbase addresses, their proposals will have different archives (different content) |
| 42 | + * 4. Other validators will detect the duplicate and emit a slash event |
| 43 | + */ |
| 44 | +describe('e2e_p2p_duplicate_proposal_slash', () => { |
| 45 | + let t: P2PNetworkTest; |
| 46 | + let nodes: AztecNodeService[]; |
| 47 | + |
| 48 | + // Small slashing unit so we don't kick anyone out |
| 49 | + const slashingUnit = BigInt(1e14); |
| 50 | + const slashingQuorum = 3; |
| 51 | + const slashingRoundSize = 4; |
| 52 | + const aztecEpochDuration = 2; |
| 53 | + |
| 54 | + beforeEach(async () => { |
| 55 | + t = await P2PNetworkTest.create({ |
| 56 | + testName: 'e2e_p2p_duplicate_proposal_slash', |
| 57 | + numberOfNodes: 0, |
| 58 | + numberOfValidators: NUM_VALIDATORS, |
| 59 | + basePort: BOOT_NODE_UDP_PORT, |
| 60 | + metricsPort: shouldCollectMetrics(), |
| 61 | + initialConfig: { |
| 62 | + listenAddress: '127.0.0.1', |
| 63 | + aztecEpochDuration, |
| 64 | + ethereumSlotDuration: ETHEREUM_SLOT_DURATION, |
| 65 | + aztecSlotDuration: AZTEC_SLOT_DURATION, |
| 66 | + aztecTargetCommitteeSize: COMMITTEE_SIZE, |
| 67 | + aztecProofSubmissionEpochs: 1024, // effectively do not reorg |
| 68 | + slashInactivityConsecutiveEpochThreshold: 32, // effectively do not slash for inactivity |
| 69 | + minTxsPerBlock: 0, // always be building |
| 70 | + mockGossipSubNetwork: true, // do not worry about p2p connectivity issues |
| 71 | + slashingQuorum, |
| 72 | + slashingRoundSizeInEpochs: slashingRoundSize / aztecEpochDuration, |
| 73 | + slashAmountSmall: slashingUnit, |
| 74 | + slashAmountMedium: slashingUnit * 2n, |
| 75 | + slashAmountLarge: slashingUnit * 3n, |
| 76 | + enforceTimeTable: true, |
| 77 | + blockDurationMs: BLOCK_DURATION * 1000, |
| 78 | + slashDuplicateProposalPenalty: slashingUnit, |
| 79 | + slashingOffsetInRounds: 1, |
| 80 | + }, |
| 81 | + }); |
| 82 | + |
| 83 | + await t.setup(); |
| 84 | + await t.applyBaseSetup(); |
| 85 | + }); |
| 86 | + |
| 87 | + afterEach(async () => { |
| 88 | + await t.stopNodes(nodes); |
| 89 | + await t.teardown(); |
| 90 | + for (let i = 0; i < NUM_VALIDATORS; i++) { |
| 91 | + fs.rmSync(`${DATA_DIR}-${i}`, { recursive: true, force: true, maxRetries: 3 }); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + const debugRollup = async () => { |
| 96 | + await t.ctx.cheatCodes.rollup.debugRollup(); |
| 97 | + }; |
| 98 | + |
| 99 | + it('slashes validator who sends duplicate proposals', async () => { |
| 100 | + const { rollup } = await t.getContracts(); |
| 101 | + |
| 102 | + // Jump forward to an epoch in the future such that the validator set is not empty |
| 103 | + await t.ctx.cheatCodes.rollup.advanceToEpoch(EpochNumber(4)); |
| 104 | + await debugRollup(); |
| 105 | + |
| 106 | + t.logger.warn('Creating nodes'); |
| 107 | + |
| 108 | + // Get the attester private key that will be shared between two malicious nodes |
| 109 | + // We'll use validator index 0 for the "malicious" validator key |
| 110 | + const maliciousValidatorIndex = 0; |
| 111 | + const maliciousValidatorPrivateKey = getPrivateKeyFromIndex( |
| 112 | + ATTESTER_PRIVATE_KEYS_START_INDEX + maliciousValidatorIndex, |
| 113 | + )!; |
| 114 | + const maliciousValidatorAddress = EthAddress.fromString( |
| 115 | + privateKeyToAccount(`0x${maliciousValidatorPrivateKey.toString('hex')}`).address, |
| 116 | + ); |
| 117 | + |
| 118 | + t.logger.warn(`Malicious proposer address: ${maliciousValidatorAddress.toString()}`); |
| 119 | + |
| 120 | + // Create two nodes with the SAME validator key but DIFFERENT coinbase addresses |
| 121 | + // This will cause them to create proposals with different content for the same slot |
| 122 | + const maliciousPrivateKeyHex = bufferToHex(maliciousValidatorPrivateKey); |
| 123 | + const coinbase1 = EthAddress.random(); |
| 124 | + const coinbase2 = EthAddress.random(); |
| 125 | + |
| 126 | + t.logger.warn(`Creating malicious node 1 with coinbase ${coinbase1.toString()}`); |
| 127 | + const maliciousNode1 = await createNode( |
| 128 | + { ...t.ctx.aztecNodeConfig, validatorPrivateKey: maliciousPrivateKeyHex, coinbase: coinbase1 }, |
| 129 | + t.ctx.dateProvider!, |
| 130 | + BOOT_NODE_UDP_PORT + 1, |
| 131 | + t.bootstrapNodeEnr, |
| 132 | + maliciousValidatorIndex, |
| 133 | + t.prefilledPublicData, |
| 134 | + `${DATA_DIR}-0`, |
| 135 | + shouldCollectMetrics(), |
| 136 | + ); |
| 137 | + |
| 138 | + t.logger.warn(`Creating malicious node 2 with coinbase ${coinbase2.toString()}`); |
| 139 | + const maliciousNode2 = await createNode( |
| 140 | + { ...t.ctx.aztecNodeConfig, validatorPrivateKey: maliciousPrivateKeyHex, coinbase: coinbase2 }, |
| 141 | + t.ctx.dateProvider!, |
| 142 | + BOOT_NODE_UDP_PORT + 2, |
| 143 | + t.bootstrapNodeEnr, |
| 144 | + maliciousValidatorIndex, |
| 145 | + t.prefilledPublicData, |
| 146 | + `${DATA_DIR}-1`, |
| 147 | + shouldCollectMetrics(), |
| 148 | + ); |
| 149 | + |
| 150 | + // Create honest nodes with unique validator keys (indices 1 and 2) |
| 151 | + t.logger.warn('Creating honest nodes'); |
| 152 | + const honestNode1 = await createNode( |
| 153 | + t.ctx.aztecNodeConfig, |
| 154 | + t.ctx.dateProvider!, |
| 155 | + BOOT_NODE_UDP_PORT + 3, |
| 156 | + t.bootstrapNodeEnr, |
| 157 | + 1, |
| 158 | + t.prefilledPublicData, |
| 159 | + `${DATA_DIR}-2`, |
| 160 | + shouldCollectMetrics(), |
| 161 | + ); |
| 162 | + const honestNode2 = await createNode( |
| 163 | + t.ctx.aztecNodeConfig, |
| 164 | + t.ctx.dateProvider!, |
| 165 | + BOOT_NODE_UDP_PORT + 4, |
| 166 | + t.bootstrapNodeEnr, |
| 167 | + 2, |
| 168 | + t.prefilledPublicData, |
| 169 | + `${DATA_DIR}-3`, |
| 170 | + shouldCollectMetrics(), |
| 171 | + ); |
| 172 | + |
| 173 | + nodes = [maliciousNode1, maliciousNode2, honestNode1, honestNode2]; |
| 174 | + |
| 175 | + // Wait for P2P mesh and the committee to be fully formed before proceeding |
| 176 | + await t.waitForP2PMeshConnectivity(nodes, NUM_VALIDATORS); |
| 177 | + await awaitCommitteeExists({ rollup, logger: t.logger }); |
| 178 | + |
| 179 | + // Wait for offense to be detected |
| 180 | + // The honest nodes should detect the duplicate proposal from the malicious validator |
| 181 | + t.logger.warn('Waiting for duplicate proposal offense to be detected...'); |
| 182 | + const offenses = await awaitOffenseDetected({ |
| 183 | + epochDuration: t.ctx.aztecNodeConfig.aztecEpochDuration, |
| 184 | + logger: t.logger, |
| 185 | + nodeAdmin: honestNode1, // Use honest node to check for offenses |
| 186 | + slashingRoundSize, |
| 187 | + waitUntilOffenseCount: 1, |
| 188 | + timeoutSeconds: AZTEC_SLOT_DURATION * 16, |
| 189 | + }); |
| 190 | + |
| 191 | + t.logger.warn(`Collected offenses`, { offenses }); |
| 192 | + |
| 193 | + // Verify the offense is correct |
| 194 | + expect(offenses.length).toBeGreaterThan(0); |
| 195 | + for (const offense of offenses) { |
| 196 | + expect(offense.offenseType).toEqual(OffenseType.DUPLICATE_PROPOSAL); |
| 197 | + expect(offense.validator.toString()).toEqual(maliciousValidatorAddress.toString()); |
| 198 | + } |
| 199 | + |
| 200 | + // Verify that for each offense, the proposer for that slot is the malicious validator |
| 201 | + const epochCache = (honestNode1 as TestAztecNodeService).epochCache; |
| 202 | + for (const offense of offenses) { |
| 203 | + const offenseSlot = SlotNumber(Number(offense.epochOrSlot)); |
| 204 | + const proposerForSlot = await epochCache.getProposerAttesterAddressInSlot(offenseSlot); |
| 205 | + t.logger.info(`Offense slot ${offenseSlot}: proposer is ${proposerForSlot?.toString()}`); |
| 206 | + expect(proposerForSlot?.toString()).toEqual(maliciousValidatorAddress.toString()); |
| 207 | + } |
| 208 | + |
| 209 | + t.logger.warn('Duplicate proposal offense correctly detected and recorded'); |
| 210 | + }); |
| 211 | +}); |
0 commit comments