Skip to content

Commit

Permalink
Fix up some rpc data
Browse files Browse the repository at this point in the history
  • Loading branch information
pgherveou committed Jan 8, 2025
1 parent 2cb0295 commit 557019e
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 18 deletions.
Binary file modified substrate/frame/revive/rpc/examples/js/bun.lockb
Binary file not shown.
8 changes: 4 additions & 4 deletions substrate/frame/revive/rpc/examples/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
"preview": "vite preview"
},
"dependencies": {
"@parity/revive": "^0.0.8",
"ethers": "^6.13.4",
"@parity/revive": "^0.0.9",
"ethers": "^6.13.5",
"solc": "^0.8.28",
"viem": "^2.21.55"
"viem": "^2.22.4"
},
"devDependencies": {
"prettier": "^3.4.2",
"@types/bun": "^1.1.14",
"@types/bun": "^1.1.15",
"typescript": "^5.7.2",
"vite": "^5.4.11"
}
Expand Down
Binary file modified substrate/frame/revive/rpc/examples/js/pvm/FlipperCaller.polkavm
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ for (const file of input) {
}

console.log('Compiling with revive...')
const reviveOut = await compile(input, { wasm: !!process.env.WASM })
const reviveOut = await compile(input, { bin: 'resolc' })

for (const contracts of Object.values(reviveOut.contracts)) {
for (const [name, contract] of Object.entries(contracts)) {
Expand Down
30 changes: 26 additions & 4 deletions substrate/frame/revive/rpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ use crate::{
subxt_client::{
revive::calls::types::EthTransact, runtime_types::pallet_revive::storage::ContractInfo,
},
BlockInfoProvider, ReceiptProvider, LOG_TARGET,
BlockInfoProvider, ReceiptProvider, TransactionInfo, LOG_TARGET,
};
use jsonrpsee::types::{error::CALL_EXECUTION_FAILED_CODE, ErrorObjectOwned};
use pallet_revive::{
evm::{
Block, BlockNumberOrTag, BlockNumberOrTagOrHash, Bytes256, GenericTransaction, ReceiptInfo,
Block, BlockNumberOrTag, BlockNumberOrTagOrHash, GenericTransaction, ReceiptInfo,
SyncingProgress, SyncingStatus, TransactionSigned, H160, H256, U256,
},
EthTransactError, EthTransactInfo,
Expand Down Expand Up @@ -668,7 +668,11 @@ impl Client {
}

/// Get the EVM block for the given hash.
pub async fn evm_block(&self, block: Arc<SubstrateBlock>) -> Result<Block, ClientError> {
pub async fn evm_block(
&self,
block: Arc<SubstrateBlock>,
hydrated_transactions: bool,
) -> Result<Block, ClientError> {
let runtime_api = self.api.runtime_api().at(block.hash());
let max_fee = Self::weight_to_fee(&runtime_api, self.max_block_weight()).await?;
let gas_limit = gas_from_fee(max_fee);
Expand All @@ -681,6 +685,23 @@ impl Client {
let state_root = header.state_root.0.into();
let extrinsics_root = header.extrinsics_root.0.into();

let receipts = extract_receipts_from_block(&block).await?;
let gas_used =
receipts.iter().fold(U256::zero(), |acc, (_, receipt)| acc + receipt.gas_used);
let transactions = if hydrated_transactions {
receipts
.into_iter()
.map(|(signed_tx, receipt)| TransactionInfo::new(receipt, signed_tx).into())
.collect::<Vec<TransactionInfo>>()
.into()
} else {
receipts
.into_iter()
.map(|(_, receipt)| receipt.transaction_hash)
.collect::<Vec<_>>()
.into()
};

Ok(Block {
hash: block.hash(),
parent_hash,
Expand All @@ -690,8 +711,9 @@ impl Client {
timestamp: timestamp.into(),
difficulty: Some(0u32.into()),
gas_limit,
logs_bloom: Bytes256([0u8; 256]),
gas_used,
receipts_root: extrinsics_root,
transactions,
..Default::default()
})
}
Expand Down
8 changes: 4 additions & 4 deletions substrate/frame/revive/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ impl EthRpcServer for EthRpcServerImpl {
async fn get_block_by_hash(
&self,
block_hash: H256,
_hydrated_transactions: bool,
hydrated_transactions: bool,
) -> RpcResult<Option<Block>> {
let Some(block) = self.client.block_by_hash(&block_hash).await? else {
return Ok(None);
};
let block = self.client.evm_block(block).await?;
let block = self.client.evm_block(block, hydrated_transactions).await?;
Ok(Some(block))
}

Expand Down Expand Up @@ -238,12 +238,12 @@ impl EthRpcServer for EthRpcServerImpl {
async fn get_block_by_number(
&self,
block: BlockNumberOrTag,
_hydrated_transactions: bool,
hydrated_transactions: bool,
) -> RpcResult<Option<Block>> {
let Some(block) = self.client.block_by_number_or_tag(&block).await? else {
return Ok(None);
};
let block = self.client.evm_block(block).await?;
let block = self.client.evm_block(block, hydrated_transactions).await?;
Ok(Some(block))
}

Expand Down
10 changes: 5 additions & 5 deletions substrate/frame/revive/src/evm/api/rpc_types_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub struct Block {
/// Total difficulty
#[serde(rename = "totalDifficulty", skip_serializing_if = "Option::is_none")]
pub total_difficulty: Option<U256>,
pub transactions: H256OrTransactionInfo,
pub transactions: HashesOrTransactionInfos,
/// Transactions root
#[serde(rename = "transactionsRoot")]
pub transactions_root: H256,
Expand Down Expand Up @@ -357,15 +357,15 @@ pub enum BlockTag {
Debug, Clone, Encode, Decode, TypeInfo, Serialize, Deserialize, From, TryInto, Eq, PartialEq,
)]
#[serde(untagged)]
pub enum H256OrTransactionInfo {
pub enum HashesOrTransactionInfos {
/// Transaction hashes
H256s(Vec<H256>),
Hashes(Vec<H256>),
/// Full transactions
TransactionInfos(Vec<TransactionInfo>),
}
impl Default for H256OrTransactionInfo {
impl Default for HashesOrTransactionInfos {
fn default() -> Self {
H256OrTransactionInfo::H256s(Default::default())
HashesOrTransactionInfos::Hashes(Default::default())
}
}

Expand Down

0 comments on commit 557019e

Please sign in to comment.