@aztec/txe 0.44.0

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.
@@ -0,0 +1,63 @@
1
+ import { type AztecAddress, Fr, type FunctionSelector, unpackBytecode } from '@aztec/circuits.js';
2
+ import { type ContractArtifact } from '@aztec/foundation/abi';
3
+ import { PrivateFunctionsTree } from '@aztec/pxe';
4
+ import {
5
+ type ContractClassPublic,
6
+ type ContractDataSource,
7
+ type ContractInstanceWithAddress,
8
+ type PublicFunction,
9
+ } from '@aztec/types/contracts';
10
+
11
+ import { type TXE } from '../oracle/txe_oracle.js';
12
+
13
+ export class TXEPublicContractDataSource implements ContractDataSource {
14
+ constructor(private txeOracle: TXE) {}
15
+
16
+ async getPublicFunction(address: AztecAddress, selector: FunctionSelector): Promise<PublicFunction | undefined> {
17
+ const bytecode = await this.txeOracle.getContractDataOracle().getBytecode(address, selector);
18
+ if (!bytecode) {
19
+ return undefined;
20
+ }
21
+ return { bytecode, selector };
22
+ }
23
+
24
+ getBlockNumber(): Promise<number> {
25
+ return this.txeOracle.getBlockNumber();
26
+ }
27
+
28
+ async getContractClass(id: Fr): Promise<ContractClassPublic | undefined> {
29
+ const contractClass = await this.txeOracle.getContractDataOracle().getContractClass(id);
30
+ const artifact = await this.txeOracle.getContractDataOracle().getContractArtifact(id);
31
+ const tree = new PrivateFunctionsTree(artifact);
32
+ const privateFunctionsRoot = tree.getFunctionTreeRoot();
33
+
34
+ return {
35
+ id,
36
+ artifactHash: contractClass!.artifactHash,
37
+ packedBytecode: contractClass!.packedBytecode,
38
+ publicFunctions: unpackBytecode(contractClass!.packedBytecode),
39
+ privateFunctionsRoot: new Fr(privateFunctionsRoot!.root),
40
+ version: contractClass!.version,
41
+ privateFunctions: [],
42
+ unconstrainedFunctions: [],
43
+ };
44
+ }
45
+
46
+ async getContract(address: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
47
+ const instance = await this.txeOracle.getContractDataOracle().getContractInstance(address);
48
+ return { ...instance, address };
49
+ }
50
+
51
+ getContractClassIds(): Promise<Fr[]> {
52
+ throw new Error('Method not implemented.');
53
+ }
54
+
55
+ async getContractArtifact(address: AztecAddress): Promise<ContractArtifact | undefined> {
56
+ const instance = await this.txeOracle.getContractDataOracle().getContractInstance(address);
57
+ return this.txeOracle.getContractDataOracle().getContractArtifact(instance.contractClassId);
58
+ }
59
+
60
+ addContractArtifact(address: AztecAddress, contract: ContractArtifact): Promise<void> {
61
+ return this.txeOracle.addContractArtifact(contract);
62
+ }
63
+ }
@@ -0,0 +1,57 @@
1
+ import { MerkleTreeId } from '@aztec/circuit-types';
2
+ import {
3
+ type AztecAddress,
4
+ Fr,
5
+ PUBLIC_DATA_SUBTREE_HEIGHT,
6
+ PublicDataTreeLeaf,
7
+ type PublicDataTreeLeafPreimage,
8
+ } from '@aztec/circuits.js';
9
+ import { computePublicDataTreeLeafSlot } from '@aztec/circuits.js/hash';
10
+ import { type PublicStateDB } from '@aztec/simulator';
11
+
12
+ import { type TXE } from '../oracle/txe_oracle.js';
13
+
14
+ export class TXEPublicStateDB implements PublicStateDB {
15
+ constructor(private txeOracle: TXE) {}
16
+
17
+ async storageRead(contract: AztecAddress, slot: Fr): Promise<Fr> {
18
+ const db = this.txeOracle.getTrees().asLatest();
19
+ const leafSlot = computePublicDataTreeLeafSlot(contract, slot).toBigInt();
20
+
21
+ const lowLeafResult = await db.getPreviousValueIndex(MerkleTreeId.PUBLIC_DATA_TREE, leafSlot);
22
+
23
+ let value = Fr.ZERO;
24
+ if (lowLeafResult && lowLeafResult.alreadyPresent) {
25
+ const preimage = (await db.getLeafPreimage(
26
+ MerkleTreeId.PUBLIC_DATA_TREE,
27
+ lowLeafResult.index,
28
+ )) as PublicDataTreeLeafPreimage;
29
+ value = preimage.value;
30
+ }
31
+ return value;
32
+ }
33
+
34
+ async storageWrite(contract: AztecAddress, slot: Fr, newValue: Fr): Promise<bigint> {
35
+ const db = this.txeOracle.getTrees().asLatest();
36
+
37
+ await db.batchInsert(
38
+ MerkleTreeId.PUBLIC_DATA_TREE,
39
+ [new PublicDataTreeLeaf(computePublicDataTreeLeafSlot(contract, slot), newValue).toBuffer()],
40
+ PUBLIC_DATA_SUBTREE_HEIGHT,
41
+ );
42
+ return newValue.toBigInt();
43
+ }
44
+
45
+ checkpoint(): Promise<void> {
46
+ return Promise.resolve();
47
+ }
48
+ rollbackToCheckpoint(): Promise<void> {
49
+ throw new Error('Cannot rollback');
50
+ }
51
+ commit(): Promise<void> {
52
+ return Promise.resolve();
53
+ }
54
+ rollbackToCommit(): Promise<void> {
55
+ throw new Error('Cannot rollback');
56
+ }
57
+ }