-
Notifications
You must be signed in to change notification settings - Fork 39
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
refactor(platform): replace bls library #2257
base: v1.7-dev
Are you sure you want to change the base?
Changes from all commits
0c1ce6d
46d9698
7d44dcb
5b6d1f0
ab74355
e6bda11
5859848
b86245e
ae22fb8
6164e1b
98c0215
1267771
f745857
9f9c123
deda86d
01d5af7
0c4dd5c
6eeec1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ use bincode::enc::Encoder; | |
use bincode::error::EncodeError; | ||
#[cfg(feature = "core-types-serialization")] | ||
use bincode::{BorrowDecode, Decode, Encode}; | ||
use dashcore::blsful::Bls12381G2Impl; | ||
use dashcore::hashes::Hash; | ||
use dashcore::{ProTxHash, QuorumHash}; | ||
use itertools::Itertools; | ||
|
@@ -34,7 +35,7 @@ pub struct ValidatorSetV0 { | |
/// The list of masternodes | ||
pub members: BTreeMap<ProTxHash, ValidatorV0>, | ||
/// The threshold quorum public key | ||
pub threshold_public_key: BlsPublicKey, | ||
pub threshold_public_key: BlsPublicKey<Bls12381G2Impl>, | ||
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. Maybe we should define 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. Agree, but it will work only in case if we have abstraction on top of implementation. |
||
} | ||
|
||
impl Display for ValidatorSetV0 { | ||
|
@@ -61,7 +62,7 @@ impl Display for ValidatorSetV0 { | |
pro_tx_hash, validator.node_ip | ||
)) | ||
.join(", "), | ||
hex::encode(self.threshold_public_key.to_bytes().as_slice()) // Assuming BlsPublicKey is a byte array | ||
hex::encode(self.threshold_public_key.0.to_compressed()) // Assuming BlsPublicKey is a byte array | ||
QuantumExplorer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
} | ||
} | ||
|
@@ -85,7 +86,7 @@ impl Encode for ValidatorSetV0 { | |
|
||
// Custom encoding for BlsPublicKey if needed | ||
// Assuming BlsPublicKey can be serialized to a byte slice | ||
let public_key_bytes = *self.threshold_public_key.to_bytes(); | ||
let public_key_bytes = self.threshold_public_key.0.to_compressed(); | ||
QuantumExplorer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public_key_bytes.encode(encoder)?; | ||
|
||
Ok(()) | ||
|
@@ -118,9 +119,13 @@ impl Decode for ValidatorSetV0 { | |
let mut public_key_bytes = [0u8; 48]; | ||
let bytes = <[u8; 48]>::decode(decoder)?; | ||
public_key_bytes.copy_from_slice(&bytes); | ||
let threshold_public_key = BlsPublicKey::from_bytes(&public_key_bytes).map_err(|_| { | ||
bincode::error::DecodeError::OtherString("Failed to decode BlsPublicKey".to_string()) | ||
})?; | ||
let threshold_public_key = | ||
BlsPublicKey::try_from(public_key_bytes.as_slice()).map_err(|e| { | ||
bincode::error::DecodeError::OtherString(format!( | ||
"Failed to decode BlsPublicKey: {}", | ||
e | ||
)) | ||
})?; | ||
|
||
Ok(ValidatorSetV0 { | ||
quorum_hash: QuorumHash::from_byte_array(quorum_hash), | ||
|
@@ -162,11 +167,13 @@ impl<'de> BorrowDecode<'de> for ValidatorSetV0 { | |
let mut public_key_bytes = [0u8; 48]; | ||
let bytes = <[u8; 48]>::decode(decoder)?; | ||
public_key_bytes.copy_from_slice(&bytes); | ||
let threshold_public_key = BlsPublicKey::from_bytes(&public_key_bytes).map_err(|_| { | ||
bincode::error::DecodeError::OtherString( | ||
"Failed to decode BlsPublicKey in borrow decode".to_string(), | ||
) | ||
})?; | ||
let threshold_public_key = | ||
BlsPublicKey::try_from(public_key_bytes.as_slice()).map_err(|e| { | ||
bincode::error::DecodeError::OtherString(format!( | ||
"Failed to decode BlsPublicKey in borrow decode: {}", | ||
e | ||
)) | ||
})?; | ||
|
||
Ok(ValidatorSetV0 { | ||
quorum_hash: QuorumHash::from_byte_array(quorum_hash), | ||
|
@@ -211,7 +218,7 @@ pub trait ValidatorSetV0Getters { | |
/// Returns the members of the validator set. | ||
fn members_owned(self) -> BTreeMap<ProTxHash, ValidatorV0>; | ||
/// Returns the threshold public key of the validator set. | ||
fn threshold_public_key(&self) -> &BlsPublicKey; | ||
fn threshold_public_key(&self) -> &BlsPublicKey<Bls12381G2Impl>; | ||
} | ||
|
||
/// Trait providing setter methods for `ValidatorSetV0` struct | ||
|
@@ -225,7 +232,7 @@ pub trait ValidatorSetV0Setters { | |
/// Sets the members of the validator set. | ||
fn set_members(&mut self, members: BTreeMap<ProTxHash, ValidatorV0>); | ||
/// Sets the threshold public key of the validator set. | ||
fn set_threshold_public_key(&mut self, threshold_public_key: BlsPublicKey); | ||
fn set_threshold_public_key(&mut self, threshold_public_key: BlsPublicKey<Bls12381G2Impl>); | ||
} | ||
|
||
impl ValidatorSetV0Getters for ValidatorSetV0 { | ||
|
@@ -253,7 +260,7 @@ impl ValidatorSetV0Getters for ValidatorSetV0 { | |
self.members | ||
} | ||
|
||
fn threshold_public_key(&self) -> &BlsPublicKey { | ||
fn threshold_public_key(&self) -> &BlsPublicKey<Bls12381G2Impl> { | ||
&self.threshold_public_key | ||
} | ||
} | ||
|
@@ -275,7 +282,7 @@ impl ValidatorSetV0Setters for ValidatorSetV0 { | |
self.members = members; | ||
} | ||
|
||
fn set_threshold_public_key(&mut self, threshold_public_key: BlsPublicKey) { | ||
fn set_threshold_public_key(&mut self, threshold_public_key: BlsPublicKey<Bls12381G2Impl>) { | ||
self.threshold_public_key = threshold_public_key; | ||
} | ||
} | ||
|
@@ -284,7 +291,10 @@ impl ValidatorSetV0Setters for ValidatorSetV0 { | |
mod tests { | ||
use super::*; | ||
use bincode::config; | ||
use dashcore::blsful::SecretKey; | ||
use dashcore::PubkeyHash; | ||
use rand::rngs::StdRng; | ||
use rand::SeedableRng; | ||
use std::collections::BTreeMap; | ||
|
||
#[test] | ||
|
@@ -296,7 +306,8 @@ mod tests { | |
|
||
// Create a sample ProTxHash and ValidatorV0 instance | ||
let pro_tx_hash = ProTxHash::from_slice(&[2; 32]).unwrap(); | ||
let public_key = Some(BlsPublicKey::generate()); | ||
let mut rng = StdRng::seed_from_u64(0); | ||
let public_key = Some(SecretKey::<Bls12381G2Impl>::random(&mut rng).public_key()); | ||
let node_ip = "192.168.1.1".to_string(); | ||
let node_id = PubkeyHash::from_slice(&[4; 20]).unwrap(); | ||
let validator = ValidatorV0 { | ||
|
@@ -315,7 +326,7 @@ mod tests { | |
members.insert(pro_tx_hash, validator); | ||
|
||
// Create a sample threshold public key | ||
let threshold_public_key = BlsPublicKey::generate(); | ||
let threshold_public_key = SecretKey::<Bls12381G2Impl>::random(&mut rng).public_key(); | ||
|
||
// Create the ValidatorSetV0 instance | ||
let validator_set = ValidatorSetV0 { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused import