Skip to content

Commit

Permalink
Replace net_version with eth_chainId (#967)
Browse files Browse the repository at this point in the history
Fixes #966

* replaces `net_version` calls with `eth_chainId` because that is
supported by all nodes by default whereas the `net` functions might not
be.
* bumps the version so we can immediately start using this improvement
in the services repo
* gets rid of unnecessary imports to avoid clippy warnings on nightly

### Test Plan
Adjusted existing tests
  • Loading branch information
MartinquaXD authored Feb 22, 2024
1 parent bbce7c8 commit db4a20f
Show file tree
Hide file tree
Showing 21 changed files with 26 additions and 72 deletions.
2 changes: 1 addition & 1 deletion ethcontract-common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ethcontract-common"
version = "0.25.4"
version = "0.25.5"
authors = ["Gnosis developers <developers@gnosis.io>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand Down
6 changes: 3 additions & 3 deletions ethcontract-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ethcontract-derive"
version = "0.25.4"
version = "0.25.5"
authors = ["Gnosis developers <developers@gnosis.io>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -20,8 +20,8 @@ proc-macro = true

[dependencies]
anyhow = "1.0"
ethcontract-common = { version = "0.25.4", path = "../ethcontract-common" }
ethcontract-generate = { version = "0.25.4", path = "../ethcontract-generate", default-features = false }
ethcontract-common = { version = "0.25.5", path = "../ethcontract-common" }
ethcontract-generate = { version = "0.25.5", path = "../ethcontract-generate", default-features = false }
proc-macro2 = "1.0"
quote = "1.0"
syn = "2.0"
4 changes: 2 additions & 2 deletions ethcontract-generate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ethcontract-generate"
version = "0.25.4"
version = "0.25.5"
authors = ["Gnosis developers <developers@gnosis.io>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -18,7 +18,7 @@ http = ["curl"]
[dependencies]
anyhow = "1.0"
curl = { version = "0.4", optional = true }
ethcontract-common = { version = "0.25.4", path = "../ethcontract-common" }
ethcontract-common = { version = "0.25.5", path = "../ethcontract-common" }
Inflector = "0.11"
proc-macro2 = "1.0"
quote = "1.0"
Expand Down
1 change: 0 additions & 1 deletion ethcontract-generate/src/generate/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,6 @@ fn expand_invalid_data() -> TokenStream {
mod tests {
use super::*;
use crate::ContractBuilder;
use ethcontract_common::abi::{EventParam, ParamType};
use ethcontract_common::Contract;

#[test]
Expand Down
6 changes: 3 additions & 3 deletions ethcontract-mock/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ethcontract-mock"
version = "0.25.4"
version = "0.25.5"
authors = ["Gnosis developers <developers@gnosis.io>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -12,12 +12,12 @@ Tools for mocking ethereum contracts.
"""

[dependencies]
ethcontract = { version = "0.25.4", path = "../ethcontract", default-features = false, features = ["derive"] }
ethcontract = { version = "0.25.5", path = "../ethcontract", default-features = false, features = ["derive"] }
hex = "0.4"
mockall = "0.11"
rlp = "0.5"
predicates = "3.0"

[dev-dependencies]
tokio = { version = "1.6", features = ["macros", "rt"] }
ethcontract-derive = { version = "0.25.4", path = "../ethcontract-derive", default-features = false }
ethcontract-derive = { version = "0.25.5", path = "../ethcontract-derive", default-features = false }
12 changes: 0 additions & 12 deletions ethcontract-mock/src/details/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Implementation details of mock node.

use std::collections::HashMap;
use std::convert::TryFrom;
use std::future::ready;
use std::sync::{Arc, Mutex};

Expand Down Expand Up @@ -449,10 +448,6 @@ impl MockTransport {
let name = "eth_getTransactionReceipt";
self.eth_get_transaction_receipt(Parser::new(name, params))
}
"net_version" => {
let name = "net_version";
self.net_version(Parser::new(name, params))
}
unsupported => panic!("mock node does not support rpc method {:?}", unsupported),
};

Expand Down Expand Up @@ -679,13 +674,6 @@ impl MockTransport {
}))
}

fn net_version(&self, args: Parser) -> Result<Value, Error> {
args.done();

let state = self.state.lock().unwrap();
Self::ok(state.chain_id.to_string())
}

fn ok<T: Serialize>(t: T) -> Result<Value, Error> {
Ok(to_value(t).unwrap())
}
Expand Down
1 change: 0 additions & 1 deletion ethcontract-mock/src/test/eth_block_number.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::*;
use crate::Mock;

#[tokio::test]
async fn block_number_initially_zero() -> Result {
Expand Down
1 change: 0 additions & 1 deletion ethcontract-mock/src/test/eth_chain_id.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::*;
use crate::Mock;

#[tokio::test]
async fn chain_id() -> Result {
Expand Down
1 change: 0 additions & 1 deletion ethcontract-mock/src/test/eth_gas_price.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::*;
use crate::Mock;

#[tokio::test]
async fn gas_price() -> Result {
Expand Down
2 changes: 0 additions & 2 deletions ethcontract-mock/src/test/eth_transaction_count.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use super::*;
use crate::Mock;
use ethcontract::BlockNumber;

#[tokio::test]
async fn transaction_count_initially_zero() -> Result {
Expand Down
19 changes: 0 additions & 19 deletions ethcontract-mock/src/test/net_version.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::*;
use crate::Mock;

#[tokio::test]
async fn chain_id() -> Result {
Expand All @@ -9,21 +8,3 @@ async fn chain_id() -> Result {

Ok(())
}

#[tokio::test]
async fn net_version() -> Result {
let web3 = Mock::new(1234).web3();

assert_eq!(web3.net().version().await?, "1234");

Ok(())
}

#[tokio::test]
async fn net_version_main() -> Result {
let web3 = Mock::new(1).web3(); // simulate mainnet

assert_eq!(web3.net().version().await?, "1");

Ok(())
}
6 changes: 3 additions & 3 deletions ethcontract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ethcontract"
version = "0.25.4"
version = "0.25.5"
authors = ["Gnosis developers <developers@gnosis.io>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand Down Expand Up @@ -35,8 +35,8 @@ ws-tokio = ["web3/ws-tokio"]
aws-config = { version = "0.55", optional = true }
aws-sdk-kms = { version = "0.28", optional = true }
arrayvec = "0.7"
ethcontract-common = { version = "0.25.4", path = "../ethcontract-common" }
ethcontract-derive = { version = "0.25.4", path = "../ethcontract-derive", optional = true, default-features = false }
ethcontract-common = { version = "0.25.5", path = "../ethcontract-common" }
ethcontract-derive = { version = "0.25.5", path = "../ethcontract-derive", optional = true, default-features = false }
futures = "0.3"
futures-timer = "3.0"
hex = "0.4"
Expand Down
22 changes: 8 additions & 14 deletions ethcontract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl<T: Transport> Instance<T> {
/// Note that this does not verify that a contract with a matching `Abi` is
/// actually deployed at the given address.
pub async fn deployed(web3: Web3<T>, contract: Contract) -> Result<Self, DeployError> {
let network_id = web3.net().version().await?;
let network_id = web3.eth().chain_id().await?.to_string();
let network = contract
.networks
.get(&network_id)
Expand Down Expand Up @@ -389,20 +389,17 @@ mod tests {
use super::*;
use crate::test::prelude::*;
use ethcontract_common::contract::Network;
use ethcontract_common::Contract;
use web3::types::H256;

#[test]
fn deployed() {
let mut transport = TestTransport::new();
let web3 = Web3::new(transport.clone());

let network_id = "42";
let address = addr!("0x0102030405060708091011121314151617181920");
let contract = {
let mut contract = Contract::empty();
contract.networks.insert(
network_id.to_string(),
"42".to_string(),
Network {
address,
deployment_information: Some(H256::repeat_byte(0x42).into()),
Expand All @@ -411,12 +408,12 @@ mod tests {
contract
};

transport.add_response(json!(network_id)); // get network ID response
transport.add_response(json!("0x2a")); // eth_chainId response
let instance = Instance::deployed(web3, contract)
.immediate()
.expect("successful deployment");

transport.assert_request("net_version", &[]);
transport.assert_request("eth_chainId", &[]);
transport.assert_no_more_requests();

assert_eq!(instance.address(), address);
Expand All @@ -433,23 +430,20 @@ mod tests {
let mut transport = TestTransport::new();
let web3 = Web3::new(transport.clone());

let network_id = "42";

transport.add_response(json!(network_id)); // get network ID response
transport.add_response(json!("0x2a")); // eth_chainId response
let err = Instance::deployed(web3, Contract::empty())
.immediate()
.expect_err("unexpected success getting deployed contract");

transport.assert_request("net_version", &[]);
transport.assert_request("eth_chainId", &[]);
transport.assert_no_more_requests();

assert!(
match &err {
DeployError::NotFound(id) => id == network_id,
DeployError::NotFound(id) => id == "42",
_ => false,
},
"expected network {} not found error but got '{:?}'",
network_id,
"expected network 42 not found error but got '{:?}'",
err
);
}
Expand Down
2 changes: 1 addition & 1 deletion ethcontract/src/contract/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ mod tests {
use super::*;
use crate::contract::{Instance, Linker};
use crate::test::prelude::*;
use ethcontract_common::{Bytecode, Contract};
use ethcontract_common::Contract;

type InstanceDeployBuilder<T> = DeployBuilder<T, Instance<T>>;

Expand Down
2 changes: 1 addition & 1 deletion ethcontract/src/contract/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ mod tests {
use ethcontract_common::abi::{EventParam, ParamType};
use futures::stream::StreamExt;
use serde_json::Value;
use web3::types::{Address, H2048, H256, U256, U64};
use web3::types::{H2048, U256, U64};

fn test_abi_event() -> (AbiEvent, Value) {
let event = AbiEvent {
Expand Down
1 change: 0 additions & 1 deletion ethcontract/src/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use crate::errors::{ParseI256Error, TryFromBigIntError};
use serde::{Deserialize, Serialize};
use std::cmp;
use std::convert::{TryFrom, TryInto};
use std::fmt;
use std::iter;
use std::ops;
Expand Down
1 change: 0 additions & 1 deletion ethcontract/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::I256;
use arrayvec::ArrayVec;
use ethcontract_common::{abi::Token, TransactionHash};
use serde::{Deserialize, Serialize};
use std::convert::TryInto;
use web3::types::{Address, U256};

/// A tokenization related error.
Expand Down
1 change: 0 additions & 1 deletion ethcontract/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ impl<T: Transport> TransactionBuilder<T> {
#[cfg(test)]
mod tests {
use super::*;
use crate::errors::ExecutionError;
use crate::test::prelude::*;
use hex_literal::hex;
use web3::types::{AccessListItem, H2048, H256};
Expand Down
4 changes: 2 additions & 2 deletions examples/examples/deployments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ async fn main() {
let web3 = Web3::new(http);

let network_id = web3
.net()
.version()
.eth()
.chain_id()
.await
.expect("failed to get network ID");
let instance = RustCoin::deployed(&web3)
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/kms.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ethcontract::{
aws_config,
prelude::*,
transaction::{kms, Account, TransactionBuilder},
transaction::{kms, TransactionBuilder},
};
use std::env;

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/revert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async fn main() {
let error = result_0.unwrap_err().inner;
assert!(matches!(
error,
ExecutionError::Revert(Some(reason)) if reason == "reason"
ExecutionError::Revert(Some(reason)) if reason == "revert: reason"
));

let error = result_1.unwrap_err().inner;
Expand Down

0 comments on commit db4a20f

Please sign in to comment.