Skip to content

Commit

Permalink
[pallet-revive] adjust fee dry-run calculation (#6393)
Browse files Browse the repository at this point in the history
- Fix bare_eth_transact so that it estimate more precisely the
transaction fee
- Add some context to the build.rs to make it easier to troubleshoot
errors
- Add TransactionBuilder for the RPC tests.
- Improve error message, proxy rpc error from the node and handle
reverted error message
- Add logs in ReceiptInfo

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
pgherveou and actions-user authored Nov 12, 2024
1 parent aff3a07 commit 9f8656b
Show file tree
Hide file tree
Showing 40 changed files with 9,996 additions and 4,582 deletions.
12,537 changes: 8,502 additions & 4,035 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,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" }
Expand Down Expand Up @@ -1316,8 +1316,8 @@ 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" }
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" }
Expand Down Expand Up @@ -1387,7 +1387,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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,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_004,
spec_version: 1_016_005,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 16,
Expand Down
16 changes: 16 additions & 0 deletions prdoc/pr_6393.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
title: '[pallet-revive] adjust fee dry-run calculation'
doc:
- audience: Runtime Dev
description: |-
- Fix bare_eth_transact so that it estimate more precisely the transaction fee
- Add some context to the build.rs to make it easier to troubleshoot errors
- Add TransactionBuilder for the RPC tests.
- Tweaked some error message, We will need to wait for the next subxt release to properly downcast some errors and
adopt MM error code (https://eips.ethereum.org/EIPS/eip-1474#error-codes)
crates:
- name: pallet-revive-eth-rpc
bump: minor
- name: pallet-revive
bump: minor
- name: pallet-revive-fixtures
bump: minor
2 changes: 1 addition & 1 deletion substrate/bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ fn props() -> Properties {
fn eth_account(from: subxt_signer::eth::Keypair) -> AccountId32 {
let mut account_id = AccountId32::new([0xEE; 32]);
<AccountId32 as AsMut<[u8; 32]>>::as_mut(&mut account_id)[..20]
.copy_from_slice(&from.account_id().0);
.copy_from_slice(&from.public_key().to_account_id().as_ref());
account_id
}

Expand Down
15 changes: 10 additions & 5 deletions substrate/frame/revive/fixtures/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ fn create_cargo_toml<'a>(
);

let cargo_toml = toml::to_string_pretty(&cargo_toml)?;
fs::write(output_dir.join("Cargo.toml"), cargo_toml).map_err(Into::into)
fs::write(output_dir.join("Cargo.toml"), cargo_toml.clone())
.with_context(|| format!("Failed to write {cargo_toml:?}"))?;
Ok(())
}

fn invoke_build(target: &Path, current_dir: &Path) -> Result<()> {
Expand Down Expand Up @@ -154,10 +156,11 @@ fn post_process(input_path: &Path, output_path: &Path) -> Result<()> {
let mut config = polkavm_linker::Config::default();
config.set_strip(strip);
config.set_optimize(optimize);
let orig = fs::read(input_path).with_context(|| format!("Failed to read {:?}", input_path))?;
let orig = fs::read(input_path).with_context(|| format!("Failed to read {input_path:?}"))?;
let linked = polkavm_linker::program_from_elf(config, orig.as_ref())
.map_err(|err| anyhow::format_err!("Failed to link polkavm program: {}", err))?;
fs::write(output_path, linked).map_err(Into::into)
fs::write(output_path, linked).with_context(|| format!("Failed to write {output_path:?}"))?;
Ok(())
}

/// Write the compiled contracts to the given output directory.
Expand Down Expand Up @@ -209,9 +212,11 @@ pub fn main() -> Result<()> {
let symlink_dir: PathBuf = symlink_dir.into();
let symlink_dir: PathBuf = symlink_dir.join("target").join("pallet-revive-fixtures");
if symlink_dir.is_symlink() {
fs::remove_file(&symlink_dir)?
fs::remove_file(&symlink_dir)
.with_context(|| format!("Failed to remove_file {symlink_dir:?}"))?;
}
std::os::unix::fs::symlink(&out_dir, &symlink_dir)?;
std::os::unix::fs::symlink(&out_dir, &symlink_dir)
.with_context(|| format!("Failed to symlink {out_dir:?} -> {symlink_dir:?}"))?;
}

Ok(())
Expand Down
8 changes: 7 additions & 1 deletion substrate/frame/revive/fixtures/contracts/rpc_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#![no_std]
#![no_main]

use common::input;
use common::{input, u64_output};
use uapi::{HostFn, HostFnImpl as api};

#[no_mangle]
Expand All @@ -31,6 +31,12 @@ pub extern "C" fn deploy() {
#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn call() {
// Not payable
let value = u64_output!(api::value_transferred,);
if value > 0 {
panic!();
}

input!(128, data: [u8],);
api::deposit_event(&[], data);
}
9 changes: 4 additions & 5 deletions substrate/frame/revive/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ jsonrpsee = { workspace = true, features = ["full"] }
serde_json = { workspace = true }
thiserror = { workspace = true }
sp-crypto-hashing = { workspace = true }
subxt = { workspace = true, default-features = true, features = [
"unstable-reconnecting-rpc-client",
] }
subxt = { workspace = true, default-features = true, features = ["reconnecting-rpc-client"] }
tokio = { workspace = true, features = ["full"] }
codec = { workspace = true, features = ["derive"] }
log.workspace = true
Expand All @@ -65,14 +63,15 @@ rlp = { workspace = true, optional = true }
subxt-signer = { workspace = true, optional = true, features = [
"unstable-eth",
] }
hex = { workspace = true, optional = true }
hex = { workspace = true }
hex-literal = { workspace = true, optional = true }
scale-info = { workspace = true }
secp256k1 = { workspace = true, optional = true, features = ["recovery"] }
env_logger = { workspace = true }
ethabi = { version = "18.0.0" }

[features]
example = ["hex", "hex-literal", "rlp", "secp256k1", "subxt-signer"]
example = ["hex-literal", "rlp", "secp256k1", "subxt-signer"]

[dev-dependencies]
hex-literal = { workspace = true }
Expand Down
34 changes: 3 additions & 31 deletions substrate/frame/revive/rpc/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ zombienet spawn --provider native westend_local_network.toml
This command starts the Ethereum JSON-RPC server, which runs on `localhost:8545` by default:

```bash
RUST_LOG="info,eth-rpc=debug" cargo run -p pallet-revive-eth-rpc --features dev
RUST_LOG="info,eth-rpc=debug" cargo run -p pallet-revive-eth-rpc -- --dev
```

## Rust examples
Expand Down Expand Up @@ -65,34 +65,6 @@ bun src/script.ts

### Configure MetaMask

You can use the following instructions to setup [MetaMask] with the local chain.
See the doc [here](https://contracts.polkadot.io/work-with-a-local-node#metemask-configuration) for more
information on how to configure MetaMask.

> **Note**: When you interact with MetaMask and restart the chain, you need to clear the activity tab (Settings >
Advanced > Clear activity tab data), and in some cases lock/unlock MetaMask to reset the nonce.
See [this guide][reset-account] for more info on how to reset the account activity.

#### Add a new network

To interact with the local chain, add a new network in [MetaMask].
See [this guide][add-network] for more info on how to add a custom network.

Make sure the node and the RPC server are started, and use the following settings to configure the network
(MetaMask > Networks > Add a network manually):

- Network name: KitchenSink
- RPC URL: <http://localhost:8545>
- Chain ID: 420420420
- Currency Symbol: `DEV`

#### Import Dev account

You will need to import the following account, endowed with some balance at genesis, to interact with the chain.
See [this guide][import-account] for more info on how to import an account.

- Account: `0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac`
- Private Key: `5fb92d6e98884f76de468fa3f6278f8807c48bebc13595d45af5bdc4da702133`

[MetaMask]: https://metamask.io
[add-network]: https://support.metamask.io/networks-and-sidechains/managing-networks/how-to-add-a-custom-network-rpc/#adding-a-network-manually
[import-account]: https://support.metamask.io/managing-my-wallet/accounts-and-addresses/how-to-import-an-account/
[reset-account]: https://support.metamask.io/managing-my-wallet/resetting-deleting-and-restoring/how-to-clear-your-account-activity-reset-account
Binary file added substrate/frame/revive/rpc/examples/bun.lockb
Binary file not shown.
6 changes: 6 additions & 0 deletions substrate/frame/revive/rpc/examples/js/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 4,
"semi": false,
"singleQuote": true
}
13 changes: 13 additions & 0 deletions substrate/frame/revive/rpc/examples/js/contracts/Event.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract EventExample {
event ExampleEvent(address indexed sender, uint256 value, string message);

function triggerEvent() public {
uint256 value = 12345;
string memory message = "Hello world";
emit ExampleEvent(msg.sender, value, message);
}
}

11 changes: 11 additions & 0 deletions substrate/frame/revive/rpc/examples/js/contracts/Revert.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract RevertExample {
constructor() {
}

function doRevert() public {
revert("revert message");
}
}
56 changes: 56 additions & 0 deletions substrate/frame/revive/rpc/examples/js/evm-contracts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"event": {
"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"
}
],
"bytecode": "6080604052348015600e575f5ffd5b506101b68061001c5f395ff3fe608060405234801561000f575f5ffd5b5060043610610029575f3560e01c8063ede48fb71461002d575b5f5ffd5b610035610037565b005b5f61303990505f6040518060400160405280600b81526020017f48656c6c6f20776f726c6400000000000000000000000000000000000000000081525090503373ffffffffffffffffffffffffffffffffffffffff167f1585375487296ff2f0370daeec4214074a032b31af827c12622fa9a58c16c7d083836040516100be929190610152565b60405180910390a25050565b5f819050919050565b6100dc816100ca565b82525050565b5f81519050919050565b5f82825260208201905092915050565b8281835e5f83830152505050565b5f601f19601f8301169050919050565b5f610124826100e2565b61012e81856100ec565b935061013e8185602086016100fc565b6101478161010a565b840191505092915050565b5f6040820190506101655f8301856100d3565b8181036020830152610177818461011a565b9050939250505056fea2646970667358221220a159f2cdba512e018377f5822cfd8ef04769755d98c3e494275605d96d7d13e864736f6c634300081c0033"
},
"revert": {
"abi": [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "doRevert",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
],
"bytecode": "6080604052348015600e575f5ffd5b506101138061001c5f395ff3fe6080604052348015600e575f5ffd5b50600436106026575f3560e01c8063afc874d214602a575b5f5ffd5b60306032565b005b6040517f08c379a000000000000000000000000000000000000000000000000000000000815260040160629060c1565b60405180910390fd5b5f82825260208201905092915050565b7f726576657274206d6573736167650000000000000000000000000000000000005f82015250565b5f60ad600e83606b565b915060b682607b565b602082019050919050565b5f6020820190508181035f83015260d68160a3565b905091905056fea264697066735822122084f7096e030faf779d12b20184187708d335e4bcd4208a52c0651700fe8dde6a64736f6c634300081c0033"
}
}
2 changes: 1 addition & 1 deletion substrate/frame/revive/rpc/examples/js/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
<button id="callButton">Call Contract</button>

<button id="deployAndCallButton">Deploy and Call Contract</button>
<script type="module" src="src/main.ts"></script>
<script type="module" src="src/web.ts"></script>
</body>
</html>
Loading

0 comments on commit 9f8656b

Please sign in to comment.