Repository navigation
fix(dash-spv)!: deliver only verified InstantLocks #1336
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
base: dev
Are you sure you want to change the base?
Changes from all commits
cc7b73c
f7bb4ba
2e3a1a7
32cfbe8
285d3ea
fa3c85a
493f66b
a487959
8f75a8a
bbb7a81
6770c57
fc7c703
c90478d
0d4a6a8
66d3833
eeb42f9
e5387dd
5ac610e
357029f
3f3d330
9bf93c1
ae2218d
38823cd
8b1e686
18c8d78
6f206d4
20bccb1
94fb8b5
1e960df
8137008
85d97ca
a182887
514b9fc
2b7cbd8
f1237a6
3ccc21a
358abcc
18f5471
bd7021c
b75d212
1818275
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -56,6 +56,48 @@ impl QRInfoFeedResult { | |||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /// A stored cycle whose quorums may sign this network's InstantSend locks. | ||||||||||||||||||||||
| #[derive(Clone, Copy, Debug)] | ||||||||||||||||||||||
| pub(crate) struct InstantSendCycle<'a> { | ||||||||||||||||||||||
| /// The cycle's quorums, by quorum index. | ||||||||||||||||||||||
| pub(crate) quorums: &'a BTreeMap<u16, QualifiedQuorumEntry>, | ||||||||||||||||||||||
| /// Whether no later QRInfo can replace the cycle's quorums, so it can refute a lock. | ||||||||||||||||||||||
| pub(crate) complete: bool, | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /// `true` when the cycle holds a `Verified` quorum of `isd_type` for every active slot. | ||||||||||||||||||||||
| fn fills_every_slot(quorums: &BTreeMap<u16, QualifiedQuorumEntry>, isd_type: LLMQType) -> bool { | ||||||||||||||||||||||
| quorums.len() == isd_type.active_quorum_count() as usize | ||||||||||||||||||||||
| && quorums.values().all(|quorum| { | ||||||||||||||||||||||
| quorum.verified == LLMQEntryVerificationStatus::Verified | ||||||||||||||||||||||
| && quorum.quorum_entry.llmq_type == isd_type | ||||||||||||||||||||||
| }) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /// Whether a stored cycle is complete, or `None` when one of its quorums is unverified or | ||||||||||||||||||||||
| /// cannot sign IS locks: of type `isd_type`, or any rotating type when `reads_served_type`. | ||||||||||||||||||||||
| /// Storage stops merging into a cycle only once it fills every slot of `isd_type`. | ||||||||||||||||||||||
| fn instant_send_cycle_completeness( | ||||||||||||||||||||||
| quorums: &BTreeMap<u16, QualifiedQuorumEntry>, | ||||||||||||||||||||||
| isd_type: LLMQType, | ||||||||||||||||||||||
| reads_served_type: bool, | ||||||||||||||||||||||
| ) -> Option<bool> { | ||||||||||||||||||||||
| let signs = |llmq_type: LLMQType| { | ||||||||||||||||||||||
| if reads_served_type { | ||||||||||||||||||||||
| llmq_type.is_rotating_quorum_type() | ||||||||||||||||||||||
| } else { | ||||||||||||||||||||||
| llmq_type == isd_type | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
| if quorums.values().any(|quorum| { | ||||||||||||||||||||||
| quorum.verified != LLMQEntryVerificationStatus::Verified | ||||||||||||||||||||||
| || !signs(quorum.quorum_entry.llmq_type) | ||||||||||||||||||||||
| }) { | ||||||||||||||||||||||
| return None; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| Some(fills_every_slot(quorums, isd_type)) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /// Why the engine did not take a diff or a QRInfo. | ||||||||||||||||||||||
| #[derive(Debug, thiserror::Error)] | ||||||||||||||||||||||
| pub enum FeedError<E> { | ||||||||||||||||||||||
|
|
@@ -391,12 +433,31 @@ impl<H: BlockHeaderStorage> MasternodeListEngine<H> { | |||||||||||||||||||||
| self.masternode_lists.last_key_value().map(|(_, list)| list) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /// The rotated quorums of the cycle `cycle_hash`, by quorum index. | ||||||||||||||||||||||
| pub fn rotated_quorums_of_cycle( | ||||||||||||||||||||||
| /// The cycle `cycle_hash` when every quorum of it is verified and of a type that | ||||||||||||||||||||||
| /// signs this network's InstantSend locks. | ||||||||||||||||||||||
| pub(crate) fn instant_send_cycle( | ||||||||||||||||||||||
| &self, | ||||||||||||||||||||||
| cycle_hash: &BlockHash, | ||||||||||||||||||||||
| ) -> Option<&BTreeMap<u16, QualifiedQuorumEntry>> { | ||||||||||||||||||||||
| self.rotated_quorums_per_cycle.get(cycle_hash) | ||||||||||||||||||||||
| ) -> Option<InstantSendCycle<'_>> { | ||||||||||||||||||||||
| let quorums = self.rotated_quorums_per_cycle.get(cycle_hash)?; | ||||||||||||||||||||||
| let complete = instant_send_cycle_completeness( | ||||||||||||||||||||||
| quorums, | ||||||||||||||||||||||
| self.network.isd_llmq_type(), | ||||||||||||||||||||||
| self.reads_served_rotation_type(), | ||||||||||||||||||||||
| )?; | ||||||||||||||||||||||
| Some(InstantSendCycle { | ||||||||||||||||||||||
| quorums, | ||||||||||||||||||||||
| complete, | ||||||||||||||||||||||
| }) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| #[cfg(feature = "test-utils")] | ||||||||||||||||||||||
| pub(crate) fn install_test_instant_lock_cycle( | ||||||||||||||||||||||
| &mut self, | ||||||||||||||||||||||
| cycle: BlockHash, | ||||||||||||||||||||||
| quorums: BTreeMap<u16, QualifiedQuorumEntry>, | ||||||||||||||||||||||
| ) { | ||||||||||||||||||||||
| self.rotated_quorums_per_cycle.insert(cycle, quorums); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| #[cfg(any(test, feature = "test-utils"))] | ||||||||||||||||||||||
|
|
@@ -702,14 +763,14 @@ impl<H: BlockHeaderStorage> MasternodeListEngine<H> { | |||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /// `true` when the stored cycle has a `Verified` quorum for every active | ||||||||||||||||||||||
| /// slot. A partial cycle stays open so a later QRInfo can complete it. | ||||||||||||||||||||||
| /// `true` when the stored cycle has a `Verified` quorum of the configured type for | ||||||||||||||||||||||
| /// every active slot. A partial or foreign-type cycle stays open so a later QRInfo | ||||||||||||||||||||||
| /// can complete it. | ||||||||||||||||||||||
| fn is_cycle_fully_verified(&self, cycle_hash: &BlockHash) -> bool { | ||||||||||||||||||||||
| let expected = self.network.isd_llmq_type().active_quorum_count() as usize; | ||||||||||||||||||||||
| self.rotated_quorums_per_cycle.get(cycle_hash).is_some_and(|existing| { | ||||||||||||||||||||||
| existing.len() == expected | ||||||||||||||||||||||
| && existing.values().all(|q| q.verified == LLMQEntryVerificationStatus::Verified) | ||||||||||||||||||||||
| }) | ||||||||||||||||||||||
| let isd_type = self.network.isd_llmq_type(); | ||||||||||||||||||||||
| self.rotated_quorums_per_cycle | ||||||||||||||||||||||
| .get(cycle_hash) | ||||||||||||||||||||||
| .is_some_and(|existing| fills_every_slot(existing, isd_type)) | ||||||||||||||||||||||
|
lklimek marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /// Keeps the restored cycles whose quorums are all `Verified` and held by | ||||||||||||||||||||||
|
|
@@ -799,7 +860,7 @@ impl<H: BlockHeaderStorage> MasternodeListEngine<H> { | |||||||||||||||||||||
| /// rotating type the peer serves. | ||||||||||||||||||||||
| fn rotation_quorum_type(&self, served: &[QuorumEntry]) -> LLMQType { | ||||||||||||||||||||||
| let configured = self.network.isd_llmq_type(); | ||||||||||||||||||||||
| if self.network != Network::Devnet || devnet_isd_type_override().is_some() { | ||||||||||||||||||||||
| if !self.reads_served_rotation_type() { | ||||||||||||||||||||||
| return configured; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| served | ||||||||||||||||||||||
|
|
@@ -809,6 +870,10 @@ impl<H: BlockHeaderStorage> MasternodeListEngine<H> { | |||||||||||||||||||||
| .unwrap_or(configured) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| fn reads_served_rotation_type(&self) -> bool { | ||||||||||||||||||||||
| self.network == Network::Devnet && devnet_isd_type_override().is_none() | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /// The hash of the newest cycle base in an active set. A failed DKG leaves | ||||||||||||||||||||||
| /// an older cycle's quorum at its index, so a set can span cycles, and the | ||||||||||||||||||||||
| /// newest one is the cycle the set is active for. | ||||||||||||||||||||||
|
|
@@ -1135,7 +1200,7 @@ pub(crate) mod test_support { | |||||||||||||||||||||
| use dashcore::consensus::deserialize; | ||||||||||||||||||||||
| use dashcore::hash_types::QuorumVVecHash; | ||||||||||||||||||||||
| use dashcore::sml::masternode_list_entry::qualified_masternode_list_entry::QualifiedMasternodeListEntry; | ||||||||||||||||||||||
| use dashcore::ProTxHash; | ||||||||||||||||||||||
| use dashcore::{InstantLock, ProTxHash}; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| pub(crate) type TestEngine = MasternodeListEngine<MockHeaderStorage>; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
@@ -1171,6 +1236,11 @@ pub(crate) mod test_support { | |||||||||||||||||||||
| block_hashes.into_iter().map(|(height, block_hash)| (block_hash, height)).collect() | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /// An IS lock of the mainnet fixture's cycle, signed by its quorum at index 23. | ||||||||||||||||||||||
| pub(crate) fn mainnet_fixture_is_lock() -> InstantLock { | ||||||||||||||||||||||
| deserialize(&hex_conservative::decode_to_vec("01018d53e7997ead57409750942af0d5e0aafc06f852a9a52308f4781b6a8220298f00000000c6f9d8c63dd15937ea70aaddb7890daad42c91bf6818e2bf76d183d6f2d9215b4b5f84978fad9dde7ab52bdcc0674be891e9029cc1ef0cb01200000000000000a27c98836c4c04653ab81eb4e07ddfc2c8c2c1036b75247969c05a4f25451cd78913a971f1899d9f2bddec9cf8e0104004f72f20c2856453e5aa3bcd2a8200670ec28feda38f67cc400fc72ef1966956656ec0765478c9d16e9a9e470c07f9ed").unwrap()).unwrap() | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /// A commitment with one signer and zeroed keys and signatures. | ||||||||||||||||||||||
| pub(crate) fn quorum_entry( | ||||||||||||||||||||||
| llmq_type: LLMQType, | ||||||||||||||||||||||
|
|
@@ -1396,9 +1466,11 @@ pub(crate) mod test_support { | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| #[cfg(test)] | ||||||||||||||||||||||
| mod tests { | ||||||||||||||||||||||
| use super::build_cycle_quorum_map; | ||||||||||||||||||||||
| use super::test_support::{fixture_heights, quorum_entry, TestEngine}; | ||||||||||||||||||||||
| use super::valid_coinbase_chain_lock; | ||||||||||||||||||||||
| use super::test_support::{fixture_heights, mainnet_fixture_is_lock, quorum_entry, TestEngine}; | ||||||||||||||||||||||
| use super::{ | ||||||||||||||||||||||
| build_cycle_quorum_map, fills_every_slot, instant_send_cycle_completeness, | ||||||||||||||||||||||
| valid_coinbase_chain_lock, | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
| use super::{FeedError, MasternodeListEngine, QRInfoFeedResult}; | ||||||||||||||||||||||
| use crate::error::{StorageError, StorageResult}; | ||||||||||||||||||||||
| use crate::storage::{BlockHeaderStorage, BlockHeaderTip}; | ||||||||||||||||||||||
|
|
@@ -3312,4 +3384,25 @@ mod tests { | |||||||||||||||||||||
| restored.coinbase_chain_locks.get(&tip_height) | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /// On a devnet the signing cycle can be of another rotating type than the configured | ||||||||||||||||||||||
| /// one. Holding as many quorums as the configured type has slots does not complete | ||||||||||||||||||||||
| /// it, so a lock it does not verify is deferred and a later QRInfo still merges in. | ||||||||||||||||||||||
| #[test] | ||||||||||||||||||||||
| fn should_not_complete_a_devnet_cycle_of_another_rotating_type() { | ||||||||||||||||||||||
| let lock = mainnet_fixture_is_lock(); | ||||||||||||||||||||||
| let cycle = | ||||||||||||||||||||||
| TestEngine::mainnet_fixture().rotated_quorums_per_cycle[&lock.cyclehash].clone(); | ||||||||||||||||||||||
| let served = LLMQType::Llmqtype60_75; | ||||||||||||||||||||||
| let configured = LLMQType::LlmqtypeDevnetDIP0024; | ||||||||||||||||||||||
| let slots = configured.active_quorum_count() as usize; | ||||||||||||||||||||||
| assert!(slots < cycle.len(), "the mainnet cycle has more slots than a devnet one"); | ||||||||||||||||||||||
| let partial: BTreeMap<_, _> = cycle.clone().into_iter().take(slots).collect(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| assert!(!fills_every_slot(&partial, configured)); | ||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ Suggestion, not blocking · Testing · Moderate Devnet storage-gate regression test no longer exercises Why: The PR deleted How to fix it, why, 3 places read, and 2 other checksHow to fix it Why it matters Checked in the code
rust-dashcore/dash-spv/src/sml_engine.rs Lines 768 to 772 in 1818275
The hunk deletes rust-dashcore/dash-spv/src/sml_engine.rs Lines 2635 to 2639 in 1818275
|
||||||||||||||||||||||
| assert_eq!(instant_send_cycle_completeness(&partial, configured, true), Some(false)); | ||||||||||||||||||||||
| assert_eq!(instant_send_cycle_completeness(&partial, configured, false), None); | ||||||||||||||||||||||
| assert_eq!(instant_send_cycle_completeness(&cycle, served, false), Some(true)); | ||||||||||||||||||||||
| assert_eq!(instant_send_cycle_completeness(&partial, served, false), Some(false)); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.