Skip to content

Commit fe48ae2

Browse files
fix: make salt dependency optional to support no_std compilation
The salt crate enables serde/std by default, which breaks no_std compilation for riscv64imac-unknown-none-elf target. This commit makes salt optional and only enables it with the std feature. Changes: - Make salt dependency optional in Cargo.toml - Enable salt only when std feature is active - Add conditional compilation for salt::state::hasher usage - In no_std environments, bucket_id_for_account and bucket_id_for_slot panic if not overridden, preventing incorrect bucket ID calculations - Mark ADDRESS_KEY_LEN, STORAGE_SLOT_LEN, STORAGE_KEY_LEN as std-only The panic behavior ensures that no_std users must provide their own bucket ID implementation, preventing silent correctness issues from using a different hashing algorithm than the std version. Verified: - cargo check --target riscv64imac-unknown-none-elf --no-default-features ✓ - cargo check -p mega-evm ✓ - cargo test ✓ Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 19487b6 commit fe48ae2

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

crates/mega-evm/Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ revm = { workspace = true, features = ["dev", "serde", "kzg-rs"] }
3030

3131
# megaeth
3232
mega-system-contracts = { workspace = true, default-features = false }
33-
salt = { workspace = true, features = [] }
33+
salt = { workspace = true, features = [], optional = true }
3434

3535
# misc
3636
auto_impl.workspace = true
@@ -53,7 +53,13 @@ sha2.workspace = true
5353

5454
[features]
5555
default = ["std", "revm/default", "op-revm/default"]
56-
std = ["revm/std", "op-revm/std", "revm/alloydb", "mega-system-contracts/std"]
56+
std = [
57+
"revm/std",
58+
"op-revm/std",
59+
"revm/alloydb",
60+
"mega-system-contracts/std",
61+
"dep:salt",
62+
]
5763
test-utils = []
5864

5965
[[bench]]

crates/mega-evm/src/external/salt.rs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ use core::{
77
fmt::{Debug, Display},
88
};
99

10-
use alloy_primitives::{Address, B256, U256};
10+
#[cfg(feature = "std")]
11+
use alloy_primitives::B256;
12+
use alloy_primitives::{Address, U256};
1113
use auto_impl::auto_impl;
14+
15+
#[cfg(feature = "std")]
1216
use salt::state::hasher;
1317

18+
#[cfg(feature = "std")]
1419
const ADDRESS_KEY_LEN: usize = Address::len_bytes();
20+
#[cfg(feature = "std")]
1521
const STORAGE_SLOT_LEN: usize = B256::len_bytes();
22+
#[cfg(feature = "std")]
1623
const STORAGE_KEY_LEN: usize = ADDRESS_KEY_LEN + STORAGE_SLOT_LEN;
1724

1825
/// SALT bucket identifier. Accounts and storage slots are mapped to buckets, which have
@@ -78,8 +85,23 @@ pub trait SaltEnv: Debug + Unpin {
7885
/// # Arguments
7986
///
8087
/// * `account` - The account address to map
88+
///
89+
/// # Panics
90+
///
91+
/// Panics in `no_std` environments if not overridden, as the default implementation
92+
/// requires the `salt` crate which is only available with the `std` feature.
8193
fn bucket_id_for_account(account: Address) -> BucketId {
82-
hasher::bucket_id(account.as_slice())
94+
#[cfg(feature = "std")]
95+
{
96+
hasher::bucket_id(account.as_slice())
97+
}
98+
#[cfg(not(feature = "std"))]
99+
{
100+
unimplemented!(
101+
"bucket_id_for_account({:?}) requires std feature or must be overridden in no_std environments",
102+
account
103+
)
104+
}
83105
}
84106

85107
/// Maps a storage slot to its bucket ID.
@@ -91,10 +113,26 @@ pub trait SaltEnv: Debug + Unpin {
91113
///
92114
/// * `address` - The contract address owning the storage
93115
/// * `key` - The storage slot key
116+
///
117+
/// # Panics
118+
///
119+
/// Panics in `no_std` environments if not overridden, as the default implementation
120+
/// requires the `salt` crate which is only available with the `std` feature.
94121
fn bucket_id_for_slot(address: Address, key: U256) -> BucketId {
95-
hasher::bucket_id(
96-
address.concat_const::<STORAGE_SLOT_LEN, STORAGE_KEY_LEN>(key.into()).as_slice(),
97-
)
122+
#[cfg(feature = "std")]
123+
{
124+
hasher::bucket_id(
125+
address.concat_const::<STORAGE_SLOT_LEN, STORAGE_KEY_LEN>(key.into()).as_slice(),
126+
)
127+
}
128+
#[cfg(not(feature = "std"))]
129+
{
130+
unimplemented!(
131+
"bucket_id_for_slot({:?}, {:?}) requires std feature or must be overridden in no_std environments",
132+
address,
133+
key
134+
)
135+
}
98136
}
99137
}
100138

0 commit comments

Comments
 (0)