Skip to content

Commit

Permalink
Implement Web3Api (#205)
Browse files Browse the repository at this point in the history
* Implement Web3Api

* Add Web3Api tests

* Fix checker

* Fix test
  • Loading branch information
tgmichel committed Nov 21, 2020
1 parent 567619b commit bd08913
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 6 deletions.
2 changes: 1 addition & 1 deletion client/rpc-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ mod web3;
pub use eth::{EthApi, EthApiServer, EthFilterApi};
pub use eth_pubsub::{EthPubSubApi, EthPubSubApiServer};
pub use net::{NetApi, NetApiServer};
pub use web3::Web3Api;
pub use web3::{Web3Api, Web3ApiServer};
2 changes: 2 additions & 0 deletions client/rpc-core/src/web3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use jsonrpc_derive::rpc;

use crate::types::Bytes;

pub use rpc_impl_Web3Api::gen_server::Web3Api as Web3ApiServer;

/// Web3 rpc interface.
#[rpc(server)]
pub trait Web3Api {
Expand Down
50 changes: 47 additions & 3 deletions client/rpc/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ use sp_runtime::{
traits::{Block as BlockT, UniqueSaturatedInto, Zero, One, Saturating, BlakeTwo256},
transaction_validity::TransactionSource
};
use sp_api::{ProvideRuntimeApi, BlockId};
use sp_api::{ProvideRuntimeApi, BlockId, Core};
use sp_transaction_pool::{TransactionPool, InPoolTransaction};
use sc_client_api::backend::{StorageProvider, Backend, StateBackend, AuxStore};
use sha3::{Keccak256, Digest};
use sp_blockchain::{Error as BlockChainError, HeaderMetadata, HeaderBackend};
use sc_network::{NetworkService, ExHashT};
use fc_rpc_core::{EthApi as EthApiT, NetApi as NetApiT};
use fc_rpc_core::{EthApi as EthApiT, NetApi as NetApiT, Web3Api as Web3ApiT};
use fc_rpc_core::types::{
BlockNumber, Bytes, CallRequest, Filter, FilteredParams, Index, Log, Receipt, RichBlock,
SyncStatus, SyncInfo, Transaction, Work, Rich, Block, BlockTransactions, VariadicValue,
Expand All @@ -44,7 +44,7 @@ use fc_rpc_core::types::{
use fp_rpc::{EthereumRuntimeRPCApi, ConvertTransaction, TransactionStatus};
use crate::{internal_err, error_on_execution_failure, EthSigner};

pub use fc_rpc_core::{EthApiServer, NetApiServer};
pub use fc_rpc_core::{EthApiServer, NetApiServer, Web3ApiServer};
use codec::{self, Encode};

pub struct EthApi<B: BlockT, C, P, CT, BE, H: ExHashT> {
Expand Down Expand Up @@ -1077,3 +1077,47 @@ impl<B, BE, C> NetApiT for NetApi<B, BE, C> where
.map_err(|_| internal_err("fetch runtime chain id failed"))?.to_string())
}
}

pub struct Web3Api<B, C> {
client: Arc<C>,
_marker: PhantomData<B>,
}

impl<B, C> Web3Api<B, C> {
pub fn new(
client: Arc<C>,
) -> Self {
Self {
client: client,
_marker: PhantomData,
}
}
}

impl<B, C> Web3ApiT for Web3Api<B, C> where
C: ProvideRuntimeApi<B> + AuxStore,
C::Api: EthereumRuntimeRPCApi<B>,
C: HeaderBackend<B> + HeaderMetadata<B, Error=BlockChainError> + 'static,
C: Send + Sync + 'static,
B: BlockT<Hash=H256> + Send + Sync + 'static,
{
fn client_version(&self) -> Result<String> {
let hash = self.client.info().best_hash;
let version = self.client.runtime_api().version(&BlockId::Hash(hash))
.map_err(|err| internal_err(format!("fetch runtime version failed: {:?}", err)))?;
Ok(format!(
"{spec_name}/v{spec_version}.{impl_version}/{pkg_name}-{pkg_version}",
spec_name = version.spec_name,
spec_version = version.spec_version,
impl_version = version.impl_version,
pkg_name = env!("CARGO_PKG_NAME"),
pkg_version = env!("CARGO_PKG_VERSION")
))
}

fn sha3(&self, input: Bytes) -> Result<H256> {
Ok(H256::from_slice(
Keccak256::digest(&input.into_vec()).as_slice()
))
}
}
2 changes: 1 addition & 1 deletion client/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
mod eth;
mod eth_pubsub;

pub use eth::{EthApi, EthApiServer, NetApi, NetApiServer};
pub use eth::{EthApi, EthApiServer, NetApi, NetApiServer, Web3Api, Web3ApiServer};
pub use eth_pubsub::{EthPubSubApi, EthPubSubApiServer};

use ethereum_types::{H160, H256};
Expand Down
9 changes: 8 additions & 1 deletion template/node/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn create_full<C, P, BE>(
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};
use fc_rpc::{
EthApi, EthApiServer, NetApi, NetApiServer, EthPubSubApi, EthPubSubApiServer,
EthDevSigner, EthSigner,
Web3Api, Web3ApiServer, EthDevSigner, EthSigner,
};

let mut io = jsonrpc_core::IoHandler::default();
Expand Down Expand Up @@ -111,6 +111,13 @@ pub fn create_full<C, P, BE>(
client.clone(),
))
);

io.extend_with(
Web3ApiServer::to_delegate(Web3Api::new(
client.clone(),
))
);

io.extend_with(
EthPubSubApiServer::to_delegate(EthPubSubApi::new(
pool.clone(),
Expand Down
18 changes: 18 additions & 0 deletions ts-tests/tests/test-web3api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect } from "chai";
import { step } from "mocha-steps";
import { describeWithFrontier, customRequest } from "./util";

describeWithFrontier("Frontier RPC (Web3Api)", `simple-specs.json`, (context) => {

step("should get client version", async function () {
const version = await context.web3.eth.getNodeInfo();
expect(version).to.be.equal("node-frontier-template/v1.1/fc-rpc-0.1.0");
});

step("should remote sha3", async function () {
const data = context.web3.utils.stringToHex("hello");
const hash = await customRequest(context.web3, "web3_sha3", [data]);
const local_hash = context.web3.utils.sha3("hello");
expect(hash.result).to.be.equal(local_hash);
});
});

0 comments on commit bd08913

Please sign in to comment.