Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
use bincode::{Decode, Encode};
use hashes::Hash;

use crate::blockdata::transaction::special_transaction::SpecialTransactionBasePayloadEncodable;
use crate::blockdata::transaction::special_transaction::asset_unlock::request_info::AssetUnlockRequestInfo;
use crate::blockdata::transaction::special_transaction::asset_unlock::unqualified_asset_unlock::AssetUnlockBasePayload;
use crate::blockdata::transaction::special_transaction::SpecialTransactionBasePayloadEncodable;
use crate::bls_sig_utils::BLSSignature;
use crate::consensus::{Decodable, Encodable, encode};
use crate::consensus::{encode, Decodable, Encodable};
use crate::hash_types::SpecialTransactionPayloadHash;
use crate::transaction::special_transaction::TransactionPayload;
use crate::transaction::special_transaction::asset_unlock::unqualified_asset_unlock::AssetUnlockBaseTransactionInfo;
use crate::{Transaction, TxIn, consensus, io};
use crate::transaction::special_transaction::TransactionPayload;
use crate::{consensus, io, Transaction, TxIn};

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

impl AssetUnlockPayload {
/// The size of the payload in bytes.
pub fn size(&self) -> usize { self.base.size() + self.request_info.size() + 96 }
pub fn size(&self) -> usize {
self.base.size() + self.request_info.size() + 96
}
}

impl SpecialTransactionBasePayloadEncodable for AssetUnlockPayload {
Expand Down Expand Up @@ -129,7 +131,11 @@ impl Decodable for AssetUnlockPayload {
let base = AssetUnlockBasePayload::consensus_decode(r)?;
let request_info = AssetUnlockRequestInfo::consensus_decode(r)?;
let quorum_sig = BLSSignature::consensus_decode(r)?;
Ok(AssetUnlockPayload { base, request_info, quorum_sig })
Ok(AssetUnlockPayload {
base,
request_info,
quorum_sig,
})
}
}

Expand All @@ -139,25 +145,29 @@ mod tests {

use hashes::Hash;
use hex::FromHex;
use internals::hex::Case;
use internals::hex::display::DisplayHex;
use internals::hex::Case;

use crate::bls_sig_utils::BLSSignature;
use crate::consensus::Encodable;
use crate::hash_types::QuorumHash;
use crate::transaction::special_transaction::TransactionPayload;
use crate::transaction::special_transaction::asset_unlock::qualified_asset_unlock::{
ASSET_UNLOCK_TX_SIZE, AssetUnlockPayload, build_asset_unlock_tx,
build_asset_unlock_tx, AssetUnlockPayload, ASSET_UNLOCK_TX_SIZE,
};
use crate::transaction::special_transaction::asset_unlock::request_info::AssetUnlockRequestInfo;
use crate::transaction::special_transaction::asset_unlock::unqualified_asset_unlock::AssetUnlockBasePayload;
use crate::{ScriptBuf, Transaction, TxOut, consensus};
use crate::transaction::special_transaction::TransactionPayload;
use crate::{consensus, ScriptBuf, Transaction, TxOut};

#[test]
fn size() {
let want = 145;
let payload = AssetUnlockPayload {
base: AssetUnlockBasePayload { version: 0, index: 0, fee: 0 },
base: AssetUnlockBasePayload {
version: 0,
index: 0,
fee: 0,
},
request_info: AssetUnlockRequestInfo {
request_height: 0,
quorum_hash: QuorumHash::all_zeros(),
Expand Down Expand Up @@ -185,7 +195,6 @@ mod tests {
"4acfa5c6d92071d206da5b767039d42f24e7ab1a694a5b8014cddc088311e448"
)
.unwrap()
.reverse()
);
assert_eq!(payload.quorum_sig, BLSSignature::from_str("aee468c03feec7caada0599457136ef0dfe9365657a42ef81bb4aa53af383d05d90552b2cd23480cae24036b953ba8480d2f98291271a338e4235265dea94feacb54d1fd96083151001eff4156e7475e998154a8e6082575e2ee461b394d24f7").unwrap());
}
Expand All @@ -200,7 +209,7 @@ mod tests {
},
request_info: AssetUnlockRequestInfo {
request_height: 1317,
quorum_hash: QuorumHash::from_str("4acfa5c6d92071d206da5b767039d42f24e7ab1a694a5b8014cddc088311e448").unwrap().reverse(),
quorum_hash: QuorumHash::from_str("4acfa5c6d92071d206da5b767039d42f24e7ab1a694a5b8014cddc088311e448").unwrap(),
},
quorum_sig: BLSSignature::from_str("aee468c03feec7caada0599457136ef0dfe9365657a42ef81bb4aa53af383d05d90552b2cd23480cae24036b953ba8480d2f98291271a338e4235265dea94feacb54d1fd96083151001eff4156e7475e998154a8e6082575e2ee461b394d24f7").unwrap()
};
Expand All @@ -216,11 +225,11 @@ mod tests {
let tx_bytes = Vec::from_hex("010009000001c8000000000000001976a914c35b782432294088e354bc28aa56d95736cb630288ac0000000001000000000000000070f915129f05000053c006055af6d0ae9aa9627df8615a71c312421a28c4712c8add83c8e1bfdadd").unwrap();
let tx_asset_unlock = build_asset_unlock_tx(&tx_bytes).unwrap();
let bytes_tx_asset_unlock = consensus::serialize(&tx_asset_unlock);
println!("tx_asset_unlock: {:?}", bytes_tx_asset_unlock);
// println!("tx_asset_unlock: {:?}", bytes_tx_asset_unlock);

let hex_tx_asset_unlock = bytes_tx_asset_unlock.to_hex_string(Case::Lower);
println!("hex_tx_asset_unlock: {:?}", hex_tx_asset_unlock);
println!("OK");
// println!("hex_tx_asset_unlock: {:?}", hex_tx_asset_unlock);
// println!("OK");
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,19 @@
//! It is defined in DIP6 [dip-0006.md](https://github.com/dashpay/dips/blob/master/dip-0006.md).
//!

#[cfg(feature = "bincode")]
use bincode::{Decode, Encode};

use crate::bls_sig_utils::{BLSPublicKey, BLSSignature};
use crate::consensus::encode::{
compact_size_len, fixed_bitset_len, read_compact_size, read_fixed_bitset, write_compact_size,
write_fixed_bitset,
};
use crate::consensus::{Decodable, Encodable, encode};
use crate::consensus::{encode, Decodable, Encodable};
use crate::hash_types::{QuorumHash, QuorumVVecHash};
use crate::io;
use crate::prelude::*;
use crate::sml::llmq_type::LLMQType;
use crate::sml::quorum_validation_error::QuorumValidationError;
#[cfg(feature = "bincode")]
use bincode::{Decode, Encode};

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

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

Expand Down Expand Up @@ -146,8 +146,11 @@ impl Decodable for QuorumEntry {
let version = u16::consensus_decode(r)?;
let llmq_type = LLMQType::consensus_decode(r)?;
let quorum_hash = QuorumHash::consensus_decode(r)?;
let quorum_index =
if version == 2 || version == 4 { Some(i16::consensus_decode(r)?) } else { None };
let quorum_index = if version == 2 || version == 4 {
Some(i16::consensus_decode(r)?)
} else {
None
};
let signers_count = read_compact_size(r)?;
let signers = read_fixed_bitset(r, signers_count as usize)?;
let valid_members_count = read_compact_size(r)?;
Expand Down Expand Up @@ -188,7 +191,9 @@ pub struct QuorumCommitmentPayload {

impl QuorumCommitmentPayload {
/// The size of the payload in bytes.
pub fn size(&self) -> usize { 2 + 4 + self.finalization_commitment.size() }
pub fn size(&self) -> usize {
2 + 4 + self.finalization_commitment.size()
}
}

impl Encodable for QuorumCommitmentPayload {
Expand All @@ -206,7 +211,11 @@ impl Decodable for QuorumCommitmentPayload {
let version = u16::consensus_decode(r)?;
let height = u32::consensus_decode(r)?;
let finalization_commitment = QuorumEntry::consensus_decode(r)?;
Ok(QuorumCommitmentPayload { version, height, finalization_commitment })
Ok(QuorumCommitmentPayload {
version,
height,
finalization_commitment,
})
}
}

Expand All @@ -215,7 +224,7 @@ mod tests {
use hashes::Hash;

use crate::bls_sig_utils::{BLSPublicKey, BLSSignature};
use crate::consensus::{Encodable, deserialize, serialize};
use crate::consensus::{deserialize, serialize, Encodable};
use crate::hash_types::{QuorumHash, QuorumVVecHash};
use crate::network::message::{NetworkMessage, RawNetworkMessage};
use crate::sml::llmq_type::LLMQType;
Expand Down
73 changes: 56 additions & 17 deletions dash/src/hash_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ mod newtypes {
#[cfg(feature = "core-block-hash-use-x11")]
use hashes::hash_x11;
use hashes::hex::Error;
use hashes::{Hash, hash_newtype, hash_newtype_no_ord, hash160, sha256, sha256d};
use hashes::{hash160, hash_newtype, hash_newtype_no_ord, sha256, sha256d, Hash};

use crate::alloc::string::ToString;
use crate::prelude::String;
Expand All @@ -83,13 +83,28 @@ mod newtypes {
hash_newtype! {
/// A dash block hash.
pub struct BlockHash(hash_x11::Hash);
/// A hash used to identify a quorum
#[hash_newtype(forward)]
pub struct QuorumHash(hash_x11::Hash);
}

#[cfg(feature = "core-block-hash-use-x11")]
/// A raw hash that is compatible with quorum and block hash.
pub type BlockCompatibleRawHash = hash_x11::Hash;

#[cfg(not(feature = "core-block-hash-use-x11"))]
hash_newtype! {
/// A dash block hash.
pub struct BlockHash(sha256d::Hash);
/// A hash used to identify a quorum
#[hash_newtype(forward)]
pub struct QuorumHash(sha256d::Hash)
}

#[cfg(not(feature = "core-block-hash-use-x11"))]
/// A raw hash that is compatible with quorum and block hash.
pub type BlockCompatibleRawHash = sha256d::Hash;

hash_newtype! {
/// A dash transaction hash/transaction ID.
pub struct Txid(sha256d::Hash);
Expand Down Expand Up @@ -168,12 +183,11 @@ mod newtypes {
}

impl PartialOrd for ScoreHash {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.cmp(other)) }
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

/// A hash used to identify a quorum
pub type QuorumHash = BlockHash;

/// A hash used to identity a cycle
pub type CycleHash = BlockHash;

Expand All @@ -193,6 +207,7 @@ mod newtypes {
impl_hashencode!(SpecialTransactionPayloadHash);
impl_hashencode!(InputsHash);

impl_hashencode!(QuorumHash);
impl_hashencode!(QuorumVVecHash);
impl_hashencode!(QuorumSigningRequestId);
impl_hashencode!(PubkeyHash);
Expand All @@ -211,10 +226,14 @@ mod newtypes {

impl Txid {
/// Create a Txid from a string
pub fn from_hex(s: &str) -> Result<Txid, Error> { Ok(Self(sha256d::Hash::from_str(s)?)) }
pub fn from_hex(s: &str) -> Result<Txid, Error> {
Ok(Self(sha256d::Hash::from_str(s)?))
}

/// Convert a Txid to a string
pub fn to_hex(&self) -> String { self.0.to_string() }
pub fn to_hex(&self) -> String {
self.0.to_string()
}
}

impl ProTxHash {
Expand All @@ -224,7 +243,9 @@ mod newtypes {
}

/// Convert a ProTxHash to a string
pub fn to_hex(&self) -> String { self.0.to_string() }
pub fn to_hex(&self) -> String {
self.0.to_string()
}
}

impl ScoreHash {
Expand All @@ -234,7 +255,9 @@ mod newtypes {
}

/// Convert a ScoreHash to a string
pub fn to_hex(&self) -> String { self.0.to_string() }
pub fn to_hex(&self) -> String {
self.0.to_string()
}

/// Creates a score based on the optional confirmed hash and the quorum modifier.
///
Expand Down Expand Up @@ -266,7 +289,9 @@ mod newtypes {
}

/// Convert a ScoreHash to a string
pub fn to_hex(&self) -> String { self.0.to_string() }
pub fn to_hex(&self) -> String {
self.0.to_string()
}

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

impl Default for ConfirmedHash {
fn default() -> Self { ConfirmedHash::from_byte_array([0; 32]) }
fn default() -> Self {
ConfirmedHash::from_byte_array([0; 32])
}
}

impl ConfirmedHash {
Expand All @@ -295,7 +322,9 @@ mod newtypes {
}

/// Convert a ConfirmedHash to a string
pub fn to_hex(&self) -> String { self.0.to_string() }
pub fn to_hex(&self) -> String {
self.0.to_string()
}
}

impl ConfirmedHashHashedWithProRegTx {
Expand All @@ -305,7 +334,9 @@ mod newtypes {
}

/// Convert a ConfirmedHash to a string
pub fn to_hex(&self) -> String { self.0.to_string() }
pub fn to_hex(&self) -> String {
self.0.to_string()
}

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

/// Convert a ConfirmedHash to a string
pub fn to_hex(&self) -> String { self.0.to_string() }
pub fn to_hex(&self) -> String {
self.0.to_string()
}
}

impl InputsHash {
Expand All @@ -339,12 +372,16 @@ mod newtypes {
}

/// Convert an InputsHash to a string
pub fn to_hex(&self) -> String { self.0.to_string() }
pub fn to_hex(&self) -> String {
self.0.to_string()
}
}

impl SpecialTransactionPayloadHash {
/// Create a SpecialTransactionPayloadHash from a string
pub fn to_hex(&self) -> String { self.0.to_string() }
pub fn to_hex(&self) -> String {
self.0.to_string()
}
}

impl PubkeyHash {
Expand All @@ -354,6 +391,8 @@ mod newtypes {
}

/// Convert a PubkeyHash to a string
pub fn to_hex(&self) -> String { self.0.to_string() }
pub fn to_hex(&self) -> String {
self.0.to_string()
}
}
}
Loading