Skip to content

Commit

Permalink
implement web3_clientVersion (paritytech#7580)
Browse files Browse the repository at this point in the history
Implements the `web3_clientVersion` method. This is a common requirement
for external Ethereum libraries when querying a client.

Fixes paritytech/contract-issues#26.

---------

Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and clangenb committed Feb 19, 2025
1 parent 02071a5 commit 067a770
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 4 deletions.
34 changes: 30 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions prdoc/pr_7580.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
title: implement web3_clientVersion
doc:
- audience: Runtime Dev
description: |-
Implements the `web3_clientVersion` method. This is a common requirement for external Ethereum libraries when querying a client.

Reference issue with more details: https://github.com/paritytech/contract-issues/issues/26.
crates:
- name: pallet-revive-eth-rpc
bump: minor
3 changes: 3 additions & 0 deletions substrate/frame/revive/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,6 @@ pretty_assertions = { workspace = true }
static_init = { workspace = true }
substrate-cli-test-utils = { workspace = true }
subxt-signer = { workspace = true, features = ["unstable-eth"] }

[build-dependencies]
git2 = { version = "0.20.0", default-features = false }
44 changes: 44 additions & 0 deletions substrate/frame/revive/rpc/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 std::process::Command;

/// Get the current branch and commit hash.
fn main() {
let output = Command::new("rustc")
.arg("--version")
.output()
.expect("cannot get the current rustc version");
// Exports the default rustc --version output:
// e.g. rustc 1.83.0 (90b35a623 2024-11-26)
// into the usual Ethereum web3_clientVersion format
// e.g. rustc1.83.0
let rustc_version = String::from_utf8_lossy(&output.stdout)
.split_whitespace()
.take(2)
.collect::<Vec<_>>()
.join("");
let target = std::env::var("TARGET").unwrap_or_else(|_| "unknown".to_string());

let repo = git2::Repository::open("../../../..").expect("should be a repository");
let head = repo.head().expect("should have head");
let commit = head.peel_to_commit().expect("should have commit");
let branch = head.shorthand().unwrap_or("unknown").to_string();
let id = &commit.id().to_string()[..7];
println!("cargo:rustc-env=GIT_REVISION={branch}-{id}");
println!("cargo:rustc-env=RUSTC_VERSION={rustc_version}");
println!("cargo:rustc-env=TARGET={target}");
}
4 changes: 4 additions & 0 deletions substrate/frame/revive/rpc/src/apis/execution_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,8 @@ pub trait EthRpc {
/// The string value of current network id
#[method(name = "net_version")]
async fn net_version(&self) -> RpcResult<String>;

/// The string value of the current client version
#[method(name = "web3_clientVersion")]
async fn web3_client_version(&self) -> RpcResult<String>;
}
7 changes: 7 additions & 0 deletions substrate/frame/revive/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,11 @@ impl EthRpcServer for EthRpcServerImpl {
let nonce = self.client.nonce(address, block).await?;
Ok(nonce)
}

async fn web3_client_version(&self) -> RpcResult<String> {
let git_revision = env!("GIT_REVISION");
let rustc_version = env!("RUSTC_VERSION");
let target = env!("TARGET");
Ok(format!("eth-rpc/{git_revision}/{target}/{rustc_version}"))
}
}

0 comments on commit 067a770

Please sign in to comment.