Skip to content

Commit

Permalink
feat: Implement web3_clientVersion (#223)
Browse files Browse the repository at this point in the history
* feat: Implement web3_clientVersion

* lint fix

---------

Co-authored-by: Dustin Brickwood <[email protected]>
  • Loading branch information
MexicanAce and dutterbutter authored Nov 27, 2023
1 parent 134805d commit 7f68fc4
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 12 deletions.
30 changes: 30 additions & 0 deletions SUPPORTED_APIS.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ The `status` options are:
| [`NETWORK`](#network-namespace) | [`net_version`](#net_version) | `SUPPORTED` | Returns the current network id <br />_(default is `260`)_ |
| [`NETWORK`](#network-namespace) | [`net_peerCount`](#net_peercount) | `SUPPORTED` | Returns the number of peers currently connected to the client <br/>_(hard-coded to `0`)_ |
| [`NETWORK`](#network-namespace) | [`net_listening`](#net_listening) | `SUPPORTED` | Returns `true` if the client is actively listening for network connections <br />_(hard-coded to `false`)_ |
| [`WEB3`](#web3-namespace) | [`web3_clientVersion`](#web3_clientversion) | `SUPPORTED` | Returns `zkSync/v2.0` |
| [`ZKS`](#zks-namespace) | [`zks_estimateFee`](#zks_estimateFee) | `SUPPORTED` | Gets the Fee estimation data for a given Request |
| `ZKS` | `zks_estimateGasL1ToL2` | `NOT IMPLEMENTED` | Estimate of the gas required for a L1 to L2 transaction |
| [`ZKS`](#zks-namespace) | [`zks_getAllAccountBalances`](#zks_getallaccountbalances) | `SUPPORTED` | Returns all balances for confirmed tokens given by an account address |
Expand Down Expand Up @@ -1708,6 +1709,35 @@ curl --request POST \
--data '{"jsonrpc": "2.0","id": "1","method": "evm_revert","params": ["0x1"]}'
```

## `WEB3 NAMESPACE`

### `web3_clientVersion`

[source](src/node/web3.rs)

Returns the client version

#### Arguments

+ _NONE_

#### Status

`SUPPORTED`

#### Example

```bash
curl --request POST \
--url http://localhost:8011/ \
--header 'content-type: application/json' \
--data '{
"jsonrpc": "2.0",
"id": "1",
"method": "web3_clientVersion"
}'
```

## `ZKS NAMESPACE`

### `zks_estimateFee`
Expand Down
7 changes: 3 additions & 4 deletions e2e-tests/test/evm-apis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,12 @@ describe("evm_snapshot", function () {
expect(await greeter.greet()).to.eq("Hi");

// Act
const snapshotId1 = await provider.send("evm_snapshot", []);
const snapshotId2 = await provider.send("evm_snapshot", []);
const snapshotId1: string = await provider.send("evm_snapshot", []);
const snapshotId2: string = await provider.send("evm_snapshot", []);

// Assert
expect(await greeter.greet()).to.eq("Hi");
expect(BigNumber.from(snapshotId1).toString()).to.eq("1");
expect(BigNumber.from(snapshotId2).toString()).to.eq("2");
expect(BigNumber.from(snapshotId2).toString()).to.eq(BigNumber.from(snapshotId1).add(1).toString());
});
});

Expand Down
17 changes: 17 additions & 0 deletions e2e-tests/test/web3-apis.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect } from "chai";
import { getTestProvider } from "../helpers/utils";

const provider = getTestProvider();

describe("web3_clientVersion", function () {
it("Should return zkSync/v2.0", async function () {
// Arrange
const expectedClientVersion = "zkSync/v2.0";

// Act
const response: string = await provider.send("web3_clientVersion", []);

// Assert
expect(response).to.equal(expectedClientVersion);
});
});
4 changes: 4 additions & 0 deletions src/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ pub struct ForkDetails<S> {
}

const SUPPORTED_VERSIONS: &[ProtocolVersionId] = &[
ProtocolVersionId::Version9,
ProtocolVersionId::Version10,
ProtocolVersionId::Version11,
ProtocolVersionId::Version12,
ProtocolVersionId::Version13,
ProtocolVersionId::Version14,
ProtocolVersionId::Version15,
Expand Down
4 changes: 2 additions & 2 deletions src/http_fork_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,8 +700,8 @@ mod tests {
"result": {
"l1Erc20DefaultBridge": format!("{:#x}", input_bridge_addresses.l1_erc20_default_bridge),
"l2Erc20DefaultBridge": format!("{:#x}", input_bridge_addresses.l2_erc20_default_bridge),
"l1WethBridge": format!("{:#x}", input_bridge_addresses.l1_weth_bridge.clone().unwrap()),
"l2WethBridge": format!("{:#x}", input_bridge_addresses.l2_weth_bridge.clone().unwrap())
"l1WethBridge": format!("{:#x}", input_bridge_addresses.l1_weth_bridge.unwrap()),
"l2WethBridge": format!("{:#x}", input_bridge_addresses.l2_weth_bridge.unwrap())
},
"id": 0
}),
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use zksync_basic_types::{H160, H256};

use crate::namespaces::{
ConfigurationApiNamespaceT, DebugNamespaceT, EthNamespaceT, EvmNamespaceT, HardhatNamespaceT,
NetNamespaceT, ZksNamespaceT,
NetNamespaceT, Web3NamespaceT, ZksNamespaceT,
};

/// List of wallets (address, private key) that we seed with tokens at start.
Expand Down Expand Up @@ -105,6 +105,7 @@ async fn build_json_http<
let mut io = MetaIoHandler::with_middleware(LoggingMiddleware::new(log_level_filter));

io.extend_with(NetNamespaceT::to_delegate(node.clone()));
io.extend_with(Web3NamespaceT::to_delegate(node.clone()));
io.extend_with(ConfigurationApiNamespaceT::to_delegate(node.clone()));
io.extend_with(DebugNamespaceT::to_delegate(node.clone()));
io.extend_with(EthNamespaceT::to_delegate(node.clone()));
Expand Down
2 changes: 2 additions & 0 deletions src/namespaces/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod config;
mod evm;
mod hardhat;
mod net;
mod web3;

use zksync_core::api_server::web3::backend_jsonrpc::namespaces::{debug, eth, zks};

Expand All @@ -11,6 +12,7 @@ pub use eth::EthNamespaceT;
pub use evm::EvmNamespaceT;
pub use hardhat::HardhatNamespaceT;
pub use net::NetNamespaceT;
pub use web3::Web3NamespaceT;
pub use zks::ZksNamespaceT;

pub type Result<T> = jsonrpc_core::Result<T>;
Expand Down
9 changes: 9 additions & 0 deletions src/namespaces/web3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use jsonrpc_derive::rpc;

use crate::namespaces::Result;

#[rpc]
pub trait Web3NamespaceT {
#[rpc(name = "web3_clientVersion", returns = "String")]
fn web3_client_version(&self) -> Result<String>;
}
6 changes: 3 additions & 3 deletions src/node/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ mod tests {
);
let secondary_deployed_address = deployed_address_create(from_account, U256::zero());
testing::deploy_contract(
&node,
node,
H256::repeat_byte(0x1),
private_key,
secondary_bytecode,
Expand All @@ -271,7 +271,7 @@ mod tests {
);
let primary_deployed_address = deployed_address_create(from_account, U256::one());
testing::deploy_contract(
&node,
node,
H256::repeat_byte(0x1),
private_key,
primary_bytecode,
Expand Down Expand Up @@ -306,7 +306,7 @@ mod tests {

// check that the call was successful
let output =
ethers::abi::decode(&[ParamType::Uint(256)], &trace.output.0.as_slice()).unwrap();
ethers::abi::decode(&[ParamType::Uint(256)], trace.output.0.as_slice()).unwrap();
assert_eq!(output[0], Token::Uint(U256::from(84)));

// find the call to primary contract in the trace
Expand Down
1 change: 1 addition & 0 deletions src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod hardhat;
mod in_memory;
mod in_memory_ext;
mod net;
mod web3;
mod zks;

pub use in_memory::*;
13 changes: 13 additions & 0 deletions src/node/web3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use crate::{
fork::ForkSource,
namespaces::{Result, Web3NamespaceT},
node::InMemoryNode,
};

impl<S: ForkSource + std::fmt::Debug + Clone + Send + Sync + 'static> Web3NamespaceT
for InMemoryNode<S>
{
fn web3_client_version(&self) -> Result<String> {
Ok("zkSync/v2.0".to_string())
}
}
4 changes: 2 additions & 2 deletions src/node/zks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -814,8 +814,8 @@ mod tests {
"result": {
"l1Erc20DefaultBridge": format!("{:#x}", input_bridge_addresses.l1_erc20_default_bridge),
"l2Erc20DefaultBridge": format!("{:#x}", input_bridge_addresses.l2_erc20_default_bridge),
"l1WethBridge": format!("{:#x}", input_bridge_addresses.l1_weth_bridge.clone().unwrap()),
"l2WethBridge": format!("{:#x}", input_bridge_addresses.l2_weth_bridge.clone().unwrap())
"l1WethBridge": format!("{:#x}", input_bridge_addresses.l1_weth_bridge.unwrap()),
"l2WethBridge": format!("{:#x}", input_bridge_addresses.l2_weth_bridge.unwrap())
},
"id": 0
}),
Expand Down
11 changes: 11 additions & 0 deletions test_endpoints.http
Original file line number Diff line number Diff line change
Expand Up @@ -790,3 +790,14 @@ content-type: application/json
"method": "config_setLogging",
"params": ["era_test_node=info,hyper=debug"]
}

###
POST http://localhost:8011
content-type: application/json

{
"jsonrpc": "2.0",
"id": "1",
"method": "web3_clientVersion",
"params": []
}

0 comments on commit 7f68fc4

Please sign in to comment.