Skip to content

Commit

Permalink
Fix test post SoftConfirmationReceipt removal
Browse files Browse the repository at this point in the history
  • Loading branch information
jfldde committed Feb 10, 2025
1 parent 7fb9076 commit 20c0adc
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 91 deletions.
169 changes: 86 additions & 83 deletions bin/citrea/src/test_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use sha2::Digest;
use sov_db::ledger_db::{LedgerDB, SharedLedgerOps};
use sov_db::rocks_db_config::RocksdbConfig;
use sov_db::schema::types::StoredTransaction;
use sov_mock_da::MockDaSpec;
#[cfg(test)]
use sov_modules_api::DaSpec;
use sov_modules_api::L2Block;
use sov_rollup_interface::soft_confirmation::{
SignedSoftConfirmationHeader, SoftConfirmationHeader,
};

struct TestExpect {
payload: serde_json::Value,
Expand Down Expand Up @@ -41,29 +41,14 @@ async fn queries_test_runner(test_queries: Vec<TestExpect>, rpc_config: RpcConfi
}
}

fn populate_ledger<'txs, Tx>(
ledger_db: &mut LedgerDB,
state_root: [u8; 32],
l2_blocks: Vec<L2Block<'txs, Tx>>,
) {
for (soft_confirmation_receipt, tx_bodies) in
soft_confirmation_receipts.into_iter().zip(tx_bodies)
{
ledger_db
.commit_l2_block(
l2_blocks
soft_confirmation_receipt,
true
)
.unwrap();
fn populate_ledger(ledger_db: &mut LedgerDB, l2_blocks: Vec<L2Block<'_, [u8; 32]>>) {
for block in l2_blocks {
let tx_hashes = block.txs.to_vec();
ledger_db.commit_l2_block(block, tx_hashes, true).unwrap();
}
}

fn test_helper<'txs, Tx>(
test_queries: Vec<TestExpect>,
l2_blocks: Vec<L2Block<'txs, Tx>>,
tx_bodies: Vec<Vec<Vec<u8>>>,
) {
fn test_helper(test_queries: Vec<TestExpect>, l2_blocks: Vec<L2Block<'_, [u8; 32]>>) {
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_io()
.enable_time()
Expand All @@ -75,11 +60,7 @@ fn test_helper<'txs, Tx>(
let tmpdir = tempfile::tempdir().unwrap();
let mut ledger_db =
LedgerDB::with_config(&RocksdbConfig::new(tmpdir.path(), None, None)).unwrap();
populate_ledger(
&mut ledger_db,
soft_confirmation_receipts,
tx_bodies,
);
populate_ledger(&mut ledger_db, l2_blocks);
let server = jsonrpsee::server::ServerBuilder::default()
.build("127.0.0.1:0")
.await
Expand Down Expand Up @@ -117,61 +98,79 @@ fn batch2_tx_receipts() -> (Vec<StoredTransaction>, Vec<Vec<u8>>) {
fn regular_test_helper(payload: serde_json::Value, expected: &serde_json::Value) {
let tx_bodies_1 = vec![b"tx1 body".to_vec(), b"tx2 body".to_vec()];
let (batch_2_receipts, tx_bodies_2) = batch2_tx_receipts();
let l2_blocks = vec![
L2Block {
header: L2BlockHeader {
inner: L2BlockHeaderInner {
l2_height: 1,
da_slot_height: 0,
da_slot_hash: <MockDaSpec as DaSpec>::SlotHash::from([0u8; 32]),
da_slot_txs_commitment: <MockDaSpec as DaSpec>::SlotHash::from([1u8; 32]),
hash: ::sha2::Sha256::digest(b"batch_receipt").into(),
prev_hash: ::sha2::Sha256::digest(b"prev_batch_receipt").into(),
state_root: [1; 32],
tx_merkle_root: compute_tx_merkle_root(&[]).unwrap(),
l1_fee_rate: 0,
timestamp: 0,
deposit_data: vec![
"aaaaab".as_bytes().to_vec(),
"eeeeeeeeee".as_bytes().to_vec(),
],
},
signature: vec![],
pub_key: vec![],
},
txs: vec![].into(),
blobs: tx_bodies_1.iter().collect(),
},
L2Block {
header: L2BlockHeader {
inner: L2BlockHeaderInner {
l2_height: 2,
da_slot_height: 1,
da_slot_hash: <MockDaSpec as DaSpec>::SlotHash::from([2; 32]),
da_slot_txs_commitment: <MockDaSpec as DaSpec>::SlotHash::from([3; 32]),
hash: ::sha2::Sha256::digest(b"batch_receipt2").into(),
prev_hash: ::sha2::Sha256::digest(b"prev_batch_receipt2").into(),
state_root: [1; 32],
tx_merkle_root: compute_tx_merkle_root(&batch_2_receipts.iter().map(|r| r.hash).collect::<Vec<_>>()).unwrap(),
l1_fee_rate: 0,
timestamp: 0,
deposit_data: vec!["c44444".as_bytes().to_vec()],
},
signature: vec![],
pub_key: vec![],
},
txs: vec![].into(),
blobs: vec![tx_bodies_1, tx_bodies_2],
}];

let tx_hashes_1 = vec![
::sha2::Sha256::digest(b"tx1").into(),
::sha2::Sha256::digest(b"tx2").into(),
];

let header1 = SoftConfirmationHeader::new(
1,
0,
[0u8; 32],
[1u8; 32],
::sha2::Sha256::digest(b"prev_batch_receipt").into(),
[1; 32],
0,
compute_tx_merkle_root(&tx_hashes_1).unwrap(),
vec![
"aaaaab".as_bytes().to_vec(),
"eeeeeeeeee".as_bytes().to_vec(),
],
0,
);

let header2 = SoftConfirmationHeader::new(
2,
1,
[2; 32],
[3; 32],
::sha2::Sha256::digest(b"prev_batch_receipt2").into(),
[1; 32],
0,
compute_tx_merkle_root(&batch_2_receipts.iter().map(|r| r.hash).collect::<Vec<_>>())
.unwrap(),
vec!["c44444".as_bytes().to_vec()],
0,
);

let signed_header1 = SignedSoftConfirmationHeader::new(
header1,
::sha2::Sha256::digest(b"batch_receipt").into(),
vec![],
vec![],
);

let signed_header2 = SignedSoftConfirmationHeader::new(
header2,
::sha2::Sha256::digest(b"batch_receipt2").into(),
vec![],
vec![],
);

let l2_blocks = vec![
L2Block::<[u8; 32]>::new(
signed_header1,
tx_bodies_1.clone().into(),
tx_hashes_1.into(),
),
L2Block::<[u8; 32]>::new(
signed_header2,
tx_bodies_2.clone().into(),
batch_2_receipts
.iter()
.map(|r| r.hash)
.collect::<Vec<_>>()
.into(),
),
];

test_helper(
vec![TestExpect {
payload,
expected: expected.clone(),
}],
soft_confirmation_receipts,
tx_bodies,
l2_blocks,
)
}

Expand Down Expand Up @@ -209,7 +208,11 @@ fn test_get_soft_confirmation() {
// Get the first soft confirmation by number
let payload = jsonrpc_req!("ledger_getSoftConfirmationByNumber", [1]);

let empty_tx_merkle_root = compute_tx_merkle_root(&[]).unwrap();
let tx_hashes = vec![
::sha2::Sha256::digest(b"tx1").into(),
::sha2::Sha256::digest(b"tx2").into(),
];
let empty_tx_merkle_root = compute_tx_merkle_root(&tx_hashes).unwrap();
let expected = jsonrpc_result!({"daSlotHeight":0,"daSlotHash":"0000000000000000000000000000000000000000000000000000000000000000","daSlotTxsCommitment":"0101010101010101010101010101010101010101010101010101010101010101","depositData": ["616161616162", "65656565656565656565"],"hash":"b5515a80204963f7db40e98af11aedb49a394b1c7e3d8b5b7a33346b8627444f","l2Height":1, "txs":["74783120626f6479", "74783220626f6479"],"prevHash":"0209d4aa08c40ed0fcb2bb6eb276481f2ad045914c3065e13e4f1657e97638b1","stateRoot":"0101010101010101010101010101010101010101010101010101010101010101","softConfirmationSignature":"","pubKey":"", "l1FeeRate":0, "timestamp": 0, "txMerkleRoot": empty_tx_merkle_root});
regular_test_helper(payload, &expected);

Expand All @@ -229,9 +232,9 @@ fn test_get_soft_confirmation() {
.collect::<Vec<String>>();

let tx_hashes = batch2_tx_receipts()
.1
.into_iter()
.flat_map(|tx| tx.try_into().ok())
.0
.iter()
.map(|r| r.hash)
.collect::<Vec<_>>();
let tx_merkle_root = compute_tx_merkle_root(&tx_hashes).unwrap();
let expected = jsonrpc_result!(
Expand All @@ -256,9 +259,9 @@ fn test_get_soft_confirmation() {
.collect::<Vec<String>>();

let tx_hashes = batch2_tx_receipts()
.1
.into_iter()
.flat_map(|tx| tx.try_into().ok())
.0
.iter()
.map(|r| r.hash)
.collect::<Vec<_>>();
let tx_merkle_root = compute_tx_merkle_root(&tx_hashes).unwrap();
let expected = jsonrpc_result!(
Expand Down
8 changes: 4 additions & 4 deletions crates/sovereign-sdk/full-node/db/sov-db/src/ledger_db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ impl SharedLedgerOps for LedgerDB {
}

/// Commits a soft confirmation to the database by inserting its transactions and batches before
fn commit_l2_block<'txs, Tx: Clone>(
fn commit_l2_block<Tx: Clone>(
&self,
l2_block: L2Block<'txs, Tx>,
l2_block: L2Block<'_, Tx>,
tx_hashes: Vec<[u8; 32]>,
include_tx_body: bool,
) -> Result<(), anyhow::Error> {
Expand Down Expand Up @@ -198,8 +198,8 @@ impl SharedLedgerOps for LedgerDB {
let soft_confirmation_to_store = StoredSoftConfirmation {
da_slot_height: l2_block.da_slot_height(),
l2_height,
da_slot_hash: l2_block.da_slot_hash().into(),
da_slot_txs_commitment: l2_block.da_slot_txs_commitment().into(),
da_slot_hash: l2_block.da_slot_hash(),
da_slot_txs_commitment: l2_block.da_slot_txs_commitment(),
hash: l2_block.hash(),
prev_hash: l2_block.prev_hash(),
txs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ pub trait SharedLedgerOps {
) -> Result<()>;

/// Commits a soft confirmation to the database by inserting its transactions and batches before
fn commit_l2_block<'txs, Tx: Clone>(
fn commit_l2_block<Tx: Clone>(
&self,
l2_block: L2Block<'txs, Tx>,
l2_block: L2Block<'_, Tx>,
tx_hashes: Vec<[u8; 32]>,
include_tx_body: bool,
) -> Result<()>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ where
val.da_slot_hash,
val.da_slot_txs_commitment,
val.prev_hash,
val.state_root.try_into().unwrap(),
val.state_root,
val.l1_fee_rate,
val.tx_merkle_root,
val.deposit_data,
Expand Down
2 changes: 1 addition & 1 deletion crates/sovereign-sdk/rollup-interface/src/node/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ where
val.da_slot_hash,
val.da_slot_txs_commitment,
val.prev_hash,
val.state_root.try_into().unwrap(),
val.state_root,
val.l1_fee_rate,
val.tx_merkle_root,
val.deposit_data.into_iter().map(|tx| tx.tx).collect(),
Expand Down

0 comments on commit 20c0adc

Please sign in to comment.