@aztec/txe 0.0.0-test.1 → 0.0.1-commit.b655e406
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.
- package/dest/bin/index.js +3 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +78 -49
- package/dest/oracle/interfaces.d.ts +54 -0
- package/dest/oracle/interfaces.d.ts.map +1 -0
- package/dest/oracle/interfaces.js +3 -0
- package/dest/oracle/txe_oracle_public_context.d.ts +34 -0
- package/dest/oracle/txe_oracle_public_context.d.ts.map +1 -0
- package/dest/oracle/txe_oracle_public_context.js +124 -0
- package/dest/oracle/txe_oracle_top_level_context.d.ts +56 -0
- package/dest/oracle/txe_oracle_top_level_context.d.ts.map +1 -0
- package/dest/oracle/txe_oracle_top_level_context.js +423 -0
- package/dest/rpc_translator.d.ts +237 -0
- package/dest/rpc_translator.d.ts.map +1 -0
- package/dest/rpc_translator.js +617 -0
- package/dest/state_machine/archiver.d.ts +61 -0
- package/dest/state_machine/archiver.d.ts.map +1 -0
- package/dest/state_machine/archiver.js +118 -0
- package/dest/state_machine/dummy_p2p_client.d.ts +52 -0
- package/dest/state_machine/dummy_p2p_client.d.ts.map +1 -0
- package/dest/state_machine/dummy_p2p_client.js +133 -0
- package/dest/state_machine/global_variable_builder.d.ts +9 -0
- package/dest/state_machine/global_variable_builder.d.ts.map +1 -0
- package/dest/state_machine/global_variable_builder.js +10 -0
- package/dest/state_machine/index.d.ts +16 -0
- package/dest/state_machine/index.d.ts.map +1 -0
- package/dest/state_machine/index.js +51 -0
- package/dest/state_machine/mock_epoch_cache.d.ts +25 -0
- package/dest/state_machine/mock_epoch_cache.d.ts.map +1 -0
- package/dest/state_machine/mock_epoch_cache.js +50 -0
- package/dest/state_machine/synchronizer.d.ts +32 -0
- package/dest/state_machine/synchronizer.d.ts.map +1 -0
- package/dest/state_machine/synchronizer.js +58 -0
- package/dest/txe_session.d.ts +67 -0
- package/dest/txe_session.d.ts.map +1 -0
- package/dest/txe_session.js +253 -0
- package/dest/util/encoding.d.ts +26 -8
- package/dest/util/encoding.d.ts.map +1 -1
- package/dest/util/encoding.js +65 -10
- package/dest/util/txe_contract_data_provider.d.ts +12 -0
- package/dest/util/txe_contract_data_provider.d.ts.map +1 -0
- package/dest/util/txe_contract_data_provider.js +22 -0
- package/dest/util/txe_public_contract_data_source.d.ts +8 -8
- package/dest/util/txe_public_contract_data_source.d.ts.map +1 -1
- package/dest/util/txe_public_contract_data_source.js +36 -57
- package/dest/utils/block_creation.d.ts +13 -0
- package/dest/utils/block_creation.d.ts.map +1 -0
- package/dest/utils/block_creation.js +24 -0
- package/dest/utils/tx_effect_creation.d.ts +5 -0
- package/dest/utils/tx_effect_creation.d.ts.map +1 -0
- package/dest/utils/tx_effect_creation.js +16 -0
- package/package.json +27 -22
- package/src/bin/index.ts +3 -2
- package/src/index.ts +92 -64
- package/src/oracle/interfaces.ts +83 -0
- package/src/oracle/txe_oracle_public_context.ts +165 -0
- package/src/oracle/txe_oracle_top_level_context.ts +658 -0
- package/src/rpc_translator.ts +1013 -0
- package/src/state_machine/archiver.ts +151 -0
- package/src/state_machine/dummy_p2p_client.ts +197 -0
- package/src/state_machine/global_variable_builder.ts +20 -0
- package/src/state_machine/index.ts +77 -0
- package/src/state_machine/mock_epoch_cache.ts +67 -0
- package/src/state_machine/synchronizer.ts +87 -0
- package/src/txe_session.ts +432 -0
- package/src/util/encoding.ts +84 -10
- package/src/util/txe_contract_data_provider.ts +36 -0
- package/src/util/txe_public_contract_data_source.ts +40 -56
- package/src/utils/block_creation.ts +62 -0
- package/src/utils/tx_effect_creation.ts +37 -0
- package/dest/node/txe_node.d.ts +0 -358
- package/dest/node/txe_node.d.ts.map +0 -1
- package/dest/node/txe_node.js +0 -504
- package/dest/oracle/txe_oracle.d.ts +0 -152
- package/dest/oracle/txe_oracle.d.ts.map +0 -1
- package/dest/oracle/txe_oracle.js +0 -833
- package/dest/txe_service/txe_service.d.ts +0 -212
- package/dest/txe_service/txe_service.d.ts.map +0 -1
- package/dest/txe_service/txe_service.js +0 -572
- package/dest/util/txe_world_state_db.d.ts +0 -14
- package/dest/util/txe_world_state_db.d.ts.map +0 -1
- package/dest/util/txe_world_state_db.js +0 -27
- package/src/node/txe_node.ts +0 -725
- package/src/oracle/txe_oracle.ts +0 -1241
- package/src/txe_service/txe_service.ts +0 -749
- package/src/util/txe_world_state_db.ts +0 -38
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP } from '@aztec/constants';
|
|
2
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
3
|
+
import { NativeWorldStateService } from '@aztec/world-state/native';
|
|
4
|
+
export class TXESynchronizer {
|
|
5
|
+
nativeWorldStateService;
|
|
6
|
+
// This works when set to 1 as well.
|
|
7
|
+
blockNumber;
|
|
8
|
+
constructor(nativeWorldStateService){
|
|
9
|
+
this.nativeWorldStateService = nativeWorldStateService;
|
|
10
|
+
this.blockNumber = 0;
|
|
11
|
+
}
|
|
12
|
+
static async create() {
|
|
13
|
+
const nativeWorldStateService = await NativeWorldStateService.tmp();
|
|
14
|
+
return new this(nativeWorldStateService);
|
|
15
|
+
}
|
|
16
|
+
async handleL2Block(block) {
|
|
17
|
+
await this.nativeWorldStateService.handleL2BlockAndMessages(block, Array(NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP).fill(0).map(Fr.zero));
|
|
18
|
+
this.blockNumber = block.header.globalVariables.blockNumber;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Forces an immediate sync to an optionally provided minimum block number
|
|
22
|
+
* @param targetBlockNumber - The target block number that we must sync to. Will download unproven blocks if needed to reach it.
|
|
23
|
+
* @param skipThrowIfTargetNotReached - Whether to skip throwing if the target block number is not reached.
|
|
24
|
+
* @returns A promise that resolves with the block number the world state was synced to
|
|
25
|
+
*/ syncImmediate(_minBlockNumber, _skipThrowIfTargetNotReached) {
|
|
26
|
+
return Promise.resolve(this.blockNumber);
|
|
27
|
+
}
|
|
28
|
+
/** Returns an instance of MerkleTreeAdminOperations that will not include uncommitted data. */ getCommitted() {
|
|
29
|
+
return this.nativeWorldStateService.getCommitted();
|
|
30
|
+
}
|
|
31
|
+
/** Forks the world state at the given block number, defaulting to the latest one. */ fork(block) {
|
|
32
|
+
return this.nativeWorldStateService.fork(block);
|
|
33
|
+
}
|
|
34
|
+
/** Gets a handle that allows reading the state as it was at the given block number. */ getSnapshot(blockNumber) {
|
|
35
|
+
return this.nativeWorldStateService.getSnapshot(blockNumber);
|
|
36
|
+
}
|
|
37
|
+
/** Backups the db to the target path. */ backupTo(dstPath, compact) {
|
|
38
|
+
return this.nativeWorldStateService.backupTo(dstPath, compact);
|
|
39
|
+
}
|
|
40
|
+
start() {
|
|
41
|
+
throw new Error('TXE Synchronizer does not implement "start"');
|
|
42
|
+
}
|
|
43
|
+
status() {
|
|
44
|
+
throw new Error('TXE Synchronizer does not implement "status"');
|
|
45
|
+
}
|
|
46
|
+
stop() {
|
|
47
|
+
throw new Error('TXE Synchronizer does not implement "stop"');
|
|
48
|
+
}
|
|
49
|
+
stopSync() {
|
|
50
|
+
throw new Error('TXE Synchronizer does not implement "stopSync"');
|
|
51
|
+
}
|
|
52
|
+
resumeSync() {
|
|
53
|
+
throw new Error('TXE Synchronizer does not implement "resumeSync"');
|
|
54
|
+
}
|
|
55
|
+
clear() {
|
|
56
|
+
throw new Error('TXE Synchronizer does not implement "clear"');
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
2
|
+
import { type Logger } from '@aztec/foundation/log';
|
|
3
|
+
import { KeyStore } from '@aztec/key-store';
|
|
4
|
+
import type { ProtocolContract } from '@aztec/protocol-contracts';
|
|
5
|
+
import { AddressDataProvider, PXEOracleInterface } from '@aztec/pxe/server';
|
|
6
|
+
import { type IPrivateExecutionOracle, type IUtilityExecutionOracle } from '@aztec/pxe/simulator';
|
|
7
|
+
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
8
|
+
import { PrivateContextInputs } from '@aztec/stdlib/kernel';
|
|
9
|
+
import type { UInt32 } from '@aztec/stdlib/types';
|
|
10
|
+
import type { IAvmExecutionOracle, ITxeExecutionOracle } from './oracle/interfaces.js';
|
|
11
|
+
import { RPCTranslator } from './rpc_translator.js';
|
|
12
|
+
import { TXEStateMachine } from './state_machine/index.js';
|
|
13
|
+
import type { ForeignCallArgs, ForeignCallResult } from './util/encoding.js';
|
|
14
|
+
import { TXEAccountDataProvider } from './util/txe_account_data_provider.js';
|
|
15
|
+
import { TXEContractDataProvider } from './util/txe_contract_data_provider.js';
|
|
16
|
+
type MethodNames<T> = {
|
|
17
|
+
[K in keyof T]: T[K] extends (...args: any[]) => any ? K : never;
|
|
18
|
+
}[keyof T];
|
|
19
|
+
/**
|
|
20
|
+
* The name of an oracle function that TXE supports, which are a combination of PXE oracles, non-transpiled AVM opcodes,
|
|
21
|
+
* and custom TXE oracles.
|
|
22
|
+
*/
|
|
23
|
+
export type TXEOracleFunctionName = Exclude<MethodNames<RPCTranslator>, 'constructor' | 'handlerAsMisc' | 'handlerAsUtility' | 'handlerAsPrivate' | 'handlerAsAvm' | 'handlerAsTxe'>;
|
|
24
|
+
export interface TXESessionStateHandler {
|
|
25
|
+
enterTopLevelState(): Promise<void>;
|
|
26
|
+
enterPublicState(contractAddress?: AztecAddress): Promise<void>;
|
|
27
|
+
enterPrivateState(contractAddress?: AztecAddress, anchorBlockNumber?: UInt32): Promise<PrivateContextInputs>;
|
|
28
|
+
enterUtilityState(contractAddress?: AztecAddress): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A `TXESession` corresponds to a Noir `#[test]` function, and handles all of its oracle calls, stores test-specific
|
|
32
|
+
* state, etc., independent of all other tests running in parallel.
|
|
33
|
+
*/
|
|
34
|
+
export declare class TXESession implements TXESessionStateHandler {
|
|
35
|
+
private logger;
|
|
36
|
+
private stateMachine;
|
|
37
|
+
private oracleHandler;
|
|
38
|
+
private contractDataProvider;
|
|
39
|
+
private keyStore;
|
|
40
|
+
private addressDataProvider;
|
|
41
|
+
private accountDataProvider;
|
|
42
|
+
private chainId;
|
|
43
|
+
private version;
|
|
44
|
+
private nextBlockTimestamp;
|
|
45
|
+
private pxeOracleInterface;
|
|
46
|
+
private state;
|
|
47
|
+
private authwits;
|
|
48
|
+
constructor(logger: Logger, stateMachine: TXEStateMachine, oracleHandler: IUtilityExecutionOracle | IPrivateExecutionOracle | IAvmExecutionOracle | ITxeExecutionOracle, contractDataProvider: TXEContractDataProvider, keyStore: KeyStore, addressDataProvider: AddressDataProvider, accountDataProvider: TXEAccountDataProvider, chainId: Fr, version: Fr, nextBlockTimestamp: bigint, pxeOracleInterface: PXEOracleInterface);
|
|
49
|
+
static init(protocolContracts: ProtocolContract[]): Promise<TXESession>;
|
|
50
|
+
/**
|
|
51
|
+
* Processes an oracle function invoked by the Noir test associated to this session.
|
|
52
|
+
* @param functionName The name of the oracle.
|
|
53
|
+
* @param inputs The inputs of the oracle.
|
|
54
|
+
* @returns The oracle return values.
|
|
55
|
+
*/
|
|
56
|
+
processFunction(functionName: TXEOracleFunctionName, inputs: ForeignCallArgs): Promise<ForeignCallResult>;
|
|
57
|
+
enterTopLevelState(): Promise<void>;
|
|
58
|
+
enterPrivateState(contractAddress?: AztecAddress, anchorBlockNumber?: UInt32): Promise<PrivateContextInputs>;
|
|
59
|
+
enterPublicState(contractAddress?: AztecAddress): Promise<void>;
|
|
60
|
+
enterUtilityState(contractAddress?: AztecAddress): Promise<void>;
|
|
61
|
+
private exitTopLevelState;
|
|
62
|
+
private exitPrivateState;
|
|
63
|
+
private exitPublicState;
|
|
64
|
+
private exitUtilityContext;
|
|
65
|
+
}
|
|
66
|
+
export {};
|
|
67
|
+
//# sourceMappingURL=txe_session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"txe_session.d.ts","sour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EvD;;;;;OAKG;IACH,eAAe,CAAC,YAAY,EAAE,qBAAqB,EAAE,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAyBnG,kBAAkB;IAuClB,iBAAiB,CACrB,eAAe,GAAE,YAA8B,EAC/C,iBAAiB,CAAC,EAAE,MAAM,GACzB,OAAO,CAAC,oBAAoB,CAAC;IAmD1B,gBAAgB,CAAC,eAAe,CAAC,EAAE,YAAY;IAwB/C,iBAAiB,CAAC,eAAe,GAAE,YAA8B;IAgBvE,OAAO,CAAC,iBAAiB;YAiBX,gBAAgB;YAoChB,eAAe;IAS7B,OAAO,CAAC,kBAAkB;CAK3B"}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
2
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
3
|
+
import { KeyStore } from '@aztec/key-store';
|
|
4
|
+
import { openTmpStore } from '@aztec/kv-store/lmdb-v2';
|
|
5
|
+
import { AddressDataProvider, CapsuleDataProvider, NoteDataProvider, PXEOracleInterface, PrivateEventDataProvider, TaggingDataProvider } from '@aztec/pxe/server';
|
|
6
|
+
import { ExecutionNoteCache, ExecutionTaggingIndexCache, HashedValuesCache, PrivateExecutionOracle, UtilityExecutionOracle } from '@aztec/pxe/simulator';
|
|
7
|
+
import { FunctionSelector } from '@aztec/stdlib/abi';
|
|
8
|
+
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
9
|
+
import { Body, L2Block } from '@aztec/stdlib/block';
|
|
10
|
+
import { GasSettings } from '@aztec/stdlib/gas';
|
|
11
|
+
import { makeAppendOnlyTreeSnapshot, makeGlobalVariables } from '@aztec/stdlib/testing';
|
|
12
|
+
import { CallContext, TxContext } from '@aztec/stdlib/tx';
|
|
13
|
+
import { z } from 'zod';
|
|
14
|
+
import { TXEOraclePublicContext } from './oracle/txe_oracle_public_context.js';
|
|
15
|
+
import { TXEOracleTopLevelContext } from './oracle/txe_oracle_top_level_context.js';
|
|
16
|
+
import { RPCTranslator } from './rpc_translator.js';
|
|
17
|
+
import { TXEStateMachine } from './state_machine/index.js';
|
|
18
|
+
import { TXEAccountDataProvider } from './util/txe_account_data_provider.js';
|
|
19
|
+
import { TXEContractDataProvider } from './util/txe_contract_data_provider.js';
|
|
20
|
+
import { getSingleTxBlockRequestHash, insertTxEffectIntoWorldTrees, makeTXEBlockHeader } from './utils/block_creation.js';
|
|
21
|
+
import { makeTxEffect } from './utils/tx_effect_creation.js';
|
|
22
|
+
const DEFAULT_ADDRESS = AztecAddress.fromNumber(42);
|
|
23
|
+
/**
|
|
24
|
+
* A `TXESession` corresponds to a Noir `#[test]` function, and handles all of its oracle calls, stores test-specific
|
|
25
|
+
* state, etc., independent of all other tests running in parallel.
|
|
26
|
+
*/ export class TXESession {
|
|
27
|
+
logger;
|
|
28
|
+
stateMachine;
|
|
29
|
+
oracleHandler;
|
|
30
|
+
contractDataProvider;
|
|
31
|
+
keyStore;
|
|
32
|
+
addressDataProvider;
|
|
33
|
+
accountDataProvider;
|
|
34
|
+
chainId;
|
|
35
|
+
version;
|
|
36
|
+
nextBlockTimestamp;
|
|
37
|
+
pxeOracleInterface;
|
|
38
|
+
state;
|
|
39
|
+
authwits;
|
|
40
|
+
constructor(logger, stateMachine, oracleHandler, contractDataProvider, keyStore, addressDataProvider, accountDataProvider, chainId, version, nextBlockTimestamp, pxeOracleInterface){
|
|
41
|
+
this.logger = logger;
|
|
42
|
+
this.stateMachine = stateMachine;
|
|
43
|
+
this.oracleHandler = oracleHandler;
|
|
44
|
+
this.contractDataProvider = contractDataProvider;
|
|
45
|
+
this.keyStore = keyStore;
|
|
46
|
+
this.addressDataProvider = addressDataProvider;
|
|
47
|
+
this.accountDataProvider = accountDataProvider;
|
|
48
|
+
this.chainId = chainId;
|
|
49
|
+
this.version = version;
|
|
50
|
+
this.nextBlockTimestamp = nextBlockTimestamp;
|
|
51
|
+
this.pxeOracleInterface = pxeOracleInterface;
|
|
52
|
+
this.state = {
|
|
53
|
+
name: 'TOP_LEVEL'
|
|
54
|
+
};
|
|
55
|
+
this.authwits = new Map();
|
|
56
|
+
}
|
|
57
|
+
static async init(protocolContracts) {
|
|
58
|
+
const store = await openTmpStore('txe-session');
|
|
59
|
+
const addressDataProvider = new AddressDataProvider(store);
|
|
60
|
+
const privateEventDataProvider = new PrivateEventDataProvider(store);
|
|
61
|
+
const contractDataProvider = new TXEContractDataProvider(store);
|
|
62
|
+
const noteDataProvider = await NoteDataProvider.create(store);
|
|
63
|
+
const taggingDataProvider = new TaggingDataProvider(store);
|
|
64
|
+
const capsuleDataProvider = new CapsuleDataProvider(store);
|
|
65
|
+
const keyStore = new KeyStore(store);
|
|
66
|
+
const accountDataProvider = new TXEAccountDataProvider(store);
|
|
67
|
+
// Register protocol contracts.
|
|
68
|
+
for (const { contractClass, instance, artifact } of protocolContracts){
|
|
69
|
+
await contractDataProvider.addContractArtifact(contractClass.id, artifact);
|
|
70
|
+
await contractDataProvider.addContractInstance(instance);
|
|
71
|
+
}
|
|
72
|
+
const stateMachine = await TXEStateMachine.create(store);
|
|
73
|
+
const nextBlockTimestamp = BigInt(Math.floor(new Date().getTime() / 1000));
|
|
74
|
+
const version = new Fr(await stateMachine.node.getVersion());
|
|
75
|
+
const chainId = new Fr(await stateMachine.node.getChainId());
|
|
76
|
+
const pxeOracleInterface = new PXEOracleInterface(stateMachine.node, keyStore, contractDataProvider, noteDataProvider, capsuleDataProvider, stateMachine.syncDataProvider, taggingDataProvider, addressDataProvider, privateEventDataProvider);
|
|
77
|
+
const topLevelOracleHandler = new TXEOracleTopLevelContext(stateMachine, contractDataProvider, keyStore, addressDataProvider, accountDataProvider, pxeOracleInterface, nextBlockTimestamp, version, chainId, new Map());
|
|
78
|
+
await topLevelOracleHandler.txeAdvanceBlocksBy(1);
|
|
79
|
+
return new TXESession(createLogger('txe:session'), stateMachine, topLevelOracleHandler, contractDataProvider, keyStore, addressDataProvider, accountDataProvider, version, chainId, nextBlockTimestamp, pxeOracleInterface);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Processes an oracle function invoked by the Noir test associated to this session.
|
|
83
|
+
* @param functionName The name of the oracle.
|
|
84
|
+
* @param inputs The inputs of the oracle.
|
|
85
|
+
* @returns The oracle return values.
|
|
86
|
+
*/ processFunction(functionName, inputs) {
|
|
87
|
+
try {
|
|
88
|
+
const translator = new RPCTranslator(this, this.oracleHandler);
|
|
89
|
+
// We perform a runtime validation to check that the function name corresponds to a real oracle handler.
|
|
90
|
+
const validatedFunctionName = z.string().refine((fn)=>typeof translator[fn] === 'function' && !fn.startsWith('handlerAs') && fn !== 'constructor').parse(functionName);
|
|
91
|
+
return translator[validatedFunctionName](...inputs);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
if (error instanceof z.ZodError) {
|
|
94
|
+
throw new Error(`${functionName} does not correspond to any oracle handler available on RPCTranslator`);
|
|
95
|
+
} else if (error instanceof Error) {
|
|
96
|
+
throw new Error(`Execution error while processing function ${functionName} in state ${this.state.name}: ${error.message}`);
|
|
97
|
+
} else {
|
|
98
|
+
throw new Error(`Unknown execution error while processing function ${functionName} in state ${this.state.name}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async enterTopLevelState() {
|
|
103
|
+
switch(this.state.name){
|
|
104
|
+
case 'PRIVATE':
|
|
105
|
+
{
|
|
106
|
+
await this.exitPrivateState();
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
case 'PUBLIC':
|
|
110
|
+
{
|
|
111
|
+
await this.exitPublicState();
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case 'UTILITY':
|
|
115
|
+
{
|
|
116
|
+
this.exitUtilityContext();
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
case 'TOP_LEVEL':
|
|
120
|
+
{
|
|
121
|
+
throw new Error(`Expected to be in state other than TOP_LEVEL`);
|
|
122
|
+
}
|
|
123
|
+
default:
|
|
124
|
+
{
|
|
125
|
+
this.state;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
this.oracleHandler = new TXEOracleTopLevelContext(this.stateMachine, this.contractDataProvider, this.keyStore, this.addressDataProvider, this.accountDataProvider, this.pxeOracleInterface, this.nextBlockTimestamp, this.version, this.chainId, this.authwits);
|
|
129
|
+
this.state = {
|
|
130
|
+
name: 'TOP_LEVEL'
|
|
131
|
+
};
|
|
132
|
+
this.logger.debug(`Entered state ${this.state.name}`);
|
|
133
|
+
}
|
|
134
|
+
async enterPrivateState(contractAddress = DEFAULT_ADDRESS, anchorBlockNumber) {
|
|
135
|
+
this.exitTopLevelState();
|
|
136
|
+
// There is no automatic message discovery and contract-driven syncing process in inlined private or utility
|
|
137
|
+
// contexts, which means that known nullifiers are also not searched for, since it is during the tagging sync that
|
|
138
|
+
// we perform this. We therefore search for known nullifiers now, as otherwise notes that were nullified would not
|
|
139
|
+
// be removed from the database.
|
|
140
|
+
// TODO(#12553): make the synchronizer sync here instead and remove this
|
|
141
|
+
await this.pxeOracleInterface.syncNoteNullifiers(contractAddress);
|
|
142
|
+
// Private execution has two associated block numbers: the anchor block (i.e. the historical block that is used to
|
|
143
|
+
// build the proof), and the *next* block, i.e. the one we'll create once the execution ends, and which will contain
|
|
144
|
+
// a single transaction with the effects of what was done in the test.
|
|
145
|
+
const anchorBlock = await this.stateMachine.node.getBlockHeader(anchorBlockNumber ?? 'latest');
|
|
146
|
+
const latestBlock = await this.stateMachine.node.getBlockHeader('latest');
|
|
147
|
+
const nextBlockGlobalVariables = makeGlobalVariables(undefined, {
|
|
148
|
+
blockNumber: latestBlock.globalVariables.blockNumber + 1,
|
|
149
|
+
timestamp: this.nextBlockTimestamp,
|
|
150
|
+
version: this.version,
|
|
151
|
+
chainId: this.chainId
|
|
152
|
+
});
|
|
153
|
+
const txRequestHash = getSingleTxBlockRequestHash(nextBlockGlobalVariables.blockNumber);
|
|
154
|
+
const noteCache = new ExecutionNoteCache(txRequestHash);
|
|
155
|
+
const taggingIndexCache = new ExecutionTaggingIndexCache();
|
|
156
|
+
this.oracleHandler = new PrivateExecutionOracle(Fr.ZERO, new TxContext(this.chainId, this.version, GasSettings.empty()), new CallContext(AztecAddress.ZERO, contractAddress, FunctionSelector.empty(), false), anchorBlock, [], [], new HashedValuesCache(), noteCache, taggingIndexCache, this.pxeOracleInterface);
|
|
157
|
+
// We store the note and tagging index caches fed into the PrivateExecutionOracle (along with some other auxiliary
|
|
158
|
+
// data) in order to refer to it later, mimicking the way this object is used by the ContractFunctionSimulator. The
|
|
159
|
+
// difference resides in that the simulator has all information needed in order to run the simulation, while ours
|
|
160
|
+
// will be ongoing as the different oracles will be invoked from the Noir test, until eventually the private
|
|
161
|
+
// execution finishes.
|
|
162
|
+
this.state = {
|
|
163
|
+
name: 'PRIVATE',
|
|
164
|
+
nextBlockGlobalVariables,
|
|
165
|
+
txRequestHash,
|
|
166
|
+
noteCache,
|
|
167
|
+
taggingIndexCache
|
|
168
|
+
};
|
|
169
|
+
this.logger.debug(`Entered state ${this.state.name}`);
|
|
170
|
+
return this.oracleHandler.getPrivateContextInputs();
|
|
171
|
+
}
|
|
172
|
+
async enterPublicState(contractAddress) {
|
|
173
|
+
this.exitTopLevelState();
|
|
174
|
+
// The PublicContext will create a block with a single transaction in it, containing the effects of what was done in
|
|
175
|
+
// the test. The block therefore gets the *next* block number and timestamp.
|
|
176
|
+
const latestBlockNumber = (await this.stateMachine.node.getBlockHeader('latest')).globalVariables.blockNumber;
|
|
177
|
+
const globalVariables = makeGlobalVariables(undefined, {
|
|
178
|
+
blockNumber: latestBlockNumber + 1,
|
|
179
|
+
timestamp: this.nextBlockTimestamp,
|
|
180
|
+
version: this.version,
|
|
181
|
+
chainId: this.chainId
|
|
182
|
+
});
|
|
183
|
+
this.oracleHandler = new TXEOraclePublicContext(contractAddress ?? DEFAULT_ADDRESS, await this.stateMachine.synchronizer.nativeWorldStateService.fork(), getSingleTxBlockRequestHash(globalVariables.blockNumber), globalVariables);
|
|
184
|
+
this.state = {
|
|
185
|
+
name: 'PUBLIC'
|
|
186
|
+
};
|
|
187
|
+
this.logger.debug(`Entered state ${this.state.name}`);
|
|
188
|
+
}
|
|
189
|
+
async enterUtilityState(contractAddress = DEFAULT_ADDRESS) {
|
|
190
|
+
this.exitTopLevelState();
|
|
191
|
+
// There is no automatic message discovery and contract-driven syncing process in inlined private or utility
|
|
192
|
+
// contexts, which means that known nullifiers are also not searched for, since it is during the tagging sync that
|
|
193
|
+
// we perform this. We therefore search for known nullifiers now, as otherwise notes that were nullified would not
|
|
194
|
+
// be removed from the database.
|
|
195
|
+
// TODO(#12553): make the synchronizer sync here instead and remove this
|
|
196
|
+
await this.pxeOracleInterface.syncNoteNullifiers(contractAddress);
|
|
197
|
+
this.oracleHandler = new UtilityExecutionOracle(contractAddress, [], [], this.pxeOracleInterface);
|
|
198
|
+
this.state = {
|
|
199
|
+
name: 'UTILITY'
|
|
200
|
+
};
|
|
201
|
+
this.logger.debug(`Entered state ${this.state.name}`);
|
|
202
|
+
}
|
|
203
|
+
exitTopLevelState() {
|
|
204
|
+
if (this.state.name != 'TOP_LEVEL') {
|
|
205
|
+
throw new Error(`Expected to be in state 'TOP_LEVEL', but got '${this.state.name}' instead`);
|
|
206
|
+
}
|
|
207
|
+
// Note that while all public and private contexts do is build a single block that we then process when exiting
|
|
208
|
+
// those, the top level context performs a large number of actions not captured in the following 'close' call. Among
|
|
209
|
+
// others, it will create empty blocks (via `txeAdvanceBlocksBy` and `deploy`), create blocks with transactions via
|
|
210
|
+
// `txePrivateCallNewFlow` and `txePublicCallNewFlow`, add accounts to PXE via `txeAddAccount`, etc. This is a
|
|
211
|
+
// slight inconsistency in the working model of this class, but is not too bad.
|
|
212
|
+
// TODO: it's quite unfortunate that we need to capture the authwits created to later pass them again when the top
|
|
213
|
+
// level context is re-created. This is because authwits create a temporary utility context that'd otherwise reset
|
|
214
|
+
// the authwits if not persisted, so we'd not be able to pass more than one per execution.
|
|
215
|
+
// Ideally authwits would be passed alongside a contract call instead of pre-seeded.
|
|
216
|
+
[this.nextBlockTimestamp, this.authwits] = this.oracleHandler.close();
|
|
217
|
+
}
|
|
218
|
+
async exitPrivateState() {
|
|
219
|
+
if (this.state.name != 'PRIVATE') {
|
|
220
|
+
throw new Error(`Expected to be in state 'PRIVATE', but got '${this.state.name}' instead`);
|
|
221
|
+
}
|
|
222
|
+
this.logger.debug('Exiting Private state, building block with collected side effects', {
|
|
223
|
+
blockNumber: this.state.nextBlockGlobalVariables.blockNumber
|
|
224
|
+
});
|
|
225
|
+
// We rely on the note cache to determine the effects of the transaction. This is incomplete as it doesn't private
|
|
226
|
+
// logs (other effects like enqueued public calls don't need to be considered since those are not allowed).
|
|
227
|
+
const txEffect = await makeTxEffect(this.state.noteCache, this.state.txRequestHash, this.state.nextBlockGlobalVariables.blockNumber);
|
|
228
|
+
// We build a block holding just this transaction
|
|
229
|
+
const forkedWorldTrees = await this.stateMachine.synchronizer.nativeWorldStateService.fork();
|
|
230
|
+
await insertTxEffectIntoWorldTrees(txEffect, forkedWorldTrees);
|
|
231
|
+
const block = new L2Block(makeAppendOnlyTreeSnapshot(), await makeTXEBlockHeader(forkedWorldTrees, this.state.nextBlockGlobalVariables), new Body([
|
|
232
|
+
txEffect
|
|
233
|
+
]));
|
|
234
|
+
await this.stateMachine.handleL2Block(block);
|
|
235
|
+
await forkedWorldTrees.close();
|
|
236
|
+
this.logger.debug('Exited PublicContext with built block', {
|
|
237
|
+
blockNumber: block.number,
|
|
238
|
+
txEffects: block.body.txEffects
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
async exitPublicState() {
|
|
242
|
+
if (this.state.name != 'PUBLIC') {
|
|
243
|
+
throw new Error(`Expected to be in state 'PUBLIC', but got '${this.state.name}' instead`);
|
|
244
|
+
}
|
|
245
|
+
const block = await this.oracleHandler.close();
|
|
246
|
+
await this.stateMachine.handleL2Block(block);
|
|
247
|
+
}
|
|
248
|
+
exitUtilityContext() {
|
|
249
|
+
if (this.state.name != 'UTILITY') {
|
|
250
|
+
throw new Error(`Expected to be in state 'UTILITY', but got '${this.state.name}' instead`);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
package/dest/util/encoding.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
1
|
+
import type { EthAddress } from '@aztec/foundation/eth-address';
|
|
3
2
|
import { Fr } from '@aztec/foundation/fields';
|
|
4
3
|
import { type ContractArtifact } from '@aztec/stdlib/abi';
|
|
5
4
|
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
@@ -17,21 +16,40 @@ export declare function fromArray(obj: ForeignCallArray): Fr[];
|
|
|
17
16
|
/**
|
|
18
17
|
* Converts an array of Noir unsigned integers to a single tightly-packed buffer.
|
|
19
18
|
* @param uintBitSize If it's an array of Noir u8's, put `8`, etc.
|
|
20
|
-
* @returns
|
|
19
|
+
* @returns A buffer where each byte is correctly represented as a single byte in the buffer.
|
|
21
20
|
*/
|
|
22
21
|
export declare function fromUintArray(obj: ForeignCallArray, uintBitSize: number): Buffer;
|
|
23
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Converts a Noir BoundedVec of unsigned integers into a Buffer. Note that BoundedVecs are structs, and therefore
|
|
24
|
+
* translated as two separate ForeignCallArray and ForeignCallSingle values (an array and a single field).
|
|
25
|
+
*
|
|
26
|
+
* @param storage The array with the BoundedVec's storage (i.e. BoundedVec::storage())
|
|
27
|
+
* @param length The length of the BoundedVec (i.e. BoundedVec::len())
|
|
28
|
+
* @param uintBitSize If it's an array of Noir u8's, put `8`, etc.
|
|
29
|
+
* @returns A buffer containing the unsigned integers tightly packed
|
|
30
|
+
*/
|
|
31
|
+
export declare function fromUintBoundedVec(storage: ForeignCallArray, length: ForeignCallSingle, uintBitSize: number): Buffer;
|
|
32
|
+
export declare function toSingle(value: AztecAddress | EthAddress | Fr | Buffer | boolean | number | bigint): ForeignCallSingle;
|
|
24
33
|
export declare function toArray(objs: Fr[]): ForeignCallArray;
|
|
34
|
+
export declare function toSingleOrArray(value: Fr | Fr[]): string | string[];
|
|
25
35
|
export declare function bufferToU8Array(buffer: Buffer): ForeignCallArray;
|
|
26
36
|
/**
|
|
27
37
|
* Converts a ForeignCallArray into a tuple which represents a nr BoundedVec.
|
|
28
38
|
* If the input array is shorter than the maxLen, it pads the result with zeros,
|
|
29
39
|
* so that nr can correctly coerce this result into a BoundedVec.
|
|
30
|
-
* @param array
|
|
40
|
+
* @param bVecStorage - The array underlying the BoundedVec.
|
|
31
41
|
* @param maxLen - the max length of the BoundedVec.
|
|
32
42
|
* @returns a tuple representing a BoundedVec.
|
|
33
43
|
*/
|
|
34
|
-
export declare function arrayToBoundedVec(
|
|
44
|
+
export declare function arrayToBoundedVec(bVecStorage: ForeignCallArray, maxLen: number): [ForeignCallArray, ForeignCallSingle];
|
|
45
|
+
/**
|
|
46
|
+
* Converts an array of arrays representing Noir BoundedVec of nested arrays into its Noir serialized form.
|
|
47
|
+
* @param bVecStorage - The array underlying the BoundedVec.
|
|
48
|
+
* @param maxLen - The max length of the BoundedVec (max num of the nested arrays in the BoundedVec).
|
|
49
|
+
* @param nestedArrayLength - The length of the nested arrays (each nested array has to have the same length).
|
|
50
|
+
* @returns Serialized BoundedVec following Noir intrinsic serialization.
|
|
51
|
+
*/
|
|
52
|
+
export declare function arrayOfArraysToBoundedVecOfArrays(bVecStorage: ForeignCallArray[], maxLen: number, nestedArrayLength: number): [ForeignCallArray, ForeignCallSingle];
|
|
35
53
|
export declare function toForeignCallResult(obj: (ForeignCallSingle | ForeignCallArray)[]): {
|
|
36
54
|
values: (string | ForeignCallArray)[];
|
|
37
55
|
};
|
|
@@ -59,7 +77,7 @@ export declare const ForeignCallArgsSchema: z.ZodArray<z.ZodUnion<[z.ZodString,
|
|
|
59
77
|
masterIncomingViewingPublicKey: string;
|
|
60
78
|
masterOutgoingViewingPublicKey: string;
|
|
61
79
|
masterTaggingPublicKey: string;
|
|
62
|
-
}>, import("@aztec/
|
|
80
|
+
}>, import("@aztec/stdlib/keys").PublicKeys, {
|
|
63
81
|
masterNullifierPublicKey: string;
|
|
64
82
|
masterIncomingViewingPublicKey: string;
|
|
65
83
|
masterOutgoingViewingPublicKey: string;
|
|
@@ -72,7 +90,7 @@ export declare const ForeignCallArgsSchema: z.ZodArray<z.ZodUnion<[z.ZodString,
|
|
|
72
90
|
currentContractClassId: Fr;
|
|
73
91
|
originalContractClassId: Fr;
|
|
74
92
|
initializationHash: Fr;
|
|
75
|
-
publicKeys: import("@aztec/
|
|
93
|
+
publicKeys: import("@aztec/stdlib/keys").PublicKeys;
|
|
76
94
|
}, {
|
|
77
95
|
version: 1;
|
|
78
96
|
publicKeys: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encoding.d.ts","sourceRoot":"","sources":["../../src/util/encoding.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"encoding.d.ts","sourceRoot":"","sources":["../../src/util/encoding.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAChE,OAAO,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAE9C,OAAO,EAAE,KAAK,gBAAgB,EAA0B,MAAM,mBAAmB,CAAC;AAClF,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAC3D,OAAO,EAAE,KAAK,2BAA2B,EAAqC,MAAM,wBAAwB,CAAC;AAE7G,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAC;AAEvC,MAAM,MAAM,gBAAgB,GAAG,MAAM,EAAE,CAAC;AAExC,MAAM,MAAM,eAAe,GAAG,CAAC,iBAAiB,GAAG,gBAAgB,GAAG,gBAAgB,GAAG,2BAA2B,CAAC,EAAE,CAAC;AAExH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,MAAM,EAAE,CAAC,iBAAi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;AAElD,eAAO,MAAM,sBAAsB,iCAAsB,CAAC;AAE1D,eAAO,MAAM,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAEjC,CAAC;AAEF,eAAO,MAAM,uBAAuB;;;;;;EAElC,CAAC"}
|
package/dest/util/encoding.js
CHANGED
|
@@ -16,7 +16,7 @@ export function fromArray(obj) {
|
|
|
16
16
|
/**
|
|
17
17
|
* Converts an array of Noir unsigned integers to a single tightly-packed buffer.
|
|
18
18
|
* @param uintBitSize If it's an array of Noir u8's, put `8`, etc.
|
|
19
|
-
* @returns
|
|
19
|
+
* @returns A buffer where each byte is correctly represented as a single byte in the buffer.
|
|
20
20
|
*/ export function fromUintArray(obj, uintBitSize) {
|
|
21
21
|
if (uintBitSize % 8 !== 0) {
|
|
22
22
|
throw new Error(`u${uintBitSize} is not a supported type in Noir`);
|
|
@@ -24,12 +24,43 @@ export function fromArray(obj) {
|
|
|
24
24
|
const uintByteSize = uintBitSize / 8;
|
|
25
25
|
return Buffer.concat(obj.map((str)=>hexToBuffer(str).slice(-uintByteSize)));
|
|
26
26
|
}
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Converts a Noir BoundedVec of unsigned integers into a Buffer. Note that BoundedVecs are structs, and therefore
|
|
29
|
+
* translated as two separate ForeignCallArray and ForeignCallSingle values (an array and a single field).
|
|
30
|
+
*
|
|
31
|
+
* @param storage The array with the BoundedVec's storage (i.e. BoundedVec::storage())
|
|
32
|
+
* @param length The length of the BoundedVec (i.e. BoundedVec::len())
|
|
33
|
+
* @param uintBitSize If it's an array of Noir u8's, put `8`, etc.
|
|
34
|
+
* @returns A buffer containing the unsigned integers tightly packed
|
|
35
|
+
*/ export function fromUintBoundedVec(storage, length, uintBitSize) {
|
|
36
|
+
if (uintBitSize % 8 !== 0) {
|
|
37
|
+
throw new Error(`u${uintBitSize} is not a supported type in Noir`);
|
|
38
|
+
}
|
|
39
|
+
const uintByteSize = uintBitSize / 8;
|
|
40
|
+
const boundedStorage = storage.slice(0, fromSingle(length).toNumber());
|
|
41
|
+
return Buffer.concat(boundedStorage.map((str)=>hexToBuffer(str).slice(-uintByteSize)));
|
|
42
|
+
}
|
|
43
|
+
// Just like toACVMField in yarn-project/simulator/src/private/acvm/serialize.ts but returns a ForeignCallSingle
|
|
44
|
+
// instead of an ACVMField.
|
|
45
|
+
export function toSingle(value) {
|
|
46
|
+
let valueAsField;
|
|
47
|
+
if (Buffer.isBuffer(value)) {
|
|
48
|
+
valueAsField = Fr.fromBuffer(value);
|
|
49
|
+
} else if (typeof value === 'boolean' || typeof value === 'number' || typeof value === 'bigint') {
|
|
50
|
+
valueAsField = new Fr(value);
|
|
51
|
+
} else if (typeof value === 'string') {
|
|
52
|
+
valueAsField = Fr.fromHexString(value);
|
|
53
|
+
} else {
|
|
54
|
+
valueAsField = value;
|
|
55
|
+
}
|
|
56
|
+
return valueAsField.toString().slice(2);
|
|
29
57
|
}
|
|
30
58
|
export function toArray(objs) {
|
|
31
59
|
return objs.map((obj)=>obj.toString());
|
|
32
60
|
}
|
|
61
|
+
export function toSingleOrArray(value) {
|
|
62
|
+
return Array.isArray(value) ? value.map(toSingle) : toSingle(value);
|
|
63
|
+
}
|
|
33
64
|
export function bufferToU8Array(buffer) {
|
|
34
65
|
return toArray(Array.from(buffer).map((byte)=>new Fr(byte)));
|
|
35
66
|
}
|
|
@@ -37,24 +68,48 @@ export function bufferToU8Array(buffer) {
|
|
|
37
68
|
* Converts a ForeignCallArray into a tuple which represents a nr BoundedVec.
|
|
38
69
|
* If the input array is shorter than the maxLen, it pads the result with zeros,
|
|
39
70
|
* so that nr can correctly coerce this result into a BoundedVec.
|
|
40
|
-
* @param array
|
|
71
|
+
* @param bVecStorage - The array underlying the BoundedVec.
|
|
41
72
|
* @param maxLen - the max length of the BoundedVec.
|
|
42
73
|
* @returns a tuple representing a BoundedVec.
|
|
43
|
-
*/ export function arrayToBoundedVec(
|
|
44
|
-
if (
|
|
45
|
-
throw new Error(`Array of length ${
|
|
74
|
+
*/ export function arrayToBoundedVec(bVecStorage, maxLen) {
|
|
75
|
+
if (bVecStorage.length > maxLen) {
|
|
76
|
+
throw new Error(`Array of length ${bVecStorage.length} larger than maxLen ${maxLen}`);
|
|
46
77
|
}
|
|
47
|
-
const lengthDiff = maxLen -
|
|
78
|
+
const lengthDiff = maxLen - bVecStorage.length;
|
|
48
79
|
// We pad the array to the maxLen of the BoundedVec.
|
|
49
80
|
const zeroPaddingArray = toArray(Array(lengthDiff).fill(new Fr(0)));
|
|
50
81
|
// These variable names match with the BoundedVec members in nr:
|
|
51
|
-
const storage =
|
|
52
|
-
const len = toSingle(new Fr(
|
|
82
|
+
const storage = bVecStorage.concat(zeroPaddingArray);
|
|
83
|
+
const len = toSingle(new Fr(bVecStorage.length));
|
|
53
84
|
return [
|
|
54
85
|
storage,
|
|
55
86
|
len
|
|
56
87
|
];
|
|
57
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Converts an array of arrays representing Noir BoundedVec of nested arrays into its Noir serialized form.
|
|
91
|
+
* @param bVecStorage - The array underlying the BoundedVec.
|
|
92
|
+
* @param maxLen - The max length of the BoundedVec (max num of the nested arrays in the BoundedVec).
|
|
93
|
+
* @param nestedArrayLength - The length of the nested arrays (each nested array has to have the same length).
|
|
94
|
+
* @returns Serialized BoundedVec following Noir intrinsic serialization.
|
|
95
|
+
*/ export function arrayOfArraysToBoundedVecOfArrays(bVecStorage, maxLen, nestedArrayLength) {
|
|
96
|
+
if (bVecStorage.length > maxLen) {
|
|
97
|
+
throw new Error(`Array of length ${bVecStorage.length} larger than maxLen ${maxLen}`);
|
|
98
|
+
}
|
|
99
|
+
// Check that all nested arrays have length nestedArrayLength
|
|
100
|
+
if (!bVecStorage.every((nestedArray)=>nestedArray.length === nestedArrayLength)) {
|
|
101
|
+
throw new Error(`Nested array length passed in from Noir does not correspond to the length obtained in TS: ${nestedArrayLength} !== ${bVecStorage[0].length}`);
|
|
102
|
+
}
|
|
103
|
+
const flattenedStorage = bVecStorage.flat();
|
|
104
|
+
const numFieldsToPad = maxLen * nestedArrayLength - flattenedStorage.length;
|
|
105
|
+
const flattenedStorageWithPadding = flattenedStorage.concat(Array(numFieldsToPad).fill(new Fr(0)));
|
|
106
|
+
// At last we get the actual length of the BoundedVec and return the values.
|
|
107
|
+
const len = toSingle(new Fr(bVecStorage.length));
|
|
108
|
+
return [
|
|
109
|
+
flattenedStorageWithPadding,
|
|
110
|
+
len
|
|
111
|
+
];
|
|
112
|
+
}
|
|
58
113
|
export function toForeignCallResult(obj) {
|
|
59
114
|
return {
|
|
60
115
|
values: obj
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ContractArtifact } from '@aztec/aztec.js/abi';
|
|
2
|
+
import { Fr } from '@aztec/aztec.js/fields';
|
|
3
|
+
import { ContractDataProvider } from '@aztec/pxe/server';
|
|
4
|
+
export type ContractArtifactWithHash = ContractArtifact & {
|
|
5
|
+
artifactHash: Fr;
|
|
6
|
+
};
|
|
7
|
+
export declare class TXEContractDataProvider extends ContractDataProvider {
|
|
8
|
+
#private;
|
|
9
|
+
addContractArtifact(id: Fr, artifact: ContractArtifact | ContractArtifactWithHash): Promise<void>;
|
|
10
|
+
getContractArtifact(contractClassId: Fr): Promise<ContractArtifact | ContractArtifactWithHash | undefined>;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=txe_contract_data_provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"txe_contract_data_provider.d.ts","sourceRoot":"","sources":["../../src/util/txe_contract_data_provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAC5D,OAAO,EAAE,EAAE,EAAE,MAAM,wBAAwB,CAAC;AAC5C,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAEzD,MAAM,MAAM,wBAAwB,GAAG,gBAAgB,GAAG;IAAE,YAAY,EAAE,EAAE,CAAA;CAAE,CAAC;AAO/E,qBAAa,uBAAwB,SAAQ,oBAAoB;;IAGzC,mBAAmB,CACvC,EAAE,EAAE,EAAE,EACN,QAAQ,EAAE,gBAAgB,GAAG,wBAAwB,GACpD,OAAO,CAAC,IAAI,CAAC;IAOM,mBAAmB,CACvC,eAAe,EAAE,EAAE,GAClB,OAAO,CAAC,gBAAgB,GAAG,wBAAwB,GAAG,SAAS,CAAC;CASpE"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Fr } from '@aztec/aztec.js/fields';
|
|
2
|
+
import { ContractDataProvider } from '@aztec/pxe/server';
|
|
3
|
+
/*
|
|
4
|
+
* A contract data provider that stores contract artifacts with their hashes. Since
|
|
5
|
+
* TXE typically deploys the same contract again and again for multiple tests, caching
|
|
6
|
+
* the *very* expensive artifact hash computation improves testing speed significantly.
|
|
7
|
+
*/ export class TXEContractDataProvider extends ContractDataProvider {
|
|
8
|
+
#artifactHashes = new Map();
|
|
9
|
+
async addContractArtifact(id, artifact) {
|
|
10
|
+
if ('artifactHash' in artifact) {
|
|
11
|
+
this.#artifactHashes.set(id.toString(), artifact.artifactHash.toBuffer());
|
|
12
|
+
}
|
|
13
|
+
await super.addContractArtifact(id, artifact);
|
|
14
|
+
}
|
|
15
|
+
async getContractArtifact(contractClassId) {
|
|
16
|
+
const artifact = await super.getContractArtifact(contractClassId);
|
|
17
|
+
if (artifact && this.#artifactHashes.has(contractClassId.toString())) {
|
|
18
|
+
artifact.artifactHash = Fr.fromBuffer(this.#artifactHashes.get(contractClassId.toString()));
|
|
19
|
+
}
|
|
20
|
+
return artifact;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { Fr } from '@aztec/foundation/fields';
|
|
2
|
+
import type { ContractDataProvider } from '@aztec/pxe/server';
|
|
2
3
|
import { type ContractArtifact, FunctionSelector } from '@aztec/stdlib/abi';
|
|
3
4
|
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
4
|
-
import { type ContractClassPublic, type ContractDataSource, type ContractInstanceWithAddress
|
|
5
|
-
import type { TXE } from '../oracle/txe_oracle.js';
|
|
5
|
+
import { type ContractClassPublic, type ContractDataSource, type ContractInstanceWithAddress } from '@aztec/stdlib/contract';
|
|
6
6
|
export declare class TXEPublicContractDataSource implements ContractDataSource {
|
|
7
|
-
private
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
#private;
|
|
8
|
+
private blockNumber;
|
|
9
|
+
private contractDataProvider;
|
|
10
|
+
constructor(blockNumber: number, contractDataProvider: ContractDataProvider);
|
|
10
11
|
getBlockNumber(): Promise<number>;
|
|
11
12
|
getContractClass(id: Fr): Promise<ContractClassPublic | undefined>;
|
|
12
13
|
getBytecodeCommitment(id: Fr): Promise<Fr | undefined>;
|
|
13
14
|
getContract(address: AztecAddress): Promise<ContractInstanceWithAddress | undefined>;
|
|
14
15
|
getContractClassIds(): Promise<Fr[]>;
|
|
15
16
|
getContractArtifact(address: AztecAddress): Promise<ContractArtifact | undefined>;
|
|
16
|
-
|
|
17
|
-
registerContractFunctionSignatures(
|
|
18
|
-
addContractClass(_contractClass: ContractClassPublic): Promise<void>;
|
|
17
|
+
getDebugFunctionName(address: AztecAddress, selector: FunctionSelector): Promise<string | undefined>;
|
|
18
|
+
registerContractFunctionSignatures(_signatures: []): Promise<void>;
|
|
19
19
|
}
|
|
20
20
|
//# sourceMappingURL=txe_public_contract_data_source.d.ts.map
|