Skip to content

Commit db0ec75

Browse files
committed
fix: reversed quorum hash
1 parent 9cb1184 commit db0ec75

File tree

7 files changed

+207
-122
lines changed

7 files changed

+207
-122
lines changed

dash/src/blockdata/transaction/special_transaction/asset_unlock/qualified_asset_unlock.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626
use bincode::{Decode, Encode};
2727
use hashes::Hash;
2828

29-
use crate::blockdata::transaction::special_transaction::SpecialTransactionBasePayloadEncodable;
3029
use crate::blockdata::transaction::special_transaction::asset_unlock::request_info::AssetUnlockRequestInfo;
3130
use crate::blockdata::transaction::special_transaction::asset_unlock::unqualified_asset_unlock::AssetUnlockBasePayload;
31+
use crate::blockdata::transaction::special_transaction::SpecialTransactionBasePayloadEncodable;
3232
use crate::bls_sig_utils::BLSSignature;
33-
use crate::consensus::{Decodable, Encodable, encode};
33+
use crate::consensus::{encode, Decodable, Encodable};
3434
use crate::hash_types::SpecialTransactionPayloadHash;
35-
use crate::transaction::special_transaction::TransactionPayload;
3635
use crate::transaction::special_transaction::asset_unlock::unqualified_asset_unlock::AssetUnlockBaseTransactionInfo;
37-
use crate::{Transaction, TxIn, consensus, io};
36+
use crate::transaction::special_transaction::TransactionPayload;
37+
use crate::{consensus, io, Transaction, TxIn};
3838

3939
// Asset unlock tx size is constant since it has zero inputs and single output only
4040
pub const ASSET_UNLOCK_TX_SIZE: usize = 190;
@@ -64,7 +64,9 @@ pub struct AssetUnlockPayload {
6464

6565
impl AssetUnlockPayload {
6666
/// The size of the payload in bytes.
67-
pub fn size(&self) -> usize { self.base.size() + self.request_info.size() + 96 }
67+
pub fn size(&self) -> usize {
68+
self.base.size() + self.request_info.size() + 96
69+
}
6870
}
6971

7072
impl SpecialTransactionBasePayloadEncodable for AssetUnlockPayload {
@@ -129,7 +131,11 @@ impl Decodable for AssetUnlockPayload {
129131
let base = AssetUnlockBasePayload::consensus_decode(r)?;
130132
let request_info = AssetUnlockRequestInfo::consensus_decode(r)?;
131133
let quorum_sig = BLSSignature::consensus_decode(r)?;
132-
Ok(AssetUnlockPayload { base, request_info, quorum_sig })
134+
Ok(AssetUnlockPayload {
135+
base,
136+
request_info,
137+
quorum_sig,
138+
})
133139
}
134140
}
135141

@@ -139,25 +145,29 @@ mod tests {
139145

140146
use hashes::Hash;
141147
use hex::FromHex;
142-
use internals::hex::Case;
143148
use internals::hex::display::DisplayHex;
149+
use internals::hex::Case;
144150

145151
use crate::bls_sig_utils::BLSSignature;
146152
use crate::consensus::Encodable;
147153
use crate::hash_types::QuorumHash;
148-
use crate::transaction::special_transaction::TransactionPayload;
149154
use crate::transaction::special_transaction::asset_unlock::qualified_asset_unlock::{
150-
ASSET_UNLOCK_TX_SIZE, AssetUnlockPayload, build_asset_unlock_tx,
155+
build_asset_unlock_tx, AssetUnlockPayload, ASSET_UNLOCK_TX_SIZE,
151156
};
152157
use crate::transaction::special_transaction::asset_unlock::request_info::AssetUnlockRequestInfo;
153158
use crate::transaction::special_transaction::asset_unlock::unqualified_asset_unlock::AssetUnlockBasePayload;
154-
use crate::{ScriptBuf, Transaction, TxOut, consensus};
159+
use crate::transaction::special_transaction::TransactionPayload;
160+
use crate::{consensus, ScriptBuf, Transaction, TxOut};
155161

156162
#[test]
157163
fn size() {
158164
let want = 145;
159165
let payload = AssetUnlockPayload {
160-
base: AssetUnlockBasePayload { version: 0, index: 0, fee: 0 },
166+
base: AssetUnlockBasePayload {
167+
version: 0,
168+
index: 0,
169+
fee: 0,
170+
},
161171
request_info: AssetUnlockRequestInfo {
162172
request_height: 0,
163173
quorum_hash: QuorumHash::all_zeros(),
@@ -185,7 +195,6 @@ mod tests {
185195
"4acfa5c6d92071d206da5b767039d42f24e7ab1a694a5b8014cddc088311e448"
186196
)
187197
.unwrap()
188-
.reverse()
189198
);
190199
assert_eq!(payload.quorum_sig, BLSSignature::from_str("aee468c03feec7caada0599457136ef0dfe9365657a42ef81bb4aa53af383d05d90552b2cd23480cae24036b953ba8480d2f98291271a338e4235265dea94feacb54d1fd96083151001eff4156e7475e998154a8e6082575e2ee461b394d24f7").unwrap());
191200
}
@@ -200,7 +209,7 @@ mod tests {
200209
},
201210
request_info: AssetUnlockRequestInfo {
202211
request_height: 1317,
203-
quorum_hash: QuorumHash::from_str("4acfa5c6d92071d206da5b767039d42f24e7ab1a694a5b8014cddc088311e448").unwrap().reverse(),
212+
quorum_hash: QuorumHash::from_str("4acfa5c6d92071d206da5b767039d42f24e7ab1a694a5b8014cddc088311e448").unwrap(),
204213
},
205214
quorum_sig: BLSSignature::from_str("aee468c03feec7caada0599457136ef0dfe9365657a42ef81bb4aa53af383d05d90552b2cd23480cae24036b953ba8480d2f98291271a338e4235265dea94feacb54d1fd96083151001eff4156e7475e998154a8e6082575e2ee461b394d24f7").unwrap()
206215
};
@@ -216,11 +225,11 @@ mod tests {
216225
let tx_bytes = Vec::from_hex("010009000001c8000000000000001976a914c35b782432294088e354bc28aa56d95736cb630288ac0000000001000000000000000070f915129f05000053c006055af6d0ae9aa9627df8615a71c312421a28c4712c8add83c8e1bfdadd").unwrap();
217226
let tx_asset_unlock = build_asset_unlock_tx(&tx_bytes).unwrap();
218227
let bytes_tx_asset_unlock = consensus::serialize(&tx_asset_unlock);
219-
println!("tx_asset_unlock: {:?}", bytes_tx_asset_unlock);
228+
// println!("tx_asset_unlock: {:?}", bytes_tx_asset_unlock);
220229

221230
let hex_tx_asset_unlock = bytes_tx_asset_unlock.to_hex_string(Case::Lower);
222-
println!("hex_tx_asset_unlock: {:?}", hex_tx_asset_unlock);
223-
println!("OK");
231+
// println!("hex_tx_asset_unlock: {:?}", hex_tx_asset_unlock);
232+
// println!("OK");
224233
}
225234

226235
#[test]

dash/src/blockdata/transaction/special_transaction/quorum_commitment.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,19 @@
1717
//! It is defined in DIP6 [dip-0006.md](https://github.com/dashpay/dips/blob/master/dip-0006.md).
1818
//!
1919
20-
#[cfg(feature = "bincode")]
21-
use bincode::{Decode, Encode};
22-
2320
use crate::bls_sig_utils::{BLSPublicKey, BLSSignature};
2421
use crate::consensus::encode::{
2522
compact_size_len, fixed_bitset_len, read_compact_size, read_fixed_bitset, write_compact_size,
2623
write_fixed_bitset,
2724
};
28-
use crate::consensus::{Decodable, Encodable, encode};
25+
use crate::consensus::{encode, Decodable, Encodable};
2926
use crate::hash_types::{QuorumHash, QuorumVVecHash};
3027
use crate::io;
3128
use crate::prelude::*;
3229
use crate::sml::llmq_type::LLMQType;
3330
use crate::sml::quorum_validation_error::QuorumValidationError;
31+
#[cfg(feature = "bincode")]
32+
use bincode::{Decode, Encode};
3433

3534
/// A Quorum Finalization Commitment. It is described in the finalization section of DIP6:
3635
/// [dip-0006.md#6-finalization-phase](https://github.com/dashpay/dips/blob/master/dip-0006.md#6-finalization-phase)
@@ -66,6 +65,7 @@ impl QuorumEntry {
6665
size
6766
}
6867

68+
/// Validates the structure of the quorum entry and returns an error if it's not valid.
6969
pub fn validate_structure(&self) -> Result<(), QuorumValidationError> {
7070
let quorum_threshold = self.llmq_type.threshold() as u64;
7171

@@ -146,8 +146,11 @@ impl Decodable for QuorumEntry {
146146
let version = u16::consensus_decode(r)?;
147147
let llmq_type = LLMQType::consensus_decode(r)?;
148148
let quorum_hash = QuorumHash::consensus_decode(r)?;
149-
let quorum_index =
150-
if version == 2 || version == 4 { Some(i16::consensus_decode(r)?) } else { None };
149+
let quorum_index = if version == 2 || version == 4 {
150+
Some(i16::consensus_decode(r)?)
151+
} else {
152+
None
153+
};
151154
let signers_count = read_compact_size(r)?;
152155
let signers = read_fixed_bitset(r, signers_count as usize)?;
153156
let valid_members_count = read_compact_size(r)?;
@@ -188,7 +191,9 @@ pub struct QuorumCommitmentPayload {
188191

189192
impl QuorumCommitmentPayload {
190193
/// The size of the payload in bytes.
191-
pub fn size(&self) -> usize { 2 + 4 + self.finalization_commitment.size() }
194+
pub fn size(&self) -> usize {
195+
2 + 4 + self.finalization_commitment.size()
196+
}
192197
}
193198

194199
impl Encodable for QuorumCommitmentPayload {
@@ -206,7 +211,11 @@ impl Decodable for QuorumCommitmentPayload {
206211
let version = u16::consensus_decode(r)?;
207212
let height = u32::consensus_decode(r)?;
208213
let finalization_commitment = QuorumEntry::consensus_decode(r)?;
209-
Ok(QuorumCommitmentPayload { version, height, finalization_commitment })
214+
Ok(QuorumCommitmentPayload {
215+
version,
216+
height,
217+
finalization_commitment,
218+
})
210219
}
211220
}
212221

@@ -215,7 +224,7 @@ mod tests {
215224
use hashes::Hash;
216225

217226
use crate::bls_sig_utils::{BLSPublicKey, BLSSignature};
218-
use crate::consensus::{Encodable, deserialize, serialize};
227+
use crate::consensus::{deserialize, serialize, Encodable};
219228
use crate::hash_types::{QuorumHash, QuorumVVecHash};
220229
use crate::network::message::{NetworkMessage, RawNetworkMessage};
221230
use crate::sml::llmq_type::LLMQType;

dash/src/hash_types.rs

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ mod newtypes {
7373
#[cfg(feature = "core-block-hash-use-x11")]
7474
use hashes::hash_x11;
7575
use hashes::hex::Error;
76-
use hashes::{Hash, hash_newtype, hash_newtype_no_ord, hash160, sha256, sha256d};
76+
use hashes::{hash160, hash_newtype, hash_newtype_no_ord, sha256, sha256d, Hash};
7777

7878
use crate::alloc::string::ToString;
7979
use crate::prelude::String;
@@ -83,13 +83,28 @@ mod newtypes {
8383
hash_newtype! {
8484
/// A dash block hash.
8585
pub struct BlockHash(hash_x11::Hash);
86+
/// A hash used to identify a quorum
87+
#[hash_newtype(forward)]
88+
pub struct QuorumHash(hash_x11::Hash);
8689
}
90+
91+
#[cfg(feature = "core-block-hash-use-x11")]
92+
/// A raw hash that is compatible with quorum and block hash.
93+
pub type BlockCompatibleRawHash = hash_x11::Hash;
94+
8795
#[cfg(not(feature = "core-block-hash-use-x11"))]
8896
hash_newtype! {
8997
/// A dash block hash.
9098
pub struct BlockHash(sha256d::Hash);
99+
/// A hash used to identify a quorum
100+
#[hash_newtype(forward)]
101+
pub struct QuorumHash(sha256d::Hash)
91102
}
92103

104+
#[cfg(not(feature = "core-block-hash-use-x11"))]
105+
/// A raw hash that is compatible with quorum and block hash.
106+
pub type BlockCompatibleRawHash = sha256d::Hash;
107+
93108
hash_newtype! {
94109
/// A dash transaction hash/transaction ID.
95110
pub struct Txid(sha256d::Hash);
@@ -168,12 +183,11 @@ mod newtypes {
168183
}
169184

170185
impl PartialOrd for ScoreHash {
171-
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.cmp(other)) }
186+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
187+
Some(self.cmp(other))
188+
}
172189
}
173190

174-
/// A hash used to identify a quorum
175-
pub type QuorumHash = BlockHash;
176-
177191
/// A hash used to identity a cycle
178192
pub type CycleHash = BlockHash;
179193

@@ -193,6 +207,7 @@ mod newtypes {
193207
impl_hashencode!(SpecialTransactionPayloadHash);
194208
impl_hashencode!(InputsHash);
195209

210+
impl_hashencode!(QuorumHash);
196211
impl_hashencode!(QuorumVVecHash);
197212
impl_hashencode!(QuorumSigningRequestId);
198213
impl_hashencode!(PubkeyHash);
@@ -211,10 +226,14 @@ mod newtypes {
211226

212227
impl Txid {
213228
/// Create a Txid from a string
214-
pub fn from_hex(s: &str) -> Result<Txid, Error> { Ok(Self(sha256d::Hash::from_str(s)?)) }
229+
pub fn from_hex(s: &str) -> Result<Txid, Error> {
230+
Ok(Self(sha256d::Hash::from_str(s)?))
231+
}
215232

216233
/// Convert a Txid to a string
217-
pub fn to_hex(&self) -> String { self.0.to_string() }
234+
pub fn to_hex(&self) -> String {
235+
self.0.to_string()
236+
}
218237
}
219238

220239
impl ProTxHash {
@@ -224,7 +243,9 @@ mod newtypes {
224243
}
225244

226245
/// Convert a ProTxHash to a string
227-
pub fn to_hex(&self) -> String { self.0.to_string() }
246+
pub fn to_hex(&self) -> String {
247+
self.0.to_string()
248+
}
228249
}
229250

230251
impl ScoreHash {
@@ -234,7 +255,9 @@ mod newtypes {
234255
}
235256

236257
/// Convert a ScoreHash to a string
237-
pub fn to_hex(&self) -> String { self.0.to_string() }
258+
pub fn to_hex(&self) -> String {
259+
self.0.to_string()
260+
}
238261

239262
/// Creates a score based on the optional confirmed hash and the quorum modifier.
240263
///
@@ -266,7 +289,9 @@ mod newtypes {
266289
}
267290

268291
/// Convert a ScoreHash to a string
269-
pub fn to_hex(&self) -> String { self.0.to_string() }
292+
pub fn to_hex(&self) -> String {
293+
self.0.to_string()
294+
}
270295

271296
/// Creates an ordering hash based on the quorum and request id.
272297
///
@@ -285,7 +310,9 @@ mod newtypes {
285310
}
286311

287312
impl Default for ConfirmedHash {
288-
fn default() -> Self { ConfirmedHash::from_byte_array([0; 32]) }
313+
fn default() -> Self {
314+
ConfirmedHash::from_byte_array([0; 32])
315+
}
289316
}
290317

291318
impl ConfirmedHash {
@@ -295,7 +322,9 @@ mod newtypes {
295322
}
296323

297324
/// Convert a ConfirmedHash to a string
298-
pub fn to_hex(&self) -> String { self.0.to_string() }
325+
pub fn to_hex(&self) -> String {
326+
self.0.to_string()
327+
}
299328
}
300329

301330
impl ConfirmedHashHashedWithProRegTx {
@@ -305,7 +334,9 @@ mod newtypes {
305334
}
306335

307336
/// Convert a ConfirmedHash to a string
308-
pub fn to_hex(&self) -> String { self.0.to_string() }
337+
pub fn to_hex(&self) -> String {
338+
self.0.to_string()
339+
}
309340

310341
/// Hashes the members
311342
pub fn hash_members(pro_tx_hash: &ProTxHash, confirmed_hash: &ConfirmedHash) -> Self {
@@ -329,7 +360,9 @@ mod newtypes {
329360
}
330361

331362
/// Convert a ConfirmedHash to a string
332-
pub fn to_hex(&self) -> String { self.0.to_string() }
363+
pub fn to_hex(&self) -> String {
364+
self.0.to_string()
365+
}
333366
}
334367

335368
impl InputsHash {
@@ -339,12 +372,16 @@ mod newtypes {
339372
}
340373

341374
/// Convert an InputsHash to a string
342-
pub fn to_hex(&self) -> String { self.0.to_string() }
375+
pub fn to_hex(&self) -> String {
376+
self.0.to_string()
377+
}
343378
}
344379

345380
impl SpecialTransactionPayloadHash {
346381
/// Create a SpecialTransactionPayloadHash from a string
347-
pub fn to_hex(&self) -> String { self.0.to_string() }
382+
pub fn to_hex(&self) -> String {
383+
self.0.to_string()
384+
}
348385
}
349386

350387
impl PubkeyHash {
@@ -354,6 +391,8 @@ mod newtypes {
354391
}
355392

356393
/// Convert a PubkeyHash to a string
357-
pub fn to_hex(&self) -> String { self.0.to_string() }
394+
pub fn to_hex(&self) -> String {
395+
self.0.to_string()
396+
}
358397
}
359398
}

0 commit comments

Comments
 (0)