Skip to content

Commit

Permalink
Implement snapshot integration test (#275)
Browse files Browse the repository at this point in the history
* update catalyst-toolbox
  • Loading branch information
dkijania authored Sep 21, 2022
1 parent 525ec37 commit cb30d62
Show file tree
Hide file tree
Showing 42 changed files with 601 additions and 302 deletions.
150 changes: 91 additions & 59 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion iapyx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ jormungandr-automation = { git = "https://github.com/input-output-hk/jormungandr
thor = { git = "https://github.com/input-output-hk/jormungandr.git", branch = "master" }
hersir = { git = "https://github.com/input-output-hk/jormungandr.git", branch = "master" }
jortestkit = { git = "https://github.com/input-output-hk/jortestkit.git", branch = "master" }
catalyst-toolbox = { git = "https://github.com/input-output-hk/catalyst-toolbox.git", branch = "test_api_for_snapshot", features=["test-api"]}
catalyst-toolbox = { git = "https://github.com/input-output-hk/catalyst-toolbox.git", branch = "main", features=["test-api"]}
snapshot-lib = { git = "https://github.com/input-output-hk/catalyst-toolbox.git", branch = "main", features=["proptest", "test-api"]}
valgrind = { path = "../valgrind"}
hyper = "0.13.6"
thiserror = "1.0"
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ registration-service = { path = "../registration-service" }
rand_chacha = "0.2"
tokio = { version = "1.1", features = ["macros","rt","rt-multi-thread"] }
chain-impl-mockchain = { git = "https://github.com/input-output-hk/chain-libs.git", branch = "master", features = [ "property-test-api" ] }
catalyst-toolbox = { git = "https://github.com/input-output-hk/catalyst-toolbox.git", branch = "test_api_for_snapshot", features=["test-api"]}
voting-hir = { git = "https://github.com/input-output-hk/catalyst-toolbox.git", branch = "test_api_for_snapshot"}
catalyst-toolbox = { git = "https://github.com/input-output-hk/catalyst-toolbox.git", branch = "main", features=["test-api"]}
snapshot-lib = { git = "https://github.com/input-output-hk/catalyst-toolbox.git", branch = "main", features=["proptest", "test-api"]}
thiserror = "1.0"
netstat2 = "0.9"
image = "0.23.9"
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/src/common/mainnet_wallet_ext.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::common::MainnetWallet;
use snapshot_lib::VoterHIR;
use vitup::config::Block0Initial;
use voting_hir::VoterHIR;

pub trait MainnetWalletExtension {
fn as_initial_entry(&self) -> Block0Initial;
Expand Down
5 changes: 5 additions & 0 deletions integration-tests/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ mod assert;
pub mod load;
pub mod mainnet_wallet_ext;
pub mod registration;
mod reps;
mod rewards;
pub mod snapshot;
pub(crate) mod snapshot_filter;
mod static_data;
mod vote_plan_status;
mod wallet;

pub use reps::{empty_assigner, RepsVoterAssignerSource, DIRECT_VOTING_GROUP, REP_VOTING_GROUP};

pub use assert::*;
pub use mainnet_tools::wallet::MainnetWallet;
pub use rewards::{funded_proposals, VotesRegistry};
pub use snapshot_filter::SnapshotFilter;
pub use static_data::SnapshotExtensions;
use thiserror::Error;
pub use vote_plan_status::{CastedVote, VotePlanStatusProvider};
Expand Down
25 changes: 25 additions & 0 deletions integration-tests/src/common/reps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use jormungandr_lib::crypto::account::Identifier;
use snapshot_lib::voting_group::RepsVotersAssigner;
use std::collections::HashSet;

pub const DIRECT_VOTING_GROUP: &str = "direct";
pub const REP_VOTING_GROUP: &str = "rep";

pub trait RepsVoterAssignerSource {
fn into_reps_voter_assigner(self) -> RepsVotersAssigner;
}

impl RepsVoterAssignerSource for HashSet<Identifier> {
fn into_reps_voter_assigner(self) -> RepsVotersAssigner {
RepsVotersAssigner::new_from_repsdb(
DIRECT_VOTING_GROUP.to_string(),
REP_VOTING_GROUP.to_string(),
self,
)
.unwrap()
}
}

pub fn empty_assigner() -> RepsVotersAssigner {
HashSet::new().into_reps_voter_assigner()
}
29 changes: 7 additions & 22 deletions integration-tests/src/common/snapshot/controller.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use catalyst_toolbox::snapshot::voting_group::VotingGroupAssigner;
use catalyst_toolbox::snapshot::RawSnapshot;
use catalyst_toolbox::snapshot::Snapshot;
use catalyst_toolbox::snapshot::VotingRegistration;
use fraction::Fraction;
use jortestkit::prelude::Wait;
use snapshot_lib::registration::VotingRegistration;
use snapshot_trigger_service::client::rest::SnapshotRestClient;
use snapshot_trigger_service::client::SnapshotResult;
use snapshot_trigger_service::config::Configuration;
use snapshot_trigger_service::config::JobParameters;
use std::process::Child;
Expand Down Expand Up @@ -36,33 +33,21 @@ impl SnapshotServiceController {
&self.configuration
}

pub fn snapshot(
&self,
job_params: JobParameters,
threshold: u64,
fraction: Fraction,
voting_group_assigner: &impl VotingGroupAssigner,
) -> Snapshot {
pub fn snapshot(&self, job_params: JobParameters) -> SnapshotResult {
let id = self.client().job_new(job_params.clone()).unwrap();

self.client()
let status = self
.client()
.wait_for_job_finish(&id, Wait::new(std::time::Duration::from_secs(10), 5))
.unwrap();

let snapshot_content = self
.client()
.get_snapshot(id, job_params.tag.as_ref().unwrap().to_string())
.unwrap();
let raw_snapshot: Vec<VotingRegistration> =
serde_json::from_str(&snapshot_content).unwrap();
let snapshot: Vec<VotingRegistration> = serde_json::from_str(&snapshot_content).unwrap();

Snapshot::from_raw_snapshot(
RawSnapshot::from(raw_snapshot),
threshold.into(),
fraction,
voting_group_assigner,
)
.unwrap()
SnapshotResult::new(status, snapshot)
}
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/src/common/snapshot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod voter_hirs_asserts;

pub use controller::SnapshotServiceController;
pub use starter::SnapshotServiceStarter;
pub use voter_hirs_asserts::VoterHIRAsserts;
pub use voter_hirs_asserts::RegistrationAsserts;

use snapshot_trigger_service::client::do_snapshot as do_snapshot_internal;
use snapshot_trigger_service::client::{Error as SnapshotClientError, SnapshotResult};
Expand Down
30 changes: 27 additions & 3 deletions integration-tests/src/common/snapshot/voter_hirs_asserts.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
use jormungandr_lib::crypto::account::Identifier;
use jormungandr_lib::interfaces::Value;
use voting_hir::VoterHIR;
use snapshot_lib::registration::{Delegations, VotingRegistration};
use snapshot_lib::VoterHIR;

pub trait VoterHIRAsserts {
pub trait RegistrationAsserts {
fn assert_contains_voting_key_and_value(&self, identifier: &Identifier, value: Value);
fn assert_not_contain_voting_key(&self, identifier: &Identifier);
}

impl VoterHIRAsserts for Vec<VoterHIR> {
impl RegistrationAsserts for Vec<VotingRegistration> {
fn assert_contains_voting_key_and_value(&self, identifier: &Identifier, value: Value) {
assert!(self.iter().any(|x| {
value == x.voting_power
&& match &x.delegations {
Delegations::New(hash_set) => {
hash_set.iter().any(|(id, _weight)| id == identifier)
}
Delegations::Legacy(id) => id == identifier,
}
}));
}

fn assert_not_contain_voting_key(&self, identifier: &Identifier) {
assert!(!self.iter().any(|x| {
match &x.delegations {
Delegations::New(hash_set) => hash_set.iter().any(|(id, _weight)| id == identifier),
Delegations::Legacy(id) => id == identifier,
}
}));
}
}

impl RegistrationAsserts for Vec<VoterHIR> {
fn assert_contains_voting_key_and_value(&self, identifier: &Identifier, value: Value) {
assert!(self
.iter()
Expand Down
112 changes: 112 additions & 0 deletions integration-tests/src/common/snapshot_filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use crate::common::RepsVoterAssignerSource;
use chain_addr::Discrimination;
use fraction::Fraction;
use jormungandr_lib::crypto::account::Identifier;
use jormungandr_lib::interfaces::InitialUTxO;
use jormungandr_lib::interfaces::Value;
use snapshot_lib::registration::VotingRegistration;
use snapshot_lib::voting_group::VotingGroupAssigner;
use snapshot_lib::{RawSnapshot, Snapshot, VoterHIR};
use snapshot_trigger_service::client::SnapshotResult;
use std::collections::HashSet;

pub trait SnapshotFilterSource {
fn filter(
&self,
voting_threshold: Value,
cap: Fraction,
voting_group_assigner: &impl VotingGroupAssigner,
) -> SnapshotFilter;
fn filter_default(&self, reps: &HashSet<Identifier>) -> SnapshotFilter;
}

impl SnapshotFilterSource for SnapshotResult {
fn filter(
&self,
voting_threshold: Value,
cap: Fraction,
voting_group_assigner: &impl VotingGroupAssigner,
) -> SnapshotFilter {
SnapshotFilter::from_snapshot_result(self, voting_threshold, cap, voting_group_assigner)
}

fn filter_default(&self, reps: &HashSet<Identifier>) -> SnapshotFilter {
SnapshotFilter::from_snapshot_result_default(self, reps)
}
}

pub struct SnapshotFilter {
snapshot: Snapshot,
}

impl SnapshotFilter {
pub(crate) fn from_snapshot_result_default(
result: &SnapshotResult,
reps: &HashSet<Identifier>,
) -> Self {
Self::from_snapshot_result(
result,
450u64.into(),
Fraction::new(1u64, 3u64),
&reps.clone().into_reps_voter_assigner(),
)
}
}

impl SnapshotFilter {
pub fn from_snapshot_result(
snapshot_result: &SnapshotResult,
voting_threshold: Value,
cap: Fraction,
voting_group_assigner: &impl VotingGroupAssigner,
) -> SnapshotFilter {
Self::from_voting_registrations(
snapshot_result.registrations().to_vec(),
voting_threshold,
cap,
voting_group_assigner,
)
}

pub fn from_voting_registrations(
voting_registrations: Vec<VotingRegistration>,
voting_threshold: Value,
cap: Fraction,
voting_group_assigner: &impl VotingGroupAssigner,
) -> SnapshotFilter {
Self {
snapshot: Snapshot::from_raw_snapshot(
RawSnapshot::from(voting_registrations),
voting_threshold,
cap,
voting_group_assigner,
)
.unwrap(),
}
}

pub fn to_voters_hirs(&self) -> Vec<VoterHIR> {
self.snapshot
.voting_keys()
.map(|vk| VoterHIR {
voting_key: vk.clone(),
voting_power: self
.snapshot
.contributions_for_voting_key(vk)
.iter()
.map(|c| c.value)
.sum::<u64>()
.into(),
voting_group: "direct".to_string(),
})
.collect()
}

pub fn to_block0_initials(&self) -> Vec<InitialUTxO> {
self.snapshot.to_block0_initials(Discrimination::Production)
}

pub fn snapshot(&self) -> Snapshot {
self.snapshot.clone()
}
}
61 changes: 61 additions & 0 deletions integration-tests/src/component/backend/cip_36_support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use crate::common::snapshot::do_snapshot;
use crate::common::snapshot_filter::SnapshotFilterSource;
use crate::common::RepsVoterAssignerSource;
use assert_fs::TempDir;
use chain_addr::Discrimination;
use fraction::Fraction;
use snapshot_trigger_service::config::JobParameters;
use std::collections::HashSet;
use vit_servicing_station_tests::common::data::ArbitraryValidVotingTemplateGenerator;
use vitup::config::Block0Initials;
use vitup::config::ConfigBuilder;
use vitup::config::SnapshotInitials;
use vitup::testing::spawn_network;
use vitup::testing::vitup_setup;

#[test]
pub fn cip_36_support() {
let testing_directory = TempDir::new().unwrap().into_persistent();
let voting_threshold = 1;
let tag = None;

let job_param = JobParameters {
slot_no: None,
tag: tag.clone(),
};

let snapshot_result = do_snapshot(job_param).unwrap();
let reps = HashSet::new();

let snapshot_filter = snapshot_result.filter(
voting_threshold.into(),
Fraction::new(1u64, 3u64),
&reps.into_reps_voter_assigner(),
);

let config = ConfigBuilder::default()
.voting_power(voting_threshold)
.block0_initials(Block0Initials::new_from_external(
snapshot_filter.to_voters_hirs(),
Discrimination::Production,
))
.snapshot_initials(SnapshotInitials::from_voters_hir(
snapshot_filter.to_voters_hirs(),
tag.unwrap_or_else(|| "".to_string()),
))
.build();

let mut template_generator = ArbitraryValidVotingTemplateGenerator::new();

let (mut controller, vit_parameters, network_params) =
vitup_setup(&config, testing_directory.path().to_path_buf()).unwrap();
let (_nodes, _vit_station, _wallet_proxy) = spawn_network(
&mut controller,
vit_parameters,
network_params,
&mut template_generator,
)
.unwrap();

std::thread::sleep(std::time::Duration::from_secs(3600))
}
1 change: 1 addition & 0 deletions integration-tests/src/component/backend/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod cip_36_support;
mod features;
mod rewards;
mod sanity;
Loading

0 comments on commit cb30d62

Please sign in to comment.