Skip to content

Commit

Permalink
test: deterministic mode (#5155)
Browse files Browse the repository at this point in the history
Fixes #3899

When exporting SEED=[number] env var deterministic mode gets used now.
Note that there are other sources of info in some tests so the results
might not be the same in all the cases. E.g. in integration l1 publisher
test the second block has a populated timestamp and therefore will have
different archive root. For first block the timestamp is set to 0 so
there an equal result should be achieved.
  • Loading branch information
benesjan authored Mar 12, 2024
1 parent 8d73f8a commit e68b56a
Show file tree
Hide file tree
Showing 46 changed files with 246 additions and 131 deletions.
10 changes: 5 additions & 5 deletions l1-contracts/test/fixtures/empty_block_0.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
]
},
"block": {
"archive": "0x12e61edc1dad6fb9e81e4157d0906a171a2d2cad5daa767594eb07b7e7c1c189",
"archive": "0x2c80dc48494603ac15fa1ea88e3cd79565bdd54cf067a9bb4da9849870b60c69",
"body": "0x00000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"txsEffectsHash": "0x9139297703640b243028d35c29ae8c0667886c4edc8db5f879c260d2051bb8a9",
"decodedHeader": {
Expand All @@ -50,8 +50,8 @@
"chainId": 31337,
"timestamp": 0,
"version": 1,
"coinbase": "0x837ac4b411a7a5911031e82d3f8799bb21c34795",
"feeRecipient": "0x1296779221953ecf9e3c115440521940f216ca20b860f64f13af55b8cdbdb9c7"
"coinbase": "0x66440eb666440eb666440eb666440eb666440eb6",
"feeRecipient": "0x061ca689507c7f1ccc68c2ad086c9d5d94f50869cb1b718c6a46aaf77a4500ba"
},
"lastArchive": {
"nextAvailableLeafIndex": 1,
Expand All @@ -78,8 +78,8 @@
}
}
},
"header": "0x012a86560737adb075e12af8253fb09abf17aa841fb56d180bc89f0d2d473c7f0000000100000000000000000000000000000000000000000000000000000000000000029139297703640b243028d35c29ae8c0667886c4edc8db5f879c260d2051bb8a90000000000000000000000000000000000000000000000000000000000000000c78009fdf07fc56a11f122370658a353aaa542ed63e44c4bc15ff4cd105ab33c1864fcdaa80ff2719154fa7c8a9050662972707168d69eac9db6fd3110829f800000001016642d9ccd8346c403aa4c3fa451178b22534a27035cdaa6ec34ae53b29c50cb000001000bcfa3e9f1a8922ee92c6dc964d6595907c1804a86753774322b468f69d4f278000001800572c8db882674dd026b8877fbba1b700a4407da3ae9ce5fa43215a28163362b000000c00000000000000000000000000000000000000000000000000000000000007a69000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000837ac4b411a7a5911031e82d3f8799bb21c347951296779221953ecf9e3c115440521940f216ca20b860f64f13af55b8cdbdb9c7",
"header": "0x012a86560737adb075e12af8253fb09abf17aa841fb56d180bc89f0d2d473c7f0000000100000000000000000000000000000000000000000000000000000000000000029139297703640b243028d35c29ae8c0667886c4edc8db5f879c260d2051bb8a90000000000000000000000000000000000000000000000000000000000000000c78009fdf07fc56a11f122370658a353aaa542ed63e44c4bc15ff4cd105ab33c1864fcdaa80ff2719154fa7c8a9050662972707168d69eac9db6fd3110829f800000001016642d9ccd8346c403aa4c3fa451178b22534a27035cdaa6ec34ae53b29c50cb000001000bcfa3e9f1a8922ee92c6dc964d6595907c1804a86753774322b468f69d4f278000001800572c8db882674dd026b8877fbba1b700a4407da3ae9ce5fa43215a28163362b000000c00000000000000000000000000000000000000000000000000000000000007a6900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000066440eb666440eb666440eb666440eb666440eb6061ca689507c7f1ccc68c2ad086c9d5d94f50869cb1b718c6a46aaf77a4500ba",
"l1ToL2MessagesHash": "0x076a27c79e5ace2a3d47f9dd2e83e4ff6ea8872b3c2218f66c92b89b55f36560",
"publicInputsHash": "0x14d3a7d947fc1660f01d5f41f7768d9bee4a74809484cc80535183daf0346d1f"
"publicInputsHash": "0x22fb6cb24475c172eea140a775dd4ca748f8dda94fda256d5f76c09cb913ecf0"
}
}
14 changes: 7 additions & 7 deletions l1-contracts/test/fixtures/empty_block_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
]
},
"block": {
"archive": "0x2eb0c375f9f387ef03e8f470f7f976a8213cf31a514b72cf3499a8dd251deb57",
"archive": "0x0dbc2a0e8143bb92a0f8a92c8a9862c4577f15b01330d89960032af0dadf5b52",
"body": "0x00000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"txsEffectsHash": "0x9139297703640b243028d35c29ae8c0667886c4edc8db5f879c260d2051bb8a9",
"decodedHeader": {
Expand All @@ -48,14 +48,14 @@
"globalVariables": {
"blockNumber": 2,
"chainId": 31337,
"timestamp": 1709939104,
"timestamp": 1710254478,
"version": 1,
"coinbase": "0x837ac4b411a7a5911031e82d3f8799bb21c34795",
"feeRecipient": "0x1296779221953ecf9e3c115440521940f216ca20b860f64f13af55b8cdbdb9c7"
"coinbase": "0x66440eb666440eb666440eb666440eb666440eb6",
"feeRecipient": "0x061ca689507c7f1ccc68c2ad086c9d5d94f50869cb1b718c6a46aaf77a4500ba"
},
"lastArchive": {
"nextAvailableLeafIndex": 2,
"root": "0x12e61edc1dad6fb9e81e4157d0906a171a2d2cad5daa767594eb07b7e7c1c189"
"root": "0x2c80dc48494603ac15fa1ea88e3cd79565bdd54cf067a9bb4da9849870b60c69"
},
"stateReference": {
"l1ToL2MessageTree": {
Expand All @@ -78,8 +78,8 @@
}
}
},
"header": "0x12e61edc1dad6fb9e81e4157d0906a171a2d2cad5daa767594eb07b7e7c1c1890000000200000000000000000000000000000000000000000000000000000000000000029139297703640b243028d35c29ae8c0667886c4edc8db5f879c260d2051bb8a90000000000000000000000000000000000000000000000000000000000000000c78009fdf07fc56a11f122370658a353aaa542ed63e44c4bc15ff4cd105ab33c1864fcdaa80ff2719154fa7c8a9050662972707168d69eac9db6fd3110829f800000002016642d9ccd8346c403aa4c3fa451178b22534a27035cdaa6ec34ae53b29c50cb000002000bcfa3e9f1a8922ee92c6dc964d6595907c1804a86753774322b468f69d4f278000002800572c8db882674dd026b8877fbba1b700a4407da3ae9ce5fa43215a28163362b000001400000000000000000000000000000000000000000000000000000000000007a69000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000065eb99a0837ac4b411a7a5911031e82d3f8799bb21c347951296779221953ecf9e3c115440521940f216ca20b860f64f13af55b8cdbdb9c7",
"header": "0x2c80dc48494603ac15fa1ea88e3cd79565bdd54cf067a9bb4da9849870b60c690000000200000000000000000000000000000000000000000000000000000000000000029139297703640b243028d35c29ae8c0667886c4edc8db5f879c260d2051bb8a90000000000000000000000000000000000000000000000000000000000000000c78009fdf07fc56a11f122370658a353aaa542ed63e44c4bc15ff4cd105ab33c1864fcdaa80ff2719154fa7c8a9050662972707168d69eac9db6fd3110829f800000002016642d9ccd8346c403aa4c3fa451178b22534a27035cdaa6ec34ae53b29c50cb000002000bcfa3e9f1a8922ee92c6dc964d6595907c1804a86753774322b468f69d4f278000002800572c8db882674dd026b8877fbba1b700a4407da3ae9ce5fa43215a28163362b000001400000000000000000000000000000000000000000000000000000000000007a69000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000065f0698e66440eb666440eb666440eb666440eb666440eb6061ca689507c7f1ccc68c2ad086c9d5d94f50869cb1b718c6a46aaf77a4500ba",
"l1ToL2MessagesHash": "0x076a27c79e5ace2a3d47f9dd2e83e4ff6ea8872b3c2218f66c92b89b55f36560",
"publicInputsHash": "0x18091cbbf29dbe3cd4e5387ea7cfa360121b458b5fb9229ff822cfd32288436e"
"publicInputsHash": "0x0fc84e6176557ac21fb1dd501d124270791bcd21474d814112b3ae4ed0c456bb"
}
}
16 changes: 8 additions & 8 deletions l1-contracts/test/fixtures/mixed_block_0.json

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions l1-contracts/test/fixtures/mixed_block_1.json

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions yarn-project/archiver/src/archiver/archiver_store_test_suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import '@aztec/circuit-types/jest';
import { AztecAddress, Fr, INITIAL_L2_BLOCK_NUM, L1_TO_L2_MSG_SUBTREE_HEIGHT } from '@aztec/circuits.js';
import { makeContractClassPublic } from '@aztec/circuits.js/testing';
import { randomBytes } from '@aztec/foundation/crypto';
import { randomBytes, randomInt } from '@aztec/foundation/crypto';
import { ContractClassPublic, ContractInstanceWithAddress, SerializableContractInstance } from '@aztec/types/contracts';

import { ArchiverDataStore } from './archiver_store.js';
Expand Down Expand Up @@ -232,7 +232,7 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch

it('returns messages in correct order', async () => {
const msgs = generateBlockMessages(l2BlockNumber, l1ToL2MessageSubtreeSize);
const shuffledMessages = msgs.slice().sort(() => Math.random() - 0.5);
const shuffledMessages = msgs.slice().sort(() => randomInt(1) - 0.5);
await store.addNewL1ToL2Messages(shuffledMessages, 100n);
const retrievedMessages = await store.getNewL1ToL2Messages(l2BlockNumber);

Expand Down Expand Up @@ -440,8 +440,8 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch

it('"txHash" filter param is respected', async () => {
// get random tx
const targetBlockIndex = Math.floor(Math.random() * numBlocks);
const targetTxIndex = Math.floor(Math.random() * txsPerBlock);
const targetBlockIndex = randomInt(numBlocks);
const targetTxIndex = randomInt(txsPerBlock);
const targetTxHash = new L2BlockContext(blocks[targetBlockIndex]).getTxHash(targetTxIndex);

const response = await store.getUnencryptedLogs({ txHash: targetTxHash });
Expand Down Expand Up @@ -481,10 +481,10 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch

it('"contractAddress" filter param is respected', async () => {
// Get a random contract address from the logs
const targetBlockIndex = Math.floor(Math.random() * numBlocks);
const targetTxIndex = Math.floor(Math.random() * txsPerBlock);
const targetFunctionLogIndex = Math.floor(Math.random() * numPublicFunctionCalls);
const targetLogIndex = Math.floor(Math.random() * numUnencryptedLogs);
const targetBlockIndex = randomInt(numBlocks);
const targetTxIndex = randomInt(txsPerBlock);
const targetFunctionLogIndex = randomInt(numPublicFunctionCalls);
const targetLogIndex = randomInt(numUnencryptedLogs);
const targetContractAddress = UnencryptedL2Log.fromBuffer(
blocks[targetBlockIndex].body.txEffects[targetTxIndex].unencryptedLogs.functionLogs[targetFunctionLogIndex]
.logs[targetLogIndex],
Expand All @@ -501,10 +501,10 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch

it('"selector" filter param is respected', async () => {
// Get a random selector from the logs
const targetBlockIndex = Math.floor(Math.random() * numBlocks);
const targetTxIndex = Math.floor(Math.random() * txsPerBlock);
const targetFunctionLogIndex = Math.floor(Math.random() * numPublicFunctionCalls);
const targetLogIndex = Math.floor(Math.random() * numUnencryptedLogs);
const targetBlockIndex = randomInt(numBlocks);
const targetTxIndex = randomInt(txsPerBlock);
const targetFunctionLogIndex = randomInt(numPublicFunctionCalls);
const targetLogIndex = randomInt(numUnencryptedLogs);
const targetSelector = UnencryptedL2Log.fromBuffer(
blocks[targetBlockIndex].body.txEffects[targetTxIndex].unencryptedLogs.functionLogs[targetFunctionLogIndex]
.logs[targetLogIndex],
Expand All @@ -521,9 +521,9 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch

it('"afterLog" filter param is respected', async () => {
// Get a random log as reference
const targetBlockIndex = Math.floor(Math.random() * numBlocks);
const targetTxIndex = Math.floor(Math.random() * txsPerBlock);
const targetLogIndex = Math.floor(Math.random() * numUnencryptedLogs);
const targetBlockIndex = randomInt(numBlocks);
const targetTxIndex = randomInt(txsPerBlock);
const targetLogIndex = randomInt(numUnencryptedLogs);

const afterLog = new LogId(targetBlockIndex + INITIAL_L2_BLOCK_NUM, targetTxIndex, targetLogIndex);

Expand Down Expand Up @@ -588,9 +588,9 @@ export function describeArchiverDataStore(testName: string, getStore: () => Arch

it('"txIndex" and "logIndex" are respected when "afterLog.blockNumber" is equal to "fromBlock"', async () => {
// Get a random log as reference
const targetBlockIndex = Math.floor(Math.random() * numBlocks);
const targetTxIndex = Math.floor(Math.random() * txsPerBlock);
const targetLogIndex = Math.floor(Math.random() * numUnencryptedLogs);
const targetBlockIndex = randomInt(numBlocks);
const targetTxIndex = randomInt(txsPerBlock);
const targetLogIndex = randomInt(numUnencryptedLogs);

const afterLog = new LogId(targetBlockIndex + INITIAL_L2_BLOCK_NUM, targetTxIndex, targetLogIndex);

Expand Down
6 changes: 4 additions & 2 deletions yarn-project/circuit-types/src/l1_to_l2_message.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { randomInt } from '@aztec/foundation/crypto';

import { L1ToL2Message, L1ToL2MessageAndIndex } from './l1_to_l2_message.js';

describe('L1 to L2 message', () => {
Expand All @@ -9,7 +11,7 @@ describe('L1 to L2 message', () => {
});

it('can encode an L1ToL2MessageAndIndex to buffer and back', () => {
const index = BigInt(Math.floor(Math.random() * 1000)); // Generate a random BigInt
const index = BigInt(randomInt(1000)); // Generate a random BigInt
const msg = L1ToL2Message.random();
const l1ToL2MsgAndIndex = new L1ToL2MessageAndIndex(index, msg);

Expand All @@ -20,7 +22,7 @@ describe('L1 to L2 message', () => {
});

it('can encode an L1ToL2MessageAndIndex to string and back', () => {
const index = BigInt(Math.floor(Math.random() * 1000)); // Generate a random BigInt
const index = BigInt(randomInt(1000)); // Generate a random BigInt
const msg = L1ToL2Message.random();
const l1ToL2MsgAndIndex = new L1ToL2MessageAndIndex(index, msg);

Expand Down
10 changes: 5 additions & 5 deletions yarn-project/circuit-types/src/l1_to_l2_message.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { toBigIntBE, toBufferBE } from '@aztec/foundation/bigint-buffer';
import { sha256 } from '@aztec/foundation/crypto';
import { randomInt, sha256 } from '@aztec/foundation/crypto';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
Expand Down Expand Up @@ -180,8 +180,8 @@ export class L1ToL2Message {
L2Actor.random(),
Fr.random(),
Fr.random(),
Math.floor(Math.random() * 1000),
Math.floor(Math.random() * 1000),
randomInt(1000),
randomInt(1000),
entryKey,
);
}
Expand Down Expand Up @@ -223,7 +223,7 @@ export class L1Actor {
}

static random(): L1Actor {
return new L1Actor(EthAddress.random(), Math.floor(Math.random() * 1000));
return new L1Actor(EthAddress.random(), randomInt(1000));
}
}

Expand Down Expand Up @@ -263,6 +263,6 @@ export class L2Actor {
}

static random(): L2Actor {
return new L2Actor(AztecAddress.random(), Math.floor(Math.random() * 1000));
return new L2Actor(AztecAddress.random(), randomInt(1000));
}
}
4 changes: 1 addition & 3 deletions yarn-project/circuit-types/src/logs/function_l2_logs.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { sha256 } from '@aztec/foundation/crypto';
import { randomBytes, sha256 } from '@aztec/foundation/crypto';
import { Fr, Point } from '@aztec/foundation/fields';
import { BufferReader, prefixBufferWithLength } from '@aztec/foundation/serialize';

import { randomBytes } from 'crypto';

import { LogType } from './log_type.js';
import { UnencryptedL2Log } from './unencrypted_l2_log.js';

Expand Down
3 changes: 2 additions & 1 deletion yarn-project/circuit-types/src/logs/l1_note_payload/note.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Vector } from '@aztec/circuits.js';
import { randomInt } from '@aztec/foundation/crypto';
import { Fr } from '@aztec/foundation/fields';
import { BufferReader } from '@aztec/foundation/serialize';

Expand Down Expand Up @@ -29,7 +30,7 @@ export class Note extends Vector<Fr> {
* @returns A randomly generated Note instance.
*/
static random() {
const numItems = Math.floor(Math.random() * 10) + 1;
const numItems = randomInt(10) + 1;
const items = Array.from({ length: numItems }, () => Fr.random());
return new Note(items);
}
Expand Down
8 changes: 5 additions & 3 deletions yarn-project/circuit-types/src/logs/log_id.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { randomInt } from '@aztec/foundation/crypto';

import { LogId } from './log_id.js';

describe('LogId', () => {
let logId: LogId;
beforeEach(() => {
const blockNumber = Math.floor(Math.random() * 1000);
const txIndex = Math.floor(Math.random() * 1000);
const logIndex = Math.floor(Math.random() * 1000);
const blockNumber = randomInt(1000);
const txIndex = randomInt(1000);
const logIndex = randomInt(1000);
logId = new LogId(blockNumber, txIndex, logIndex);
});

Expand Down
7 changes: 3 additions & 4 deletions yarn-project/circuit-types/src/logs/unencrypted_l2_log.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { AztecAddress } from '@aztec/circuits.js';
import { EventSelector } from '@aztec/foundation/abi';
import { randomBytes } from '@aztec/foundation/crypto';
import { BufferReader, prefixBufferWithLength } from '@aztec/foundation/serialize';

import { randomBytes } from 'crypto';

/**
* Represents an individual unencrypted log entry.
*/
Expand Down Expand Up @@ -71,8 +70,8 @@ export class UnencryptedL2Log {
*/
public static random(): UnencryptedL2Log {
const contractAddress = AztecAddress.random();
const selector = new EventSelector(Math.floor(Math.random() * (2 ** (EventSelector.SIZE * 8) - 1)));
const dataLength = EventSelector.SIZE + Math.floor(Math.random() * 200);
const selector = EventSelector.random();
const dataLength = EventSelector.SIZE + randomBytes(1)[0];
const data = randomBytes(dataLength);
return new UnencryptedL2Log(contractAddress, selector, data);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { toBufferBE } from '@aztec/foundation/bigint-buffer';
import { randomBytes } from '@aztec/foundation/crypto';
import { Fr } from '@aztec/foundation/fields';
import { mapTuple } from '@aztec/foundation/serialize';

import { randomBytes } from 'crypto';

import { Signature } from '../signature/index.js';

/**
Expand Down
5 changes: 3 additions & 2 deletions yarn-project/circuits.js/src/structs/call_context.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { randomInt } from '@aztec/foundation/crypto';

import { CALL_CONTEXT_LENGTH } from '../constants.gen.js';
import { makeCallContext } from '../tests/factories.js';
import { CallContext } from './call_context.js';
Expand All @@ -6,8 +8,7 @@ describe('CallContext', () => {
let callContext: CallContext;

beforeAll(() => {
const randomInt = Math.floor(Math.random() * 1000);
callContext = makeCallContext(randomInt);
callContext = makeCallContext(randomInt(1000));
});

it(`serializes to buffer and deserializes it back`, () => {
Expand Down
Loading

0 comments on commit e68b56a

Please sign in to comment.