Skip to content

Commit

Permalink
Merge branch 'main' into ci-no-fail-fast
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed Nov 21, 2022
2 parents 17812e0 + 05353d5 commit 8883dd1
Show file tree
Hide file tree
Showing 21 changed files with 522 additions and 45 deletions.
Expand Up @@ -75,7 +75,12 @@ pub fn explorer_transactions_not_existing_address_test() {
.send(&transaction.encode())
.assert_in_block_with_wait(&wait);

let explorer_process = jormungandr.explorer(ExplorerParams::default()).unwrap();
let params = ExplorerParams::new(
ADDRESS_QUERY_COMPLEXITY_LIMIT,
ADDRESS_QUERY_DEPTH_LIMIT,
None,
);
let explorer_process = jormungandr.explorer(params).unwrap();
let explorer = explorer_process.client();

let explorer_address = explorer
Expand Down
Expand Up @@ -38,7 +38,7 @@ impl ArbitraryRawSnapshotGenerator {
pub fn delegation(&mut self) -> Delegations {
match self.id_generator.next_u32() % 2 {
0 => self.legacy_delegation(),
1 = > self.new_delegation(),
1 => self.new_delegation(),
}
}

Expand Down
Expand Up @@ -95,6 +95,7 @@ pub struct RawSnapshotBuilder {
direct_voters_group: Option<String>,
representatives_group: Option<String>,
voting_registrations_count: usize,
voting_registrations: Option<Vec<VotingRegistration>>,
}

impl Default for RawSnapshotBuilder {
Expand All @@ -107,6 +108,7 @@ impl Default for RawSnapshotBuilder {
direct_voters_group: Some(DEFAULT_DIRECT_VOTER_GROUP.into()),
representatives_group: Some(DEFAULT_REPRESENTATIVE_GROUP.into()),
voting_registrations_count: 2,
voting_registrations: None,
}
}
}
Expand All @@ -122,18 +124,36 @@ impl RawSnapshotBuilder {
self
}

pub fn with_voting_registrations(
mut self,
voting_registrations: Vec<VotingRegistration>,
) -> Self {
self.voting_registrations = Some(voting_registrations);
self
}

pub fn with_timestamp(mut self, timestamp: i64) -> Self {
self.update_timestamp = timestamp;
self
}

pub fn build(self) -> RawSnapshot {
pub fn with_voting_power_cap(mut self, voting_power_cap: Fraction) -> Self {
self.voting_power_cap = voting_power_cap;
self
}

pub fn with_min_stake_threshold(mut self, min_stake_threshold: Value) -> Self {
self.min_stake_threshold = min_stake_threshold;
self
}

pub fn build(mut self) -> RawSnapshot {
let mut rng = rand::rngs::OsRng;
let mut delegation_type_count = 0;

RawSnapshot {
content: RawSnapshotInput {
snapshot: std::iter::from_fn(|| {
if self.voting_registrations.is_none() {
self.voting_registrations = Some(
std::iter::from_fn(|| {
Some(VotingRegistration {
stake_public_key: TestGen::public_key().to_string(),
voting_power: rng.gen_range(1u64, 1_00u64).into(),
Expand All @@ -153,8 +173,13 @@ impl RawSnapshotBuilder {
})
})
.take(self.voting_registrations_count)
.collect::<Vec<_>>()
.into(),
.collect::<Vec<_>>(),
)
}

RawSnapshot {
content: RawSnapshotInput {
snapshot: self.voting_registrations.unwrap().into(),
update_timestamp: self.update_timestamp,
min_stake_threshold: self.min_stake_threshold,
voting_power_cap: self.voting_power_cap,
Expand Down
@@ -0,0 +1,102 @@
use crate::common::mainnet_wallet_ext::MainnetWalletExtension;
use crate::common::snapshot::mock;
use crate::common::MainnetWallet;
use assert_fs::TempDir;
use mainnet_lib::{MainnetNetworkBuilder, MainnetWalletStateBuilder};
use snapshot_lib::SnapshotInfo;
use snapshot_trigger_service::config::JobParameters;
use vit_servicing_station_tests::common::data::ArbitraryValidVotingTemplateGenerator;
use vit_servicing_station_tests::common::raw_snapshot::RawSnapshotBuilder;
use vit_servicing_station_tests::common::snapshot::VotingPower;
use vitup::config::VoteBlockchainTime;
use vitup::config::{Block0Initials, ConfigBuilder};
use vitup::testing::spawn_network;
use vitup::testing::vitup_setup;

#[test]
pub fn put_raw_snapshot() {
let testing_directory = TempDir::new().unwrap().into_persistent();
let stake = 10_000;
let alice_wallet = MainnetWallet::new(stake);
let bob_wallet = MainnetWallet::new(stake);
let clarice_wallet = MainnetWallet::new(stake);

let (db_sync, _reps) = MainnetNetworkBuilder::default()
.with(alice_wallet.as_direct_voter())
.with(bob_wallet.as_direct_voter())
.with(clarice_wallet.as_direct_voter())
.build(&testing_directory);

let job_params = JobParameters::fund("fund9");
let snapshot_result =
mock::do_snapshot(&db_sync, job_params.clone(), &testing_directory).unwrap();

let vote_timing = VoteBlockchainTime {
vote_start: 1,
tally_start: 2,
tally_end: 3,
slots_per_epoch: 30,
};

let config = ConfigBuilder::default()
.block0_initials(Block0Initials(vec![
alice_wallet.as_initial_entry(),
bob_wallet.as_initial_entry(),
clarice_wallet.as_initial_entry(),
]))
.vote_timing(vote_timing.into())
.slot_duration_in_seconds(2)
.proposals_count(3)
.voting_power(100)
.private(false)
.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();

let registrations = snapshot_result.registrations().clone();

let raw_snapshot = RawSnapshotBuilder::default()
.with_voting_registrations(registrations)
.with_tag(job_params.tag.as_ref().unwrap())
.build();

assert!(vit_station.check_running());

vit_station.put_raw_snapshot(&raw_snapshot).unwrap();

assert_eq!(
vec![job_params.tag.unwrap()],
vit_station.snapshot_tags().unwrap(),
"expected tags vs tags taken from REST API"
);

let snapshot_infos: Vec<SnapshotInfo> = raw_snapshot.clone().try_into().unwrap();

for snapshot_info in snapshot_infos.iter() {
let voting_power = VotingPower::from(snapshot_info.clone());
let voter_info = vit_station
.voter_info(&raw_snapshot.tag, &snapshot_info.hir.voting_key.to_hex())
.unwrap();
assert_eq!(
vec![voting_power],
voter_info.voter_info,
"wrong data for entry: {:?}",
snapshot_info
);
assert_eq!(
raw_snapshot.content.update_timestamp, voter_info.last_updated,
"wrong timestamp for entry: {:?}",
snapshot_info
);
}
}
1 change: 1 addition & 0 deletions src/vit-testing/integration-tests/src/integration/mod.rs
@@ -1,3 +1,4 @@
mod from_snapshot_to_catalyst_toolbox;
mod from_snapshot_to_merge;
mod from_snapshot_to_vit_servicing_station;
mod from_snapshot_to_vitup;
61 changes: 50 additions & 11 deletions src/vit-testing/mainnet-lib/src/wallet/registration.rs
Expand Up @@ -5,7 +5,7 @@ use cardano_serialization_lib::crypto::{Ed25519Signature, PublicKey};
use cardano_serialization_lib::error::JsError;
use cardano_serialization_lib::metadata::{
decode_metadatum_to_json_str, encode_json_value_to_metadatum, GeneralTransactionMetadata,
MetadataJsonSchema, MetadataMap, TransactionMetadatum, TransactionMetadatumLabel,
MetadataJsonSchema, MetadataList, MetadataMap, TransactionMetadatum, TransactionMetadatumLabel,
};
use cardano_serialization_lib::utils::{BigNum, Int};
use serde_json::{Map, Value};
Expand Down Expand Up @@ -73,19 +73,29 @@ impl<'a> RegistrationBuilder<'a> {
pub fn build(self) -> GeneralTransactionMetadata {
let mut meta_map: MetadataMap = MetadataMap::new();

match self.delegations.expect("no registration target defined") {
Delegations::New(_delegations) => {
unimplemented!("delegations not implemented");
let delegation_metadata = match self.delegations.expect("no registration target defined") {
Delegations::New(delegations) => {
let mut metadata_list = MetadataList::new();
for (delegation, weight) in delegations {
let mut inner_metadata_list = MetadataList::new();
inner_metadata_list.add(
&TransactionMetadatum::new_bytes(hex::decode(delegation.to_hex()).unwrap())
.unwrap(),
);
inner_metadata_list.add(&TransactionMetadatum::new_int(&Int::new(
&BigNum::from(weight),
)));
metadata_list.add(&TransactionMetadatum::new_list(&inner_metadata_list));
}
TransactionMetadatum::new_list(&metadata_list)
}
Delegations::Legacy(legacy) => {
meta_map.insert(
&METADATUM_1,
&TransactionMetadatum::new_bytes(hex::decode(legacy.to_hex()).unwrap())
.unwrap(),
);
TransactionMetadatum::new_bytes(hex::decode(legacy.to_hex()).unwrap()).unwrap()
}
};

meta_map.insert(&METADATUM_1, &delegation_metadata);

meta_map.insert(
&METADATUM_2,
&TransactionMetadatum::new_bytes(self.wallet.stake_public_key().as_bytes()).unwrap(),
Expand Down Expand Up @@ -314,8 +324,7 @@ mod test {
}

#[test]

pub fn root_metadata_serialization_bijection() {
pub fn root_metadata_serialization_bijection_direct() {
let metadata_string = r#"{
"61284": {
"1":"0xa6a3c0447aeb9cc54cf6422ba32b294e5e1c3ef6d782f2acff4a70694c4d1663",
Expand Down Expand Up @@ -343,4 +352,34 @@ mod test {
.unwrap();
assert_eq!(left, right);
}

#[test]
pub fn root_metadata_serialization_bijection_delegated() {
let metadata_string = r#"{
"61284": {
"1":[["0xa6a3c0447aeb9cc54cf6422ba32b294e5e1c3ef6d782f2acff4a70694c4d1663", 1], ["0x00588e8e1d18cba576a4d35758069fe94e53f638b6faf7c07b8abd2bc5c5cdee", 3]],
"2":"0x38ee57ed01f04e7bf553f85e6115faa3f0fc94e2a7bf471c939015d716d5dbe7",
"3":"0xe0ffc025718baab0ee4cf247706e419ae25d3f1be04006d2d8214de3ed",
"4":11614075,
"5":0
},
"61285": {
"1":"0x1249f6c152e555af356ae17746457669ae40bcee11ad0671a9a7e9e55441da8a12b152f67c88c15c46f938fd5a0a360dff50e12beeb48556e54ab1e3ea684108"
}
}"#;

let root_metadata = GeneralTransactionMetadata::from_json_string(
metadata_string,
MetadataJsonSchema::BasicConversions,
)
.unwrap();
let left: Value = serde_json::from_str(metadata_string).unwrap();
let right: Value = serde_json::from_str(
&root_metadata
.to_json_string(MetadataJsonSchema::BasicConversions)
.unwrap(),
)
.unwrap();
assert_eq!(left, right);
}
}
3 changes: 3 additions & 0 deletions src/vit-testing/vitup/Cargo.toml
Expand Up @@ -91,3 +91,6 @@ features = ["blocking", "rustls-tls", "json"]
version = "0.11"
default-features = false
features = ["blocking", "rustls-tls","native-tls", "json"]

[features]
soak = []
18 changes: 18 additions & 0 deletions src/vit-testing/vitup/resources/example/snapshot.json
@@ -0,0 +1,18 @@
[
{
"voting_key": "ed25519_pk19t8y8xl43uy99ywngpfcfsaklw76h48m965y5cszt5phmj2uv4psucdev9",
"voting_power": "1",
"group": "g1"
},
{
"voting_key": "ed25519_pk158t34dk8qmjs0mcwgsa5hg75qrg00wl2mejgt45vkelhf22d0wwqf22u0v",
"voting_power": "3",
"group": "g1"
},
{
"voting_key": "ed25519_pk1fht207rmf0wqfdx59n4fa4dskvqq0w88rh2hgnava0g825lspcmsctw8t2",
"voting_power": "4",
"group": "g1"
}
]

6 changes: 5 additions & 1 deletion src/vit-testing/vitup/resources/vit_station/down.sql
Expand Up @@ -9,4 +9,8 @@ DROP TABLE IF EXISTS proposal_community_choice_challenge;
DROP TABLE IF EXISTS community_advisors_reviews;
DROP VIEW IF EXISTS full_proposals_info;
DROP TABLE IF EXISTS goals;

DROP TABLE IF EXISTS groups;
DROP TABLE IF EXISTS votes;
DROP TABLE IF EXISTS snapshots;
DROP TABLE IF EXISTS voters;
DROP TABLE IF EXISTS contributions;
36 changes: 35 additions & 1 deletion src/vit-testing/vitup/resources/vit_station/up.sql
Expand Up @@ -133,6 +133,41 @@ create table groups (
PRIMARY KEY(token_identifier, fund_id)
);

create table votes (
fragment_id TEXT PRIMARY KEY,
caster TEXT NOT NULL,
proposal INTEGER NOT NULL,
voteplan_id TEXT NOT NULL,
time REAL NOT NULL,
choice SMALLINT,
raw_fragment TEXT NOT NULL
);

create table snapshots (
tag TEXT PRIMARY KEY,
last_updated BIGINT NOT NULL
);

create table voters (
voting_key TEXT NOT NULL,
voting_power BIGINT NOT NULL,
voting_group TEXT NOT NULL,
snapshot_tag TEXT NOT NULL,
PRIMARY KEY(voting_key, voting_group, snapshot_tag),
FOREIGN KEY(snapshot_tag) REFERENCES snapshots(tag) ON DELETE CASCADE
);

create table contributions (
stake_public_key TEXT NOT NULL,
reward_address TEXT NOT NULL,
value BIGINT NOT NULL,
voting_key TEXT NOT NULL,
voting_group TEXT NOT NULL,
snapshot_tag TEXT NOT NULL,
PRIMARY KEY(stake_public_key, voting_key, voting_group, snapshot_tag),
FOREIGN KEY(snapshot_tag) REFERENCES snapshots(tag) ON DELETE CASCADE
);

CREATE VIEW full_proposals_info
AS
SELECT
Expand Down Expand Up @@ -167,4 +202,3 @@ FROM
and challenges.challenge_type = 'community-choice'
LEFT JOIN (SELECT proposal_id as review_proposal_id, COUNT (DISTINCT assessor) as reviews_count FROM community_advisors_reviews GROUP BY proposal_id)
on proposals.proposal_id = review_proposal_id;

0 comments on commit 8883dd1

Please sign in to comment.