#188 Added EVM RPC to the node (#198)

* #188 added RPC node to enable interaction
This commit is contained in:
Nikita Khateev
2024-05-28 18:29:06 +04:00
committed by GitHub
parent 3132574062
commit 135e186816
14 changed files with 1284 additions and 87 deletions
+198
View File
@@ -0,0 +1,198 @@
use std::{collections::BTreeMap, sync::Arc};
// Frontier
pub use fc_rpc::{EthBlockDataCacheTask, EthConfig, OverrideHandle};
pub use fc_rpc_core::types::{FeeHistoryCache, FeeHistoryCacheLimit, FilterPool};
pub use fc_storage::overrides_handle;
use fp_rpc::{ConvertTransaction, ConvertTransactionRuntimeApi, EthereumRuntimeRPCApi};
use jsonrpsee::RpcModule;
// Substrate
use sc_client_api::{
backend::{Backend, StorageProvider},
client::BlockchainEvents,
AuxStore, UsageProvider,
};
use sc_network::NetworkService;
use sc_network_sync::SyncingService;
use sc_rpc::SubscriptionTaskExecutor;
use sc_transaction_pool::{ChainApi, Pool};
use sc_transaction_pool_api::TransactionPool;
use sp_api::{CallApiAt, ProvideRuntimeApi};
use sp_block_builder::BlockBuilder as BlockBuilderApi;
use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};
use sp_consensus_aura::{sr25519::AuthorityId as AuraId, AuraApi};
use sp_core::H256;
use sp_inherents::CreateInherentDataProviders;
use sp_runtime::traits::Block as BlockT;
/// Extra dependencies for Ethereum compatibility.
pub struct EthDeps<B: BlockT, C, P, A: ChainApi, CT, CIDP> {
/// The client instance to use.
pub client: Arc<C>,
/// Transaction pool instance.
pub pool: Arc<P>,
/// Graph pool instance.
pub graph: Arc<Pool<A>>,
/// Ethereum transaction converter.
pub converter: Option<CT>,
/// The Node authority flag
pub is_authority: bool,
/// Whether to enable dev signer
pub enable_dev_signer: bool,
/// Network service
pub network: Arc<NetworkService<B, B::Hash>>,
/// Chain syncing service
pub sync: Arc<SyncingService<B>>,
/// Frontier Backend.
pub frontier_backend: Arc<dyn fc_api::Backend<B>>,
/// Ethereum data access overrides.
pub overrides: Arc<OverrideHandle<B>>,
/// Cache for Ethereum block data.
pub block_data_cache: Arc<EthBlockDataCacheTask<B>>,
/// EthFilterApi pool.
pub filter_pool: Option<FilterPool>,
/// Maximum number of logs in a query.
pub max_past_logs: u32,
/// Fee history cache.
pub fee_history_cache: FeeHistoryCache,
/// Maximum fee history cache size.
pub fee_history_cache_limit: FeeHistoryCacheLimit,
/// Maximum allowed gas limit will be ` block.gas_limit * execute_gas_limit_multiplier` when
/// using eth_call/eth_estimateGas.
pub execute_gas_limit_multiplier: u64,
/// Mandated parent hashes for a given block hash.
pub forced_parent_hashes: Option<BTreeMap<H256, H256>>,
/// Something that can create the inherent data providers for pending state
pub pending_create_inherent_data_providers: CIDP,
}
/// Instantiate Ethereum-compatible RPC extensions.
pub fn create_eth<B, C, BE, P, A, CT, CIDP, EC>(
mut io: RpcModule<()>,
deps: EthDeps<B, C, P, A, CT, CIDP>,
subscription_task_executor: SubscriptionTaskExecutor,
pubsub_notification_sinks: Arc<
fc_mapping_sync::EthereumBlockNotificationSinks<
fc_mapping_sync::EthereumBlockNotification<B>,
>,
>,
) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>
where
B: BlockT<Hash = H256>,
C: CallApiAt<B> + ProvideRuntimeApi<B>,
C::Api: AuraApi<B, AuraId>
+ BlockBuilderApi<B>
+ ConvertTransactionRuntimeApi<B>
+ EthereumRuntimeRPCApi<B>,
C: HeaderBackend<B> + HeaderMetadata<B, Error = BlockChainError>,
C: BlockchainEvents<B> + AuxStore + UsageProvider<B> + StorageProvider<B, BE> + 'static,
BE: Backend<B> + 'static,
P: TransactionPool<Block = B> + 'static,
A: ChainApi<Block = B> + 'static,
CT: ConvertTransaction<<B as BlockT>::Extrinsic> + Send + Sync + 'static,
CIDP: CreateInherentDataProviders<B, ()> + Send + 'static,
EC: EthConfig<B, C>,
{
use fc_rpc::{
pending::AuraConsensusDataProvider, Debug, DebugApiServer, Eth, EthApiServer, EthDevSigner,
EthFilter, EthFilterApiServer, EthPubSub, EthPubSubApiServer, EthSigner, Net, NetApiServer,
Web3, Web3ApiServer,
};
#[cfg(feature = "txpool")]
use fc_rpc::{TxPool, TxPoolApiServer};
let EthDeps {
client,
pool,
graph,
converter,
is_authority,
enable_dev_signer,
network,
sync,
frontier_backend,
overrides,
block_data_cache,
filter_pool,
max_past_logs,
fee_history_cache,
fee_history_cache_limit,
execute_gas_limit_multiplier,
forced_parent_hashes,
pending_create_inherent_data_providers,
} = deps;
let mut signers = Vec::new();
if enable_dev_signer {
signers.push(Box::new(EthDevSigner::new()) as Box<dyn EthSigner>);
}
io.merge(
Eth::<B, C, P, CT, BE, A, CIDP, EC>::new(
client.clone(),
pool.clone(),
graph.clone(),
converter,
sync.clone(),
signers,
overrides.clone(),
frontier_backend.clone(),
is_authority,
block_data_cache.clone(),
fee_history_cache,
fee_history_cache_limit,
execute_gas_limit_multiplier,
forced_parent_hashes,
pending_create_inherent_data_providers,
Some(Box::new(AuraConsensusDataProvider::new(client.clone()))),
)
.replace_config::<EC>()
.into_rpc(),
)?;
if let Some(filter_pool) = filter_pool {
io.merge(
EthFilter::new(
client.clone(),
frontier_backend.clone(),
graph.clone(),
filter_pool,
500_usize, // max stored filters
max_past_logs,
block_data_cache.clone(),
)
.into_rpc(),
)?;
}
io.merge(
EthPubSub::new(
pool,
client.clone(),
sync,
subscription_task_executor,
overrides.clone(),
pubsub_notification_sinks,
)
.into_rpc(),
)?;
io.merge(
Net::new(
client.clone(),
network,
// Whether to format the `peer_count` response as Hex (default) or not.
true,
)
.into_rpc(),
)?;
io.merge(Web3::new(client.clone()).into_rpc())?;
io.merge(Debug::new(client.clone(), frontier_backend, overrides, block_data_cache).into_rpc())?;
#[cfg(feature = "txpool")]
io.merge(TxPool::new(client, graph).into_rpc())?;
Ok(io)
}
+102
View File
@@ -0,0 +1,102 @@
//! A collection of node-specific RPC methods.
//! Substrate provides the `sc-rpc` crate, which defines the core RPC layer
//! used by Substrate nodes. This file extends those RPC definitions with
//! capabilities that are specific to this project's runtime configuration.
#![warn(missing_docs)]
mod eth;
use std::sync::Arc;
use parachain_template_runtime::{opaque::Block, AccountId, Balance, Nonce};
use sc_client_api::{backend::Backend, AuxStore, BlockchainEvents, StorageProvider, UsageProvider};
pub use sc_rpc::DenyUnsafe;
use sc_rpc::SubscriptionTaskExecutor;
use sc_transaction_pool::ChainApi;
use sc_transaction_pool_api::TransactionPool;
use sp_api::{CallApiAt, ProvideRuntimeApi};
use sp_block_builder::BlockBuilder;
use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_inherents::CreateInherentDataProviders;
use sp_runtime::traits::Block as BlockT;
pub use self::eth::{overrides_handle, EthDeps};
use crate::rpc::eth::create_eth;
/// A type representing all RPC extensions.
pub type RpcExtension = jsonrpsee::RpcModule<()>;
/// Full client dependencies
pub struct FullDeps<C, P, A: ChainApi, CT, CIDP> {
/// The client instance to use.
pub client: Arc<C>,
/// Transaction pool instance.
pub pool: Arc<P>,
/// Whether to deny unsafe calls
pub deny_unsafe: DenyUnsafe,
/// Ethereum-compatibility specific dependencies.
pub eth: EthDeps<Block, C, P, A, CT, CIDP>,
}
pub struct DefaultEthConfig<C, BE>(std::marker::PhantomData<(C, BE)>);
impl<C, BE> fc_rpc::EthConfig<Block, C> for DefaultEthConfig<C, BE>
where
C: StorageProvider<Block, BE> + Sync + Send + 'static,
BE: Backend<Block> + 'static,
{
type EstimateGasAdapter = ();
type RuntimeStorageOverride =
fc_rpc::frontier_backend_client::SystemAccountId20StorageOverride<Block, C, BE>;
}
/// Instantiate all RPC extensions.
pub fn create_full<C, P, A, CT, CIDP, BE>(
deps: FullDeps<C, P, A, CT, CIDP>,
subscription_task_executor: SubscriptionTaskExecutor,
pubsub_notification_sinks: Arc<
fc_mapping_sync::EthereumBlockNotificationSinks<
fc_mapping_sync::EthereumBlockNotification<Block>,
>,
>,
) -> Result<RpcExtension, Box<dyn std::error::Error + Send + Sync>>
where
C: ProvideRuntimeApi<Block>
+ HeaderBackend<Block>
+ AuxStore
+ HeaderMetadata<Block, Error = BlockChainError>
+ Send
+ Sync
+ CallApiAt<Block>
+ UsageProvider<Block>
+ StorageProvider<Block, BE>
+ BlockchainEvents<Block>
+ 'static,
C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,
C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Nonce>,
C::Api: sp_consensus_aura::AuraApi<Block, AuraId>,
C::Api: BlockBuilder<Block>,
C::Api: fp_rpc::ConvertTransactionRuntimeApi<Block>,
C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,
P: TransactionPool<Block = Block> + Sync + Send + 'static,
A: ChainApi<Block = Block> + 'static,
CIDP: CreateInherentDataProviders<Block, ()> + Send + 'static,
CT: fp_rpc::ConvertTransaction<<Block as BlockT>::Extrinsic> + Send + Sync + 'static,
BE: Backend<Block> + 'static,
{
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
use substrate_frame_rpc_system::{System, SystemApiServer};
let mut module = RpcExtension::new(());
let FullDeps { client, pool, deny_unsafe, eth } = deps;
module.merge(System::new(client.clone(), pool, deny_unsafe).into_rpc())?;
module.merge(TransactionPayment::new(client).into_rpc())?;
let module = create_eth::<_, _, _, _, _, _, _, DefaultEthConfig<C, BE>>(
module,
eth,
subscription_task_executor,
pubsub_notification_sinks,
)?;
Ok(module)
}