From b501845cf6ffe8e7a4ce7759f58afad541f2f31f Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 24 May 2024 11:08:38 +0200 Subject: [PATCH 1/2] Move RPC oracles to a separate subfolder --- .../src/noir/oracles/rpc/transactionOracle.int.test.ts | 4 ++-- .../oracles/src/noir/oracles/rpc/transactionOracle/encode.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ethereum/oracles/src/noir/oracles/rpc/transactionOracle.int.test.ts b/ethereum/oracles/src/noir/oracles/rpc/transactionOracle.int.test.ts index beb80717..c223e710 100644 --- a/ethereum/oracles/src/noir/oracles/rpc/transactionOracle.int.test.ts +++ b/ethereum/oracles/src/noir/oracles/rpc/transactionOracle.int.test.ts @@ -1,10 +1,10 @@ import { describe, expect, it } from 'vitest'; import { createMockMultiChainClient } from '../../../ethereum/mockClient.js'; -import { OFFSETS, getTransactionOracle } from './transactionOracle.js'; -import { MAX_DATA_LEN_M } from './common/txConfig.js'; import { padArray } from '../../../util/array.js'; +import { MAX_DATA_LEN_M } from './common/txConfig.js'; import { ZERO_PAD_VALUE } from '../common/const.js'; import { txProofConfigM } from './common/proofConfig/tx.js'; +import { OFFSETS, getTransactionOracle } from './transactionOracle.js'; describe('getTransactionOracle', () => { it('success', async () => { diff --git a/ethereum/oracles/src/noir/oracles/rpc/transactionOracle/encode.ts b/ethereum/oracles/src/noir/oracles/rpc/transactionOracle/encode.ts index b78276fe..3dd99d83 100644 --- a/ethereum/oracles/src/noir/oracles/rpc/transactionOracle/encode.ts +++ b/ethereum/oracles/src/noir/oracles/rpc/transactionOracle/encode.ts @@ -1,4 +1,3 @@ -import { Transaction } from 'viem'; import { MAX_DATA_LEN_M } from '../common/txConfig.js'; import { ForeignCallOutput } from '@noir-lang/noir_js'; import { padArray } from '../../../../util/array.js'; @@ -8,6 +7,7 @@ import { ZERO_PAD_VALUE, MAX_TRIE_NODE_LEN } from '../../common/const.js'; import { encodeField, encodeAddress, encodeU128, encodeHex, encodeBytes, encodeProof } from '../../common/encode.js'; import { txProofConfigM } from '../common/proofConfig/tx.js'; import { Proof } from '../../../../ethereum/proof.js'; +import { Transaction } from 'viem'; export enum TX_OFFSETS { NONCE, From 78200a761ffa81f3e8431ad90fc29042825e481b Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 24 May 2024 11:18:34 +0200 Subject: [PATCH 2/2] Remove dead code & rename file --- .../oracles/src/noir/oracles/oracles.test.ts | 17 --------- ethereum/oracles/src/noir/oracles/oracles.ts | 35 ------------------- .../src/noir/oracles/rpc/accountOracle.ts | 2 +- .../src/noir/oracles/rpc/headerOracle.ts | 2 +- .../src/noir/oracles/rpc/proofOracle.ts | 2 +- .../src/noir/oracles/rpc/receiptOracle.ts | 2 +- .../src/noir/oracles/rpc/transactionOracle.ts | 2 +- .../src/noir/oracles/server/encode.test.ts | 2 +- .../oracles/src/noir/oracles/server/encode.ts | 2 +- .../src/noir/oracles/server/handlers.ts | 2 +- ethereum/oracles/src/noir/oracles/types.ts | 7 ++++ 11 files changed, 15 insertions(+), 60 deletions(-) delete mode 100644 ethereum/oracles/src/noir/oracles/oracles.test.ts delete mode 100644 ethereum/oracles/src/noir/oracles/oracles.ts create mode 100644 ethereum/oracles/src/noir/oracles/types.ts diff --git a/ethereum/oracles/src/noir/oracles/oracles.test.ts b/ethereum/oracles/src/noir/oracles/oracles.test.ts deleted file mode 100644 index 6ca18978..00000000 --- a/ethereum/oracles/src/noir/oracles/oracles.test.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { describe, expect, it } from 'vitest'; -import { createOracles } from './oracles.js'; -import { MultiChainClient } from '../../ethereum/client.js'; - -describe('importOracles', () => { - it('success', async () => { - const oracle = createOracles({} as MultiChainClient)({ - stub: async () => await Promise.resolve(['7']) - }); - expect(await oracle('stub', [])).toStrictEqual(['7']); - }); - - it('throws when non-existing oracle', async () => { - const oracle = createOracles({} as MultiChainClient)({}); - await expect(oracle('non-existing', [])).rejects.toThrow(); - }); -}); diff --git a/ethereum/oracles/src/noir/oracles/oracles.ts b/ethereum/oracles/src/noir/oracles/oracles.ts deleted file mode 100644 index b451cfad..00000000 --- a/ethereum/oracles/src/noir/oracles/oracles.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { type ForeignCallOutput } from '@noir-lang/noir_js'; -import { MultiChainClient } from '../../ethereum/client.js'; -import { getAccountOracle } from './rpc/accountOracle.js'; -import { getHeaderOracle } from './rpc/headerOracle.js'; -import { getProofOracle } from './rpc/proofOracle.js'; -import { getReceiptOracle } from './rpc/receiptOracle.js'; -import { getTransactionOracle } from './rpc/transactionOracle.js'; - -export type NoirArgument = string[]; -export type NoirArguments = NoirArgument[]; - -export type Oracle = (multiChainClient: MultiChainClient, args: NoirArguments) => Promise; - -export type Oracles = (name: string, args: NoirArguments) => Promise; - -type OracleMap = Record; - -export const createOracles = - (multiChainClient: MultiChainClient) => - (dict: OracleMap): Oracles => - async (name: string, args: NoirArguments): Promise => { - const fn = dict[name]; - if (fn === undefined) { - throw new Error(`Unknown oracle ${name}`); - } - return await fn(multiChainClient, args); - }; - -export const defaultOraclesMap: OracleMap = { - get_account: getAccountOracle, - get_header: getHeaderOracle, - get_proof: getProofOracle, - get_receipt: getReceiptOracle, - get_transaction: getTransactionOracle -}; diff --git a/ethereum/oracles/src/noir/oracles/rpc/accountOracle.ts b/ethereum/oracles/src/noir/oracles/rpc/accountOracle.ts index d4dc42e5..9bb434dd 100644 --- a/ethereum/oracles/src/noir/oracles/rpc/accountOracle.ts +++ b/ethereum/oracles/src/noir/oracles/rpc/accountOracle.ts @@ -3,7 +3,7 @@ import { type Address } from 'viem'; import { assert } from '../../../util/assert.js'; import { encodeAccount, encodeStateProof } from './accountOracle/encode.js'; import { decodeAddress, decodeField } from '../common/decode.js'; -import { NoirArguments } from '../oracles.js'; +import { NoirArguments } from '../types.js'; import { MultiChainClient } from '../../../ethereum/client.js'; import { Enum } from '../../../util/enum.js'; diff --git a/ethereum/oracles/src/noir/oracles/rpc/headerOracle.ts b/ethereum/oracles/src/noir/oracles/rpc/headerOracle.ts index ee490ae5..39ebb193 100644 --- a/ethereum/oracles/src/noir/oracles/rpc/headerOracle.ts +++ b/ethereum/oracles/src/noir/oracles/rpc/headerOracle.ts @@ -3,7 +3,7 @@ import { type BlockHeader, blockToHeader } from '../../../ethereum/blockHeader.j import { assert } from '../../../util/assert.js'; import { encodeBlockHeader } from './headerOracle/encode.js'; import { decodeField } from '../common/decode.js'; -import { NoirArguments } from '../oracles.js'; +import { NoirArguments } from '../types.js'; import { type Block } from '../../../ethereum/blockHeader.js'; import { AlchemyClient, MultiChainClient } from '../../../ethereum/client.js'; import { Enum } from '../../../util/enum.js'; diff --git a/ethereum/oracles/src/noir/oracles/rpc/proofOracle.ts b/ethereum/oracles/src/noir/oracles/rpc/proofOracle.ts index 72c60359..c2d30f21 100644 --- a/ethereum/oracles/src/noir/oracles/rpc/proofOracle.ts +++ b/ethereum/oracles/src/noir/oracles/rpc/proofOracle.ts @@ -2,7 +2,7 @@ import { type ForeignCallOutput } from '@noir-lang/noir_js'; import { assert } from '../../../util/assert.js'; import { encodeAccount, encodeStateProof, encodeStorageProof } from './accountOracle/encode.js'; import { decodeAddress, decodeBytes32, decodeField } from '../common/decode.js'; -import { NoirArguments } from '../oracles.js'; +import { NoirArguments } from '../types.js'; import { Hex } from 'viem'; import { MultiChainClient } from '../../../ethereum/client.js'; import { Enum } from '../../../util/enum.js'; diff --git a/ethereum/oracles/src/noir/oracles/rpc/receiptOracle.ts b/ethereum/oracles/src/noir/oracles/rpc/receiptOracle.ts index 989f098b..a746e5c4 100644 --- a/ethereum/oracles/src/noir/oracles/rpc/receiptOracle.ts +++ b/ethereum/oracles/src/noir/oracles/rpc/receiptOracle.ts @@ -1,7 +1,7 @@ import { type ForeignCallOutput } from '@noir-lang/noir_js'; import { assert } from '../../../util/assert.js'; import { decodeField } from '../common/decode.js'; -import { NoirArguments } from '../oracles.js'; +import { NoirArguments } from '../types.js'; import { MultiChainClient } from '../../../ethereum/client.js'; import { getReceiptProof } from '../../../ethereum/receiptProof.js'; import { encodeReceipt, encodeReceiptProof } from './receiptOracle/encode.js'; diff --git a/ethereum/oracles/src/noir/oracles/rpc/transactionOracle.ts b/ethereum/oracles/src/noir/oracles/rpc/transactionOracle.ts index 9133e277..cca2428d 100644 --- a/ethereum/oracles/src/noir/oracles/rpc/transactionOracle.ts +++ b/ethereum/oracles/src/noir/oracles/rpc/transactionOracle.ts @@ -1,5 +1,5 @@ import { type ForeignCallOutput } from '@noir-lang/noir_js'; -import { NoirArguments } from '../oracles.js'; +import { NoirArguments } from '../types.js'; import { MultiChainClient } from '../../../ethereum/client.js'; import { txTypeToHex } from '../../../ethereum/receipt.js'; import { getTxProof } from '../../../ethereum/txProof.js'; diff --git a/ethereum/oracles/src/noir/oracles/server/encode.test.ts b/ethereum/oracles/src/noir/oracles/server/encode.test.ts index b85c97f8..6a2056cf 100644 --- a/ethereum/oracles/src/noir/oracles/server/encode.test.ts +++ b/ethereum/oracles/src/noir/oracles/server/encode.test.ts @@ -1,7 +1,7 @@ import { describe, it, expect } from 'vitest'; import { decodeNoirArguments, encodeForeignCallResult } from './encode.js'; import { ForeignCallParams, ForeignCallResult } from './types.js'; -import { NoirArguments } from '../oracles.js'; +import { NoirArguments } from '../types.js'; describe('decodeNoirArguments', () => { it('should decode a single foreign call param correctly', () => { diff --git a/ethereum/oracles/src/noir/oracles/server/encode.ts b/ethereum/oracles/src/noir/oracles/server/encode.ts index d6570d76..d383d610 100644 --- a/ethereum/oracles/src/noir/oracles/server/encode.ts +++ b/ethereum/oracles/src/noir/oracles/server/encode.ts @@ -1,6 +1,6 @@ import { ForeignCallOutput } from '@noir-lang/noir_js'; import { ForeignCallParam, ForeignCallParams, ForeignCallResult } from './types.js'; -import { NoirArguments } from '../oracles.js'; +import { NoirArguments } from '../types.js'; /// DECODE export function decodeNoirArguments(params: ForeignCallParams): NoirArguments { diff --git a/ethereum/oracles/src/noir/oracles/server/handlers.ts b/ethereum/oracles/src/noir/oracles/server/handlers.ts index 007ab406..68ba4879 100644 --- a/ethereum/oracles/src/noir/oracles/server/handlers.ts +++ b/ethereum/oracles/src/noir/oracles/server/handlers.ts @@ -1,7 +1,7 @@ import { ForeignCallResult, ForeignCallParams } from './types.js'; import { decodeNoirArguments, encodeForeignCallResult } from './encode.js'; import { MultiChainClient } from '../../../ethereum/client.js'; -import { Oracle } from '../oracles.js'; +import { Oracle } from '../types.js'; /** * The format that the Noir oracles server receives the arguments in is slightly different than the format that acvm.js uses. diff --git a/ethereum/oracles/src/noir/oracles/types.ts b/ethereum/oracles/src/noir/oracles/types.ts new file mode 100644 index 00000000..857388c9 --- /dev/null +++ b/ethereum/oracles/src/noir/oracles/types.ts @@ -0,0 +1,7 @@ +import { type ForeignCallOutput } from '@noir-lang/noir_js'; +import { MultiChainClient } from '../../ethereum/client.js'; + +export type NoirArgument = string[]; +export type NoirArguments = NoirArgument[]; + +export type Oracle = (multiChainClient: MultiChainClient, args: NoirArguments) => Promise;