Skip to content

Commit

Permalink
fix: add patch for moonbase block 2285348
Browse files Browse the repository at this point in the history
  • Loading branch information
RomarQ committed Nov 6, 2024
1 parent 24e7817 commit 9bcae62
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions node/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ thiserror = { workspace = true }
tracing = { workspace = true }
ansi_term = { workspace = true }

# Ethereum
ethereum = { workspace = true, features = [ "with-codec" ] }
ethereum-types = { workspace = true }

# Moonbeam
moonbeam-dev-rpc = { workspace = true }
moonbeam-cli-opt = { workspace = true }
Expand Down
18 changes: 18 additions & 0 deletions node/service/src/frontier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2024 Moonbeam foundation
// This file is part of Moonbeam.

// Moonbeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Moonbeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.

pub mod storage;
mod block_patches;
54 changes: 54 additions & 0 deletions node/service/src/frontier/block_patches.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2024 Moonbeam foundation
// This file is part of Moonbeam.

// Moonbeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Moonbeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.

//! This file aggregates ethereum specific block patches.
//!
//! Patches:
//!
//! [Moonbase]
//!
//! Block: 0xdadbbbfd8a7f177c466117a6f5635eff51d698d12cec9f5e2b360909621beb43
//! Description:
//! Block 2285348 includes transactions from the previous block.
//!
//! The problem was caused by this change in frontier, which moved `Pending::<T>::kill()`
//! from `on_initialize` to `on_finalize`: https://github.com/polkadot-evm/frontier/pull/638
//!
//! The bug was included with runtime 1603 in block 2285347.
//!
//! Github issue: https://github.com/moonbeam-foundation/moonbeam/issues/3026
//!
use sp_core::H256;

struct BlockPatch {
pub hash: H256,
pub invalid_transaction: Vec<H256>,
}

pub const MOONBASE_BLOCK_PATCHES: Vec<BlockPatch> = vec![
BlockPatch {
hash: hex_literal::hex!("dadbbbfd8a7f177c466117a6f5635eff51d698d12cec9f5e2b360909621beb43").into(),
invalid_transaction: vec![
hex_literal::hex!("006a6843eb35ad35a9ea9a99affa8d81f1ed500253c98cc9c080d84171a0afb3").into(),
hex_literal::hex!("64c102f664eb435206ad4fcb49b526722176bcf74801c79473c3b5b2c281a243").into(),
hex_literal::hex!("f546335453b6e35ce7e236ee873c96ba3a22602b3acc4f45f5d68b33a76d79ca").into(),
hex_literal::hex!("4ed713ccd474fc33d2022a802f064cc012e3e37cd22891d4a89c7ba3d776f2db").into(),
hex_literal::hex!("a5355f86844bb23fe666b10b509543fa377a9e324513eb221e0a2c926a64cae4").into(),
hex_literal::hex!("c14791a3a392018fc3438f39cac1d572e8baadd4ed350e0355d1ca874a169e6a").into()
]
}
];
101 changes: 101 additions & 0 deletions node/service/src/frontier/storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2024 Moonbeam foundation
// This file is part of Moonbeam.

// Moonbeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Moonbeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.

use block_patches::MOONBASE_BLOCK_PATCHES;

use std::sync::Arc;
use fc_rpc::{StorageOverride, StorageOverrideHandler};

use ethereum::{BlockV2, ReceiptV3};
use ethereum_types::{Address, H256, U256};
// Substrate
use sc_client_api::{backend::Backend, StorageProvider};
use sp_api::ProvideRuntimeApi;
use sp_runtime::{traits::Block as BlockT, Permill};
// Frontier
use fp_rpc::{EthereumRuntimeRPCApi, TransactionStatus};
use crate::frontier::block_patches;

/// A storage override for runtimes that use different ethereum schema.
///
/// It fetches data from the state backend, with some assumptions about pallet-ethereum's storage
/// schema, as a preference. However, if there is no ethereum schema in the state, it'll use the
/// runtime API as fallback implementation.
///
/// It is used to avoid spawning the runtime and the overhead associated with it.
#[derive(Clone)]
pub struct FrontierStorageOverrideHandler<B, C, BE> {
handler: StorageOverrideHandler<B, C, BE>,
}

impl<B, C, BE> FrontierStorageOverrideHandler<B, C, BE> {
pub fn new(client: Arc<C>) -> Self {
Self {
handler: StorageOverrideHandler::<B, C, BE>::new(client.clone())
}
}
}

impl<B, C, BE> StorageOverride<B> for FrontierStorageOverrideHandler<B, C, BE>
where
B: BlockT,
C: ProvideRuntimeApi<B>,
C::Api: EthereumRuntimeRPCApi<B>,
C: StorageProvider<B, BE> + Send + Sync + 'static,
BE: Backend<B> + 'static,
{
fn account_code_at(&self, at: B::Hash, address: Address) -> Option<Vec<u8>> {
self.handler.account_code_at(at, address)
}

fn account_storage_at(&self, at: B::Hash, address: Address, index: U256) -> Option<H256> {
self.handler.account_storage_at(at, address, index)
}

fn current_block(&self, at: B::Hash) -> Option<BlockV2> {
let block = self.handler.current_block(at);
block.map(|mut block| {
for patch in MOONBASE_BLOCK_PATCHES {
if block.header.hash() == patch.hash {
block.transactions = block.transactions.iter().filter_map(|tx| {
if patch.invalid_transaction.contains(&tx.hash()) {
None
} else {
Some(tx.clone())
}
}).collect();
}
}
block
})
}

fn current_receipts(&self, at: B::Hash) -> Option<Vec<ReceiptV3>> {
self.handler.current_receipts(at)
}

fn current_transaction_statuses(&self, at: B::Hash) -> Option<Vec<TransactionStatus>> {
self.handler.current_transaction_statuses(at)
}

fn elasticity(&self, at: B::Hash) -> Option<Permill> {
self.handler.elasticity(at)
}

fn is_eip1559(&self, at: B::Hash) -> bool {
self.handler.is_eip1559(at)
}
}
4 changes: 2 additions & 2 deletions node/service/src/lazy_loading/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use cumulus_client_parachain_inherent::{MockValidationDataInherentDataProvider,
use cumulus_primitives_core::{relay_chain, BlockT, ParaId};
use cumulus_primitives_parachain_inherent::ParachainInherentData;
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
use fc_rpc::StorageOverrideHandler;
use fc_rpc_core::types::{FeeHistoryCache, FilterPool};
use futures::{FutureExt, StreamExt};
use moonbeam_cli_opt::{EthApi as EthApiCmd, LazyLoadingConfig, RpcConfig};
Expand Down Expand Up @@ -56,6 +55,7 @@ use std::collections::BTreeMap;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use crate::frontier::storage::FrontierStorageOverrideHandler;

pub mod backend;
pub mod call_executor;
Expand Down Expand Up @@ -489,7 +489,7 @@ where
}

let prometheus_registry = config.prometheus_registry().cloned();
let overrides = Arc::new(StorageOverrideHandler::new(client.clone()));
let overrides = Arc::new(FrontierStorageOverrideHandler::new(client.clone()));
let fee_history_limit = rpc_config.fee_history_limit;
let mut command_sink = None;
let mut dev_rpc_data = None;
Expand Down
9 changes: 5 additions & 4 deletions node/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ use cumulus_relay_chain_interface::{OverseerHandle, RelayChainInterface, RelayCh
use cumulus_relay_chain_minimal_node::build_minimal_relay_chain_node_with_rpc;
use fc_consensus::FrontierBlockImport as TFrontierBlockImport;
use fc_db::DatabaseSource;
use fc_rpc::StorageOverrideHandler;
use fc_rpc_core::types::{FeeHistoryCache, FilterPool};
use futures::{FutureExt, StreamExt};
use maplit::hashmap;
Expand Down Expand Up @@ -88,6 +87,7 @@ pub mod chain_spec;
mod client;
#[cfg(feature = "lazy-loading")]
pub mod lazy_loading;
mod frontier;

type FullClient<RuntimeApi> = TFullClient<Block, RuntimeApi, WasmExecutor<HostFunctions>>;
type FullBackend = TFullBackend<Block>;
Expand Down Expand Up @@ -326,7 +326,7 @@ where
thread_count,
cache_size,
} => {
let overrides = Arc::new(StorageOverrideHandler::new(client.clone()));
let overrides = Arc::new(FrontierStorageOverrideHandler::new(client.clone()));
let sqlite_db_path = frontier_database_dir(config, "sql");
std::fs::create_dir_all(&sqlite_db_path).expect("failed creating sql db directory");
let backend = futures::executor::block_on(fc_db::sql::Backend::new(
Expand All @@ -353,6 +353,7 @@ where
}

use sp_runtime::{traits::BlakeTwo256, DigestItem, Percent};
use crate::frontier::storage::FrontierStorageOverrideHandler;

pub const SOFT_DEADLINE_PERCENT: Percent = Percent::from_percent(100);

Expand Down Expand Up @@ -694,7 +695,7 @@ where
})
.await?;

let overrides = Arc::new(StorageOverrideHandler::new(client.clone()));
let overrides = Arc::new(FrontierStorageOverrideHandler::new(client.clone()));
let fee_history_limit = rpc_config.fee_history_limit;

// Sinks for pubsub notifications.
Expand Down Expand Up @@ -1238,7 +1239,7 @@ where
}

let prometheus_registry = config.prometheus_registry().cloned();
let overrides = Arc::new(StorageOverrideHandler::new(client.clone()));
let overrides = Arc::new(FrontierStorageOverrideHandler::new(client.clone()));
let fee_history_limit = rpc_config.fee_history_limit;
let mut command_sink = None;
let mut dev_rpc_data = None;
Expand Down

0 comments on commit 9bcae62

Please sign in to comment.