Skip to content
Merged
630 changes: 315 additions & 315 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ revive-strategy = { path = "crates/revive-strategy" }
revive-utils = { path = "crates/revive-utils" }

# polkadot-sdk
polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master", features = [
polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "pg/finalized-block-dev-node", features = [
"experimental",
"runtime",
"polkadot-runtime-common",
Expand Down
6 changes: 3 additions & 3 deletions crates/anvil-polkadot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ path = "bin/main.rs"
# foundry internal
codec = { version = "3.7.5", default-features = true, package = "parity-scale-codec" }
substrate-runtime = { path = "substrate-runtime" }
pallet-revive-eth-rpc = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master" }
pallet-revive-eth-rpc = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "pg/finalized-block-dev-node" }
secp256k1 = { version = "0.28.0", default-features = false }
libsecp256k1 = { version = "0.7.0", default-features = false }
sp-runtime-interface = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master", default-features = false }
polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master", default-features = false, features = [
sp-runtime-interface = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "pg/finalized-block-dev-node", default-features = false }
polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "pg/finalized-block-dev-node", default-features = false, features = [
"sc-allocator",
"sc-basic-authorship",
"sc-block-builder",
Expand Down
27 changes: 16 additions & 11 deletions crates/anvil-polkadot/src/api_server/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,17 +1262,22 @@ impl ApiServer {
}
// ----- Helpers
async fn update_block_provider_on_revert(&self, info: &Info<OpaqueBlock>) -> Result<()> {
let new_best_block = self.block_provider.block_by_number(info.best_number).await?;
let new_finalized_block =
self.block_provider.block_by_number(info.finalized_number).await?;

if let Some(block) = new_best_block.and_then(Arc::into_inner) {
self.block_provider.update_latest(block, SubscriptionType::BestBlocks).await;
}

if let Some(block) = new_finalized_block.and_then(Arc::into_inner) {
self.block_provider.update_latest(block, SubscriptionType::FinalizedBlocks).await;
}
let best_block = self.block_provider.block_by_number(info.best_number).await?.ok_or(
Error::InternalError(format!(
"Could not find best block with number {}",
info.best_number
)),
)?;
self.block_provider.update_latest(best_block, SubscriptionType::BestBlocks).await;

let finalized_block =
self.block_provider.block_by_number(info.finalized_number).await?.ok_or(
Error::InternalError(format!(
"Could not find finalized block with number {}",
info.best_number
)),
)?;
self.block_provider.update_latest(finalized_block, SubscriptionType::FinalizedBlocks).await;

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/anvil-polkadot/substrate-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ license.workspace = true
[dependencies]
array-bytes = { version = "6.2.2", default-features = false }
codec = { version = "3.7.5", default-features = false, package = "parity-scale-codec" }
polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master", default-features = false, features = [
polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "pg/finalized-block-dev-node", default-features = false, features = [
"pallet-aura",
"pallet-balances",
"pallet-revive",
Expand All @@ -30,7 +30,7 @@ scale-info = { version = "2.11.6", default-features = false }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }

[build-dependencies]
polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master", default-features = false, optional = true, features = ["substrate-wasm-builder"] }
polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "pg/finalized-block-dev-node", default-features = false, optional = true, features = ["substrate-wasm-builder"] }

[features]
default = ["std"]
Expand Down
2 changes: 0 additions & 2 deletions crates/anvil-polkadot/tests/it/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ async fn test_set_next_fee_multiplier(#[case] rpc_driven: bool) {
let tx_hash = node.send_transaction(transaction, None).await.unwrap();
unwrap_response::<()>(node.eth_rpc(EthRequest::Mine(None, None)).await.unwrap()).unwrap();
node.wait_for_block_with_timeout(1, Duration::from_millis(400)).await.unwrap();
tokio::time::sleep(Duration::from_millis(400)).await;
let transaction_receipt = node.get_transaction_receipt(tx_hash).await;
let effective_gas_price =
U256::from_be_bytes(transaction_receipt.effective_gas_price.to_big_endian());
Expand Down Expand Up @@ -129,7 +128,6 @@ async fn test_next_fee_multiplier_minimum() {
let tx_hash = node.send_transaction(transaction, None).await.unwrap();
unwrap_response::<()>(node.eth_rpc(EthRequest::Mine(None, None)).await.unwrap()).unwrap();
node.wait_for_block_with_timeout(1, Duration::from_millis(400)).await.unwrap();
tokio::time::sleep(Duration::from_millis(400)).await;
let transaction_receipt = node.get_transaction_receipt(tx_hash).await;
let effective_gas_price =
U256::from_be_bytes(transaction_receipt.effective_gas_price.to_big_endian());
Expand Down
50 changes: 20 additions & 30 deletions crates/anvil-polkadot/tests/it/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,8 @@ use polkadot_sdk::pallet_revive::{
use std::collections::HashSet;
use subxt::utils::H256;

async fn assert_block_number_is_best_and_finalized(
node: &mut TestNode,
n: u64,
wait_for_block_provider: Option<Duration>,
) {
async fn assert_block_number_is_best_and_finalized(node: &mut TestNode, n: u64) {
assert_eq!(std::convert::Into::<u64>::into(node.best_block_number().await), n);
if let Some(duration) = wait_for_block_provider {
tokio::time::sleep(duration).await;
}
let best_block = unwrap_response::<Block>(
node.eth_rpc(EthRequest::EthGetBlockByNumber(alloy_eips::BlockNumberOrTag::Latest, false))
.await
Expand Down Expand Up @@ -74,22 +67,20 @@ async fn mine_blocks(node: &mut TestNode, blocks: u64, assert_best_block: u64) {
node.eth_rpc(EthRequest::Mine(Some(U256::from(blocks)), None)).await.unwrap(),
)
.unwrap();
assert_block_number_is_best_and_finalized(node, assert_best_block, None).await;
assert_block_number_is_best_and_finalized(node, assert_best_block).await;
}

async fn revert(
node: &mut TestNode,
snapshot_id: U256,
assert_best_block: u64,
assert_success: bool,
wait_for_block_provider: Option<Duration>,
) {
let reverted =
unwrap_response::<bool>(node.eth_rpc(EthRequest::EvmRevert(snapshot_id)).await.unwrap())
.unwrap();
assert_eq!(reverted, assert_success);
assert_block_number_is_best_and_finalized(node, assert_best_block, wait_for_block_provider)
.await;
assert_block_number_is_best_and_finalized(node, assert_best_block).await;
}

async fn do_transfer(
Expand Down Expand Up @@ -156,7 +147,7 @@ async fn test_best_block_after_evm_revert() {
let mut node = TestNode::new(anvil_node_config.clone(), substrate_node_config).await.unwrap();

// Assert on initial best block number.
assert_block_number_is_best_and_finalized(&mut node, 0, None).await;
assert_block_number_is_best_and_finalized(&mut node, 0).await;

// Snapshot at genesis.
let zero = snapshot(&mut node, U256::ZERO).await;
Expand All @@ -172,27 +163,27 @@ async fn test_best_block_after_evm_revert() {

// Snapshot again at block number 10.
let two = snapshot(&mut node, U256::from(2)).await;
assert_block_number_is_best_and_finalized(&mut node, 10, None).await;
assert_block_number_is_best_and_finalized(&mut node, 10).await;

// Mine 5 more blocks.
mine_blocks(&mut node, 5, 15).await;

// Revert to the second snapshot and assert best block number is 10.
revert(&mut node, two, 10, true, None).await;
revert(&mut node, two, 10, true).await;

// Check mining works fine after reverting.
mine_blocks(&mut node, 10, 20).await;

// Revert immediatelly after a snapshot (same best number is expected after the revert).
let id = snapshot(&mut node, U256::from(3)).await;
revert(&mut node, id, 20, true, Some(Duration::from_millis(500))).await;
revert(&mut node, id, 20, true).await;

// Test the case of revert id -> revert same id.
revert(&mut node, one, 5, true, Some(Duration::from_millis(500))).await;
revert(&mut node, one, 5, false, Some(Duration::from_millis(500))).await;
revert(&mut node, one, 5, true).await;
revert(&mut node, one, 5, false).await;

// Test reverting down to genesis.
revert(&mut node, zero, 0, true, Some(Duration::from_millis(500))).await;
revert(&mut node, zero, 0, true).await;
}

#[tokio::test(flavor = "multi_thread")]
Expand All @@ -202,7 +193,7 @@ async fn test_balances_and_txs_index_after_evm_revert() {
let mut node = TestNode::new(anvil_node_config.clone(), substrate_node_config).await.unwrap();

// Assert on initial best block number.
assert_block_number_is_best_and_finalized(&mut node, 0, None).await;
assert_block_number_is_best_and_finalized(&mut node, 0).await;

// Mine 5 blocks and assert on the new best block.
mine_blocks(&mut node, 5, 5).await;
Expand Down Expand Up @@ -262,7 +253,7 @@ async fn test_balances_and_txs_index_after_evm_revert() {
);

// Revert to a block before the transactions have been included.
revert(&mut node, zero, 5, true, Some(Duration::from_millis(500))).await;
revert(&mut node, zero, 5, true).await;

let after_revert_gas_price =
unwrap_response::<U256>(node.eth_rpc(EthRequest::EthGasPrice(())).await.unwrap()).unwrap();
Expand Down Expand Up @@ -383,7 +374,7 @@ async fn test_evm_revert_and_timestamp() {
);

// Revert to block number 2.
revert(&mut node, one, 2, true, None).await;
revert(&mut node, one, 2, true).await;
let seconds_ts_after_revert = node.get_decoded_timestamp(None).await;
assert_with_tolerance(
seconds_ts_after_revert.saturating_sub(second_timestamp),
Expand All @@ -405,7 +396,7 @@ async fn test_evm_revert_and_timestamp() {
);

// Revert to genesis block number.
revert(&mut node, zero, 0, true, None).await;
revert(&mut node, zero, 0, true).await;
let reverted_genesis_block_ts = node.get_decoded_timestamp(None).await;
assert_with_tolerance(
reverted_genesis_block_ts.saturating_div(1000),
Expand All @@ -431,22 +422,21 @@ async fn test_rollback() {
let mut node = TestNode::new(anvil_node_config.clone(), substrate_node_config).await.unwrap();

// Assert on initial best block number.
assert_block_number_is_best_and_finalized(&mut node, 0, None).await;
assert_block_number_is_best_and_finalized(&mut node, 0).await;

// Mine 5 blocks and assert on the new best block.
mine_blocks(&mut node, 5, 5).await;

// Rollback 2 blocks.
unwrap_response::<()>(node.eth_rpc(EthRequest::Rollback(Some(2))).await.unwrap()).unwrap();
assert_block_number_is_best_and_finalized(&mut node, 3, Some(Duration::from_millis(500))).await;
assert_block_number_is_best_and_finalized(&mut node, 3).await;

// Check mining works fine after reverting.
mine_blocks(&mut node, 10, 13).await;

// Rollback 1 block.
unwrap_response::<()>(node.eth_rpc(EthRequest::Rollback(None)).await.unwrap()).unwrap();
assert_block_number_is_best_and_finalized(&mut node, 12, Some(Duration::from_millis(500)))
.await;
assert_block_number_is_best_and_finalized(&mut node, 12).await;
}

#[tokio::test(flavor = "multi_thread")]
Expand All @@ -456,7 +446,7 @@ async fn test_mine_with_txs_in_mempool_before_revert() {
let mut node = TestNode::new(anvil_node_config.clone(), substrate_node_config).await.unwrap();

// Assert on initial best block number.
assert_block_number_is_best_and_finalized(&mut node, 0, None).await;
assert_block_number_is_best_and_finalized(&mut node, 0).await;

// Mine 5 blocks and assert on the new best block.
mine_blocks(&mut node, 5, 5).await;
Expand All @@ -481,7 +471,7 @@ async fn test_mine_with_txs_in_mempool_before_revert() {
let _ = node.send_transaction(transaction, None).await.unwrap();

// Revert to a block before the transactions have been sent.
revert(&mut node, zero, 5, true, None).await;
revert(&mut node, zero, 5, true).await;
let inspect: TxpoolInspect =
unwrap_response(node.eth_rpc(EthRequest::TxPoolInspect(())).await.unwrap()).unwrap();
assert_eq!(inspect.pending.len(), 2);
Expand Down Expand Up @@ -594,7 +584,7 @@ async fn test_timestmap_in_contract_after_revert() {
);

// Now check we got back to timestamp after the first block when reverting.
revert(&mut node, zero, 1, true, None).await;
revert(&mut node, zero, 1, true).await;
let timestamp = multicall_get_timestamp(&mut node, alith_addr, contract_address).await;
assert_with_tolerance(
U256::from(first_timestamp.saturating_div(1000)),
Expand Down
Loading