Skip to content

[pallet-revive] Add chain ID to config an runtime API#5807

Merged
xermicus merged 9 commits into
masterfrom
cl/chain_id
Sep 23, 2024
Merged

[pallet-revive] Add chain ID to config an runtime API#5807
xermicus merged 9 commits into
masterfrom
cl/chain_id

Conversation

@xermicus

Copy link
Copy Markdown
Member

This PR adds the EVM chain ID to Config as well as a corresponding runtime API so contracts can query it.

Related issue: paritytech/revive#44

@xermicus xermicus added R0-no-crate-publish-required The change does not require any crates to be re-published. T7-smart_contracts This PR/Issue is related to smart contracts. labels Sep 23, 2024
@xermicus xermicus requested review from athei and pgherveou September 23, 2024 09:39
@xermicus

Copy link
Copy Markdown
Member Author

bot fmt

@command-bot

command-bot Bot commented Sep 23, 2024

Copy link
Copy Markdown

@xermicus https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/7408786 was started for your command "$PIPELINE_SCRIPTS_DIR/commands/fmt/fmt.sh". Check out https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/pipelines?page=1&scope=all&username=group_605_bot to know what else is being executed currently.

Comment bot cancel 1-24908ac9-45b8-4123-a584-f2f8f559f943 to cancel this command or bot cancel to cancel all commands in this pull request.

@command-bot

command-bot Bot commented Sep 23, 2024

Copy link
Copy Markdown

@xermicus Command "$PIPELINE_SCRIPTS_DIR/commands/fmt/fmt.sh" has finished. Result: https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/7408786 has finished. If any artifacts were generated, you can download them from https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/7408786/artifacts/download.

Signed-off-by: xermicus <cyrill@parity.io>
Signed-off-by: xermicus <cyrill@parity.io>
/// This is a unique identifier assigned to each blockchain network,
/// preventing replay attacks.
#[pallet::constant]
type ChainId: Get<u64>;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not U256 if that is the native format of the ID?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

People seem to generally pick them within the bounds of 64bits. I guess they want to have easily readable for humans? Also there is no ConstU256 yet. But it this be fine, I hope humanity never needs more than 2**64 blockchains :P

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I have the same patch in my branch I used u32, there..., both are fine I guess

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok let's stick with u64. As long as we don't need to convert back to u64 it's fine.

@xermicus xermicus Sep 23, 2024

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the RPC side I'm not sure but for the runtime I don't see where we would need to parse this ever.

@xermicus xermicus Sep 23, 2024

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However even if so, I feel like this would need to be a fallible conversion anyways (you can parse any U256 but if you want it to match against the configured chain ID, if the U256 is > u64::MAX you know that the chain ID would not match anyways).

type Xcm = ();
type RuntimeMemory = ConstU32<{ 128 * 1024 * 1024 }>;
type PVFMemory = ConstU32<{ 512 * 1024 * 1024 }>;
type ChainId = ConstU64<{ 0 }>;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mind setting it to 42 here, I need to reconfigure my Metamask extension otherwise 🙃

@athei athei Sep 23, 2024

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by extension? Shouldn't Metamask just use an RPC to query to chain id?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this ChainId is not really used. It is just the default derive that can be used for tests. But it is overriden in tests.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you configure a local network with metamask, you need to specify the chain_id, you can update it later on, but from what I experienced there is a bug and it still use the old value, so you delete your custom network and recreate it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

42 is already taken and I thought this should either be zero or something that's not taken yet? (Something that's not taken yet could however get taken later on). WDYT?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good then, I just need to remember to change this once I rebase on top of this

Comment on lines +1545 to +1549
/// Returns the chain ID.
/// See [`pallet_revive_uapi::HostFn::chain_id`].
#[api_version(0)]
fn chain_id(&mut self, memory: &mut M, out_ptr: u32) -> Result<(), TrapReason> {
Ok(self.write_fixed_sandbox_output(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't charge any gas except the syscall overhead. Needs a benchmark.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes good catch. Since this doesn't really do anything except writing a constant to contract memory and the syscall overhead is apparently already charged, would the RuntimeCosts::CopyToContract(len) do it instead of a dedicated benchmark?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this should be fine. However, we need to remember to make our seal_input benchmark better (where this weight is based in). Right now it just does a memory copy. Which is probably fair with in-runtime PolkaVM but with a JIT there will be some constant overhead for a copy.

There are probably also many other "getters" which don't access storage that could be refactored this way.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah right I see. Just added the gas token for now, thanks!

Signed-off-by: xermicus <cyrill@parity.io>
Signed-off-by: xermicus <cyrill@parity.io>
@xermicus xermicus requested a review from athei September 23, 2024 12:57
@xermicus xermicus enabled auto-merge September 23, 2024 13:46
@xermicus

Copy link
Copy Markdown
Member Author

bot fmt

@command-bot

command-bot Bot commented Sep 23, 2024

Copy link
Copy Markdown

@xermicus https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/7411720 was started for your command "$PIPELINE_SCRIPTS_DIR/commands/fmt/fmt.sh". Check out https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/pipelines?page=1&scope=all&username=group_605_bot to know what else is being executed currently.

Comment bot cancel 3-0bf5f027-c0fd-4775-b58d-82848faa6d5b to cancel this command or bot cancel to cancel all commands in this pull request.

@command-bot

command-bot Bot commented Sep 23, 2024

Copy link
Copy Markdown

@xermicus Command "$PIPELINE_SCRIPTS_DIR/commands/fmt/fmt.sh" has finished. Result: https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/7411720 has finished. If any artifacts were generated, you can download them from https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/7411720/artifacts/download.

@xermicus xermicus added this pull request to the merge queue Sep 23, 2024
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 23, 2024
@xermicus xermicus enabled auto-merge September 23, 2024 16:50
@xermicus xermicus added this pull request to the merge queue Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

R0-no-crate-publish-required The change does not require any crates to be re-published. T7-smart_contracts This PR/Issue is related to smart contracts.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants