Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/p2p #262

Merged
merged 3 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ The address of the entry point smart contract is the same on all EVM networks.
| :--------: | :-------: | :-------: | :-------: |
| [0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789](https://blockscan.com/address/0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789) | 0.6.0 | [9b5f2e4](https://github.com/eth-infinitism/account-abstraction/commit/9b5f2e4bb30a81aa30761749d9e2e43fee64c768) | [April 2023](https://blog.openzeppelin.com/eip-4337-ethereum-account-abstraction-incremental-audit)

## Paymasters and account factories

You can find a list of many paymasters and account factories [here](https://docs.google.com/spreadsheets/d/1QJEYDOr-AMD2bNAoupfjQJYJabFgdb2TRSyekdIfquM/edit#gid=0).

## Examples

To get started, check the examples [here](./examples/). More examples will be added in the future.
Expand Down
4 changes: 2 additions & 2 deletions bin/silius/src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ethers::types::{Address, U256};
use expanded_pathbuf::ExpandedPathBuf;
use silius_p2p::config::{Config, ListenAddr};
use silius_primitives::{
bundler::SendBundleMode,
bundler::{SendBundleMode, DEFAULT_BUNDLE_INTERVAL},
consts::{
networking::{
DEFAULT_BUNDLER_GRPC_PORT, DEFAULT_HTTP_RPC_PORT, DEFAULT_UOPOOL_GRPC_PORT,
Expand Down Expand Up @@ -56,7 +56,7 @@ pub struct BundlerArgs {
/// The bundle interval in seconds.
///
/// By default the interval time is set to 10
#[clap(long, default_value_t = 10)]
#[clap(long, default_value_t = DEFAULT_BUNDLE_INTERVAL)]
pub bundle_interval: u64,

/// Sets the send bundle mode.
Expand Down
1 change: 1 addition & 0 deletions crates/p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ futures = "0.3.28"
futures-bounded = "0.2.0"
lazy_static = { workspace = true }
libp2p-mplex = { version = "0.40.0" }
sha2 = "0.10.8"
silius-primitives = { path = "../primitives" }
snap = "1"
ssz_rs = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions crates/p2p/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl Behaviour {
let reqrep = request_response::Behaviour::new(Default::default());
let discovery = Discovery::new(enr, key, config)?;
let peer_manager = PeerManager::new(ping_interval, target_peers);

Ok(Self {
gossipsub,
reqrep,
Expand Down
30 changes: 27 additions & 3 deletions crates/p2p/src/gossipsub.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use libp2p::gossipsub::{
Behaviour, ConfigBuilder, DataTransform, IdentTopic, Message, MessageAuthenticity, RawMessage,
TopicHash, ValidationMode, WhitelistSubscriptionFilter,
Behaviour, ConfigBuilder, DataTransform, IdentTopic, Message, MessageAuthenticity, MessageId,
RawMessage, TopicHash, ValidationMode, WhitelistSubscriptionFilter,
};
use sha2::{Digest, Sha256};
use silius_primitives::consts::p2p::{
MAX_GOSSIP_SNAP_SIZE, SSZ_SNAPPY_ENCODING, TOPIC_PREFIX, USER_OPS_WITH_ENTRY_POINT_TOPIC,
MAX_GOSSIP_SNAP_SIZE, MESSAGE_DOMAIN_VALID_SNAPPY, SSZ_SNAPPY_ENCODING, TOPIC_PREFIX,
USER_OPS_WITH_ENTRY_POINT_TOPIC,
};
use snap::raw::{decompress_len, Decoder, Encoder};
use std::{
Expand All @@ -27,6 +29,7 @@ impl SnappyTransform {
}
}
}

impl Default for SnappyTransform {
fn default() -> Self {
SnappyTransform {
Expand Down Expand Up @@ -93,11 +96,31 @@ pub fn topic(mempool_id: &str) -> IdentTopic {
))
}

pub fn message_id_fn(message: &Message) -> MessageId {
let topic_bytes = message.topic.as_str().as_bytes();
let topic_len_bytes = topic_bytes.len().to_le_bytes();

let mut vec: Vec<u8> = Vec::with_capacity(
MESSAGE_DOMAIN_VALID_SNAPPY.len()
+ topic_len_bytes.len()
+ topic_bytes.len()
+ message.data.len(),
);
vec.extend_from_slice(&MESSAGE_DOMAIN_VALID_SNAPPY);
vec.extend_from_slice(&topic_len_bytes);
vec.extend_from_slice(topic_bytes);
vec.extend_from_slice(&message.data);

Sha256::digest(vec)[..20].into()
}

/// Creates a gossipsub instance with the given mempool ids
pub fn create_gossisub(mempool_ids: Vec<String>) -> Result<Gossipsub, &'static str> {
let filter = create_whitelist_filter(mempool_ids.clone());
let gs_config = ConfigBuilder::default()
.validate_messages()
.validation_mode(ValidationMode::Anonymous)
.message_id_fn(message_id_fn)
.build()?;
let snappy_transform = SnappyTransform::new(MAX_GOSSIP_SNAP_SIZE);
let mut gossipsub = Gossipsub::new_with_subscription_filter_and_transform(
Expand All @@ -112,5 +135,6 @@ pub fn create_gossisub(mempool_ids: Vec<String>) -> Result<Gossipsub, &'static s
.subscribe(&topic(&mempool_id))
.map_err(|_| "subscribe error")?;
}

Ok(gossipsub)
}
4 changes: 4 additions & 0 deletions crates/primitives/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,8 @@ pub mod p2p {
pub const SSZ_SNAPPY_ENCODING: &str = "ssz_snappy";
/// The maximum size of a gossipsub message
pub const MAX_GOSSIP_SNAP_SIZE: usize = 1048576; // bytes
/// 4-byte domain for gossip message-id isolation of *invalid* snappy messages
pub const MESSAGE_DOMAIN_INVALID_SNAPPY: [u8; 4] = [0, 0, 0, 0];
/// 4-byte domain for gossip message-id isolation of *valid* snappy messages
pub const MESSAGE_DOMAIN_VALID_SNAPPY: [u8; 4] = [1, 0, 0, 0];
}
47 changes: 6 additions & 41 deletions crates/uopool/src/validate/sanity/call_gas.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use crate::{
mempool::{Mempool, UserOperationAct, UserOperationAddrAct, UserOperationCodeHashAct},
reputation::{HashSetOp, ReputationEntryOp},
utils::calculate_call_gas_limit,
validate::{SanityCheck, SanityHelper},
Reputation,
};
use ethers::{providers::Middleware, types::BlockNumber};
use silius_contracts::entry_point::EntryPointErr;
use ethers::{providers::Middleware, types::U256};
use silius_primitives::{sanity::SanityCheckError, UserOperation};

#[derive(Clone)]
Expand All @@ -27,7 +25,7 @@ impl<M: Middleware> SanityCheck<M> for CallGas {
uo: &UserOperation,
_mempool: &Mempool<T, Y, X, Z>,
_reputation: &Reputation<H, R>,
helper: &SanityHelper<M>,
_helper: &SanityHelper<M>,
) -> Result<(), SanityCheckError>
where
T: UserOperationAct,
Expand All @@ -37,43 +35,10 @@ impl<M: Middleware> SanityCheck<M> for CallGas {
H: HashSetOp,
R: ReputationEntryOp,
{
let exec_res = match helper.entry_point.simulate_handle_op(uo.clone()).await {
Ok(res) => res,
Err(err) => {
return Err(match err {
EntryPointErr::FailedOp(f) => {
SanityCheckError::Validation { message: f.reason }
}
_ => SanityCheckError::UnknownError {
message: format!("{err:?}"),
},
})
}
};

let block = helper
.entry_point
.eth_client()
.get_block(BlockNumber::Latest)
.await
.map_err(|err| SanityCheckError::UnknownError {
message: err.to_string(),
})?
.ok_or(SanityCheckError::UnknownError {
message: "No block found".to_string(),
})?;
let base_fee_per_gas = block
.base_fee_per_gas
.ok_or(SanityCheckError::UnknownError {
message: "No base fee".to_string(),
})?;

let call_gas_limit = calculate_call_gas_limit(
exec_res.paid,
exec_res.pre_op_gas,
uo.max_fee_per_gas
.min(uo.max_priority_fee_per_gas + base_fee_per_gas),
);
// call gas limit is at least the cost of a CALL with non-zero value
// https://github.com/wolflo/evm-opcodes/blob/main/gas.md#aa-1-call
// gas_cost = 100 + 9000
let call_gas_limit = U256::from(9100);

if uo.call_gas_limit >= call_gas_limit {
return Ok(());
Expand Down
4 changes: 2 additions & 2 deletions docs/P2P.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ cargo run -- node --eth-client-address http://127.0.0.1:8545 --mnemonic-file ./b
Run first peer node

```
cargo run -- node --eth-client-address http://127.0.0.1:8545 --mnemonic-file ./bundler-spec-tests/keys/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789 --http --http.port 4001 --eth-client-proxy-address http://127.0.0.1:8545 --p2p.baddr 127.0.0.1 --bootnodes "enr:-Iu4QBh2tesC8BokO61v1w43MnbfHF5H95ZJNHVEQaRq_MjFFuxmeVQnoEXxUDk5qKJCHM944gC72Xg4dYwRkGt9zA4BgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQKRdyIA8OvArCcZbt3hoJHu4nVe6CblqjO0CnrbGACi-IN0Y3CCEPGDdWRwghDx" --enable-p2p --discovery.port 4338 --p2p.port 4338 --datadir ./.local/node1
cargo run -- node --eth-client-address http://127.0.0.1:8545 --mnemonic-file ./bundler-spec-tests/keys/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789 --uopool.port 3004 --bundler.port 3005 --http --http.port 4001 --eth-client-proxy-address http://127.0.0.1:8545 --p2p.baddr 127.0.0.1 --bootnodes "enr:-Iu4QBh2tesC8BokO61v1w43MnbfHF5H95ZJNHVEQaRq_MjFFuxmeVQnoEXxUDk5qKJCHM944gC72Xg4dYwRkGt9zA4BgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQKRdyIA8OvArCcZbt3hoJHu4nVe6CblqjO0CnrbGACi-IN0Y3CCEPGDdWRwghDx" --enable-p2p --discovery.port 4338 --p2p.port 4338 --datadir ./.local/node1
```

Run second peer node

```
cargo run -- node --eth-client-address http://127.0.0.1:8545 --mnemonic-file ./bundler-spec-tests/keys/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789 --http --http.port 4002 --eth-client-proxy-address http://127.0.0.1:8545 --p2p.baddr 127.0.0.1 --bootnodes "enr:-Iu4QBh2tesC8BokO61v1w43MnbfHF5H95ZJNHVEQaRq_MjFFuxmeVQnoEXxUDk5qKJCHM944gC72Xg4dYwRkGt9zA4BgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQKRdyIA8OvArCcZbt3hoJHu4nVe6CblqjO0CnrbGACi-IN0Y3CCEPGDdWRwghDx" --enable-p2p --discovery.port 4339 --p2p.port 4339 --datadir ./.local/node2
cargo run -- node --eth-client-address http://127.0.0.1:8545 --mnemonic-file ./bundler-spec-tests/keys/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --beneficiary 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --entry-points 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789 --uopool.port 3006 --bundler.port 3007 --http --http.port 4002 --eth-client-proxy-address http://127.0.0.1:8545 --p2p.baddr 127.0.0.1 --bootnodes "enr:-Iu4QBh2tesC8BokO61v1w43MnbfHF5H95ZJNHVEQaRq_MjFFuxmeVQnoEXxUDk5qKJCHM944gC72Xg4dYwRkGt9zA4BgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQKRdyIA8OvArCcZbt3hoJHu4nVe6CblqjO0CnrbGACi-IN0Y3CCEPGDdWRwghDx" --enable-p2p --discovery.port 4339 --p2p.port 4339 --datadir ./.local/node2
```
Loading