Skip to content

Commit

Permalink
fix: use the same sorting key for in memory impl of stacks chain tip
Browse files Browse the repository at this point in the history
  • Loading branch information
matteojug committed Jan 29, 2025
1 parent cd40127 commit c7c2c75
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 11 deletions.
8 changes: 4 additions & 4 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion signer/src/storage/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
128 changes: 127 additions & 1 deletion signer/tests/integration/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -3115,3 +3128,116 @@ async fn signer_utxo_reorg_suite<const N: usize>(desc: ReorgDescription<N>) {

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;
}
2 changes: 0 additions & 2 deletions signers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions supply-chain/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down Expand Up @@ -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]]
Expand Down

0 comments on commit c7c2c75

Please sign in to comment.