Skip to content

Commit c86e37d

Browse files
committed
fix: tests pass again
1 parent 3081915 commit c86e37d

File tree

5 files changed

+94
-26
lines changed

5 files changed

+94
-26
lines changed

crates/block-processor/src/alias.rs

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use alloy::{
44
};
55
use eyre::OptionExt;
66
use reth::providers::{StateProvider, StateProviderFactory};
7+
use std::sync::{Arc, Mutex};
78

89
/// Simple trait to allow checking if an address should be aliased.
910
pub trait AliasOracle {
@@ -33,33 +34,67 @@ impl AliasOracle for Box<dyn StateProvider> {
3334
}
3435
}
3536

36-
impl AliasOracle for &HashSet<Address> {
37+
impl AliasOracle for HashSet<Address> {
3738
fn should_alias(&self, address: Address) -> eyre::Result<bool> {
3839
Ok(self.contains(&address))
3940
}
4041
}
4142

4243
/// Factory trait to create new [`AliasOracle`] instances.
44+
///
45+
/// The default implementation on `Box<dyn StateProviderFactory>` creates
46+
/// [`AliasOracle`] instances backed by the state provider for a given block
47+
/// height. It will error if that state provider cannot be obtained.
48+
///
49+
/// This trait is primarily intended to allow injecting test implementations
50+
/// of [`AliasOracle`] into the Signet Node for testing purposes. The test
51+
/// implementation on [`HashSet<Address>`] allows specifying a fixed set of
52+
/// addresses to be aliased.
4353
pub trait AliasOracleFactory: Send + Sync + 'static {
4454
/// The [`AliasOracle`] type.
45-
type Oracle<'a>: AliasOracle;
55+
type Oracle: AliasOracle;
4656

4757
/// Create a new [`AliasOracle`].
48-
fn create(&self, block_height: u64) -> eyre::Result<Self::Oracle<'_>>;
58+
fn create(&self, block_height: u64) -> eyre::Result<Self::Oracle>;
4959
}
5060

5161
impl AliasOracleFactory for Box<dyn StateProviderFactory> {
52-
type Oracle<'a> = Box<dyn StateProvider>;
62+
type Oracle = Box<dyn StateProvider>;
5363

54-
fn create(&self, block_height: u64) -> eyre::Result<Self::Oracle<'_>> {
64+
fn create(&self, block_height: u64) -> eyre::Result<Self::Oracle> {
5565
self.state_by_block_number_or_tag(block_height.into()).map_err(Into::into)
5666
}
5767
}
5868

69+
/// This implementation is primarily for testing purposes.
5970
impl AliasOracleFactory for HashSet<Address> {
60-
type Oracle<'a> = &'a HashSet<Address>;
71+
type Oracle = HashSet<Address>;
6172

62-
fn create(&self, _block_height: u64) -> eyre::Result<Self::Oracle<'_>> {
63-
Ok(self)
73+
fn create(&self, _block_height: u64) -> eyre::Result<Self::Oracle> {
74+
Ok(self.clone())
75+
}
76+
}
77+
78+
impl<T> AliasOracleFactory for Mutex<T>
79+
where
80+
T: AliasOracleFactory,
81+
{
82+
type Oracle = T::Oracle;
83+
84+
fn create(&self, block_height: u64) -> eyre::Result<Self::Oracle> {
85+
let guard =
86+
self.lock().map_err(|_| eyre::eyre!("failed to lock AliasOracleFactory mutex"))?;
87+
guard.create(block_height)
88+
}
89+
}
90+
91+
impl<T> AliasOracleFactory for Arc<T>
92+
where
93+
T: AliasOracleFactory,
94+
{
95+
type Oracle = T::Oracle;
96+
97+
fn create(&self, block_height: u64) -> eyre::Result<Self::Oracle> {
98+
self.as_ref().create(block_height)
6499
}
65100
}

crates/node-tests/src/context.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use alloy::{
88
consensus::{BlockHeader, TxEnvelope, constants::ETH_TO_WEI},
99
genesis::{Genesis, GenesisAccount},
1010
network::{Ethereum, EthereumWallet, TransactionBuilder as _},
11-
primitives::{Address, I256, Sign, U256, keccak256},
11+
primitives::{Address, I256, Sign, U256, keccak256, map::HashSet},
1212
providers::{
1313
Provider as _, ProviderBuilder, SendableTx,
1414
fillers::{BlobGasFiller, SimpleNonceManager},
@@ -31,7 +31,7 @@ use signet_test_utils::contracts::counter::COUNTER_DEPLOY_CODE;
3131
use signet_types::constants::{HostPermitted, RollupPermitted, SignetSystemConstants};
3232
use signet_zenith::{HostOrders::OrdersInstance, RollupPassage::RollupPassageInstance};
3333
use std::sync::{
34-
Arc,
34+
Arc, Mutex,
3535
atomic::{AtomicU64, Ordering},
3636
};
3737
use tokio::{sync::watch, task::JoinHandle};
@@ -74,7 +74,10 @@ pub struct SignetTestContext {
7474
/// The current host block height
7575
pub height: AtomicU64,
7676

77-
/// Test addreses, copied from [`signet_types::test_utils::TEST_USERS`] for
77+
/// The alias oracle used by the Signet Node instance.
78+
pub alias_oracle: Arc<Mutex<HashSet<Address>>>,
79+
80+
/// Test addresses, copied from [`signet_types::test_utils::TEST_USERS`] for
7881
/// convenience
7982
pub addresses: [Address; 10],
8083
}
@@ -99,9 +102,17 @@ impl SignetTestContext {
99102

100103
let factory = create_test_provider_factory_with_chain_spec(chain_spec.clone());
101104

105+
let alias_oracle: Arc<Mutex<HashSet<Address>>> = Arc::new(Mutex::new(HashSet::default()));
106+
102107
// instantiate Signet Node, booting rpc
103-
let (node, mut node_status) =
104-
SignetNode::new(ctx, cfg.clone(), factory.clone(), Default::default()).unwrap();
108+
let (node, mut node_status) = SignetNode::new(
109+
ctx,
110+
cfg.clone(),
111+
factory.clone(),
112+
Arc::clone(&alias_oracle),
113+
Default::default(),
114+
)
115+
.unwrap();
105116

106117
// Spawn the node, and wait for it to indicate RPC readiness.
107118
let node = tokio::spawn(node.start());
@@ -152,12 +163,24 @@ impl SignetTestContext {
152163
constants,
153164
height: AtomicU64::new(cfg.constants().unwrap().host_deploy_height()),
154165

166+
alias_oracle,
155167
addresses,
156168
};
157169

158170
(this, node)
159171
}
160172

173+
/// Set whether an address should be aliased. This will be propagated to
174+
/// the running node.
175+
pub fn set_should_alias(&self, address: Address, should_alias: bool) {
176+
let mut guard = self.alias_oracle.lock().expect("failed to lock alias oracle mutex");
177+
if should_alias {
178+
guard.insert(address);
179+
} else {
180+
guard.remove(&address);
181+
}
182+
}
183+
161184
/// Clone the Signet system constants
162185
pub fn constants(&self) -> SignetSystemConstants {
163186
self.constants.clone()

crates/node-tests/tests/db.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use alloy::primitives::hex;
1+
use alloy::primitives::{Address, hex, map::HashSet};
22
use reth::providers::BlockReader;
33
use serial_test::serial;
44
use signet_node::SignetNode;
@@ -17,7 +17,14 @@ async fn test_genesis() {
1717
assert_eq!(chain_spec.genesis().config.chain_id, consts.unwrap().ru_chain_id());
1818

1919
let factory = create_test_provider_factory_with_chain_spec(chain_spec.clone());
20-
let (_, _) = SignetNode::new(ctx, cfg.clone(), factory.clone(), Default::default()).unwrap();
20+
let (_, _) = SignetNode::<_, _, HashSet<Address>>::new(
21+
ctx,
22+
cfg.clone(),
23+
factory.clone(),
24+
Default::default(),
25+
Default::default(),
26+
)
27+
.unwrap();
2128

2229
let genesis_block = factory.provider().unwrap().block_by_number(0).unwrap().unwrap();
2330

crates/node/src/node.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use reth_db_common::init;
2222
use reth_exex::{ExExContext, ExExEvent, ExExHead, ExExNotificationsStream};
2323
use reth_node_api::{FullNodeComponents, FullNodeTypes, NodeTypes};
2424
use signet_blobber::BlobFetcher;
25-
use signet_block_processor::SignetBlockProcessorV1;
25+
use signet_block_processor::{AliasOracleFactory, SignetBlockProcessorV1};
2626
use signet_db::{DbProviderExt, RuChain, RuWriter};
2727
use signet_node_config::SignetNodeConfig;
2828
use signet_node_types::{NodeStatus, NodeTypesDbTrait, SignetNodeTypes};
@@ -42,7 +42,7 @@ type ExExNotification<Host> = reth_exex::ExExNotification<PrimitivesOf<Host>>;
4242
type Chain<Host> = reth::providers::Chain<PrimitivesOf<Host>>;
4343

4444
/// Signet context and configuration.
45-
pub struct SignetNode<Host, Db>
45+
pub struct SignetNode<Host, Db, AliasOracle = Box<dyn StateProviderFactory>>
4646
where
4747
Host: FullNodeComponents,
4848
Host::Types: NodeTypes<Primitives = EthPrimitives>,
@@ -72,13 +72,13 @@ where
7272
pub(crate) status: watch::Sender<NodeStatus>,
7373

7474
/// The block processor
75-
pub(crate) processor: SignetBlockProcessorV1<Db>,
75+
pub(crate) processor: SignetBlockProcessorV1<Db, AliasOracle>,
7676

7777
/// A reqwest client, used by the blob fetch and the tx cache forwarder.
7878
pub(crate) client: reqwest::Client,
7979
}
8080

81-
impl<Host, Db> fmt::Debug for SignetNode<Host, Db>
81+
impl<Host, Db, AliasOracle> fmt::Debug for SignetNode<Host, Db, AliasOracle>
8282
where
8383
Host: FullNodeComponents,
8484
Host::Types: NodeTypes<Primitives = EthPrimitives>,
@@ -89,7 +89,7 @@ where
8989
}
9090
}
9191

92-
impl<Host, Db> NodePrimitivesProvider for SignetNode<Host, Db>
92+
impl<Host, Db, AliasOracle> NodePrimitivesProvider for SignetNode<Host, Db, AliasOracle>
9393
where
9494
Host: FullNodeComponents,
9595
Host::Types: NodeTypes<Primitives = EthPrimitives>,
@@ -98,22 +98,24 @@ where
9898
type Primitives = EthPrimitives;
9999
}
100100

101-
impl<Host, Db> CanonStateSubscriptions for SignetNode<Host, Db>
101+
impl<Host, Db, AliasOracle> CanonStateSubscriptions for SignetNode<Host, Db, AliasOracle>
102102
where
103103
Host: FullNodeComponents,
104104
Host::Types: NodeTypes<Primitives = EthPrimitives>,
105105
Db: NodeTypesDbTrait,
106+
AliasOracle: AliasOracleFactory,
106107
{
107108
fn subscribe_to_canonical_state(&self) -> CanonStateNotifications<Self::Primitives> {
108109
self.bp.subscribe_to_canonical_state()
109110
}
110111
}
111112

112-
impl<Host, Db> SignetNode<Host, Db>
113+
impl<Host, Db, AliasOracle> SignetNode<Host, Db, AliasOracle>
113114
where
114115
Host: FullNodeComponents,
115116
Host::Types: NodeTypes<Primitives = EthPrimitives>,
116117
Db: NodeTypesDbTrait,
118+
AliasOracle: AliasOracleFactory,
117119
{
118120
/// Create a new Signet instance.
119121
///
@@ -124,6 +126,7 @@ where
124126
ctx: ExExContext<Host>,
125127
config: SignetNodeConfig,
126128
factory: ProviderFactory<SignetNodeTypes<Db>>,
129+
alias_oracle: AliasOracle,
127130
client: reqwest::Client,
128131
) -> eyre::Result<(Self, tokio::sync::watch::Receiver<NodeStatus>)> {
129132
let constants =
@@ -174,13 +177,11 @@ where
174177
.wrap_err("failed to create blob cacher")?
175178
.spawn();
176179

177-
let bdsp: Box<dyn StateProviderFactory> = Box::new(ctx.provider().clone());
178-
179180
let processor = SignetBlockProcessorV1::new(
180181
constants.clone(),
181182
config.chain_spec().clone(),
182183
factory.clone(),
183-
bdsp,
184+
alias_oracle,
184185
config.slot_calculator(),
185186
blob_cacher,
186187
);

crates/node/src/rpc.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
use crate::SignetNode;
22
use reth::{primitives::EthPrimitives, rpc::builder::config::RethRpcServerConfig};
33
use reth_node_api::{FullNodeComponents, NodeTypes};
4+
use signet_block_processor::AliasOracleFactory;
45
use signet_node_types::NodeTypesDbTrait;
56
use signet_rpc::{RpcCtx, RpcServerGuard, ServeConfig};
67
use signet_tx_cache::client::TxCache;
78
use tracing::info;
89

9-
impl<Host, Db> SignetNode<Host, Db>
10+
impl<Host, Db, AliasOracle> SignetNode<Host, Db, AliasOracle>
1011
where
1112
Host: FullNodeComponents,
1213
Host::Types: NodeTypes<Primitives = EthPrimitives>,
1314
Db: NodeTypesDbTrait,
15+
AliasOracle: AliasOracleFactory,
1416
{
1517
/// Start the RPC server.
1618
pub async fn start_rpc(&mut self) -> eyre::Result<()> {

0 commit comments

Comments
 (0)