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

Convert tx hash bytes into hex #340

Merged
merged 10 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
26 changes: 23 additions & 3 deletions bin/citrea/src/test_rpc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::marker::PhantomData;

use hex::ToHex;
use proptest::prelude::any_with;
use proptest::strategy::Strategy;
use proptest::{prop_compose, proptest};
Expand Down Expand Up @@ -315,7 +316,7 @@ fn test_get_batches() {
fn test_get_soft_batch() {
// Get the first soft batch by number
let payload = jsonrpc_req!("ledger_getSoftBatchByNumber", [1]);
let expected = jsonrpc_result!({"da_slot_height":0,"da_slot_hash":"0000000000000000000000000000000000000000000000000000000000000000","da_slot_txs_commitment":"0101010101010101010101010101010101010101010101010101010101010101","hash":"b5515a80204963f7db40e98af11aedb49a394b1c7e3d8b5b7a33346b8627444f","txs":[[116,120,49,32,98,111,100,121],[116,120,50,32,98,111,100,121]],"pre_state_root":"","post_state_root":"","soft_confirmation_signature":"","pub_key":"","l1_fee_rate":0});
let expected = jsonrpc_result!({"da_slot_height":0,"da_slot_hash":"0000000000000000000000000000000000000000000000000000000000000000","da_slot_txs_commitment":"0101010101010101010101010101010101010101010101010101010101010101","hash":"b5515a80204963f7db40e98af11aedb49a394b1c7e3d8b5b7a33346b8627444f","txs":["0x74783120626f6479", "0x74783220626f6479"],"pre_state_root":"","post_state_root":"","soft_confirmation_signature":"","pub_key":"","l1_fee_rate":0});
regular_test_helper(payload, &expected);

// Get the first soft batch by hash
Expand All @@ -327,8 +328,17 @@ fn test_get_soft_batch() {

// Get the second soft batch by number
let payload = jsonrpc_req!("ledger_getSoftBatchByNumber", [2]);
let txs = batch2_tx_receipts()
.into_iter()
.map(|tx_receipt| {
format!(
"0x{}",
tx_receipt.body_to_save.unwrap().encode_hex::<String>()
)
})
.collect::<Vec<String>>();
let expected = jsonrpc_result!(
{"da_slot_height":1,"da_slot_hash":"0202020202020202020202020202020202020202020202020202020202020202","da_slot_txs_commitment":"0303030303030303030303030303030303030303030303030303030303030303","hash":"f85fe0cb36fdaeca571c896ed476b49bb3c8eff00d935293a8967e1e9a62071e","txs": batch2_tx_receipts().into_iter().map(|tx_receipt| tx_receipt.body_to_save.unwrap()).collect::<Vec<_>>(), "pre_state_root":"","post_state_root":"","soft_confirmation_signature":"","pub_key":"","l1_fee_rate":0}
{"da_slot_height":1,"da_slot_hash":"0202020202020202020202020202020202020202020202020202020202020202","da_slot_txs_commitment":"0303030303030303030303030303030303030303030303030303030303030303","hash":"f85fe0cb36fdaeca571c896ed476b49bb3c8eff00d935293a8967e1e9a62071e","txs": txs, "pre_state_root":"","post_state_root":"","soft_confirmation_signature":"","pub_key":"","l1_fee_rate":0}
);
regular_test_helper(payload, &expected);

Expand All @@ -341,8 +351,18 @@ fn test_get_soft_batch() {

// Get range of soft batches
let payload = jsonrpc_req!("ledger_getSoftBatchRange", [1, 2]);

let txs = batch2_tx_receipts()
.into_iter()
.map(|tx_receipt| {
format!(
"0x{}",
tx_receipt.body_to_save.unwrap().encode_hex::<String>()
)
})
.collect::<Vec<String>>();
let expected = jsonrpc_result!(
[{"da_slot_height":0,"da_slot_hash":"0000000000000000000000000000000000000000000000000000000000000000","da_slot_txs_commitment":"0101010101010101010101010101010101010101010101010101010101010101","hash":"b5515a80204963f7db40e98af11aedb49a394b1c7e3d8b5b7a33346b8627444f","txs":[[116,120,49,32,98,111,100,121],[116,120,50,32,98,111,100,121]],"pre_state_root":"","post_state_root":"","soft_confirmation_signature":"","pub_key":"","l1_fee_rate":0},{"da_slot_height":1,"da_slot_hash":"0202020202020202020202020202020202020202020202020202020202020202","da_slot_txs_commitment":"0303030303030303030303030303030303030303030303030303030303030303","hash":"f85fe0cb36fdaeca571c896ed476b49bb3c8eff00d935293a8967e1e9a62071e","txs": batch2_tx_receipts().into_iter().map(|tx_receipt| tx_receipt.body_to_save.unwrap()).collect::<Vec<_>>(), "pre_state_root":"","post_state_root":"","soft_confirmation_signature":"","pub_key":"","l1_fee_rate":0}
[{"da_slot_height":0,"da_slot_hash":"0000000000000000000000000000000000000000000000000000000000000000","da_slot_txs_commitment":"0101010101010101010101010101010101010101010101010101010101010101","hash":"b5515a80204963f7db40e98af11aedb49a394b1c7e3d8b5b7a33346b8627444f","txs":["0x74783120626f6479", "0x74783220626f6479"],"pre_state_root":"","post_state_root":"","soft_confirmation_signature":"","pub_key":"","l1_fee_rate":0},{"da_slot_height":1,"da_slot_hash":"0202020202020202020202020202020202020202020202020202020202020202","da_slot_txs_commitment":"0303030303030303030303030303030303030303030303030303030303030303","hash":"f85fe0cb36fdaeca571c896ed476b49bb3c8eff00d935293a8967e1e9a62071e","txs": txs, "pre_state_root":"","post_state_root":"","soft_confirmation_signature":"","pub_key":"","l1_fee_rate":0}
]
);
regular_test_helper(payload, &expected);
Expand Down
3 changes: 2 additions & 1 deletion crates/sequencer-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use jsonrpsee::http_client::{HttpClient, HttpClientBuilder};
use jsonrpsee::rpc_params;
use reth_primitives::B256;
use serde::Deserialize;
use sov_rollup_interface::rpc::utils::tx_hex;
use sov_rollup_interface::soft_confirmation::SignedSoftConfirmationBatch;

/// Configuration for SequencerClient.
Expand Down Expand Up @@ -76,7 +77,7 @@ pub struct GetSoftBatchResponse {
pub da_slot_hash: [u8; 32],
#[serde(with = "hex::serde")]
pub da_slot_txs_commitment: [u8; 32],
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none", with = "tx_hex")]
pub txs: Option<Vec<Vec<u8>>>,
#[serde(with = "hex::serde")]
pub pre_state_root: Vec<u8>,
Expand Down
83 changes: 82 additions & 1 deletion crates/sovereign-sdk/rollup-interface/src/node/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ pub struct SoftBatchResponse {
#[serde(with = "hex::serde")]
pub hash: [u8; 32],
/// The transactions in this batch.
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none", with = "utils::tx_hex")]
pub txs: Option<Vec<Vec<u8>>>,
rakanalh marked this conversation as resolved.
Show resolved Hide resolved
/// Pre-state root of the soft batch.
#[serde(with = "hex::serde")]
Expand Down Expand Up @@ -476,6 +476,87 @@ pub mod utils {
deserializer.deserialize_str(HexStrVisitor(PhantomData))
}
}

/// Serialize an optional vector of transactions, each into a hex format.
///
/// This would override the existing way of serializing the transactions by emitting
/// the actual vector of bytes by returning the hexidecimal representation of those bytes
/// for each item.
pub mod tx_hex {
use core::fmt;
use core::marker::PhantomData;

use hex::{FromHex, ToHex};
use serde::de::{self, Error, SeqAccess, Visitor};
use serde::ser::SerializeSeq;
use serde::{Deserializer, Serializer};

use crate::maybestd::string::String;
use crate::maybestd::vec::Vec;
use crate::maybestd::{format, vec};

/// Serializes `data` as hex string using lowercase characters and prefixing with '0x'.
pub fn serialize<S>(data: &Option<Vec<Vec<u8>>>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if let Some(data) = data {
let data = data
.iter()
.map(|item| format!("0x{}", item.encode_hex::<String>()));
let mut seq = serializer.serialize_seq(Some(data.len()))?;
for e in data {
seq.serialize_element(&e)?;
kpp marked this conversation as resolved.
Show resolved Hide resolved
}
return seq.end();
}
serializer.serialize_none()
}

/// Deserializes a vector of hex string into raw bytes.
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Vec<Vec<u8>>>, D::Error>
where
D: Deserializer<'de>,
{
struct HexStrVisitor(PhantomData<Option<Vec<Vec<u8>>>>);

impl<'de> Visitor<'de> for HexStrVisitor {
type Value = Option<Vec<Vec<u8>>>;

fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "a hex encoded string")
}

fn visit_str<E>(self, data: &str) -> Result<Self::Value, E>
where
E: Error,
{
let data = data.trim_start_matches("0x");
FromHex::from_hex(data)
.map_err(Error::custom)
.map(|res| Some(vec![res]))
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let mut result = vec![];
while let Some(item) = seq.next_element::<String>()? {
let item = item.trim_start_matches("0x");
result.push(
FromHex::from_hex(item)
.map_err(|_| de::Error::custom("Could not convert from hex"))?,
);
}

Ok(Some(result))
}
}

deserializer.deserialize_seq(HexStrVisitor(PhantomData))
}
}
}

#[cfg(test)]
Expand Down
Loading