Skip to content

Commit

Permalink
feat: hardfork storage
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Aug 29, 2023
1 parent 14f5b3a commit c90e94a
Show file tree
Hide file tree
Showing 15 changed files with 355 additions and 240 deletions.
8 changes: 6 additions & 2 deletions core/api/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use protocol::traits::{
};
use protocol::trie::Trie as _;
use protocol::types::{
Account, BigEndianHash, Block, BlockNumber, Bytes, CkbRelatedInfo, ExecutorContext, Hash,
Header, Metadata, Proposal, Receipt, SignedTransaction, TxResp, H160, H256,
Account, BigEndianHash, Block, BlockNumber, Bytes, CkbRelatedInfo, ExecutorContext,
HardforkInfo, Hash, Header, Metadata, Proposal, Receipt, SignedTransaction, TxResp, H160, H256,
MAX_BLOCK_GAS_LIMIT, NIL_DATA, RLP_NULL, U256,
};
use protocol::{async_trait, codec::ProtocolCodec, trie, ProtocolResult};
Expand Down Expand Up @@ -281,4 +281,8 @@ where
)?
.get_metadata_root())
}

async fn hardfork_info(&self, ctx: Context) -> ProtocolResult<HardforkInfo> {
MetadataHandle::new(self.get_metadata_root(ctx).await?).hardfork_infos()
}
}
11 changes: 10 additions & 1 deletion core/api/src/jsonrpc/impl/axon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use jsonrpsee::core::Error;

use protocol::async_trait;
use protocol::traits::{APIAdapter, Context};
use protocol::types::{Block, CkbRelatedInfo, Metadata, Proof, Proposal, U256};
use protocol::types::{Block, CkbRelatedInfo, HardforkInfo, Metadata, Proof, Proposal, U256};

use crate::jsonrpc::web3_types::BlockId;
use crate::jsonrpc::{AxonRpcServer, RpcResult};
Expand Down Expand Up @@ -96,4 +96,13 @@ impl<Adapter: APIAdapter + 'static> AxonRpcServer for AxonRpcImpl<Adapter> {

Ok(ret)
}

async fn hardfork_info(&self) -> RpcResult<HardforkInfo> {
let ret = self
.adapter
.hardfork_info(Context::new())
.await
.map_err(|e| Error::Custom(e.to_string()))?;
Ok(ret)
}
}
5 changes: 4 additions & 1 deletion core/api/src/jsonrpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use jsonrpsee::{core::Error, proc_macros::rpc};
use common_config_parser::types::Config;
use protocol::traits::APIAdapter;
use protocol::types::{
Block, CkbRelatedInfo, Hash, Hex, Metadata, Proof, Proposal, H160, H256, U256,
Block, CkbRelatedInfo, HardforkInfo, Hash, Hex, Metadata, Proof, Proposal, H160, H256, U256,
};
use protocol::ProtocolResult;

Expand Down Expand Up @@ -225,6 +225,9 @@ pub trait AxonRpc {

#[method(name = "axon_getCkbRelatedInfo")]
async fn get_ckb_related_info(&self) -> RpcResult<CkbRelatedInfo>;

#[method(name = "axon_getHardforkInfo")]
async fn hardfork_info(&self) -> RpcResult<HardforkInfo>;
}

#[rpc(server)]
Expand Down
1 change: 1 addition & 0 deletions core/executor/benches/bench_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl BenchAdapter {
gas_price: 85u64.into(),
block_gas_limit: 100_000_000_000u64.into(),
block_base_fee_per_gas: Default::default(),
extra_data: Default::default(),
},
)
.unwrap()
Expand Down
1 change: 1 addition & 0 deletions core/executor/benches/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub fn mock_executor_context() -> ExecutorContext {
gas_price: 85u64.into(),
block_gas_limit: 100_000_000_000u64.into(),
block_base_fee_per_gas: Default::default(),
extra_data: Default::default(),
}
}

Expand Down
1 change: 1 addition & 0 deletions core/executor/src/debugger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ impl EvmDebugger {
gas_price: 1u64.into(),
block_gas_limit: 4294967295000u64.into(),
block_base_fee_per_gas: 1337u64.into(),
extra_data: Default::default(),
};

AxonExecutorApplyAdapter::from_root(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ pub use ckb_light_client_contract::*;
clippy::upper_case_acronyms,
clippy::type_complexity,
dead_code,
non_camel_case_types
non_camel_case_types,
)]
pub mod ckb_light_client_contract {
#[rustfmt::skip]
const __ABI: &str = "[\n {\n \"inputs\": [\n {\n \"internalType\": \"bytes32[]\",\n \"name\": \"blockHashes\",\n \"type\": \"bytes32[]\"\n }\n ],\n \"name\": \"rollback\",\n \"outputs\": [],\n \"stateMutability\": \"view\",\n \"type\": \"function\"\n },\n {\n \"inputs\": [\n {\n \"internalType\": \"bool\",\n \"name\": \"allowRead\",\n \"type\": \"bool\"\n }\n ],\n \"name\": \"setState\",\n \"outputs\": [],\n \"stateMutability\": \"view\",\n \"type\": \"function\"\n },\n {\n \"inputs\": [\n {\n \"components\": [\n {\n \"internalType\": \"uint32\",\n \"name\": \"version\",\n \"type\": \"uint32\"\n },\n {\n \"internalType\": \"uint32\",\n \"name\": \"compactTarget\",\n \"type\": \"uint32\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"timestamp\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"number\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"epoch\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"bytes32\",\n \"name\": \"parentHash\",\n \"type\": \"bytes32\"\n },\n {\n \"internalType\": \"bytes32\",\n \"name\": \"transactionsRoot\",\n \"type\": \"bytes32\"\n },\n {\n \"internalType\": \"bytes32\",\n \"name\": \"proposalsHash\",\n \"type\": \"bytes32\"\n },\n {\n \"internalType\": \"bytes32\",\n \"name\": \"extraHash\",\n \"type\": \"bytes32\"\n },\n {\n \"internalType\": \"bytes32\",\n \"name\": \"dao\",\n \"type\": \"bytes32\"\n },\n {\n \"internalType\": \"uint128\",\n \"name\": \"nonce\",\n \"type\": \"uint128\"\n },\n {\n \"internalType\": \"bytes\",\n \"name\": \"extension\",\n \"type\": \"bytes\"\n },\n {\n \"internalType\": \"bytes32\",\n \"name\": \"blockHash\",\n \"type\": \"bytes32\"\n }\n ],\n \"internalType\": \"struct CkbType.Header[]\",\n \"name\": \"headers\",\n \"type\": \"tuple[]\"\n }\n ],\n \"name\": \"update\",\n \"outputs\": [],\n \"stateMutability\": \"view\",\n \"type\": \"function\"\n }\n]\n";
/// The parsed JSON ABI of the contract.
pub static CKBLIGHTCLIENTCONTRACT_ABI: ::ethers::contract::Lazy<::ethers::core::abi::Abi> =
::ethers::contract::Lazy::new(|| {
::ethers::core::utils::__serde_json::from_str(__ABI).expect("ABI is always valid")
});
///The parsed JSON ABI of the contract.
pub static CKBLIGHTCLIENTCONTRACT_ABI: ::ethers::contract::Lazy<
::ethers::core::abi::Abi,
> = ::ethers::contract::Lazy::new(|| {
::ethers::core::utils::__serde_json::from_str(__ABI)
.expect("ABI is always valid")
});
pub struct CkbLightClientContract<M>(::ethers::contract::Contract<M>);
impl<M> ::core::clone::Clone for CkbLightClientContract<M> {
fn clone(&self) -> Self {
Expand All @@ -25,7 +27,6 @@ pub mod ckb_light_client_contract {
}
impl<M> ::core::ops::Deref for CkbLightClientContract<M> {
type Target = ::ethers::contract::Contract<M>;

fn deref(&self) -> &Self::Target {
&self.0
}
Expand All @@ -43,21 +44,21 @@ pub mod ckb_light_client_contract {
}
}
impl<M: ::ethers::providers::Middleware> CkbLightClientContract<M> {
/// Creates a new contract instance with the specified `ethers` client
/// at `address`. The contract derefs to a `ethers::Contract`
/// object.
/// Creates a new contract instance with the specified `ethers` client at
/// `address`. The contract derefs to a `ethers::Contract` object.
pub fn new<T: Into<::ethers::core::types::Address>>(
address: T,
client: ::std::sync::Arc<M>,
) -> Self {
Self(::ethers::contract::Contract::new(
address.into(),
CKBLIGHTCLIENTCONTRACT_ABI.clone(),
client,
))
Self(
::ethers::contract::Contract::new(
address.into(),
CKBLIGHTCLIENTCONTRACT_ABI.clone(),
client,
),
)
}

/// Calls the contract's `rollback` (0xd32a2285) function
///Calls the contract's `rollback` (0xd32a2285) function
pub fn rollback(
&self,
block_hashes: ::std::vec::Vec<[u8; 32]>,
Expand All @@ -66,8 +67,7 @@ pub mod ckb_light_client_contract {
.method_hash([211, 42, 34, 133], block_hashes)
.expect("method not found (this should never happen)")
}

/// Calls the contract's `setState` (0xac9f0222) function
///Calls the contract's `setState` (0xac9f0222) function
pub fn set_state(
&self,
allow_read: bool,
Expand All @@ -76,8 +76,7 @@ pub mod ckb_light_client_contract {
.method_hash([172, 159, 2, 34], allow_read)
.expect("method not found (this should never happen)")
}

/// Calls the contract's `update` (0x5d4e8442) function
///Calls the contract's `update` (0x5d4e8442) function
pub fn update(
&self,
headers: ::std::vec::Vec<Header>,
Expand All @@ -88,14 +87,12 @@ pub mod ckb_light_client_contract {
}
}
impl<M: ::ethers::providers::Middleware> From<::ethers::contract::Contract<M>>
for CkbLightClientContract<M>
{
for CkbLightClientContract<M> {
fn from(contract: ::ethers::contract::Contract<M>) -> Self {
Self::new(contract.address(), contract.client())
}
}
/// Container type for all input parameters for the `rollback` function with
/// signature `rollback(bytes32[])` and selector `0xd32a2285`
///Container type for all input parameters for the `rollback` function with signature `rollback(bytes32[])` and selector `0xd32a2285`
#[derive(
Clone,
::ethers::contract::EthCall,
Expand All @@ -104,14 +101,13 @@ pub mod ckb_light_client_contract {
Debug,
PartialEq,
Eq,
Hash,
Hash
)]
#[ethcall(name = "rollback", abi = "rollback(bytes32[])")]
pub struct RollbackCall {
pub block_hashes: ::std::vec::Vec<[u8; 32]>,
}
/// Container type for all input parameters for the `setState` function with
/// signature `setState(bool)` and selector `0xac9f0222`
///Container type for all input parameters for the `setState` function with signature `setState(bool)` and selector `0xac9f0222`
#[derive(
Clone,
::ethers::contract::EthCall,
Expand All @@ -120,16 +116,13 @@ pub mod ckb_light_client_contract {
Debug,
PartialEq,
Eq,
Hash,
Hash
)]
#[ethcall(name = "setState", abi = "setState(bool)")]
pub struct SetStateCall {
pub allow_read: bool,
}
/// Container type for all input parameters for the `update` function with
/// signature `update((uint32,uint32,uint64,uint64,uint64,bytes32,bytes32,
/// bytes32,bytes32,bytes32,uint128,bytes,bytes32)[])` and selector
/// `0x5d4e8442`
///Container type for all input parameters for the `update` function with signature `update((uint32,uint32,uint64,uint64,uint64,bytes32,bytes32,bytes32,bytes32,bytes32,uint128,bytes,bytes32)[])` and selector `0x5d4e8442`
#[derive(Clone, ::ethers::contract::EthCall, ::ethers::contract::EthDisplay)]
#[ethcall(
name = "update",
Expand All @@ -138,7 +131,7 @@ pub mod ckb_light_client_contract {
pub struct UpdateCall {
pub headers: ::std::vec::Vec<Header>,
}
/// Container type for all of the contract's call
///Container type for all of the contract's call
#[derive(Clone, ::ethers::contract::EthAbiType)]
pub enum CkbLightClientContractCalls {
Rollback(RollbackCall),
Expand All @@ -150,13 +143,16 @@ pub mod ckb_light_client_contract {
data: impl AsRef<[u8]>,
) -> ::core::result::Result<Self, ::ethers::core::abi::AbiError> {
let data = data.as_ref();
if let Ok(decoded) = <RollbackCall as ::ethers::core::abi::AbiDecode>::decode(data) {
if let Ok(decoded)
= <RollbackCall as ::ethers::core::abi::AbiDecode>::decode(data) {
return Ok(Self::Rollback(decoded));
}
if let Ok(decoded) = <SetStateCall as ::ethers::core::abi::AbiDecode>::decode(data) {
if let Ok(decoded)
= <SetStateCall as ::ethers::core::abi::AbiDecode>::decode(data) {
return Ok(Self::SetState(decoded));
}
if let Ok(decoded) = <UpdateCall as ::ethers::core::abi::AbiDecode>::decode(data) {
if let Ok(decoded)
= <UpdateCall as ::ethers::core::abi::AbiDecode>::decode(data) {
return Ok(Self::Update(decoded));
}
Err(::ethers::core::abi::Error::InvalidData.into())
Expand All @@ -165,8 +161,12 @@ pub mod ckb_light_client_contract {
impl ::ethers::core::abi::AbiEncode for CkbLightClientContractCalls {
fn encode(self) -> Vec<u8> {
match self {
Self::Rollback(element) => ::ethers::core::abi::AbiEncode::encode(element),
Self::SetState(element) => ::ethers::core::abi::AbiEncode::encode(element),
Self::Rollback(element) => {
::ethers::core::abi::AbiEncode::encode(element)
}
Self::SetState(element) => {
::ethers::core::abi::AbiEncode::encode(element)
}
Self::Update(element) => ::ethers::core::abi::AbiEncode::encode(element),
}
}
Expand Down Expand Up @@ -195,8 +195,7 @@ pub mod ckb_light_client_contract {
Self::Update(value)
}
}
/// `Header(uint32,uint32,uint64,uint64,uint64,bytes32,bytes32,bytes32,
/// bytes32,bytes32,uint128,bytes,bytes32)`
///`Header(uint32,uint32,uint64,uint64,uint64,bytes32,bytes32,bytes32,bytes32,bytes32,uint128,bytes,bytes32)`
#[derive(
Clone,
::ethers::contract::EthAbiType,
Expand All @@ -205,21 +204,21 @@ pub mod ckb_light_client_contract {
Debug,
PartialEq,
Eq,
Hash,
Hash
)]
pub struct Header {
pub version: u32,
pub compact_target: u32,
pub timestamp: u64,
pub number: u64,
pub epoch: u64,
pub parent_hash: [u8; 32],
pub version: u32,
pub compact_target: u32,
pub timestamp: u64,
pub number: u64,
pub epoch: u64,
pub parent_hash: [u8; 32],
pub transactions_root: [u8; 32],
pub proposals_hash: [u8; 32],
pub extra_hash: [u8; 32],
pub dao: [u8; 32],
pub nonce: u128,
pub extension: ::ethers::core::types::Bytes,
pub block_hash: [u8; 32],
pub proposals_hash: [u8; 32],
pub extra_hash: [u8; 32],
pub dao: [u8; 32],
pub nonce: u128,
pub extension: ::ethers::core::types::Bytes,
pub block_hash: [u8; 32],
}
}
Loading

0 comments on commit c90e94a

Please sign in to comment.