Skip to content

Commit

Permalink
STR-453: Rework strata-primitives Buf serialization to get rid of 0x …
Browse files Browse the repository at this point in the history
…prefix (#652)

* Rework strata-primitives::Buf serialization - get rid of FixedBytes usage and implement it manually.

* Minor fix in fn-tests to account for removal of 0x in hex serialization.

* Remove reth-primitives from the list of deps of strata-primitives.

* Review fixes.
  • Loading branch information
evgenyzdanovich authored Feb 4, 2025
1 parent a0d413d commit 8ec94e7
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 56 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion bin/prover-client/src/operators/cl_stf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl ClStfOperator {
pub async fn get_exec_id(&self, cl_block_id: L2BlockId) -> Result<Buf32, ProvingTaskError> {
let header = self.get_l2_block_header(cl_block_id).await?;
let block = self.evm_ee_operator.get_block(header.block_idx).await?;
Ok(block.header.hash.into())
Ok(Buf32(block.header.hash.into()))
}

/// Retrieves the specified number of ancestor block IDs for the given block ID.
Expand Down
5 changes: 3 additions & 2 deletions bin/prover-client/src/operators/evm_ee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ impl ProvingOp for EvmEeOperator {
if blkid == start_block_hash {
break;
} else {
blkid = Buf32::from(
blkid = Buf32(
self.get_block_header(blkid)
.await
.map_err(|e| ProvingTaskError::RpcError(e.to_string()))?
.parent_hash,
.parent_hash
.into(),
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/evmexec/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use borsh::BorshDeserialize;
use revm_primitives::B256;
use revm_primitives::{FixedBytes, B256};
use strata_primitives::evm_exec::EVMExtraPayload;
use strata_state::block::{L2Block, L2BlockBundle};
use thiserror::Error;
Expand All @@ -12,7 +12,7 @@ pub(crate) struct EVML2Block {

impl EVML2Block {
pub fn block_hash(&self) -> B256 {
self.extra_payload.block_hash().into()
FixedBytes(*self.extra_payload.block_hash().as_ref())
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/evmexec/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ mod tests {

let timestamp = 0;
let el_ops = vec![];
let safe_l1_block = FixedBytes::<32>::random().into();
let safe_l1_block = Buf32(FixedBytes::<32>::random().into());
let prev_l2_block = Buf32(FixedBytes::<32>::random().into()).into();

let payload_env = PayloadEnv::new(timestamp, prev_l2_block, safe_l1_block, el_ops);
Expand Down Expand Up @@ -570,7 +570,7 @@ mod tests {
let fcs = ForkchoiceState::default();

let el_payload = ElPayload {
base_fee_per_gas: FixedBytes::<32>::from(U256::from(10)).into(),
base_fee_per_gas: Buf32(FixedBytes::<32>::from(U256::from(10)).into()),
parent_hash: Default::default(),
fee_recipient: Default::default(),
state_root: Default::default(),
Expand Down
4 changes: 3 additions & 1 deletion crates/evmexec/src/fork_choice_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ pub fn fork_choice_state_initial<D: Database>(
.and_then(|state| state.sync())
.map(|sync_state| sync_state.chain_tip_blkid()),
)?
.unwrap_or(rollup_params.evm_genesis_block_hash.into());
.unwrap_or(revm_primitives::FixedBytes(
*rollup_params.evm_genesis_block_hash.as_ref(),
));

let finalized_block_hash = get_block_hash_by_id(
db.as_ref(),
Expand Down
3 changes: 1 addition & 2 deletions crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ bitcoin-bosd = { workspace = true, features = [
"arbitrary",
] }
borsh.workspace = true
const-hex = "1.14"
digest.workspace = true
hex.workspace = true
musig2 = { workspace = true, features = ["serde"] }
num_enum.workspace = true
rand = { workspace = true, optional = true }
reth-primitives.workspace = true
revm-primitives.workspace = true
secp256k1 = { workspace = true, optional = true }
serde.workspace = true
serde_json.workspace = true
Expand Down
22 changes: 19 additions & 3 deletions crates/primitives/src/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bitcoin::{
secp256k1::{schnorr, SecretKey, XOnlyPublicKey},
BlockHash, Txid, Wtxid,
};
use revm_primitives::alloy_primitives::hex;
use const_hex as hex;
#[cfg(feature = "zeroize")]
use zeroize::Zeroize;

Expand Down Expand Up @@ -237,7 +237,7 @@ mod tests {
fn test_buf32_serialization() {
assert_eq!(
serde_json::to_string(&Buf32::from([0; 32])).unwrap(),
String::from("\"0x0000000000000000000000000000000000000000000000000000000000000000\"")
String::from("\"0000000000000000000000000000000000000000000000000000000000000000\"")
);

assert_eq!(
Expand All @@ -246,7 +246,7 @@ mod tests {
1, 1, 1, 170u8
]))
.unwrap(),
String::from("\"0x01010101010101010101010101010101010101010101010101010101010101aa\"")
String::from("\"01010101010101010101010101010101010101010101010101010101010101aa\"")
);
}

Expand All @@ -263,4 +263,20 @@ mod tests {
assert_eq!(buf32, Buf32::from([0; 32]));
assert_eq!(buf64, Buf64::from([0; 64]));
}

#[test]
fn test_buf32_parse() {
"0x37ad61cff1367467a98cf7c54c4ac99e989f1fbb1bc1e646235e90c065c565ba"
.parse::<Buf32>()
.unwrap();
}

#[test]
fn test_buf32_from_str() {
Buf32::from_str("a9f913c3d7fe56c462228ad22bb7631742a121a6a138d57c1fc4a351314948fa")
.unwrap();

Buf32::from_str("81060cb3997dcefc463e3db0a776efb5360e458064a666459b8807f60c0201c2")
.unwrap();
}
}
5 changes: 1 addition & 4 deletions crates/primitives/src/l1/btc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use bitcoin::{
use bitcoin_bosd::Descriptor;
use borsh::{BorshDeserialize, BorshSerialize};
use rand::rngs::OsRng;
use revm_primitives::FixedBytes;
use serde::{de, Deserialize, Deserializer, Serialize};

use crate::{buf::Buf32, constants::HASH_SIZE, errors::ParseError};
Expand Down Expand Up @@ -736,9 +735,7 @@ impl XOnlyPk {
let pubkey_bytes = &script_pubkey.as_bytes()[2..34];
let output_key: XOnlyPublicKey = XOnlyPublicKey::from_slice(pubkey_bytes)?;

let serialized_key: FixedBytes<32> = output_key.serialize().into();

Ok(Self(Buf32(serialized_key.into())))
Ok(Self(Buf32(output_key.serialize())))
} else {
Err(ParseError::UnsupportedAddress(checked_addr.address_type()))
}
Expand Down
Loading

0 comments on commit 8ec94e7

Please sign in to comment.