mirror of
https://github.com/pezkuwichain/revive-differential-tests.git
synced 2026-06-14 19:01:02 +00:00
Add a way to get the block difficulty from the node
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
//! This crate implements all node interactions.
|
//! This crate implements all node interactions.
|
||||||
|
|
||||||
use alloy::primitives::{Address, ChainId};
|
use alloy::primitives::{Address, ChainId, U256};
|
||||||
use alloy::rpc::types::trace::geth::{DiffMode, GethTrace};
|
use alloy::rpc::types::trace::geth::{DiffMode, GethTrace};
|
||||||
use alloy::rpc::types::{TransactionReceipt, TransactionRequest};
|
use alloy::rpc::types::{TransactionReceipt, TransactionRequest};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
@@ -28,8 +28,11 @@ pub trait EthereumNode {
|
|||||||
// TODO: This is currently a u128 due to Kitchensink needing more than 64 bits for its gas limit
|
// TODO: This is currently a u128 due to Kitchensink needing more than 64 bits for its gas limit
|
||||||
// when we implement the changes to the gas we need to adjust this to be a u64.
|
// when we implement the changes to the gas we need to adjust this to be a u64.
|
||||||
/// Returns the gas limit of the last block.
|
/// Returns the gas limit of the last block.
|
||||||
fn gas_limit(&self) -> Result<u128>;
|
fn block_gas_limit(&self) -> Result<u128>;
|
||||||
|
|
||||||
/// Returns the coinbase of the last block.
|
/// Returns the coinbase of the last block.
|
||||||
fn coinbase(&self) -> Result<Address>;
|
fn block_coinbase(&self) -> Result<Address>;
|
||||||
|
|
||||||
|
/// Returns the difficulty of the last block.
|
||||||
|
fn block_difficulty(&self) -> Result<U256>;
|
||||||
}
|
}
|
||||||
|
|||||||
+32
-6
@@ -16,7 +16,7 @@ use std::{
|
|||||||
use alloy::{
|
use alloy::{
|
||||||
eips::BlockNumberOrTag,
|
eips::BlockNumberOrTag,
|
||||||
network::{Ethereum, EthereumWallet},
|
network::{Ethereum, EthereumWallet},
|
||||||
primitives::Address,
|
primitives::{Address, U256},
|
||||||
providers::{
|
providers::{
|
||||||
Provider, ProviderBuilder,
|
Provider, ProviderBuilder,
|
||||||
ext::DebugApi,
|
ext::DebugApi,
|
||||||
@@ -363,7 +363,7 @@ impl EthereumNode for Instance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
|
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
|
||||||
fn gas_limit(&self) -> anyhow::Result<u128> {
|
fn block_gas_limit(&self) -> anyhow::Result<u128> {
|
||||||
let provider = self.provider();
|
let provider = self.provider();
|
||||||
BlockingExecutor::execute(async move {
|
BlockingExecutor::execute(async move {
|
||||||
provider
|
provider
|
||||||
@@ -376,7 +376,7 @@ impl EthereumNode for Instance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
|
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
|
||||||
fn coinbase(&self) -> anyhow::Result<Address> {
|
fn block_coinbase(&self) -> anyhow::Result<Address> {
|
||||||
let provider = self.provider();
|
let provider = self.provider();
|
||||||
BlockingExecutor::execute(async move {
|
BlockingExecutor::execute(async move {
|
||||||
provider
|
provider
|
||||||
@@ -387,6 +387,19 @@ impl EthereumNode for Instance {
|
|||||||
.map(|block| block.header.beneficiary)
|
.map(|block| block.header.beneficiary)
|
||||||
})?
|
})?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
|
||||||
|
fn block_difficulty(&self) -> anyhow::Result<U256> {
|
||||||
|
let provider = self.provider();
|
||||||
|
BlockingExecutor::execute(async move {
|
||||||
|
provider
|
||||||
|
.await?
|
||||||
|
.get_block_by_number(BlockNumberOrTag::Latest)
|
||||||
|
.await?
|
||||||
|
.ok_or(anyhow::Error::msg("Blockchain has no blocks"))
|
||||||
|
.map(|block| block.header.difficulty)
|
||||||
|
})?
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Node for Instance {
|
impl Node for Instance {
|
||||||
@@ -536,7 +549,7 @@ mod tests {
|
|||||||
let (node, _temp_dir) = new_node();
|
let (node, _temp_dir) = new_node();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
let gas_limit = node.gas_limit();
|
let gas_limit = node.block_gas_limit();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
let gas_limit = gas_limit.expect("Failed to get the gas limit");
|
let gas_limit = gas_limit.expect("Failed to get the gas limit");
|
||||||
@@ -549,10 +562,23 @@ mod tests {
|
|||||||
let (node, _temp_dir) = new_node();
|
let (node, _temp_dir) = new_node();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
let coinbase = node.coinbase();
|
let coinbase = node.block_coinbase();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
let coinbase = coinbase.expect("Failed to get the gas limit");
|
let coinbase = coinbase.expect("Failed to get the coinbase");
|
||||||
assert_eq!(coinbase, Address::new([0xFF; 20]))
|
assert_eq!(coinbase, Address::new([0xFF; 20]))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn can_get_block_difficulty_from_node() {
|
||||||
|
// Arrange
|
||||||
|
let (node, _temp_dir) = new_node();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
let block_difficulty = node.block_difficulty();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
let block_difficulty = block_difficulty.expect("Failed to get the block difficulty");
|
||||||
|
assert_eq!(block_difficulty, U256::ZERO)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -439,7 +439,7 @@ impl EthereumNode for KitchensinkNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
|
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
|
||||||
fn gas_limit(&self) -> anyhow::Result<u128> {
|
fn block_gas_limit(&self) -> anyhow::Result<u128> {
|
||||||
let provider = self.provider();
|
let provider = self.provider();
|
||||||
BlockingExecutor::execute(async move {
|
BlockingExecutor::execute(async move {
|
||||||
provider
|
provider
|
||||||
@@ -452,7 +452,7 @@ impl EthereumNode for KitchensinkNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
|
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
|
||||||
fn coinbase(&self) -> anyhow::Result<Address> {
|
fn block_coinbase(&self) -> anyhow::Result<Address> {
|
||||||
let provider = self.provider();
|
let provider = self.provider();
|
||||||
BlockingExecutor::execute(async move {
|
BlockingExecutor::execute(async move {
|
||||||
provider
|
provider
|
||||||
@@ -463,6 +463,19 @@ impl EthereumNode for KitchensinkNode {
|
|||||||
.map(|block| block.header.beneficiary)
|
.map(|block| block.header.beneficiary)
|
||||||
})?
|
})?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip_all, fields(geth_node_id = self.id))]
|
||||||
|
fn block_difficulty(&self) -> anyhow::Result<U256> {
|
||||||
|
let provider = self.provider();
|
||||||
|
BlockingExecutor::execute(async move {
|
||||||
|
provider
|
||||||
|
.await?
|
||||||
|
.get_block_by_number(BlockNumberOrTag::Latest)
|
||||||
|
.await?
|
||||||
|
.ok_or(anyhow::Error::msg("Blockchain has no blocks"))
|
||||||
|
.map(|block| block.header.difficulty)
|
||||||
|
})?
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Node for KitchensinkNode {
|
impl Node for KitchensinkNode {
|
||||||
@@ -1209,7 +1222,7 @@ mod tests {
|
|||||||
let (node, _temp_dir) = new_node();
|
let (node, _temp_dir) = new_node();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
let gas_limit = node.gas_limit();
|
let gas_limit = node.block_gas_limit();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
let gas_limit = gas_limit.expect("Failed to get the gas limit");
|
let gas_limit = gas_limit.expect("Failed to get the gas limit");
|
||||||
@@ -1222,10 +1235,23 @@ mod tests {
|
|||||||
let (node, _temp_dir) = new_node();
|
let (node, _temp_dir) = new_node();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
let coinbase = node.coinbase();
|
let coinbase = node.block_coinbase();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
let coinbase = coinbase.expect("Failed to get the gas limit");
|
let coinbase = coinbase.expect("Failed to get the coinbase");
|
||||||
assert_eq!(coinbase, Address::ZERO)
|
assert_eq!(coinbase, Address::ZERO)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn can_get_block_difficulty_from_node() {
|
||||||
|
// Arrange
|
||||||
|
let (node, _temp_dir) = new_node();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
let block_difficulty = node.block_difficulty();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
let block_difficulty = block_difficulty.expect("Failed to get the block difficulty");
|
||||||
|
assert_eq!(block_difficulty, U256::ZERO)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user