From 06e289cec5654e9fd10ecdadaea27ecbfeffd241 Mon Sep 17 00:00:00 2001 From: pgherveou Date: Fri, 22 Nov 2024 15:42:16 +0100 Subject: [PATCH 1/8] squashed --- Cargo.toml | 35 +- prdoc/pr_5926.prdoc | 13 + substrate/frame/revive/rpc/codegen/Cargo.toml | 18 + substrate/frame/revive/rpc/codegen/README.md | 5 + .../frame/revive/rpc/codegen/openrpc.json | 2268 +++++++++++++++++ .../frame/revive/rpc/codegen/src/LICENSE.txt | 16 + .../frame/revive/rpc/codegen/src/generator.rs | 740 ++++++ .../frame/revive/rpc/codegen/src/main.rs | 64 + .../frame/revive/rpc/codegen/src/open_rpc.rs | 834 ++++++ .../frame/revive/rpc/codegen/src/printer.rs | 496 ++++ 10 files changed, 4468 insertions(+), 21 deletions(-) create mode 100644 prdoc/pr_5926.prdoc create mode 100644 substrate/frame/revive/rpc/codegen/Cargo.toml create mode 100644 substrate/frame/revive/rpc/codegen/README.md create mode 100644 substrate/frame/revive/rpc/codegen/openrpc.json create mode 100644 substrate/frame/revive/rpc/codegen/src/LICENSE.txt create mode 100644 substrate/frame/revive/rpc/codegen/src/generator.rs create mode 100644 substrate/frame/revive/rpc/codegen/src/main.rs create mode 100644 substrate/frame/revive/rpc/codegen/src/open_rpc.rs create mode 100644 substrate/frame/revive/rpc/codegen/src/printer.rs diff --git a/Cargo.toml b/Cargo.toml index 533ea4c9e878..5402b3e60cc4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -403,6 +403,7 @@ members = [ "substrate/frame/revive/mock-network", "substrate/frame/revive/proc-macro", "substrate/frame/revive/rpc", + "substrate/frame/revive/rpc/codegen", "substrate/frame/revive/uapi", "substrate/frame/root-offences", "substrate/frame/root-testing", @@ -556,13 +557,7 @@ default-members = [ [workspace.lints.rust] suspicious_double_ref_op = { level = "allow", priority = 2 } # `substrate_runtime` is a common `cfg` condition name used in the repo. -unexpected_cfgs = { level = "warn", check-cfg = [ - 'cfg(build_opt_level, values("3"))', - 'cfg(build_profile, values("debug", "release"))', - 'cfg(enable_alloc_error_handler)', - 'cfg(fuzzing)', - 'cfg(substrate_runtime)', -] } +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(build_opt_level, values("3"))', 'cfg(build_profile, values("debug", "release"))', 'cfg(enable_alloc_error_handler)', 'cfg(fuzzing)', 'cfg(substrate_runtime)'] } [workspace.lints.clippy] all = { level = "allow", priority = 0 } @@ -637,7 +632,7 @@ bitvec = { version = "1.0.1", default-features = false } blake2 = { version = "0.10.4", default-features = false } blake2b_simd = { version = "1.0.2", default-features = false } blake3 = { version = "1.5" } -bounded-collections = { version = "0.2.2", default-features = false } +bounded-collections = { version = "0.2.0", default-features = false } bounded-vec = { version = "0.7" } bp-asset-hub-rococo = { path = "bridges/chains/chain-asset-hub-rococo", default-features = false } bp-asset-hub-westend = { path = "bridges/chains/chain-asset-hub-westend", default-features = false } @@ -683,7 +678,6 @@ cid = { version = "0.9.0" } clap = { version = "4.5.13" } clap-num = { version = "1.0.2" } clap_complete = { version = "4.5.13" } -cmd_lib = { version = "1.9.5" } coarsetime = { version = "0.1.22" } codec = { version = "3.6.12", default-features = false, package = "parity-scale-codec" } collectives-westend-emulated-chain = { path = "cumulus/parachains/integration-tests/emulated/chains/parachains/collectives/collectives-westend" } @@ -742,7 +736,7 @@ derive_more = { version = "0.99.17", default-features = false } digest = { version = "0.10.3", default-features = false } directories = { version = "5.0.1" } dlmalloc = { version = "0.2.4" } -docify = { version = "0.2.9" } +docify = { version = "0.2.8" } dyn-clonable = { version = "0.9.0" } dyn-clone = { version = "1.0.16" } ed25519-dalek = { version = "2.1", default-features = false } @@ -754,7 +748,7 @@ enumn = { version = "0.1.13" } env_logger = { version = "0.11.2" } environmental = { version = "1.1.4", default-features = false } equivocation-detector = { path = "bridges/relays/equivocation" } -ethabi = { version = "2.0.0", default-features = false, package = "ethabi-decode" } +ethabi = { version = "1.0.0", default-features = false, package = "ethabi-decode" } ethbloom = { version = "0.14.1", default-features = false } ethereum-types = { version = "0.15.1", default-features = false } exit-future = { version = "0.2.0" } @@ -792,7 +786,7 @@ frame-system-rpc-runtime-api = { path = "substrate/frame/system/rpc/runtime-api" frame-try-runtime = { path = "substrate/frame/try-runtime", default-features = false } fs4 = { version = "0.7.0" } fs_extra = { version = "1.3.0" } -futures = { version = "0.3.31" } +futures = { version = "0.3.30" } futures-channel = { version = "0.3.23" } futures-timer = { version = "3.0.2" } futures-util = { version = "0.3.30", default-features = false } @@ -848,7 +842,7 @@ linked-hash-map = { version = "0.5.4" } linked_hash_set = { version = "0.1.4" } linregress = { version = "0.5.1" } lite-json = { version = "0.2.0", default-features = false } -litep2p = { version = "0.8.1", features = ["websocket"] } +litep2p = { version = "0.7.0", features = ["websocket"] } log = { version = "0.4.22", default-features = false } macro_magic = { version = "0.5.1" } maplit = { version = "1.0.2" } @@ -1094,9 +1088,7 @@ polkavm-derive = "0.9.1" polkavm-linker = "0.9.2" portpicker = { version = "0.1.1" } pretty_assertions = { version = "1.3.0" } -primitive-types = { version = "0.13.1", default-features = false, features = [ - "num-traits", -] } +primitive-types = { version = "0.13.1", default-features = false, features = ["num-traits"] } proc-macro-crate = { version = "3.0.0" } proc-macro-warning = { version = "1.0.0", default-features = false } proc-macro2 = { version = "1.0.86" } @@ -1205,11 +1197,12 @@ seccompiler = { version = "0.4.0" } secp256k1 = { version = "0.28.0", default-features = false } secrecy = { version = "0.8.0", default-features = false } separator = { version = "0.4.1" } -serde = { version = "1.0.214", default-features = false } +serde = { version = "1.0.210", default-features = false } serde-big-array = { version = "0.3.2" } serde_derive = { version = "1.0.117" } serde_json = { version = "1.0.132", default-features = false } serde_yaml = { version = "0.9" } +serial_test = { version = "2.0.0" } sha1 = { version = "0.10.6" } sha2 = { version = "0.10.7", default-features = false } sha3 = { version = "0.10.0", default-features = false } @@ -1316,9 +1309,9 @@ substrate-test-runtime-client = { path = "substrate/test-utils/runtime/client" } substrate-test-runtime-transaction-pool = { path = "substrate/test-utils/runtime/transaction-pool" } substrate-test-utils = { path = "substrate/test-utils" } substrate-wasm-builder = { path = "substrate/utils/wasm-builder", default-features = false } -subxt = { version = "0.38", default-features = false } -subxt-signer = { version = "0.38" } -syn = { version = "2.0.87" } +subxt = { version = "0.37", default-features = false } +subxt-signer = { version = "0.37" } +syn = { version = "2.0.82" } sysinfo = { version = "0.30" } tar = { version = "0.4" } tempfile = { version = "3.8.1" } @@ -1387,7 +1380,7 @@ xcm-procedural = { path = "polkadot/xcm/procedural", default-features = false } xcm-runtime-apis = { path = "polkadot/xcm/xcm-runtime-apis", default-features = false } xcm-simulator = { path = "polkadot/xcm/xcm-simulator", default-features = false } zeroize = { version = "1.7.0", default-features = false } -zombienet-sdk = { version = "0.2.15" } +zombienet-sdk = { version = "0.2.13" } zstd = { version = "0.12.4", default-features = false } [profile.release] diff --git a/prdoc/pr_5926.prdoc b/prdoc/pr_5926.prdoc new file mode 100644 index 000000000000..f05aeb93eb71 --- /dev/null +++ b/prdoc/pr_5926.prdoc @@ -0,0 +1,13 @@ +# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 +# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json + +title: "[pallet-revive] add codegen for Ethereum RPC API" + +doc: + - audience: Runtime Dev + description: | + Add codegen crate for generating Ethereum RPC methods and types from the spec. + +crates: + - name: pallet-revive-rpc-codegen + bump: patch diff --git a/substrate/frame/revive/rpc/codegen/Cargo.toml b/substrate/frame/revive/rpc/codegen/Cargo.toml new file mode 100644 index 000000000000..65d8ba501b1c --- /dev/null +++ b/substrate/frame/revive/rpc/codegen/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "pallet-revive-rpc-codegen" +version = "0.1.0" +edition.workspace = true +publish = false + +[dependencies] +Inflector = { workspace = true } +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } +anyhow = { workspace = true } + +[dev-dependencies] +pretty_assertions.workspace = true + +[features] +default = ["std"] +std = ["anyhow/std", "serde/std", "serde_json/std"] diff --git a/substrate/frame/revive/rpc/codegen/README.md b/substrate/frame/revive/rpc/codegen/README.md new file mode 100644 index 000000000000..2ca838f0db23 --- /dev/null +++ b/substrate/frame/revive/rpc/codegen/README.md @@ -0,0 +1,5 @@ +Generates the Ethereum JSON-RPC API from the official specification. + +- See +- See building instructions to re-generate the openrpc.json +- Include fixes from diff --git a/substrate/frame/revive/rpc/codegen/openrpc.json b/substrate/frame/revive/rpc/codegen/openrpc.json new file mode 100644 index 000000000000..7b55131590ee --- /dev/null +++ b/substrate/frame/revive/rpc/codegen/openrpc.json @@ -0,0 +1,2268 @@ +{ + "openrpc": "1.2.4", + "info": { + "title": "Ethereum JSON-RPC Specification", + "description": "A specification of the standard interface for Ethereum clients.", + "license": { + "name": "CC0-1.0", + "url": "https://creativecommons.org/publicdomain/zero/1.0/legalcode" + }, + "version": "0.0.0" + }, + "methods": [ + { + "name": "eth_accounts", + "summary": "Returns a list of addresses owned by client.", + "params": [], + "result": { + "name": "Accounts", + "schema": { + "title": "Accounts", + "type": "array", + "items": { + "$ref": "#/components/schemas/address" + } + } + } + }, + { + "name": "eth_blobBaseFee", + "summary": "Returns the base fee per blob gas in wei.", + "params": [], + "result": { + "name": "Blob gas base fee", + "schema": { + "title": "Blob gas base fee", + "$ref": "#/components/schemas/uint" + } + } + }, + { + "name": "eth_blockNumber", + "summary": "Returns the number of most recent block.", + "params": [], + "result": { + "name": "Block number", + "schema": { + "$ref": "#/components/schemas/uint" + } + } + }, + { + "name": "eth_call", + "summary": "Executes a new message call immediately without creating a transaction on the block chain.", + "params": [ + { + "name": "Transaction", + "required": true, + "schema": { + "$ref": "#/components/schemas/GenericTransaction" + } + }, + { + "name": "Block", + "required": false, + "schema": { + "$ref": "#/components/schemas/BlockNumberOrTagOrHash" + } + } + ], + "result": { + "name": "Return data", + "schema": { + "$ref": "#/components/schemas/bytes" + } + } + }, + { + "name": "eth_chainId", + "summary": "Returns the chain ID of the current network.", + "params": [], + "result": { + "name": "Chain ID", + "schema": { + "$ref": "#/components/schemas/uint" + } + } + }, + { + "name": "eth_coinbase", + "summary": "Returns the client coinbase address.", + "params": [], + "result": { + "name": "Coinbase address", + "schema": { + "$ref": "#/components/schemas/address" + } + } + }, + { + "name": "eth_createAccessList", + "summary": "Generates an access list for a transaction.", + "params": [ + { + "name": "Transaction", + "required": true, + "schema": { + "$ref": "#/components/schemas/GenericTransaction" + } + }, + { + "name": "Block", + "required": false, + "schema": { + "$ref": "#/components/schemas/BlockNumberOrTag" + } + } + ], + "result": { + "name": "Gas used", + "schema": { + "title": "Access list result", + "type": "object", + "additionalProperties": false, + "properties": { + "accessList": { + "title": "accessList", + "$ref": "#/components/schemas/AccessList" + }, + "error": { + "title": "error", + "type": "string" + }, + "gasUsed": { + "title": "Gas used", + "$ref": "#/components/schemas/uint" + } + } + } + } + }, + { + "name": "eth_estimateGas", + "summary": "Generates and returns an estimate of how much gas is necessary to allow the transaction to complete.", + "params": [ + { + "name": "Transaction", + "required": true, + "schema": { + "$ref": "#/components/schemas/GenericTransaction" + } + }, + { + "name": "Block", + "required": false, + "schema": { + "$ref": "#/components/schemas/BlockNumberOrTag" + } + } + ], + "result": { + "name": "Gas used", + "schema": { + "$ref": "#/components/schemas/uint" + } + } + }, + { + "name": "eth_feeHistory", + "summary": "Transaction fee history", + "description": "Returns transaction base fee per gas and effective priority fee per gas for the requested/supported block range.", + "params": [ + { + "name": "blockCount", + "description": "Requested range of blocks. Clients will return less than the requested range if not all blocks are available.", + "required": true, + "schema": { + "$ref": "#/components/schemas/uint" + } + }, + { + "name": "newestBlock", + "description": "Highest block of the requested range.", + "required": true, + "schema": { + "$ref": "#/components/schemas/BlockNumberOrTag" + } + }, + { + "name": "rewardPercentiles", + "description": "A monotonically increasing list of percentile values. For each block in the requested range, the transactions will be sorted in ascending order by effective tip per gas and the coresponding effective tip for the percentile will be determined, accounting for gas consumed.", + "required": true, + "schema": { + "title": "rewardPercentiles", + "type": "array", + "items": { + "title": "rewardPercentile", + "description": "Floating point value between 0 and 100.", + "type": "number" + } + } + } + ], + "result": { + "name": "feeHistoryResult", + "description": "Fee history for the returned block range. This can be a subsection of the requested range if not all blocks are available.", + "schema": { + "title": "feeHistoryResults", + "description": "Fee history results.", + "type": "object", + "required": [ + "oldestBlock", + "baseFeePerGas", + "gasUsedRatio" + ], + "additionalProperties": false, + "properties": { + "oldestBlock": { + "title": "oldestBlock", + "description": "Lowest number block of returned range.", + "$ref": "#/components/schemas/uint" + }, + "baseFeePerGas": { + "title": "baseFeePerGasArray", + "description": "An array of block base fees per gas. This includes the next block after the newest of the returned range, because this value can be derived from the newest block. Zeroes are returned for pre-EIP-1559 blocks.", + "type": "array", + "items": { + "$ref": "#/components/schemas/uint" + } + }, + "baseFeePerBlobGas": { + "title": "baseFeePerBlobGasArray", + "description": "An array of block base fees per blob gas. This includes the next block after the newest of the returned range, because this value can be derived from the newest block. Zeroes are returned for pre-EIP-4844 blocks.", + "type": "array", + "items": { + "$ref": "#/components/schemas/uint" + } + }, + "gasUsedRatio": { + "title": "gasUsedRatio", + "description": "An array of block gas used ratios. These are calculated as the ratio of gasUsed and gasLimit.", + "type": "array", + "items": { + "$ref": "#/components/schemas/ratio" + } + }, + "blobGasUsedRatio": { + "title": "blobGasUsedRatio", + "description": "An array of block blob gas used ratios. These are calculated as the ratio of blobGasUsed and the max blob gas per block.", + "type": "array", + "items": { + "$ref": "#/components/schemas/ratio" + } + }, + "reward": { + "title": "rewardArray", + "description": "A two-dimensional array of effective priority fees per gas at the requested block percentiles.", + "type": "array", + "items": { + "title": "rewardPercentile", + "description": "An array of effective priority fee per gas data points from a single block. All zeroes are returned if the block is empty.", + "type": "array", + "items": { + "title": "rewardPercentile", + "description": "A given percentile sample of effective priority fees per gas from a single block in ascending order, weighted by gas used. Zeroes are returned if the block is empty.", + "$ref": "#/components/schemas/uint" + } + } + } + } + } + } + }, + { + "name": "eth_gasPrice", + "summary": "Returns the current price per gas in wei.", + "params": [], + "result": { + "name": "Gas price", + "schema": { + "title": "Gas price", + "$ref": "#/components/schemas/uint" + } + } + }, + { + "name": "eth_getBalance", + "summary": "Returns the balance of the account of given address.", + "params": [ + { + "name": "Address", + "required": true, + "schema": { + "$ref": "#/components/schemas/address" + } + }, + { + "name": "Block", + "required": true, + "schema": { + "$ref": "#/components/schemas/BlockNumberOrTagOrHash" + } + } + ], + "result": { + "name": "Balance", + "schema": { + "$ref": "#/components/schemas/uint" + } + } + }, + { + "name": "eth_getBlockByHash", + "summary": "Returns information about a block by hash.", + "params": [ + { + "name": "Block hash", + "required": true, + "schema": { + "$ref": "#/components/schemas/hash32" + } + }, + { + "name": "Hydrated transactions", + "required": true, + "schema": { + "title": "hydrated", + "type": "boolean" + } + } + ], + "result": { + "name": "Block information", + "schema": { + "oneOf": [ + { + "$ref": "#/components/schemas/notFound" + }, + { + "$ref": "#/components/schemas/Block" + } + ] + } + } + }, + { + "name": "eth_getBlockByNumber", + "summary": "Returns information about a block by number.", + "params": [ + { + "name": "Block", + "required": true, + "schema": { + "$ref": "#/components/schemas/BlockNumberOrTag" + } + }, + { + "name": "Hydrated transactions", + "required": true, + "schema": { + "title": "hydrated", + "type": "boolean" + } + } + ], + "result": { + "name": "Block information", + "schema": { + "oneOf": [ + { + "$ref": "#/components/schemas/notFound" + }, + { + "$ref": "#/components/schemas/Block" + } + ] + } + } + }, + { + "name": "eth_getBlockReceipts", + "summary": "Returns the receipts of a block by number or hash.", + "params": [ + { + "name": "Block", + "required": true, + "schema": { + "$ref": "#/components/schemas/BlockNumberOrTagOrHash" + } + } + ], + "result": { + "name": "Receipts information", + "schema": { + "oneOf": [ + { + "$ref": "#/components/schemas/notFound" + }, + { + "title": "Receipts information", + "type": "array", + "items": { + "$ref": "#/components/schemas/ReceiptInfo" + } + } + ] + } + } + }, + { + "name": "eth_getBlockTransactionCountByHash", + "summary": "Returns the number of transactions in a block from a block matching the given block hash.", + "params": [ + { + "name": "Block hash", + "schema": { + "$ref": "#/components/schemas/hash32" + } + } + ], + "result": { + "name": "Transaction count", + "schema": { + "oneOf": [ + { + "$ref": "#/components/schemas/notFound" + }, + { + "title": "Transaction count", + "$ref": "#/components/schemas/uint" + } + ] + } + } + }, + { + "name": "eth_getBlockTransactionCountByNumber", + "summary": "Returns the number of transactions in a block matching the given block number.", + "params": [ + { + "name": "Block", + "schema": { + "$ref": "#/components/schemas/BlockNumberOrTag" + } + } + ], + "result": { + "name": "Transaction count", + "schema": { + "oneOf": [ + { + "$ref": "#/components/schemas/notFound" + }, + { + "title": "Transaction count", + "$ref": "#/components/schemas/uint" + } + ] + } + } + }, + { + "name": "eth_getCode", + "summary": "Returns code at a given address.", + "params": [ + { + "name": "Address", + "required": true, + "schema": { + "$ref": "#/components/schemas/address" + } + }, + { + "name": "Block", + "required": true, + "schema": { + "$ref": "#/components/schemas/BlockNumberOrTagOrHash" + } + } + ], + "result": { + "name": "Bytecode", + "schema": { + "$ref": "#/components/schemas/bytes" + } + } + }, + { + "name": "eth_getFilterChanges", + "summary": "Polling method for a filter, which returns an array of logs which occurred since last poll.", + "params": [ + { + "name": "Filter Identifier", + "schema": { + "$ref": "#/components/schemas/uint" + } + } + ], + "result": { + "name": "Log objects", + "schema": { + "$ref": "#/components/schemas/FilterResults" + } + } + }, + { + "name": "eth_getFilterLogs", + "summary": "Returns an array of all logs matching filter with given id.", + "params": [ + { + "name": "Filter Identifier", + "schema": { + "$ref": "#/components/schemas/uint" + } + } + ], + "result": { + "name": "Log objects", + "schema": { + "$ref": "#/components/schemas/FilterResults" + } + } + }, + { + "name": "eth_getLogs", + "summary": "Returns an array of all logs matching filter with given id.", + "params": [ + { + "name": "Filter", + "schema": { + "$ref": "#/components/schemas/Filter" + } + } + ], + "result": { + "name": "Log objects", + "schema": { + "$ref": "#/components/schemas/FilterResults" + } + } + }, + { + "name": "eth_getProof", + "summary": "Returns the merkle proof for a given account and optionally some storage keys.", + "params": [ + { + "name": "Address", + "required": true, + "schema": { + "$ref": "#/components/schemas/address" + } + }, + { + "name": "StorageKeys", + "required": true, + "schema": { + "title": "Storage keys", + "type": "array", + "items": { + "$ref": "#/components/schemas/bytesMax32" + } + } + }, + { + "name": "Block", + "required": true, + "schema": { + "$ref": "#/components/schemas/BlockNumberOrTagOrHash" + } + } + ], + "result": { + "name": "Account", + "schema": { + "$ref": "#/components/schemas/AccountProof" + } + } + }, + { + "name": "eth_getStorageAt", + "summary": "Returns the value from a storage position at a given address.", + "params": [ + { + "name": "Address", + "required": true, + "schema": { + "$ref": "#/components/schemas/address" + } + }, + { + "name": "Storage slot", + "required": true, + "schema": { + "$ref": "#/components/schemas/uint256" + } + }, + { + "name": "Block", + "required": true, + "schema": { + "$ref": "#/components/schemas/BlockNumberOrTagOrHash" + } + } + ], + "result": { + "name": "Value", + "schema": { + "$ref": "#/components/schemas/bytes" + } + } + }, + { + "name": "eth_getTransactionByBlockHashAndIndex", + "summary": "Returns information about a transaction by block hash and transaction index position.", + "params": [ + { + "name": "Block hash", + "required": true, + "schema": { + "$ref": "#/components/schemas/hash32" + } + }, + { + "name": "Transaction index", + "required": true, + "schema": { + "$ref": "#/components/schemas/uint" + } + } + ], + "result": { + "name": "Transaction information", + "schema": { + "oneOf": [ + { + "$ref": "#/components/schemas/notFound" + }, + { + "$ref": "#/components/schemas/TransactionInfo" + } + ] + } + } + }, + { + "name": "eth_getTransactionByBlockNumberAndIndex", + "summary": "Returns information about a transaction by block number and transaction index position.", + "params": [ + { + "name": "Block", + "required": true, + "schema": { + "$ref": "#/components/schemas/BlockNumberOrTag" + } + }, + { + "name": "Transaction index", + "required": true, + "schema": { + "$ref": "#/components/schemas/uint" + } + } + ], + "result": { + "name": "Transaction information", + "schema": { + "oneOf": [ + { + "$ref": "#/components/schemas/notFound" + }, + { + "$ref": "#/components/schemas/TransactionInfo" + } + ] + } + } + }, + { + "name": "eth_getTransactionByHash", + "summary": "Returns the information about a transaction requested by transaction hash.", + "params": [ + { + "name": "Transaction hash", + "required": true, + "schema": { + "$ref": "#/components/schemas/hash32" + } + } + ], + "result": { + "name": "Transaction information", + "schema": { + "oneOf": [ + { + "$ref": "#/components/schemas/notFound" + }, + { + "$ref": "#/components/schemas/TransactionInfo" + } + ] + } + } + }, + { + "name": "eth_getTransactionCount", + "summary": "Returns the number of transactions sent from an address.", + "params": [ + { + "name": "Address", + "required": true, + "schema": { + "$ref": "#/components/schemas/address" + } + }, + { + "name": "Block", + "required": true, + "schema": { + "$ref": "#/components/schemas/BlockNumberOrTagOrHash" + } + } + ], + "result": { + "name": "Transaction count", + "schema": { + "$ref": "#/components/schemas/uint" + } + } + }, + { + "name": "eth_getTransactionReceipt", + "summary": "Returns the receipt of a transaction by transaction hash.", + "params": [ + { + "name": "Transaction hash", + "required": true, + "schema": { + "$ref": "#/components/schemas/hash32" + } + } + ], + "result": { + "name": "Receipt information", + "schema": { + "oneOf": [ + { + "$ref": "#/components/schemas/notFound" + }, + { + "$ref": "#/components/schemas/ReceiptInfo" + } + ] + } + } + }, + { + "name": "eth_getUncleCountByBlockHash", + "summary": "Returns the number of uncles in a block from a block matching the given block hash.", + "params": [ + { + "name": "Block hash", + "schema": { + "$ref": "#/components/schemas/hash32" + } + } + ], + "result": { + "name": "Uncle count", + "schema": { + "oneOf": [ + { + "$ref": "#/components/schemas/notFound" + }, + { + "title": "Uncle count", + "$ref": "#/components/schemas/uint" + } + ] + } + } + }, + { + "name": "eth_getUncleCountByBlockNumber", + "summary": "Returns the number of transactions in a block matching the given block number.", + "params": [ + { + "name": "Block", + "schema": { + "$ref": "#/components/schemas/BlockNumberOrTag" + } + } + ], + "result": { + "name": "Uncle count", + "schema": { + "oneOf": [ + { + "$ref": "#/components/schemas/notFound" + }, + { + "title": "Uncle count", + "$ref": "#/components/schemas/uint" + } + ] + } + } + }, + { + "name": "eth_maxPriorityFeePerGas", + "summary": "Returns the current maxPriorityFeePerGas per gas in wei.", + "params": [], + "result": { + "name": "Max priority fee per gas", + "schema": { + "title": "Max priority fee per gas", + "$ref": "#/components/schemas/uint" + } + } + }, + { + "name": "eth_newBlockFilter", + "summary": "Creates a filter in the node, to notify when a new block arrives.", + "params": [], + "result": { + "name": "Filter Identifier", + "schema": { + "$ref": "#/components/schemas/uint" + } + } + }, + { + "name": "eth_newFilter", + "summary": "Creates a filter object, based on filter options, to notify when the state changes (logs).", + "params": [ + { + "name": "Filter", + "schema": { + "$ref": "#/components/schemas/Filter" + } + } + ], + "result": { + "name": "Filter Identifier", + "schema": { + "$ref": "#/components/schemas/uint" + } + } + }, + { + "name": "eth_newPendingTransactionFilter", + "summary": "Creates a filter in the node, to notify when new pending transactions arrive.", + "params": [], + "result": { + "name": "Filter Identifier", + "schema": { + "$ref": "#/components/schemas/uint" + } + } + }, + { + "name": "eth_sendRawTransaction", + "summary": "Submits a raw transaction. For EIP-4844 transactions, the raw form must be the network form. This means it includes the blobs, KZG commitments, and KZG proofs.", + "params": [ + { + "name": "Transaction", + "required": true, + "schema": { + "$ref": "#/components/schemas/bytes" + } + } + ], + "result": { + "name": "Transaction hash", + "schema": { + "$ref": "#/components/schemas/hash32" + } + } + }, + { + "name": "eth_sendTransaction", + "summary": "Signs and submits a transaction.", + "params": [ + { + "name": "Transaction", + "required": true, + "schema": { + "$ref": "#/components/schemas/GenericTransaction" + } + } + ], + "result": { + "name": "Transaction hash", + "schema": { + "$ref": "#/components/schemas/hash32" + } + } + }, + { + "name": "eth_sign", + "summary": "Returns an EIP-191 signature over the provided data.", + "params": [ + { + "name": "Address", + "required": true, + "schema": { + "$ref": "#/components/schemas/address" + } + }, + { + "name": "Message", + "required": true, + "schema": { + "$ref": "#/components/schemas/bytes" + } + } + ], + "result": { + "name": "Signature", + "schema": { + "$ref": "#/components/schemas/bytes65" + } + } + }, + { + "name": "eth_signTransaction", + "summary": "Returns an RLP encoded transaction signed by the specified account.", + "params": [ + { + "name": "Transaction", + "required": true, + "schema": { + "$ref": "#/components/schemas/GenericTransaction" + } + } + ], + "result": { + "name": "Encoded transaction", + "schema": { + "$ref": "#/components/schemas/bytes" + } + } + }, + { + "name": "eth_syncing", + "summary": "Returns an object with data about the sync status or false.", + "params": [], + "result": { + "name": "Syncing status", + "schema": { + "$ref": "#/components/schemas/SyncingStatus" + } + } + }, + { + "name": "eth_uninstallFilter", + "summary": "Uninstalls a filter with given id.", + "params": [ + { + "name": "Filter Identifier", + "schema": { + "$ref": "#/components/schemas/uint" + } + } + ], + "result": { + "name": "Success", + "schema": { + "type": "boolean" + } + } + } + ], + "components": { + "schemas": { + "address": { + "title": "hex encoded address", + "type": "string", + "pattern": "^0x[0-9a-fA-F]{40}$" + }, + "addresses": { + "title": "hex encoded address", + "type": "array", + "items": { + "$ref": "#/components/schemas/address" + } + }, + "byte": { + "title": "hex encoded byte", + "type": "string", + "pattern": "^0x([0-9a-fA-F]?){1,2}$" + }, + "bytes": { + "title": "hex encoded bytes", + "type": "string", + "pattern": "^0x[0-9a-f]*$" + }, + "bytesMax32": { + "title": "32 hex encoded bytes", + "type": "string", + "pattern": "^0x[0-9a-f]{0,64}$" + }, + "bytes8": { + "title": "8 hex encoded bytes", + "type": "string", + "pattern": "^0x[0-9a-f]{16}$" + }, + "bytes32": { + "title": "32 hex encoded bytes", + "type": "string", + "pattern": "^0x[0-9a-f]{64}$" + }, + "bytes48": { + "title": "48 hex encoded bytes", + "type": "string", + "pattern": "^0x[0-9a-f]{96}$" + }, + "bytes96": { + "title": "96 hex encoded bytes", + "type": "string", + "pattern": "^0x[0-9a-f]{192}$" + }, + "bytes256": { + "title": "256 hex encoded bytes", + "type": "string", + "pattern": "^0x[0-9a-f]{512}$" + }, + "bytes65": { + "title": "65 hex encoded bytes", + "type": "string", + "pattern": "^0x[0-9a-f]{130}$" + }, + "ratio": { + "title": "normalized ratio", + "type": "number", + "minimum": 0, + "maximum": 1 + }, + "uint": { + "title": "hex encoded unsigned integer", + "type": "string", + "pattern": "^0x([1-9a-f]+[0-9a-f]*|0)$" + }, + "uint64": { + "title": "hex encoded 64 bit unsigned integer", + "type": "string", + "pattern": "^0x([1-9a-f]+[0-9a-f]{0,15})|0$" + }, + "uint256": { + "title": "hex encoded 256 bit unsigned integer", + "type": "string", + "pattern": "^0x([1-9a-f]+[0-9a-f]{0,31})|0$" + }, + "hash32": { + "title": "32 byte hex value", + "type": "string", + "pattern": "^0x[0-9a-f]{64}$" + }, + "notFound": { + "title": "Not Found (null)", + "type": "null" + }, + "Block": { + "title": "Block object", + "type": "object", + "required": [ + "hash", + "parentHash", + "sha3Uncles", + "miner", + "stateRoot", + "transactionsRoot", + "receiptsRoot", + "logsBloom", + "number", + "gasLimit", + "gasUsed", + "timestamp", + "extraData", + "mixHash", + "nonce", + "size", + "transactions", + "uncles" + ], + "additionalProperties": false, + "properties": { + "hash": { + "title": "Hash", + "$ref": "#/components/schemas/hash32" + }, + "parentHash": { + "title": "Parent block hash", + "$ref": "#/components/schemas/hash32" + }, + "sha3Uncles": { + "title": "Ommers hash", + "$ref": "#/components/schemas/hash32" + }, + "miner": { + "title": "Coinbase", + "$ref": "#/components/schemas/address" + }, + "stateRoot": { + "title": "State root", + "$ref": "#/components/schemas/hash32" + }, + "transactionsRoot": { + "title": "Transactions root", + "$ref": "#/components/schemas/hash32" + }, + "receiptsRoot": { + "title": "Receipts root", + "$ref": "#/components/schemas/hash32" + }, + "logsBloom": { + "title": "Bloom filter", + "$ref": "#/components/schemas/bytes256" + }, + "difficulty": { + "title": "Difficulty", + "$ref": "#/components/schemas/uint" + }, + "number": { + "title": "Number", + "$ref": "#/components/schemas/uint" + }, + "gasLimit": { + "title": "Gas limit", + "$ref": "#/components/schemas/uint" + }, + "gasUsed": { + "title": "Gas used", + "$ref": "#/components/schemas/uint" + }, + "timestamp": { + "title": "Timestamp", + "$ref": "#/components/schemas/uint" + }, + "extraData": { + "title": "Extra data", + "$ref": "#/components/schemas/bytes" + }, + "mixHash": { + "title": "Mix hash", + "$ref": "#/components/schemas/hash32" + }, + "nonce": { + "title": "Nonce", + "$ref": "#/components/schemas/bytes8" + }, + "totalDifficulty": { + "title": "Total difficulty", + "$ref": "#/components/schemas/uint" + }, + "baseFeePerGas": { + "title": "Base fee per gas", + "$ref": "#/components/schemas/uint" + }, + "withdrawalsRoot": { + "title": "Withdrawals root", + "$ref": "#/components/schemas/hash32" + }, + "blobGasUsed": { + "title": "Blob gas used", + "$ref": "#/components/schemas/uint" + }, + "excessBlobGas": { + "title": "Excess blob gas", + "$ref": "#/components/schemas/uint" + }, + "parentBeaconBlockRoot": { + "title": "Parent Beacon Block Root", + "$ref": "#/components/schemas/hash32" + }, + "size": { + "title": "Block size", + "$ref": "#/components/schemas/uint" + }, + "transactions": { + "anyOf": [ + { + "title": "Transaction hashes", + "type": "array", + "items": { + "$ref": "#/components/schemas/hash32" + } + }, + { + "title": "Full transactions", + "type": "array", + "items": { + "$ref": "#/components/schemas/TransactionInfo" + } + } + ] + }, + "withdrawals": { + "title": "Withdrawals", + "type": "array", + "items": { + "$ref": "#/components/schemas/Withdrawal" + } + }, + "uncles": { + "title": "Uncles", + "type": "array", + "items": { + "$ref": "#/components/schemas/hash32" + } + } + } + }, + "BlockTag": { + "title": "Block tag", + "type": "string", + "enum": [ + "earliest", + "finalized", + "safe", + "latest", + "pending" + ], + "description": "`earliest`: The lowest numbered block the client has available; `finalized`: The most recent crypto-economically secure block, cannot be re-orged outside of manual intervention driven by community coordination; `safe`: The most recent block that is safe from re-orgs under honest majority and certain synchronicity assumptions; `latest`: The most recent block in the canonical chain observed by the client, this block may be re-orged out of the canonical chain even under healthy/normal conditions; `pending`: A sample next block built by the client on top of `latest` and containing the set of transactions usually taken from local mempool. Before the merge transition is finalized, any call querying for `finalized` or `safe` block MUST be responded to with `-39001: Unknown block` error" + }, + "BlockNumberOrTag": { + "title": "Block number or tag", + "oneOf": [ + { + "title": "Block number", + "$ref": "#/components/schemas/uint" + }, + { + "title": "Block tag", + "$ref": "#/components/schemas/BlockTag" + } + ] + }, + "BlockNumberOrTagOrHash": { + "title": "Block number, tag, or block hash", + "anyOf": [ + { + "title": "Block number", + "$ref": "#/components/schemas/uint" + }, + { + "title": "Block tag", + "$ref": "#/components/schemas/BlockTag" + }, + { + "title": "Block hash", + "$ref": "#/components/schemas/hash32" + } + ] + }, + "BadBlock": { + "title": "Bad block", + "type": "object", + "required": [ + "block", + "hash", + "rlp" + ], + "additionalProperties": false, + "properties": { + "block": { + "title": "Block", + "$ref": "#/components/schemas/Block" + }, + "hash": { + "title": "Hash", + "$ref": "#/components/schemas/hash32" + }, + "rlp": { + "title": "RLP", + "$ref": "#/components/schemas/bytes" + } + } + }, + "SyncingStatus": { + "title": "Syncing status", + "oneOf": [ + { + "title": "Syncing progress", + "type": "object", + "additionalProperties": false, + "properties": { + "startingBlock": { + "title": "Starting block", + "$ref": "#/components/schemas/uint" + }, + "currentBlock": { + "title": "Current block", + "$ref": "#/components/schemas/uint" + }, + "highestBlock": { + "title": "Highest block", + "$ref": "#/components/schemas/uint" + } + } + }, + { + "title": "Not syncing", + "description": "Should always return false if not syncing.", + "type": "boolean" + } + ] + }, + "FilterResults": { + "title": "Filter results", + "oneOf": [ + { + "title": "new block or transaction hashes", + "type": "array", + "items": { + "$ref": "#/components/schemas/hash32" + } + }, + { + "title": "new logs", + "type": "array", + "items": { + "$ref": "#/components/schemas/Log" + } + } + ] + }, + "Filter": { + "title": "filter", + "type": "object", + "additionalProperties": false, + "properties": { + "fromBlock": { + "title": "from block", + "$ref": "#/components/schemas/uint" + }, + "toBlock": { + "title": "to block", + "$ref": "#/components/schemas/uint" + }, + "address": { + "title": "Address(es)", + "oneOf": [ + { + "title": "Any Address", + "type": "null" + }, + { + "title": "Address", + "$ref": "#/components/schemas/address" + }, + { + "title": "Addresses", + "$ref": "#/components/schemas/addresses" + } + ] + }, + "topics": { + "title": "Topics", + "$ref": "#/components/schemas/FilterTopics" + } + } + }, + "FilterTopics": { + "title": "Filter Topics", + "oneOf": [ + { + "title": "Any Topic Match", + "type": "null" + }, + { + "title": "Specified Filter Topics", + "type": "array", + "items": { + "$ref": "#/components/schemas/FilterTopic" + } + } + ] + }, + "FilterTopic": { + "title": "Filter Topic List Entry", + "oneOf": [ + { + "title": "Single Topic Match", + "$ref": "#/components/schemas/bytes32" + }, + { + "title": "Multiple Topic Match", + "type": "array", + "items": { + "$ref": "#/components/schemas/bytes32" + } + } + ] + }, + "Log": { + "title": "log", + "type": "object", + "required": [ + "transactionHash" + ], + "additionalProperties": false, + "properties": { + "removed": { + "title": "removed", + "type": "boolean" + }, + "logIndex": { + "title": "log index", + "$ref": "#/components/schemas/uint" + }, + "transactionIndex": { + "title": "transaction index", + "$ref": "#/components/schemas/uint" + }, + "transactionHash": { + "title": "transaction hash", + "$ref": "#/components/schemas/hash32" + }, + "blockHash": { + "title": "block hash", + "$ref": "#/components/schemas/hash32" + }, + "blockNumber": { + "title": "block number", + "$ref": "#/components/schemas/uint" + }, + "address": { + "title": "address", + "$ref": "#/components/schemas/address" + }, + "data": { + "title": "data", + "$ref": "#/components/schemas/bytes" + }, + "topics": { + "title": "topics", + "type": "array", + "items": { + "$ref": "#/components/schemas/bytes32" + } + } + } + }, + "ReceiptInfo": { + "type": "object", + "title": "Receipt information", + "required": [ + "blockHash", + "blockNumber", + "from", + "cumulativeGasUsed", + "gasUsed", + "logs", + "logsBloom", + "transactionHash", + "transactionIndex", + "effectiveGasPrice" + ], + "additionalProperties": false, + "properties": { + "type": { + "title": "type", + "$ref": "#/components/schemas/byte" + }, + "transactionHash": { + "title": "transaction hash", + "$ref": "#/components/schemas/hash32" + }, + "transactionIndex": { + "title": "transaction index", + "$ref": "#/components/schemas/uint" + }, + "blockHash": { + "title": "block hash", + "$ref": "#/components/schemas/hash32" + }, + "blockNumber": { + "title": "block number", + "$ref": "#/components/schemas/uint" + }, + "from": { + "title": "from", + "$ref": "#/components/schemas/address" + }, + "to": { + "title": "to", + "description": "Address of the receiver or null in a contract creation transaction.", + "oneOf": [ + { + "title": "Contract Creation (null)", + "type": "null" + }, + { + "title": "Recipient Address", + "$ref": "#/components/schemas/address" + } + ] + }, + "cumulativeGasUsed": { + "title": "cumulative gas used", + "description": "The sum of gas used by this transaction and all preceding transactions in the same block.", + "$ref": "#/components/schemas/uint" + }, + "gasUsed": { + "title": "gas used", + "description": "The amount of gas used for this specific transaction alone.", + "$ref": "#/components/schemas/uint" + }, + "blobGasUsed": { + "title": "blob gas used", + "description": "The amount of blob gas used for this specific transaction. Only specified for blob transactions as defined by EIP-4844.", + "$ref": "#/components/schemas/uint" + }, + "contractAddress": { + "title": "contract address", + "description": "The contract address created, if the transaction was a contract creation, otherwise null.", + "oneOf": [ + { + "$ref": "#/components/schemas/address" + }, + { + "title": "Null", + "type": "null" + } + ] + }, + "logs": { + "title": "logs", + "type": "array", + "items": { + "$ref": "#/components/schemas/Log" + } + }, + "logsBloom": { + "title": "logs bloom", + "$ref": "#/components/schemas/bytes256" + }, + "root": { + "title": "state root", + "description": "The post-transaction state root. Only specified for transactions included before the Byzantium upgrade.", + "$ref": "#/components/schemas/hash32" + }, + "status": { + "title": "status", + "description": "Either 1 (success) or 0 (failure). Only specified for transactions included after the Byzantium upgrade.", + "$ref": "#/components/schemas/uint" + }, + "effectiveGasPrice": { + "title": "effective gas price", + "description": "The actual value per gas deducted from the sender's account. Before EIP-1559, this is equal to the transaction's gas price. After, it is equal to baseFeePerGas + min(maxFeePerGas - baseFeePerGas, maxPriorityFeePerGas).", + "$ref": "#/components/schemas/uint" + }, + "blobGasPrice": { + "title": "blob gas price", + "description": "The actual value per gas deducted from the sender's account for blob gas. Only specified for blob transactions as defined by EIP-4844.", + "$ref": "#/components/schemas/uint" + } + } + }, + "AccountProof": { + "title": "Account proof", + "type": "object", + "required": [ + "address", + "accountProof", + "balance", + "codeHash", + "nonce", + "storageHash", + "storageProof" + ], + "additionalProperties": false, + "properties": { + "address": { + "title": "address", + "$ref": "#/components/schemas/address" + }, + "accountProof": { + "title": "accountProof", + "type": "array", + "items": { + "$ref": "#/components/schemas/bytes" + } + }, + "balance": { + "title": "balance", + "$ref": "#/components/schemas/uint256" + }, + "codeHash": { + "title": "codeHash", + "$ref": "#/components/schemas/hash32" + }, + "nonce": { + "title": "nonce", + "$ref": "#/components/schemas/uint64" + }, + "storageHash": { + "title": "storageHash", + "$ref": "#/components/schemas/hash32" + }, + "storageProof": { + "title": "Storage proofs", + "type": "array", + "items": { + "$ref": "#/components/schemas/StorageProof" + } + } + } + }, + "StorageProof": { + "title": "Storage proof", + "type": "object", + "required": [ + "key", + "value", + "proof" + ], + "additionalProperties": false, + "properties": { + "key": { + "title": "key", + "$ref": "#/components/schemas/bytesMax32" + }, + "value": { + "title": "value", + "$ref": "#/components/schemas/uint256" + }, + "proof": { + "title": "proof", + "type": "array", + "items": { + "$ref": "#/components/schemas/bytes" + } + } + } + }, + "Transaction4844Unsigned": { + "type": "object", + "title": "EIP-4844 transaction.", + "required": [ + "type", + "nonce", + "to", + "gas", + "value", + "input", + "maxPriorityFeePerGas", + "maxFeePerGas", + "maxFeePerBlobGas", + "accessList", + "blobVersionedHashes", + "chainId" + ], + "properties": { + "type": { + "title": "type", + "type": "string", + "pattern": "^0x3$" + }, + "nonce": { + "title": "nonce", + "$ref": "#/components/schemas/uint" + }, + "to": { + "title": "to address", + "$ref": "#/components/schemas/address" + }, + "gas": { + "title": "gas limit", + "$ref": "#/components/schemas/uint" + }, + "value": { + "title": "value", + "$ref": "#/components/schemas/uint" + }, + "input": { + "title": "input data", + "$ref": "#/components/schemas/bytes" + }, + "maxPriorityFeePerGas": { + "title": "max priority fee per gas", + "description": "Maximum fee per gas the sender is willing to pay to miners in wei", + "$ref": "#/components/schemas/uint" + }, + "maxFeePerGas": { + "title": "max fee per gas", + "description": "The maximum total fee per gas the sender is willing to pay (includes the network / base fee and miner / priority fee) in wei", + "$ref": "#/components/schemas/uint" + }, + "maxFeePerBlobGas": { + "title": "max fee per blob gas", + "description": "The maximum total fee per gas the sender is willing to pay for blob gas in wei", + "$ref": "#/components/schemas/uint" + }, + "accessList": { + "title": "accessList", + "description": "EIP-2930 access list", + "$ref": "#/components/schemas/AccessList" + }, + "blobVersionedHashes": { + "title": "blobVersionedHashes", + "description": "List of versioned blob hashes associated with the transaction's EIP-4844 data blobs.", + "type": "array", + "items": { + "$ref": "#/components/schemas/hash32" + } + }, + "chainId": { + "title": "chainId", + "description": "Chain ID that this transaction is valid on.", + "$ref": "#/components/schemas/uint" + } + } + }, + "AccessListEntry": { + "title": "Access list entry", + "type": "object", + "additionalProperties": false, + "required": [ "address", "storageKeys" ], + "properties": { + "address": { + "$ref": "#/components/schemas/address" + }, + "storageKeys": { + "type": "array", + "items": { + "$ref": "#/components/schemas/hash32" + } + } + } + }, + "AccessList": { + "title": "Access list", + "type": "array", + "items": { + "$ref": "#/components/schemas/AccessListEntry" + } + }, + "Transaction1559Unsigned": { + "type": "object", + "title": "EIP-1559 transaction.", + "required": [ + "type", + "nonce", + "gas", + "value", + "input", + "maxFeePerGas", + "maxPriorityFeePerGas", + "gasPrice", + "chainId", + "accessList" + ], + "properties": { + "type": { + "title": "type", + "type": "string", + "pattern": "^0x2$" + }, + "nonce": { + "title": "nonce", + "$ref": "#/components/schemas/uint" + }, + "to": { + "title": "to address", + "oneOf": [ + { + "title": "Contract Creation (null)", + "type": "null" + }, + { + "title": "Address", + "$ref": "#/components/schemas/address" + } + ] + }, + "gas": { + "title": "gas limit", + "$ref": "#/components/schemas/uint" + }, + "value": { + "title": "value", + "$ref": "#/components/schemas/uint" + }, + "input": { + "title": "input data", + "$ref": "#/components/schemas/bytes" + }, + "maxPriorityFeePerGas": { + "title": "max priority fee per gas", + "description": "Maximum fee per gas the sender is willing to pay to miners in wei", + "$ref": "#/components/schemas/uint" + }, + "maxFeePerGas": { + "title": "max fee per gas", + "description": "The maximum total fee per gas the sender is willing to pay (includes the network / base fee and miner / priority fee) in wei", + "$ref": "#/components/schemas/uint" + }, + "gasPrice": { + "title": "gas price", + "description": "The effective gas price paid by the sender in wei. For transactions not yet included in a block, this value should be set equal to the max fee per gas. This field is DEPRECATED, please transition to using effectiveGasPrice in the receipt object going forward.", + "$ref": "#/components/schemas/uint" + }, + "accessList": { + "title": "accessList", + "description": "EIP-2930 access list", + "$ref": "#/components/schemas/AccessList" + }, + "chainId": { + "title": "chainId", + "description": "Chain ID that this transaction is valid on.", + "$ref": "#/components/schemas/uint" + } + } + }, + "Transaction2930Unsigned": { + "type": "object", + "title": "EIP-2930 transaction.", + "required": [ + "type", + "nonce", + "gas", + "value", + "input", + "gasPrice", + "chainId", + "accessList" + ], + "properties": { + "type": { + "title": "type", + "type": "string", + "pattern": "^0x1$" + }, + "nonce": { + "title": "nonce", + "$ref": "#/components/schemas/uint" + }, + "to": { + "title": "to address", + "oneOf": [ + { + "title": "Contract Creation (null)", + "type": "null" + }, + { + "title": "Address", + "$ref": "#/components/schemas/address" + } + ] + }, + "gas": { + "title": "gas limit", + "$ref": "#/components/schemas/uint" + }, + "value": { + "title": "value", + "$ref": "#/components/schemas/uint" + }, + "input": { + "title": "input data", + "$ref": "#/components/schemas/bytes" + }, + "gasPrice": { + "title": "gas price", + "description": "The gas price willing to be paid by the sender in wei", + "$ref": "#/components/schemas/uint" + }, + "accessList": { + "title": "accessList", + "description": "EIP-2930 access list", + "$ref": "#/components/schemas/AccessList" + }, + "chainId": { + "title": "chainId", + "description": "Chain ID that this transaction is valid on.", + "$ref": "#/components/schemas/uint" + } + } + }, + "TransactionLegacyUnsigned": { + "type": "object", + "title": "Legacy transaction.", + "required": [ + "type", + "nonce", + "gas", + "value", + "input", + "gasPrice" + ], + "properties": { + "type": { + "title": "type", + "type": "string", + "pattern": "^0x0$" + }, + "nonce": { + "title": "nonce", + "$ref": "#/components/schemas/uint" + }, + "to": { + "title": "to address", + "oneOf": [ + { + "title": "Contract Creation (null)", + "type": "null" + }, + { + "title": "Address", + "$ref": "#/components/schemas/address" + } + ] + }, + "gas": { + "title": "gas limit", + "$ref": "#/components/schemas/uint" + }, + "value": { + "title": "value", + "$ref": "#/components/schemas/uint" + }, + "input": { + "title": "input data", + "$ref": "#/components/schemas/bytes" + }, + "gasPrice": { + "title": "gas price", + "description": "The gas price willing to be paid by the sender in wei", + "$ref": "#/components/schemas/uint" + }, + "chainId": { + "title": "chainId", + "description": "Chain ID that this transaction is valid on.", + "$ref": "#/components/schemas/uint" + } + } + }, + "TransactionUnsigned": { + "oneOf": [ + { + "$ref": "#/components/schemas/Transaction4844Unsigned" + }, + { + "$ref": "#/components/schemas/Transaction1559Unsigned" + }, + { + "$ref": "#/components/schemas/Transaction2930Unsigned" + }, + { + "$ref": "#/components/schemas/TransactionLegacyUnsigned" + } + ] + }, + "Transaction4844Signed": { + "title": "Signed 4844 Transaction", + "type": "object", + "allOf": [ + { + "$ref": "#/components/schemas/Transaction4844Unsigned" + }, + { + "title": "EIP-4844 transaction signature properties.", + "required": [ + "r", + "s" + ], + "properties": { + "yParity": { + "title": "yParity", + "description": "The parity (0 for even, 1 for odd) of the y-value of the secp256k1 signature.", + "$ref": "#/components/schemas/uint" + }, + "r": { + "title": "r", + "$ref": "#/components/schemas/uint" + }, + "s": { + "title": "s", + "$ref": "#/components/schemas/uint" + } + } + } + ] + }, + "Transaction1559Signed": { + "title": "Signed 1559 Transaction", + "type": "object", + "allOf": [ + { + "$ref": "#/components/schemas/Transaction1559Unsigned" + }, + { + "title": "EIP-1559 transaction signature properties.", + "required": [ + "r", + "s" + ], + "properties": { + "yParity": { + "title": "yParity", + "description": "The parity (0 for even, 1 for odd) of the y-value of the secp256k1 signature.", + "$ref": "#/components/schemas/uint" + }, + "v": { + "title": "v", + "description": "For backwards compatibility, `v` is optionally provided as an alternative to `yParity`. This field is DEPRECATED and all use of it should migrate to `yParity`.", + "$ref": "#/components/schemas/uint" + }, + "r": { + "title": "r", + "$ref": "#/components/schemas/uint" + }, + "s": { + "title": "s", + "$ref": "#/components/schemas/uint" + } + } + } + ] + }, + "Transaction2930Signed": { + "title": "Signed 2930 Transaction", + "type": "object", + "allOf": [ + { + "$ref": "#/components/schemas/Transaction2930Unsigned" + }, + { + "title": "EIP-2930 transaction signature properties.", + "required": [ + "yParity", + "r", + "s" + ], + "properties": { + "yParity": { + "title": "yParity", + "description": "The parity (0 for even, 1 for odd) of the y-value of the secp256k1 signature.", + "$ref": "#/components/schemas/uint" + }, + "v": { + "title": "v", + "description": "For backwards compatibility, `v` is optionally provided as an alternative to `yParity`. This field is DEPRECATED and all use of it should migrate to `yParity`.", + "$ref": "#/components/schemas/uint" + }, + "r": { + "title": "r", + "$ref": "#/components/schemas/uint" + }, + "s": { + "title": "s", + "$ref": "#/components/schemas/uint" + } + } + } + ] + }, + "TransactionLegacySigned": { + "title": "Signed Legacy Transaction", + "type": "object", + "allOf": [ + { + "$ref": "#/components/schemas/TransactionLegacyUnsigned" + }, + { + "title": "Legacy transaction signature properties.", + "required": [ + "v", + "r", + "s" + ], + "properties": { + "v": { + "title": "v", + "$ref": "#/components/schemas/uint" + }, + "r": { + "title": "r", + "$ref": "#/components/schemas/uint" + }, + "s": { + "title": "s", + "$ref": "#/components/schemas/uint" + } + } + } + ] + }, + "TransactionSigned": { + "oneOf": [ + { + "$ref": "#/components/schemas/Transaction4844Signed" + }, + { + "$ref": "#/components/schemas/Transaction1559Signed" + }, + { + "$ref": "#/components/schemas/Transaction2930Signed" + }, + { + "$ref": "#/components/schemas/TransactionLegacySigned" + } + ] + }, + "TransactionInfo": { + "type": "object", + "title": "Transaction information", + "allOf": [ + { + "title": "Contextual information", + "required": [ + "blockHash", + "blockNumber", + "from", + "hash", + "transactionIndex" + ], + "unevaluatedProperties": false, + "properties": { + "blockHash": { + "title": "block hash", + "$ref": "#/components/schemas/hash32" + }, + "blockNumber": { + "title": "block number", + "$ref": "#/components/schemas/uint" + }, + "from": { + "title": "from address", + "$ref": "#/components/schemas/address" + }, + "hash": { + "title": "transaction hash", + "$ref": "#/components/schemas/hash32" + }, + "transactionIndex": { + "title": "transaction index", + "$ref": "#/components/schemas/uint" + } + } + }, + { + "$ref": "#/components/schemas/TransactionSigned" + } + ] + }, + "GenericTransaction": { + "type": "object", + "title": "Transaction object generic to all types", + "additionalProperties": false, + "properties": { + "type": { + "title": "type", + "$ref": "#/components/schemas/byte" + }, + "nonce": { + "title": "nonce", + "$ref": "#/components/schemas/uint" + }, + "to": { + "title": "to address", + "oneOf": [ + { + "title": "Contract Creation (null)", + "type": "null" + }, + { + "title": "Address", + "$ref": "#/components/schemas/address" + } + ] + }, + "from": { + "title": "from address", + "$ref": "#/components/schemas/address" + }, + "gas": { + "title": "gas limit", + "$ref": "#/components/schemas/uint" + }, + "value": { + "title": "value", + "$ref": "#/components/schemas/uint" + }, + "input": { + "title": "input data", + "$ref": "#/components/schemas/bytes" + }, + "gasPrice": { + "title": "gas price", + "description": "The gas price willing to be paid by the sender in wei", + "$ref": "#/components/schemas/uint" + }, + "maxPriorityFeePerGas": { + "title": "max priority fee per gas", + "description": "Maximum fee per gas the sender is willing to pay to miners in wei", + "$ref": "#/components/schemas/uint" + }, + "maxFeePerGas": { + "title": "max fee per gas", + "description": "The maximum total fee per gas the sender is willing to pay (includes the network / base fee and miner / priority fee) in wei", + "$ref": "#/components/schemas/uint" + }, + "maxFeePerBlobGas": { + "title": "max fee per blob gas", + "description": "The maximum total fee per gas the sender is willing to pay for blob gas in wei", + "$ref": "#/components/schemas/uint" + }, + "accessList": { + "title": "accessList", + "description": "EIP-2930 access list", + "$ref": "#/components/schemas/AccessList" + }, + "blobVersionedHashes": { + "title": "blobVersionedHashes", + "description": "List of versioned blob hashes associated with the transaction's EIP-4844 data blobs.", + "type": "array", + "items": { + "$ref": "#/components/schemas/hash32" + } + }, + "blobs": { + "title": "blobs", + "description": "Raw blob data.", + "type": "array", + "items": { + "$ref": "#/components/schemas/bytes" + } + }, + "chainId": { + "title": "chainId", + "description": "Chain ID that this transaction is valid on.", + "$ref": "#/components/schemas/uint" + } + } + }, + "Withdrawal": { + "type": "object", + "title": "Validator withdrawal", + "required": [ + "index", + "validatorIndex", + "address", + "amount" + ], + "additionalProperties": false, + "properties": { + "index": { + "title": "index of withdrawal", + "$ref": "#/components/schemas/uint64" + }, + "validatorIndex": { + "title": "index of validator that generated withdrawal", + "$ref": "#/components/schemas/uint64" + }, + "address": { + "title": "recipient address for withdrawal value", + "$ref": "#/components/schemas/address" + }, + "amount": { + "title": "value contained in withdrawal", + "$ref": "#/components/schemas/uint256" + } + } + } + } + } +} diff --git a/substrate/frame/revive/rpc/codegen/src/LICENSE.txt b/substrate/frame/revive/rpc/codegen/src/LICENSE.txt new file mode 100644 index 000000000000..ecd364a6d62e --- /dev/null +++ b/substrate/frame/revive/rpc/codegen/src/LICENSE.txt @@ -0,0 +1,16 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. diff --git a/substrate/frame/revive/rpc/codegen/src/generator.rs b/substrate/frame/revive/rpc/codegen/src/generator.rs new file mode 100644 index 000000000000..6419a994b6d3 --- /dev/null +++ b/substrate/frame/revive/rpc/codegen/src/generator.rs @@ -0,0 +1,740 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +use inflector::Inflector; +use std::{ + collections::{BTreeMap, HashMap, HashSet}, + mem, + sync::LazyLock, +}; + +use crate::{ + open_rpc::*, + printer::{ + doc_str_from_schema, Fields, Required, TypeContent, TypeInfo, TypeNameProvider, + TypePrinter, Variants, + }, + writeln, +}; + +pub const LICENSE: &str = include_str!("LICENSE.txt"); + +/// List of supported Ethereum RPC methods we want to generate. +pub static SUPPORTED_ETH_METHODS: LazyLock> = LazyLock::new(|| { + vec![ + "eth_accounts", + "eth_blockNumber", + "eth_call", + "eth_chainId", + "eth_estimateGas", + "eth_gasPrice", + "eth_getBalance", + "eth_getBlockByHash", + "eth_getBlockByNumber", + "eth_getBlockTransactionCountByHash", + "eth_getBlockTransactionCountByNumber", + "eth_getCode", + "eth_getStorageAt", + "eth_getTransactionByBlockHashAndIndex", + "eth_getTransactionByBlockNumberAndIndex", + "eth_getTransactionByHash", + "eth_getTransactionCount", + "eth_getTransactionReceipt", + "eth_sendRawTransaction", + "eth_sendTransaction", + "eth_syncing", + "net_version", + ] +}); + +/// Mapping of primitive schema types to their Rust counterparts. +pub static PRIMITIVE_MAPPINGS: LazyLock> = + LazyLock::new(|| { + HashMap::from([ + ("#/components/schemas/address", "Address"), + ("#/components/schemas/byte", "Byte"), + ("#/components/schemas/bytes", "Bytes"), + ("#/components/schemas/bytes256", "Bytes256"), + ("#/components/schemas/hash32", "H256"), + ("#/components/schemas/bytes32", "H256"), + ("#/components/schemas/bytes8", "Bytes8"), + ("#/components/schemas/uint", "U256"), + ("#/components/schemas/uint256", "U256"), + ("#/components/schemas/uint64", "U256"), + ]) + }); + +/// Mapping of legacy aliases to their new names. +pub static LEGACY_ALIASES: LazyLock>> = + LazyLock::new(|| { + HashMap::from([ + // We accept "data" and "input" for backwards-compatibility reasons. + // Issue detail: https://github.com/ethereum/go-ethereum/issues/15628 + ("#/components/schemas/GenericTransaction", HashMap::from([("input", "data")])), + ]) + }); + +/// Read the OpenRPC specs, and inject extra methods and legacy aliases. +pub fn read_specs() -> anyhow::Result { + let content = include_str!("../openrpc.json"); + let mut specs: OpenRpc = serde_json::from_str(content)?; + + // Inject legacy aliases. + inject_legacy_aliases(&mut specs); + + // Inject extra methods. + specs.methods.push(RefOr::Inline(Method { + name: "net_version".to_string(), + summary: Some("The string value of current network id".to_string()), + result: Some(RefOr::Reference { reference: "String".to_string() }), + ..Default::default() + })); + + Ok(specs) +} + +// Inject legacy aliases declared by [`LEGACY_ALIASES`]. +pub fn inject_legacy_aliases(specs: &mut OpenRpc) { + for (alias, mapping) in LEGACY_ALIASES.iter() { + let schema = specs.get_schema_mut(alias).unwrap(); + match &mut schema.contents { + SchemaContents::Object(o) | SchemaContents::Literal(Literal::Object(o)) => { + o.legacy_aliases = + mapping.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect(); + }, + _ => { + panic!("Alias should be an object got {:?} instead", schema.contents); + }, + } + } +} + +/// Format the given code using rustfmt. +pub fn format_code(code: &str) -> anyhow::Result { + use std::{io::Write, process::*}; + let mut rustfmt = Command::new("rustup") + .args(["run", "nightly", "rustfmt"]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn()?; + + let stdin = rustfmt.stdin.as_mut().expect("Failed to open stdin"); + stdin.write_all(code.as_bytes())?; + + let output = rustfmt.wait_with_output()?; + if !output.status.success() { + anyhow::bail!("rustfmt failed: {}", String::from_utf8_lossy(&output.stderr)); + } + + let formatted_code = String::from_utf8_lossy(&output.stdout).to_string(); + Ok(formatted_code) +} + +/// Type generator for generating RPC methods and types. +#[derive(Default)] +pub struct TypeGenerator { + /// List of collected types, that are not yet generated. + collected: BTreeMap, + /// List of already generated types. + generated: HashSet, + /// List of filtered method names, we want to generate. + filtered_method_names: HashSet, + /// Stripped prefix for the generated method names. + prefix: String, +} + +/// Reference or schema +pub enum ReferenceOrSchema { + // A reference to a schema such as `#/components/schemas/Foo`. + Reference(String), + // A schema definition. + Schema(Schema), +} + +impl ReferenceOrSchema { + /// Return the schema for the reference or the schema itself. + fn schema<'a>(&'a self, specs: &'a OpenRpc) -> &'a Schema { + match self { + Self::Schema(schema) => schema, + Self::Reference(reference) => specs.get_schema(reference).unwrap(), + } + } +} + +impl TypeGenerator { + /// Create a new type generator. + pub fn new() -> Self { + let mut generated = + HashSet::from_iter(["notFound"].into_iter().map(|name| name.to_pascal_case())); + + generated.extend(PRIMITIVE_MAPPINGS.keys().map(|name| reference_to_name(name))); + generated.extend(PRIMITIVE_MAPPINGS.values().map(|name| name.to_string())); + let filtered_method_names = + SUPPORTED_ETH_METHODS.iter().map(|name| name.to_string()).collect(); + + Self { + collected: Default::default(), + filtered_method_names, + generated, + prefix: "eth".to_string(), + } + } + + /// Generate the RPC method, and add the collected types. + pub fn generate_rpc_methods(&mut self, specs: &OpenRpc) -> String { + let methods = specs + .methods + .iter() + .map(RefOr::unwrap_inline) + .filter(|method| self.filtered_method_names.contains(&method.name)) + .collect::>(); + + if methods.len() != self.filtered_method_names.len() { + let available = + methods.iter().map(|method| method.name.clone()).collect::>(); + let missing = self.filtered_method_names.difference(&available).collect::>(); + panic!("Missing methods: {missing:?}"); + } + + let mut code = LICENSE.to_string(); + code.push_str( + r#" + //! Generated JSON-RPC methods. + #![allow(missing_docs)] + + use super::*; + use jsonrpsee::core::RpcResult; + use jsonrpsee::proc_macros::rpc; + + #[rpc(server, client)] + pub trait EthRpc { + "#, + ); + + for method in methods { + self.generate_rpc_method(&mut code, method); + code.push('\n'); + } + code.push('}'); + code.push('\n'); + code + } + + pub fn collect_extra_type(&mut self, type_name: &str) { + self.collect( + type_name, + ReferenceOrSchema::Reference(format!("#/components/schemas/{}", type_name)), + ); + } + + /// Recursively collect the types and generate them. + /// + /// Note: This should be called after [`TypeGenerator::generate_rpc_methods`] to collect the + /// types used in the RPC methods. + pub fn generate_types(&mut self, specs: &OpenRpc) -> String { + let mut code = LICENSE.to_string(); + code.push_str( + r#" + //! Generated JSON-RPC types. + #![allow(missing_docs)] + + use super::{byte::*, Type0, Type1, Type2, Type3}; + use alloc::vec::Vec; + use codec::{Decode, Encode}; + use derive_more::{From, TryInto}; + pub use ethereum_types::*; + use scale_info::TypeInfo; + use serde::{Deserialize, Serialize}; + + "#, + ); + loop { + let collected = mem::take(&mut self.collected); + self.generated.extend(collected.keys().cloned()); + + if collected.is_empty() { + break; + } + + for (name, ref_or_schema) in collected { + let r#type = self.generate_type(name, ref_or_schema.schema(specs)); + r#type.print(&mut code); + code.push('\n'); + } + } + + code + } + + /// Return the type printer for the given schema. + fn generate_type(&mut self, name: String, schema: &Schema) -> TypePrinter { + let doc = doc_str_from_schema(schema); + + let content = match &schema.contents { + &SchemaContents::Literal(Literal::Object(ref o)) | &SchemaContents::Object(ref o) => + TypeContent::Struct(Fields::from(o, self)), + SchemaContents::AllOf { all_of } => + TypeContent::Struct(Fields::from_all_of(all_of, self)), + &SchemaContents::AnyOf { any_of: ref items } | + &SchemaContents::OneOf { one_of: ref items } => + TypeContent::Enum(Variants::from_one_of(items, self)), + &SchemaContents::Literal(Literal::Array(ArrayLiteral { items: Some(ref schema) })) => { + let mut type_info = + self.type_info(schema).expect("Anonymous array type not supported"); + type_info.array = true; + + TypeContent::TypeAlias(type_info) + }, + &SchemaContents::Literal(Literal::String(StringLiteral { + min_length: None, + max_length: None, + pattern: None, + format: None, + enumeration: Some(ref enumeration), + })) => TypeContent::UntaggedEnum(enumeration.clone()), + v => { + panic!("Unsupported type {name} {v:#?}") + }, + }; + + TypePrinter { name, doc, content } + } + + fn generate_rpc_method(&mut self, buffer: &mut String, method: &Method) { + let Method { ref summary, ref name, ref params, ref result, .. } = method; + writeln!(@doc buffer, summary); + + let result = result + .as_ref() + .map(|content| match content { + RefOr::Inline(descriptor) => self + .type_info(&descriptor.schema) + .expect("Result type should be defined") + .get_type(), + RefOr::Reference { reference } => reference.clone(), + }) + .unwrap_or("()".to_string()); + + let parameters = params + .iter() + .map(RefOr::unwrap_inline) + .map(|ContentDescriptor { name, required, schema, .. }| { + let name_arg = name.to_snake_case().replace(' ', "_"); + let name_type = self + .type_info(schema) + .expect("Parameter type should be defined") + .set_required(*required) + .get_type(); + format!("{name_arg}: {name_type}") + }) + .collect::>() + .join(", "); + + writeln!(buffer, "#[method(name = \"{name}\")]"); + let method_name = name.trim_start_matches(&self.prefix).to_snake_case(); + writeln!(buffer, "async fn {method_name}(&self, {parameters}) -> RpcResult<{result}>;"); + } + + /// Collect the type if it's not yet generated or collected. + fn collect(&mut self, type_name: &str, ref_or_schema: ReferenceOrSchema) { + if !self.generated.contains(type_name) && !self.collected.contains_key(type_name) { + self.collected.insert(type_name.to_string(), ref_or_schema); + } + } +} + +/// Convert a reference to a type name. +fn reference_to_name(reference: &str) -> String { + if PRIMITIVE_MAPPINGS.contains_key(reference) { + return PRIMITIVE_MAPPINGS[reference].to_string(); + } + reference.split('/').last().unwrap().to_pascal_case() +} + +impl TypeNameProvider for TypeGenerator { + fn record_inline_type(&mut self, type_name: String, schema: &Schema) -> TypeInfo { + self.collect(&type_name, ReferenceOrSchema::Schema(schema.clone())); + TypeInfo { name: type_name, required: Required::Yes, array: false } + } + + fn type_info(&mut self, schema: &Schema) -> Option { + match &schema.contents { + SchemaContents::Reference { reference } => { + let type_name = reference_to_name(reference); + self.collect(&type_name, ReferenceOrSchema::Reference(reference.to_string())); + Some(type_name.into()) + }, + SchemaContents::Literal(Literal::Array(ArrayLiteral { items: Some(ref schema) })) => { + let mut type_info = + self.type_info(schema).expect("Anonymous array type not supported"); + type_info.array = true; + Some(type_info) + }, + SchemaContents::AllOf { all_of } => Some( + all_of + .iter() + .map(|s| self.type_info(s).expect("Anonymous all_of type not supported").name) + .collect::>() + .join("And") + .into(), + ), + SchemaContents::AnyOf { any_of: ref items } | + SchemaContents::OneOf { one_of: ref items } => { + let mut required = Required::Yes; + let items = items + .iter() + .filter_map(|s| { + let info = self.type_info(s).expect("Anonymous any_of type not supported"); + let name = info.name; + + if name == "Null" || name == "NotFound" { + required = Required::No { skip_if_null: false }; + None + } else { + Some(name) + } + }) + .collect::>(); + + let name = items.join("Or"); + if items.len() > 1 { + self.collect(&name, ReferenceOrSchema::Schema(schema.clone())); + } + + Some(TypeInfo { name, required, array: false }) + }, + SchemaContents::Literal(Literal::Null) => Some("Null".into()), + + // Use Type0, Type1, Type2, ... for String that have a single digit pattern. + SchemaContents::Literal(Literal::String(StringLiteral { + min_length: None, + max_length: None, + pattern: Some(ref pattern), + format: None, + enumeration: None, + })) if ["^0x0$", "^0x1$", "^0x2$", "^0x3$"].contains(&pattern.as_str()) => { + let type_id = format!("Type{}", &pattern[3..4]); + + Some(type_id.into()) + }, + + SchemaContents::Literal(Literal::Boolean) => Some("bool".into()), + SchemaContents::Object(_) => None, + SchemaContents::Literal(Literal::Object(_)) => None, + v => { + panic!("No type name for {v:#?}"); + }, + } + } +} + +#[cfg(test)] +pub fn assert_code_match(expected: &str, actual: &str) { + assert_eq!(format_code(expected).unwrap().trim(), format_code(actual).unwrap().trim()); +} + +#[cfg(test)] +mod test { + use super::*; + use pretty_assertions::assert_eq; + + #[test] + fn generate_works() { + let specs = read_specs().unwrap(); + + let mut generator = TypeGenerator::new(); + SUPPORTED_ETH_METHODS.iter().for_each(|name| { + generator.filtered_method_names.insert(name.to_string()); + }); + + let buffer = generator.generate_rpc_methods(&specs); + println!("{}", buffer); + } + + #[test] + fn generate_rpc_works() { + let method = serde_json::from_str::( + r###" + { + "name": "eth_estimateGas", + "summary": "Generates and returns an estimate of how much gas is necessary to allow the transaction to complete.", + "params": [ + { + "name": "Transaction", + "required": true, + "schema": { + "$ref": "#/components/schemas/GenericTransaction" + } + }, + { + "name": "Block", + "required": false, + "schema": { + "$ref": "#/components/schemas/BlockNumberOrTag" + } + } + ], + "result": { + "name": "Gas used", + "schema": { + "$ref": "#/components/schemas/uint" + } + } + } + "###, + ) + .unwrap(); + + let mut buffer = String::new(); + let mut generator = TypeGenerator::new(); + + generator.generate_rpc_method(&mut buffer, &method); + assert_code_match( + &buffer, + r#" + /// Generates and returns an estimate of how much gas is necessary to allow the transaction to complete. + #[method(name = "eth_estimateGas")] + async fn estimate_gas(&self, transaction: GenericTransaction, block: Option) -> RpcResult; + "#, + ); + } + + #[test] + fn generate_type_name_works() { + let mut generator = TypeGenerator::new(); + + let schema: Schema = serde_json::from_str( + r###" + { + "title": "to address", + "oneOf": [ + { "title": "Contract Creation (null)", "type": "null" }, + { "title": "Address", "$ref": "#/components/schemas/address" } + ] + } + "###, + ) + .unwrap(); + + assert_eq!(&generator.type_info(&schema).unwrap().get_type(), "Option
"); + } + + #[test] + fn generate_all_off_type_works() { + let specs = read_specs().unwrap(); + let mut generator = TypeGenerator::new(); + let res = generator.generate_type( + "Transaction4844Signed".to_string(), + specs.get_schema("#/components/schemas/Transaction4844Signed").unwrap(), + ); + let mut buffer = String::new(); + res.print(&mut buffer); + assert_code_match( + &buffer, + r#" + /// Signed 4844 Transaction + #[derive(Debug, Default, Clone, Encode, Decode, TypeInfo, Serialize, Deserialize, Eq, PartialEq)] + pub struct Transaction4844Signed { + #[serde(flatten)] + pub transaction_4844_unsigned: Transaction4844Unsigned, + /// r + pub r: U256, + /// s + pub s: U256, + /// yParity + /// The parity (0 for even, 1 for odd) of the y-value of the secp256k1 signature. + #[serde(rename = "yParity", skip_serializing_if = "Option::is_none")] + pub y_parity: Option, + } + "#, + ); + } + + #[test] + fn generate_one_of_type_works() { + let specs = read_specs().unwrap(); + let mut generator = TypeGenerator::new(); + let res = generator.generate_type( + "TransactionUnsigned".to_string(), + specs.get_schema("#/components/schemas/TransactionUnsigned").unwrap(), + ); + let mut buffer = String::new(); + res.print(&mut buffer); + assert_code_match( + &buffer, + r#" + #[derive(Debug, Clone, Encode, Decode, TypeInfo, Serialize, Deserialize, From, TryInto, Eq, PartialEq)] + #[serde(untagged)] + pub enum TransactionUnsigned { + Transaction4844Unsigned(Transaction4844Unsigned), + Transaction1559Unsigned(Transaction1559Unsigned), + Transaction2930Unsigned(Transaction2930Unsigned), + TransactionLegacyUnsigned(TransactionLegacyUnsigned), + } + impl Default for TransactionUnsigned { + fn default() -> Self { + TransactionUnsigned::Transaction4844Unsigned(Default::default()) + } + } + "#, + ); + } + + #[test] + fn generate_type_with_inline_variant_works() { + let specs = read_specs().unwrap(); + let mut generator = TypeGenerator::new(); + let res = generator.generate_type( + "SyncingStatus".to_string(), + specs.get_schema("#/components/schemas/SyncingStatus").unwrap(), + ); + let mut buffer = String::new(); + res.print(&mut buffer); + + assert_code_match( + &buffer, + r#" + /// Syncing status + #[derive(Debug, Clone, Encode, Decode, TypeInfo, Serialize, Deserialize, From, TryInto, Eq, PartialEq)] + #[serde(untagged)] + pub enum SyncingStatus { + /// Syncing progress + SyncingProgress(SyncingProgress), + /// Not syncing + /// Should always return false if not syncing. + Bool(bool), + } + impl Default for SyncingStatus { + fn default() -> Self { + SyncingStatus::SyncingProgress(Default::default()) + } + } + "#, + ); + } + + #[test] + fn generate_array_type_works() { + let specs = read_specs().unwrap(); + let mut generator = TypeGenerator::new(); + let res = generator.generate_type( + "AccessList".to_string(), + specs.get_schema("#/components/schemas/AccessList").unwrap(), + ); + let mut buffer = String::new(); + res.print(&mut buffer); + assert_code_match( + &buffer, + r#" + /// Access list + pub type AccessList = Vec; + "#, + ); + } + + #[test] + fn generate_one_of_with_null_variant_works() { + let specs = read_specs().unwrap(); + let mut generator = TypeGenerator::new(); + let res = generator.generate_type( + "FilterTopics".to_string(), + specs.get_schema("#/components/schemas/FilterTopics").unwrap(), + ); + let mut buffer = String::new(); + res.print(&mut buffer); + assert_code_match( + &buffer, + r#" + /// Filter Topics + pub type FilterTopics = Vec; + "#, + ); + } + + #[test] + fn generate_object_type_works() { + let specs = read_specs().unwrap(); + let mut generator = TypeGenerator::new(); + let res = generator.generate_type( + "Transaction".to_string(), + specs.get_schema("#/components/schemas/GenericTransaction").unwrap(), + ); + + let mut buffer = String::new(); + res.print(&mut buffer); + assert_code_match( + &buffer, + r#" + /// Transaction object generic to all types + #[derive(Debug, Default, Clone, Encode, Decode, TypeInfo, Serialize, Deserialize, Eq, PartialEq)] + pub struct Transaction { + /// accessList + /// EIP-2930 access list + #[serde(rename = "accessList", skip_serializing_if = "Option::is_none")] + pub access_list: Option, + /// blobVersionedHashes + /// List of versioned blob hashes associated with the transaction's EIP-4844 data blobs. + #[serde(rename = "blobVersionedHashes", skip_serializing_if = "Option::is_none")] + pub blob_versioned_hashes: Option>, + /// blobs + /// Raw blob data. + #[serde(skip_serializing_if = "Option::is_none")] + pub blobs: Option>, + /// chainId + /// Chain ID that this transaction is valid on. + #[serde(rename = "chainId", skip_serializing_if = "Option::is_none")] + pub chain_id: Option, + /// from address + #[serde(skip_serializing_if = "Option::is_none")] + pub from: Option
, + /// gas limit + #[serde(skip_serializing_if = "Option::is_none")] + pub gas: Option, + /// gas price + /// The gas price willing to be paid by the sender in wei + #[serde(rename = "gasPrice", skip_serializing_if = "Option::is_none")] + pub gas_price: Option, + /// input data + #[serde(alias = "data", skip_serializing_if = "Option::is_none")] + pub input: Option, + /// max fee per blob gas + /// The maximum total fee per gas the sender is willing to pay for blob gas in wei + #[serde(rename = "maxFeePerBlobGas", skip_serializing_if = "Option::is_none")] + pub max_fee_per_blob_gas: Option, + /// max fee per gas + /// The maximum total fee per gas the sender is willing to pay (includes the network / base fee and miner / priority fee) in wei + #[serde(rename = "maxFeePerGas", skip_serializing_if = "Option::is_none")] + pub max_fee_per_gas: Option, + /// max priority fee per gas + /// Maximum fee per gas the sender is willing to pay to miners in wei + #[serde(rename = "maxPriorityFeePerGas", skip_serializing_if = "Option::is_none")] + pub max_priority_fee_per_gas: Option, + /// nonce + #[serde(skip_serializing_if = "Option::is_none")] + pub nonce: Option, + /// to address + pub to: Option
, + /// type + #[serde(skip_serializing_if = "Option::is_none")] + pub r#type: Option, + /// value + #[serde(skip_serializing_if = "Option::is_none")] + pub value: Option, + } + "#, + ); + } +} diff --git a/substrate/frame/revive/rpc/codegen/src/main.rs b/substrate/frame/revive/rpc/codegen/src/main.rs new file mode 100644 index 000000000000..e0b660ea84e5 --- /dev/null +++ b/substrate/frame/revive/rpc/codegen/src/main.rs @@ -0,0 +1,64 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +use crate::generator::{format_code, TypeGenerator}; +use anyhow::Context; +use std::path::Path; + +mod generator; +mod open_rpc; +mod printer; + +fn main() -> anyhow::Result<()> { + let specs = generator::read_specs()?; + + let mut generator = TypeGenerator::new(); + generator.collect_extra_type("TransactionUnsigned"); + + let out_dir = if let Ok(dir) = std::env::var("CARGO_MANIFEST_DIR") { + Path::new(&dir).join("../src") + } else { + "../src".into() + } + .canonicalize() + .with_context(|| "Failed to find the api directory")?; + + let out = out_dir.join("rpc_methods_gen.rs"); + println!("Generating rpc_methods at {out:?}"); + format_and_write_file(&out, &generator.generate_rpc_methods(&specs)) + .with_context(|| format!("Failed to generate code to {out:?}"))?; + + let out_dir = if let Ok(dir) = std::env::var("CARGO_MANIFEST_DIR") { + Path::new(&dir).join("../../src/evm/api") + } else { + "../../src/evm/api".into() + } + .canonicalize() + .with_context(|| "Failed to find the api directory")?; + + let out = std::fs::canonicalize(out_dir.join("rpc_types_gen.rs"))?; + println!("Generating rpc_types at {out:?}"); + format_and_write_file(&out, &generator.generate_types(&specs)) + .with_context(|| format!("Failed to generate code to {out:?}"))?; + + Ok(()) +} + +fn format_and_write_file(path: &Path, content: &str) -> anyhow::Result<()> { + let code = format_code(content)?; + std::fs::write(path, code).expect("Unable to write file"); + Ok(()) +} diff --git a/substrate/frame/revive/rpc/codegen/src/open_rpc.rs b/substrate/frame/revive/rpc/codegen/src/open_rpc.rs new file mode 100644 index 000000000000..fa7510a50561 --- /dev/null +++ b/substrate/frame/revive/rpc/codegen/src/open_rpc.rs @@ -0,0 +1,834 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +//! Defines the types defined by the [`OpenRPC`](https://spec.open-rpc.org) specification. + +#![warn(missing_docs, missing_debug_implementations)] + +use serde::{Deserialize, Serialize}; +use std::collections::{BTreeMap, HashMap}; + +/// Represents an OpenRPC document. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct OpenRpc { + /// The semantic version number of the OpenRPC Specification version that the OpenRPC document + /// uses. + /// + /// This field should be used by tooling specifications and clients to interpret the OpenRPC + /// document. + pub openrpc: String, + /// Provides metadata about the API. + /// + /// This metadata may be used by tooling as required. + pub info: Info, + /// An array of [`Server`] objects, which provide connectivity information to a target server. + /// + /// If the `servers` property is not provided, or is an empty array, the default value would + /// be a [`Server`] with a `url` value of `localhost`. This is taken care of by the + /// [`open-rpc`](crate) crate. + #[serde(default = "serde_fns::servers")] + pub servers: Vec, + /// The available methods for the API. While this field is required, it is legal to leave it + /// empty. + pub methods: Vec>, + /// Holds various schemas for the specification. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub components: Option, + /// Contains additional documentation. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub external_docs: Option, +} + +impl OpenRpc { + /// Returns the [`Method`] with the given path reference. + /// + /// # Examples + /// + /// ```no_run + /// let path = "#/components/schemas/MY_SCHEMA"; + /// let schema = openrpc.get_schema(path).unwrap(); + /// ``` + pub fn get_schema(&self, reference: &str) -> Option<&Schema> { + let mut components = reference.split('/'); + + if !matches!(components.next(), Some("#")) { + return None; + } + + if !matches!(components.next(), Some("components")) { + return None; + } + + if !matches!(components.next(), Some("schemas")) { + return None; + } + + let name = components.next()?; + self.components.as_ref()?.schemas.get(name) + } + + /// Same as [`OpenRpc::get_schema`] but returns a &mut reference + pub fn get_schema_mut(&mut self, reference: &str) -> Option<&mut Schema> { + let mut components = reference.split('/'); + + if !matches!(components.next(), Some("#")) { + return None; + } + + if !matches!(components.next(), Some("components")) { + return None; + } + + if !matches!(components.next(), Some("schemas")) { + return None; + } + + let name = components.next()?; + self.components.as_mut()?.schemas.get_mut(name) + } +} + +/// Provides metadata about the API. +/// +/// The metadata may be used by clients if needed, and may be presented in editing or +/// documentation generation tools for convenience. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct Info { + /// The title of the application. + #[serde(default)] + pub title: String, + /// A verbose description of the application. + /// + /// GitHub Flavored Markdown syntax may be used for rich text representation. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub description: Option, + /// A URL to the Terms of Service for the API. + /// + /// This must contain an URL. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub terms_of_service: Option, + /// contact information for the exposed API. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub contact: Option, + /// License information for the exposed API. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub license: Option, + /// The version of the OpenRPC document. + /// + /// Note that this is distinct from the `openrpc` field of [`OpenRpc`] which specifies the + /// version of the OpenRPC Specification used. + #[serde(default)] + pub version: String, +} + +/// Contact information for the exposed API. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct Contact { + /// The identifying name of the contact person/organization. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub name: Option, + /// The URL pointing to the contact information. + /// + /// This must contain an URL. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub url: Option, + /// The email address of the contact person/organization. + /// + /// This must contain an email address. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub email: Option, +} + +/// License information for the exposed API. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct License { + /// The name of the license used for the API. + #[serde(default)] + pub name: String, + /// The URL pointing to the license used for the API. + /// + /// This must contain an URL. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub url: Option, +} + +/// A server. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct Server { + /// A name to be used as the canonical name for the server. + #[serde(default)] + pub name: String, + /// A URL to the target host. + /// + /// This URL supports Server Variables and may be relative to indicate that the host location + /// is relative to the location where the OpenRPC document is being served. + /// + /// Server Variables are passed into the Runtime Expression to produce a server URL. + pub url: RuntimeExpression, + /// A short description of what the server is. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub summary: Option, + /// Describes the host designated by the URL. + /// + /// GitHub Flavored Markdown may be used for rich text presentation. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub description: Option, + /// The values of this object are passed to the [`RuntimeExpression`] to produce an actual + /// URL. + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] + pub variables: BTreeMap, +} + +/// An object representing a Server Variable for server URL template substitution. +#[derive(Serialize, Deserialize, Debug, Clone, Default)] +#[serde(rename_all = "camelCase")] +pub struct ServerVariable { + /// An enumeration of string values to be used if the substitution options are from a limited + /// set. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub enum_: Vec, + /// The default value to use for substitution, which shall be sent if an alternate value is + /// not supplied. + /// + /// Note this behavior is different than the Schema Object's treatment of default values, + /// because in those cases parameter values are optional. + #[serde(default)] + pub default: String, + /// An optional description for the server variable. + /// + /// GitHub Flavored Markdown syntax may be used for rich text representation. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub description: Option, +} + +/// Describes the interface for the given method name. +/// +/// The method name is used as the `method` field of the JSON-RPC body. It therefore must be +/// unique. +#[derive(Default, Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct Method { + /// The canonical name of the method. + /// + /// This name must be unique within the methods array. + #[serde(default)] + pub name: String, + /// A list of tags for API documentation control. Tags can be used for logical grouping + /// of methods by resources or any other qualifier. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub tags: Vec>, + /// A short summary of what the method does. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub summary: Option, + /// A verbose explanation of the method behavior. + /// + /// GitHub Flavored Markdown syntax may be used for rich text representation. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub description: Option, + /// Additional external documentation for this method. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub external_docs: Option, + /// A list of parameters that are applicable for this method. + /// + /// The list must not include duplicated parameters and therefore require `name` to be + /// unique. + /// + /// All required parameters must be listed *before* any optional parameters. + #[serde(default)] + pub params: Vec>, + /// The description of the result returned by the method. + /// + /// If defined, it must be a [`ContentDescriptor`] or a Reference. + /// + /// If undefined, the method must only be used as a *notification*. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub result: Option>, + /// Declares this method as deprecated. + /// + /// Consumers should refrain from usage of the declared method. + /// + /// The default value is `false`. + #[serde(default, skip_serializing_if = "serde_fns::is_false")] + pub deprecated: bool, + /// An alternative `servers` array to service this method. + /// + /// If specified, it overrides the `servers` array defined at the root level. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub servers: Option>, + /// A list of custom application-defined errors that may be returned. + /// + /// The errors must have unique error codes. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub errors: Vec>, + /// A list of possible links from this method call. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub links: Vec>, + /// The expected format of the parameters. + /// + /// The parameters of a method may be an array, an object, or either. When a method + /// has a `param_structure` value of [`ByName`], callers of the method must pass an + /// object as the parameters. When a method has a `param_structure` value of [`ByPosition`], + /// callers of the method must pass an array as the parameters. Otherwise, callers may + /// pass either an array or an object as the parameters. + /// + /// The default value is [`Either`]. + /// + /// [`ByName`]: ParamStructure::ByName + /// [`ByPosition`]: ParamStructure::ByPosition + /// [`Either`]: ParamStructure::Either + #[serde(default, skip_serializing_if = "serde_fns::is_default")] + pub param_structure: ParamStructure, + /// An array of [`ExamplePairing`] objects, where each example includes a valid + /// params-to-result [`ContentDescriptor`] pairing. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub examples: Vec>, +} + +/// A possible value for the `param_structure` field of [`Method`]. +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Default)] +#[serde(rename_all = "kebab-case")] +pub enum ParamStructure { + /// Parameters must be passed as a JSON object. + ByName, + /// Parameters must be passed as a JSON array. + ByPosition, + /// Parameters may be passed as either a JSON object or a JSON array. + #[default] + Either, +} + +/// Content descriptors are that do just as they suggest - describe content. They are reusable +/// ways of describing either parameters or results. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct ContentDescriptor { + /// The name of the content being described. + /// + /// If the content described is a method parameter assignable + /// [`ByName`](ParamStructure::ByName), this field must be the name of the parameter. + #[serde(default)] + pub name: String, + /// A short summary of the content that is being described. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub summary: Option, + /// A verbose explanation of the content being described. + /// + /// GitHub Flavored Markdown syntax may be used for rich text representation. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub description: Option, + /// Determines if the content is a required field. + /// + /// Default is `false`. + #[serde(default, skip_serializing_if = "serde_fns::is_false")] + pub required: bool, + /// A [`Schema`] that describes what is allowed in the content. + #[serde(default)] + pub schema: Schema, + /// Whether the content is deprecated. + /// + /// Default is `false`. + #[serde(default, skip_serializing_if = "serde_fns::is_false")] + pub deprecated: bool, +} + +/// Allows the definition of input and output data types. +#[derive(Serialize, Deserialize, Debug, Clone, Default)] +#[serde(rename_all = "camelCase")] +pub struct Schema { + /// The title of the schema. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub title: Option, + /// The description of the schema. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub description: Option, + /// The contents of the schema. + #[serde(flatten)] + pub contents: SchemaContents, +} + +/// The content of a schema. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(untagged)] +pub enum SchemaContents { + /// The schema contains a reference to another schema. + Reference { + /// The reference string. + #[serde(rename = "$ref")] + reference: String, + }, + /// The schema is made of a combination of other schemas. + /// + /// The final object must match *all* of the schemas. + AllOf { + /// The schemas that the final object must match. + #[serde(rename = "allOf")] + all_of: Vec, + }, + /// The schema is made of a combination of other schemas. + /// + /// The final object must match *any* of the schemas. + AnyOf { + /// The schemas that the final object must match. + #[serde(rename = "anyOf")] + any_of: Vec, + }, + /// The schema is made of a combination of other schemas. + /// + /// The final object must match exactly *one* of the schemas. + OneOf { + /// The schemas that the final object must match. + #[serde(rename = "oneOf")] + one_of: Vec, + }, + /// The schema contains a literal value. + Literal(Literal), + /// The schema contains an Object. + /// + /// Note this is a workaround to parse Literal(Literal::ObjectLiteral), that don't havethe + /// type: "object" field. + Object(ObjectLiteral), +} + +impl Default for SchemaContents { + #[inline] + fn default() -> Self { + Self::Literal(Literal::Null) + } +} + +/// A literal value. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(tag = "type", rename_all = "lowercase")] +pub enum Literal { + /// The literal is a boolean. + Boolean, + /// The literal is an integer. + Integer(IntegerLiteral), + /// The literal is a number. + Number(NumberLiteral), + /// The literal is a string. + String(StringLiteral), + // The literal is an object. + Object(ObjectLiteral), + /// The literal is an array. + Array(ArrayLiteral), + /// The literal is a null value. + Null, +} + +/// The constraints that may be applied to an integer literal schema. +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +#[derive(Debug, Clone)] +pub struct IntegerLiteral { + /// The integer must be a multiple of this value. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub multiple_of: Option, + /// The minimum value of the integer. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub minimum: Option, + /// The maximum value of the integer. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub maximum: Option, + /// Whether the minimum value is exclusive. + /// + /// Default is `false`. + #[serde(default, skip_serializing_if = "serde_fns::is_false")] + pub exclusive_minimum: bool, + /// Whether the maximum value is exclusive. + /// + /// Default is `false`. + #[serde(default, skip_serializing_if = "serde_fns::is_false")] + pub exclusive_maximum: bool, +} + +/// The constraints that may be applied to a number literal schema. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct NumberLiteral { + /// The number must be a multiple of this value. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub multiple_of: Option, + /// The minimum value of the number. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub minimum: Option, + /// The maximum value of the number. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub maximum: Option, + /// Whether the minimum value is exclusive. + /// + /// Default is `false`. + #[serde(default, skip_serializing_if = "serde_fns::is_false")] + pub exclusive_minimum: bool, + /// Whether the maximum value is exclusive. + /// + /// Default is `false`. + #[serde(default, skip_serializing_if = "serde_fns::is_false")] + pub exclusive_maximum: bool, +} + +/// The constraints that may be applied to an array literal schema. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct ArrayLiteral { + /// The schema that the items in the array must match. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub items: Option>, +} + +/// The constraints that may be applied to an string literal schema. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct StringLiteral { + /// The minimum length of the string. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub min_length: Option, + /// The maximum length of the string.s + #[serde(default, skip_serializing_if = "Option::is_none")] + pub max_length: Option, + /// The pattern that the string must match. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub pattern: Option, + /// The format that the string must be in. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub format: Option, + /// A list of possible values for the string. + #[serde(default, skip_serializing_if = "Option::is_none", rename = "enum")] + pub enumeration: Option>, +} + +/// A string format. +#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[serde(rename_all = "kebab-case")] +pub enum StringFormat { + /// Date and time together, for example, `2018-11-13T20:20:39+00:00`. + DateTime, + /// Time, for example, `20:20:39+00:00`. + Time, + /// Date, for example, `2018-11-13`. + Date, + /// A duration as defined by the [ISO 8601 ABNF](https://datatracker.ietf.org/doc/html/rfc3339#appendix-A). + Duration, + /// An email. See [RFC 5321](http://tools.ietf.org/html/rfc5321#section-4.1.2). + Email, + /// The internationalized version of an email. See [RFC 6531](https://tools.ietf.org/html/rfc6531). + IdnEmail, + /// A host name. See [RFC 1123](https://datatracker.ietf.org/doc/html/rfc1123#section-2.1). + Hostname, + /// The internationalized version of a host name. See [RFC 5890](https://tools.ietf.org/html/rfc5890#section-2.3.2.3). + IdnHostname, + /// An IP v4. See [RFC 2673](http://tools.ietf.org/html/rfc2673#section-3.2). + #[serde(rename = "ipv4")] + IpV4, + /// An IP v6. See [RFC 2373](http://tools.ietf.org/html/rfc2373#section-2.2). + #[serde(rename = "ipv6")] + IpV6, + /// A universally unique identifier. See [RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122). + Uuid, + /// A universal resource identifier . See [RFC 3986](http://tools.ietf.org/html/rfc3986). + Uri, + /// A URI reference. See (RFC 3986)[]. + UriReference, + /// The internationalized version of a URI. See [RFC 3987](https://tools.ietf.org/html/rfc3987). + Iri, + /// The internationalized version of a URI reference. See [RFC 3987](https://tools.ietf.org/html/rfc3987). + IriReference, + /// A URI template. See [RFC 6570](https://tools.ietf.org/html/rfc6570). + UriTemplate, + /// A JSON pointer. See [RFC 6901](https://tools.ietf.org/html/rfc6901). + JsonPointer, + /// A relative JSON pointer. See [Relative JSON Pointer](https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01). + RelativeJsonPointer, + /// A regular expression. See [ECMA 262](https://www.ecma-international.org/publications-and-standards/standards/ecma-262/). + Regex, +} + +/// The constraints that may be applied to an object literal schema. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct ObjectLiteral { + /// The properties that the object might have. + pub properties: BTreeMap, + + /// List of legacy aliases for properties. + #[serde(skip)] + pub legacy_aliases: HashMap, + + /// A list of properties that the object must have. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub required: Vec, +} + +/// A set of example parameters and a result. +/// +/// This result is what you'd expect from the JSON-RPC service given the exact params. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct ExamplePairing { + /// The name for the example pairing. + #[serde(default)] + pub name: String, + /// A verbose description of the example pairing. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub description: Option, + /// A short summary of the example pairing. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub summary: Option, + /// Example parameters. + #[serde(default)] + pub params: Vec>, + /// Example result. + /// + /// When undefined, shows the usage of the method as a notification. + #[serde(default)] + pub result: RefOr, +} + +/// Defines an example that is intended to match a [`Schema`] of a given [`ContentDescriptor`]. +#[derive(Serialize, Deserialize, Default, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct ExampleObject { + /// Canonical name of the example. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub name: Option, + /// A verbose description of the example + /// + /// GitHub Flavored Markdown syntax may be used for rich text representation. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub description: Option, + /// A short summary of the example. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub summary: Option, + /// The value of the example. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub value: Option, +} + +/// The example value of an [`ExampleObject`]. +#[derive(Serialize, Deserialize, Debug, Clone)] +pub enum ExampleValue { + /// The value is a JSON object embedded in the document. + /// A link to an external document containing the value. + #[serde(rename = "externalValue")] + External(String), +} + +/// Represents a possible design-time link for a result. +/// +/// The presence of a link does not guarantee the caller's ability to successfully invoke it, +/// rather it provides a known relationship and traversal mechanism between results and other +/// methods. +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +#[derive(Debug, Clone)] +pub struct Link { + /// Canonical name for the link. + #[serde(default)] + pub name: String, + /// A description of the link. + /// + /// GitHub Flavored Markdown syntax may be used for rich text representation. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub description: Option, + /// Short description for the link. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub summary: Option, + /// The name of an *existing*, resolvable OpenRPC method, as defined with a unique + /// `method`. This field must resolve to a unique [`Method`] object. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub method: Option, + /// The parameters to pass to a method as specified with `method`. The key is the parameter + /// name to be used, whereas the value can be a constant or a [`RuntimeExpression`] to be + /// evaluated and passed to the linked method. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub params: Option, + /// A server object to be used by the target method. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub server: Option, +} + +/// The content of the `params` field of a [`Link`]. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(untagged)] +pub enum LinkParams { + /// A [`RuntimeExpression`] that evaluates to the parameters. + Dynamic(RuntimeExpression), +} + +/// Runtime expressions allow the user to define an expression which will evaluate to a +/// string once the desired value(s) are known. +/// +/// They are used when the desired value of a link or server can only be constructed at +/// run time. This mechanism is used by [`Link`] objects and [`ServerVariable`]s. +/// +/// This runtime expression makes use of JSON template strings. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(transparent)] +pub struct RuntimeExpression(pub String); + +/// An application-level error. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct Error { + /// An application-defined error code. + #[serde(default)] + pub code: i64, + /// A string providing a short description of the error. + /// + /// The message should be limited to a concise single sentence. + #[serde(default)] + pub message: String, +} + +/// Holds a set of reusable objects for different aspects of the OpenRPC document. +/// +/// All objects defined within the [`Components`] object will have no effect on the API +/// unless they are explicitly referenced from properties outside of the [`Components`] +/// object. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct Components { + /// A list of reusable [`ContentDescriptor`]s. + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] + pub content_descriptors: BTreeMap, + /// A list of reusable [`Schema`]s. + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] + pub schemas: BTreeMap, + /// A list of reusable [`ExampleObject`]s. + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] + pub examples: BTreeMap, + /// A list of reusable [`Link`]s. + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] + pub links: BTreeMap, + /// A list of reusable [`Error`]s. + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] + pub errors: BTreeMap, + /// A list of reusable [`ExamplePairing`]s. + #[serde(default, skip_serializing_if = "BTreeMap::is_empty", rename = "examplePairingObjects")] + pub example_pairings: BTreeMap, + /// A list of reusable [`Tag`]s. + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] + pub tags: BTreeMap, +} + +/// Adds metadata to a single tag that is used by the [`Method`] Object. +/// +/// It is not mandatory to have a [`Tag`] Object per tag defined in the [`Method`] +/// Object instances. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct Tag { + /// The name of the tag. + #[serde(default)] + pub name: String, + /// A short summary of the tag. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub summary: Option, + /// A verbose explanation of the tag. + /// + /// GitHub Flavored Markdown syntax may be used for rich text representation. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub description: Option, + /// Additional external documentation for this tag. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub external_docs: Option, +} + +/// Allows referencing an external resource for extended documentation. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(rename_all = "camelCase")] +pub struct ExternalDocumentation { + /// A verbose explanation of the target documentation. + /// + /// GitHub Flavored Markdown syntax may be used for rich text representation. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub description: Option, + /// A URL for the target documentation. + /// + /// This must contain an URL. + #[serde(default)] + pub url: String, +} + +/// Either a reference or an inline object. +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(untagged)] +pub enum RefOr { + /// A reference to an object defined elsewhere. + Reference { + /// The reference string. + #[serde(rename = "$ref")] + reference: String, + }, + /// An inline object. + Inline(T), +} + +impl RefOr { + /// Unwraps the inlined object. + pub fn unwrap_inline(&self) -> &T { + match self { + RefOr::Reference { reference } => panic!("Unexpected reference: {reference}"), + RefOr::Inline(v) => v, + } + } +} + +impl Default for RefOr { + #[inline] + fn default() -> Self { + RefOr::Inline(T::default()) + } +} + +/// Functions used by `serde`, such as predicates and default values. +mod serde_fns { + use std::collections::BTreeMap; + + use super::{RuntimeExpression, Server}; + + /// Returns the default value of the `servers` field. + pub fn servers() -> Vec { + vec![Server { + name: "default".into(), + url: RuntimeExpression("localhost".into()), + summary: None, + description: None, + variables: BTreeMap::new(), + }] + } + + /// Returns whether `b` is `false`. + pub fn is_false(b: &bool) -> bool { + !*b + } + + /// Returns whether the given value is the default value of its type. + pub fn is_default(t: &T) -> bool { + *t == T::default() + } +} + +#[test] +fn parsing_works() { + let content = include_str!("../openrpc.json"); + let _: OpenRpc = dbg!(serde_json::from_str(content).unwrap()); +} diff --git a/substrate/frame/revive/rpc/codegen/src/printer.rs b/substrate/frame/revive/rpc/codegen/src/printer.rs new file mode 100644 index 000000000000..4d1bb620c2e4 --- /dev/null +++ b/substrate/frame/revive/rpc/codegen/src/printer.rs @@ -0,0 +1,496 @@ +// This file is part of Substrate. + +// Copyright (C) Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +use crate::open_rpc::*; +use inflector::Inflector; + +/// Type information used for generating the type. +#[derive(Debug, Clone)] +pub struct TypeInfo { + /// The type name. + pub name: String, + /// Whether the type is an array. + pub array: bool, + /// Whether the type is required. + pub required: Required, +} + +impl TypeInfo { + pub fn set_required(mut self, required: bool) -> Self { + if required { + self.required = Required::Yes; + } else { + self.required = Required::No { skip_if_null: true }; + } + self + } + + /// Return Whether the type is optional. + pub fn is_optional(&self) -> bool { + matches!(self.required, Required::No { .. }) + } +} + +/// A trait to provide type names. +pub trait TypeNameProvider { + /// Returns type information for a schema. + fn type_info(&mut self, schema: &Schema) -> Option; + + /// Record an inline type. + fn record_inline_type(&mut self, name: String, schema: &Schema) -> TypeInfo; +} + +/// Describes whether the type is required or not. +#[derive(Debug, Clone)] +pub enum Required { + /// The type is required. + Yes, + /// The type is not required, and may be skipped when serializing if it's None and skip_if_null + /// is true. + No { skip_if_null: bool }, +} + +impl TypeInfo { + //// Convert the type info to a string we can use in the generated code. + pub fn get_type(&self) -> String { + let mut type_name = self.name.clone(); + if self.array { + type_name = format!("Vec<{}>", type_name) + } + if self.is_optional() { + type_name = format!("Option<{}>", type_name) + } + type_name + } +} + +impl From for TypeInfo +where + T: Into, +{ + fn from(name: T) -> Self { + Self { name: name.into(), required: Required::Yes, array: false } + } +} +/// Represents a field in a struct. +#[derive(Debug)] +pub struct Field { + /// The documentation for the field. + doc: Option, + /// The name of the field. + name: String, + /// the type information for the field. + type_info: TypeInfo, + /// Whether to flatten the field, when serializing. + flatten: bool, + /// Legacy alias for the field. + alias: Option, +} + +/// Represents a collection of fields. +#[derive(Debug)] +pub struct Fields(Vec); + +impl From> for Fields { + fn from(value: Vec) -> Self { + Self(value) + } +} + +impl IntoIterator for Fields { + type Item = Field; + type IntoIter = std::vec::IntoIter; + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} + +impl Fields { + /// Creates a collection of fields from an [`ObjectLiteral]. + /// + /// The methods also takes a [`TypeNameProvider`] to resolve the types of the fields, and to + /// collect child types. + pub fn from(value: &ObjectLiteral, provider: &mut impl TypeNameProvider) -> Self { + let ObjectLiteral { properties, legacy_aliases, required } = value; + + properties + .iter() + .map(|(name, schema)| { + let mut type_info = provider.type_info(schema).expect("Type should be defined"); + if matches!(type_info.required, Required::Yes) && !required.contains(name) { + type_info.required = Required::No { skip_if_null: true }; + } + + let doc = doc_str_from_schema(schema); + Field { + doc, + name: name.clone(), + type_info, + alias: legacy_aliases.get(name).cloned(), + flatten: false, + } + }) + .collect::>() + .into() + } + + /// Creates a collection of fields from the items of a [`SchemaContents::AllOf`] schema. + pub fn from_all_of(all_of: &[Schema], provider: &mut impl TypeNameProvider) -> Fields { + all_of + .iter() + .flat_map(|schema| { + let doc = doc_str_from_schema(schema); + if let Some(type_info) = provider.type_info(schema) { + vec![Field { + doc, + name: type_info.name.clone(), + type_info, + alias: None, + flatten: true, + }] + } else { + let object = match &schema.contents { + SchemaContents::Object(object) => object, + SchemaContents::Literal(Literal::Object(object)) => object, + v => panic!("Unsupported anonymous all_of type {:?}", v), + }; + + Fields::from(object, provider).0 + } + }) + .collect::>() + .into() + } +} + +/// The variant of an enum. +#[derive(Debug)] +pub struct Variant { + /// The documentation for the variant. + doc: Option, + /// The type information for the variant. + type_info: TypeInfo, +} + +impl Variant { + pub fn name(&self) -> String { + let name = self.type_info.name.to_pascal_case(); + if self.type_info.array { + format!("{}s", name) + } else { + name + } + } +} + +pub fn doc_str_from_schema(schema: &Schema) -> Option { + let mut doc = schema.title.clone(); + + if let Some(description) = &schema.description { + doc = Some(doc.map_or_else(|| description.clone(), |doc| format!("{doc}\n{description}"))); + } + + doc +} + +#[derive(Debug)] +pub struct Variants(Vec); +impl Variants { + /// Creates a collection of variants from the items of a [`SchemaContents::OneOf`] schema. + pub(crate) fn from_one_of(one_of: &[Schema], provider: &mut impl TypeNameProvider) -> Variants { + one_of + .iter() + .filter_map(|schema| { + let doc = doc_str_from_schema(schema); + if let Some(type_info) = provider.type_info(schema) { + if type_info.name == "Null" || type_info.name == "NotFound" { + return None; + } + + Some(Variant { doc, type_info }) + } else { + let name = schema + .title + .clone() + .expect("Title should be defined for inline variant") + .to_pascal_case(); + + let type_info = provider.record_inline_type(name.clone(), schema); + Some(Variant { doc, type_info }) + } + }) + .collect::>() + .into() + } +} + +impl From> for Variants { + fn from(value: Vec) -> Self { + Self(value) + } +} + +/// The content of a type. +#[derive(Debug)] +pub enum TypeContent { + /// A struct type. + Struct(Fields), + /// A unit struct type. + TypeAlias(TypeInfo), + /// An enum type. + Enum(Variants), + /// A serde untagged enum type. + UntaggedEnum(Vec), +} + +/// A type printer. +#[derive(Debug)] +pub struct TypePrinter { + pub doc: Option, + pub name: String, + pub content: TypeContent, +} + +/// A macro to write a formatted line to a buffer. +#[macro_export] +macro_rules! writeln { + (@doc $s: ident, $doc: ident) => { + $crate::writeln!(@doc $s, $doc, 0) + }; + (@doc $s: ident, $doc: ident, $indent: literal) => { + if let Some(doc) = $doc { + for line in doc.lines() { + writeln!($s, "{:indent$}/// {}", "", line, indent = $indent); + } + } + }; + ($s: ident, $($arg: tt)*) => { + $s.push_str(&format!($($arg)*)); + $s.push_str("\n"); + }; + + + +} + +impl TypePrinter { + /// Prints the type to a buffer. + pub fn print(self, buffer: &mut String) { + let Self { doc, name, content, .. } = self; + + writeln!(@doc buffer, doc); + match content { + TypeContent::Enum(variants) if variants.0.len() == 1 => { + let type_info = &variants.0[0].type_info; + writeln!(buffer, "pub type {name} = {};", type_info.get_type()); + }, + TypeContent::TypeAlias(type_info) => { + writeln!(buffer, "pub type {name} = {};", type_info.get_type()); + }, + TypeContent::Enum(variants) => { + writeln!( + buffer, + "#[derive(Debug, Clone, Encode, Decode, TypeInfo, Serialize, Deserialize, From, TryInto, Eq, PartialEq)]" + ); + writeln!(buffer, "#[serde(untagged)]"); + writeln!(buffer, "pub enum {name} {{"); + for variant in variants.0.iter() { + let doc = &variant.doc; + writeln!(@doc buffer, doc, 2); + writeln!(buffer, " {}({}),", variant.name(), variant.type_info.get_type()); + } + writeln!(buffer, "}}"); + + // Implement Default trait + let variant = variants.0[0].name(); + writeln!(buffer, "impl Default for {name} {{"); + writeln!(buffer, " fn default() -> Self {{"); + writeln!(buffer, " {name}::{variant}(Default::default())"); + writeln!(buffer, " }}"); + writeln!(buffer, "}}"); + }, + TypeContent::UntaggedEnum(variants) => { + writeln!( + buffer, + "#[derive(Debug, Default, Clone, Encode, Decode, TypeInfo, Serialize, Deserialize, Eq, PartialEq)]" + ); + writeln!(buffer, "pub enum {name} {{"); + for (i, name) in variants.iter().enumerate() { + writeln!(buffer, " #[serde(rename = \"{name}\")]"); + if i == 0 { + writeln!(buffer, " #[default]"); + } + let pascal_name = name.to_pascal_case(); + writeln!(buffer, " {pascal_name},"); + } + writeln!(buffer, "}}"); + }, + TypeContent::Struct(fields) => { + writeln!( + buffer, + "#[derive(Debug, Default, Clone, Encode, Decode, TypeInfo, Serialize, Deserialize, Eq, PartialEq)]" + ); + + writeln!(buffer, "pub struct {name} {{"); + for Field { doc, name, type_info, alias, flatten } in fields { + writeln!(@doc buffer, doc, 2); + let mut snake_name = name.to_snake_case(); + let mut serde_params = vec![]; + + if flatten { + serde_params.push("flatten".to_string()); + } else if snake_name != name { + serde_params.push(format!("rename = \"{}\"", name)); + } + + if let Some(alias) = alias { + serde_params.push(format!("alias = \"{}\"", alias)); + } + + if matches!(type_info.required, Required::No { skip_if_null: true }) { + serde_params.push("skip_serializing_if = \"Option::is_none\"".to_string()); + } + + if !serde_params.is_empty() { + writeln!(buffer, " #[serde({})]", serde_params.join(", ")); + } + + let type_name = type_info.get_type(); + + if snake_name == "type" { + snake_name = "r#type".to_string() + } + writeln!(buffer, " pub {snake_name}: {type_name},"); + } + writeln!(buffer, "}}"); + }, + } + } +} + +#[cfg(test)] +mod test { + use super::*; + use crate::generator::assert_code_match; + + #[test] + fn print_struct_works() { + let gen = TypePrinter { + doc: Some("A simple struct".to_string()), + name: "SimpleStruct".to_string(), + content: TypeContent::Struct( + vec![ + Field { + doc: Some("The first field".to_string()), + name: "firstField".to_string(), + type_info: "u32".into(), + flatten: false, + alias: None, + }, + Field { + doc: None, + name: "second".to_string(), + type_info: TypeInfo { + name: "String".to_string(), + required: Required::No { skip_if_null: true }, + array: false, + }, + flatten: true, + alias: None, + }, + ] + .into(), + ), + }; + let mut buffer = String::new(); + gen.print(&mut buffer); + assert_code_match( + &buffer, + r#" + /// A simple struct + #[derive(Debug, Default, Clone, Encode, Decode, TypeInfo, Serialize, Deserialize, Eq, PartialEq)] + pub struct SimpleStruct { + /// The first field + #[serde(rename = "firstField")] + pub first_field: u32, + #[serde(flatten, skip_serializing_if = "Option::is_none")] + pub second: Option, + } + "#, + ); + } + + #[test] + fn print_untagged_enum_works() { + let gen = TypePrinter { + doc: Some("A simple untagged enum".to_string()), + name: "SimpleUntaggedEnum".to_string(), + content: TypeContent::UntaggedEnum(vec!["first".to_string(), "second".to_string()]), + }; + let mut buffer = String::new(); + gen.print(&mut buffer); + assert_code_match( + &buffer, + r#" + /// A simple untagged enum + #[derive(Debug, Default, Clone, Encode, Decode, TypeInfo, Serialize, Deserialize, Eq, PartialEq)] + pub enum SimpleUntaggedEnum { + #[serde(rename = "first")] + #[default] + First, + #[serde(rename = "second")] + Second, + } + "#, + ); + } + + #[test] + fn print_enum_works() { + let gen = TypePrinter { + doc: Some("A simple enum".to_string()), + name: "SimpleEnum".to_string(), + content: TypeContent::Enum( + vec![ + Variant { doc: Some("The Foo variant".to_string()), type_info: "Foo".into() }, + Variant { doc: Some("The Bar variant".to_string()), type_info: "Bar".into() }, + ] + .into(), + ), + }; + let mut buffer = String::new(); + gen.print(&mut buffer); + assert_code_match( + &buffer, + r#" + /// A simple enum + #[derive(Debug, Clone, Encode, Decode, TypeInfo, Serialize, Deserialize, From, TryInto, Eq, PartialEq)] + #[serde(untagged)] + pub enum SimpleEnum { + /// The Foo variant + Foo(Foo), + /// The Bar variant + Bar(Bar), + } + impl Default for SimpleEnum { + fn default() -> Self { + SimpleEnum::Foo(Default::default()) + } + } + "#, + ); + } +} From 3529fbbc82756272677e1ab5c116cba7f6979ef4 Mon Sep 17 00:00:00 2001 From: pgherveou Date: Fri, 22 Nov 2024 15:46:37 +0100 Subject: [PATCH 2/8] Add geth-diff-tests --- .../rpc/examples/js/abi/errorTester.json | 106 +++++ .../revive/rpc/examples/js/abi/errorTester.ts | 106 +++++ .../revive/rpc/examples/js/abi/event.json | 66 +-- .../frame/revive/rpc/examples/js/abi/event.ts | 34 ++ .../revive/rpc/examples/js/abi/piggyBank.json | 128 +++--- .../revive/rpc/examples/js/abi/piggyBank.ts | 65 +++ .../frame/revive/rpc/examples/js/bun.lockb | Bin 45391 -> 33662 bytes .../rpc/examples/js/contracts/ErrorTester.sol | 52 +++ .../frame/revive/rpc/examples/js/package.json | 8 +- .../rpc/examples/js/pvm/errorTester.polkavm | Bin 0 -> 12890 bytes .../revive/rpc/examples/js/src/balance.ts | 9 + .../rpc/examples/js/src/build-contracts.ts | 26 +- .../frame/revive/rpc/examples/js/src/event.ts | 40 +- .../rpc/examples/js/src/geth-diff.test.ts | 398 ++++++++++++++++++ .../frame/revive/rpc/examples/js/src/lib.ts | 133 +++--- .../revive/rpc/examples/js/src/piggy-bank.ts | 81 +++- .../revive/rpc/examples/js/src/revert.ts | 10 - .../revive/rpc/examples/js/src/transfer.ts | 15 +- substrate/frame/revive/rpc/src/client.rs | 67 +-- substrate/frame/revive/rpc/src/lib.rs | 37 +- substrate/frame/revive/rpc/src/tests.rs | 1 + .../frame/revive/src/evm/api/rpc_types_gen.rs | 21 +- 22 files changed, 1148 insertions(+), 255 deletions(-) create mode 100644 substrate/frame/revive/rpc/examples/js/abi/errorTester.json create mode 100644 substrate/frame/revive/rpc/examples/js/abi/errorTester.ts create mode 100644 substrate/frame/revive/rpc/examples/js/abi/event.ts create mode 100644 substrate/frame/revive/rpc/examples/js/abi/piggyBank.ts create mode 100644 substrate/frame/revive/rpc/examples/js/contracts/ErrorTester.sol create mode 100644 substrate/frame/revive/rpc/examples/js/pvm/errorTester.polkavm create mode 100644 substrate/frame/revive/rpc/examples/js/src/balance.ts create mode 100644 substrate/frame/revive/rpc/examples/js/src/geth-diff.test.ts delete mode 100644 substrate/frame/revive/rpc/examples/js/src/revert.ts diff --git a/substrate/frame/revive/rpc/examples/js/abi/errorTester.json b/substrate/frame/revive/rpc/examples/js/abi/errorTester.json new file mode 100644 index 000000000000..2d8dccc771e8 --- /dev/null +++ b/substrate/frame/revive/rpc/examples/js/abi/errorTester.json @@ -0,0 +1,106 @@ +[ + { + "inputs": [ + { + "internalType": "string", + "name": "message", + "type": "string" + } + ], + "name": "CustomError", + "type": "error" + }, + { + "inputs": [ + { + "internalType": "bool", + "name": "newState", + "type": "bool" + } + ], + "name": "setState", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "state", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "triggerAssertError", + "outputs": [], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [], + "name": "triggerCustomError", + "outputs": [], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [], + "name": "triggerDivisionByZero", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [], + "name": "triggerOutOfBoundsError", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [], + "name": "triggerRequireError", + "outputs": [], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [], + "name": "triggerRevertError", + "outputs": [], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "value", + "type": "uint256" + } + ], + "name": "valueMatch", + "outputs": [], + "stateMutability": "payable", + "type": "function" + } +] \ No newline at end of file diff --git a/substrate/frame/revive/rpc/examples/js/abi/errorTester.ts b/substrate/frame/revive/rpc/examples/js/abi/errorTester.ts new file mode 100644 index 000000000000..d1ad60c1f55a --- /dev/null +++ b/substrate/frame/revive/rpc/examples/js/abi/errorTester.ts @@ -0,0 +1,106 @@ +export const abi = [ + { + inputs: [ + { + internalType: "string", + name: "message", + type: "string", + }, + ], + name: "CustomError", + type: "error", + }, + { + inputs: [ + { + internalType: "bool", + name: "newState", + type: "bool", + }, + ], + name: "setState", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [], + name: "state", + outputs: [ + { + internalType: "bool", + name: "", + type: "bool", + }, + ], + stateMutability: "view", + type: "function", + }, + { + inputs: [], + name: "triggerAssertError", + outputs: [], + stateMutability: "pure", + type: "function", + }, + { + inputs: [], + name: "triggerCustomError", + outputs: [], + stateMutability: "pure", + type: "function", + }, + { + inputs: [], + name: "triggerDivisionByZero", + outputs: [ + { + internalType: "uint256", + name: "", + type: "uint256", + }, + ], + stateMutability: "pure", + type: "function", + }, + { + inputs: [], + name: "triggerOutOfBoundsError", + outputs: [ + { + internalType: "uint256", + name: "", + type: "uint256", + }, + ], + stateMutability: "pure", + type: "function", + }, + { + inputs: [], + name: "triggerRequireError", + outputs: [], + stateMutability: "pure", + type: "function", + }, + { + inputs: [], + name: "triggerRevertError", + outputs: [], + stateMutability: "pure", + type: "function", + }, + { + inputs: [ + { + internalType: "uint256", + name: "value", + type: "uint256", + }, + ], + name: "valueMatch", + outputs: [], + stateMutability: "payable", + type: "function", + }, +] as const; diff --git a/substrate/frame/revive/rpc/examples/js/abi/event.json b/substrate/frame/revive/rpc/examples/js/abi/event.json index d36089fbc84e..a64c920c4068 100644 --- a/substrate/frame/revive/rpc/examples/js/abi/event.json +++ b/substrate/frame/revive/rpc/examples/js/abi/event.json @@ -1,34 +1,34 @@ [ - { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "internalType": "address", - "name": "sender", - "type": "address" - }, - { - "indexed": false, - "internalType": "uint256", - "name": "value", - "type": "uint256" - }, - { - "indexed": false, - "internalType": "string", - "name": "message", - "type": "string" - } - ], - "name": "ExampleEvent", - "type": "event" - }, - { - "inputs": [], - "name": "triggerEvent", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - } -] + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "sender", + "type": "address" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "value", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "string", + "name": "message", + "type": "string" + } + ], + "name": "ExampleEvent", + "type": "event" + }, + { + "inputs": [], + "name": "triggerEvent", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + } +] \ No newline at end of file diff --git a/substrate/frame/revive/rpc/examples/js/abi/event.ts b/substrate/frame/revive/rpc/examples/js/abi/event.ts new file mode 100644 index 000000000000..317ed00b92f9 --- /dev/null +++ b/substrate/frame/revive/rpc/examples/js/abi/event.ts @@ -0,0 +1,34 @@ +export const abi = [ + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: "address", + name: "sender", + type: "address", + }, + { + indexed: false, + internalType: "uint256", + name: "value", + type: "uint256", + }, + { + indexed: false, + internalType: "string", + name: "message", + type: "string", + }, + ], + name: "ExampleEvent", + type: "event", + }, + { + inputs: [], + name: "triggerEvent", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, +] as const; diff --git a/substrate/frame/revive/rpc/examples/js/abi/piggyBank.json b/substrate/frame/revive/rpc/examples/js/abi/piggyBank.json index 2c2cfd5f7533..e6655889e21a 100644 --- a/substrate/frame/revive/rpc/examples/js/abi/piggyBank.json +++ b/substrate/frame/revive/rpc/examples/js/abi/piggyBank.json @@ -1,65 +1,65 @@ [ - { - "inputs": [], - "stateMutability": "nonpayable", - "type": "constructor" - }, - { - "inputs": [], - "name": "deposit", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "payable", - "type": "function" - }, - { - "inputs": [], - "name": "getDeposit", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [], - "name": "owner", - "outputs": [ - { - "internalType": "address", - "name": "", - "type": "address" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "uint256", - "name": "withdrawAmount", - "type": "uint256" - } - ], - "name": "withdraw", - "outputs": [ - { - "internalType": "uint256", - "name": "remainingBal", - "type": "uint256" - } - ], - "stateMutability": "nonpayable", - "type": "function" - } -] + { + "inputs": [], + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "inputs": [], + "name": "deposit", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [], + "name": "getDeposit", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "owner", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "withdrawAmount", + "type": "uint256" + } + ], + "name": "withdraw", + "outputs": [ + { + "internalType": "uint256", + "name": "remainingBal", + "type": "uint256" + } + ], + "stateMutability": "nonpayable", + "type": "function" + } +] \ No newline at end of file diff --git a/substrate/frame/revive/rpc/examples/js/abi/piggyBank.ts b/substrate/frame/revive/rpc/examples/js/abi/piggyBank.ts new file mode 100644 index 000000000000..bc685e0b827a --- /dev/null +++ b/substrate/frame/revive/rpc/examples/js/abi/piggyBank.ts @@ -0,0 +1,65 @@ +export const abi = [ + { + inputs: [], + stateMutability: "nonpayable", + type: "constructor", + }, + { + inputs: [], + name: "deposit", + outputs: [ + { + internalType: "uint256", + name: "", + type: "uint256", + }, + ], + stateMutability: "payable", + type: "function", + }, + { + inputs: [], + name: "getDeposit", + outputs: [ + { + internalType: "uint256", + name: "", + type: "uint256", + }, + ], + stateMutability: "view", + type: "function", + }, + { + inputs: [], + name: "owner", + outputs: [ + { + internalType: "address", + name: "", + type: "address", + }, + ], + stateMutability: "view", + type: "function", + }, + { + inputs: [ + { + internalType: "uint256", + name: "withdrawAmount", + type: "uint256", + }, + ], + name: "withdraw", + outputs: [ + { + internalType: "uint256", + name: "remainingBal", + type: "uint256", + }, + ], + stateMutability: "nonpayable", + type: "function", + }, +] as const; diff --git a/substrate/frame/revive/rpc/examples/js/bun.lockb b/substrate/frame/revive/rpc/examples/js/bun.lockb index 700dca51da2ad3f843e890258b59c16fd4df6457..0ff3d54157db21a636e30076634343a24c812dca 100755 GIT binary patch delta 9083 zcmeG?X;@UpvgZr~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xgS_c}q>!JpbO@7?a8(UfIuFe$TQg&6(`#b ztpW<(N3&fVFH(yX`fL`8n(S{t=aWsG;KIulBK7DTyb@ zvrgpmFj%Dt5S2eHbf7)Uy1&fA``*qfFJ&4bPhd^nPUZ<%iCGyLoXjK^pO?vHadJ}x zK;sGo+{`>4hc7_<(nP24DApiXvIPs@Y}#Yl3A{`eDOjuyddF8~!D$PB(4Y{$N8++F zIJxObS%sN|_TQ7ly|_P8OEm6Zv<2z-E)=Byh4mdFU%*M>3i5J!nJKK~tlSJv9(I?( zcN7@nyE@3WK|5y{ppASPFTy{B6lnHW(JXw!OSplPo6DKXO5h}>=W!DFTo#`*H7lPO zZKNat+atke!67M{qQb1)Bz#Rx6C`oD+4w1ho0)`_)sd(}Z;2WUZb7l4)&qT^!9azD zueyYja=A(QiClb-m;_TYh$SRxUB^{H$D9`+cC*tRps7gS=^k7G#kHcX^m+BcUmXEU zKkZ*UeIlKbd9bf@x!HV9B3E>4VU-BN_=zYhRoC=83P*qk{4)t=&>lNLPf`-JldO_X z`rB0i4+dDq57rr8AHY%ietp3GJzh2c&xvyutVn&kq_)-%+;Dazxf!Nv(c=9wv4hD{>09wEs0Xth_VxOlw zx!`vTz{Lp8Rk)4E(ldCOJUFisQv z63Z4Fq$42+8cEcMT?12M)juZi`^zxZ_}&>CoC`||j;3tv(k$W}@&6MII{t}5EMi+? zwvZ1zE%g zmK8A(D&(UZ!4-WVi^lLwl!=dIfhc;YA%!X6=OUnD1T@qz!_a|zK?G#7NL&gD`%L14Vpt(@@^lg|(UF+%`;z^BPEp+34&I!KGWW&@8bLJMcnd9!<2S{M~+ z^Xrjgnl`e`7^=oe%H<>{PqpS|3Rs!=O3LR=Q6GH?R}1NGm`fYEUM-rbccR7Mj#SN|7sAwr`7 diff --git a/substrate/frame/revive/rpc/examples/js/contracts/ErrorTester.sol b/substrate/frame/revive/rpc/examples/js/contracts/ErrorTester.sol new file mode 100644 index 000000000000..d32cc7e11a9a --- /dev/null +++ b/substrate/frame/revive/rpc/examples/js/contracts/ErrorTester.sol @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +contract ErrorTester { + + // Payable function that can be used to test unsifficient funds errors + function valueMatch(uint value) public payable { + require(msg.value == value , "msg.value does not match value"); + } + + bool public state; + + function setState(bool newState) public { + state = newState; + } + + // Trigger a require statement failure with a custom error message + function triggerRequireError() public pure { + require(false, "This is a require error"); + } + + // Trigger an assert statement failure + function triggerAssertError() public pure { + assert(false); + } + + // Trigger a revert statement with a custom error message + function triggerRevertError() public pure { + revert("This is a revert error"); + } + + // Trigger a division by zero error + function triggerDivisionByZero() public pure returns (uint256) { + uint256 a = 1; + uint256 b = 0; + return a / b; + } + + // Trigger an out-of-bounds array access + function triggerOutOfBoundsError() public pure returns (uint256) { + uint256[] memory arr = new uint256[](1); + return arr[2]; + } + + // Trigger a custom error + error CustomError(string message); + + function triggerCustomError() public pure { + revert CustomError("This is a custom error"); + } +} + diff --git a/substrate/frame/revive/rpc/examples/js/package.json b/substrate/frame/revive/rpc/examples/js/package.json index 3ae1f0fbd799..fa101dcad1d9 100644 --- a/substrate/frame/revive/rpc/examples/js/package.json +++ b/substrate/frame/revive/rpc/examples/js/package.json @@ -6,16 +6,16 @@ "scripts": { "dev": "vite", "build": "tsc && vite build", - "preview": "vite preview", - "generate-types": "typechain --target=ethers-v6 'abi/*.json'" + "preview": "vite preview" }, "dependencies": { - "@typechain/ethers-v6": "^0.5.1", "ethers": "^6.13.4", + "prettier": "^3.3.3", "solc": "^0.8.28", - "typechain": "^8.3.2" + "viem": "^2.21.47" }, "devDependencies": { + "@types/bun": "^1.1.13", "typescript": "^5.5.3", "vite": "^5.4.8" } diff --git a/substrate/frame/revive/rpc/examples/js/pvm/errorTester.polkavm b/substrate/frame/revive/rpc/examples/js/pvm/errorTester.polkavm new file mode 100644 index 0000000000000000000000000000000000000000..aebe24c4c0f597fb3d0171a9f527bc86d2d77b93 GIT binary patch literal 12890 zcmds73v?URn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ObNyoR*tb}s5j~OKc7(3i8NN&zYQxsMT$WYOoKT?}1i8fRf-*mjwgy>*{?hrx+#BHgDpx~@{`_lZX)SN_+EAl1o P|DsnIq4bp_3F&_TlmMih literal 0 HcmV?d00001 diff --git a/substrate/frame/revive/rpc/examples/js/src/balance.ts b/substrate/frame/revive/rpc/examples/js/src/balance.ts new file mode 100644 index 000000000000..0c5e59d07849 --- /dev/null +++ b/substrate/frame/revive/rpc/examples/js/src/balance.ts @@ -0,0 +1,9 @@ +import { walletClient } from './lib.ts' + +const recipient = '0x8D97689C9818892B700e27F316cc3E41e17fBeb9' +try { + console.log(`Recipient balance: ${await walletClient.getBalance({ address: recipient })}`) +} catch (err) { + console.error(err) +} + diff --git a/substrate/frame/revive/rpc/examples/js/src/build-contracts.ts b/substrate/frame/revive/rpc/examples/js/src/build-contracts.ts index c6b7700d1ccf..3bafdd30a34d 100644 --- a/substrate/frame/revive/rpc/examples/js/src/build-contracts.ts +++ b/substrate/frame/revive/rpc/examples/js/src/build-contracts.ts @@ -1,11 +1,23 @@ import { compile } from '@parity/revive' +import { format } from 'prettier' +import { parseArgs } from 'node:util' import solc from 'solc' import { readFileSync, writeFileSync } from 'fs' import { join } from 'path' type CompileInput = Parameters[0] -type CompileOutput = Awaited> -type Abi = CompileOutput['contracts'][string][string]['abi'] + +const { + values: { filter }, +} = parseArgs({ + args: process.argv.slice(2), + options: { + filter: { + type: 'string', + short: 'f', + }, + }, +}) function evmCompile(sources: CompileInput) { const input = { @@ -27,9 +39,9 @@ console.log('Compiling contracts...') const input = [ { file: 'Event.sol', contract: 'EventExample', keypath: 'event' }, - { file: 'Revert.sol', contract: 'RevertExample', keypath: 'revert' }, { file: 'PiggyBank.sol', contract: 'PiggyBank', keypath: 'piggyBank' }, -] + { file: 'ErrorTester.sol', contract: 'ErrorTester', keypath: 'errorTester' }, +].filter(({ keypath }) => !filter || keypath.includes(filter)) for (const { keypath, contract, file } of input) { const input = { @@ -42,6 +54,12 @@ for (const { keypath, contract, file } of input) { const entry = out.contracts[file][contract] writeFileSync(join('evm', `${keypath}.bin`), Buffer.from(entry.evm.bytecode.object, 'hex')) writeFileSync(join('abi', `${keypath}.json`), JSON.stringify(entry.abi, null, 2)) + writeFileSync( + join('abi', `${keypath}.ts`), + await format(`export const abi = ${JSON.stringify(entry.abi, null, 2)} as const`, { + parser: 'typescript', + }) + ) } { diff --git a/substrate/frame/revive/rpc/examples/js/src/event.ts b/substrate/frame/revive/rpc/examples/js/src/event.ts index 94cc2560272e..2e672a9772ff 100644 --- a/substrate/frame/revive/rpc/examples/js/src/event.ts +++ b/substrate/frame/revive/rpc/examples/js/src/event.ts @@ -1,15 +1,29 @@ //! Run with bun run script-event.ts -import { call, getContract, deploy } from './lib.ts' - -try { - const { abi, bytecode } = getContract('event') - const contract = await deploy(bytecode, abi) - const receipt = await call('triggerEvent', await contract.getAddress(), abi) - if (receipt) { - for (const log of receipt.logs) { - console.log('Event log:', JSON.stringify(log, null, 2)) - } - } -} catch (err) { - console.error(err) + +import { abi } from '../abi/event.ts' +import { assert, getByteCode, walletClient } from './lib.ts' + +const deployHash = await walletClient.deployContract({ + abi, + bytecode: getByteCode('event'), +}) +const deployReceipt = await walletClient.waitForTransactionReceipt({ hash: deployHash }) +const contractAddress = deployReceipt.contractAddress +console.log('Contract deployed:', contractAddress) +assert(contractAddress, 'Contract address should be set') + +const { request } = await walletClient.simulateContract({ + account: walletClient.account, + address: contractAddress, + abi, + functionName: 'triggerEvent', +}) + +const hash = await walletClient.writeContract(request) +const receipt = await walletClient.waitForTransactionReceipt({ hash }) +console.log(`Receipt: ${receipt.status}`) +console.log(`Logs receipt: ${receipt.status}`) + +for (const log of receipt.logs) { + console.log('Event log:', log) } diff --git a/substrate/frame/revive/rpc/examples/js/src/geth-diff.test.ts b/substrate/frame/revive/rpc/examples/js/src/geth-diff.test.ts new file mode 100644 index 000000000000..ef4cde1824e4 --- /dev/null +++ b/substrate/frame/revive/rpc/examples/js/src/geth-diff.test.ts @@ -0,0 +1,398 @@ +import { spawn, spawnSync, Subprocess } from 'bun' +import { join } from 'path' +import { readFileSync } from 'fs' +import { afterAll, afterEach, beforeAll, describe, expect, test } from 'bun:test' +import { + createWalletClient, + defineChain, + encodeFunctionData, + Hex, + http, + parseEther, + publicActions, +} from 'viem' +import { privateKeyToAccount } from 'viem/accounts' +import { abi } from '../abi/errorTester' + +export function getByteCode(name: string, evm: boolean): Hex { + const bytecode = evm ? readFileSync(`evm/${name}.bin`) : readFileSync(`pvm/${name}.polkavm`) + return `0x${Buffer.from(bytecode).toString('hex')}` +} + +type JsonRpcError = { + code: number + message: string + data: Hex +} + +function killProcessOnPort(port: number) { + // Check which process is using the specified port + const result = spawnSync(['lsof', '-ti', `:${port}`]) + const output = result.stdout.toString().trim() + + if (output) { + console.log(`Port ${port} is in use. Killing process...`) + const pids = output.split('\n') + + // Kill each process using the port + for (const pid of pids) { + spawnSync(['kill', '-9', pid]) + console.log(`Killed process with PID: ${pid}`) + } + } +} + +let jsonRpcErrors: JsonRpcError[] = [] +async function createEnv(name: 'geth' | 'kitchensink') { + const gethPort = process.env.GETH_PORT || '8546' + const kitchensinkPort = process.env.KITCHENSINK_PORT || '8545' + const url = `http://localhost:${name == 'geth' ? gethPort : kitchensinkPort}` + const chain = defineChain({ + id: name == 'geth' ? 1337 : 420420420, + name, + nativeCurrency: { + name: 'Westie', + symbol: 'WST', + decimals: 18, + }, + rpcUrls: { + default: { + http: [url], + }, + }, + testnet: true, + }) + + const transport = http(url, { + onFetchResponse: async (response) => { + const raw = await response.clone().json() + if (raw.error) { + jsonRpcErrors.push(raw.error as JsonRpcError) + } + }, + }) + + const wallet = createWalletClient({ + transport, + chain, + }) + + const [account] = await wallet.getAddresses() + const serverWallet = createWalletClient({ + account, + transport, + chain, + }).extend(publicActions) + + const accountWallet = createWalletClient({ + account: privateKeyToAccount( + '0xa872f6cbd25a0e04a08b1e21098017a9e6194d101d75e13111f71410c59cd57f' + ), + transport, + chain, + }).extend(publicActions) + + return { serverWallet, accountWallet, evm: name == 'geth' } +} + +// wait for http request to return 200 +export function waitForHealth(url: string) { + return new Promise((resolve, reject) => { + const start = Date.now() + const interval = setInterval(() => { + fetch(url) + .then((res) => { + if (res.status === 200) { + clearInterval(interval) + resolve() + } + }) + .catch(() => { + const elapsed = Date.now() - start + if (elapsed > 30_000) { + clearInterval(interval) + reject(new Error('hit timeout')) + } + }) + }, 1000) + }) +} + +const procs: Subprocess[] = [] +if (!process.env.USE_LIVE_SERVERS) { + procs.push( + // Run geth on port 8546 + // + (() => { + killProcessOnPort(8546) + return spawn( + 'geth --http --http.api web3,eth,debug,personal,net --http.port 8546 --dev --verbosity 0'.split( + ' ' + ), + { stdout: Bun.file('/tmp/geth.out.log'), stderr: Bun.file('/tmp/geth.err.log') } + ) + })(), + //Run the substate node + (() => { + killProcessOnPort(9944) + return spawn( + [ + './target/debug/substrate-node', + '--dev', + '-l=error,evm=debug,sc_rpc_server=info,runtime::revive=debug', + ], + { + stdout: Bun.file('/tmp/kitchensink.out.log'), + stderr: Bun.file('/tmp/kitchensink.err.log'), + cwd: join(process.env.HOME!, 'polkadot-sdk'), + } + ) + })() + , + // Run eth-rpc on 8545 + await (async () => { + killProcessOnPort(8545) + const proc = spawn( + [ + './target/debug/eth-rpc', + '--dev', + '--node-rpc-url=ws://localhost:9944', + '-l=rpc-metrics=debug,eth-rpc=debug', + ], + { + stdout: Bun.file('/tmp/eth-rpc.out.log'), + stderr: Bun.file('/tmp/eth-rpc.err.log'), + cwd: join(process.env.HOME!, 'polkadot-sdk'), + } + ) + await waitForHealth('http://localhost:8545/health').catch() + return proc + })() + ) +} + +afterEach(() => { + jsonRpcErrors = [] +}) + +afterAll(async () => { + procs.forEach((proc) => proc.kill()) +}) + +const envs = await Promise.all([createEnv('geth'), createEnv('kitchensink')]) + +for (const env of envs) { + describe(env.serverWallet.chain.name, () => { + let errorTesterAddr: Hex = '0x' + beforeAll(async () => { + const hash = await env.serverWallet.deployContract({ + abi, + bytecode: getByteCode('errorTester', env.evm), + }) + const deployReceipt = await env.serverWallet.waitForTransactionReceipt({ hash }) + if (!deployReceipt.contractAddress) throw new Error('Contract address should be set') + errorTesterAddr = deployReceipt.contractAddress + }) + + test('triggerAssertError', async () => { + expect.assertions(3) + try { + await env.accountWallet.readContract({ + address: errorTesterAddr, + abi, + functionName: 'triggerAssertError', + }) + } catch (err) { + const lastJsonRpcError = jsonRpcErrors.pop() + expect(lastJsonRpcError?.code).toBe(3) + expect(lastJsonRpcError?.data).toBe( + '0x4e487b710000000000000000000000000000000000000000000000000000000000000001' + ) + expect(lastJsonRpcError?.message).toBe('execution reverted: assert(false)') + } + }) + + test('triggerRevertError', async () => { + expect.assertions(3) + try { + await env.accountWallet.readContract({ + address: errorTesterAddr, + abi, + functionName: 'triggerRevertError', + }) + } catch (err) { + const lastJsonRpcError = jsonRpcErrors.pop() + expect(lastJsonRpcError?.code).toBe(3) + expect(lastJsonRpcError?.message).toBe('execution reverted: This is a revert error') + expect(lastJsonRpcError?.data).toBe( + '0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001654686973206973206120726576657274206572726f7200000000000000000000' + ) + } + }) + + test('triggerDivisionByZero', async () => { + expect.assertions(3) + try { + await env.accountWallet.readContract({ + address: errorTesterAddr, + abi, + functionName: 'triggerDivisionByZero', + }) + } catch (err) { + const lastJsonRpcError = jsonRpcErrors.pop() + expect(lastJsonRpcError?.code).toBe(3) + expect(lastJsonRpcError?.data).toBe( + '0x4e487b710000000000000000000000000000000000000000000000000000000000000012' + ) + expect(lastJsonRpcError?.message).toBe( + 'execution reverted: division or modulo by zero' + ) + } + }) + + test('triggerOutOfBoundsError', async () => { + expect.assertions(3) + try { + await env.accountWallet.readContract({ + address: errorTesterAddr, + abi, + functionName: 'triggerOutOfBoundsError', + }) + } catch (err) { + const lastJsonRpcError = jsonRpcErrors.pop() + expect(lastJsonRpcError?.code).toBe(3) + expect(lastJsonRpcError?.data).toBe( + '0x4e487b710000000000000000000000000000000000000000000000000000000000000032' + ) + expect(lastJsonRpcError?.message).toBe( + 'execution reverted: out-of-bounds access of an array or bytesN' + ) + } + }) + + test('triggerCustomError', async () => { + expect.assertions(3) + try { + await env.accountWallet.readContract({ + address: errorTesterAddr, + abi, + functionName: 'triggerCustomError', + }) + } catch (err) { + const lastJsonRpcError = jsonRpcErrors.pop() + expect(lastJsonRpcError?.code).toBe(3) + expect(lastJsonRpcError?.data).toBe( + '0x8d6ea8be0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001654686973206973206120637573746f6d206572726f7200000000000000000000' + ) + expect(lastJsonRpcError?.message).toBe('execution reverted') + } + }) + + test('eth_call (not enough funds)', async () => { + expect.assertions(3) + try { + await env.accountWallet.simulateContract({ + address: errorTesterAddr, + abi, + functionName: 'valueMatch', + value: parseEther('10'), + args: [parseEther('10')], + }) + } catch (err) { + const lastJsonRpcError = jsonRpcErrors.pop() + expect(lastJsonRpcError?.code).toBe(-32000) + expect(lastJsonRpcError?.message).toInclude('insufficient funds') + expect(lastJsonRpcError?.data).toBeUndefined() + } + }) + + test('eth_estimate (not enough funds)', async () => { + expect.assertions(3) + try { + await env.accountWallet.estimateContractGas({ + address: errorTesterAddr, + abi, + functionName: 'valueMatch', + value: parseEther('10'), + args: [parseEther('10')], + }) + } catch (err) { + const lastJsonRpcError = jsonRpcErrors.pop() + expect(lastJsonRpcError?.code).toBe(-32000) + expect(lastJsonRpcError?.message).toInclude('insufficient funds') + expect(lastJsonRpcError?.data).toBeUndefined() + } + }) + + test('eth_estimate (revert)', async () => { + expect.assertions(3) + try { + await env.serverWallet.estimateContractGas({ + address: errorTesterAddr, + abi, + functionName: 'valueMatch', + value: parseEther('11'), + args: [parseEther('10')], + }) + } catch (err) { + const lastJsonRpcError = jsonRpcErrors.pop() + expect(lastJsonRpcError?.code).toBe(3) + expect(lastJsonRpcError?.message).toBe( + 'execution reverted: msg.value does not match value' + ) + expect(lastJsonRpcError?.data).toBe( + '0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001e6d73672e76616c756520646f6573206e6f74206d617463682076616c75650000' + ) + } + }) + + test('eth_get_balance (no account)', async () => { + const balance = await env.serverWallet.getBalance({ + address: '0x0000000000000000000000000000000000000123', + }) + expect(balance).toBe(0n) + }) + + test('eth_estimate (not enough funds to cover gas specified)', async () => { + expect.assertions(4) + try { + let balance = await env.serverWallet.getBalance(env.accountWallet.account) + expect(balance).toBe(0n) + + await env.accountWallet.estimateContractGas({ + address: errorTesterAddr, + abi, + functionName: 'setState', + args: [true], + }) + } catch (err) { + const lastJsonRpcError = jsonRpcErrors.pop() + expect(lastJsonRpcError?.code).toBe(-32000) + expect(lastJsonRpcError?.message).toInclude('insufficient funds') + expect(lastJsonRpcError?.data).toBeUndefined() + } + }) + + test('eth_estimate (no gas specified)', async () => { + let balance = await env.serverWallet.getBalance(env.accountWallet.account) + expect(balance).toBe(0n) + + const data = encodeFunctionData({ + abi, + functionName: 'setState', + args: [true], + }) + + await env.accountWallet.request({ + method: 'eth_estimateGas', + params: [ + { + data, + from: env.accountWallet.account.address, + to: errorTesterAddr, + }, + ], + }) + }) + }) +} diff --git a/substrate/frame/revive/rpc/examples/js/src/lib.ts b/substrate/frame/revive/rpc/examples/js/src/lib.ts index 975d8faf15b3..d1f14bbc064e 100644 --- a/substrate/frame/revive/rpc/examples/js/src/lib.ts +++ b/substrate/frame/revive/rpc/examples/js/src/lib.ts @@ -1,22 +1,18 @@ -import { - Contract, - ContractFactory, - JsonRpcProvider, - TransactionReceipt, - TransactionResponse, - Wallet, -} from 'ethers' import { readFileSync } from 'node:fs' -import type { compile } from '@parity/revive' import { spawn } from 'node:child_process' import { parseArgs } from 'node:util' -import { BaseContract } from 'ethers' - -type CompileOutput = Awaited> -type Abi = CompileOutput['contracts'][string][string]['abi'] +import { + createWalletClient, + defineChain, + Hex, + http, + parseEther, + publicActions, +} from 'viem' +import { privateKeyToAccount } from 'viem/accounts' const { - values: { geth, westend, ['private-key']: privateKey }, + values: { geth, proxy, westend, endowment, ['private-key']: privateKey }, } = parseArgs({ args: process.argv.slice(2), options: { @@ -24,6 +20,13 @@ const { type: 'string', short: 'k', }, + endowment: { + type: 'string', + short: 'e', + }, + proxy: { + type: 'boolean', + }, geth: { type: 'boolean', }, @@ -42,7 +45,7 @@ if (geth) { '--http.api', 'web3,eth,debug,personal,net', '--http.port', - '8546', + process.env.GETH_PORT ?? '8546', '--dev', '--verbosity', '0', @@ -55,56 +58,78 @@ if (geth) { await new Promise((resolve) => setTimeout(resolve, 500)) } -export const provider = new JsonRpcProvider( - westend +const rpcUrl = proxy + ? 'http://localhost:8080' + : westend ? 'https://westend-asset-hub-eth-rpc.polkadot.io' : geth ? 'http://localhost:8546' : 'http://localhost:8545' -) -export const signer = privateKey ? new Wallet(privateKey, provider) : await provider.getSigner() -console.log(`Signer address: ${await signer.getAddress()}, Nonce: ${await signer.getNonce()}`) +export const chain = defineChain({ + id: geth ? 1337 : 420420420, + name: 'Asset Hub Westend', + network: 'asset-hub', + nativeCurrency: { + name: 'Westie', + symbol: 'WST', + decimals: 18, + }, + rpcUrls: { + default: { + http: [rpcUrl], + }, + }, + testnet: true, +}) + +const wallet = createWalletClient({ + transport: http(), + chain, +}) +const [account] = await wallet.getAddresses() +export const serverWalletClient = createWalletClient({ + account, + transport: http(), + chain, +}) + +export const walletClient = await (async () => { + if (privateKey) { + const account = privateKeyToAccount(`0x${privateKey}`) + console.log(`Wallet address ${account.address}`) + + const wallet = createWalletClient({ + account, + transport: http(), + chain, + }) + + if (endowment) { + await serverWalletClient.sendTransaction({ + to: account.address, + value: parseEther(endowment), + }) + console.log(`Endowed address ${account.address} with: ${endowment}`) + } + + return wallet.extend(publicActions) + } else { + return serverWalletClient.extend(publicActions) + } +})() /** * Get one of the pre-built contracts * @param name - the contract name */ -export function getContract(name: string): { abi: Abi; bytecode: string } { +export function getByteCode(name: string): Hex { const bytecode = geth ? readFileSync(`evm/${name}.bin`) : readFileSync(`pvm/${name}.polkavm`) - const abi = JSON.parse(readFileSync(`abi/${name}.json`, 'utf8')) as Abi - return { abi, bytecode: Buffer.from(bytecode).toString('hex') } + return `0x${Buffer.from(bytecode).toString('hex')}` } -/** - * Deploy a contract - * @returns the contract address - **/ -export async function deploy(bytecode: string, abi: Abi, args: any[] = []): Promise { - console.log('Deploying contract with', args) - const contractFactory = new ContractFactory(abi, bytecode, signer) - - const contract = await contractFactory.deploy(args) - await contract.waitForDeployment() - const address = await contract.getAddress() - console.log(`Contract deployed: ${address}`) - - return contract -} - -/** - * Call a contract - **/ -export async function call( - method: string, - address: string, - abi: Abi, - args: any[] = [], - opts: { value?: bigint } = {} -): Promise { - console.log(`Calling ${method} at ${address} with`, args, opts) - const contract = new Contract(address, abi, signer) - const tx = (await contract[method](...args, opts)) as TransactionResponse - console.log('Call transaction hash:', tx.hash) - return tx.wait() +export function assert(condition: any, message: string): asserts condition { + if (!condition) { + throw new Error(message) + } } diff --git a/substrate/frame/revive/rpc/examples/js/src/piggy-bank.ts b/substrate/frame/revive/rpc/examples/js/src/piggy-bank.ts index 7a8edbde3662..0040b0c78dc4 100644 --- a/substrate/frame/revive/rpc/examples/js/src/piggy-bank.ts +++ b/substrate/frame/revive/rpc/examples/js/src/piggy-bank.ts @@ -1,24 +1,69 @@ -import { provider, call, getContract, deploy } from './lib.ts' -import { parseEther } from 'ethers' -import { PiggyBank } from '../types/ethers-contracts/PiggyBank' +import { assert, getByteCode, walletClient } from './lib.ts' +import { abi } from '../abi/piggyBank.ts' +import { parseEther } from 'viem' -try { - const { abi, bytecode } = getContract('piggyBank') - const contract = (await deploy(bytecode, abi)) as PiggyBank - const address = await contract.getAddress() +const hash = await walletClient.deployContract({ + abi, + bytecode: getByteCode('piggyBank'), +}) +const deployReceipt = await walletClient.waitForTransactionReceipt({ hash }) +const contractAddress = deployReceipt.contractAddress +console.log('Contract deployed:', contractAddress) +assert(contractAddress, 'Contract address should be set') - let receipt = await call('deposit', address, abi, [], { - value: parseEther('10.0'), +// Deposit 10 WST +{ + const result = await walletClient.estimateContractGas({ + account: walletClient.account, + address: contractAddress, + abi, + functionName: 'deposit', + value: parseEther('10'), }) - console.log('Deposit receipt:', receipt?.status) - console.log(`Contract balance: ${await provider.getBalance(address)}`) - console.log('deposit: ', await contract.getDeposit()) + console.log(`Gas estimate: ${result}`) - receipt = await call('withdraw', address, abi, [parseEther('5.0')]) - console.log('Withdraw receipt:', receipt?.status) - console.log(`Contract balance: ${await provider.getBalance(address)}`) - console.log('deposit: ', await contract.getDeposit()) -} catch (err) { - console.error(err) + const { request } = await walletClient.simulateContract({ + account: walletClient.account, + address: contractAddress, + abi, + functionName: 'deposit', + value: parseEther('10'), + }) + + request.nonce = 0 + const hash = await walletClient.writeContract(request) + + const receipt = await walletClient.waitForTransactionReceipt({ hash }) + console.log(`Deposit receipt: ${receipt.status}`) + if (process.env.STOP) { + process.exit(0) + } +} + +// Withdraw 5 WST +{ + const { request } = await walletClient.simulateContract({ + account: walletClient.account, + address: contractAddress, + abi, + functionName: 'withdraw', + args: [parseEther('5')], + }) + + const hash = await walletClient.writeContract(request) + const receipt = await walletClient.waitForTransactionReceipt({ hash }) + console.log(`Withdraw receipt: ${receipt.status}`) + + // Check remaining balance + const balance = await walletClient.readContract({ + address: contractAddress, + abi, + functionName: 'getDeposit', + }) + + console.log(`Get deposit: ${balance}`) + console.log( + `Get contract balance: ${await walletClient.getBalance({ address: contractAddress })}` + ) } diff --git a/substrate/frame/revive/rpc/examples/js/src/revert.ts b/substrate/frame/revive/rpc/examples/js/src/revert.ts deleted file mode 100644 index ea1bf4eceeb9..000000000000 --- a/substrate/frame/revive/rpc/examples/js/src/revert.ts +++ /dev/null @@ -1,10 +0,0 @@ -//! Run with bun run script-revert.ts -import { call, getContract, deploy } from './lib.ts' - -try { - const { abi, bytecode } = getContract('revert') - const contract = await deploy(bytecode, abi) - await call('doRevert', await contract.getAddress(), abi) -} catch (err) { - console.error(err) -} diff --git a/substrate/frame/revive/rpc/examples/js/src/transfer.ts b/substrate/frame/revive/rpc/examples/js/src/transfer.ts index ae2dd50f2af8..aef9a487b0c0 100644 --- a/substrate/frame/revive/rpc/examples/js/src/transfer.ts +++ b/substrate/frame/revive/rpc/examples/js/src/transfer.ts @@ -1,17 +1,18 @@ -import { parseEther } from 'ethers' -import { provider, signer } from './lib.ts' +import { parseEther } from 'viem' +import { walletClient } from './lib.ts' const recipient = '0x75E480dB528101a381Ce68544611C169Ad7EB342' try { - console.log(`Signer balance: ${await provider.getBalance(signer.address)}`) - console.log(`Recipient balance: ${await provider.getBalance(recipient)}`) - await signer.sendTransaction({ + console.log(`Signer balance: ${await walletClient.getBalance(walletClient.account)}`) + console.log(`Recipient balance: ${await walletClient.getBalance({ address: recipient })}`) + + await walletClient.sendTransaction({ to: recipient, value: parseEther('1.0'), }) console.log(`Sent: ${parseEther('1.0')}`) - console.log(`Signer balance: ${await provider.getBalance(signer.address)}`) - console.log(`Recipient balance: ${await provider.getBalance(recipient)}`) + console.log(`Signer balance: ${await walletClient.getBalance(walletClient.account)}`) + console.log(`Recipient balance: ${await walletClient.getBalance({ address: recipient })}`) } catch (err) { console.error(err) } diff --git a/substrate/frame/revive/rpc/src/client.rs b/substrate/frame/revive/rpc/src/client.rs index d37f1d760065..d7cbca520113 100644 --- a/substrate/frame/revive/rpc/src/client.rs +++ b/substrate/frame/revive/rpc/src/client.rs @@ -118,16 +118,40 @@ fn unwrap_call_err(err: &subxt::error::RpcError) -> Option { fn extract_revert_message(exec_data: &[u8]) -> Option { let function_selector = exec_data.get(0..4)?; - // keccak256("Error(string)") - let expected_selector = [0x08, 0xC3, 0x79, 0xA0]; - if function_selector != expected_selector { - return None; - } + match function_selector { + // assert(false) + [0x4E, 0x48, 0x7B, 0x71] => { + let panic_code: u32 = U256::from_big_endian(&exec_data.get(4..36)?).try_into().ok()?; + + // See https://docs.soliditylang.org/en/latest/control-structures.html#panic-via-assert-and-error-via-require + let msg = match panic_code { + 0x00 => "generic panic", + 0x01 => "assert(false)", + 0x11 => "arithmetic underflow or overflow", + 0x12 => "division or modulo by zero", + 0x21 => "enum overflow", + 0x22 => "invalid encoded storage byte array accessed", + 0x31 => "out-of-bounds array access; popping on an empty array", + 0x32 => "out-of-bounds access of an array or bytesN", + 0x41 => "out of memory", + 0x51 => "uninitialized function", + code => return Some(format!("execution reverted: unknown panic code: {code:#x}")), + }; - let decoded = ethabi::decode(&[ethabi::ParamType::String], &exec_data[4..]).ok()?; - match decoded.first()? { - ethabi::Token::String(msg) => Some(msg.to_string()), - _ => None, + Some(format!("execution reverted: {msg}")) + }, + // revert(string) + [0x08, 0xC3, 0x79, 0xA0] => { + let decoded = ethabi::decode(&[ethabi::ParamType::String], &exec_data[4..]).ok()?; + if let Some(ethabi::Token::String(msg)) = decoded.first() { + return Some(format!("execution reverted: {msg}")) + } + Some("execution reverted".to_string()) + }, + _ => { + log::debug!(target: LOG_TARGET, "Unknown revert function selector: {function_selector:?}"); + Some("execution reverted".to_string()) + }, } } @@ -147,25 +171,26 @@ pub enum ClientError { #[error(transparent)] CodecError(#[from] codec::Error), /// The dry run failed. - #[error("Dry run failed: {0}")] + #[error("dry run failed: {0}")] DryRunFailed(String), /// Contract reverted - #[error("Execution reverted: {}", extract_revert_message(.0).unwrap_or_default())] + #[error("{}", extract_revert_message(.0).unwrap_or_default())] Reverted(Vec), /// A decimal conversion failed. - #[error("Conversion failed")] + #[error("conversion failed")] ConversionFailed, /// The block hash was not found. - #[error("Hash not found")] + #[error("hash not found")] BlockNotFound, /// The transaction fee could not be found - #[error("TransactionFeePaid event not found")] + #[error("transactionFeePaid event not found")] TxFeeNotFound, /// The cache is empty. - #[error("Cache is empty")] + #[error("cache is empty")] CacheEmpty, } +const REVERT_CODE: i32 = 3; // TODO convert error code to https://eips.ethereum.org/EIPS/eip-1474#error-codes impl From for ErrorObjectOwned { fn from(err: ClientError) -> Self { @@ -179,7 +204,7 @@ impl From for ErrorObjectOwned { }, ClientError::Reverted(data) => { let data = format!("0x{}", hex::encode(data)); - ErrorObjectOwned::owned::(CALL_EXECUTION_FAILED_CODE, msg, Some(data)) + ErrorObjectOwned::owned::(REVERT_CODE, msg, Some(data)) }, _ => ErrorObjectOwned::owned::(CALL_EXECUTION_FAILED_CODE, msg, None), } @@ -672,16 +697,6 @@ impl Client { } } - /// Dry run a transaction and returns the gas estimate for the transaction. - pub async fn estimate_gas( - &self, - tx: &GenericTransaction, - block: BlockNumberOrTagOrHash, - ) -> Result { - let dry_run = self.dry_run(tx, block).await?; - Ok(U256::from(dry_run.fee / GAS_PRICE as u128) + GAS_PRICE) - } - /// Get the nonce of the given address. pub async fn nonce( &self, diff --git a/substrate/frame/revive/rpc/src/lib.rs b/substrate/frame/revive/rpc/src/lib.rs index 6a324e63a857..b35497e34bd7 100644 --- a/substrate/frame/revive/rpc/src/lib.rs +++ b/substrate/frame/revive/rpc/src/lib.rs @@ -128,10 +128,24 @@ impl EthRpcServer for EthRpcServerImpl { async fn estimate_gas( &self, transaction: GenericTransaction, - _block: Option, + block: Option, ) -> RpcResult { - let result = self.client.estimate_gas(&transaction, BlockTag::Latest.into()).await?; - Ok(result) + // estimate_gas only fails returns even if the contract traps + let dry_run = self.client.dry_run(&transaction, block.unwrap_or_default().into()).await?; + + Ok(U256::from(dry_run.fee / GAS_PRICE as u128) + GAS_PRICE) + } + + async fn call( + &self, + transaction: GenericTransaction, + block: Option, + ) -> RpcResult { + let dry_run = self + .client + .dry_run(&transaction, block.unwrap_or_else(|| BlockTag::Latest.into())) + .await?; + Ok(dry_run.result.into()) } async fn send_raw_transaction(&self, transaction: Bytes) -> RpcResult { @@ -158,7 +172,10 @@ impl EthRpcServer for EthRpcServerImpl { gas_required.into(), storage_deposit, ); - self.client.submit(call).await?; + self.client.submit(call).await.map_err(|err| { + log::debug!(target: LOG_TARGET, "submit call failed: {err:?}"); + err + })?; log::debug!(target: LOG_TARGET, "send_raw_transaction hash: {hash:?}"); Ok(hash) } @@ -234,18 +251,6 @@ impl EthRpcServer for EthRpcServerImpl { Ok(self.accounts.iter().map(|account| account.address()).collect()) } - async fn call( - &self, - transaction: GenericTransaction, - block: Option, - ) -> RpcResult { - let dry_run = self - .client - .dry_run(&transaction, block.unwrap_or_else(|| BlockTag::Latest.into())) - .await?; - Ok(dry_run.result.into()) - } - async fn get_block_by_number( &self, block: BlockNumberOrTag, diff --git a/substrate/frame/revive/rpc/src/tests.rs b/substrate/frame/revive/rpc/src/tests.rs index 7734c8c57209..686cebcf657d 100644 --- a/substrate/frame/revive/rpc/src/tests.rs +++ b/substrate/frame/revive/rpc/src/tests.rs @@ -237,6 +237,7 @@ async fn revert_call() -> anyhow::Result<()> { let call_err = unwrap_call_err!(err.source().unwrap()); assert_eq!(call_err.message(), "Execution reverted: revert message"); + assert_eq!(call_err.code(), 3); Ok(()) } diff --git a/substrate/frame/revive/src/evm/api/rpc_types_gen.rs b/substrate/frame/revive/src/evm/api/rpc_types_gen.rs index 5037ec05d881..1370ea2b7612 100644 --- a/substrate/frame/revive/src/evm/api/rpc_types_gen.rs +++ b/substrate/frame/revive/src/evm/api/rpc_types_gen.rs @@ -114,7 +114,7 @@ pub enum BlockNumberOrTag { } impl Default for BlockNumberOrTag { fn default() -> Self { - BlockNumberOrTag::U256(Default::default()) + BlockTag::Latest.into() } } @@ -133,7 +133,16 @@ pub enum BlockNumberOrTagOrHash { } impl Default for BlockNumberOrTagOrHash { fn default() -> Self { - BlockNumberOrTagOrHash::U256(Default::default()) + BlockTag::Latest.into() + } +} + +impl From for BlockNumberOrTagOrHash { + fn from(b: BlockNumberOrTag) -> Self { + match b { + BlockNumberOrTag::U256(n) => BlockNumberOrTagOrHash::U256(n), + BlockNumberOrTag::BlockTag(t) => BlockNumberOrTagOrHash::BlockTag(t), + } } } @@ -281,7 +290,7 @@ pub enum SyncingStatus { } impl Default for SyncingStatus { fn default() -> Self { - SyncingStatus::SyncingProgress(Default::default()) + SyncingStatus::Bool(false) } } @@ -319,7 +328,7 @@ pub enum TransactionUnsigned { } impl Default for TransactionUnsigned { fn default() -> Self { - TransactionUnsigned::Transaction4844Unsigned(Default::default()) + TransactionUnsigned::TransactionLegacyUnsigned(Default::default()) } } @@ -341,13 +350,13 @@ pub type AccessList = Vec; )] pub enum BlockTag { #[serde(rename = "earliest")] - #[default] Earliest, #[serde(rename = "finalized")] Finalized, #[serde(rename = "safe")] Safe, #[serde(rename = "latest")] + #[default] Latest, #[serde(rename = "pending")] Pending, @@ -574,7 +583,7 @@ pub enum TransactionSigned { } impl Default for TransactionSigned { fn default() -> Self { - TransactionSigned::Transaction4844Signed(Default::default()) + TransactionSigned::TransactionLegacySigned(Default::default()) } } From 87fd63fa25b1343e51be3527d05aacaffd58831f Mon Sep 17 00:00:00 2001 From: pgherveou Date: Fri, 22 Nov 2024 15:51:20 +0100 Subject: [PATCH 3/8] fixup cargo.toml --- Cargo.toml | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5402b3e60cc4..5ece645f82c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -557,7 +557,13 @@ default-members = [ [workspace.lints.rust] suspicious_double_ref_op = { level = "allow", priority = 2 } # `substrate_runtime` is a common `cfg` condition name used in the repo. -unexpected_cfgs = { level = "warn", check-cfg = ['cfg(build_opt_level, values("3"))', 'cfg(build_profile, values("debug", "release"))', 'cfg(enable_alloc_error_handler)', 'cfg(fuzzing)', 'cfg(substrate_runtime)'] } +unexpected_cfgs = { level = "warn", check-cfg = [ + 'cfg(build_opt_level, values("3"))', + 'cfg(build_profile, values("debug", "release"))', + 'cfg(enable_alloc_error_handler)', + 'cfg(fuzzing)', + 'cfg(substrate_runtime)', +] } [workspace.lints.clippy] all = { level = "allow", priority = 0 } @@ -632,7 +638,7 @@ bitvec = { version = "1.0.1", default-features = false } blake2 = { version = "0.10.4", default-features = false } blake2b_simd = { version = "1.0.2", default-features = false } blake3 = { version = "1.5" } -bounded-collections = { version = "0.2.0", default-features = false } +bounded-collections = { version = "0.2.2", default-features = false } bounded-vec = { version = "0.7" } bp-asset-hub-rococo = { path = "bridges/chains/chain-asset-hub-rococo", default-features = false } bp-asset-hub-westend = { path = "bridges/chains/chain-asset-hub-westend", default-features = false } @@ -678,6 +684,7 @@ cid = { version = "0.9.0" } clap = { version = "4.5.13" } clap-num = { version = "1.0.2" } clap_complete = { version = "4.5.13" } +cmd_lib = { version = "1.9.5" } coarsetime = { version = "0.1.22" } codec = { version = "3.6.12", default-features = false, package = "parity-scale-codec" } collectives-westend-emulated-chain = { path = "cumulus/parachains/integration-tests/emulated/chains/parachains/collectives/collectives-westend" } @@ -736,7 +743,7 @@ derive_more = { version = "0.99.17", default-features = false } digest = { version = "0.10.3", default-features = false } directories = { version = "5.0.1" } dlmalloc = { version = "0.2.4" } -docify = { version = "0.2.8" } +docify = { version = "0.2.9" } dyn-clonable = { version = "0.9.0" } dyn-clone = { version = "1.0.16" } ed25519-dalek = { version = "2.1", default-features = false } @@ -748,7 +755,7 @@ enumn = { version = "0.1.13" } env_logger = { version = "0.11.2" } environmental = { version = "1.1.4", default-features = false } equivocation-detector = { path = "bridges/relays/equivocation" } -ethabi = { version = "1.0.0", default-features = false, package = "ethabi-decode" } +ethabi = { version = "2.0.0", default-features = false, package = "ethabi-decode" } ethbloom = { version = "0.14.1", default-features = false } ethereum-types = { version = "0.15.1", default-features = false } exit-future = { version = "0.2.0" } @@ -786,7 +793,7 @@ frame-system-rpc-runtime-api = { path = "substrate/frame/system/rpc/runtime-api" frame-try-runtime = { path = "substrate/frame/try-runtime", default-features = false } fs4 = { version = "0.7.0" } fs_extra = { version = "1.3.0" } -futures = { version = "0.3.30" } +futures = { version = "0.3.31" } futures-channel = { version = "0.3.23" } futures-timer = { version = "3.0.2" } futures-util = { version = "0.3.30", default-features = false } @@ -842,7 +849,7 @@ linked-hash-map = { version = "0.5.4" } linked_hash_set = { version = "0.1.4" } linregress = { version = "0.5.1" } lite-json = { version = "0.2.0", default-features = false } -litep2p = { version = "0.7.0", features = ["websocket"] } +litep2p = { version = "0.8.1", features = ["websocket"] } log = { version = "0.4.22", default-features = false } macro_magic = { version = "0.5.1" } maplit = { version = "1.0.2" } @@ -1088,7 +1095,9 @@ polkavm-derive = "0.9.1" polkavm-linker = "0.9.2" portpicker = { version = "0.1.1" } pretty_assertions = { version = "1.3.0" } -primitive-types = { version = "0.13.1", default-features = false, features = ["num-traits"] } +primitive-types = { version = "0.13.1", default-features = false, features = [ + "num-traits", +] } proc-macro-crate = { version = "3.0.0" } proc-macro-warning = { version = "1.0.0", default-features = false } proc-macro2 = { version = "1.0.86" } @@ -1197,12 +1206,11 @@ seccompiler = { version = "0.4.0" } secp256k1 = { version = "0.28.0", default-features = false } secrecy = { version = "0.8.0", default-features = false } separator = { version = "0.4.1" } -serde = { version = "1.0.210", default-features = false } +serde = { version = "1.0.214", default-features = false } serde-big-array = { version = "0.3.2" } serde_derive = { version = "1.0.117" } serde_json = { version = "1.0.132", default-features = false } serde_yaml = { version = "0.9" } -serial_test = { version = "2.0.0" } sha1 = { version = "0.10.6" } sha2 = { version = "0.10.7", default-features = false } sha3 = { version = "0.10.0", default-features = false } @@ -1309,9 +1317,9 @@ substrate-test-runtime-client = { path = "substrate/test-utils/runtime/client" } substrate-test-runtime-transaction-pool = { path = "substrate/test-utils/runtime/transaction-pool" } substrate-test-utils = { path = "substrate/test-utils" } substrate-wasm-builder = { path = "substrate/utils/wasm-builder", default-features = false } -subxt = { version = "0.37", default-features = false } -subxt-signer = { version = "0.37" } -syn = { version = "2.0.82" } +subxt = { version = "0.38", default-features = false } +subxt-signer = { version = "0.38" } +syn = { version = "2.0.87" } sysinfo = { version = "0.30" } tar = { version = "0.4" } tempfile = { version = "3.8.1" } @@ -1380,7 +1388,7 @@ xcm-procedural = { path = "polkadot/xcm/procedural", default-features = false } xcm-runtime-apis = { path = "polkadot/xcm/xcm-runtime-apis", default-features = false } xcm-simulator = { path = "polkadot/xcm/xcm-simulator", default-features = false } zeroize = { version = "1.7.0", default-features = false } -zombienet-sdk = { version = "0.2.13" } +zombienet-sdk = { version = "0.2.15" } zstd = { version = "0.12.4", default-features = false } [profile.release] From aa1f1be50a56f02e55a49c19b2363152959712a6 Mon Sep 17 00:00:00 2001 From: pgherveou Date: Fri, 22 Nov 2024 16:25:14 +0100 Subject: [PATCH 4/8] gen fixes --- Cargo.lock | 11 +++++++++++ .../frame/revive/rpc/codegen/openrpc.json | 6 ++++-- .../frame/revive/rpc/codegen/src/generator.rs | 18 ++++++++++-------- .../frame/revive/rpc/src/rpc_methods_gen.rs | 1 + 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 330c2563d976..4eb40bc9761f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14835,6 +14835,17 @@ dependencies = [ "syn 2.0.87", ] +[[package]] +name = "pallet-revive-rpc-codegen" +version = "0.1.0" +dependencies = [ + "Inflector", + "anyhow", + "pretty_assertions", + "serde", + "serde_json", +] + [[package]] name = "pallet-revive-uapi" version = "0.1.0" diff --git a/substrate/frame/revive/rpc/codegen/openrpc.json b/substrate/frame/revive/rpc/codegen/openrpc.json index 7b55131590ee..44e590cd3e7d 100644 --- a/substrate/frame/revive/rpc/codegen/openrpc.json +++ b/substrate/frame/revive/rpc/codegen/openrpc.json @@ -1962,7 +1962,8 @@ "title": "EIP-4844 transaction signature properties.", "required": [ "r", - "s" + "s", + "yParity" ], "properties": { "yParity": { @@ -1993,7 +1994,8 @@ "title": "EIP-1559 transaction signature properties.", "required": [ "r", - "s" + "s", + "yParity" ], "properties": { "yParity": { diff --git a/substrate/frame/revive/rpc/codegen/src/generator.rs b/substrate/frame/revive/rpc/codegen/src/generator.rs index 6419a994b6d3..bf18efaa17f8 100644 --- a/substrate/frame/revive/rpc/codegen/src/generator.rs +++ b/substrate/frame/revive/rpc/codegen/src/generator.rs @@ -247,11 +247,10 @@ impl TypeGenerator { pub fn generate_types(&mut self, specs: &OpenRpc) -> String { let mut code = LICENSE.to_string(); code.push_str( - r#" - //! Generated JSON-RPC types. + r#"//! Generated JSON-RPC types. #![allow(missing_docs)] - use super::{byte::*, Type0, Type1, Type2, Type3}; + use super::{byte::*, TypeEip1559, TypeEip2930, TypeEip4844, TypeLegacy}; use alloc::vec::Vec; use codec::{Decode, Encode}; use derive_more::{From, TryInto}; @@ -425,11 +424,14 @@ impl TypeNameProvider for TypeGenerator { pattern: Some(ref pattern), format: None, enumeration: None, - })) if ["^0x0$", "^0x1$", "^0x2$", "^0x3$"].contains(&pattern.as_str()) => { - let type_id = format!("Type{}", &pattern[3..4]); - - Some(type_id.into()) - }, + })) if ["^0x0$", "^0x1$", "^0x2$", "^0x3$"].contains(&pattern.as_str()) => + match pattern.as_str() { + "^0x0$" => Some("TypeLegacy".into()), + "^0x1$" => Some("TypeEip2930".into()), + "^0x2$" => Some("TypeEip1559".into()), + "^0x3$" => Some("TypeEip4844".into()), + _ => unreachable!(), + }, SchemaContents::Literal(Literal::Boolean) => Some("bool".into()), SchemaContents::Object(_) => None, diff --git a/substrate/frame/revive/rpc/src/rpc_methods_gen.rs b/substrate/frame/revive/rpc/src/rpc_methods_gen.rs index 339080368969..ad34dbfdfb49 100644 --- a/substrate/frame/revive/rpc/src/rpc_methods_gen.rs +++ b/substrate/frame/revive/rpc/src/rpc_methods_gen.rs @@ -14,6 +14,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + //! Generated JSON-RPC methods. #![allow(missing_docs)] From e9823a0f8f28c485498b0070d61bec13cc31b992 Mon Sep 17 00:00:00 2001 From: pgherveou Date: Fri, 22 Nov 2024 17:21:27 +0100 Subject: [PATCH 5/8] fix generator --- .../frame/revive/rpc/codegen/openrpc.json | 3 +- .../frame/revive/rpc/codegen/src/generator.rs | 34 +++++++++++---- .../frame/revive/rpc/codegen/src/printer.rs | 42 ++++++++++++++++--- .../frame/revive/src/evm/api/rpc_types.rs | 9 ++++ .../frame/revive/src/evm/api/rpc_types_gen.rs | 27 ++++-------- 5 files changed, 81 insertions(+), 34 deletions(-) diff --git a/substrate/frame/revive/rpc/codegen/openrpc.json b/substrate/frame/revive/rpc/codegen/openrpc.json index 44e590cd3e7d..4a91ee18177c 100644 --- a/substrate/frame/revive/rpc/codegen/openrpc.json +++ b/substrate/frame/revive/rpc/codegen/openrpc.json @@ -1395,7 +1395,8 @@ "title": "log", "type": "object", "required": [ - "transactionHash" + "transactionHash", + "address" ], "additionalProperties": false, "properties": { diff --git a/substrate/frame/revive/rpc/codegen/src/generator.rs b/substrate/frame/revive/rpc/codegen/src/generator.rs index bf18efaa17f8..c4881a186c50 100644 --- a/substrate/frame/revive/rpc/codegen/src/generator.rs +++ b/substrate/frame/revive/rpc/codegen/src/generator.rs @@ -87,6 +87,18 @@ pub static LEGACY_ALIASES: LazyLock> = + LazyLock::new(|| { + HashMap::from([ + ("TransactionUnsigned", "TransactionLegacyUnsigned"), + ("TransactionSigned", "TransactionLegacySigned"), + ("BlockNumberOrTagOrHash", "BlockTag"), + ("BlockNumberOrTag", "BlockTag"), + ("BlockTag", "Latest"), + ]) + }); + /// Read the OpenRPC specs, and inject extra methods and legacy aliases. pub fn read_specs() -> anyhow::Result { let content = include_str!("../openrpc.json"); @@ -309,7 +321,8 @@ impl TypeGenerator { }, }; - TypePrinter { name, doc, content } + let default_variant = CUSTOM_DEFAULT_VARIANTS.get(&name.as_str()).map(|v| v.to_string()); + TypePrinter::new(doc, name, content, default_variant) } fn generate_rpc_method(&mut self, buffer: &mut String, method: &Method) { @@ -445,7 +458,10 @@ impl TypeNameProvider for TypeGenerator { #[cfg(test)] pub fn assert_code_match(expected: &str, actual: &str) { - assert_eq!(format_code(expected).unwrap().trim(), format_code(actual).unwrap().trim()); + pretty_assertions::assert_eq!( + format_code(expected).unwrap().trim(), + format_code(actual).unwrap().trim() + ); } #[cfg(test)] @@ -558,8 +574,8 @@ mod test { pub s: U256, /// yParity /// The parity (0 for even, 1 for odd) of the y-value of the secp256k1 signature. - #[serde(rename = "yParity", skip_serializing_if = "Option::is_none")] - pub y_parity: Option, + #[serde(rename = "yParity")] + pub y_parity: U256, } "#, ); @@ -588,7 +604,7 @@ mod test { } impl Default for TransactionUnsigned { fn default() -> Self { - TransactionUnsigned::Transaction4844Unsigned(Default::default()) + TransactionUnsigned::TransactionLegacyUnsigned(Default::default()) } } "#, @@ -689,12 +705,12 @@ mod test { pub access_list: Option, /// blobVersionedHashes /// List of versioned blob hashes associated with the transaction's EIP-4844 data blobs. - #[serde(rename = "blobVersionedHashes", skip_serializing_if = "Option::is_none")] - pub blob_versioned_hashes: Option>, + #[serde(rename = "blobVersionedHashes", skip_serializing_if = "Vec::is_empty")] + pub blob_versioned_hashes: Vec, /// blobs /// Raw blob data. - #[serde(skip_serializing_if = "Option::is_none")] - pub blobs: Option>, + #[serde(skip_serializing_if = "Vec::is_empty")] + pub blobs: Vec, /// chainId /// Chain ID that this transaction is valid on. #[serde(rename = "chainId", skip_serializing_if = "Option::is_none")] diff --git a/substrate/frame/revive/rpc/codegen/src/printer.rs b/substrate/frame/revive/rpc/codegen/src/printer.rs index 4d1bb620c2e4..0b13500b85c5 100644 --- a/substrate/frame/revive/rpc/codegen/src/printer.rs +++ b/substrate/frame/revive/rpc/codegen/src/printer.rs @@ -69,8 +69,7 @@ impl TypeInfo { let mut type_name = self.name.clone(); if self.array { type_name = format!("Vec<{}>", type_name) - } - if self.is_optional() { + } else if self.is_optional() { type_name = format!("Option<{}>", type_name) } type_name @@ -262,6 +261,7 @@ pub struct TypePrinter { pub doc: Option, pub name: String, pub content: TypeContent, + custom_default_variant: Option, } /// A macro to write a formatted line to a buffer. @@ -287,6 +287,15 @@ macro_rules! writeln { } impl TypePrinter { + pub fn new( + doc: Option, + name: String, + content: TypeContent, + custom_default_variant: Option, + ) -> Self { + Self { doc, name, content, custom_default_variant } + } + /// Prints the type to a buffer. pub fn print(self, buffer: &mut String) { let Self { doc, name, content, .. } = self; @@ -315,10 +324,14 @@ impl TypePrinter { writeln!(buffer, "}}"); // Implement Default trait - let variant = variants.0[0].name(); + let default_variant = self + .custom_default_variant + .map(|s| s.to_string()) + .unwrap_or_else(|| variants.0[0].name()); + writeln!(buffer, "impl Default for {name} {{"); writeln!(buffer, " fn default() -> Self {{"); - writeln!(buffer, " {name}::{variant}(Default::default())"); + writeln!(buffer, " {name}::{default_variant}(Default::default())"); writeln!(buffer, " }}"); writeln!(buffer, "}}"); }, @@ -328,9 +341,17 @@ impl TypePrinter { "#[derive(Debug, Default, Clone, Encode, Decode, TypeInfo, Serialize, Deserialize, Eq, PartialEq)]" ); writeln!(buffer, "pub enum {name} {{"); + + let default_variant_index = self.custom_default_variant.map_or(0, |v| { + variants + .iter() + .position(|x| x.eq_ignore_ascii_case(&v)) + .expect("Default variant not found") + }); + for (i, name) in variants.iter().enumerate() { writeln!(buffer, " #[serde(rename = \"{name}\")]"); - if i == 0 { + if i == default_variant_index { writeln!(buffer, " #[default]"); } let pascal_name = name.to_pascal_case(); @@ -361,7 +382,13 @@ impl TypePrinter { } if matches!(type_info.required, Required::No { skip_if_null: true }) { - serde_params.push("skip_serializing_if = \"Option::is_none\"".to_string()); + if type_info.array { + serde_params + .push("skip_serializing_if = \"Vec::is_empty\"".to_string()); + } else { + serde_params + .push("skip_serializing_if = \"Option::is_none\"".to_string()); + } } if !serde_params.is_empty() { @@ -414,6 +441,7 @@ mod test { ] .into(), ), + custom_default_variant: None, }; let mut buffer = String::new(); gen.print(&mut buffer); @@ -439,6 +467,7 @@ mod test { doc: Some("A simple untagged enum".to_string()), name: "SimpleUntaggedEnum".to_string(), content: TypeContent::UntaggedEnum(vec!["first".to_string(), "second".to_string()]), + custom_default_variant: None, }; let mut buffer = String::new(); gen.print(&mut buffer); @@ -470,6 +499,7 @@ mod test { ] .into(), ), + custom_default_variant: None, }; let mut buffer = String::new(); gen.print(&mut buffer); diff --git a/substrate/frame/revive/src/evm/api/rpc_types.rs b/substrate/frame/revive/src/evm/api/rpc_types.rs index 1cf8d984b68b..287d84fc186f 100644 --- a/substrate/frame/revive/src/evm/api/rpc_types.rs +++ b/substrate/frame/revive/src/evm/api/rpc_types.rs @@ -19,6 +19,15 @@ use super::*; use alloc::vec::Vec; use sp_core::{H160, U256}; +impl From for BlockNumberOrTagOrHash { + fn from(b: BlockNumberOrTag) -> Self { + match b { + BlockNumberOrTag::U256(n) => BlockNumberOrTagOrHash::U256(n), + BlockNumberOrTag::BlockTag(t) => BlockNumberOrTagOrHash::BlockTag(t), + } + } +} + impl TransactionInfo { /// Create a new [`TransactionInfo`] from a receipt and a signed transaction. pub fn new(receipt: ReceiptInfo, transaction_signed: TransactionSigned) -> Self { diff --git a/substrate/frame/revive/src/evm/api/rpc_types_gen.rs b/substrate/frame/revive/src/evm/api/rpc_types_gen.rs index 1370ea2b7612..a9206e38fcc8 100644 --- a/substrate/frame/revive/src/evm/api/rpc_types_gen.rs +++ b/substrate/frame/revive/src/evm/api/rpc_types_gen.rs @@ -94,8 +94,8 @@ pub struct Block { /// Uncles pub uncles: Vec, /// Withdrawals - #[serde(skip_serializing_if = "Option::is_none")] - pub withdrawals: Option>, + #[serde(skip_serializing_if = "Vec::is_empty")] + pub withdrawals: Vec, /// Withdrawals root #[serde(rename = "withdrawalsRoot", skip_serializing_if = "Option::is_none")] pub withdrawals_root: Option, @@ -114,7 +114,7 @@ pub enum BlockNumberOrTag { } impl Default for BlockNumberOrTag { fn default() -> Self { - BlockTag::Latest.into() + BlockNumberOrTag::BlockTag(Default::default()) } } @@ -133,16 +133,7 @@ pub enum BlockNumberOrTagOrHash { } impl Default for BlockNumberOrTagOrHash { fn default() -> Self { - BlockTag::Latest.into() - } -} - -impl From for BlockNumberOrTagOrHash { - fn from(b: BlockNumberOrTag) -> Self { - match b { - BlockNumberOrTag::U256(n) => BlockNumberOrTagOrHash::U256(n), - BlockNumberOrTag::BlockTag(t) => BlockNumberOrTagOrHash::BlockTag(t), - } + BlockNumberOrTagOrHash::BlockTag(Default::default()) } } @@ -157,12 +148,12 @@ pub struct GenericTransaction { pub access_list: Option, /// blobVersionedHashes /// List of versioned blob hashes associated with the transaction's EIP-4844 data blobs. - #[serde(rename = "blobVersionedHashes", skip_serializing_if = "Option::is_none")] - pub blob_versioned_hashes: Option>, + #[serde(rename = "blobVersionedHashes", skip_serializing_if = "Vec::is_empty")] + pub blob_versioned_hashes: Vec, /// blobs /// Raw blob data. - #[serde(skip_serializing_if = "Option::is_none")] - pub blobs: Option>, + #[serde(skip_serializing_if = "Vec::is_empty")] + pub blobs: Vec, /// chainId /// Chain ID that this transaction is valid on. #[serde(rename = "chainId", skip_serializing_if = "Option::is_none")] @@ -290,7 +281,7 @@ pub enum SyncingStatus { } impl Default for SyncingStatus { fn default() -> Self { - SyncingStatus::Bool(false) + SyncingStatus::SyncingProgress(Default::default()) } } From 61619bdb1482b0f7f5ec67955df1543b4347b45e Mon Sep 17 00:00:00 2001 From: pgherveou Date: Fri, 22 Nov 2024 17:24:11 +0100 Subject: [PATCH 6/8] type generator update fixes --- substrate/frame/revive/src/evm/api/rpc_types.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/substrate/frame/revive/src/evm/api/rpc_types.rs b/substrate/frame/revive/src/evm/api/rpc_types.rs index 287d84fc186f..338be88e69b5 100644 --- a/substrate/frame/revive/src/evm/api/rpc_types.rs +++ b/substrate/frame/revive/src/evm/api/rpc_types.rs @@ -182,7 +182,7 @@ impl GenericTransaction { gas: Some(tx.gas), gas_price: Some(tx.max_fee_per_blob_gas), access_list: Some(tx.access_list), - blob_versioned_hashes: Some(tx.blob_versioned_hashes), + blob_versioned_hashes: tx.blob_versioned_hashes, max_fee_per_blob_gas: Some(tx.max_fee_per_blob_gas), max_fee_per_gas: Some(tx.max_fee_per_gas), max_priority_fee_per_gas: Some(tx.max_priority_fee_per_gas), @@ -278,7 +278,7 @@ impl GenericTransaction { max_fee_per_blob_gas: self.max_fee_per_blob_gas.unwrap_or_default(), max_priority_fee_per_gas: self.max_priority_fee_per_gas.unwrap_or_default(), access_list: self.access_list.unwrap_or_default(), - blob_versioned_hashes: self.blob_versioned_hashes.unwrap_or_default(), + blob_versioned_hashes: self.blob_versioned_hashes, } .into()), _ => Err(()), From 716f40a4b0e0284e0620854674e5037787ad9ec5 Mon Sep 17 00:00:00 2001 From: pgherveou Date: Tue, 26 Nov 2024 14:09:45 +0100 Subject: [PATCH 7/8] wip --- Cargo.lock | 1 + .../assets/asset-hub-westend/src/lib.rs | 30 +-- substrate/bin/node/runtime/src/lib.rs | 25 +-- substrate/frame/revive/Cargo.toml | 1 + .../frame/revive/rpc/codegen/src/printer.rs | 5 +- .../rpc/examples/js/src/geth-diff.test.ts | 30 +-- .../frame/revive/rpc/revive_chain.metadata | Bin 658056 -> 659977 bytes substrate/frame/revive/rpc/src/client.rs | 57 ++--- substrate/frame/revive/rpc/src/lib.rs | 19 +- .../frame/revive/rpc/src/subxt_client.rs | 12 +- .../frame/revive/src/evm/api/rlp_codec.rs | 18 +- .../frame/revive/src/evm/api/rpc_types.rs | 137 ++++++------ .../frame/revive/src/evm/api/rpc_types_gen.rs | 8 +- substrate/frame/revive/src/evm/runtime.rs | 93 +++++--- substrate/frame/revive/src/exec.rs | 71 +++++- substrate/frame/revive/src/lib.rs | 211 ++++++++++++------ substrate/frame/revive/src/primitives.rs | 42 +++- substrate/frame/revive/src/storage/meter.rs | 52 +++-- .../frame/revive/src/test_utils/builder.rs | 11 +- substrate/frame/revive/src/tests.rs | 12 +- .../frame/revive/src/tests/test_debug.rs | 5 +- substrate/frame/revive/src/wasm/mod.rs | 11 +- 22 files changed, 521 insertions(+), 330 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4eb40bc9761f..65580fe0239c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14602,6 +14602,7 @@ dependencies = [ "assert_matches", "bitflags 1.3.2", "derive_more 0.99.17", + "env_logger 0.11.3", "environmental", "ethereum-types 0.15.1", "frame-benchmarking 28.0.0", diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs index cafea3b6ff8b..5807d80ab542 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs @@ -124,7 +124,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: alloc::borrow::Cow::Borrowed("westmint"), impl_name: alloc::borrow::Cow::Borrowed("westmint"), authoring_version: 1, - spec_version: 1_016_006, + spec_version: 1_016_008, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 16, @@ -2088,18 +2088,10 @@ impl_runtime_apis! { let account = ::AddressMapper::to_account_id(&address); System::account_nonce(account) } - fn eth_transact( - from: H160, - dest: Option, - value: U256, - input: Vec, - gas_limit: Option, - storage_deposit_limit: Option, - ) -> pallet_revive::EthContractResult + + fn eth_transact(tx: pallet_revive::evm::GenericTransaction) -> Result, pallet_revive::EthTransactError> { - use pallet_revive::AddressMapper; - let blockweights = ::BlockWeights::get(); - let origin = ::AddressMapper::to_account_id(&from); + let blockweights: BlockWeights = ::BlockWeights::get(); let encoded_size = |pallet_call| { let call = RuntimeCall::Revive(pallet_call); @@ -2108,15 +2100,9 @@ impl_runtime_apis! { }; Revive::bare_eth_transact( - origin, - dest, - value, - input, - gas_limit.unwrap_or(blockweights.max_block), - storage_deposit_limit.unwrap_or(u128::MAX), + tx, + blockweights.max_block, encoded_size, - pallet_revive::DebugInfo::UnsafeDebug, - pallet_revive::CollectEvents::UnsafeCollect, ) } @@ -2134,7 +2120,7 @@ impl_runtime_apis! { dest, value, gas_limit.unwrap_or(blockweights.max_block), - storage_deposit_limit.unwrap_or(u128::MAX), + pallet_revive::DepositLimit::Balance(storage_deposit_limit.unwrap_or(u128::MAX)), input_data, pallet_revive::DebugInfo::UnsafeDebug, pallet_revive::CollectEvents::UnsafeCollect, @@ -2156,7 +2142,7 @@ impl_runtime_apis! { RuntimeOrigin::signed(origin), value, gas_limit.unwrap_or(blockweights.max_block), - storage_deposit_limit.unwrap_or(u128::MAX), + pallet_revive::DepositLimit::Balance(storage_deposit_limit.unwrap_or(u128::MAX)), code, data, salt, diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index bff263548087..faffcd23fbcf 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -3218,18 +3218,9 @@ impl_runtime_apis! { System::account_nonce(account) } - fn eth_transact( - from: H160, - dest: Option, - value: U256, - input: Vec, - gas_limit: Option, - storage_deposit_limit: Option, - ) -> pallet_revive::EthContractResult + fn eth_transact(tx: pallet_revive::evm::GenericTransaction) -> Result, pallet_revive::EthTransactError> { - use pallet_revive::AddressMapper; let blockweights: BlockWeights = ::BlockWeights::get(); - let origin = ::AddressMapper::to_account_id(&from); let encoded_size = |pallet_call| { let call = RuntimeCall::Revive(pallet_call); @@ -3238,15 +3229,9 @@ impl_runtime_apis! { }; Revive::bare_eth_transact( - origin, - dest, - value, - input, - gas_limit.unwrap_or(blockweights.max_block), - storage_deposit_limit.unwrap_or(u128::MAX), + tx, + blockweights.max_block, encoded_size, - pallet_revive::DebugInfo::UnsafeDebug, - pallet_revive::CollectEvents::UnsafeCollect, ) } @@ -3263,7 +3248,7 @@ impl_runtime_apis! { dest, value, gas_limit.unwrap_or(RuntimeBlockWeights::get().max_block), - storage_deposit_limit.unwrap_or(u128::MAX), + pallet_revive::DepositLimit::Balance(storage_deposit_limit.unwrap_or(u128::MAX)), input_data, pallet_revive::DebugInfo::UnsafeDebug, pallet_revive::CollectEvents::UnsafeCollect, @@ -3284,7 +3269,7 @@ impl_runtime_apis! { RuntimeOrigin::signed(origin), value, gas_limit.unwrap_or(RuntimeBlockWeights::get().max_block), - storage_deposit_limit.unwrap_or(u128::MAX), + pallet_revive::DepositLimit::Balance(storage_deposit_limit.unwrap_or(u128::MAX)), code, data, salt, diff --git a/substrate/frame/revive/Cargo.toml b/substrate/frame/revive/Cargo.toml index 81fbbc8cf38e..d2026d2ce9eb 100644 --- a/substrate/frame/revive/Cargo.toml +++ b/substrate/frame/revive/Cargo.toml @@ -65,6 +65,7 @@ pallet-revive-fixtures = { workspace = true, default-features = true } secp256k1 = { workspace = true, features = ["recovery"] } serde_json = { workspace = true } hex-literal = { workspace = true } +env_logger = { workspace = true } # Polkadot SDK Dependencies pallet-balances = { workspace = true, default-features = true } diff --git a/substrate/frame/revive/rpc/codegen/src/printer.rs b/substrate/frame/revive/rpc/codegen/src/printer.rs index 0b13500b85c5..8a8933b4f432 100644 --- a/substrate/frame/revive/rpc/codegen/src/printer.rs +++ b/substrate/frame/revive/rpc/codegen/src/printer.rs @@ -383,8 +383,9 @@ impl TypePrinter { if matches!(type_info.required, Required::No { skip_if_null: true }) { if type_info.array { - serde_params - .push("skip_serializing_if = \"Vec::is_empty\"".to_string()); + serde_params.push( + "default, skip_serializing_if = \"Vec::is_empty\"".to_string(), + ); } else { serde_params .push("skip_serializing_if = \"Option::is_none\"".to_string()); diff --git a/substrate/frame/revive/rpc/examples/js/src/geth-diff.test.ts b/substrate/frame/revive/rpc/examples/js/src/geth-diff.test.ts index ef4cde1824e4..c5cb64e63afd 100644 --- a/substrate/frame/revive/rpc/examples/js/src/geth-diff.test.ts +++ b/substrate/frame/revive/rpc/examples/js/src/geth-diff.test.ts @@ -134,21 +134,20 @@ if (!process.env.USE_LIVE_SERVERS) { })(), //Run the substate node (() => { - killProcessOnPort(9944) - return spawn( - [ - './target/debug/substrate-node', - '--dev', - '-l=error,evm=debug,sc_rpc_server=info,runtime::revive=debug', - ], - { - stdout: Bun.file('/tmp/kitchensink.out.log'), - stderr: Bun.file('/tmp/kitchensink.err.log'), - cwd: join(process.env.HOME!, 'polkadot-sdk'), - } - ) - })() - , + killProcessOnPort(9944) + return spawn( + [ + './target/debug/substrate-node', + '--dev', + '-l=error,evm=debug,sc_rpc_server=info,runtime::revive=debug', + ], + { + stdout: Bun.file('/tmp/kitchensink.out.log'), + stderr: Bun.file('/tmp/kitchensink.err.log'), + cwd: join(process.env.HOME!, 'polkadot-sdk'), + } + ) + })(), // Run eth-rpc on 8545 await (async () => { killProcessOnPort(8545) @@ -301,6 +300,7 @@ for (const env of envs) { } catch (err) { const lastJsonRpcError = jsonRpcErrors.pop() expect(lastJsonRpcError?.code).toBe(-32000) + console.log(lastJsonRpcError?.message) expect(lastJsonRpcError?.message).toInclude('insufficient funds') expect(lastJsonRpcError?.data).toBeUndefined() } diff --git a/substrate/frame/revive/rpc/revive_chain.metadata b/substrate/frame/revive/rpc/revive_chain.metadata index 3560b3b90407acce7f602ce91ac089843be8dea8..64b1f2014dd06815fcea6a87bc96306eb00eda8b 100644 GIT binary patch delta 13838 zcmbt*4OmrG*6`Wq-h1}h_a6lX0llcGC?F^(D41xZSR|-ezKM8+i{6BL;r^&hkul{Y zjWikIip-KJGG}s(96MPtMrM;GCY6m;_%o$xrD~dt*uWbXI?LQ8 z&VbiW$n z&*w@|q3Uby7`ZDetvteL0ZAYGR)C2W!Y z!^31Gd>LnW{EkAhV1>(3;qsKYJbGN*rq!&&YPdIAn_sUv{j%3WNjj`E;CfLe37 zDud24hZj4%J{JNwJ&vW>Yux^T%M)-r%Wia4c>V6c(!_iMdCLu#>fd70#+Mud`(76bGLv zR6K2nQQ&DqWH<>`cD-tdB*wffkHcA9jFX10*9;3Ci?bZ7ya8A5^;Yg(s$!zs?o}aU&D(C+KB+0}~pUd5piJ$RBX7L|sLBOG{mzVpqmo zt)1^$g`)?m5L$SXMCG}B%iLvUg3!p|_--;(x!WxbXVF`bn%Kky{2^7LT_-fFPup3V zupM6YN#n#F44euXto-VeZYE+2t8coLG;uf6&*zl)FcQAdRh+S4njq}vm}YybC}1~| z#rIZ~SQ`r$+EhF!dWj4q`=Mr)6anU-G@TrP+@R#fM(sKrl%5D{XC!Tbv#hKtV}`TL ziBm$_?fiQ&>EO*k_-U1N3poPWtEF4TlMD)T=uq(#gX$bQ0*Z^;tAoLhSJ7_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5M%5fFC3){`3y)#nq#mt%I_7r!RM@MvG&v5B1 z8;7c`MK1U96&`NcI0p{@n+NnZSvDrB11b3ZY$oQG?#HiB1L4ZQaT8$>{~nrcB&>Rm zAHbk`@DZMfPocR-_L6T(wxIW-50gEk?XN|)W zu{0|;W91Tzu~v*ulb0RiQ$>r*GN2GY0&!(yrsgPf%ZEPXuZx-@OR;>TiHC^r#0m5P z>*U|=@Et^+)4;#eirSB%`U@Vo?G*3LeBaB;$4>EAw5Vsx&vK23f?nCk*K0Rev_~N7 z9PbZ)=g<;1z|eDi3v}G!(`C&!{EDb4^DdJsl7n*kxBNMR2SUpw{x<5yE8p=d%YN@Z zVyzgBd19+!^;JFt_FU!(7)4yT%%37hTJTLM%~uD1lZbMlHjvC#K2A0T#E;M7(AC_0JMYiI{tK6Q;hP9%rL znB~f``oTljd2nYdP+oVPZxoSe+R87XU0-WO$Uu4dO};=xVb$K^|D&>US{r{vv_)Gd ztaKGGQ9g=LuA8~J%aF|$dpsn!@uBjqpU@{zwI@yO0r7}t07PBjlHBqO#}H1Hg1<() z&LSlN>EHnsk^0Rfrs9x(42iG!LH`IE_8*DB!v1}z3PU+eXixbWi!eJdd)a-K<#UV4N4 MsTJ4Mlv;)V0}5B>2><{9 diff --git a/substrate/frame/revive/rpc/src/client.rs b/substrate/frame/revive/rpc/src/client.rs index d7cbca520113..5f664be115e2 100644 --- a/substrate/frame/revive/rpc/src/client.rs +++ b/substrate/frame/revive/rpc/src/client.rs @@ -32,7 +32,7 @@ use pallet_revive::{ Block, BlockNumberOrTag, BlockNumberOrTagOrHash, Bytes256, GenericTransaction, Log, ReceiptInfo, SyncingProgress, SyncingStatus, TransactionSigned, H160, H256, U256, }, - EthContractResult, + EthTransactError, EthTransactInfo, }; use sp_core::keccak_256; use sp_weights::Weight; @@ -170,12 +170,9 @@ pub enum ClientError { /// A [`codec::Error`] wrapper error. #[error(transparent)] CodecError(#[from] codec::Error), - /// The dry run failed. - #[error("dry run failed: {0}")] - DryRunFailed(String), /// Contract reverted - #[error("{}", extract_revert_message(.0).unwrap_or_default())] - Reverted(Vec), + #[error("Contract reverted")] + Reverted(EthTransactError), /// A decimal conversion failed. #[error("conversion failed")] ConversionFailed, @@ -194,19 +191,26 @@ const REVERT_CODE: i32 = 3; // TODO convert error code to https://eips.ethereum.org/EIPS/eip-1474#error-codes impl From for ErrorObjectOwned { fn from(err: ClientError) -> Self { - let msg = err.to_string(); match err { ClientError::SubxtError(subxt::Error::Rpc(err)) | ClientError::RpcError(err) => { if let Some(err) = unwrap_call_err(&err) { return err; } - ErrorObjectOwned::owned::>(CALL_EXECUTION_FAILED_CODE, msg, None) + ErrorObjectOwned::owned::>( + CALL_EXECUTION_FAILED_CODE, + err.to_string(), + None, + ) }, - ClientError::Reverted(data) => { + ClientError::Reverted(EthTransactError::Data(data)) => { + let msg = extract_revert_message(&data).unwrap_or_default(); let data = format!("0x{}", hex::encode(data)); ErrorObjectOwned::owned::(REVERT_CODE, msg, Some(data)) }, - _ => ErrorObjectOwned::owned::(CALL_EXECUTION_FAILED_CODE, msg, None), + ClientError::Reverted(EthTransactError::Message(msg)) => + ErrorObjectOwned::owned::(CALL_EXECUTION_FAILED_CODE, msg, None), + _ => + ErrorObjectOwned::owned::(CALL_EXECUTION_FAILED_CODE, err.to_string(), None), } } } @@ -659,41 +663,22 @@ impl Client { Ok(result) } - /// Dry run a transaction and returns the [`EthContractResult`] for the transaction. + /// Dry run a transaction and returns the [`EthTransactInfo`] for the transaction. pub async fn dry_run( &self, - tx: &GenericTransaction, + tx: GenericTransaction, block: BlockNumberOrTagOrHash, - ) -> Result>, ClientError> { + ) -> Result, ClientError> { let runtime_api = self.runtime_api(&block).await?; + let payload = subxt_client::apis().revive_api().eth_transact(tx.into()); - // TODO: remove once subxt is updated - let value = subxt::utils::Static(tx.value.unwrap_or_default()); - let from = tx.from.map(|v| v.0.into()); - let to = tx.to.map(|v| v.0.into()); - - let payload = subxt_client::apis().revive_api().eth_transact( - from.unwrap_or_default(), - to, - value, - tx.input.clone().unwrap_or_default().0, - None, - None, - ); - - let EthContractResult { fee, gas_required, storage_deposit, result } = - runtime_api.call(payload).await?.0; + let result = runtime_api.call(payload).await?; match result { Err(err) => { log::debug!(target: LOG_TARGET, "Dry run failed {err:?}"); - Err(ClientError::DryRunFailed(format!("{err:?}"))) - }, - Ok(result) if result.did_revert() => { - log::debug!(target: LOG_TARGET, "Dry run reverted"); - Err(ClientError::Reverted(result.0.data)) + Err(ClientError::Reverted(err.0)) }, - Ok(result) => - Ok(EthContractResult { fee, gas_required, storage_deposit, result: result.0.data }), + Ok(result) => Ok(result.0), } } diff --git a/substrate/frame/revive/rpc/src/lib.rs b/substrate/frame/revive/rpc/src/lib.rs index b35497e34bd7..8072de4d3ce3 100644 --- a/substrate/frame/revive/rpc/src/lib.rs +++ b/substrate/frame/revive/rpc/src/lib.rs @@ -23,7 +23,7 @@ use jsonrpsee::{ core::{async_trait, RpcResult}, types::{ErrorCode, ErrorObjectOwned}, }; -use pallet_revive::{evm::*, EthContractResult}; +use pallet_revive::evm::*; use sp_core::{keccak_256, H160, H256, U256}; use thiserror::Error; @@ -130,10 +130,8 @@ impl EthRpcServer for EthRpcServerImpl { transaction: GenericTransaction, block: Option, ) -> RpcResult { - // estimate_gas only fails returns even if the contract traps - let dry_run = self.client.dry_run(&transaction, block.unwrap_or_default().into()).await?; - - Ok(U256::from(dry_run.fee / GAS_PRICE as u128) + GAS_PRICE) + let dry_run = self.client.dry_run(transaction, block.unwrap_or_default().into()).await?; + Ok(U256::from(dry_run.eth_gas)) } async fn call( @@ -143,9 +141,9 @@ impl EthRpcServer for EthRpcServerImpl { ) -> RpcResult { let dry_run = self .client - .dry_run(&transaction, block.unwrap_or_else(|| BlockTag::Latest.into())) + .dry_run(transaction, block.unwrap_or_else(|| BlockTag::Latest.into())) .await?; - Ok(dry_run.result.into()) + Ok(dry_run.data.into()) } async fn send_raw_transaction(&self, transaction: Bytes) -> RpcResult { @@ -164,13 +162,12 @@ impl EthRpcServer for EthRpcServerImpl { let tx = GenericTransaction::from_signed(tx, Some(eth_addr)); // Dry run the transaction to get the weight limit and storage deposit limit - let dry_run = self.client.dry_run(&tx, BlockTag::Latest.into()).await?; + let dry_run = self.client.dry_run(tx, BlockTag::Latest.into()).await?; - let EthContractResult { gas_required, storage_deposit, .. } = dry_run; let call = subxt_client::tx().revive().eth_transact( transaction.0, - gas_required.into(), - storage_deposit, + dry_run.gas_required.into(), + dry_run.storage_deposit, ); self.client.submit(call).await.map_err(|err| { log::debug!(target: LOG_TARGET, "submit call failed: {err:?}"); diff --git a/substrate/frame/revive/rpc/src/subxt_client.rs b/substrate/frame/revive/rpc/src/subxt_client.rs index a232b231bc7c..1e1c395028a4 100644 --- a/substrate/frame/revive/rpc/src/subxt_client.rs +++ b/substrate/frame/revive/rpc/src/subxt_client.rs @@ -27,8 +27,16 @@ use subxt::config::{signed_extensions, Config, PolkadotConfig}; with = "::subxt::utils::Static<::sp_core::U256>" ), substitute_type( - path = "pallet_revive::primitives::EthContractResult", - with = "::subxt::utils::Static<::pallet_revive::EthContractResult>" + path = "pallet_revive::evm::api::rpc_types_gen::GenericTransaction", + with = "::subxt::utils::Static<::pallet_revive::evm::GenericTransaction>" + ), + substitute_type( + path = "pallet_revive::primitives::EthTransactInfo", + with = "::subxt::utils::Static<::pallet_revive::EthTransactInfo>" + ), + substitute_type( + path = "pallet_revive::primitives::EthTransactError", + with = "::subxt::utils::Static<::pallet_revive::EthTransactError>" ), substitute_type( path = "pallet_revive::primitives::ExecReturnValue", diff --git a/substrate/frame/revive/src/evm/api/rlp_codec.rs b/substrate/frame/revive/src/evm/api/rlp_codec.rs index 3442ed73acca..18b7e7c17e09 100644 --- a/substrate/frame/revive/src/evm/api/rlp_codec.rs +++ b/substrate/frame/revive/src/evm/api/rlp_codec.rs @@ -88,14 +88,14 @@ impl TransactionSigned { } } -impl TransactionLegacyUnsigned { - /// Get the rlp encoded bytes of a signed transaction with a dummy 65 bytes signature. +impl TransactionUnsigned { + /// Get a signed transaction with a dummy 65 bytes signature. pub fn dummy_signed_payload(&self) -> Vec { - let mut s = rlp::RlpStream::new(); - s.append(self); const DUMMY_SIGNATURE: [u8; 65] = [0u8; 65]; - s.append_raw(&DUMMY_SIGNATURE.as_ref(), 1); - s.out().to_vec() + self.unsigned_payload() + .into_iter() + .chain(DUMMY_SIGNATURE.iter().copied()) + .collect::>() } } @@ -567,7 +567,7 @@ mod test { #[test] fn dummy_signed_payload_works() { - let tx = TransactionLegacyUnsigned { + let tx: TransactionUnsigned = TransactionLegacyUnsigned { chain_id: Some(596.into()), gas: U256::from(21000), nonce: U256::from(1), @@ -576,10 +576,10 @@ mod test { value: U256::from(123123), input: Bytes(vec![]), r#type: TypeLegacy, - }; + } + .into(); let dummy_signed_payload = tx.dummy_signed_payload(); - let tx: TransactionUnsigned = tx.into(); let payload = Account::default().sign_transaction(tx).signed_payload(); assert_eq!(dummy_signed_payload.len(), payload.len()); } diff --git a/substrate/frame/revive/src/evm/api/rpc_types.rs b/substrate/frame/revive/src/evm/api/rpc_types.rs index 338be88e69b5..ed046cb4da44 100644 --- a/substrate/frame/revive/src/evm/api/rpc_types.rs +++ b/substrate/frame/revive/src/evm/api/rpc_types.rs @@ -28,6 +28,18 @@ impl From for BlockNumberOrTagOrHash { } } +impl From for TransactionUnsigned { + fn from(tx: TransactionSigned) -> Self { + use TransactionSigned::*; + match tx { + Transaction4844Signed(tx) => tx.transaction_4844_unsigned.into(), + Transaction1559Signed(tx) => tx.transaction_1559_unsigned.into(), + Transaction2930Signed(tx) => tx.transaction_2930_unsigned.into(), + TransactionLegacySigned(tx) => tx.transaction_legacy_unsigned.into(), + } + } +} + impl TransactionInfo { /// Create a new [`TransactionInfo`] from a receipt and a signed transaction. pub fn new(receipt: ReceiptInfo, transaction_signed: TransactionSigned) -> Self { @@ -152,76 +164,69 @@ fn logs_bloom_works() { impl GenericTransaction { /// Create a new [`GenericTransaction`] from a signed transaction. pub fn from_signed(tx: TransactionSigned, from: Option) -> Self { - use TransactionSigned::*; + Self::from_unsigned(tx.into(), from) + } + + /// Create a new [`GenericTransaction`] from a unsigned transaction. + pub fn from_unsigned(tx: TransactionUnsigned, from: Option) -> Self { + use TransactionUnsigned::*; match tx { - TransactionLegacySigned(tx) => { - let tx = tx.transaction_legacy_unsigned; - GenericTransaction { - from, - r#type: Some(tx.r#type.as_byte()), - chain_id: tx.chain_id, - input: Some(tx.input), - nonce: Some(tx.nonce), - value: Some(tx.value), - to: tx.to, - gas: Some(tx.gas), - gas_price: Some(tx.gas_price), - ..Default::default() - } + TransactionLegacyUnsigned(tx) => GenericTransaction { + from, + r#type: Some(tx.r#type.as_byte()), + chain_id: tx.chain_id, + input: Some(tx.input), + nonce: Some(tx.nonce), + value: Some(tx.value), + to: tx.to, + gas: Some(tx.gas), + gas_price: Some(tx.gas_price), + ..Default::default() }, - Transaction4844Signed(tx) => { - let tx = tx.transaction_4844_unsigned; - GenericTransaction { - from, - r#type: Some(tx.r#type.as_byte()), - chain_id: Some(tx.chain_id), - input: Some(tx.input), - nonce: Some(tx.nonce), - value: Some(tx.value), - to: Some(tx.to), - gas: Some(tx.gas), - gas_price: Some(tx.max_fee_per_blob_gas), - access_list: Some(tx.access_list), - blob_versioned_hashes: tx.blob_versioned_hashes, - max_fee_per_blob_gas: Some(tx.max_fee_per_blob_gas), - max_fee_per_gas: Some(tx.max_fee_per_gas), - max_priority_fee_per_gas: Some(tx.max_priority_fee_per_gas), - ..Default::default() - } + Transaction4844Unsigned(tx) => GenericTransaction { + from, + r#type: Some(tx.r#type.as_byte()), + chain_id: Some(tx.chain_id), + input: Some(tx.input), + nonce: Some(tx.nonce), + value: Some(tx.value), + to: Some(tx.to), + gas: Some(tx.gas), + gas_price: Some(tx.max_fee_per_blob_gas), + access_list: Some(tx.access_list), + blob_versioned_hashes: tx.blob_versioned_hashes, + max_fee_per_blob_gas: Some(tx.max_fee_per_blob_gas), + max_fee_per_gas: Some(tx.max_fee_per_gas), + max_priority_fee_per_gas: Some(tx.max_priority_fee_per_gas), + ..Default::default() }, - Transaction1559Signed(tx) => { - let tx = tx.transaction_1559_unsigned; - GenericTransaction { - from, - r#type: Some(tx.r#type.as_byte()), - chain_id: Some(tx.chain_id), - input: Some(tx.input), - nonce: Some(tx.nonce), - value: Some(tx.value), - to: tx.to, - gas: Some(tx.gas), - gas_price: Some(tx.gas_price), - access_list: Some(tx.access_list), - max_fee_per_gas: Some(tx.max_fee_per_gas), - max_priority_fee_per_gas: Some(tx.max_priority_fee_per_gas), - ..Default::default() - } + Transaction1559Unsigned(tx) => GenericTransaction { + from, + r#type: Some(tx.r#type.as_byte()), + chain_id: Some(tx.chain_id), + input: Some(tx.input), + nonce: Some(tx.nonce), + value: Some(tx.value), + to: tx.to, + gas: Some(tx.gas), + gas_price: Some(tx.gas_price), + access_list: Some(tx.access_list), + max_fee_per_gas: Some(tx.max_fee_per_gas), + max_priority_fee_per_gas: Some(tx.max_priority_fee_per_gas), + ..Default::default() }, - Transaction2930Signed(tx) => { - let tx = tx.transaction_2930_unsigned; - GenericTransaction { - from, - r#type: Some(tx.r#type.as_byte()), - chain_id: Some(tx.chain_id), - input: Some(tx.input), - nonce: Some(tx.nonce), - value: Some(tx.value), - to: tx.to, - gas: Some(tx.gas), - gas_price: Some(tx.gas_price), - access_list: Some(tx.access_list), - ..Default::default() - } + Transaction2930Unsigned(tx) => GenericTransaction { + from, + r#type: Some(tx.r#type.as_byte()), + chain_id: Some(tx.chain_id), + input: Some(tx.input), + nonce: Some(tx.nonce), + value: Some(tx.value), + to: tx.to, + gas: Some(tx.gas), + gas_price: Some(tx.gas_price), + access_list: Some(tx.access_list), + ..Default::default() }, } } diff --git a/substrate/frame/revive/src/evm/api/rpc_types_gen.rs b/substrate/frame/revive/src/evm/api/rpc_types_gen.rs index a9206e38fcc8..1d65fdefdde6 100644 --- a/substrate/frame/revive/src/evm/api/rpc_types_gen.rs +++ b/substrate/frame/revive/src/evm/api/rpc_types_gen.rs @@ -94,7 +94,7 @@ pub struct Block { /// Uncles pub uncles: Vec, /// Withdrawals - #[serde(skip_serializing_if = "Vec::is_empty")] + #[serde(default, skip_serializing_if = "Vec::is_empty")] pub withdrawals: Vec, /// Withdrawals root #[serde(rename = "withdrawalsRoot", skip_serializing_if = "Option::is_none")] @@ -148,11 +148,11 @@ pub struct GenericTransaction { pub access_list: Option, /// blobVersionedHashes /// List of versioned blob hashes associated with the transaction's EIP-4844 data blobs. - #[serde(rename = "blobVersionedHashes", skip_serializing_if = "Vec::is_empty")] + #[serde(rename = "blobVersionedHashes", default, skip_serializing_if = "Vec::is_empty")] pub blob_versioned_hashes: Vec, /// blobs /// Raw blob data. - #[serde(skip_serializing_if = "Vec::is_empty")] + #[serde(default, skip_serializing_if = "Vec::is_empty")] pub blobs: Vec, /// chainId /// Chain ID that this transaction is valid on. @@ -392,7 +392,7 @@ pub struct Log { #[serde(skip_serializing_if = "Option::is_none")] pub removed: Option, /// topics - #[serde(skip_serializing_if = "Vec::is_empty")] + #[serde(default, skip_serializing_if = "Vec::is_empty")] pub topics: Vec, /// transaction hash #[serde(rename = "transactionHash")] diff --git a/substrate/frame/revive/src/evm/runtime.rs b/substrate/frame/revive/src/evm/runtime.rs index 40c210304ca2..e25069b77121 100644 --- a/substrate/frame/revive/src/evm/runtime.rs +++ b/substrate/frame/revive/src/evm/runtime.rs @@ -48,7 +48,7 @@ type CallOf = ::RuntimeCall; /// We use a fixed value for the gas price. /// This let us calculate the gas estimate for a transaction with the formula: /// `estimate_gas = substrate_fee / gas_price`. -pub const GAS_PRICE: u32 = 1u32; +pub const GAS_PRICE: u32 = 1_000u32; /// Wraps [`generic::UncheckedExtrinsic`] to support checking unsigned /// [`crate::Call::eth_transact`] extrinsic. @@ -451,7 +451,7 @@ mod test { /// A builder for creating an unchecked extrinsic, and test that the check function works. #[derive(Clone)] struct UncheckedExtrinsicBuilder { - tx: TransactionLegacyUnsigned, + tx: GenericTransaction, gas_limit: Weight, storage_deposit_limit: BalanceOf, } @@ -460,9 +460,10 @@ mod test { /// Create a new builder with default values. fn new() -> Self { Self { - tx: TransactionLegacyUnsigned { + tx: GenericTransaction { + from: Some(Account::default().address()), chain_id: Some(::ChainId::get().into()), - gas_price: U256::from(GAS_PRICE), + gas_price: Some(U256::from(GAS_PRICE)), ..Default::default() }, gas_limit: Weight::zero(), @@ -471,22 +472,22 @@ mod test { } fn estimate_gas(&mut self) { - let dry_run = crate::Pallet::::bare_eth_transact( - Account::default().substrate_account(), - self.tx.to, - self.tx.value.try_into().unwrap(), - self.tx.input.clone().0, - Weight::MAX, - u64::MAX, - |call| { + let dry_run = + crate::Pallet::::bare_eth_transact(self.tx.clone(), Weight::MAX, |call| { let call = RuntimeCall::Contracts(call); let uxt: Ex = sp_runtime::generic::UncheckedExtrinsic::new_bare(call).into(); uxt.encoded_size() as u32 + }); + + match dry_run { + Ok(dry_run) => { + log::debug!(target: LOG_TARGET, "Estimated gas: {:?}", dry_run.eth_gas); + self.tx.gas = Some(dry_run.eth_gas); }, - crate::DebugInfo::Skip, - crate::CollectEvents::Skip, - ); - self.tx.gas = ((dry_run.fee + GAS_PRICE as u64) / (GAS_PRICE as u64)).into(); + Err(err) => { + log::debug!(target: LOG_TARGET, "Failed to estimate gas: {:?}", err); + }, + } } /// Create a new builder with a call to the given address. @@ -500,13 +501,13 @@ mod test { /// Create a new builder with an instantiate call. fn instantiate_with(code: Vec, data: Vec) -> Self { let mut builder = Self::new(); - builder.tx.input = Bytes(code.into_iter().chain(data.into_iter()).collect()); + builder.tx.input = Some(Bytes(code.into_iter().chain(data.into_iter()).collect())); builder.estimate_gas(); builder } /// Update the transaction with the given function. - fn update(mut self, f: impl FnOnce(&mut TransactionLegacyUnsigned) -> ()) -> Self { + fn update(mut self, f: impl FnOnce(&mut GenericTransaction) -> ()) -> Self { f(&mut self.tx); self } @@ -522,7 +523,8 @@ mod test { 100_000_000_000_000, ); - let payload = account.sign_transaction(tx.into()).signed_payload(); + let payload = + account.sign_transaction(tx.try_into_unsigned().unwrap()).signed_payload(); let call = RuntimeCall::Contracts(crate::Call::eth_transact { payload, gas_limit, @@ -557,10 +559,10 @@ mod test { builder.check().unwrap().0, crate::Call::call:: { dest: builder.tx.to.unwrap(), - value: builder.tx.value.as_u64(), + value: builder.tx.value.unwrap_or_default().as_u64(), gas_limit: builder.gas_limit, storage_deposit_limit: builder.storage_deposit_limit, - data: builder.tx.input.0 + data: builder.tx.input.unwrap_or_default().0 } .into() ); @@ -577,7 +579,7 @@ mod test { assert_eq!( builder.check().unwrap().0, crate::Call::instantiate_with_code:: { - value: builder.tx.value.as_u64(), + value: builder.tx.value.unwrap_or_default().as_u64(), gas_limit: builder.gas_limit, storage_deposit_limit: builder.storage_deposit_limit, code, @@ -593,7 +595,7 @@ mod test { fn check_eth_transact_nonce_works() { ExtBuilder::default().build().execute_with(|| { let builder = UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20])) - .update(|tx| tx.nonce = 1u32.into()); + .update(|tx| tx.nonce = Some(1u32.into())); assert_eq!( builder.check(), @@ -632,7 +634,7 @@ mod test { // Fail because the tx input fail to get the blob length assert_eq!( - builder.clone().update(|tx| tx.input = Bytes(vec![1, 2, 3])).check(), + builder.clone().update(|tx| tx.input = Some(Bytes(vec![1, 2, 3]))).check(), Err(TransactionValidityError::Invalid(InvalidTransaction::Call)) ); }); @@ -641,20 +643,40 @@ mod test { #[test] fn check_transaction_fees() { ExtBuilder::default().build().execute_with(|| { - let scenarios: [(_, Box, _); 5] = [ - ("Eth fees too low", Box::new(|tx| tx.gas_price /= 2), InvalidTransaction::Payment), - ("Gas fees too high", Box::new(|tx| tx.gas *= 2), InvalidTransaction::Call), - ("Gas fees too low", Box::new(|tx| tx.gas *= 2), InvalidTransaction::Call), + let scenarios: [(_, Box, _); 5] = [ + ( + "Eth fees too low", + Box::new(|tx| { + tx.gas_price = Some(tx.gas_price.unwrap() / 2); + }), + InvalidTransaction::Payment, + ), + ( + "Gas fees too high", + Box::new(|tx| { + tx.gas = Some(tx.gas.unwrap() * 2); + }), + InvalidTransaction::Call, + ), + ( + "Gas fees too low", + Box::new(|tx| { + tx.gas = Some(tx.gas.unwrap() * 2); + }), + InvalidTransaction::Call, + ), ( "Diff > 10%", - Box::new(|tx| tx.gas = tx.gas * 111 / 100), + Box::new(|tx| { + tx.gas = Some(tx.gas.unwrap() * 111 / 100); + }), InvalidTransaction::Call, ), ( "Diff < 10%", Box::new(|tx| { - tx.gas_price *= 2; - tx.gas = tx.gas * 89 / 100 + tx.gas_price = Some(tx.gas_price.unwrap() * 2); + tx.gas = Some(tx.gas.unwrap() * 89 / 100); }), InvalidTransaction::Call, ), @@ -671,14 +693,19 @@ mod test { #[test] fn check_transaction_tip() { + let _ = env_logger::builder().is_test(true).try_init(); ExtBuilder::default().build().execute_with(|| { let (code, _) = compile_module("dummy").unwrap(); let data = vec![]; let builder = UncheckedExtrinsicBuilder::instantiate_with(code.clone(), data.clone()) - .update(|tx| tx.gas_price = tx.gas_price * 103 / 100); + .update(|tx| { + tx.gas_price = Some(tx.gas_price.unwrap() * 103 / 100); + log::debug!(target: LOG_TARGET, "Gas price: {:?}", tx.gas_price); + }); let tx = &builder.tx; - let expected_tip = tx.gas_price * tx.gas - U256::from(GAS_PRICE) * tx.gas; + let expected_tip = + tx.gas_price.unwrap() * tx.gas.unwrap() - U256::from(GAS_PRICE) * tx.gas.unwrap(); let (_, extra) = builder.check().unwrap(); assert_eq!(U256::from(extra.1.tip()), expected_tip); }); diff --git a/substrate/frame/revive/src/exec.rs b/substrate/frame/revive/src/exec.rs index 49c08166483e..3f88b3087e9c 100644 --- a/substrate/frame/revive/src/exec.rs +++ b/substrate/frame/revive/src/exec.rs @@ -562,6 +562,8 @@ pub struct Stack<'a, T: Config, E> { debug_message: Option<&'a mut DebugBuffer>, /// Transient storage used to store data, which is kept for the duration of a transaction. transient_storage: TransientStorage, + /// Whether or not actual transfer of funds should be performed. + unchecked: bool, /// No executable is held by the struct but influences its behaviour. _phantom: PhantomData, } @@ -777,6 +779,7 @@ where storage_meter: &'a mut storage::meter::Meter, value: U256, input_data: Vec, + unchecked: bool, debug_message: Option<&'a mut DebugBuffer>, ) -> ExecResult { let dest = T::AddressMapper::to_account_id(&dest); @@ -786,6 +789,7 @@ where gas_meter, storage_meter, value, + unchecked, debug_message, )? { stack.run(executable, input_data).map(|_| stack.first_frame.last_frame_output) @@ -812,6 +816,7 @@ where value: U256, input_data: Vec, salt: Option<&[u8; 32]>, + unchecked: bool, debug_message: Option<&'a mut DebugBuffer>, ) -> Result<(H160, ExecReturnValue), ExecError> { let (mut stack, executable) = Self::new( @@ -825,6 +830,7 @@ where gas_meter, storage_meter, value, + unchecked, debug_message, )? .expect(FRAME_ALWAYS_EXISTS_ON_INSTANTIATE); @@ -869,6 +875,7 @@ where gas_meter: &'a mut GasMeter, storage_meter: &'a mut storage::meter::Meter, value: U256, + unchecked: bool, debug_message: Option<&'a mut DebugBuffer>, ) -> Result, ExecError> { origin.ensure_mapped()?; @@ -896,6 +903,7 @@ where frames: Default::default(), debug_message, transient_storage: TransientStorage::new(limits::TRANSIENT_STORAGE_BYTES), + unchecked, _phantom: Default::default(), }; @@ -1073,6 +1081,7 @@ where &frame.account_id, frame.contract_info.get(&frame.account_id), executable.code_info(), + self.unchecked, )?; // Needs to be incremented before calling into the code so that it is visible // in case of recursion. @@ -2101,6 +2110,7 @@ mod tests { &mut storage_meter, value.into(), vec![], + false, None, ), Ok(_) @@ -2193,6 +2203,7 @@ mod tests { &mut storage_meter, value.into(), vec![], + false, None, ) .unwrap(); @@ -2233,6 +2244,7 @@ mod tests { &mut storage_meter, value.into(), vec![], + false, None, )); @@ -2269,6 +2281,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, ), ExecError { @@ -2286,6 +2299,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, )); }); @@ -2314,6 +2328,7 @@ mod tests { &mut storage_meter, 55u64.into(), vec![], + false, None, ) .unwrap(); @@ -2363,6 +2378,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, ); @@ -2392,6 +2408,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, ); @@ -2421,6 +2438,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![1, 2, 3, 4], + false, None, ); assert_matches!(result, Ok(_)); @@ -2457,6 +2475,7 @@ mod tests { min_balance.into(), vec![1, 2, 3, 4], Some(&[0; 32]), + false, None, ); assert_matches!(result, Ok(_)); @@ -2511,6 +2530,7 @@ mod tests { &mut storage_meter, value.into(), vec![], + false, None, ); @@ -2575,6 +2595,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, ); @@ -2640,6 +2661,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, ); @@ -2672,6 +2694,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, ); assert_matches!(result, Ok(_)); @@ -2709,6 +2732,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![0], + false, None, ); assert_matches!(result, Ok(_)); @@ -2735,6 +2759,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![0], + false, None, ); assert_matches!(result, Ok(_)); @@ -2779,6 +2804,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![0], + false, None, ); assert_matches!(result, Ok(_)); @@ -2805,6 +2831,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![0], + false, None, ); assert_matches!(result, Ok(_)); @@ -2831,6 +2858,7 @@ mod tests { &mut storage_meter, 1u64.into(), vec![0], + false, None, ); assert_matches!(result, Err(_)); @@ -2875,6 +2903,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![0], + false, None, ); assert_matches!(result, Ok(_)); @@ -2920,6 +2949,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, ); @@ -2946,6 +2976,7 @@ mod tests { U256::zero(), // <- zero value vec![], Some(&[0; 32]), + false, None, ), Err(_) @@ -2981,6 +3012,7 @@ mod tests { min_balance.into(), vec![], Some(&[0 ;32]), + false, None, ), Ok((address, ref output)) if output.data == vec![80, 65, 83, 83] => address @@ -3032,10 +3064,10 @@ mod tests { executable, &mut gas_meter, &mut storage_meter, - min_balance.into(), vec![], Some(&[0; 32]), + false, None, ), Ok((address, ref output)) if output.data == vec![70, 65, 73, 76] => address @@ -3100,6 +3132,7 @@ mod tests { &mut storage_meter, (min_balance * 10).into(), vec![], + false, None, ), Ok(_) @@ -3180,6 +3213,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, ), Ok(_) @@ -3223,6 +3257,7 @@ mod tests { 100u64.into(), vec![], Some(&[0; 32]), + false, None, ), Err(Error::::TerminatedInConstructor.into()) @@ -3287,6 +3322,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![0], + false, None, ); assert_matches!(result, Ok(_)); @@ -3349,6 +3385,7 @@ mod tests { 10u64.into(), vec![], Some(&[0; 32]), + false, None, ); assert_matches!(result, Ok(_)); @@ -3395,6 +3432,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, ) .unwrap(); @@ -3426,6 +3464,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, Some(&mut debug_buffer), ) .unwrap(); @@ -3459,6 +3498,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, Some(&mut debug_buffer), ); assert!(result.is_err()); @@ -3492,6 +3532,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, Some(&mut debug_buf_after), ) .unwrap(); @@ -3525,6 +3566,7 @@ mod tests { &mut storage_meter, U256::zero(), CHARLIE_ADDR.as_bytes().to_vec(), + false, None, )); @@ -3537,6 +3579,7 @@ mod tests { &mut storage_meter, U256::zero(), BOB_ADDR.as_bytes().to_vec(), + false, None, ) .map_err(|e| e.error), @@ -3587,6 +3630,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![0], + false, None, ) .map_err(|e| e.error), @@ -3621,6 +3665,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, ) .unwrap(); @@ -3705,6 +3750,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, ) .unwrap(); @@ -3831,6 +3877,7 @@ mod tests { (min_balance * 100).into(), vec![], Some(&[0; 32]), + false, None, ) .ok(); @@ -3844,6 +3891,7 @@ mod tests { (min_balance * 100).into(), vec![], Some(&[0; 32]), + false, None, )); assert_eq!(System::account_nonce(&ALICE), 1); @@ -3856,6 +3904,7 @@ mod tests { (min_balance * 200).into(), vec![], Some(&[0; 32]), + false, None, )); assert_eq!(System::account_nonce(&ALICE), 2); @@ -3868,6 +3917,7 @@ mod tests { (min_balance * 200).into(), vec![], Some(&[0; 32]), + false, None, )); assert_eq!(System::account_nonce(&ALICE), 3); @@ -3936,6 +3986,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, )); }); @@ -4047,6 +4098,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, )); }); @@ -4086,6 +4138,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, )); }); @@ -4125,6 +4178,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, )); }); @@ -4178,6 +4232,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, )); }); @@ -4234,6 +4289,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, )); }); @@ -4309,6 +4365,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, )); }); @@ -4379,6 +4436,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![0], + false, None, ); assert_matches!(result, Ok(_)); @@ -4417,6 +4475,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, )); }); @@ -4479,6 +4538,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![0], + false, None, ); assert_matches!(result, Ok(_)); @@ -4512,6 +4572,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, ); assert_matches!(result, Ok(_)); @@ -4595,6 +4656,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, ) .unwrap() @@ -4663,6 +4725,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![0], + false, None, ); assert_matches!(result, Ok(_)); @@ -4734,6 +4797,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, ); assert_matches!(result, Ok(_)); @@ -4785,6 +4849,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, ) .unwrap() @@ -4854,6 +4919,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, ) .unwrap() @@ -4900,6 +4966,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, ) .unwrap() @@ -4944,6 +5011,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![], + false, None, ) .unwrap() @@ -4999,6 +5067,7 @@ mod tests { &mut storage_meter, U256::zero(), vec![0], + false, None, ), Ok(_) diff --git a/substrate/frame/revive/src/lib.rs b/substrate/frame/revive/src/lib.rs index b55854e2eec5..12a55004600c 100644 --- a/substrate/frame/revive/src/lib.rs +++ b/substrate/frame/revive/src/lib.rs @@ -41,13 +41,13 @@ pub mod test_utils; pub mod weights; use crate::{ - evm::{runtime::GAS_PRICE, TransactionLegacyUnsigned}, + evm::{runtime::GAS_PRICE, GenericTransaction}, exec::{AccountIdOf, ExecError, Executable, Ext, Key, Origin, Stack as ExecStack}, gas::GasMeter, storage::{meter::Meter as StorageMeter, ContractInfo, DeletionQueueManager}, wasm::{CodeInfo, RuntimeCosts, WasmBlob}, }; -use alloc::boxed::Box; +use alloc::{boxed::Box, format, vec}; use codec::{Codec, Decode, Encode}; use environmental::*; use frame_support::{ @@ -74,7 +74,7 @@ use pallet_transaction_payment::OnChargeTransaction; use scale_info::TypeInfo; use sp_core::{H160, H256, U256}; use sp_runtime::{ - traits::{BadOrigin, Convert, Dispatchable, Saturating, Zero}, + traits::{BadOrigin, Bounded, Convert, Dispatchable, Saturating, Zero}, DispatchError, }; @@ -573,6 +573,8 @@ pub mod pallet { AccountUnmapped, /// Tried to map an account that is already mapped. AccountAlreadyMapped, + /// The transaction used to dry-run a contract is invalid. + InvalidGenericTransaction, } /// A reason for the pallet contracts placing a hold on funds. @@ -823,7 +825,7 @@ pub mod pallet { dest, value, gas_limit, - storage_deposit_limit, + DepositLimit::Balance(storage_deposit_limit), data, DebugInfo::Skip, CollectEvents::Skip, @@ -859,7 +861,7 @@ pub mod pallet { origin, value, gas_limit, - storage_deposit_limit, + DepositLimit::Balance(storage_deposit_limit), Code::Existing(code_hash), data, salt, @@ -925,7 +927,7 @@ pub mod pallet { origin, value, gas_limit, - storage_deposit_limit, + DepositLimit::Balance(storage_deposit_limit), Code::Upload(code), data, salt, @@ -1083,7 +1085,7 @@ fn dispatch_result( impl Pallet where - BalanceOf: Into + TryFrom, + BalanceOf: Into + TryFrom + Bounded, MomentOf: Into, T::Hash: frame_support::traits::IsType, { @@ -1098,7 +1100,7 @@ where dest: H160, value: BalanceOf, gas_limit: Weight, - storage_deposit_limit: BalanceOf, + storage_deposit_limit: DepositLimit>, data: Vec, debug: DebugInfo, collect_events: CollectEvents, @@ -1112,7 +1114,10 @@ where }; let try_call = || { let origin = Origin::from_runtime_origin(origin)?; - let mut storage_meter = StorageMeter::new(&origin, storage_deposit_limit, value)?; + let mut storage_meter = match storage_deposit_limit { + DepositLimit::Balance(limit) => StorageMeter::new(&origin, limit, value)?, + DepositLimit::Unchecked => StorageMeter::new_unchecked(BalanceOf::::max_value()), + }; let result = ExecStack::>::run_call( origin.clone(), dest, @@ -1120,9 +1125,14 @@ where &mut storage_meter, Self::convert_native_to_evm(value), data, + storage_deposit_limit.is_unchecked(), debug_message.as_mut(), )?; - storage_deposit = storage_meter.try_into_deposit(&origin)?; + storage_deposit = storage_meter + .try_into_deposit(&origin, storage_deposit_limit.is_unchecked()) + .inspect_err(|err| { + log::error!(target: LOG_TARGET, "Failed to transfer deposit: {err:?}"); + })?; Ok(result) }; let result = Self::run_guarded(try_call); @@ -1151,7 +1161,7 @@ where origin: OriginFor, value: BalanceOf, gas_limit: Weight, - mut storage_deposit_limit: BalanceOf, + storage_deposit_limit: DepositLimit>, code: Code, data: Vec, salt: Option<[u8; 32]>, @@ -1162,13 +1172,24 @@ where let mut storage_deposit = Default::default(); let mut debug_message = if debug == DebugInfo::UnsafeDebug { Some(DebugBuffer::default()) } else { None }; + + let unchecked_deposit_limit = storage_deposit_limit.is_unchecked(); + let mut storage_deposit_limit = match storage_deposit_limit { + DepositLimit::Balance(limit) => limit, + DepositLimit::Unchecked => BalanceOf::::max_value(), + }; + let try_instantiate = || { let instantiate_account = T::InstantiateOrigin::ensure_origin(origin.clone())?; let (executable, upload_deposit) = match code { Code::Upload(code) => { let upload_account = T::UploadOrigin::ensure_origin(origin)?; - let (executable, upload_deposit) = - Self::try_upload_code(upload_account, code, storage_deposit_limit)?; + let (executable, upload_deposit) = Self::try_upload_code( + upload_account, + code, + storage_deposit_limit, + unchecked_deposit_limit, + )?; storage_deposit_limit.saturating_reduce(upload_deposit); (executable, upload_deposit) }, @@ -1176,8 +1197,12 @@ where (WasmBlob::from_storage(code_hash, &mut gas_meter)?, Default::default()), }; let instantiate_origin = Origin::from_account_id(instantiate_account.clone()); - let mut storage_meter = - StorageMeter::new(&instantiate_origin, storage_deposit_limit, value)?; + let mut storage_meter = if unchecked_deposit_limit { + StorageMeter::new_unchecked(storage_deposit_limit) + } else { + StorageMeter::new(&instantiate_origin, storage_deposit_limit, value)? + }; + let result = ExecStack::>::run_instantiate( instantiate_account, executable, @@ -1186,10 +1211,11 @@ where Self::convert_native_to_evm(value), data, salt.as_ref(), + unchecked_deposit_limit, debug_message.as_mut(), ); storage_deposit = storage_meter - .try_into_deposit(&instantiate_origin)? + .try_into_deposit(&instantiate_origin, unchecked_deposit_limit)? .saturating_add(&StorageDeposit::Charge(upload_deposit)); result }; @@ -1227,16 +1253,10 @@ where /// - `debug`: Debugging configuration. /// - `collect_events`: Event collection configuration. pub fn bare_eth_transact( - origin: T::AccountId, - dest: Option, - value: U256, - input: Vec, + mut tx: GenericTransaction, gas_limit: Weight, - storage_deposit_limit: BalanceOf, utx_encoded_size: impl Fn(Call) -> u32, - debug: DebugInfo, - collect_events: CollectEvents, - ) -> EthContractResult> + ) -> Result>, EthTransactError> where T: pallet_transaction_payment::Config, ::RuntimeCall: @@ -1247,26 +1267,61 @@ where T::Nonce: Into, T::Hash: frame_support::traits::IsType, { - log::debug!(target: LOG_TARGET, "bare_eth_transact: dest: {dest:?} value: {value:?} - gas_limit: {gas_limit:?} storage_deposit_limit: {storage_deposit_limit:?}"); + log::debug!(target: LOG_TARGET, "bare_eth_transact: tx: {tx:?} gas_limit: {gas_limit:?}"); + + let Some(from) = tx.from else { + return Err(EthTransactError::Message("Missing from address".into())); + }; + + let origin = T::AddressMapper::to_account_id(&from); - // Get the nonce to encode in the tx. - let nonce: T::Nonce = >::account_nonce(&origin); + let storage_deposit_limit = if tx.gas.is_some() { + DepositLimit::Balance(BalanceOf::::max_value()) + } else { + DepositLimit::Unchecked + }; + + // TODO remove once we have revisited how we encode the gas limit. + if tx.nonce.is_none() { + tx.nonce = Some(>::account_nonce(&origin).into()); + } + if tx.gas_price.is_none() { + tx.gas_price = Some(GAS_PRICE.into()); + } + if tx.chain_id.is_none() { + tx.chain_id = Some(T::ChainId::get().into()); + } // Convert the value to the native balance type. - let native_value = match Self::convert_evm_to_native(value) { + let evm_value = tx.value.unwrap_or_default(); + let native_value = match Self::convert_evm_to_native(evm_value) { Ok(v) => v, - Err(err) => - return EthContractResult { - gas_required: Default::default(), - storage_deposit: Default::default(), - fee: Default::default(), - result: Err(err.into()), - }, + Err(_) => return Err(EthTransactError::Message("Failed to convert value".into())), + }; + + let input = tx.input.clone().unwrap_or_default().0; + let debug = DebugInfo::Skip; + let collect_events = CollectEvents::Skip; + + let extract_error = |err| { + if err == Error::::TransferFailed.into() || + err == Error::::StorageDepositNotEnoughFunds.into() || + err == Error::::StorageDepositLimitExhausted.into() + { + let balance = Self::evm_balance(&from); + return Err(EthTransactError::Message( + format!("insufficient funds for gas * price + value: address {from:?} have {balance} want {evm_value} (supplied gas {})", + tx.gas.unwrap_or_default())) + ); + } + + return Err(EthTransactError::Message(format!( + "Failed to instantiate contract: {err:?}" + ))); }; // Dry run the call - let (mut result, dispatch_info) = match dest { + let (mut result, dispatch_info) = match tx.to { // A contract call. Some(dest) => { // Dry run the call. @@ -1281,11 +1336,24 @@ where collect_events, ); - let result = EthContractResult { + let data = match result.result { + Ok(return_value) => { + if return_value.did_revert() { + return Err(EthTransactError::Data(return_value.data)); + } + return_value.data + }, + Err(err) => { + log::debug!(target: LOG_TARGET, "Failed to execute call: {err:?}"); + return extract_error(err) + }, + }; + + let result = EthTransactInfo { gas_required: result.gas_required, storage_deposit: result.storage_deposit.charge_or_zero(), - result: result.result, - fee: Default::default(), + data, + eth_gas: Default::default(), }; // Get the dispatch info of the call. let dispatch_call: ::RuntimeCall = crate::Call::::call { @@ -1326,11 +1394,24 @@ where collect_events, ); - let result = EthContractResult { + let returned_data = match result.result { + Ok(return_value) => { + if return_value.result.did_revert() { + return Err(EthTransactError::Data(return_value.result.data)); + } + return_value.result.data + }, + Err(err) => { + log::debug!(target: LOG_TARGET, "Failed to instantiate: {err:?}"); + return extract_error(err) + }, + }; + + let result = EthTransactInfo { gas_required: result.gas_required, storage_deposit: result.storage_deposit.charge_or_zero(), - result: result.result.map(|v| v.result), - fee: Default::default(), + data: returned_data, + eth_gas: Default::default(), }; // Get the dispatch info of the call. @@ -1348,23 +1429,18 @@ where }, }; - let mut tx = TransactionLegacyUnsigned { - value, - input: input.into(), - nonce: nonce.into(), - chain_id: Some(T::ChainId::get().into()), - gas_price: GAS_PRICE.into(), - to: dest, - ..Default::default() - }; - // The transaction fees depend on the extrinsic's length, which in turn is influenced by // the encoded length of the gas limit specified in the transaction (tx.gas). // We iteratively compute the fee by adjusting tx.gas until the fee stabilizes. // with a maximum of 3 iterations to avoid an infinite loop. for _ in 0..3 { + let Ok(unsigned_tx) = tx.clone().try_into_unsigned() else { + log::debug!(target: LOG_TARGET, "Failed to convert to unsigned"); + return Err(EthTransactError::Message("Invalid transaction".into())); + }; + let eth_dispatch_call = crate::Call::::eth_transact { - payload: tx.dummy_signed_payload(), + payload: unsigned_tx.dummy_signed_payload(), gas_limit: result.gas_required, storage_deposit_limit: result.storage_deposit, }; @@ -1375,17 +1451,18 @@ where 0u32.into(), ) .into(); + let eth_gas: U256 = (fee / GAS_PRICE.into()).into(); - if fee == result.fee { - log::trace!(target: LOG_TARGET, "bare_eth_call: encoded_len: {encoded_len:?} fee: {fee:?}"); + if eth_gas == result.eth_gas { + log::trace!(target: LOG_TARGET, "bare_eth_call: encoded_len: {encoded_len:?} eth_gas: {eth_gas:?}"); break; } - result.fee = fee; - tx.gas = (fee / GAS_PRICE.into()).into(); - log::debug!(target: LOG_TARGET, "Adjusting Eth gas to: {:?}", tx.gas); + result.eth_gas = eth_gas; + tx.gas = Some(eth_gas.into()); + log::debug!(target: LOG_TARGET, "Adjusting Eth gas to: {eth_gas:?}"); } - result + Ok(result) } /// Get the balance with EVM decimals of the given `address`. @@ -1403,7 +1480,7 @@ where storage_deposit_limit: BalanceOf, ) -> CodeUploadResult> { let origin = T::UploadOrigin::ensure_origin(origin)?; - let (module, deposit) = Self::try_upload_code(origin, code, storage_deposit_limit)?; + let (module, deposit) = Self::try_upload_code(origin, code, storage_deposit_limit, false)?; Ok(CodeUploadReturnValue { code_hash: *module.code_hash(), deposit }) } @@ -1421,9 +1498,10 @@ where origin: T::AccountId, code: Vec, storage_deposit_limit: BalanceOf, + unchecked: bool, ) -> Result<(WasmBlob, BalanceOf), DispatchError> { let mut module = WasmBlob::from_code(code, origin)?; - let deposit = module.store_code()?; + let deposit = module.store_code(unchecked)?; ensure!(storage_deposit_limit >= deposit, >::StorageDepositLimitExhausted); Ok((module, deposit)) } @@ -1527,14 +1605,7 @@ sp_api::decl_runtime_apis! { /// Perform an Ethereum call. /// /// See [`crate::Pallet::bare_eth_transact`] - fn eth_transact( - origin: H160, - dest: Option, - value: U256, - input: Vec, - gas_limit: Option, - storage_deposit_limit: Option, - ) -> EthContractResult; + fn eth_transact(tx: GenericTransaction) -> Result, EthTransactError>; /// Upload new code without instantiating a contract from it. /// diff --git a/substrate/frame/revive/src/primitives.rs b/substrate/frame/revive/src/primitives.rs index 024b1f3448e1..c091ecf288c5 100644 --- a/substrate/frame/revive/src/primitives.rs +++ b/substrate/frame/revive/src/primitives.rs @@ -17,8 +17,8 @@ //! A crate that hosts a common definitions that are relevant for the pallet-revive. -use crate::H160; -use alloc::vec::Vec; +use crate::{H160, U256}; +use alloc::{string::String, vec::Vec}; use codec::{Decode, Encode, MaxEncodedLen}; use frame_support::weights::Weight; use pallet_revive_uapi::ReturnFlags; @@ -28,6 +28,27 @@ use sp_runtime::{ DispatchError, RuntimeDebug, }; +#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)] +pub enum DepositLimit { + Unchecked, + Balance(T), +} + +impl DepositLimit { + pub fn is_unchecked(&self) -> bool { + match self { + Self::Unchecked => true, + _ => false, + } + } +} + +impl From for DepositLimit { + fn from(value: T) -> Self { + Self::Balance(value) + } +} + /// Result type of a `bare_call` or `bare_instantiate` call as well as `ContractsApi::call` and /// `ContractsApi::instantiate`. /// @@ -84,15 +105,22 @@ pub struct ContractResult { /// The result of the execution of a `eth_transact` call. #[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)] -pub struct EthContractResult> { - /// The fee charged for the execution. - pub fee: Balance, +pub struct EthTransactInfo { /// The amount of gas that was necessary to execute the transaction. pub gas_required: Weight, /// Storage deposit charged. pub storage_deposit: Balance, - /// The execution result. - pub result: R, + /// The weight and deposit equivalent in EVM Gas. + pub eth_gas: U256, + /// The execution return value. + pub data: Vec, +} + +/// Error type of a `eth_transact` call. +#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)] +pub enum EthTransactError { + Data(Vec), + Message(String), } /// Result type of a `bare_code_upload` call. diff --git a/substrate/frame/revive/src/storage/meter.rs b/substrate/frame/revive/src/storage/meter.rs index 712010bc8257..f068509c34f1 100644 --- a/substrate/frame/revive/src/storage/meter.rs +++ b/substrate/frame/revive/src/storage/meter.rs @@ -373,24 +373,36 @@ where } } + /// Create new storage meter without checking the limit. + pub fn new_unchecked(limit: BalanceOf) -> Self { + return Self { limit, ..Default::default() } + } + /// The total amount of deposit that should change hands as result of the execution /// that this meter was passed into. This will also perform all the charges accumulated /// in the whole contract stack. /// /// This drops the root meter in order to make sure it is only called when the whole /// execution did finish. - pub fn try_into_deposit(self, origin: &Origin) -> Result, DispatchError> { - // Only refund or charge deposit if the origin is not root. - let origin = match origin { - Origin::Root => return Ok(Deposit::Charge(Zero::zero())), - Origin::Signed(o) => o, - }; - for charge in self.charges.iter().filter(|c| matches!(c.amount, Deposit::Refund(_))) { - E::charge(origin, &charge.contract, &charge.amount, &charge.state)?; - } - for charge in self.charges.iter().filter(|c| matches!(c.amount, Deposit::Charge(_))) { - E::charge(origin, &charge.contract, &charge.amount, &charge.state)?; + pub fn try_into_deposit( + self, + origin: &Origin, + unchecked: bool, + ) -> Result, DispatchError> { + if !unchecked { + // Only refund or charge deposit if the origin is not root. + let origin = match origin { + Origin::Root => return Ok(Deposit::Charge(Zero::zero())), + Origin::Signed(o) => o, + }; + for charge in self.charges.iter().filter(|c| matches!(c.amount, Deposit::Refund(_))) { + E::charge(origin, &charge.contract, &charge.amount, &charge.state)?; + } + for charge in self.charges.iter().filter(|c| matches!(c.amount, Deposit::Charge(_))) { + E::charge(origin, &charge.contract, &charge.amount, &charge.state)?; + } } + Ok(self.total_deposit) } } @@ -425,13 +437,18 @@ impl> RawMeter { contract: &T::AccountId, contract_info: &mut ContractInfo, code_info: &CodeInfo, + unchecked: bool, ) -> Result<(), DispatchError> { debug_assert!(matches!(self.contract_state(), ContractState::Alive)); // We need to make sure that the contract's account exists. let ed = Pallet::::min_balance(); self.total_deposit = Deposit::Charge(ed); - T::Currency::transfer(origin, contract, ed, Preservation::Preserve)?; + if unchecked { + T::Currency::set_balance(contract, ed); + } else { + T::Currency::transfer(origin, contract, ed, Preservation::Preserve)?; + } // A consumer is added at account creation and removed it on termination, otherwise the // runtime could remove the account. As long as a contract exists its account must exist. @@ -479,6 +496,7 @@ impl> RawMeter { } if let Deposit::Charge(amount) = total_deposit { if amount > self.limit { + log::debug!( target: LOG_TARGET, "Storage deposit limit exhausted: {:?} > {:?}", amount, self.limit); return Err(>::StorageDepositLimitExhausted.into()) } } @@ -811,7 +829,10 @@ mod tests { nested0.enforce_limit(Some(&mut nested0_info)).unwrap(); meter.absorb(nested0, &BOB, Some(&mut nested0_info)); - assert_eq!(meter.try_into_deposit(&test_case.origin).unwrap(), test_case.deposit); + assert_eq!( + meter.try_into_deposit(&test_case.origin, false).unwrap(), + test_case.deposit + ); assert_eq!(nested0_info.extra_deposit(), 112); assert_eq!(nested1_info.extra_deposit(), 110); @@ -882,7 +903,10 @@ mod tests { nested0.absorb(nested1, &CHARLIE, None); meter.absorb(nested0, &BOB, None); - assert_eq!(meter.try_into_deposit(&test_case.origin).unwrap(), test_case.deposit); + assert_eq!( + meter.try_into_deposit(&test_case.origin, false).unwrap(), + test_case.deposit + ); assert_eq!(TestExtTestValue::get(), test_case.expected) } } diff --git a/substrate/frame/revive/src/test_utils/builder.rs b/substrate/frame/revive/src/test_utils/builder.rs index e64f58894432..8ba5e7384070 100644 --- a/substrate/frame/revive/src/test_utils/builder.rs +++ b/substrate/frame/revive/src/test_utils/builder.rs @@ -18,7 +18,8 @@ use super::{deposit_limit, GAS_LIMIT}; use crate::{ address::AddressMapper, AccountIdOf, BalanceOf, Code, CollectEvents, Config, ContractResult, - DebugInfo, EventRecordOf, ExecReturnValue, InstantiateReturnValue, OriginFor, Pallet, Weight, + DebugInfo, DepositLimit, EventRecordOf, ExecReturnValue, InstantiateReturnValue, OriginFor, + Pallet, Weight, }; use frame_support::pallet_prelude::DispatchResultWithPostInfo; use paste::paste; @@ -133,7 +134,7 @@ builder!( origin: OriginFor, value: BalanceOf, gas_limit: Weight, - storage_deposit_limit: BalanceOf, + storage_deposit_limit: DepositLimit>, code: Code, data: Vec, salt: Option<[u8; 32]>, @@ -159,7 +160,7 @@ builder!( origin, value: 0u32.into(), gas_limit: GAS_LIMIT, - storage_deposit_limit: deposit_limit::(), + storage_deposit_limit: DepositLimit::Balance(deposit_limit::()), code, data: vec![], salt: Some([0; 32]), @@ -198,7 +199,7 @@ builder!( dest: H160, value: BalanceOf, gas_limit: Weight, - storage_deposit_limit: BalanceOf, + storage_deposit_limit: DepositLimit>, data: Vec, debug: DebugInfo, collect_events: CollectEvents, @@ -216,7 +217,7 @@ builder!( dest, value: 0u32.into(), gas_limit: GAS_LIMIT, - storage_deposit_limit: deposit_limit::(), + storage_deposit_limit: DepositLimit::Balance(deposit_limit::()), data: vec![], debug: DebugInfo::UnsafeDebug, collect_events: CollectEvents::Skip, diff --git a/substrate/frame/revive/src/tests.rs b/substrate/frame/revive/src/tests.rs index 34afe8aabfe6..1df300f031a7 100644 --- a/substrate/frame/revive/src/tests.rs +++ b/substrate/frame/revive/src/tests.rs @@ -1249,7 +1249,7 @@ fn transfer_expendable_cannot_kill_account() { test_utils::contract_info_storage_deposit(&addr) ); - // Some ot the total balance is held, so it can't be transferred. + // Some or the total balance is held, so it can't be transferred. assert_err!( <::Currency as Mutate>::transfer( &account, @@ -2290,7 +2290,7 @@ fn gas_estimation_for_subcalls() { // Make the same call using the estimated gas. Should succeed. let result = builder::bare_call(addr_caller) .gas_limit(result_orig.gas_required) - .storage_deposit_limit(result_orig.storage_deposit.charge_or_zero()) + .storage_deposit_limit(result_orig.storage_deposit.charge_or_zero().into()) .data(input.clone()) .build(); assert_ok!(&result.result); @@ -2298,7 +2298,7 @@ fn gas_estimation_for_subcalls() { // Check that it fails with too little ref_time let result = builder::bare_call(addr_caller) .gas_limit(result_orig.gas_required.sub_ref_time(1)) - .storage_deposit_limit(result_orig.storage_deposit.charge_or_zero()) + .storage_deposit_limit(result_orig.storage_deposit.charge_or_zero().into()) .data(input.clone()) .build(); assert_err!(result.result, error); @@ -2306,7 +2306,7 @@ fn gas_estimation_for_subcalls() { // Check that it fails with too little proof_size let result = builder::bare_call(addr_caller) .gas_limit(result_orig.gas_required.sub_proof_size(1)) - .storage_deposit_limit(result_orig.storage_deposit.charge_or_zero()) + .storage_deposit_limit(result_orig.storage_deposit.charge_or_zero().into()) .data(input.clone()) .build(); assert_err!(result.result, error); @@ -3592,7 +3592,7 @@ fn deposit_limit_in_nested_instantiate() { // Set enough deposit limit for the child instantiate. This should succeed. let result = builder::bare_call(addr_caller) .origin(RuntimeOrigin::signed(BOB)) - .storage_deposit_limit(callee_info_len + 2 + ED + 4 + 2) + .storage_deposit_limit((callee_info_len + 2 + ED + 4 + 2).into()) .data((1u32, &code_hash_callee, U256::from(callee_info_len + 2 + ED + 3 + 2)).encode()) .build(); @@ -3879,7 +3879,7 @@ fn locking_delegate_dependency_works() { // Locking a dependency with a storage limit too low should fail. assert_err!( builder::bare_call(addr_caller) - .storage_deposit_limit(dependency_deposit - 1) + .storage_deposit_limit((dependency_deposit - 1).into()) .data((1u32, hash2addr(&callee_hashes[0]), callee_hashes[0]).encode()) .build() .result, diff --git a/substrate/frame/revive/src/tests/test_debug.rs b/substrate/frame/revive/src/tests/test_debug.rs index 7c4fbba71f65..c9e19e52ace1 100644 --- a/substrate/frame/revive/src/tests/test_debug.rs +++ b/substrate/frame/revive/src/tests/test_debug.rs @@ -21,6 +21,7 @@ use crate::{ debug::{CallInterceptor, CallSpan, ExecResult, ExportedFunction, Tracing}, primitives::ExecReturnValue, test_utils::*, + DepositLimit, }; use frame_support::traits::Currency; use pretty_assertions::assert_eq; @@ -114,7 +115,7 @@ fn debugging_works() { RuntimeOrigin::signed(ALICE), 0, GAS_LIMIT, - deposit_limit::(), + DepositLimit::Balance(deposit_limit::()), Code::Upload(wasm), vec![], Some([0u8; 32]), @@ -198,7 +199,7 @@ fn call_interception_works() { RuntimeOrigin::signed(ALICE), 0, GAS_LIMIT, - deposit_limit::(), + deposit_limit::().into(), Code::Upload(wasm), vec![], // some salt to ensure that the address of this contract is unique among all tests diff --git a/substrate/frame/revive/src/wasm/mod.rs b/substrate/frame/revive/src/wasm/mod.rs index f10c4f5fddf8..82aa67a1d678 100644 --- a/substrate/frame/revive/src/wasm/mod.rs +++ b/substrate/frame/revive/src/wasm/mod.rs @@ -183,7 +183,7 @@ where } /// Puts the module blob into storage, and returns the deposit collected for the storage. - pub fn store_code(&mut self) -> Result, Error> { + pub fn store_code(&mut self, unchecked: bool) -> Result, Error> { let code_hash = *self.code_hash(); >::mutate(code_hash, |stored_code_info| { match stored_code_info { @@ -195,15 +195,16 @@ where // the `owner` is always the origin of the current transaction. None => { let deposit = self.code_info.deposit; - T::Currency::hold( + + if !unchecked { + T::Currency::hold( &HoldReason::CodeUploadDepositReserve.into(), &self.code_info.owner, deposit, - ) - .map_err(|err| { - log::debug!(target: LOG_TARGET, "failed to store code for owner: {:?}: {err:?}", self.code_info.owner); + ) .map_err(|err| { log::debug!(target: LOG_TARGET, "failed to store code for owner: {:?}: {err:?}", self.code_info.owner); >::StorageDepositNotEnoughFunds })?; + } self.code_info.refcount = 0; >::insert(code_hash, &self.code); From 3a10a37f0b80ceb46835c26814fbde8dc85d7556 Mon Sep 17 00:00:00 2001 From: pgherveou Date: Tue, 26 Nov 2024 15:22:37 +0100 Subject: [PATCH 8/8] fixes --- substrate/frame/revive/src/evm/runtime.rs | 334 +++++++++++----------- 1 file changed, 168 insertions(+), 166 deletions(-) diff --git a/substrate/frame/revive/src/evm/runtime.rs b/substrate/frame/revive/src/evm/runtime.rs index e25069b77121..a25ea53d276c 100644 --- a/substrate/frame/revive/src/evm/runtime.rs +++ b/substrate/frame/revive/src/evm/runtime.rs @@ -48,7 +48,7 @@ type CallOf = ::RuntimeCall; /// We use a fixed value for the gas price. /// This let us calculate the gas estimate for a transaction with the formula: /// `estimate_gas = substrate_fee / gas_price`. -pub const GAS_PRICE: u32 = 1_000u32; +pub const GAS_PRICE: u32 = 1u32; /// Wraps [`generic::UncheckedExtrinsic`] to support checking unsigned /// [`crate::Call::eth_transact`] extrinsic. @@ -454,6 +454,7 @@ mod test { tx: GenericTransaction, gas_limit: Weight, storage_deposit_limit: BalanceOf, + before_validate: Option>, } impl UncheckedExtrinsicBuilder { @@ -468,6 +469,7 @@ mod test { }, gas_limit: Weight::zero(), storage_deposit_limit: 0, + before_validate: None, } } @@ -494,7 +496,7 @@ mod test { fn call_with(dest: H160) -> Self { let mut builder = Self::new(); builder.tx.to = Some(dest); - builder.estimate_gas(); + ExtBuilder::default().build().execute_with(|| builder.estimate_gas()); builder } @@ -502,7 +504,7 @@ mod test { fn instantiate_with(code: Vec, data: Vec) -> Self { let mut builder = Self::new(); builder.tx.input = Some(Bytes(code.into_iter().chain(data.into_iter()).collect())); - builder.estimate_gas(); + ExtBuilder::default().build().execute_with(|| builder.estimate_gas()); builder } @@ -511,203 +513,203 @@ mod test { f(&mut self.tx); self } + /// Set before_validate function. + fn before_validate(mut self, f: impl Fn() + Send + Sync + 'static) -> Self { + self.before_validate = Some(std::sync::Arc::new(f)); + self + } /// Call `check` on the unchecked extrinsic, and `pre_dispatch` on the signed extension. fn check(&self) -> Result<(RuntimeCall, SignedExtra), TransactionValidityError> { - let UncheckedExtrinsicBuilder { tx, gas_limit, storage_deposit_limit } = self.clone(); - - // Fund the account. - let account = Account::default(); - let _ = ::Currency::set_balance( - &account.substrate_account(), - 100_000_000_000_000, - ); - - let payload = - account.sign_transaction(tx.try_into_unsigned().unwrap()).signed_payload(); - let call = RuntimeCall::Contracts(crate::Call::eth_transact { - payload, - gas_limit, - storage_deposit_limit, - }); - - let encoded_len = call.encoded_size(); - let uxt: Ex = generic::UncheckedExtrinsic::new_bare(call).into(); - let result: CheckedExtrinsic<_, _, _> = uxt.check(&TestContext {})?; - let (account_id, extra): (AccountId32, SignedExtra) = match result.format { - ExtrinsicFormat::Signed(signer, extra) => (signer, extra), - _ => unreachable!(), - }; + ExtBuilder::default().build().execute_with(|| { + let UncheckedExtrinsicBuilder { + tx, + gas_limit, + storage_deposit_limit, + before_validate, + } = self.clone(); + + // Fund the account. + let account = Account::default(); + let _ = ::Currency::set_balance( + &account.substrate_account(), + 100_000_000_000_000, + ); + + let payload = + account.sign_transaction(tx.try_into_unsigned().unwrap()).signed_payload(); + let call = RuntimeCall::Contracts(crate::Call::eth_transact { + payload, + gas_limit, + storage_deposit_limit, + }); - extra.clone().validate_and_prepare( - RuntimeOrigin::signed(account_id), - &result.function, - &result.function.get_dispatch_info(), - encoded_len, - 0, - )?; + let encoded_len = call.encoded_size(); + let uxt: Ex = generic::UncheckedExtrinsic::new_bare(call).into(); + let result: CheckedExtrinsic<_, _, _> = uxt.check(&TestContext {})?; + let (account_id, extra): (AccountId32, SignedExtra) = match result.format { + ExtrinsicFormat::Signed(signer, extra) => (signer, extra), + _ => unreachable!(), + }; - Ok((result.function, extra)) + before_validate.map(|f| f()); + extra.clone().validate_and_prepare( + RuntimeOrigin::signed(account_id), + &result.function, + &result.function.get_dispatch_info(), + encoded_len, + 0, + )?; + + Ok((result.function, extra)) + }) } } #[test] fn check_eth_transact_call_works() { - ExtBuilder::default().build().execute_with(|| { - let builder = UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20])); - assert_eq!( - builder.check().unwrap().0, - crate::Call::call:: { - dest: builder.tx.to.unwrap(), - value: builder.tx.value.unwrap_or_default().as_u64(), - gas_limit: builder.gas_limit, - storage_deposit_limit: builder.storage_deposit_limit, - data: builder.tx.input.unwrap_or_default().0 - } - .into() - ); - }); + let builder = UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20])); + assert_eq!( + builder.check().unwrap().0, + crate::Call::call:: { + dest: builder.tx.to.unwrap(), + value: builder.tx.value.unwrap_or_default().as_u64(), + gas_limit: builder.gas_limit, + storage_deposit_limit: builder.storage_deposit_limit, + data: builder.tx.input.unwrap_or_default().0 + } + .into() + ); } #[test] fn check_eth_transact_instantiate_works() { - ExtBuilder::default().build().execute_with(|| { - let (code, _) = compile_module("dummy").unwrap(); - let data = vec![]; - let builder = UncheckedExtrinsicBuilder::instantiate_with(code.clone(), data.clone()); - - assert_eq!( - builder.check().unwrap().0, - crate::Call::instantiate_with_code:: { - value: builder.tx.value.unwrap_or_default().as_u64(), - gas_limit: builder.gas_limit, - storage_deposit_limit: builder.storage_deposit_limit, - code, - data, - salt: None - } - .into() - ); - }); + let (code, _) = compile_module("dummy").unwrap(); + let data = vec![]; + let builder = UncheckedExtrinsicBuilder::instantiate_with(code.clone(), data.clone()); + + assert_eq!( + builder.check().unwrap().0, + crate::Call::instantiate_with_code:: { + value: builder.tx.value.unwrap_or_default().as_u64(), + gas_limit: builder.gas_limit, + storage_deposit_limit: builder.storage_deposit_limit, + code, + data, + salt: None + } + .into() + ); } #[test] fn check_eth_transact_nonce_works() { - ExtBuilder::default().build().execute_with(|| { - let builder = UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20])) - .update(|tx| tx.nonce = Some(1u32.into())); - - assert_eq!( - builder.check(), - Err(TransactionValidityError::Invalid(InvalidTransaction::Future)) - ); - - >::inc_account_nonce(Account::default().substrate_account()); - - let builder = UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20])); - assert_eq!( - builder.check(), - Err(TransactionValidityError::Invalid(InvalidTransaction::Stale)) - ); - }); + let builder = UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20])) + .update(|tx| tx.nonce = Some(1u32.into())); + + assert_eq!( + builder.check(), + Err(TransactionValidityError::Invalid(InvalidTransaction::Future)) + ); + + let builder = + UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20])).before_validate(|| { + >::inc_account_nonce(Account::default().substrate_account()); + }); + + assert_eq!( + builder.check(), + Err(TransactionValidityError::Invalid(InvalidTransaction::Stale)) + ); } #[test] fn check_eth_transact_chain_id_works() { - ExtBuilder::default().build().execute_with(|| { - let builder = UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20])) - .update(|tx| tx.chain_id = Some(42.into())); - - assert_eq!( - builder.check(), - Err(TransactionValidityError::Invalid(InvalidTransaction::Call)) - ); - }); + let builder = UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20])) + .update(|tx| tx.chain_id = Some(42.into())); + + assert_eq!( + builder.check(), + Err(TransactionValidityError::Invalid(InvalidTransaction::Call)) + ); } #[test] fn check_instantiate_data() { - ExtBuilder::default().build().execute_with(|| { - let code = b"invalid code".to_vec(); - let data = vec![1]; - let builder = UncheckedExtrinsicBuilder::instantiate_with(code.clone(), data.clone()); - - // Fail because the tx input fail to get the blob length - assert_eq!( - builder.clone().update(|tx| tx.input = Some(Bytes(vec![1, 2, 3]))).check(), - Err(TransactionValidityError::Invalid(InvalidTransaction::Call)) - ); - }); + let code = b"invalid code".to_vec(); + let data = vec![1]; + let builder = UncheckedExtrinsicBuilder::instantiate_with(code.clone(), data.clone()); + + // Fail because the tx input fail to get the blob length + assert_eq!( + builder.clone().update(|tx| tx.input = Some(Bytes(vec![1, 2, 3]))).check(), + Err(TransactionValidityError::Invalid(InvalidTransaction::Call)) + ); } #[test] fn check_transaction_fees() { - ExtBuilder::default().build().execute_with(|| { - let scenarios: [(_, Box, _); 5] = [ - ( - "Eth fees too low", - Box::new(|tx| { - tx.gas_price = Some(tx.gas_price.unwrap() / 2); - }), - InvalidTransaction::Payment, - ), - ( - "Gas fees too high", - Box::new(|tx| { - tx.gas = Some(tx.gas.unwrap() * 2); - }), - InvalidTransaction::Call, - ), - ( - "Gas fees too low", - Box::new(|tx| { - tx.gas = Some(tx.gas.unwrap() * 2); - }), - InvalidTransaction::Call, - ), - ( - "Diff > 10%", - Box::new(|tx| { - tx.gas = Some(tx.gas.unwrap() * 111 / 100); - }), - InvalidTransaction::Call, - ), - ( - "Diff < 10%", - Box::new(|tx| { - tx.gas_price = Some(tx.gas_price.unwrap() * 2); - tx.gas = Some(tx.gas.unwrap() * 89 / 100); - }), - InvalidTransaction::Call, - ), - ]; - - for (msg, update_tx, err) in scenarios { - let builder = - UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20])).update(update_tx); - - assert_eq!(builder.check(), Err(TransactionValidityError::Invalid(err)), "{}", msg); - } - }); + let scenarios: [(_, Box, _); 5] = [ + ( + "Eth fees too low", + Box::new(|tx| { + tx.gas_price = Some(tx.gas_price.unwrap() / 2); + }), + InvalidTransaction::Payment, + ), + ( + "Gas fees too high", + Box::new(|tx| { + tx.gas = Some(tx.gas.unwrap() * 2); + }), + InvalidTransaction::Call, + ), + ( + "Gas fees too low", + Box::new(|tx| { + tx.gas = Some(tx.gas.unwrap() * 2); + }), + InvalidTransaction::Call, + ), + ( + "Diff > 10%", + Box::new(|tx| { + tx.gas = Some(tx.gas.unwrap() * 111 / 100); + }), + InvalidTransaction::Call, + ), + ( + "Diff < 10%", + Box::new(|tx| { + tx.gas_price = Some(tx.gas_price.unwrap() * 2); + tx.gas = Some(tx.gas.unwrap() * 89 / 100); + }), + InvalidTransaction::Call, + ), + ]; + + for (msg, update_tx, err) in scenarios { + let builder = + UncheckedExtrinsicBuilder::call_with(H160::from([1u8; 20])).update(update_tx); + + assert_eq!(builder.check(), Err(TransactionValidityError::Invalid(err)), "{}", msg); + } } #[test] fn check_transaction_tip() { - let _ = env_logger::builder().is_test(true).try_init(); - ExtBuilder::default().build().execute_with(|| { - let (code, _) = compile_module("dummy").unwrap(); - let data = vec![]; - let builder = UncheckedExtrinsicBuilder::instantiate_with(code.clone(), data.clone()) - .update(|tx| { - tx.gas_price = Some(tx.gas_price.unwrap() * 103 / 100); - log::debug!(target: LOG_TARGET, "Gas price: {:?}", tx.gas_price); - }); + let (code, _) = compile_module("dummy").unwrap(); + let data = vec![]; + let builder = UncheckedExtrinsicBuilder::instantiate_with(code.clone(), data.clone()) + .update(|tx| { + tx.gas_price = Some(tx.gas_price.unwrap() * 103 / 100); + log::debug!(target: LOG_TARGET, "Gas price: {:?}", tx.gas_price); + }); - let tx = &builder.tx; - let expected_tip = - tx.gas_price.unwrap() * tx.gas.unwrap() - U256::from(GAS_PRICE) * tx.gas.unwrap(); - let (_, extra) = builder.check().unwrap(); - assert_eq!(U256::from(extra.1.tip()), expected_tip); - }); + let tx = &builder.tx; + let expected_tip = + tx.gas_price.unwrap() * tx.gas.unwrap() - U256::from(GAS_PRICE) * tx.gas.unwrap(); + let (_, extra) = builder.check().unwrap(); + assert_eq!(U256::from(extra.1.tip()), expected_tip); } }