Skip to content

Commit

Permalink
fix: voting proofs work as intended and various fixes (#1910)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumExplorer authored Jun 28, 2024
1 parent 467bfa7 commit b33eb5a
Show file tree
Hide file tree
Showing 39 changed files with 2,554 additions and 250 deletions.
207 changes: 168 additions & 39 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/dapi-grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ tonic = { version = "0.11", features = [
serde = { version = "1.0.197", optional = true, features = ["derive"] }
serde_bytes = { version = "0.11.12", optional = true }
serde_json = { version = "1.0", optional = true }
tenderdash-proto = { git = "https://github.com/dashpay/rs-tenderdash-abci", version = "0.14.0-dev.12", default-features = false }
tenderdash-proto = { git = "https://github.com/dashpay/rs-tenderdash-abci", version = "1.0.0-dev.1", default-features = false }
dapi-grpc-macros = { path = "../rs-dapi-grpc-macros" }
platform-version = { path = "../rs-platform-version" }

Expand Down
7 changes: 4 additions & 3 deletions packages/rs-dpp/src/voting/contender_structs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use platform_value::string_encoding::Encoding;
use platform_value::Identifier;
use platform_version::version::PlatformVersion;
use std::fmt;
use std::fmt::format;

pub use contender::v0::{ContenderV0, ContenderWithSerializedDocumentV0};
pub use contender::{Contender, ContenderWithSerializedDocument};
Expand Down Expand Up @@ -38,15 +39,15 @@ pub struct FinalizedContenderWithSerializedDocument {
pub struct FinalizedResourceVoteChoicesWithVoterInfo {
/// The resource vote choice.
pub resource_vote_choice: ResourceVoteChoice,
/// The pro_tx_hashes of the voters for this contender.
pub voters: Vec<Identifier>,
/// The pro_tx_hashes of the voters for this contender along with their strength
pub voters: Vec<(Identifier, u8)>,
}
impl fmt::Display for FinalizedResourceVoteChoicesWithVoterInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let voters_str: Vec<String> = self
.voters
.iter()
.map(|v| v.to_string(Encoding::Base58))
.map(|(id, strength)| format!("{}:{}", id, strength))
.collect();
write!(
f,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl ContestedDocumentVotePollStoredInfoV0Getters for ContestedDocumentVotePollS
}
}

fn last_locked_voters(&self) -> Option<Vec<Identifier>> {
fn last_locked_voters(&self) -> Option<Vec<(Identifier, u8)>> {
match self {
ContestedDocumentVotePollStoredInfo::V0(v0) => v0.last_locked_voters(),
}
Expand All @@ -178,7 +178,7 @@ impl ContestedDocumentVotePollStoredInfoV0Getters for ContestedDocumentVotePollS
}
}

fn last_abstain_voters(&self) -> Option<Vec<Identifier>> {
fn last_abstain_voters(&self) -> Option<Vec<(Identifier, u8)>> {
match self {
ContestedDocumentVotePollStoredInfo::V0(v0) => v0.last_abstain_voters(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ pub trait ContestedDocumentVotePollStoredInfoV0Getters {

fn last_locked_votes(&self) -> Option<u32>;

fn last_locked_voters(&self) -> Option<Vec<Identifier>>;
fn last_locked_voters(&self) -> Option<Vec<(Identifier, u8)>>;

fn last_abstain_votes(&self) -> Option<u32>;

fn last_abstain_voters(&self) -> Option<Vec<Identifier>>;
fn last_abstain_voters(&self) -> Option<Vec<(Identifier, u8)>>;
fn contender_votes_in_vec_of_contender_with_serialized_document(
&self,
) -> Option<Vec<ContenderWithSerializedDocument>>;
Expand Down Expand Up @@ -218,12 +218,19 @@ impl ContestedDocumentVotePollStoredInfoV0Getters for ContestedDocumentVotePollS
.filter(|choice| {
matches!(choice.resource_vote_choice, ResourceVoteChoice::Lock)
})
.map(|choice| choice.voters.len() as u32)
.map(|choice| {
let sum: u32 = choice
.voters
.iter()
.map(|(_, strength)| *strength as u32)
.sum();
sum
})
.sum()
})
}

fn last_locked_voters(&self) -> Option<Vec<Identifier>> {
fn last_locked_voters(&self) -> Option<Vec<(Identifier, u8)>> {
self.last_resource_vote_choices()
.map(|resource_vote_choices| {
resource_vote_choices
Expand All @@ -244,12 +251,19 @@ impl ContestedDocumentVotePollStoredInfoV0Getters for ContestedDocumentVotePollS
.filter(|choice| {
matches!(choice.resource_vote_choice, ResourceVoteChoice::Abstain)
})
.map(|choice| choice.voters.len() as u32)
.map(|choice| {
let sum: u32 = choice
.voters
.iter()
.map(|(_, strength)| *strength as u32)
.sum();
sum
})
.sum()
})
}

fn last_abstain_voters(&self) -> Option<Vec<Identifier>> {
fn last_abstain_voters(&self) -> Option<Vec<(Identifier, u8)>> {
self.last_resource_vote_choices()
.map(|resource_vote_choices| {
resource_vote_choices
Expand All @@ -273,11 +287,16 @@ impl ContestedDocumentVotePollStoredInfoV0Getters for ContestedDocumentVotePollS
if let ResourceVoteChoice::TowardsIdentity(identity_id) =
&choice.resource_vote_choice
{
let vote_tally: u32 = choice
.voters
.iter()
.map(|(_, strength)| *strength as u32)
.sum();
Some(
ContenderWithSerializedDocumentV0 {
identity_id: *identity_id,
serialized_document: None,
vote_tally: Some(choice.voters.len() as u32),
vote_tally: Some(vote_tally),
}
.into(),
)
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 @@ -55,7 +55,7 @@ tracing-subscriber = { version = "0.3.16", default-features = false, features =
"tracing-log",
], optional = false }
atty = { version = "0.2.14", optional = false }
tenderdash-abci = { git = "https://github.com/dashpay/rs-tenderdash-abci", version = "0.14.0-dev.12", features = [
tenderdash-abci = { git = "https://github.com/dashpay/rs-tenderdash-abci", version = "1.0.0-dev.1", features = [
"grpc",
] }
lazy_static = "1.4.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,17 @@ where
platform_version,
)?;

// Run all dao platform events, such as vote tallying and distribution of contested documents
// This must be done before state transition processing
// Otherwise we would expect a proof after a successful vote that has since been cleaned up.
self.run_dao_platform_events(
&block_info,
last_committed_platform_state,
&block_platform_state,
Some(transaction),
platform_version,
)?;

// Process transactions
let state_transitions_result = self.process_raw_state_transitions(
raw_state_transitions,
Expand All @@ -313,16 +324,6 @@ where
platform_version,
)?;

// Run all dao platform events, such as vote tallying and distribution of contested documents

self.run_dao_platform_events(
&block_info,
last_committed_platform_state,
&block_platform_state,
Some(transaction),
platform_version,
)?;

// Create a new block execution context

let mut block_execution_context: BlockExecutionContext =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ where
)?)
}
ExecutionEvent::PaidFromAssetLockWithoutIdentity { .. }
| ExecutionEvent::PaidFixedCost { .. }
| ExecutionEvent::Free { .. } => None,
};

Expand Down Expand Up @@ -154,6 +155,29 @@ where
))
}
}
ExecutionEvent::PaidFixedCost {
operations,
fees_to_add_to_pool,
} => {
if consensus_errors.is_empty() {
self.drive
.apply_drive_operations(
operations,
true,
block_info,
Some(transaction),
platform_version,
)
.map_err(Error::Drive)?;

Ok(SuccessfulPaidExecution(
None,
FeeResult::default_with_fees(0, fees_to_add_to_pool),
))
} else {
Ok(UnpaidConsensusExecutionError(consensus_errors))
}
}
ExecutionEvent::Free { operations } => {
self.drive
.apply_drive_operations(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ where
))
}
}
ExecutionEvent::Free { .. }
ExecutionEvent::PaidFixedCost { .. }
| ExecutionEvent::Free { .. }
| ExecutionEvent::PaidFromAssetLockWithoutIdentity { .. } => Ok(
ConsensusValidationResult::new_with_data(FeeResult::default()),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::error::execution::ExecutionError;
use crate::error::Error;
use crate::platform_types::platform::Platform;
use crate::platform_types::platform_state::PlatformState;
use crate::rpc::core::CoreRPCLike;
use dpp::block::block_info::BlockInfo;
use dpp::version::PlatformVersion;
Expand All @@ -15,6 +16,7 @@ where
/// Checks for ended vote polls
pub(in crate::execution) fn check_for_ended_vote_polls(
&self,
block_platform_state: &PlatformState,
block_info: &BlockInfo,
transaction: TransactionArg,
platform_version: &PlatformVersion,
Expand All @@ -25,7 +27,12 @@ where
.voting
.check_for_ended_vote_polls
{
0 => self.check_for_ended_vote_polls_v0(block_info, transaction, platform_version),
0 => self.check_for_ended_vote_polls_v0(
block_platform_state,
block_info,
transaction,
platform_version,
),
version => Err(Error::Execution(ExecutionError::UnknownVersionMismatch {
method: "check_for_ended_vote_polls".to_string(),
known_versions: vec![0],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::error::Error;
use crate::platform_types::platform::Platform;
use crate::platform_types::platform_state::PlatformState;
use crate::rpc::core::CoreRPCLike;
use dpp::block::block_info::BlockInfo;
use dpp::document::DocumentV0Getters;
Expand All @@ -23,6 +24,7 @@ where
#[inline(always)]
pub(super) fn check_for_ended_vote_polls_v0(
&self,
block_platform_state: &PlatformState,
block_info: &BlockInfo,
transaction: TransactionArg,
platform_version: &PlatformVersion,
Expand Down Expand Up @@ -69,27 +71,23 @@ where

let (contenders_with_votes, contenders_with_no_votes) : (Vec<_>, Vec<_>) = sorted_contenders.iter().partition(|a| a.final_vote_tally > 0);

let (restrict_to_only_fetch_contenders, mut other_contenders) = if contenders_with_no_votes.is_empty()
let fetch_contenders = contenders_with_votes
.iter()
.map(|contender| contender.identity_id)
.collect::<Vec<_>>();
let mut other_contenders = if contenders_with_no_votes.is_empty()
{
(None, BTreeMap::new())
BTreeMap::new()
} else {
// Collect all identity_ids from contenders_with_votes
let restrict_to_only_fetch_contenders = Some(
contenders_with_votes
.iter()
.map(|contender| contender.identity_id)
.collect::<Vec<_>>(),
);

// Other contenders are only those with no votes
(restrict_to_only_fetch_contenders, contenders_with_no_votes.into_iter().map(|contender| (TowardsIdentity(contender.identity_id), vec![])).collect())
contenders_with_no_votes.into_iter().map(|contender| (TowardsIdentity(contender.identity_id), vec![])).collect()
};

// We need to get the votes of the sorted contenders
let mut identifiers_voting_for_contenders =
self.drive.fetch_identities_voting_for_contenders(
&resolved_contested_document_resource_vote_poll,
restrict_to_only_fetch_contenders,
fetch_contenders,
true,
transaction,
platform_version,
Expand Down Expand Up @@ -156,6 +154,7 @@ where
};
// We want to keep a record of how everyone voted
self.keep_record_of_finished_contested_resource_vote_poll(
block_platform_state,
block_info,
&resolved_contested_document_resource_vote_poll,
&identifiers_voting_for_contenders,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::error::execution::ExecutionError;
use crate::error::Error;
use crate::platform_types::platform::Platform;
use crate::platform_types::platform_state::PlatformState;
use crate::rpc::core::CoreRPCLike;
use dpp::block::block_info::BlockInfo;
use dpp::identifier::Identifier;
Expand All @@ -20,6 +21,7 @@ where
/// Keeps a record of the vote poll after it has finished
pub(in crate::execution) fn keep_record_of_finished_contested_resource_vote_poll(
&self,
block_platform_state: &PlatformState,
block_info: &BlockInfo,
vote_poll: &ContestedDocumentResourceVotePollWithContractInfo,
contender_votes: &BTreeMap<ResourceVoteChoice, Vec<Identifier>>,
Expand All @@ -34,6 +36,7 @@ where
.keep_record_of_finished_contested_resource_vote_poll
{
0 => self.keep_record_of_finished_contested_resource_vote_poll_v0(
block_platform_state,
block_info,
vote_poll,
contender_votes,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
use crate::error::Error;
use crate::platform_types::platform::Platform;
use crate::platform_types::platform_state::v0::PlatformStateV0Methods;
use crate::platform_types::platform_state::PlatformState;
use crate::rpc::core::CoreRPCLike;
use dashcore_rpc::dashcore_rpc_json::MasternodeType;
use dpp::block::block_info::BlockInfo;
use dpp::consensus::state::voting::masternode_not_found_error::MasternodeNotFoundError;
use dpp::dashcore::hashes::Hash;
use dpp::dashcore::ProTxHash;
use dpp::identifier::Identifier;
use dpp::prelude::ConsensusValidationResult;
use dpp::version::PlatformVersion;
use dpp::voting::contender_structs::FinalizedResourceVoteChoicesWithVoterInfo;
use dpp::voting::vote_choices::resource_vote_choice::ResourceVoteChoice;
Expand All @@ -20,6 +27,7 @@ where
#[inline(always)]
pub(super) fn keep_record_of_finished_contested_resource_vote_poll_v0(
&self,
block_platform_state: &PlatformState,
block_info: &BlockInfo,
vote_poll: &ContestedDocumentResourceVotePollWithContractInfo,
contender_votes: &BTreeMap<ResourceVoteChoice, Vec<Identifier>>,
Expand All @@ -29,12 +37,30 @@ where
) -> Result<(), Error> {
let finalized_resource_vote_choices_with_voter_infos = contender_votes
.iter()
.map(
|(resource_vote_choice, voters)| FinalizedResourceVoteChoicesWithVoterInfo {
resource_vote_choice: resource_vote_choice.clone(),
voters: voters.clone(),
},
)
.map(|(resource_vote_choice, voters)| {
let full_masternode_list = block_platform_state.full_masternode_list();
let voters = voters
.iter()
.map(|pro_tx_hash_identifier| {
let strength = if let Some(masternode) = full_masternode_list.get(
&ProTxHash::from_byte_array(pro_tx_hash_identifier.to_buffer()),
) {
match masternode.node_type {
MasternodeType::Regular => 1,
MasternodeType::Evo => 4,
}
} else {
0
};
(*pro_tx_hash_identifier, strength)
})
.collect();

FinalizedResourceVoteChoicesWithVoterInfo {
resource_vote_choice: *resource_vote_choice,
voters,
}
})
.collect();
let stored_info_from_disk = self
.drive
Expand Down
Loading

0 comments on commit b33eb5a

Please sign in to comment.