Skip to content
Merged
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
6 changes: 3 additions & 3 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 packages/rs-dpp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ tokio = { version = "1.17", features = ["full"] }
pretty_assertions = { version = "1.3.0" }

[features]
default = ["fixtures-and-mocks"]
default = ["fixtures-and-mocks", "cbor"]
cbor = ["ciborium"]
fixtures-and-mocks = ["mockall"]
19 changes: 16 additions & 3 deletions packages/rs-dpp/src/data_contract/data_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,8 @@ mod test {
}

#[test]
fn conversion_to_buffer_from_buffer() {
#[cfg(feature = "cbor")]
fn conversion_to_cbor_buffer_from_cbor_buffer() {
init();
let data_contract = get_data_contract_fixture(None);

Expand Down Expand Up @@ -848,7 +849,8 @@ mod test {
}

#[test]
fn conversion_to_buffer_from_buffer_high_version() {
#[cfg(feature = "cbor")]
fn conversion_to_cbor_buffer_from_cbor_buffer_high_version() {
init();
let mut data_contract = get_data_contract_fixture(None);
data_contract.protocol_version = 10000;
Expand Down Expand Up @@ -880,7 +882,7 @@ mod test {
}

#[test]
fn conversion_to_buffer_from_buffer_too_high_version() {
fn conversion_to_cbor_buffer_from_cbor_buffer_too_high_version() {
init();
let data_contract = get_data_contract_fixture(None);

Expand Down Expand Up @@ -1046,4 +1048,15 @@ mod test {

assert_eq!(hex::encode(data_contract_cbor), hex::encode(serialized));
}

#[test]
fn serialize_deterministically_serialize_to_bincode() {
let data_contract_cbor = get_data_contract_cbor_bytes();

let data_contract = DataContract::from_cbor_buffer(&data_contract_cbor).unwrap();

let serialized = data_contract.to_cbor_buffer().unwrap();

assert_eq!(hex::encode(data_contract_cbor), hex::encode(serialized));
}
}
11 changes: 11 additions & 0 deletions packages/rs-dpp/src/identity/identity_public_key/key_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ impl KeyType {
KEY_TYPE_SIZES[self]
}

/// Are keys of this type unique?
pub fn is_unique_key_type(&self) -> bool {
match self {
KeyType::ECDSA_SECP256K1 => true,
KeyType::BLS12_381 => true,
KeyType::ECDSA_HASH160 => false,
KeyType::BIP13_SCRIPT_HASH => false,
KeyType::EDDSA_25519_HASH160 => false,
}
}

//todo: put this in a specific feature
/// Gets the default size of the public key
pub fn random_public_key_data(&self, rng: &mut StdRng) -> Vec<u8> {
Expand Down
5 changes: 4 additions & 1 deletion packages/rs-dpp/src/identity/identity_public_key/purpose.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::identity::Purpose::{AUTHENTICATION, DECRYPTION, ENCRYPTION, SYSTEM, WITHDRAW};
use crate::identity::Purpose::{AUTHENTICATION, DECRYPTION, ENCRYPTION, SYSTEM, VOTING, WITHDRAW};
use anyhow::bail;
use bincode::{Decode, Encode};
#[cfg(feature = "cbor")]
Expand Down Expand Up @@ -32,6 +32,8 @@ pub enum Purpose {
WITHDRAW = 3,
/// this key cannot be used for signing documents
SYSTEM = 4,
/// this key cannot be used for signing documents
VOTING = 5,
}

impl TryFrom<u8> for Purpose {
Expand All @@ -43,6 +45,7 @@ impl TryFrom<u8> for Purpose {
2 => Ok(DECRYPTION),
3 => Ok(WITHDRAW),
4 => Ok(SYSTEM),
5 => Ok(VOTING),
value => bail!("unrecognized purpose: {}", value),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@
"type": "array",
"byteArray": true,
"minItems": 32,
"maxItems": 32
"maxItems": 32,
"contentMediaType": "application/x.dash.dpp.identifier"
},
"encryptedPublicKey": {
"type": "array",
Expand Down
2 changes: 1 addition & 1 deletion packages/rs-drive-abci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ tracing-subscriber = { version = "0.3.16", default-features = false, features =
"ansi",
], optional = true }
atty = { version = "0.2.14", optional = true }
tenderdash-abci = { git = "https://github.com/dashpay/rs-tenderdash-abci", branch = "fix/signatures", optional = true }
tenderdash-abci = { git = "https://github.com/dashpay/rs-tenderdash-abci", branch = "master", optional = true }
# tenderdash-abci = { path = "../../../rs-tenderdash-abci/abci", optional = true }
anyhow = { version = "1.0.70" }
lazy_static = "1.4.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/rs-drive-abci/src/abci/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Commit {
height: height as i64,
round: ci.round as i32,
// we need to "un-reverse" quorum hash, as it was reversed in [CleanedCommitInfo::try_from]
quorum_hash: ci.quorum_hash.iter().rev().cloned().collect(),
quorum_hash: ci.quorum_hash.to_vec(),
threshold_block_signature: ci.block_signature.to_vec(),
threshold_vote_extensions: ci.threshold_vote_extensions.to_vec(),
},
Expand Down
43 changes: 39 additions & 4 deletions packages/rs-drive-abci/src/abci/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,48 @@ where
..Default::default()
};

let mut block_execution_context_guard =
self.platform.block_execution_context.write().unwrap();

let block_execution_context = block_execution_context_guard
.as_mut()
.expect("expected that a block execution context was set");
block_execution_context.proposer_results = Some(response.clone());

Ok(response)
}

fn process_proposal(
&self,
mut request: RequestProcessProposal,
) -> Result<ResponseProcessProposal, ResponseException> {
let mut block_execution_context_guard =
self.platform.block_execution_context.write().unwrap();

if let Some(block_execution_context) = block_execution_context_guard.as_mut() {
// We are already in a block
// This only makes sense if we were the proposer
let Some(proposal_info) = block_execution_context.proposer_results.as_ref() else {
return Err(Error::Abci(AbciError::BadRequest(
"received a process proposal request twice".to_string(),
)))?;
};
// We need to set the block hash
block_execution_context.block_state_info.block_hash =
Some(request.hash.clone().try_into().map_err(|_| {
Error::Abci(AbciError::BadRequestDataSize(
"block hash is not 32 bytes in process proposal".to_string(),
))
})?);
return Ok(ResponseProcessProposal {
status: proto::response_process_proposal::ProposalStatus::Accept.into(),
app_hash: proposal_info.app_hash.clone(),
tx_results: proposal_info.tx_results.clone(),
consensus_param_updates: proposal_info.consensus_param_updates.clone(),
validator_set_update: proposal_info.validator_set_update.clone(),
});
}

let transaction_guard = if request.height == self.platform.config.abci.genesis_height as i64
{
// special logic on init chain
Expand Down Expand Up @@ -267,9 +302,9 @@ where
block_hash.clone(),
)? {
return Err(Error::from(AbciError::RequestForWrongBlockReceived(format!(
"received request for height: {} round: {}, block: {}; expected height: {} round: {}, block: {}",
"received extend vote request for height: {} round: {}, block: {}; expected height: {} round: {}, block: {}",
height, round, block_hash.to_hex(),
block_state_info.height, block_state_info.round, block_state_info.block_hash.to_hex()
block_state_info.height, block_state_info.round, block_state_info.block_hash.map(|block_hash| block_hash.to_hex()).unwrap_or("None".to_string())
)))
.into());
} else {
Expand Down Expand Up @@ -317,9 +352,9 @@ where
block_hash.clone(),
)? {
return Err(Error::from(AbciError::RequestForWrongBlockReceived(format!(
"received request for height: {} round: {}, block: {}; expected height: {} round: {}, block: {}",
"received verify vote request for height: {} round: {}, block: {}; expected height: {} round: {}, block: {}",
height, round,block_hash.to_hex(),
block_state_info.height, block_state_info.round, block_state_info.block_hash.to_hex()
block_state_info.height, block_state_info.round, block_state_info.block_hash.map(|block_hash| block_hash.to_hex()).unwrap_or("None".to_string())
Comment thread
lklimek marked this conversation as resolved.
)))
.into());
}
Expand Down
75 changes: 52 additions & 23 deletions packages/rs-drive-abci/src/abci/mimic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,22 @@ use dashcore_rpc::dashcore::blockdata::transaction::special_transaction::Transac
use dashcore_rpc::dashcore::bls_sig_utils::BLSSignature;
use dashcore_rpc::dashcore::consensus::Decodable;
use dashcore_rpc::dashcore;
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use dpp::block::block_info::BlockInfo;
use dpp::serialization_traits::PlatformSerializable;
use dpp::state_transition::StateTransition;
use dpp::util::deserializer::ProtocolVersion;
use tenderdash_abci::proto::abci::response_verify_vote_extension::VerifyStatus;
use tenderdash_abci::proto::abci::{
CommitInfo, RequestExtendVote, RequestFinalizeBlock, RequestPrepareProposal,
RequestVerifyVoteExtension, ResponsePrepareProposal, ValidatorSetUpdate,
};
use tenderdash_abci::proto::abci::{CommitInfo, RequestExtendVote, RequestFinalizeBlock, RequestPrepareProposal, RequestProcessProposal, RequestVerifyVoteExtension, ResponsePrepareProposal, ResponseProcessProposal, ValidatorSetUpdate};
use tenderdash_abci::proto::google::protobuf::Timestamp;
use tenderdash_abci::proto::types::{
Block, BlockId, Data, EvidenceList, Header, PartSetHeader, VoteExtension, VoteExtensionType,
};
use tenderdash_abci::{
signatures::SignDigest,
proto::{self, version::Consensus},
Application
Application,
};

/// The outcome struct when mimicking block execution
Expand All @@ -47,11 +46,13 @@ impl<'a, C: CoreRPCLike> AbciApplication<'a, C> {
proposer_pro_tx_hash: [u8; 32],
current_quorum: &TestQuorumInfo,
proposed_version: ProtocolVersion,
_total_hpmns: u32,
block_info: BlockInfo,
expect_validation_errors: bool,
state_transitions: Vec<StateTransition>,
) -> Result<MimicExecuteBlockOutcome, Error> {
let mut rng = StdRng::seed_from_u64(block_info.height);
let block_hash: [u8; 32] = rng.gen(); // We fake a block hash for the test
let next_validators_hash: [u8; 32] = rng.gen(); // We fake a block hash for the test
let serialized_state_transitions = state_transitions
.into_iter()
.map(|st| st.serialize().map_err(Error::Protocol))
Expand All @@ -64,17 +65,19 @@ impl<'a, C: CoreRPCLike> AbciApplication<'a, C> {
epoch: _,
} = block_info;

// PREPARE (also processes internally)

let request_prepare_proposal = RequestPrepareProposal {
max_tx_bytes: 0,
txs: serialized_state_transitions,
txs: serialized_state_transitions.clone(),
local_last_commit: None,
misbehavior: vec![],
height: height as i64,
time: Some(Timestamp {
seconds: (time_ms / 1000) as i64,
nanos: ((time_ms % 1000) * 1000) as i32,
}),
next_validators_hash: vec![],
next_validators_hash: next_validators_hash.to_vec(),
round: 0,
core_chain_locked_height: core_height,
proposer_pro_tx_hash: proposer_pro_tx_hash.to_vec(),
Expand Down Expand Up @@ -117,10 +120,41 @@ impl<'a, C: CoreRPCLike> AbciApplication<'a, C> {
})?;
}

// PROCESS

let request_process_proposal = RequestProcessProposal {
txs: serialized_state_transitions,
proposed_last_commit: None,
misbehavior: vec![],
hash: block_hash.to_vec(),
height: height as i64,
time: Some(Timestamp {
seconds: (time_ms / 1000) as i64,
nanos: ((time_ms % 1000) * 1000) as i32,
}),
next_validators_hash: next_validators_hash.to_vec(),
round: 0,
core_chain_locked_height: core_height,
core_chain_lock_update,
proposer_pro_tx_hash: proposer_pro_tx_hash.to_vec(),
proposed_app_version: proposed_version as u64,
version: Some(Consensus { block: 0, app: 0 }),
quorum_hash: current_quorum.quorum_hash.to_vec(),
};

//we must call process proposal so the app hash is set
self.process_proposal(request_process_proposal)
.unwrap_or_else(|e| {
panic!(
"should skip processing (because we prepared it) block #{} at time #{} : {:?}",
block_info.height, block_info.time_ms, e
)
});

let tx_order_for_finalize_block = tx_records.into_iter().map(|record| record.tx).collect();

let request_extend_vote = RequestExtendVote {
hash: [0; 32].to_vec(), //todo
hash: block_hash.to_vec(),
height: height as i64,
round: 0,
};
Expand All @@ -138,7 +172,7 @@ impl<'a, C: CoreRPCLike> AbciApplication<'a, C> {

for validator in current_quorum.validator_set.iter() {
let request_verify_vote_extension = RequestVerifyVoteExtension {
hash: [0; 32].to_vec(), //todo
hash: block_hash.to_vec(),
validator_pro_tx_hash: validator.pro_tx_hash.to_vec(),
height: height as i64,
round: 0,
Expand Down Expand Up @@ -214,8 +248,6 @@ impl<'a, C: CoreRPCLike> AbciApplication<'a, C> {
drop(guarded_block_execution_context);

// We need to sign the block hash

let block_hash = [0; 32]; //todo
let chain_id = "strategy_tests".to_string();
let quorum_type = self.platform.config.quorum_type();

Expand All @@ -225,9 +257,12 @@ impl<'a, C: CoreRPCLike> AbciApplication<'a, C> {
state_id: [0; 32].to_vec(), //todo
};

let mut quorum_hash = current_quorum.quorum_hash.to_vec();
// quorum_hash.reverse();

let mut commit_info = CommitInfo {
round: 0,
quorum_hash: current_quorum.quorum_hash.to_vec(),
quorum_hash: quorum_hash.clone(),
block_signature: Default::default(),
threshold_vote_extensions: extensions,
};
Expand All @@ -236,22 +271,16 @@ impl<'a, C: CoreRPCLike> AbciApplication<'a, C> {
block_id: Some(block_id.clone()),
height: height as i64,
round: 0,
quorum_hash: current_quorum.quorum_hash.to_vec(),
quorum_hash: quorum_hash.clone(),
threshold_block_signature: Default::default(),
threshold_vote_extensions: Default::default(),
};

//if not in testing this will default to true
if self.platform.config.testing_configs.block_signing {
let quorum_hash:[u8;32] = current_quorum.quorum_hash[..].try_into().expect("wrong quorum hash len");
let quorum_hash: [u8; 32] = quorum_hash.try_into().expect("wrong quorum hash len");
let digest = commit
.sign_digest(
&chain_id,
quorum_type as u8,
&quorum_hash,
height as i64,
0,
)
.sign_digest(&chain_id, quorum_type as u8, &quorum_hash, height as i64, 0)
.expect("expected to sign digest");

let block_signature = current_quorum.private_key.sign(digest.as_slice());
Expand All @@ -268,7 +297,7 @@ impl<'a, C: CoreRPCLike> AbciApplication<'a, C> {
let request_finalize_block = RequestFinalizeBlock {
commit: Some(commit_info),
misbehavior: vec![],
hash: app_hash.clone(), //todo: change this to block hash
hash: block_hash.to_vec(),
height: height as i64,
round: 0,
block: Some(Block {
Expand Down
Loading