@aztec/txe 0.0.0-test.1 → 0.0.1-commit.1142ef1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/dest/bin/index.d.ts +1 -1
  2. package/dest/bin/index.js +3 -2
  3. package/dest/constants.d.ts +4 -0
  4. package/dest/constants.d.ts.map +1 -0
  5. package/dest/constants.js +3 -0
  6. package/dest/index.d.ts +1 -1
  7. package/dest/index.d.ts.map +1 -1
  8. package/dest/index.js +81 -51
  9. package/dest/oracle/interfaces.d.ts +57 -0
  10. package/dest/oracle/interfaces.d.ts.map +1 -0
  11. package/dest/oracle/interfaces.js +3 -0
  12. package/dest/oracle/txe_oracle_public_context.d.ts +34 -0
  13. package/dest/oracle/txe_oracle_public_context.d.ts.map +1 -0
  14. package/dest/oracle/txe_oracle_public_context.js +122 -0
  15. package/dest/oracle/txe_oracle_top_level_context.d.ts +65 -0
  16. package/dest/oracle/txe_oracle_top_level_context.d.ts.map +1 -0
  17. package/dest/oracle/txe_oracle_top_level_context.js +463 -0
  18. package/dest/rpc_translator.d.ts +246 -0
  19. package/dest/rpc_translator.d.ts.map +1 -0
  20. package/dest/rpc_translator.js +666 -0
  21. package/dest/state_machine/archiver.d.ts +80 -0
  22. package/dest/state_machine/archiver.d.ts.map +1 -0
  23. package/dest/state_machine/archiver.js +229 -0
  24. package/dest/state_machine/dummy_p2p_client.d.ts +55 -0
  25. package/dest/state_machine/dummy_p2p_client.d.ts.map +1 -0
  26. package/dest/state_machine/dummy_p2p_client.js +141 -0
  27. package/dest/state_machine/global_variable_builder.d.ts +11 -0
  28. package/dest/state_machine/global_variable_builder.d.ts.map +1 -0
  29. package/dest/state_machine/global_variable_builder.js +22 -0
  30. package/dest/state_machine/index.d.ts +16 -0
  31. package/dest/state_machine/index.d.ts.map +1 -0
  32. package/dest/state_machine/index.js +45 -0
  33. package/dest/state_machine/mock_epoch_cache.d.ts +27 -0
  34. package/dest/state_machine/mock_epoch_cache.d.ts.map +1 -0
  35. package/dest/state_machine/mock_epoch_cache.js +54 -0
  36. package/dest/state_machine/synchronizer.d.ts +33 -0
  37. package/dest/state_machine/synchronizer.d.ts.map +1 -0
  38. package/dest/state_machine/synchronizer.js +59 -0
  39. package/dest/txe_session.d.ts +73 -0
  40. package/dest/txe_session.d.ts.map +1 -0
  41. package/dest/txe_session.js +286 -0
  42. package/dest/util/encoding.d.ts +647 -30
  43. package/dest/util/encoding.d.ts.map +1 -1
  44. package/dest/util/encoding.js +66 -11
  45. package/dest/util/expected_failure_error.d.ts +1 -1
  46. package/dest/util/expected_failure_error.d.ts.map +1 -1
  47. package/dest/util/txe_account_store.d.ts +10 -0
  48. package/dest/util/txe_account_store.d.ts.map +1 -0
  49. package/dest/util/{txe_account_data_provider.js → txe_account_store.js} +1 -1
  50. package/dest/util/txe_contract_store.d.ts +12 -0
  51. package/dest/util/txe_contract_store.d.ts.map +1 -0
  52. package/dest/util/txe_contract_store.js +22 -0
  53. package/dest/util/txe_public_contract_data_source.d.ts +12 -11
  54. package/dest/util/txe_public_contract_data_source.d.ts.map +1 -1
  55. package/dest/util/txe_public_contract_data_source.js +37 -58
  56. package/dest/utils/block_creation.d.ts +28 -0
  57. package/dest/utils/block_creation.d.ts.map +1 -0
  58. package/dest/utils/block_creation.js +44 -0
  59. package/dest/utils/tx_effect_creation.d.ts +6 -0
  60. package/dest/utils/tx_effect_creation.d.ts.map +1 -0
  61. package/dest/utils/tx_effect_creation.js +16 -0
  62. package/package.json +30 -24
  63. package/src/bin/index.ts +3 -2
  64. package/src/constants.ts +4 -0
  65. package/src/index.ts +105 -74
  66. package/src/oracle/interfaces.ts +86 -0
  67. package/src/oracle/txe_oracle_public_context.ts +160 -0
  68. package/src/oracle/txe_oracle_top_level_context.ts +718 -0
  69. package/src/rpc_translator.ts +1083 -0
  70. package/src/state_machine/archiver.ts +294 -0
  71. package/src/state_machine/dummy_p2p_client.ts +209 -0
  72. package/src/state_machine/global_variable_builder.ts +38 -0
  73. package/src/state_machine/index.ts +78 -0
  74. package/src/state_machine/mock_epoch_cache.ts +72 -0
  75. package/src/state_machine/synchronizer.ts +88 -0
  76. package/src/txe_session.ts +525 -0
  77. package/src/util/encoding.ts +85 -11
  78. package/src/util/{txe_account_data_provider.ts → txe_account_store.ts} +1 -1
  79. package/src/util/txe_contract_store.ts +36 -0
  80. package/src/util/txe_public_contract_data_source.ts +43 -58
  81. package/src/utils/block_creation.ts +94 -0
  82. package/src/utils/tx_effect_creation.ts +38 -0
  83. package/dest/node/txe_node.d.ts +0 -358
  84. package/dest/node/txe_node.d.ts.map +0 -1
  85. package/dest/node/txe_node.js +0 -504
  86. package/dest/oracle/txe_oracle.d.ts +0 -152
  87. package/dest/oracle/txe_oracle.d.ts.map +0 -1
  88. package/dest/oracle/txe_oracle.js +0 -833
  89. package/dest/txe_service/txe_service.d.ts +0 -212
  90. package/dest/txe_service/txe_service.d.ts.map +0 -1
  91. package/dest/txe_service/txe_service.js +0 -572
  92. package/dest/util/txe_account_data_provider.d.ts +0 -10
  93. package/dest/util/txe_account_data_provider.d.ts.map +0 -1
  94. package/dest/util/txe_world_state_db.d.ts +0 -14
  95. package/dest/util/txe_world_state_db.d.ts.map +0 -1
  96. package/dest/util/txe_world_state_db.js +0 -27
  97. package/src/node/txe_node.ts +0 -725
  98. package/src/oracle/txe_oracle.ts +0 -1241
  99. package/src/txe_service/txe_service.ts +0 -749
  100. package/src/util/txe_world_state_db.ts +0 -38
@@ -1,4 +1,5 @@
1
- import { Fr } from '@aztec/foundation/fields';
1
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
+ import type { EthAddress } from '@aztec/foundation/eth-address';
2
3
  import { hexToBuffer } from '@aztec/foundation/string';
3
4
  import { type ContractArtifact, ContractArtifactSchema } from '@aztec/stdlib/abi';
4
5
  import { AztecAddress } from '@aztec/stdlib/aztec-address';
@@ -31,7 +32,7 @@ export function fromArray(obj: ForeignCallArray) {
31
32
  /**
32
33
  * Converts an array of Noir unsigned integers to a single tightly-packed buffer.
33
34
  * @param uintBitSize If it's an array of Noir u8's, put `8`, etc.
34
- * @returns
35
+ * @returns A buffer where each byte is correctly represented as a single byte in the buffer.
35
36
  */
36
37
  export function fromUintArray(obj: ForeignCallArray, uintBitSize: number): Buffer {
37
38
  if (uintBitSize % 8 !== 0) {
@@ -41,14 +42,50 @@ export function fromUintArray(obj: ForeignCallArray, uintBitSize: number): Buffe
41
42
  return Buffer.concat(obj.map(str => hexToBuffer(str).slice(-uintByteSize)));
42
43
  }
43
44
 
44
- export function toSingle(obj: Fr | AztecAddress): ForeignCallSingle {
45
- return obj.toString().slice(2);
45
+ /**
46
+ * Converts a Noir BoundedVec of unsigned integers into a Buffer. Note that BoundedVecs are structs, and therefore
47
+ * translated as two separate ForeignCallArray and ForeignCallSingle values (an array and a single field).
48
+ *
49
+ * @param storage The array with the BoundedVec's storage (i.e. BoundedVec::storage())
50
+ * @param length The length of the BoundedVec (i.e. BoundedVec::len())
51
+ * @param uintBitSize If it's an array of Noir u8's, put `8`, etc.
52
+ * @returns A buffer containing the unsigned integers tightly packed
53
+ */
54
+ export function fromUintBoundedVec(storage: ForeignCallArray, length: ForeignCallSingle, uintBitSize: number): Buffer {
55
+ if (uintBitSize % 8 !== 0) {
56
+ throw new Error(`u${uintBitSize} is not a supported type in Noir`);
57
+ }
58
+ const uintByteSize = uintBitSize / 8;
59
+ const boundedStorage = storage.slice(0, fromSingle(length).toNumber());
60
+ return Buffer.concat(boundedStorage.map(str => hexToBuffer(str).slice(-uintByteSize)));
61
+ }
62
+
63
+ // Just like toACVMField in yarn-project/simulator/src/private/acvm/serialize.ts but returns a ForeignCallSingle
64
+ // instead of an ACVMField.
65
+ export function toSingle(
66
+ value: AztecAddress | EthAddress | Fr | Buffer | boolean | number | bigint,
67
+ ): ForeignCallSingle {
68
+ let valueAsField;
69
+ if (Buffer.isBuffer(value)) {
70
+ valueAsField = Fr.fromBuffer(value);
71
+ } else if (typeof value === 'boolean' || typeof value === 'number' || typeof value === 'bigint') {
72
+ valueAsField = new Fr(value);
73
+ } else if (typeof value === 'string') {
74
+ valueAsField = Fr.fromHexString(value);
75
+ } else {
76
+ valueAsField = value;
77
+ }
78
+ return valueAsField.toString().slice(2);
46
79
  }
47
80
 
48
81
  export function toArray(objs: Fr[]): ForeignCallArray {
49
82
  return objs.map(obj => obj.toString());
50
83
  }
51
84
 
85
+ export function toSingleOrArray(value: Fr | Fr[]) {
86
+ return Array.isArray(value) ? value.map(toSingle) : toSingle(value);
87
+ }
88
+
52
89
  export function bufferToU8Array(buffer: Buffer): ForeignCallArray {
53
90
  return toArray(Array.from(buffer).map(byte => new Fr(byte)));
54
91
  }
@@ -57,24 +94,61 @@ export function bufferToU8Array(buffer: Buffer): ForeignCallArray {
57
94
  * Converts a ForeignCallArray into a tuple which represents a nr BoundedVec.
58
95
  * If the input array is shorter than the maxLen, it pads the result with zeros,
59
96
  * so that nr can correctly coerce this result into a BoundedVec.
60
- * @param array
97
+ * @param bVecStorage - The array underlying the BoundedVec.
61
98
  * @param maxLen - the max length of the BoundedVec.
62
99
  * @returns a tuple representing a BoundedVec.
63
100
  */
64
- export function arrayToBoundedVec(array: ForeignCallArray, maxLen: number): [ForeignCallArray, ForeignCallSingle] {
65
- if (array.length > maxLen) {
66
- throw new Error(`Array of length ${array.length} larger than maxLen ${maxLen}`);
101
+ export function arrayToBoundedVec(
102
+ bVecStorage: ForeignCallArray,
103
+ maxLen: number,
104
+ ): [ForeignCallArray, ForeignCallSingle] {
105
+ if (bVecStorage.length > maxLen) {
106
+ throw new Error(`Array of length ${bVecStorage.length} larger than maxLen ${maxLen}`);
67
107
  }
68
- const lengthDiff = maxLen - array.length;
108
+ const lengthDiff = maxLen - bVecStorage.length;
69
109
  // We pad the array to the maxLen of the BoundedVec.
70
110
  const zeroPaddingArray = toArray(Array(lengthDiff).fill(new Fr(0)));
71
111
 
72
112
  // These variable names match with the BoundedVec members in nr:
73
- const storage = array.concat(zeroPaddingArray);
74
- const len = toSingle(new Fr(array.length));
113
+ const storage = bVecStorage.concat(zeroPaddingArray);
114
+ const len = toSingle(new Fr(bVecStorage.length));
75
115
  return [storage, len];
76
116
  }
77
117
 
118
+ /**
119
+ * Converts an array of arrays representing Noir BoundedVec of nested arrays into its Noir serialized form.
120
+ * @param bVecStorage - The array underlying the BoundedVec.
121
+ * @param maxLen - The max length of the BoundedVec (max num of the nested arrays in the BoundedVec).
122
+ * @param nestedArrayLength - The length of the nested arrays (each nested array has to have the same length).
123
+ * @returns Serialized BoundedVec following Noir intrinsic serialization.
124
+ */
125
+ export function arrayOfArraysToBoundedVecOfArrays(
126
+ bVecStorage: ForeignCallArray[],
127
+ maxLen: number,
128
+ nestedArrayLength: number,
129
+ ): [ForeignCallArray, ForeignCallSingle] {
130
+ if (bVecStorage.length > maxLen) {
131
+ throw new Error(`Array of length ${bVecStorage.length} larger than maxLen ${maxLen}`);
132
+ }
133
+
134
+ // Check that all nested arrays have length nestedArrayLength
135
+ if (!bVecStorage.every(nestedArray => nestedArray.length === nestedArrayLength)) {
136
+ throw new Error(
137
+ `Nested array length passed in from Noir does not correspond to the length obtained in TS: ${nestedArrayLength} !== ${bVecStorage[0].length}`,
138
+ );
139
+ }
140
+
141
+ const flattenedStorage = bVecStorage.flat();
142
+
143
+ const numFieldsToPad = maxLen * nestedArrayLength - flattenedStorage.length;
144
+
145
+ const flattenedStorageWithPadding = flattenedStorage.concat(Array(numFieldsToPad).fill(new Fr(0)));
146
+
147
+ // At last we get the actual length of the BoundedVec and return the values.
148
+ const len = toSingle(new Fr(bVecStorage.length));
149
+ return [flattenedStorageWithPadding, len];
150
+ }
151
+
78
152
  export function toForeignCallResult(obj: (ForeignCallSingle | ForeignCallArray)[]) {
79
153
  return { values: obj };
80
154
  }
@@ -2,7 +2,7 @@ import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
2
2
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
3
3
  import { CompleteAddress } from '@aztec/stdlib/contract';
4
4
 
5
- export class TXEAccountDataProvider {
5
+ export class TXEAccountStore {
6
6
  #accounts: AztecAsyncMap<string, Buffer>;
7
7
 
8
8
  constructor(store: AztecAsyncKVStore) {
@@ -0,0 +1,36 @@
1
+ import type { ContractArtifact } from '@aztec/aztec.js/abi';
2
+ import { Fr } from '@aztec/aztec.js/fields';
3
+ import { ContractStore } from '@aztec/pxe/server';
4
+
5
+ export type ContractArtifactWithHash = ContractArtifact & { artifactHash: Fr };
6
+
7
+ /*
8
+ * A contract store that stores contract artifacts with their hashes. Since
9
+ * TXE typically deploys the same contract again and again for multiple tests, caching
10
+ * the *very* expensive artifact hash computation improves testing speed significantly.
11
+ */
12
+ export class TXEContractStore extends ContractStore {
13
+ #artifactHashes: Map<string, Buffer> = new Map();
14
+
15
+ public override async addContractArtifact(
16
+ id: Fr,
17
+ artifact: ContractArtifact | ContractArtifactWithHash,
18
+ ): Promise<void> {
19
+ if ('artifactHash' in artifact) {
20
+ this.#artifactHashes.set(id.toString(), artifact.artifactHash.toBuffer());
21
+ }
22
+ await super.addContractArtifact(id, artifact);
23
+ }
24
+
25
+ public override async getContractArtifact(
26
+ contractClassId: Fr,
27
+ ): Promise<ContractArtifact | ContractArtifactWithHash | undefined> {
28
+ const artifact = await super.getContractArtifact(contractClassId);
29
+ if (artifact && this.#artifactHashes.has(contractClassId.toString())) {
30
+ (artifact as ContractArtifactWithHash).artifactHash = Fr.fromBuffer(
31
+ this.#artifactHashes.get(contractClassId.toString())!,
32
+ );
33
+ }
34
+ return artifact;
35
+ }
36
+ }
@@ -1,67 +1,70 @@
1
- import { PUBLIC_DISPATCH_SELECTOR } from '@aztec/constants';
2
- import { Fr } from '@aztec/foundation/fields';
3
- import { PrivateFunctionsTree } from '@aztec/pxe/server';
4
- import { type ContractArtifact, FunctionSelector } from '@aztec/stdlib/abi';
1
+ import { BlockNumber } from '@aztec/foundation/branded-types';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
+ import type { ContractStore } from '@aztec/pxe/server';
4
+ import { type ContractArtifact, FunctionSelector, FunctionType } from '@aztec/stdlib/abi';
5
5
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
6
6
  import {
7
7
  type ContractClassPublic,
8
8
  type ContractDataSource,
9
9
  type ContractInstanceWithAddress,
10
- type PublicFunction,
10
+ computePrivateFunctionsRoot,
11
11
  computePublicBytecodeCommitment,
12
+ getContractClassPrivateFunctionFromArtifact,
12
13
  } from '@aztec/stdlib/contract';
13
14
 
14
- import type { TXE } from '../oracle/txe_oracle.js';
15
-
16
15
  export class TXEPublicContractDataSource implements ContractDataSource {
17
- constructor(private txeOracle: TXE) {}
18
-
19
- async getPublicFunction(address: AztecAddress, selector: FunctionSelector): Promise<PublicFunction | undefined> {
20
- const bytecode = await this.txeOracle.getContractDataProvider().getBytecode(address, selector);
21
- if (!bytecode) {
22
- return undefined;
23
- }
24
- return { bytecode, selector };
25
- }
16
+ #privateFunctionsRoot: Map<string, Buffer> = new Map();
17
+ constructor(
18
+ private blockNumber: BlockNumber,
19
+ private contractStore: ContractStore,
20
+ ) {}
26
21
 
27
- getBlockNumber(): Promise<number> {
28
- return this.txeOracle.getBlockNumber();
22
+ getBlockNumber(): Promise<BlockNumber> {
23
+ return Promise.resolve(this.blockNumber);
29
24
  }
30
25
 
31
26
  async getContractClass(id: Fr): Promise<ContractClassPublic | undefined> {
32
- const contractClass = await this.txeOracle.getContractDataProvider().getContractClass(id);
33
- const artifact = await this.txeOracle.getContractDataProvider().getContractArtifact(id);
34
- const tree = await PrivateFunctionsTree.create(artifact);
35
- const privateFunctionsRoot = await tree.getFunctionTreeRoot();
27
+ const contractClass = await this.contractStore.getContractClass(id);
28
+ if (!contractClass) {
29
+ return;
30
+ }
31
+ const artifact = await this.contractStore.getContractArtifact(id);
32
+ if (!artifact) {
33
+ return;
34
+ }
36
35
 
37
- const publicFunctions: PublicFunction[] = [];
38
- if (contractClass!.packedBytecode.length > 0) {
39
- publicFunctions.push({
40
- selector: FunctionSelector.fromField(new Fr(PUBLIC_DISPATCH_SELECTOR)),
41
- bytecode: contractClass!.packedBytecode,
42
- });
36
+ let privateFunctionsRoot;
37
+ if (!this.#privateFunctionsRoot.has(id.toString())) {
38
+ const privateFunctions = await Promise.all(
39
+ artifact.functions
40
+ .filter(fn => fn.functionType === FunctionType.PRIVATE)
41
+ .map(fn => getContractClassPrivateFunctionFromArtifact(fn)),
42
+ );
43
+ privateFunctionsRoot = await computePrivateFunctionsRoot(privateFunctions);
44
+ this.#privateFunctionsRoot.set(id.toString(), privateFunctionsRoot.toBuffer());
45
+ } else {
46
+ privateFunctionsRoot = Fr.fromBuffer(this.#privateFunctionsRoot.get(id.toString())!);
43
47
  }
44
48
 
45
49
  return {
46
50
  id,
47
51
  artifactHash: contractClass!.artifactHash,
48
52
  packedBytecode: contractClass!.packedBytecode,
49
- publicFunctions: publicFunctions,
50
- privateFunctionsRoot: new Fr(privateFunctionsRoot!.root),
53
+ privateFunctionsRoot,
51
54
  version: contractClass!.version,
52
55
  privateFunctions: [],
53
- unconstrainedFunctions: [],
56
+ utilityFunctions: [],
54
57
  };
55
58
  }
56
59
 
57
60
  async getBytecodeCommitment(id: Fr): Promise<Fr | undefined> {
58
- const contractClass = await this.txeOracle.getContractDataProvider().getContractClass(id);
59
- return computePublicBytecodeCommitment(contractClass.packedBytecode);
61
+ const contractClass = await this.contractStore.getContractClass(id);
62
+ return contractClass && computePublicBytecodeCommitment(contractClass.packedBytecode);
60
63
  }
61
64
 
62
65
  async getContract(address: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
63
- const instance = await this.txeOracle.getContractDataProvider().getContractInstance(address);
64
- return { ...instance, address };
66
+ const instance = await this.contractStore.getContractInstance(address);
67
+ return instance && { ...instance, address };
65
68
  }
66
69
 
67
70
  getContractClassIds(): Promise<Fr[]> {
@@ -69,33 +72,15 @@ export class TXEPublicContractDataSource implements ContractDataSource {
69
72
  }
70
73
 
71
74
  async getContractArtifact(address: AztecAddress): Promise<ContractArtifact | undefined> {
72
- const instance = await this.txeOracle.getContractDataProvider().getContractInstance(address);
73
- return this.txeOracle.getContractDataProvider().getContractArtifact(instance.currentContractClassId);
75
+ const instance = await this.contractStore.getContractInstance(address);
76
+ return instance && this.contractStore.getContractArtifact(instance.currentContractClassId);
74
77
  }
75
78
 
76
- async getContractFunctionName(address: AztecAddress, selector: FunctionSelector): Promise<string | undefined> {
77
- const artifact = await this.getContractArtifact(address);
78
- if (!artifact) {
79
- return undefined;
80
- }
81
- const functionSelectorsAndNames = await Promise.all(
82
- artifact.functions.map(async f => ({
83
- name: f.name,
84
- selector: await FunctionSelector.fromNameAndParameters({ name: f.name, parameters: f.parameters }),
85
- })),
86
- );
87
- const func = functionSelectorsAndNames.find(f => f.selector.equals(selector));
88
-
89
- return Promise.resolve(func?.name);
90
- }
91
-
92
- registerContractFunctionSignatures(_address: AztecAddress, _signatures: []): Promise<void> {
93
- return Promise.resolve();
79
+ async getDebugFunctionName(address: AztecAddress, selector: FunctionSelector): Promise<string | undefined> {
80
+ return await this.contractStore.getDebugFunctionName(address, selector);
94
81
  }
95
82
 
96
- // TODO(#10007): Remove this method.
97
- addContractClass(_contractClass: ContractClassPublic): Promise<void> {
98
- // We don't really need to do anything for the txe here
83
+ registerContractFunctionSignatures(_signatures: []): Promise<void> {
99
84
  return Promise.resolve();
100
85
  }
101
86
  }
@@ -0,0 +1,94 @@
1
+ import {
2
+ MAX_NOTE_HASHES_PER_TX,
3
+ MAX_NULLIFIERS_PER_TX,
4
+ NULLIFIER_SUBTREE_HEIGHT,
5
+ NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
6
+ } from '@aztec/constants';
7
+ import { BlockNumber } from '@aztec/foundation/branded-types';
8
+ import { padArrayEnd } from '@aztec/foundation/collection';
9
+ import { Fr } from '@aztec/foundation/curves/bn254';
10
+ import { Body, L2Block, L2BlockHeader } from '@aztec/stdlib/block';
11
+ import { AppendOnlyTreeSnapshot, MerkleTreeId, type MerkleTreeWriteOperations } from '@aztec/stdlib/trees';
12
+ import { GlobalVariables, TxEffect } from '@aztec/stdlib/tx';
13
+
14
+ /**
15
+ * Returns a transaction request hash that is valid for transactions that are the only ones in a block.
16
+ * @param blockNumber The number for the block in which there is a single transaction.
17
+ * @returns The transaction request hash.
18
+ */
19
+ export function getSingleTxBlockRequestHash(blockNumber: BlockNumber): Fr {
20
+ return new Fr(blockNumber + 9999); // Why does this need to be a high number? Why do small numbered nullifiers already exist?
21
+ }
22
+
23
+ export async function insertTxEffectIntoWorldTrees(
24
+ txEffect: TxEffect,
25
+ worldTrees: MerkleTreeWriteOperations,
26
+ ): Promise<void> {
27
+ await worldTrees.appendLeaves(
28
+ MerkleTreeId.NOTE_HASH_TREE,
29
+ padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
30
+ );
31
+
32
+ await worldTrees.batchInsert(
33
+ MerkleTreeId.NULLIFIER_TREE,
34
+ padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX).map(nullifier => nullifier.toBuffer()),
35
+ NULLIFIER_SUBTREE_HEIGHT,
36
+ );
37
+
38
+ await worldTrees.appendLeaves(
39
+ MerkleTreeId.L1_TO_L2_MESSAGE_TREE,
40
+ padArrayEnd<Fr, number>(txEffect.l2ToL1Msgs, Fr.ZERO, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP),
41
+ );
42
+
43
+ // We do not need to add public data writes because we apply them as we go.
44
+ }
45
+
46
+ export async function makeTXEBlockHeader(
47
+ worldTrees: MerkleTreeWriteOperations,
48
+ globalVariables: GlobalVariables,
49
+ ): Promise<L2BlockHeader> {
50
+ const stateReference = await worldTrees.getStateReference();
51
+ const archiveInfo = await worldTrees.getTreeInfo(MerkleTreeId.ARCHIVE);
52
+
53
+ return new L2BlockHeader(
54
+ new AppendOnlyTreeSnapshot(new Fr(archiveInfo.root), Number(archiveInfo.size)),
55
+ Fr.ZERO,
56
+ Fr.ZERO,
57
+ stateReference,
58
+ globalVariables,
59
+ Fr.ZERO,
60
+ Fr.ZERO,
61
+ Fr.ZERO,
62
+ Fr.ZERO,
63
+ );
64
+ }
65
+
66
+ /**
67
+ * Creates an L2Block with proper archive chaining.
68
+ * This function:
69
+ * 1. Gets the current archive state as lastArchive for the header
70
+ * 2. Creates the block header
71
+ * 3. Updates the archive tree with the header hash
72
+ * 4. Gets the new archive state for the block's archive
73
+ *
74
+ * @param worldTrees - The world trees to read/write from
75
+ * @param globalVariables - Global variables for the block
76
+ * @param txEffects - Transaction effects to include in the block
77
+ * @returns The created L2Block with proper archive chaining
78
+ */
79
+ export async function makeTXEBlock(
80
+ worldTrees: MerkleTreeWriteOperations,
81
+ globalVariables: GlobalVariables,
82
+ txEffects: TxEffect[],
83
+ ): Promise<L2Block> {
84
+ const header = await makeTXEBlockHeader(worldTrees, globalVariables);
85
+
86
+ // Update the archive tree with this block's header hash
87
+ await worldTrees.updateArchive(header.toBlockHeader());
88
+
89
+ // Get the new archive state after updating
90
+ const newArchiveInfo = await worldTrees.getTreeInfo(MerkleTreeId.ARCHIVE);
91
+ const newArchive = new AppendOnlyTreeSnapshot(new Fr(newArchiveInfo.root), Number(newArchiveInfo.size));
92
+
93
+ return new L2Block(newArchive, header, new Body(txEffects));
94
+ }
@@ -0,0 +1,38 @@
1
+ import { BlockNumber } from '@aztec/foundation/branded-types';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
+ import type { ExecutionNoteCache } from '@aztec/pxe/simulator';
4
+ import { computeNoteHashNonce, computeUniqueNoteHash, siloNoteHash } from '@aztec/stdlib/hash';
5
+ import { TxEffect, TxHash } from '@aztec/stdlib/tx';
6
+
7
+ export async function makeTxEffect(
8
+ noteCache: ExecutionNoteCache,
9
+ protocolNullifier: Fr,
10
+ txBlockNumber: BlockNumber,
11
+ ): Promise<TxEffect> {
12
+ const txEffect = TxEffect.empty();
13
+
14
+ const { usedProtocolNullifierForNonces } = noteCache.finish();
15
+ const nonceGenerator = usedProtocolNullifierForNonces ? protocolNullifier : noteCache.getAllNullifiers()[0];
16
+
17
+ txEffect.noteHashes = await Promise.all(
18
+ noteCache
19
+ .getAllNotes()
20
+ .map(async (pendingNote, i) =>
21
+ computeUniqueNoteHash(
22
+ await computeNoteHashNonce(nonceGenerator, i),
23
+ await siloNoteHash(pendingNote.note.contractAddress, pendingNote.noteHashForConsumption),
24
+ ),
25
+ ),
26
+ );
27
+
28
+ // Nullifiers are already siloed
29
+ txEffect.nullifiers = noteCache.getAllNullifiers();
30
+
31
+ if (usedProtocolNullifierForNonces) {
32
+ txEffect.nullifiers.unshift(protocolNullifier);
33
+ }
34
+
35
+ txEffect.txHash = new TxHash(new Fr(txBlockNumber));
36
+
37
+ return txEffect;
38
+ }