Skip to content
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

test(sdk): increase test coverage of masternode voting #1906

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
9cd2b69
test(sdk): update test contested_resource_vote_states_not_found
lklimek Jun 17, 2024
01b87be
chore: minor improvements
lklimek Jun 18, 2024
9af521e
test(sdk): mn voting tests WIP
lklimek Jun 18, 2024
8c8a16d
Merge remote-tracking branch 'origin/feat/contestedDocumentResolution…
lklimek Jun 18, 2024
8e56659
chore: build fixes after merge
lklimek Jun 18, 2024
4a7e2dd
test(sdk): contested_resources_paginate green
lklimek Jun 19, 2024
17a41f7
refactor(sdk): refactor contested_resource tests
lklimek Jun 19, 2024
3c159d9
test(sdk): vote polls by ts id
lklimek Jun 20, 2024
6bd15b4
refactor(sdk): fix typo in test file name
lklimek Jun 20, 2024
89a9075
feat(sdk): impl LimitQuery for some mn voting queries
lklimek Jun 20, 2024
0e8070e
test(sdk): contested_resource additional tests
lklimek Jun 20, 2024
05dea66
test(sdk): contested_resource_vote_state
lklimek Jun 20, 2024
79bf4a8
test(sdk): separate sub-scenarios folders
lklimek Jun 20, 2024
e4e896d
Merge remote-tracking branch 'origin/feat/contestedDocumentResolution…
lklimek Jun 24, 2024
6ac5e08
fix: fixes for contested document queries 2
QuantumExplorer Jun 24, 2024
d5beaf0
test(sdk): minor renames
lklimek Jun 24, 2024
c5eaa47
Merge remote-tracking branch 'origin/fix/contestedDocumentResolutionF…
lklimek Jun 24, 2024
4784dc3
fix: masternode vote fixes
QuantumExplorer Jun 24, 2024
fb65aa2
cleanup
QuantumExplorer Jun 24, 2024
6deef69
cleanup
QuantumExplorer Jun 24, 2024
f8e730d
cleanup
QuantumExplorer Jun 24, 2024
cf731c6
small fix
QuantumExplorer Jun 25, 2024
eb17694
Merge remote-tracking branch 'origin/fix/contestedDocumentResolutionF…
lklimek Jun 25, 2024
ad23844
Merge remote-tracking branch 'origin/feat/contestedDocumentResolution…
lklimek Jun 25, 2024
0609444
test(sdk): mn voting tests updated and improved
lklimek Jun 25, 2024
cadcfdd
added error if too many end index values
QuantumExplorer Jun 25, 2024
5834c1b
feat(sdk): add OrderedBtreeMap that can be iterated in asc or desc order
lklimek Jun 25, 2024
0dc0511
Merge remote-tracking branch 'origin/fix/contestedDocumentResolutionF…
lklimek Jun 25, 2024
a52e4ec
build: update Cargo.lock
lklimek Jun 25, 2024
43c259c
test(sdk): fix vote_polls_by_ts_limit
lklimek Jun 25, 2024
f5e706f
voting strategy tests
QuantumExplorer Jun 25, 2024
00a11f1
a lot of work on strategy tests for voting
QuantumExplorer Jun 25, 2024
6da525c
Merge remote-tracking branch 'origin/fix/contestedDocumentResolutionF…
lklimek Jun 26, 2024
a0d0850
fix: PutVote was using wrong identity for nonce
pauldelucia Jun 26, 2024
c4ff2a8
remove bs58 dependency
pauldelucia Jun 27, 2024
02fb3a0
extract copied code to function
pauldelucia Jun 27, 2024
93b2a86
fix: putVote was using wrong identity for nonce (#1907)
pauldelucia Jun 27, 2024
9116c8c
many more fixes
QuantumExplorer Jun 27, 2024
5cf3edd
Merge remote-tracking branch 'origin/fix/contestedDocumentResolutionF…
lklimek Jun 27, 2024
f546e53
build: revert rs-tenderdash-abci to v0.14.0-dev.12
lklimek Jun 27, 2024
522e3a1
fix: empty proofs are returned for masternode vote state transition
shumkov Jun 27, 2024
8e51d66
Merge remote-tracking branch 'origin/fix/contestedDocumentResolutionF…
lklimek Jun 28, 2024
bdb85ca
build: update cargo.lock
lklimek Jun 28, 2024
767e0de
Merge branch 'feat/contestedDocumentResolutionWithMasternodeVoting' i…
QuantumExplorer Jun 28, 2024
84dfbef
fix: stateTransition.getProTxHash is not a function
shumkov Jun 28, 2024
1b8eba3
Merge remote-tracking branch 'origin/fix/contestedDocumentResolutionF…
shumkov Jun 28, 2024
ccf2c0b
fix: missing the document contested status
shumkov Jun 28, 2024
170b3a0
refactor: use generic for verify
QuantumExplorer Jun 28, 2024
ba216c7
test(sdk): further sdk tests
lklimek Jun 28, 2024
5715aa0
Merge remote-tracking branch 'origin/fix/contestedDocumentResolutionF…
lklimek Jun 28, 2024
625b83f
Merge remote-tracking branch 'origin/fix/contestedDocumentResolutionF…
lklimek Jun 28, 2024
7f3f3a5
chore(drive-proof-verifier): remove unused OrderedBTreeMap
lklimek Jun 28, 2024
bdfe46f
refactor(sdk): VotePollsGroupedByTimestamp uses Vec, not BTreeMap
lklimek Jun 28, 2024
4cf0f83
fix(sdk): broadcasting state transition missing impl of contract prov…
lklimek Jun 28, 2024
3672380
Merge branch 'feat/contestedDocumentResolutionWithMasternodeVoting' i…
lklimek Jun 30, 2024
7beb2af
Merge remote-tracking branch 'origin/feat/contestedDocumentResolution…
lklimek Jul 2, 2024
13ac443
chore: cargo update
lklimek Jul 2, 2024
7beda50
chore: self review
lklimek Jul 2, 2024
7e7a968
test(sdk): fix vote_polls_by_ts_limit test non-determinism
lklimek Jul 2, 2024
3bc081c
chore(sdk): support passing --include-ignored and similar flags
lklimek Jul 2, 2024
257db65
chore: minor fix in rs-sdk fetch config
lklimek Jul 2, 2024
37a627a
test(sdk): refactor contested_rss_vote_state_fields to use test_case …
lklimek Jul 2, 2024
34391e4
test(sdk): refactor contested resource test to use #test_case
lklimek Jul 3, 2024
19d203a
chore: self review
lklimek Jul 3, 2024
4b0d380
chore: ignore some tests
lklimek Jul 3, 2024
2b4a977
chore: remove invalid test case
lklimek Jul 3, 2024
f8720f4
added validation that we are not already a contestant
QuantumExplorer Jul 3, 2024
89c4a83
Merge remote-tracking branch 'origin/fix/contestedDocumentResolutionF…
lklimek Jul 3, 2024
412fe96
fixes
QuantumExplorer Jul 3, 2024
98513a9
small fix
QuantumExplorer Jul 3, 2024
5629357
fixes
QuantumExplorer Jul 3, 2024
52f43cc
small fix
QuantumExplorer Jul 3, 2024
6873ec3
Merge remote-tracking branch 'origin/fix/contestedDocumentResolutionF…
lklimek Jul 3, 2024
b0059a3
Merge remote-tracking branch 'origin/feat/contestedDocumentResolution…
lklimek Jul 4, 2024
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
51 changes: 43 additions & 8 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion packages/dapi-grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ 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 = "1.0.0-dev.1", default-features = false }
tenderdash-proto = { git = "https://github.com/dashpay/rs-tenderdash-abci", version = "0.14.0-dev.12", tag = "v0.14.0-dev.12", default-features = false, features = [
"grpc",
] }
dapi-grpc-macros = { path = "../rs-dapi-grpc-macros" }
platform-version = { path = "../rs-platform-version" }

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 = "1.0.0-dev.1", features = [
tenderdash-abci = { git = "https://github.com/dashpay/rs-tenderdash-abci", version = "0.14.0-dev.12", tag = "v0.14.0-dev.12", features = [
"grpc",
] }
lazy_static = "1.4.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/rs-drive-proof-verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dpp = { path = "../rs-dpp", features = [
bincode = { version = "2.0.0-rc.3", features = ["serde"], optional = true }
platform-serialization-derive = { path = "../rs-platform-serialization-derive", optional = true }
platform-serialization = { path = "../rs-platform-serialization", optional = true }
tenderdash-abci = { git = "https://github.com/dashpay/rs-tenderdash-abci", version = "1.0.0-dev.1", features = [
tenderdash-abci = { git = "https://github.com/dashpay/rs-tenderdash-abci", version = "0.14.0-dev.12", tag = "v0.14.0-dev.12", features = [
"crypto",
], default-features = false }
tracing = { version = "0.1.37" }
Expand Down
6 changes: 3 additions & 3 deletions packages/rs-drive-proof-verifier/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,7 @@ impl FromProof<platform::GetVotePollsByEndDateRequest> for VotePollsGroupedByTim
let mtd = response.metadata().or(Err(Error::EmptyResponseMetadata))?;

let (root_hash, vote_polls) = drive_query
.verify_vote_polls_by_end_date_proof::<BTreeMap<_, _>>(
.verify_vote_polls_by_end_date_proof::<Vec<(_, _)>>(
&proof.grovedb_proof,
platform_version,
)
Expand All @@ -1470,7 +1470,7 @@ impl FromProof<platform::GetVotePollsByEndDateRequest> for VotePollsGroupedByTim

verify_tenderdash_proof(proof, mtd, &root_hash, provider)?;

let response = VotePollsGroupedByTimestamp(vote_polls);
let response = VotePollsGroupedByTimestamp(vote_polls).sorted(drive_query.order_ascending);

Ok((response.into_option(), mtd.clone()))
}
Expand Down Expand Up @@ -1655,7 +1655,7 @@ define_length!(Contenders, |x: &Contenders| x.contenders.len());
define_length!(Voters, |x: &Voters| x.0.len());
define_length!(
VotePollsGroupedByTimestamp,
|x: &VotePollsGroupedByTimestamp| x.0.values().map(|v| v.len()).sum()
|x: &VotePollsGroupedByTimestamp| x.0.iter().map(|v| v.1.len()).sum()
);
trait IntoOption
where
Expand Down
37 changes: 27 additions & 10 deletions packages/rs-drive-proof-verifier/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use dpp::{
};
use drive::grovedb::Element;
use std::collections::{BTreeMap, BTreeSet};

#[cfg(feature = "mocks")]
use {
bincode::{Decode, Encode},
Expand Down Expand Up @@ -293,19 +294,35 @@ impl From<&PrefundedSpecializedBalance> for Credits {
derive(Encode, Decode, PlatformSerialize, PlatformDeserialize),
platform_serialize(unversioned)
)]
pub struct VotePollsGroupedByTimestamp(pub BTreeMap<TimestampMillis, Vec<VotePoll>>);
pub struct VotePollsGroupedByTimestamp(pub Vec<(TimestampMillis, Vec<VotePoll>)>);
impl VotePollsGroupedByTimestamp {
/// Sort the vote polls by timestamp.
pub fn sorted(mut self, ascending: bool) -> Self {
self.0.sort_by(|a, b| {
if ascending {
a.0.cmp(&b.0)
} else {
b.0.cmp(&a.0)
}
});

self
}
}

/// Insert items into the map, appending them to the existing values for the same key.
impl FromIterator<(u64, Vec<VotePoll>)> for VotePollsGroupedByTimestamp {
fn from_iter<T: IntoIterator<Item = (u64, Vec<VotePoll>)>>(iter: T) -> Self {
let mut map = BTreeMap::new();

for (timestamp, vote_poll) in iter {
let entry = map.entry(timestamp).or_insert_with(Vec::new);
entry.extend(vote_poll);
}

Self(map)
// collect all vote polls for the same timestamp into a single vector
let data = iter
.into_iter()
.fold(BTreeMap::new(), |mut acc, (timestamp, vote_poll)| {
let entry: &mut Vec<VotePoll> = acc.entry(timestamp).or_default();
entry.extend(vote_poll);
acc
});

Self(data.into_iter().collect())
}
}

Expand All @@ -329,7 +346,7 @@ impl FromIterator<(u64, Option<VotePoll>)> for VotePollsGroupedByTimestamp {

impl IntoIterator for VotePollsGroupedByTimestamp {
type Item = (u64, Vec<VotePoll>);
type IntoIter = std::collections::btree_map::IntoIter<u64, Vec<VotePoll>>;
type IntoIter = std::vec::IntoIter<(u64, Vec<VotePoll>)>;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
Expand Down
2 changes: 2 additions & 0 deletions packages/rs-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dashcore-rpc = { git = "https://github.com/dashpay/rust-dashcore-rpc", tag = "v0
lru = { version = "0.12.3", optional = true }
bip37-bloom-filter = { git = "https://github.com/dashpay/rs-bip37-bloom-filter", branch = "develop" }
pollster = { version = "0.3.0" }
sha2 = "0.10.8"

[dev-dependencies]
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread"] }
Expand All @@ -52,6 +53,7 @@ tokio-test = { version = "0.4.4" }
clap = { version = "4.5.4", features = ["derive"] }
sanitize-filename = { version = "0.5.0" }
chrono = { version = "0.4.38" }
test-case = { version = "3.3.1" }

[features]
default = ["mocks", "offline-testing"]
Expand Down
2 changes: 1 addition & 1 deletion packages/rs-sdk/scripts/generate_test_vectors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ pushd "$CARGO_DIR"
cargo test -p dash-sdk \
--no-default-features \
--features generate-test-vectors \
"$1"
"$@"

popd
24 changes: 24 additions & 0 deletions packages/rs-sdk/src/platform/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,3 +520,27 @@ impl Query<GetContestedResourceIdentityVotesRequest> for VoteQuery {
.into())
}
}

impl Query<GetContestedResourceIdentityVotesRequest> for LimitQuery<VoteQuery> {
fn query(self, prove: bool) -> Result<GetContestedResourceIdentityVotesRequest, Error> {
if !prove {
unimplemented!("queries without proofs are not supported yet");
}
use proto::get_contested_resource_identity_votes_request::{
get_contested_resource_identity_votes_request_v0::StartAtVotePollIdInfo, Version,
};

Ok(match self.query.query(prove)?.version {
None => return Err(Error::Protocol(dpp::ProtocolError::NoProtocolVersionError)),
Some(Version::V0(v0)) => GetContestedResourceIdentityVotesRequestV0 {
limit: self.limit,
start_at_vote_poll_id_info: self.start_info.map(|v| StartAtVotePollIdInfo {
start_at_poll_identifier: v.start_key.to_vec(),
start_poll_identifier_included: v.start_included,
}),
..v0
},
}
.into())
}
}
22 changes: 20 additions & 2 deletions packages/rs-sdk/src/platform/transition/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::platform::transition::put_settings::PutSettings;
use crate::platform::Fetch;
use crate::{Error, Sdk};
use dapi_grpc::platform::VersionedGrpcResponse;
use dpp::identifier::MasternodeIdentifiers;
use dpp::identity::hash::IdentityPublicKeyHashMethodsV0;
use dpp::identity::signer::Signer;
use dpp::identity::IdentityPublicKey;
use dpp::prelude::Identifier;
Expand Down Expand Up @@ -50,8 +52,10 @@ impl<S: Signer> PutVote<S> for Vote {
signer: &S,
settings: Option<PutSettings>,
) -> Result<(), Error> {
let voting_identity_id = get_voting_identity_id(voter_pro_tx_hash, voting_public_key)?;

let new_masternode_voting_nonce = sdk
.get_identity_nonce(voter_pro_tx_hash, true, settings)
.get_identity_nonce(voting_identity_id, true, settings)
.await?;

let settings = settings.unwrap_or_default();
Expand Down Expand Up @@ -80,8 +84,10 @@ impl<S: Signer> PutVote<S> for Vote {
signer: &S,
settings: Option<PutSettings>,
) -> Result<Vote, Error> {
let voting_identity_id = get_voting_identity_id(voter_pro_tx_hash, voting_public_key)?;

let new_masternode_voting_nonce = sdk
.get_identity_nonce(voter_pro_tx_hash, true, settings)
.get_identity_nonce(voting_identity_id, true, settings)
.await?;

let settings = settings.unwrap_or_default();
Expand Down Expand Up @@ -147,3 +153,15 @@ impl<S: Signer> PutVote<S> for Vote {
}
}
}

fn get_voting_identity_id(
voter_pro_tx_hash: Identifier,
voting_public_key: &IdentityPublicKey,
) -> Result<Identifier, Error> {
let pub_key_hash = voting_public_key.public_key_hash()?;

Ok(Identifier::create_voter_identifier(
voter_pro_tx_hash.as_bytes(),
&pub_key_hash,
))
}
3 changes: 3 additions & 0 deletions packages/rs-sdk/tests/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ DASH_SDK_PLATFORM_HOST="127.0.0.1"
DASH_SDK_PLATFORM_PORT=2443
DASH_SDK_PLATFORM_SSL=false

# ProTxHash of masternode that has at least 1 vote casted for DPNS name `testname`
DASH_SDK_MASTERNODE_OWNER_PRO_REG_TX_HASH="6ac88f64622d9bc0cb79ad0f69657aa9488b213157d20ae0ca371fa5f04fb222"

DASH_SDK_CORE_PORT=20002
DASH_SDK_CORE_USER="someuser"
DASH_SDK_CORE_PASSWORD="verysecretpassword"
29 changes: 29 additions & 0 deletions packages/rs-sdk/tests/fetch/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
use dash_sdk::{mock::Mockable, platform::Query, Sdk};
use dpp::{data_contract::DataContractFactory, prelude::Identifier};
use hex::ToHex;
use rs_dapi_client::transport::TransportRequest;

use super::config::Config;

/// Test DPNS name for testing of the Sdk; at least 3 identities should request this name to be reserved
pub(crate) const TEST_DPNS_NAME: &str = "testname";

/// Create a mock document type for testing of mock API
pub fn mock_document_type() -> dpp::data_contract::document_type::DocumentType {
Expand Down Expand Up @@ -80,3 +88,24 @@ pub fn setup_logs() {
.try_init()
.ok();
}

/// Configure test case generated with [::test_case] crate.
///
/// This function is intended to use with multiple test cases in a single function.
/// As a test case shares function body, we need to generate unique name for each of them to isolate generated
/// test vectors. It is done by hashing query and using it as a suffix for test case name.
///
/// ## Returns
///
/// Returns unique name of test case (generated from `name_prefix` and hash of query) and configured SDK.
pub(crate) async fn setup_sdk_for_test_case<T: TransportRequest + Mockable, Q: Query<T>>(
cfg: Config,
query: Q,
name_prefix: &str,
) -> (String, Sdk) {
let key = rs_dapi_client::mock::Key::new(&query.query(true).expect("valid query"));
let test_case_id = format!("{}_{}", name_prefix, key.encode_hex::<String>());

// create new sdk to ensure that test cases don't interfere with each other
(test_case_id.clone(), cfg.setup_api(&test_case_id).await)
}
Loading
Loading