Skip to content
Open
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/rs-drive-abci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ async-trait = "0.1.77"
console-subscriber = { version = "0.4", optional = true }
dashcore-crypto = { workspace = true, features = ["bls"], optional = true }
grovedb-commitment-tree = { workspace = true }
# The error a sum taken past its range returns, which ending a contest tells from others.
grovedb-merk = { workspace = true }
nonempty = "0.11"
# Shielded-pool snapshot needs raw RocksDB SstFileWriter + ingest_external_file_cf
# bindings, and blake3 for the snapshot-file checksum.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use drive::grovedb::TransactionArg;

mod v0;
mod v1;
mod v2;

impl<C> Platform<C>
where
Expand Down Expand Up @@ -43,9 +44,16 @@ where
transaction,
platform_version,
),
2 => self.check_for_ended_vote_polls_v2(
last_committed_platform_state,
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, 1],
known_versions: vec![0, 1, 2],
received: version,
})),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ use crate::platform_types::platform::Platform;
use crate::platform_types::platform_state::{PlatformState, PlatformStateV0Methods};
use crate::rpc::core::CoreRPCLike;
use dpp::block::block_info::BlockInfo;
use dpp::data_contract::document_type::ContestedIndexResolution;
use dpp::data_contract::document_type::{ContestedIndexResolution, DocumentTypeRef};
use dpp::document::DocumentV0Getters;
use dpp::identifier::Identifier;
use dpp::prelude::TimestampMillis;
use dpp::version::PlatformVersion;
use dpp::voting::contender_structs::FinalizedContender;
use dpp::voting::contender_structs::{
FinalizedContender, FinalizedContenderWithSerializedDocument,
};
use dpp::voting::vote_choices::resource_vote_choice::ResourceVoteChoice::TowardsIdentity;
use dpp::voting::vote_info_storage::contested_document_vote_poll_winner_info::ContestedDocumentVotePollWinnerInfo;
use dpp::voting::vote_polls::yes_no_vote_poll::{
VotingPower, YesNoMinimumVotingPower, YesNoVotePoll,
};
use dpp::voting::vote_polls::VotePoll;
use drive::drive::votes::paths::vote_end_date_queries_tree_path_vec;
use drive::drive::votes::resolved::vote_polls::contested_document_resource_vote_poll::ContestedDocumentResourceVotePollWithContractInfo;
use drive::drive::votes::resolved::vote_polls::resolve::VotePollResolver;
use drive::drive::votes::resolved::vote_polls::{ResolvedVotePoll, ResolvedVotePollWithVotes};
use drive::error::drive::DriveError;
Expand All @@ -39,10 +42,97 @@ where
/// them and cleans their votes up.
#[inline(always)]
pub(super) fn check_for_ended_vote_polls_v1(
&self,
last_committed_platform_state: &PlatformState,
block_platform_state: &PlatformState,
block_info: &BlockInfo,
transaction: TransactionArg,
platform_version: &PlatformVersion,
) -> Result<(), Error> {
self.end_due_vote_polls_v1(
last_committed_platform_state,
block_platform_state,
block_info,
Self::award_contest_winner_v1,
transaction,
platform_version,
)
}

/// Picks the winner of an ended contest and awards it the document: the earliest of the
/// contenders with the highest tally, every one of them decoded first, unless the lock tally
/// is higher. Any error is returned.
///
/// Moved here unchanged from the body of generation 1, which protocol version 14 selects,
/// so that generation 2 shares the rest of the body: a pure refactor, reading, writing and
/// failing as before, in the same order.
pub(super) fn award_contest_winner_v1(
&self,
block_info: &BlockInfo,
contest: EndedContest,
transaction: TransactionArg,
platform_version: &PlatformVersion,
) -> Result<ContestedDocumentVotePollWinnerInfo, Error> {
let EndedContest {
sorted_contenders,
locked_vote_tally,
locking_allowed,
document_type,
vote_poll,
} = contest;
let highest_vote_tally = sorted_contenders
.first()
.map(|max_voted_contender| max_voted_contender.final_vote_tally)
.unwrap_or_default();
// These are all the people who got top votes, every one of them
// considered (up to `maximum_contenders_to_consider`); version 0
// compared at most 100
let top_contenders: Vec<FinalizedContender> = sorted_contenders
.into_iter()
.filter(|c| c.final_vote_tally == highest_vote_tally)
.map(|contender| {
FinalizedContender::try_from_contender_with_serialized_document(
contender,
document_type,
platform_version,
)
.map_err(Error::Protocol)
})
.collect::<Result<Vec<_>, Error>>()?;
let maybe_top_contender = earliest_contender(top_contenders);
// We award the document to the top contender
let winner_info = if let Some(top_contender) = maybe_top_contender {
// let's check to make sure the lock votes didn't win it
// if the lock is tied with the top contender the top contender gets it
if locking_allowed && locked_vote_tally > top_contender.final_vote_tally {
// the record will show it's locked
ContestedDocumentVotePollWinnerInfo::Locked
} else {
let contender_id = top_contender.identity_id;
// We award the document to the winner of the vote poll
self.award_document_to_winner(
block_info,
top_contender,
vote_poll,
transaction,
platform_version,
)?;
ContestedDocumentVotePollWinnerInfo::WonByIdentity(contender_id)
}
} else {
ContestedDocumentVotePollWinnerInfo::NoWinner
};
Ok(winner_info)
}

/// The body of generation 1, with the step picking and awarding a contest's winner given:
/// [`Self::award_contest_winner_v1`] in generation 1, and its successor in generation 2.
pub(super) fn end_due_vote_polls_v1(
&self,
_last_committed_platform_state: &PlatformState,
block_platform_state: &PlatformState,
block_info: &BlockInfo,
award_contest_winner: AwardContestWinner<C>,
transaction: TransactionArg,
platform_version: &PlatformVersion,
) -> Result<(), Error> {
Expand Down Expand Up @@ -184,68 +274,20 @@ where

identifiers_voting_for_contenders.append(&mut other_contenders);

let highest_vote_tally = sorted_contenders
.first()
.map(|max_voted_contender| max_voted_contender.final_vote_tally)
.unwrap_or_default();
// These are all the people who got top votes, every one of them
// considered (up to `maximum_contenders_to_consider`); version 0
// compared at most 100
let top_contenders: Vec<FinalizedContender> = sorted_contenders
.into_iter()
.filter(|c| c.final_vote_tally == highest_vote_tally)
.map(|contender| {
FinalizedContender::try_from_contender_with_serialized_document(
contender,
document_type,
platform_version,
)
.map_err(Error::Protocol)
})
.collect::<Result<Vec<_>, Error>>()?;
// A tie among the top contenders goes to the earliest document:
// creation time, then block height, then core height, then id.
// Version 0 awarded the latest.
let maybe_top_contender = top_contenders.into_iter().min_by(|a, b| {
a.document
.created_at()
.cmp(&b.document.created_at())
.then_with(|| {
a.document
.created_at_block_height()
.cmp(&b.document.created_at_block_height())
})
.then_with(|| {
a.document
.created_at_core_block_height()
.cmp(&b.document.created_at_core_block_height())
})
.then_with(|| a.document.id().cmp(&b.document.id()))
});
// We award the document to the top contender
let winner_info = if let Some(top_contender) = maybe_top_contender {
// let's check to make sure the lock votes didn't win it
// if the lock is tied with the top contender the top contender gets it
if locking_allowed
&& result.locked_vote_tally > top_contender.final_vote_tally
{
// the record will show it's locked
ContestedDocumentVotePollWinnerInfo::Locked
} else {
let contender_id = top_contender.identity_id;
// We award the document to the winner of the vote poll
self.award_document_to_winner(
block_info,
top_contender,
&resolved_contested_document_resource_vote_poll,
transaction,
platform_version,
)?;
ContestedDocumentVotePollWinnerInfo::WonByIdentity(contender_id)
}
} else {
ContestedDocumentVotePollWinnerInfo::NoWinner
};
// We pick the winner and award the document to it
let winner_info = award_contest_winner(
self,
block_info,
EndedContest {
sorted_contenders,
locked_vote_tally: result.locked_vote_tally,
locking_allowed,
document_type,
vote_poll: &resolved_contested_document_resource_vote_poll,
},
transaction,
platform_version,
)?;
// We want to keep a record of how everyone voted
self.keep_record_of_finished_contested_resource_vote_poll(
block_platform_state,
Expand Down Expand Up @@ -363,3 +405,50 @@ where
Ok(Some(voters))
}
}

/// What the end of a contest's vote poll picks its winner from
pub(super) struct EndedContest<'a> {
/// The contenders, sorted by tally from the highest
pub(super) sorted_contenders: Vec<FinalizedContenderWithSerializedDocument>,
/// The lock tally
pub(super) locked_vote_tally: u32,
/// Whether the lock tally can beat the contenders: not on an index resolved without locking
pub(super) locking_allowed: bool,
/// The contested document type, which the contenders' documents decode with
pub(super) document_type: DocumentTypeRef<'a>,
/// The contest's vote poll
pub(super) vote_poll: &'a ContestedDocumentResourceVotePollWithContractInfo,
}

/// The step of ending a contest that picks its winner, awards it the document and returns the
/// outcome to record
pub(super) type AwardContestWinner<C> = fn(
&Platform<C>,
&BlockInfo,
EndedContest,
TransactionArg,
&PlatformVersion,
) -> Result<ContestedDocumentVotePollWinnerInfo, Error>;

/// The earliest of tied contenders: by creation time, then block height, then core height,
/// then id. Version 0 awarded the latest.
pub(super) fn earliest_contender(
contenders: Vec<FinalizedContender>,
) -> Option<FinalizedContender> {
contenders.into_iter().min_by(|a, b| {
a.document
.created_at()
.cmp(&b.document.created_at())
.then_with(|| {
a.document
.created_at_block_height()
.cmp(&b.document.created_at_block_height())
})
.then_with(|| {
a.document
.created_at_core_block_height()
.cmp(&b.document.created_at_core_block_height())
})
.then_with(|| a.document.id().cmp(&b.document.id()))
})
}
Loading
Loading