From c7c2c75ee519403553f38ed5a6507734f61fe474 Mon Sep 17 00:00:00 2001 From: Matteo Almanza Date: Wed, 29 Jan 2025 02:21:40 +0000 Subject: [PATCH] fix: use the same sorting key for in memory impl of stacks chain tip --- Cargo.lock | 8 +- Cargo.toml | 2 +- signer/src/storage/in_memory.rs | 2 +- signer/tests/integration/postgres.rs | 128 ++++++++++++++++++++++++++- signers.sh | 2 - supply-chain/config.toml | 4 +- 6 files changed, 135 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6aa74b60f..edf2813b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1577,9 +1577,9 @@ checksum = "dcbb2bf8e87535c23f7a8a321e364ce21462d0ff10cb6407820e8e96dfff6653" [[package]] name = "dummy" -version = "0.7.0" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e57e12b69e57fad516e01e2b3960f122696fdb13420e1a88ed8e210316f2876" +checksum = "b3ee4e39146145f7dd28e6c85ffdce489d93c0d9c88121063b8aacabbd9858d2" dependencies = [ "darling", "proc-macro2", @@ -1744,9 +1744,9 @@ dependencies = [ [[package]] name = "fake" -version = "2.9.2" +version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c25829bde82205da46e1823b2259db6273379f626fc211f126f65654a2669be" +checksum = "aef603df4ba9adbca6a332db7da6f614f21eafefbaf8e087844e452fdec152d0" dependencies = [ "deunicode", "dummy", diff --git a/Cargo.toml b/Cargo.toml index b3f90ca86..4f566823c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -88,7 +88,7 @@ axum = { version = "0.8.1", default-features = false, features = ["http1", "json tracing-subscriber = { version = "0.3.19", default-features = false, features = ["env-filter", "fmt", "json", "time", "ansi"] } # Crates used only for testing -fake = { version = "2.9.2", default-features = false, features = ["derive", "time"] } +fake = { version = "3.1.0", default-features = false, features = ["derive", "time"] } mockall = { version = "0.13.1", default-features = false } mockito = { version = "1.6.1", default-features = false } more-asserts = { version = "0.3.1", default-features = false } diff --git a/signer/src/storage/in_memory.rs b/signer/src/storage/in_memory.rs index 7d0dae4e4..7bb8c3d08 100644 --- a/signer/src/storage/in_memory.rs +++ b/signer/src/storage/in_memory.rs @@ -226,7 +226,7 @@ impl Store { .filter_map(|block| self.bitcoin_anchor_to_stacks_blocks.get(&block.block_hash)) .flatten() .filter_map(|stacks_block_hash| self.stacks_blocks.get(stacks_block_hash)) - .max_by_key(|block| (block.block_height, &block.block_hash)) + .max_by_key(|block| (block.block_height, block.block_hash.to_bytes())) .cloned() } diff --git a/signer/tests/integration/postgres.rs b/signer/tests/integration/postgres.rs index c1d08d946..13f0311d7 100644 --- a/signer/tests/integration/postgres.rs +++ b/signer/tests/integration/postgres.rs @@ -34,6 +34,7 @@ use signer::stacks::contracts::ReqContext; use signer::stacks::contracts::RotateKeysV1; use signer::storage; use signer::storage::model; +use signer::storage::model::BitcoinBlock; use signer::storage::model::BitcoinBlockHash; use signer::storage::model::BitcoinTxId; use signer::storage::model::BitcoinTxSigHash; @@ -615,7 +616,6 @@ async fn should_return_the_same_pending_accepted_deposit_requests_as_in_memory_s /// This ensures that the postgres store and the in memory stores returns equivalent results /// when fetching pending accepted withdraw requests -/// NOTE: This test breaks with `fake` >= `2.10` due to the `dummy` version bump. #[tokio::test] async fn should_return_the_same_pending_accepted_withdraw_requests_as_in_memory_store() { let mut pg_store = testing::storage::new_test_database().await; @@ -658,6 +658,19 @@ async fn should_return_the_same_pending_accepted_withdraw_requests_as_in_memory_ chain_tip ); + assert_eq!( + in_memory_store + .get_stacks_chain_tip(&chain_tip) + .await + .expect("failed to get stacks chain tip") + .expect("no chain tip"), + pg_store + .get_stacks_chain_tip(&chain_tip) + .await + .expect("failed to get stacks chain tip") + .expect("no chain tip"), + ); + let mut pending_accepted_withdraw_requests = in_memory_store .get_pending_accepted_withdrawal_requests(&chain_tip, context_window, threshold) .await @@ -3115,3 +3128,116 @@ async fn signer_utxo_reorg_suite(desc: ReorgDescription) { signer::testing::storage::drop_db(db).await; } + +fn hex_to_block_hash(hash: &str) -> [u8; 32] { + hex::decode(hash).unwrap().as_slice().try_into().unwrap() +} + +#[tokio::test] +async fn compare_in_memory_bitcoin_chain_tip() { + let mut rng = rand::rngs::StdRng::seed_from_u64(51); + + let pg_store = testing::storage::new_test_database().await; + let in_memory_store = storage::in_memory::Store::new_shared(); + + let root: BitcoinBlock = fake::Faker.fake_with_rng(&mut rng); + let mut blocks = vec![root.clone()]; + for block_hash in [ + "FF00000000000000000000000000000000000000000000000000000000000011", + "11000000000000000000000000000000000000000000000000000000000000FF", + ] { + blocks.push(BitcoinBlock { + block_hash: hex_to_block_hash(block_hash).into(), + block_height: root.block_height + 1, + parent_hash: root.block_hash, + }) + } + + for block in &blocks { + pg_store.write_bitcoin_block(block).await.unwrap(); + in_memory_store.write_bitcoin_block(block).await.unwrap(); + } + + assert_eq!( + in_memory_store + .get_bitcoin_canonical_chain_tip() + .await + .expect("failed to get canonical chain tip") + .expect("no chain tip"), + pg_store + .get_bitcoin_canonical_chain_tip() + .await + .expect("failed to get canonical chain tip") + .expect("no chain tip"), + ); + + signer::testing::storage::drop_db(pg_store).await; +} + +#[tokio::test] +async fn compare_in_memory_stacks_chain_tip() { + let mut rng = rand::rngs::StdRng::seed_from_u64(51); + + let pg_store = testing::storage::new_test_database().await; + let in_memory_store = storage::in_memory::Store::new_shared(); + + let root_anchor: BitcoinBlock = fake::Faker.fake_with_rng(&mut rng); + + pg_store.write_bitcoin_block(&root_anchor).await.unwrap(); + in_memory_store + .write_bitcoin_block(&root_anchor) + .await + .unwrap(); + + let root: StacksBlock = fake::Faker.fake_with_rng(&mut rng); + + let mut blocks = vec![root.clone()]; + for block_hash in [ + "FF00000000000000000000000000000000000000000000000000000000000011", + "11000000000000000000000000000000000000000000000000000000000000FF", + ] { + blocks.push(StacksBlock { + block_hash: hex_to_block_hash(block_hash).into(), + block_height: root.block_height + 1, + parent_hash: root.block_hash, + bitcoin_anchor: root_anchor.block_hash, + }) + } + + for block in &blocks { + pg_store.write_stacks_block(block).await.unwrap(); + in_memory_store.write_stacks_block(block).await.unwrap(); + } + + assert_eq!( + in_memory_store + .get_bitcoin_canonical_chain_tip() + .await + .expect("failed to get canonical chain tip") + .expect("no chain tip"), + root_anchor.block_hash + ); + assert_eq!( + pg_store + .get_bitcoin_canonical_chain_tip() + .await + .expect("failed to get canonical chain tip") + .expect("no chain tip"), + root_anchor.block_hash + ); + + assert_eq!( + in_memory_store + .get_stacks_chain_tip(&root_anchor.block_hash) + .await + .expect("failed to get canonical chain tip") + .expect("no chain tip"), + pg_store + .get_stacks_chain_tip(&root_anchor.block_hash) + .await + .expect("failed to get canonical chain tip") + .expect("no chain tip"), + ); + + signer::testing::storage::drop_db(pg_store).await; +} diff --git a/signers.sh b/signers.sh index 1b8422644..46b9661fc 100755 --- a/signers.sh +++ b/signers.sh @@ -27,8 +27,6 @@ LOG_SETTINGS="$LOG_SETTINGS,netlink_proto=info,libp2p_autonat=info,libp2p_gossip # Run the specified number of signers exec_run() { - echo "here!" - if [ "$1" -eq 1 ]; then printf "${RED}ERROR:${NC} At least 2 signers are required\n" exit 1 diff --git a/supply-chain/config.toml b/supply-chain/config.toml index c195a85ee..d6422dcba 100644 --- a/supply-chain/config.toml +++ b/supply-chain/config.toml @@ -571,7 +571,7 @@ version = "1.0.9" criteria = "safe-to-deploy" [[exemptions.dummy]] -version = "0.7.0" +version = "0.9.1" criteria = "safe-to-deploy" [[exemptions.ed25519]] @@ -615,7 +615,7 @@ version = "0.5.2" criteria = "safe-to-deploy" [[exemptions.fake]] -version = "2.9.2" +version = "3.1.0" criteria = "safe-to-deploy" [[exemptions.fallible-iterator]]