@aztec/txe 2.1.0-rc.9 → 3.0.0-devnet.2

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 (71) hide show
  1. package/dest/bin/index.js +1 -1
  2. package/dest/index.d.ts.map +1 -1
  3. package/dest/index.js +5 -2
  4. package/dest/oracle/interfaces.d.ts +54 -0
  5. package/dest/oracle/interfaces.d.ts.map +1 -0
  6. package/dest/oracle/interfaces.js +3 -0
  7. package/dest/oracle/txe_oracle_public_context.d.ts +7 -7
  8. package/dest/oracle/txe_oracle_public_context.d.ts.map +1 -1
  9. package/dest/oracle/txe_oracle_public_context.js +29 -31
  10. package/dest/oracle/txe_oracle_top_level_context.d.ts +56 -0
  11. package/dest/oracle/txe_oracle_top_level_context.d.ts.map +1 -0
  12. package/dest/oracle/txe_oracle_top_level_context.js +423 -0
  13. package/dest/{txe_service/txe_service.d.ts → rpc_translator.d.ts} +34 -28
  14. package/dest/rpc_translator.d.ts.map +1 -0
  15. package/dest/{txe_service/txe_service.js → rpc_translator.js} +143 -115
  16. package/dest/state_machine/archiver.d.ts +5 -1
  17. package/dest/state_machine/archiver.d.ts.map +1 -1
  18. package/dest/state_machine/archiver.js +8 -1
  19. package/dest/state_machine/dummy_p2p_client.d.ts +1 -0
  20. package/dest/state_machine/dummy_p2p_client.d.ts.map +1 -1
  21. package/dest/state_machine/dummy_p2p_client.js +3 -0
  22. package/dest/state_machine/global_variable_builder.d.ts +1 -14
  23. package/dest/state_machine/global_variable_builder.d.ts.map +1 -1
  24. package/dest/state_machine/global_variable_builder.js +3 -22
  25. package/dest/state_machine/index.d.ts.map +1 -1
  26. package/dest/state_machine/index.js +4 -3
  27. package/dest/txe_session.d.ts +34 -44
  28. package/dest/txe_session.d.ts.map +1 -1
  29. package/dest/txe_session.js +197 -82
  30. package/dest/util/encoding.d.ts +1 -1
  31. package/dest/util/txe_contract_data_provider.d.ts +2 -1
  32. package/dest/util/txe_contract_data_provider.d.ts.map +1 -1
  33. package/dest/util/txe_contract_data_provider.js +1 -1
  34. package/dest/util/txe_public_contract_data_source.d.ts +4 -3
  35. package/dest/util/txe_public_contract_data_source.d.ts.map +1 -1
  36. package/dest/util/txe_public_contract_data_source.js +13 -11
  37. package/dest/utils/block_creation.d.ts +13 -0
  38. package/dest/utils/block_creation.d.ts.map +1 -0
  39. package/dest/utils/block_creation.js +24 -0
  40. package/dest/utils/tx_effect_creation.d.ts +5 -0
  41. package/dest/utils/tx_effect_creation.d.ts.map +1 -0
  42. package/dest/utils/tx_effect_creation.js +16 -0
  43. package/package.json +15 -15
  44. package/src/bin/index.ts +1 -1
  45. package/src/index.ts +5 -8
  46. package/src/oracle/interfaces.ts +83 -0
  47. package/src/oracle/txe_oracle_public_context.ts +36 -69
  48. package/src/oracle/txe_oracle_top_level_context.ts +658 -0
  49. package/src/{txe_service/txe_service.ts → rpc_translator.ts} +181 -115
  50. package/src/state_machine/archiver.ts +8 -2
  51. package/src/state_machine/dummy_p2p_client.ts +4 -0
  52. package/src/state_machine/global_variable_builder.ts +6 -41
  53. package/src/state_machine/index.ts +6 -4
  54. package/src/txe_session.ts +301 -90
  55. package/src/util/txe_contract_data_provider.ts +2 -1
  56. package/src/util/txe_public_contract_data_source.ts +13 -11
  57. package/src/utils/block_creation.ts +62 -0
  58. package/src/utils/tx_effect_creation.ts +37 -0
  59. package/dest/oracle/txe_oracle.d.ts +0 -124
  60. package/dest/oracle/txe_oracle.d.ts.map +0 -1
  61. package/dest/oracle/txe_oracle.js +0 -770
  62. package/dest/oracle/txe_typed_oracle.d.ts +0 -42
  63. package/dest/oracle/txe_typed_oracle.d.ts.map +0 -1
  64. package/dest/oracle/txe_typed_oracle.js +0 -83
  65. package/dest/txe_constants.d.ts +0 -2
  66. package/dest/txe_constants.d.ts.map +0 -1
  67. package/dest/txe_constants.js +0 -7
  68. package/dest/txe_service/txe_service.d.ts.map +0 -1
  69. package/src/oracle/txe_oracle.ts +0 -1287
  70. package/src/oracle/txe_typed_oracle.ts +0 -142
  71. package/src/txe_constants.ts +0 -9
@@ -0,0 +1,83 @@
1
+ import type { ContractArtifact } from '@aztec/aztec.js/abi';
2
+ import { CompleteAddress } from '@aztec/aztec.js/addresses';
3
+ import type { ContractInstanceWithAddress } from '@aztec/aztec.js/contracts';
4
+ import { TxHash } from '@aztec/aztec.js/tx';
5
+ import type { Fr } from '@aztec/foundation/fields';
6
+ import type { FunctionSelector } from '@aztec/stdlib/abi';
7
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
8
+ import type { UInt32, UInt64 } from '@aztec/stdlib/types';
9
+
10
+ // These interfaces complement the ones defined in PXE, and combined with those contain the full list of oracles used by
11
+ // aztec-nr. In particular, these include the ones needed to run Brillig code associated to #[external("public")] functions that has
12
+ // not been transpiled (e.g. in the context of a Noir test) as well as the ones associated with managing the state of
13
+ // such a Noir test (deploying contracts, manipulating block time, making calls, etc) - the so called 'top level test
14
+ // context'.
15
+
16
+ /**
17
+ * Oracle methods associated with the execution of an Aztec #[external("public")] function.
18
+ *
19
+ * Note that real contracts have their Brillig calls to these be transpiled into opcodes, the oracles are only executed
20
+ * as such when running the original Brillig code, e.g. when invoking functions that interact with a PublicContext
21
+ * directly in a Noir test.
22
+ */
23
+ export interface IAvmExecutionOracle {
24
+ isAvm: true;
25
+
26
+ avmOpcodeAddress(): Promise<AztecAddress>;
27
+ avmOpcodeSender(): Promise<AztecAddress>;
28
+ avmOpcodeBlockNumber(): Promise<UInt32>;
29
+ avmOpcodeTimestamp(): Promise<bigint>;
30
+ avmOpcodeIsStaticCall(): Promise<boolean>;
31
+ avmOpcodeChainId(): Promise<Fr>;
32
+ avmOpcodeVersion(): Promise<Fr>;
33
+ avmOpcodeEmitNullifier(nullifier: Fr): Promise<void>;
34
+ avmOpcodeEmitNoteHash(noteHash: Fr): Promise<void>;
35
+ avmOpcodeNullifierExists(innerNullifier: Fr, targetAddress: AztecAddress): Promise<boolean>;
36
+ avmOpcodeStorageWrite(slot: Fr, value: Fr): Promise<void>;
37
+ avmOpcodeStorageRead(slot: Fr): Promise<Fr>;
38
+ }
39
+
40
+ /**
41
+ * Oracle methods associated with the execution of an Aztec Noir test.
42
+ */
43
+ export interface ITxeExecutionOracle {
44
+ isTxe: true;
45
+
46
+ txeGetNextBlockNumber(): Promise<number>;
47
+ txeGetNextBlockTimestamp(): Promise<UInt64>;
48
+ txeAdvanceBlocksBy(blocks: number): Promise<void>;
49
+ txeAdvanceTimestampBy(duration: UInt64): void;
50
+ txeDeploy(artifact: ContractArtifact, instance: ContractInstanceWithAddress, foreignSecret: Fr): Promise<void>;
51
+ txeCreateAccount(secret: Fr): Promise<CompleteAddress>;
52
+ txeAddAccount(
53
+ artifact: ContractArtifact,
54
+ instance: ContractInstanceWithAddress,
55
+ secret: Fr,
56
+ ): Promise<CompleteAddress>;
57
+ txeAddAuthWitness(address: AztecAddress, messageHash: Fr): Promise<void>;
58
+ txeGetLastBlockTimestamp(): Promise<bigint>;
59
+ txeGetLastTxEffects(): Promise<{
60
+ txHash: TxHash;
61
+ noteHashes: Fr[];
62
+ nullifiers: Fr[];
63
+ }>;
64
+ txePrivateCallNewFlow(
65
+ from: AztecAddress,
66
+ targetContractAddress: AztecAddress,
67
+ functionSelector: FunctionSelector,
68
+ args: Fr[],
69
+ argsHash: Fr,
70
+ isStaticCall: boolean,
71
+ ): Promise<Fr[]>;
72
+ txeSimulateUtilityFunction(
73
+ targetContractAddress: AztecAddress,
74
+ functionSelector: FunctionSelector,
75
+ args: Fr[],
76
+ ): Promise<Fr[]>;
77
+ txePublicCallNewFlow(
78
+ from: AztecAddress,
79
+ targetContractAddress: AztecAddress,
80
+ calldata: Fr[],
81
+ isStaticCall: boolean,
82
+ ): Promise<Fr[]>;
83
+ }
@@ -1,30 +1,25 @@
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 { padArrayEnd } from '@aztec/foundation/collection';
8
1
  import { Fr } from '@aztec/foundation/fields';
9
2
  import { type Logger, createLogger } from '@aztec/foundation/log';
10
3
  import { PublicDataWrite } from '@aztec/stdlib/avm';
11
- import type { AztecAddress } from '@aztec/stdlib/aztec-address';
4
+ import { AztecAddress } from '@aztec/stdlib/aztec-address';
12
5
  import { Body, L2Block } from '@aztec/stdlib/block';
13
6
  import { computePublicDataTreeLeafSlot, siloNoteHash, siloNullifier } from '@aztec/stdlib/hash';
14
- import { makeAppendOnlyTreeSnapshot, makeContentCommitment } from '@aztec/stdlib/testing';
7
+ import { makeAppendOnlyTreeSnapshot } from '@aztec/stdlib/testing';
15
8
  import {
16
- AppendOnlyTreeSnapshot,
17
9
  MerkleTreeId,
18
10
  type MerkleTreeWriteOperations,
19
11
  PublicDataTreeLeaf,
20
12
  PublicDataTreeLeafPreimage,
21
13
  } from '@aztec/stdlib/trees';
22
- import { BlockHeader, GlobalVariables, TxEffect, TxHash } from '@aztec/stdlib/tx';
14
+ import { GlobalVariables, TxEffect, TxHash } from '@aztec/stdlib/tx';
23
15
  import type { UInt32 } from '@aztec/stdlib/types';
24
16
 
25
- import { TXETypedOracle } from './txe_typed_oracle.js';
17
+ import { insertTxEffectIntoWorldTrees, makeTXEBlockHeader } from '../utils/block_creation.js';
18
+ import type { IAvmExecutionOracle } from './interfaces.js';
19
+
20
+ export class TXEOraclePublicContext implements IAvmExecutionOracle {
21
+ isAvm = true as const;
26
22
 
27
- export class TXEOraclePublicContext extends TXETypedOracle {
28
23
  private logger: Logger;
29
24
  private transientUniqueNoteHashes: Fr[] = [];
30
25
  private transientSiloedNullifiers: Fr[] = [];
@@ -32,80 +27,85 @@ export class TXEOraclePublicContext extends TXETypedOracle {
32
27
 
33
28
  constructor(
34
29
  private contractAddress: AztecAddress,
35
- private worldTrees: MerkleTreeWriteOperations,
30
+ private forkedWorldTrees: MerkleTreeWriteOperations,
36
31
  private txRequestHash: Fr,
37
32
  private globalVariables: GlobalVariables,
38
33
  ) {
39
- super();
40
34
  this.logger = createLogger('txe:public_context');
41
35
 
42
- this.logger.debug('Entering PublicContext', {
36
+ this.logger.debug('Entering Public Context', {
43
37
  contractAddress,
44
38
  blockNumber: globalVariables.blockNumber,
45
39
  timestamp: globalVariables.timestamp,
46
40
  });
47
41
  }
48
42
 
49
- override avmOpcodeAddress(): Promise<AztecAddress> {
43
+ avmOpcodeAddress(): Promise<AztecAddress> {
50
44
  return Promise.resolve(this.contractAddress);
51
45
  }
52
46
 
53
- override avmOpcodeBlockNumber(): Promise<UInt32> {
47
+ avmOpcodeSender(): Promise<AztecAddress> {
48
+ return Promise.resolve(AztecAddress.ZERO); // todo: change?
49
+ }
50
+
51
+ avmOpcodeBlockNumber(): Promise<UInt32> {
54
52
  return Promise.resolve(this.globalVariables.blockNumber);
55
53
  }
56
54
 
57
- override avmOpcodeTimestamp(): Promise<bigint> {
55
+ avmOpcodeTimestamp(): Promise<bigint> {
58
56
  return Promise.resolve(this.globalVariables.timestamp);
59
57
  }
60
58
 
61
- override avmOpcodeIsStaticCall(): Promise<boolean> {
59
+ avmOpcodeIsStaticCall(): Promise<boolean> {
62
60
  return Promise.resolve(false);
63
61
  }
64
62
 
65
- override avmOpcodeChainId(): Promise<Fr> {
63
+ avmOpcodeChainId(): Promise<Fr> {
66
64
  return Promise.resolve(this.globalVariables.chainId);
67
65
  }
68
66
 
69
- override avmOpcodeVersion(): Promise<Fr> {
67
+ avmOpcodeVersion(): Promise<Fr> {
70
68
  return Promise.resolve(this.globalVariables.version);
71
69
  }
72
70
 
73
- override async avmOpcodeEmitNullifier(nullifier: Fr) {
71
+ async avmOpcodeEmitNullifier(nullifier: Fr) {
74
72
  const siloedNullifier = await siloNullifier(this.contractAddress, nullifier);
75
73
  this.transientSiloedNullifiers.push(siloedNullifier);
76
74
  }
77
75
 
78
- override async avmOpcodeEmitNoteHash(noteHash: Fr) {
76
+ async avmOpcodeEmitNoteHash(noteHash: Fr) {
79
77
  const siloedNoteHash = await siloNoteHash(this.contractAddress, noteHash);
80
78
  // TODO: make the note hash unique - they are only siloed right now
81
79
  this.transientUniqueNoteHashes.push(siloedNoteHash);
82
80
  }
83
81
 
84
- override async avmOpcodeNullifierExists(innerNullifier: Fr, targetAddress: AztecAddress): Promise<boolean> {
82
+ async avmOpcodeNullifierExists(innerNullifier: Fr, targetAddress: AztecAddress): Promise<boolean> {
85
83
  const nullifier = await siloNullifier(targetAddress, innerNullifier!);
86
84
 
87
- const treeIndex = (await this.worldTrees.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, [nullifier.toBuffer()]))[0];
85
+ const treeIndex = (
86
+ await this.forkedWorldTrees.findLeafIndices(MerkleTreeId.NULLIFIER_TREE, [nullifier.toBuffer()])
87
+ )[0];
88
88
  const transientIndex = this.transientSiloedNullifiers.find(n => n.equals(nullifier));
89
89
 
90
90
  return treeIndex !== undefined || transientIndex !== undefined;
91
91
  }
92
92
 
93
- override async avmOpcodeStorageWrite(slot: Fr, value: Fr) {
93
+ async avmOpcodeStorageWrite(slot: Fr, value: Fr) {
94
94
  this.logger.debug('AVM storage write', { slot, value });
95
95
 
96
96
  const dataWrite = new PublicDataWrite(await computePublicDataTreeLeafSlot(this.contractAddress, slot), value);
97
97
 
98
98
  this.publicDataWrites.push(dataWrite);
99
99
 
100
- await this.worldTrees.sequentialInsert(MerkleTreeId.PUBLIC_DATA_TREE, [
100
+ await this.forkedWorldTrees.sequentialInsert(MerkleTreeId.PUBLIC_DATA_TREE, [
101
101
  new PublicDataTreeLeaf(dataWrite.leafSlot, dataWrite.value).toBuffer(),
102
102
  ]);
103
103
  }
104
104
 
105
- override async avmOpcodeStorageRead(slot: Fr): Promise<Fr> {
105
+ async avmOpcodeStorageRead(slot: Fr): Promise<Fr> {
106
106
  const leafSlot = await computePublicDataTreeLeafSlot(this.contractAddress, slot);
107
107
 
108
- const lowLeafResult = await this.worldTrees.getPreviousValueIndex(
108
+ const lowLeafResult = await this.forkedWorldTrees.getPreviousValueIndex(
109
109
  MerkleTreeId.PUBLIC_DATA_TREE,
110
110
  leafSlot.toBigInt(),
111
111
  );
@@ -114,7 +114,7 @@ export class TXEOraclePublicContext extends TXETypedOracle {
114
114
  !lowLeafResult || !lowLeafResult.alreadyPresent
115
115
  ? Fr.ZERO
116
116
  : (
117
- (await this.worldTrees.getLeafPreimage(
117
+ (await this.forkedWorldTrees.getLeafPreimage(
118
118
  MerkleTreeId.PUBLIC_DATA_TREE,
119
119
  lowLeafResult.index,
120
120
  )) as PublicDataTreeLeafPreimage
@@ -126,20 +126,20 @@ export class TXEOraclePublicContext extends TXETypedOracle {
126
126
  }
127
127
 
128
128
  async close(): Promise<L2Block> {
129
- this.logger.debug('Exiting PublicContext, building block with collected side effects', {
129
+ this.logger.debug('Exiting Public Context, building block with collected side effects', {
130
130
  blockNumber: this.globalVariables.blockNumber,
131
131
  });
132
132
 
133
133
  const txEffect = this.makeTxEffect();
134
- await this.insertSideEffectIntoWorldTrees(txEffect);
134
+ await insertTxEffectIntoWorldTrees(txEffect, this.forkedWorldTrees);
135
135
 
136
136
  const block = new L2Block(
137
- makeAppendOnlyTreeSnapshot(this.globalVariables.blockNumber),
138
- await this.makeBlockHeader(),
137
+ makeAppendOnlyTreeSnapshot(),
138
+ await makeTXEBlockHeader(this.forkedWorldTrees, this.globalVariables),
139
139
  new Body([txEffect]),
140
140
  );
141
141
 
142
- await this.worldTrees.close();
142
+ await this.forkedWorldTrees.close();
143
143
 
144
144
  this.logger.debug('Exited PublicContext with built block', {
145
145
  blockNumber: block.number,
@@ -162,37 +162,4 @@ export class TXEOraclePublicContext extends TXETypedOracle {
162
162
 
163
163
  return txEffect;
164
164
  }
165
-
166
- private async insertSideEffectIntoWorldTrees(txEffect: TxEffect) {
167
- const l1ToL2Messages = Array(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP).fill(0).map(Fr.zero);
168
-
169
- await this.worldTrees.appendLeaves(
170
- MerkleTreeId.NOTE_HASH_TREE,
171
- padArrayEnd(txEffect.noteHashes, Fr.ZERO, MAX_NOTE_HASHES_PER_TX),
172
- );
173
-
174
- await this.worldTrees.appendLeaves(MerkleTreeId.L1_TO_L2_MESSAGE_TREE, l1ToL2Messages);
175
-
176
- // We do not need to add public data writes because we apply them as we go.
177
-
178
- await this.worldTrees.batchInsert(
179
- MerkleTreeId.NULLIFIER_TREE,
180
- padArrayEnd(txEffect.nullifiers, Fr.ZERO, MAX_NULLIFIERS_PER_TX).map(nullifier => nullifier.toBuffer()),
181
- NULLIFIER_SUBTREE_HEIGHT,
182
- );
183
- }
184
-
185
- private async makeBlockHeader(): Promise<BlockHeader> {
186
- const stateReference = await this.worldTrees.getStateReference();
187
- const archiveInfo = await this.worldTrees.getTreeInfo(MerkleTreeId.ARCHIVE);
188
-
189
- return new BlockHeader(
190
- new AppendOnlyTreeSnapshot(new Fr(archiveInfo.root), Number(archiveInfo.size)),
191
- makeContentCommitment(),
192
- stateReference,
193
- this.globalVariables,
194
- Fr.ZERO,
195
- Fr.ZERO,
196
- );
197
- }
198
165
  }