From 3accbecec5f319e371bb74793d497ef51487aa7f Mon Sep 17 00:00:00 2001 From: Artur Michalek Date: Thu, 22 Aug 2024 12:23:47 +0200 Subject: [PATCH 01/16] Store submissions with categories --- contracts/governance/src/lib.rs | 11 ++++--- contracts/governance/src/storage.rs | 6 ++-- contracts/governance/src/types.rs | 25 +++++++++++++- contracts/governance/tests/e2e/auth.rs | 19 ++++++++--- contracts/governance/tests/e2e/voting.rs | 42 +++++++++++++++++------- 5 files changed, 79 insertions(+), 24 deletions(-) diff --git a/contracts/governance/src/lib.rs b/contracts/governance/src/lib.rs index e88b336..794c923 100644 --- a/contracts/governance/src/lib.rs +++ b/contracts/governance/src/lib.rs @@ -24,7 +24,7 @@ use crate::storage::{ write_submissions, write_voting_powers, LayerKeyData, NeuronKeyData, NeuronResultKeyData, SubmissionVotesKeyData, SubmissionsKeyData, VotingPowersKeyData, }; -use crate::types::{Vote, VotingSystemError, ABSTAIN_VOTING_POWER}; +use crate::types::{Submission, Vote, VotingSystemError, ABSTAIN_VOTING_POWER}; mod admin; mod neural_governance; @@ -95,7 +95,7 @@ impl VotingSystem { } /// Set multiple submissions. - pub fn set_submissions(env: Env, new_submissions: Vec) { + pub fn set_submissions(env: Env, new_submissions: Vec) { require_admin(&env); let mut submissions = Vec::new(&env); @@ -111,7 +111,7 @@ impl VotingSystem { } /// Get submissions for the active round. - pub fn get_submissions(env: &Env) -> Vec { + pub fn get_submissions(env: &Env) -> Vec { read_submissions(env, Self::get_current_round(env)) } @@ -123,7 +123,10 @@ impl VotingSystem { ) -> Result<(), VotingSystemError> { require_admin(env); - if !read_submissions(env, Self::get_current_round(env)).contains(submission_id.clone()) { + if !read_submissions(env, Self::get_current_round(env)) + .iter() + .any(|sub| sub.id == submission_id) + { return Err(VotingSystemError::SubmissionDoesNotExist); } diff --git a/contracts/governance/src/storage.rs b/contracts/governance/src/storage.rs index db73924..11f1414 100644 --- a/contracts/governance/src/storage.rs +++ b/contracts/governance/src/storage.rs @@ -5,7 +5,7 @@ use crate::storage::key_data::{ get_layer_key, get_neuron_key, get_neuron_result_key, get_submission_votes_key, get_submissions_key, get_voting_powers_key, }; -use crate::types::{Vote, VotingSystemError}; +use crate::types::{Submission, Vote, VotingSystemError}; use crate::{ContractResult, DataKey}; pub use crate::storage::key_data::{ @@ -101,7 +101,7 @@ pub(crate) fn write_submission_votes( env.storage().persistent().set(&key, votes); } -pub(crate) fn read_submissions(env: &Env, round: u32) -> Vec { +pub(crate) fn read_submissions(env: &Env, round: u32) -> Vec { let key = get_submissions_key(round); env.storage() .persistent() @@ -109,7 +109,7 @@ pub(crate) fn read_submissions(env: &Env, round: u32) -> Vec { .unwrap_or_else(|| Vec::new(env)) } -pub(crate) fn write_submissions(env: &Env, round: u32, submissions: &Vec) { +pub(crate) fn write_submissions(env: &Env, round: u32, submissions: &Vec) { let key = get_submissions_key(round); env.storage().persistent().set(&key, submissions); } diff --git a/contracts/governance/src/types.rs b/contracts/governance/src/types.rs index 5bb66d5..3656f20 100644 --- a/contracts/governance/src/types.rs +++ b/contracts/governance/src/types.rs @@ -1,7 +1,30 @@ -use soroban_sdk::{contracterror, contracttype}; +use soroban_sdk::{contracterror, contracttype, String}; pub const ABSTAIN_VOTING_POWER: i32 = 0; +#[contracttype] +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +pub enum SubmissionCategory { + Application, + FinancialProtocols, + InfrastructureAndServices, + DeveloperTools, +} + +#[contracttype] +#[non_exhaustive] +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct Submission { + pub id: String, + pub category: SubmissionCategory, +} + +impl Submission { + pub fn new(id: String, category: SubmissionCategory) -> Self { + Self { id, category } + } +} + #[contracttype] #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum Vote { diff --git a/contracts/governance/tests/e2e/auth.rs b/contracts/governance/tests/e2e/auth.rs index 694e5cf..a522850 100644 --- a/contracts/governance/tests/e2e/auth.rs +++ b/contracts/governance/tests/e2e/auth.rs @@ -1,11 +1,11 @@ +use crate::e2e::common::contract_utils::deploy_contract_without_initialization; +use governance::types::{Submission, SubmissionCategory}; use soroban_sdk::testutils::{ Address as AddressTrait, AuthorizedFunction, AuthorizedInvocation, MockAuth, MockAuthInvoke, }; use soroban_sdk::xdr::{ScErrorCode, ScErrorType}; use soroban_sdk::{vec, Address, Env, Error, IntoVal, String, Symbol}; -use crate::e2e::common::contract_utils::deploy_contract_without_initialization; - #[test] fn uninitialized_contract_is_not_callable() { let env = Env::default(); @@ -32,7 +32,10 @@ fn auth() { contract_client.initialize(&admin, &25); let submission_name = String::from_str(&env, "abc"); - contract_client.set_submissions(&vec![&env, submission_name.clone()]); + contract_client.set_submissions(&vec![ + &env, + Submission::new(submission_name.clone(), SubmissionCategory::Application), + ]); dbg!(&env.auths()); @@ -84,7 +87,10 @@ fn transfer_admin() { sub_invokes: &[], }, }]); - let result = contract_client.try_set_submissions(&vec![&env, submission_name.clone()]); + let result = contract_client.try_set_submissions(&vec![ + &env, + Submission::new(submission_name.clone(), SubmissionCategory::Application), + ]); assert!(result.is_err()); // Verify new admin can modify state @@ -97,7 +103,10 @@ fn transfer_admin() { sub_invokes: &[], }, }]); - contract_client.set_submissions(&vec![&env, submission_name]); + contract_client.set_submissions(&vec![ + &env, + Submission::new(submission_name.clone(), SubmissionCategory::Application), + ]); } #[test] diff --git a/contracts/governance/tests/e2e/voting.rs b/contracts/governance/tests/e2e/voting.rs index 5036b6e..bafea20 100644 --- a/contracts/governance/tests/e2e/voting.rs +++ b/contracts/governance/tests/e2e/voting.rs @@ -1,6 +1,6 @@ use soroban_sdk::{vec, Env, Map, String, Vec, I256}; -use governance::types::{Vote, VotingSystemError}; +use governance::types::{Submission, SubmissionCategory, Vote, VotingSystemError}; use governance::{LayerAggregator, DECIMALS}; use crate::e2e::common::contract_utils::deploy_contract; @@ -29,7 +29,11 @@ fn voting_data_upload() { let submission1 = String::from_str(&env, "submission1"); let submission2 = String::from_str(&env, "submission2"); - contract_client.set_submissions(&vec![&env, submission1.clone(), submission2.clone()]); + contract_client.set_submissions(&vec![ + &env, + Submission::new(submission1.clone(), SubmissionCategory::Application), + Submission::new(submission2.clone(), SubmissionCategory::Application), + ]); let mut votes_submission1 = Map::new(&env); votes_submission1.set(user1.clone(), Vote::Yes); @@ -45,7 +49,11 @@ fn voting_data_upload() { contract_client.set_votes_for_submission(&submission1, &votes_submission1); contract_client.set_votes_for_submission(&submission2, &votes_submission2); - contract_client.set_submissions(&vec![&env, submission1.clone(), submission2.clone()]); + contract_client.set_submissions(&vec![ + &env, + Submission::new(submission1.clone(), SubmissionCategory::Application), + Submission::new(submission2.clone(), SubmissionCategory::Application), + ]); let mut neuron_result = Map::new(&env); neuron_result.set(user1.clone(), I256::from_i128(&env, 100 * DECIMALS)); @@ -115,13 +123,16 @@ fn adding_duplicate_submissions() { contract_client.set_submissions(&vec![ &env, - String::from_str(&env, "a"), - String::from_str(&env, "a"), + Submission::new(String::from_str(&env, "a"), SubmissionCategory::Application), + Submission::new(String::from_str(&env, "a"), SubmissionCategory::Application), ]); let submissions = contract_client.get_submissions(); let mut expected = Vec::new(&env); - expected.push_back(String::from_str(&env, "a")); + expected.push_back(Submission::new( + String::from_str(&env, "a"), + SubmissionCategory::Application, + )); assert_eq!(submissions, expected); } @@ -167,7 +178,10 @@ fn set_bump_round_flow() { ); // Set votes and results for round 25 - contract_client.set_submissions(&vec![&env, submission.clone()]); + contract_client.set_submissions(&vec![ + &env, + Submission::new(submission.clone(), SubmissionCategory::Application), + ]); let mut votes25 = Map::new(&env); votes25.set(user1.clone(), Vote::Yes); @@ -193,7 +207,8 @@ fn set_bump_round_flow() { // Verify submission is active assert!(contract_client .get_submissions() - .contains(submission.clone())); + .iter() + .any(|sub| sub.id == submission)); // Bump the round contract_client.set_current_round(&26); @@ -216,7 +231,10 @@ fn set_bump_round_flow() { // Set votes and results for round 26 let new_submission = String::from_str(&env, "sub2"); - contract_client.set_submissions(&vec![&env, new_submission.clone()]); + contract_client.set_submissions(&vec![ + &env, + Submission::new(new_submission.clone(), SubmissionCategory::Application), + ]); let mut votes26 = Map::new(&env); votes26.set(user1.clone(), Vote::No); @@ -242,10 +260,12 @@ fn set_bump_round_flow() { // Verify new submission is active and old is not assert!(contract_client .get_submissions() - .contains(new_submission.clone())); + .iter() + .any(|sub| sub.id == new_submission)); assert!(!contract_client .get_submissions() - .contains(submission.clone())); + .iter() + .any(|sub| sub.id == submission)); // Verify historical results are still accessible assert_eq!( From 177964f476324a7ff13280c380d944cfc98238ed Mon Sep 17 00:00:00 2001 From: Artur Michalek Date: Fri, 23 Aug 2024 11:59:49 +0200 Subject: [PATCH 02/16] Add tests to delegation resolving --- neurons/src/quorum.rs | 242 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 241 insertions(+), 1 deletion(-) diff --git a/neurons/src/quorum.rs b/neurons/src/quorum.rs index 705761f..20d6b5f 100644 --- a/neurons/src/quorum.rs +++ b/neurons/src/quorum.rs @@ -2,7 +2,7 @@ use crate::Vote; use anyhow::{anyhow, bail, Result}; use std::collections::HashMap; -const QUORUM_SIZE: u32 = 3; +const QUORUM_SIZE: u32 = 5; const QUORUM_ABSOLUTE_PARTICIPATION_THRESHOLD: f64 = 1.0 / 2.0; const QUORUM_RELATIVE_PARTICIPATION_THRESHOLD: f64 = 2.0 / 3.0; @@ -103,3 +103,243 @@ fn calculate_quorum_consensus( }, ) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn resolve_delegate_yes() { + let mut submission_votes = HashMap::new(); + let mut delegates_for_user = HashMap::new(); + + let user1 = String::from("user1"); + let user2 = String::from("user2"); + let user3 = String::from("user3"); + let user4 = String::from("user4"); + let user5 = String::from("user5"); + let user6 = String::from("user6"); + // let user7 = String::from("user7"); + // let user8 = String::from("user8"); + // let user9 = String::from("user9"); + // let user10 = String::from("user10"); + + submission_votes.insert(user1.clone(), Vote::Delegate); + submission_votes.insert(user2.clone(), Vote::Yes); + submission_votes.insert(user3.clone(), Vote::Yes); + submission_votes.insert(user4.clone(), Vote::Yes); + submission_votes.insert(user5.clone(), Vote::Yes); + submission_votes.insert(user6.clone(), Vote::Yes); + + delegates_for_user.insert( + user1.clone(), + vec![ + user2.clone(), + user3.clone(), + user4.clone(), + user5.clone(), + user6.clone(), + ], + ); + + let normalized_votes = + normalize_votes_for_submission(&submission_votes, &delegates_for_user).unwrap(); + + assert_eq!(normalized_votes.get(&user1).unwrap(), &Vote::Yes); + assert_eq!(normalized_votes.get(&user2).unwrap(), &Vote::Yes); + assert_eq!(normalized_votes.get(&user3).unwrap(), &Vote::Yes); + assert_eq!(normalized_votes.get(&user4).unwrap(), &Vote::Yes); + assert_eq!(normalized_votes.get(&user5).unwrap(), &Vote::Yes); + assert_eq!(normalized_votes.get(&user6).unwrap(), &Vote::Yes); + } + + #[test] + fn resolve_delegate_no() { + let mut submission_votes = HashMap::new(); + let mut delegates_for_user = HashMap::new(); + + let user1 = String::from("user1"); + let user2 = String::from("user2"); + let user3 = String::from("user3"); + let user4 = String::from("user4"); + let user5 = String::from("user5"); + let user6 = String::from("user6"); + + submission_votes.insert(user1.clone(), Vote::Delegate); + submission_votes.insert(user2.clone(), Vote::No); + submission_votes.insert(user3.clone(), Vote::No); + submission_votes.insert(user4.clone(), Vote::No); + submission_votes.insert(user5.clone(), Vote::No); + submission_votes.insert(user6.clone(), Vote::No); + + delegates_for_user.insert( + user1.clone(), + vec![ + user2.clone(), + user3.clone(), + user4.clone(), + user5.clone(), + user6.clone(), + ], + ); + + let normalized_votes = + normalize_votes_for_submission(&submission_votes, &delegates_for_user).unwrap(); + + assert_eq!(normalized_votes.get(&user1).unwrap(), &Vote::No); + assert_eq!(normalized_votes.get(&user2).unwrap(), &Vote::No); + assert_eq!(normalized_votes.get(&user3).unwrap(), &Vote::No); + assert_eq!(normalized_votes.get(&user4).unwrap(), &Vote::No); + assert_eq!(normalized_votes.get(&user5).unwrap(), &Vote::No); + assert_eq!(normalized_votes.get(&user6).unwrap(), &Vote::No); + } + + #[test] + fn resolve_delegate_abstain() { + let mut submission_votes = HashMap::new(); + let mut delegates_for_user = HashMap::new(); + + let user1 = String::from("user1"); + let user2 = String::from("user2"); + let user3 = String::from("user3"); + let user4 = String::from("user4"); + let user5 = String::from("user5"); + let user6 = String::from("user6"); + + // Agreement = 3 - 2 = 1 + // Relative agreement = 1 / 5 + // Absolute agreement = 1 / 5 + submission_votes.insert(user1.clone(), Vote::Delegate); + submission_votes.insert(user2.clone(), Vote::Yes); + submission_votes.insert(user3.clone(), Vote::Yes); + submission_votes.insert(user4.clone(), Vote::Yes); + submission_votes.insert(user5.clone(), Vote::No); + submission_votes.insert(user6.clone(), Vote::No); + + delegates_for_user.insert( + user1.clone(), + vec![ + user2.clone(), + user3.clone(), + user4.clone(), + user5.clone(), + user6.clone(), + ], + ); + + let normalized_votes = + normalize_votes_for_submission(&submission_votes, &delegates_for_user).unwrap(); + + assert_eq!(normalized_votes.get(&user1).unwrap(), &Vote::Abstain); + assert_eq!(normalized_votes.get(&user2).unwrap(), &Vote::Yes); + assert_eq!(normalized_votes.get(&user3).unwrap(), &Vote::Yes); + assert_eq!(normalized_votes.get(&user4).unwrap(), &Vote::Yes); + assert_eq!(normalized_votes.get(&user5).unwrap(), &Vote::No); + assert_eq!(normalized_votes.get(&user6).unwrap(), &Vote::No); + } + + #[test] + fn non_voting_delegates_are_ignored() { + let mut submission_votes = HashMap::new(); + let mut delegates_for_user = HashMap::new(); + + let user1 = String::from("user1"); + let user2 = String::from("user2"); + let user3 = String::from("user3"); + let user4 = String::from("user4"); + let user5 = String::from("user5"); + let user6 = String::from("user6"); + + // Agreement = 3 + // Relative agreement = 3 / 3 + // Absolute agreement = 3 / 5 + submission_votes.insert(user1.clone(), Vote::Delegate); + submission_votes.insert(user2.clone(), Vote::Abstain); + submission_votes.insert(user3.clone(), Vote::Abstain); + submission_votes.insert(user4.clone(), Vote::Yes); + submission_votes.insert(user5.clone(), Vote::Yes); + submission_votes.insert(user6.clone(), Vote::Yes); + + delegates_for_user.insert( + user1.clone(), + vec![ + user2.clone(), + user3.clone(), + user4.clone(), + user5.clone(), + user6.clone(), + ], + ); + + let normalized_votes = + normalize_votes_for_submission(&submission_votes, &delegates_for_user).unwrap(); + + assert_eq!(normalized_votes.get(&user1).unwrap(), &Vote::Yes); + assert_eq!(normalized_votes.get(&user2).unwrap(), &Vote::Abstain); + assert_eq!(normalized_votes.get(&user3).unwrap(), &Vote::Abstain); + assert_eq!(normalized_votes.get(&user4).unwrap(), &Vote::Yes); + assert_eq!(normalized_votes.get(&user5).unwrap(), &Vote::Yes); + assert_eq!(normalized_votes.get(&user6).unwrap(), &Vote::Yes); + } + + #[test] + fn relative_threshold_not_passed() { + let mut submission_votes = HashMap::new(); + let mut delegates_for_user = HashMap::new(); + + let user1 = String::from("user1"); + let user2 = String::from("user2"); + let user3 = String::from("user3"); + + // Agreement = 2 + // Relative agreement = 2 / 2 + // Absolute agreement = 2 / 5 + submission_votes.insert(user1.clone(), Vote::Delegate); + submission_votes.insert(user2.clone(), Vote::Yes); + submission_votes.insert(user3.clone(), Vote::Yes); + + delegates_for_user.insert(user1.clone(), vec![user2.clone(), user3.clone()]); + + let normalized_votes = + normalize_votes_for_submission(&submission_votes, &delegates_for_user).unwrap(); + + assert_eq!(normalized_votes.get(&user1).unwrap(), &Vote::Abstain); + assert_eq!(normalized_votes.get(&user2).unwrap(), &Vote::Yes); + assert_eq!(normalized_votes.get(&user3).unwrap(), &Vote::Yes); + } + + #[test] + fn absolute_threshold_not_passed() { + let mut submission_votes = HashMap::new(); + let mut delegates_for_user = HashMap::new(); + + let user1 = String::from("user1"); + let user2 = String::from("user2"); + let user3 = String::from("user3"); + let user4 = String::from("user4"); + let user5 = String::from("user5"); + let user6 = String::from("user6"); + + // Agreement = 4 - 1 = 3 + // Relative agreement = 3 / 5 + // Absolute agreement = 3 / 5 + submission_votes.insert(user1.clone(), Vote::Delegate); + submission_votes.insert(user2.clone(), Vote::Yes); + submission_votes.insert(user3.clone(), Vote::Yes); + submission_votes.insert(user4.clone(), Vote::Yes); + submission_votes.insert(user5.clone(), Vote::Yes); + submission_votes.insert(user6.clone(), Vote::No); + + delegates_for_user.insert(user1.clone(), vec![user2.clone(), user3.clone()]); + + let normalized_votes = + normalize_votes_for_submission(&submission_votes, &delegates_for_user).unwrap(); + + assert_eq!(normalized_votes.get(&user1).unwrap(), &Vote::Abstain); + assert_eq!(normalized_votes.get(&user2).unwrap(), &Vote::Yes); + assert_eq!(normalized_votes.get(&user3).unwrap(), &Vote::Yes); + assert_eq!(normalized_votes.get(&user4).unwrap(), &Vote::Yes); + assert_eq!(normalized_votes.get(&user5).unwrap(), &Vote::Yes); + assert_eq!(normalized_votes.get(&user6).unwrap(), &Vote::No); + } +} From e2f4670e17f7653433260bf14570dbd4113167ed Mon Sep 17 00:00:00 2001 From: Artur Michalek Date: Fri, 23 Aug 2024 14:32:16 +0200 Subject: [PATCH 03/16] Change quorum resolving to support submissions categories --- neurons/src/lib.rs | 22 ++++ neurons/src/main.rs | 8 +- neurons/src/quorum.rs | 300 +++++++++++++++++++++++++++++++++++------- 3 files changed, 277 insertions(+), 53 deletions(-) diff --git a/neurons/src/lib.rs b/neurons/src/lib.rs index 738003b..3c82a89 100644 --- a/neurons/src/lib.rs +++ b/neurons/src/lib.rs @@ -3,6 +3,28 @@ use serde::{Deserialize, Serialize}; pub mod neurons; pub mod quorum; +#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] +pub enum SubmissionCategory { + Application, + FinancialProtocols, + InfrastructureAndServices, + DeveloperTools, +} + +#[non_exhaustive] +#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] +pub struct Submission { + name: String, + category: SubmissionCategory, +} + +impl Submission { + #[must_use] + pub fn new(name: String, category: SubmissionCategory) -> Self { + Self { name, category } + } +} + #[derive(Deserialize, Serialize, Debug, Clone, PartialEq)] pub enum Vote { Yes, diff --git a/neurons/src/main.rs b/neurons/src/main.rs index 7e35d04..c59eea6 100644 --- a/neurons/src/main.rs +++ b/neurons/src/main.rs @@ -3,8 +3,8 @@ use neurons::neurons::assigned_reputation::AssignedReputationNeuron; use neurons::neurons::prior_voting_history::PriorVotingHistoryNeuron; use neurons::neurons::trust_graph::TrustGraphNeuron; use neurons::neurons::Neuron; -use neurons::quorum::normalize_votes; -use neurons::Vote; +use neurons::quorum::{normalize_votes, DelegateesForUser}; +use neurons::{Submission, Vote}; use serde::Serialize; use std::collections::{BTreeMap, HashMap}; use std::fs; @@ -57,10 +57,10 @@ fn main() { let users: Vec = serde_json::from_str(users_raw.as_str()).unwrap(); let votes_raw = fs::read_to_string("data/votes.json").unwrap(); - let votes: HashMap> = + let votes: HashMap> = serde_json::from_str(votes_raw.as_str()).unwrap(); let delegatees_for_user_raw = fs::read_to_string("data/delegatees_for_user.json").unwrap(); - let delegatees_for_user: HashMap> = + let delegatees_for_user: HashMap = serde_json::from_str(delegatees_for_user_raw.as_str()).unwrap(); let normalized_votes = normalize_votes(votes, &delegatees_for_user).unwrap(); write_result( diff --git a/neurons/src/quorum.rs b/neurons/src/quorum.rs index 20d6b5f..9dc10a1 100644 --- a/neurons/src/quorum.rs +++ b/neurons/src/quorum.rs @@ -1,29 +1,74 @@ -use crate::Vote; +use crate::{Submission, SubmissionCategory, Vote}; use anyhow::{anyhow, bail, Result}; +use serde::{Deserialize, Serialize}; use std::collections::HashMap; const QUORUM_SIZE: u32 = 5; const QUORUM_ABSOLUTE_PARTICIPATION_THRESHOLD: f64 = 1.0 / 2.0; const QUORUM_RELATIVE_PARTICIPATION_THRESHOLD: f64 = 2.0 / 3.0; +#[non_exhaustive] +#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)] +pub struct DelegateesForUser { + application: Vec, + financial_protocols: Vec, + infrastructure_and_services: Vec, + developer_tools: Vec, +} + +impl DelegateesForUser { + #[must_use] + pub fn new( + application: Vec, + financial_protocols: Vec, + infrastructure_and_services: Vec, + developer_tools: Vec, + ) -> Self { + Self { + application, + financial_protocols, + infrastructure_and_services, + developer_tools, + } + } +} + #[allow(clippy::implicit_hasher)] pub fn normalize_votes( - votes: HashMap>, - delegatees_for_user: &HashMap>, -) -> Result>> { + votes: HashMap>, + delegatees_for_user: &HashMap, +) -> Result>> { votes .into_iter() .map(|(submission, submission_votes)| { - let submission_votes = - normalize_votes_for_submission(&submission_votes, delegatees_for_user)?; + let submission_votes = normalize_votes_for_submission( + &submission, + &submission_votes, + delegatees_for_user, + )?; Ok((submission, submission_votes)) }) .collect::>() } +fn delegatees_for_category<'a>( + submission_category: &SubmissionCategory, + delegatees_for_user: &'a DelegateesForUser, +) -> &'a Vec { + match submission_category { + SubmissionCategory::Application => &delegatees_for_user.application, + SubmissionCategory::FinancialProtocols => &delegatees_for_user.financial_protocols, + SubmissionCategory::InfrastructureAndServices => { + &delegatees_for_user.infrastructure_and_services + } + SubmissionCategory::DeveloperTools => &delegatees_for_user.developer_tools, + } +} + fn normalize_votes_for_submission( + submission: &Submission, submission_votes: &HashMap, - delegatees_for_user: &HashMap>, + delegatees_for_user: &HashMap, ) -> Result> { submission_votes .clone() @@ -33,6 +78,7 @@ fn normalize_votes_for_submission( let delegatees = delegatees_for_user .get(&user) .ok_or_else(|| anyhow!("Delegatees missing for user {user}"))?; + let delegatees = delegatees_for_category(&submission.category, delegatees); let normalized_vote = calculate_quorum_consensus(delegatees, submission_votes)?; Ok((user, normalized_vote)) } else { @@ -133,17 +179,26 @@ mod tests { delegates_for_user.insert( user1.clone(), - vec![ - user2.clone(), - user3.clone(), - user4.clone(), - user5.clone(), - user6.clone(), - ], + DelegateesForUser::new( + vec![ + user2.clone(), + user3.clone(), + user4.clone(), + user5.clone(), + user6.clone(), + ], + vec![], + vec![], + vec![], + ), ); - let normalized_votes = - normalize_votes_for_submission(&submission_votes, &delegates_for_user).unwrap(); + let normalized_votes = normalize_votes_for_submission( + &Submission::new("sub1".to_string(), SubmissionCategory::Application), + &submission_votes, + &delegates_for_user, + ) + .unwrap(); assert_eq!(normalized_votes.get(&user1).unwrap(), &Vote::Yes); assert_eq!(normalized_votes.get(&user2).unwrap(), &Vote::Yes); @@ -174,17 +229,26 @@ mod tests { delegates_for_user.insert( user1.clone(), - vec![ - user2.clone(), - user3.clone(), - user4.clone(), - user5.clone(), - user6.clone(), - ], + DelegateesForUser::new( + vec![ + user2.clone(), + user3.clone(), + user4.clone(), + user5.clone(), + user6.clone(), + ], + vec![], + vec![], + vec![], + ), ); - let normalized_votes = - normalize_votes_for_submission(&submission_votes, &delegates_for_user).unwrap(); + let normalized_votes = normalize_votes_for_submission( + &Submission::new("sub1".to_string(), SubmissionCategory::Application), + &submission_votes, + &delegates_for_user, + ) + .unwrap(); assert_eq!(normalized_votes.get(&user1).unwrap(), &Vote::No); assert_eq!(normalized_votes.get(&user2).unwrap(), &Vote::No); @@ -218,17 +282,26 @@ mod tests { delegates_for_user.insert( user1.clone(), - vec![ - user2.clone(), - user3.clone(), - user4.clone(), - user5.clone(), - user6.clone(), - ], + DelegateesForUser::new( + vec![ + user2.clone(), + user3.clone(), + user4.clone(), + user5.clone(), + user6.clone(), + ], + vec![], + vec![], + vec![], + ), ); - let normalized_votes = - normalize_votes_for_submission(&submission_votes, &delegates_for_user).unwrap(); + let normalized_votes = normalize_votes_for_submission( + &Submission::new("sub1".to_string(), SubmissionCategory::Application), + &submission_votes, + &delegates_for_user, + ) + .unwrap(); assert_eq!(normalized_votes.get(&user1).unwrap(), &Vote::Abstain); assert_eq!(normalized_votes.get(&user2).unwrap(), &Vote::Yes); @@ -262,17 +335,26 @@ mod tests { delegates_for_user.insert( user1.clone(), - vec![ - user2.clone(), - user3.clone(), - user4.clone(), - user5.clone(), - user6.clone(), - ], + DelegateesForUser::new( + vec![ + user2.clone(), + user3.clone(), + user4.clone(), + user5.clone(), + user6.clone(), + ], + vec![], + vec![], + vec![], + ), ); - let normalized_votes = - normalize_votes_for_submission(&submission_votes, &delegates_for_user).unwrap(); + let normalized_votes = normalize_votes_for_submission( + &Submission::new("sub1".to_string(), SubmissionCategory::Application), + &submission_votes, + &delegates_for_user, + ) + .unwrap(); assert_eq!(normalized_votes.get(&user1).unwrap(), &Vote::Yes); assert_eq!(normalized_votes.get(&user2).unwrap(), &Vote::Abstain); @@ -298,10 +380,17 @@ mod tests { submission_votes.insert(user2.clone(), Vote::Yes); submission_votes.insert(user3.clone(), Vote::Yes); - delegates_for_user.insert(user1.clone(), vec![user2.clone(), user3.clone()]); + delegates_for_user.insert( + user1.clone(), + DelegateesForUser::new(vec![user2.clone(), user3.clone()], vec![], vec![], vec![]), + ); - let normalized_votes = - normalize_votes_for_submission(&submission_votes, &delegates_for_user).unwrap(); + let normalized_votes = normalize_votes_for_submission( + &Submission::new("sub1".to_string(), SubmissionCategory::Application), + &submission_votes, + &delegates_for_user, + ) + .unwrap(); assert_eq!(normalized_votes.get(&user1).unwrap(), &Vote::Abstain); assert_eq!(normalized_votes.get(&user2).unwrap(), &Vote::Yes); @@ -330,10 +419,17 @@ mod tests { submission_votes.insert(user5.clone(), Vote::Yes); submission_votes.insert(user6.clone(), Vote::No); - delegates_for_user.insert(user1.clone(), vec![user2.clone(), user3.clone()]); + delegates_for_user.insert( + user1.clone(), + DelegateesForUser::new(vec![user2.clone(), user3.clone()], vec![], vec![], vec![]), + ); - let normalized_votes = - normalize_votes_for_submission(&submission_votes, &delegates_for_user).unwrap(); + let normalized_votes = normalize_votes_for_submission( + &Submission::new("sub1".to_string(), SubmissionCategory::Application), + &submission_votes, + &delegates_for_user, + ) + .unwrap(); assert_eq!(normalized_votes.get(&user1).unwrap(), &Vote::Abstain); assert_eq!(normalized_votes.get(&user2).unwrap(), &Vote::Yes); @@ -342,4 +438,110 @@ mod tests { assert_eq!(normalized_votes.get(&user5).unwrap(), &Vote::Yes); assert_eq!(normalized_votes.get(&user6).unwrap(), &Vote::No); } + + #[test] + fn resolve_delegates_from_multiple_categories() { + let mut submission1_votes = HashMap::new(); + let mut submission2_votes = HashMap::new(); + let mut delegates_for_user = HashMap::new(); + + let user1 = String::from("user1"); + let user2 = String::from("user2"); + let user3 = String::from("user3"); + let user4 = String::from("user4"); + let user5 = String::from("user5"); + let user6 = String::from("user6"); + let user7 = String::from("user7"); + let user8 = String::from("user8"); + let user9 = String::from("user9"); + let user10 = String::from("user10"); + + submission1_votes.insert(user1.clone(), Vote::Delegate); + submission1_votes.insert(user2.clone(), Vote::Yes); + submission1_votes.insert(user3.clone(), Vote::Yes); + submission1_votes.insert(user4.clone(), Vote::Yes); + submission1_votes.insert(user5.clone(), Vote::Yes); + submission1_votes.insert(user6.clone(), Vote::Yes); + + submission2_votes.insert(user1.clone(), Vote::Delegate); + submission2_votes.insert(user7.clone(), Vote::No); + submission2_votes.insert(user8.clone(), Vote::No); + submission2_votes.insert(user9.clone(), Vote::No); + submission2_votes.insert(user10.clone(), Vote::No); + + delegates_for_user.insert( + user1.clone(), + DelegateesForUser::new( + vec![ + user2.clone(), + user3.clone(), + user4.clone(), + user5.clone(), + user6.clone(), + ], + vec![user7.clone(), user8.clone(), user9.clone(), user10.clone()], + vec![], + vec![], + ), + ); + + let normalized_votes_for_submission1 = normalize_votes_for_submission( + &Submission::new("sub1".to_string(), SubmissionCategory::Application), + &submission1_votes, + &delegates_for_user, + ) + .unwrap(); + let normalized_votes_for_submission2 = normalize_votes_for_submission( + &Submission::new("sub2".to_string(), SubmissionCategory::FinancialProtocols), + &submission2_votes, + &delegates_for_user, + ) + .unwrap(); + + assert_eq!( + normalized_votes_for_submission1.get(&user1).unwrap(), + &Vote::Yes + ); + assert_eq!( + normalized_votes_for_submission1.get(&user2).unwrap(), + &Vote::Yes + ); + assert_eq!( + normalized_votes_for_submission1.get(&user3).unwrap(), + &Vote::Yes + ); + assert_eq!( + normalized_votes_for_submission1.get(&user4).unwrap(), + &Vote::Yes + ); + assert_eq!( + normalized_votes_for_submission1.get(&user5).unwrap(), + &Vote::Yes + ); + assert_eq!( + normalized_votes_for_submission1.get(&user6).unwrap(), + &Vote::Yes + ); + + assert_eq!( + normalized_votes_for_submission2.get(&user1).unwrap(), + &Vote::No + ); + assert_eq!( + normalized_votes_for_submission2.get(&user7).unwrap(), + &Vote::No + ); + assert_eq!( + normalized_votes_for_submission2.get(&user8).unwrap(), + &Vote::No + ); + assert_eq!( + normalized_votes_for_submission2.get(&user9).unwrap(), + &Vote::No + ); + assert_eq!( + normalized_votes_for_submission2.get(&user10).unwrap(), + &Vote::No + ); + } } From b38ef68a867d4bfe1a546dae830488ad3fdbcc96 Mon Sep 17 00:00:00 2001 From: Artur Michalek Date: Thu, 29 Aug 2024 14:57:25 +0200 Subject: [PATCH 04/16] Rename application to applications --- contracts/governance/src/types.rs | 2 +- contracts/governance/tests/e2e/auth.rs | 6 +++--- contracts/governance/tests/e2e/voting.rs | 18 +++++++++--------- neurons/src/lib.rs | 2 +- neurons/src/quorum.rs | 20 ++++++++++---------- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/contracts/governance/src/types.rs b/contracts/governance/src/types.rs index 3656f20..c2ccfc7 100644 --- a/contracts/governance/src/types.rs +++ b/contracts/governance/src/types.rs @@ -5,7 +5,7 @@ pub const ABSTAIN_VOTING_POWER: i32 = 0; #[contracttype] #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum SubmissionCategory { - Application, + Applications, FinancialProtocols, InfrastructureAndServices, DeveloperTools, diff --git a/contracts/governance/tests/e2e/auth.rs b/contracts/governance/tests/e2e/auth.rs index a522850..b41d454 100644 --- a/contracts/governance/tests/e2e/auth.rs +++ b/contracts/governance/tests/e2e/auth.rs @@ -34,7 +34,7 @@ fn auth() { let submission_name = String::from_str(&env, "abc"); contract_client.set_submissions(&vec![ &env, - Submission::new(submission_name.clone(), SubmissionCategory::Application), + Submission::new(submission_name.clone(), SubmissionCategory::Applications), ]); dbg!(&env.auths()); @@ -89,7 +89,7 @@ fn transfer_admin() { }]); let result = contract_client.try_set_submissions(&vec![ &env, - Submission::new(submission_name.clone(), SubmissionCategory::Application), + Submission::new(submission_name.clone(), SubmissionCategory::Applications), ]); assert!(result.is_err()); @@ -105,7 +105,7 @@ fn transfer_admin() { }]); contract_client.set_submissions(&vec![ &env, - Submission::new(submission_name.clone(), SubmissionCategory::Application), + Submission::new(submission_name.clone(), SubmissionCategory::Applications), ]); } diff --git a/contracts/governance/tests/e2e/voting.rs b/contracts/governance/tests/e2e/voting.rs index bafea20..92f6107 100644 --- a/contracts/governance/tests/e2e/voting.rs +++ b/contracts/governance/tests/e2e/voting.rs @@ -31,8 +31,8 @@ fn voting_data_upload() { contract_client.set_submissions(&vec![ &env, - Submission::new(submission1.clone(), SubmissionCategory::Application), - Submission::new(submission2.clone(), SubmissionCategory::Application), + Submission::new(submission1.clone(), SubmissionCategory::Applications), + Submission::new(submission2.clone(), SubmissionCategory::Applications), ]); let mut votes_submission1 = Map::new(&env); @@ -51,8 +51,8 @@ fn voting_data_upload() { contract_client.set_submissions(&vec![ &env, - Submission::new(submission1.clone(), SubmissionCategory::Application), - Submission::new(submission2.clone(), SubmissionCategory::Application), + Submission::new(submission1.clone(), SubmissionCategory::Applications), + Submission::new(submission2.clone(), SubmissionCategory::Applications), ]); let mut neuron_result = Map::new(&env); @@ -123,15 +123,15 @@ fn adding_duplicate_submissions() { contract_client.set_submissions(&vec![ &env, - Submission::new(String::from_str(&env, "a"), SubmissionCategory::Application), - Submission::new(String::from_str(&env, "a"), SubmissionCategory::Application), + Submission::new(String::from_str(&env, "a"), SubmissionCategory::Applications), + Submission::new(String::from_str(&env, "a"), SubmissionCategory::Applications), ]); let submissions = contract_client.get_submissions(); let mut expected = Vec::new(&env); expected.push_back(Submission::new( String::from_str(&env, "a"), - SubmissionCategory::Application, + SubmissionCategory::Applications, )); assert_eq!(submissions, expected); @@ -180,7 +180,7 @@ fn set_bump_round_flow() { // Set votes and results for round 25 contract_client.set_submissions(&vec![ &env, - Submission::new(submission.clone(), SubmissionCategory::Application), + Submission::new(submission.clone(), SubmissionCategory::Applications), ]); let mut votes25 = Map::new(&env); @@ -233,7 +233,7 @@ fn set_bump_round_flow() { let new_submission = String::from_str(&env, "sub2"); contract_client.set_submissions(&vec![ &env, - Submission::new(new_submission.clone(), SubmissionCategory::Application), + Submission::new(new_submission.clone(), SubmissionCategory::Applications), ]); let mut votes26 = Map::new(&env); diff --git a/neurons/src/lib.rs b/neurons/src/lib.rs index 3c82a89..478549b 100644 --- a/neurons/src/lib.rs +++ b/neurons/src/lib.rs @@ -5,7 +5,7 @@ pub mod quorum; #[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] pub enum SubmissionCategory { - Application, + Applications, FinancialProtocols, InfrastructureAndServices, DeveloperTools, diff --git a/neurons/src/quorum.rs b/neurons/src/quorum.rs index 9dc10a1..4f672ac 100644 --- a/neurons/src/quorum.rs +++ b/neurons/src/quorum.rs @@ -10,7 +10,7 @@ const QUORUM_RELATIVE_PARTICIPATION_THRESHOLD: f64 = 2.0 / 3.0; #[non_exhaustive] #[derive(Deserialize, Serialize, Debug, Clone, PartialEq)] pub struct DelegateesForUser { - application: Vec, + applications : Vec, financial_protocols: Vec, infrastructure_and_services: Vec, developer_tools: Vec, @@ -25,7 +25,7 @@ impl DelegateesForUser { developer_tools: Vec, ) -> Self { Self { - application, + applications: application, financial_protocols, infrastructure_and_services, developer_tools, @@ -56,7 +56,7 @@ fn delegatees_for_category<'a>( delegatees_for_user: &'a DelegateesForUser, ) -> &'a Vec { match submission_category { - SubmissionCategory::Application => &delegatees_for_user.application, + SubmissionCategory::Applications => &delegatees_for_user.applications, SubmissionCategory::FinancialProtocols => &delegatees_for_user.financial_protocols, SubmissionCategory::InfrastructureAndServices => { &delegatees_for_user.infrastructure_and_services @@ -194,7 +194,7 @@ mod tests { ); let normalized_votes = normalize_votes_for_submission( - &Submission::new("sub1".to_string(), SubmissionCategory::Application), + &Submission::new("sub1".to_string(), SubmissionCategory::Applications), &submission_votes, &delegates_for_user, ) @@ -244,7 +244,7 @@ mod tests { ); let normalized_votes = normalize_votes_for_submission( - &Submission::new("sub1".to_string(), SubmissionCategory::Application), + &Submission::new("sub1".to_string(), SubmissionCategory::Applications), &submission_votes, &delegates_for_user, ) @@ -297,7 +297,7 @@ mod tests { ); let normalized_votes = normalize_votes_for_submission( - &Submission::new("sub1".to_string(), SubmissionCategory::Application), + &Submission::new("sub1".to_string(), SubmissionCategory::Applications), &submission_votes, &delegates_for_user, ) @@ -350,7 +350,7 @@ mod tests { ); let normalized_votes = normalize_votes_for_submission( - &Submission::new("sub1".to_string(), SubmissionCategory::Application), + &Submission::new("sub1".to_string(), SubmissionCategory::Applications), &submission_votes, &delegates_for_user, ) @@ -386,7 +386,7 @@ mod tests { ); let normalized_votes = normalize_votes_for_submission( - &Submission::new("sub1".to_string(), SubmissionCategory::Application), + &Submission::new("sub1".to_string(), SubmissionCategory::Applications), &submission_votes, &delegates_for_user, ) @@ -425,7 +425,7 @@ mod tests { ); let normalized_votes = normalize_votes_for_submission( - &Submission::new("sub1".to_string(), SubmissionCategory::Application), + &Submission::new("sub1".to_string(), SubmissionCategory::Applications), &submission_votes, &delegates_for_user, ) @@ -486,7 +486,7 @@ mod tests { ); let normalized_votes_for_submission1 = normalize_votes_for_submission( - &Submission::new("sub1".to_string(), SubmissionCategory::Application), + &Submission::new("sub1".to_string(), SubmissionCategory::Applications), &submission1_votes, &delegates_for_user, ) From 35bf47592f9d156e3b20289c3545e8d5ad21f848 Mon Sep 17 00:00:00 2001 From: Artur Michalek Date: Thu, 29 Aug 2024 16:26:33 +0200 Subject: [PATCH 05/16] Lint and format --- contracts/governance/tests/e2e/voting.rs | 10 ++++++++-- neurons/src/quorum.rs | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/contracts/governance/tests/e2e/voting.rs b/contracts/governance/tests/e2e/voting.rs index 92f6107..66c5708 100644 --- a/contracts/governance/tests/e2e/voting.rs +++ b/contracts/governance/tests/e2e/voting.rs @@ -123,8 +123,14 @@ fn adding_duplicate_submissions() { contract_client.set_submissions(&vec![ &env, - Submission::new(String::from_str(&env, "a"), SubmissionCategory::Applications), - Submission::new(String::from_str(&env, "a"), SubmissionCategory::Applications), + Submission::new( + String::from_str(&env, "a"), + SubmissionCategory::Applications, + ), + Submission::new( + String::from_str(&env, "a"), + SubmissionCategory::Applications, + ), ]); let submissions = contract_client.get_submissions(); diff --git a/neurons/src/quorum.rs b/neurons/src/quorum.rs index 4f672ac..a377d0c 100644 --- a/neurons/src/quorum.rs +++ b/neurons/src/quorum.rs @@ -10,7 +10,7 @@ const QUORUM_RELATIVE_PARTICIPATION_THRESHOLD: f64 = 2.0 / 3.0; #[non_exhaustive] #[derive(Deserialize, Serialize, Debug, Clone, PartialEq)] pub struct DelegateesForUser { - applications : Vec, + applications: Vec, financial_protocols: Vec, infrastructure_and_services: Vec, developer_tools: Vec, From 7b9f8fcf93208b415505b9782a66b381e2820432 Mon Sep 17 00:00:00 2001 From: Artur Michalek Date: Thu, 29 Aug 2024 17:17:16 +0200 Subject: [PATCH 06/16] Load submission details as separate step --- neurons/src/main.rs | 8 ++++++-- neurons/src/quorum.rs | 22 ++++++++++++---------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/neurons/src/main.rs b/neurons/src/main.rs index c59eea6..2d54ff0 100644 --- a/neurons/src/main.rs +++ b/neurons/src/main.rs @@ -57,12 +57,16 @@ fn main() { let users: Vec = serde_json::from_str(users_raw.as_str()).unwrap(); let votes_raw = fs::read_to_string("data/votes.json").unwrap(); - let votes: HashMap> = + let votes: HashMap> = serde_json::from_str(votes_raw.as_str()).unwrap(); + + let submissions_raw = fs::read_to_string("data/submissions.json").unwrap(); + let submissions: Vec = serde_json::from_str(submissions_raw.as_str()).unwrap(); + let delegatees_for_user_raw = fs::read_to_string("data/delegatees_for_user.json").unwrap(); let delegatees_for_user: HashMap = serde_json::from_str(delegatees_for_user_raw.as_str()).unwrap(); - let normalized_votes = normalize_votes(votes, &delegatees_for_user).unwrap(); + let normalized_votes = normalize_votes(votes, &submissions, &delegatees_for_user).unwrap(); write_result( "result/normalized_votes.json", &to_sorted_map(normalized_votes), diff --git a/neurons/src/quorum.rs b/neurons/src/quorum.rs index a377d0c..80791e4 100644 --- a/neurons/src/quorum.rs +++ b/neurons/src/quorum.rs @@ -33,20 +33,22 @@ impl DelegateesForUser { } } -#[allow(clippy::implicit_hasher)] +#[allow(clippy::implicit_hasher, clippy::missing_panics_doc)] pub fn normalize_votes( - votes: HashMap>, + votes: HashMap>, + submissions: &[Submission], delegatees_for_user: &HashMap, -) -> Result>> { +) -> Result>> { votes .into_iter() - .map(|(submission, submission_votes)| { - let submission_votes = normalize_votes_for_submission( - &submission, - &submission_votes, - delegatees_for_user, - )?; - Ok((submission, submission_votes)) + .map(|(submission_name, submission_votes)| { + let submission = submissions + .iter() + .find(|sub| sub.name == submission_name) + .expect("Missing details for submission"); + let submission_votes = + normalize_votes_for_submission(submission, &submission_votes, delegatees_for_user)?; + Ok((submission_name, submission_votes)) }) .collect::>() } From e5f21a447e04772268c529fbae3486c2e2b43359 Mon Sep 17 00:00:00 2001 From: Artur Michalek Date: Thu, 29 Aug 2024 17:23:35 +0200 Subject: [PATCH 07/16] Rename `name` to `id` --- neurons/src/lib.rs | 4 ++-- neurons/src/quorum.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/neurons/src/lib.rs b/neurons/src/lib.rs index 478549b..21b390b 100644 --- a/neurons/src/lib.rs +++ b/neurons/src/lib.rs @@ -14,14 +14,14 @@ pub enum SubmissionCategory { #[non_exhaustive] #[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] pub struct Submission { - name: String, + id: String, category: SubmissionCategory, } impl Submission { #[must_use] pub fn new(name: String, category: SubmissionCategory) -> Self { - Self { name, category } + Self { id: name, category } } } diff --git a/neurons/src/quorum.rs b/neurons/src/quorum.rs index 80791e4..788cc5d 100644 --- a/neurons/src/quorum.rs +++ b/neurons/src/quorum.rs @@ -44,7 +44,7 @@ pub fn normalize_votes( .map(|(submission_name, submission_votes)| { let submission = submissions .iter() - .find(|sub| sub.name == submission_name) + .find(|sub| sub.id == submission_name) .expect("Missing details for submission"); let submission_votes = normalize_votes_for_submission(submission, &submission_votes, delegatees_for_user)?; From 3d1442c828ef1d431bb6ac7291abf7e34fe55254 Mon Sep 17 00:00:00 2001 From: Artur Michalek Date: Thu, 29 Aug 2024 17:25:18 +0200 Subject: [PATCH 08/16] Rename fields --- contracts/governance/src/types.rs | 2 +- neurons/src/lib.rs | 2 +- neurons/src/quorum.rs | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/contracts/governance/src/types.rs b/contracts/governance/src/types.rs index c2ccfc7..77373a9 100644 --- a/contracts/governance/src/types.rs +++ b/contracts/governance/src/types.rs @@ -8,7 +8,7 @@ pub enum SubmissionCategory { Applications, FinancialProtocols, InfrastructureAndServices, - DeveloperTools, + DeveloperTooling, } #[contracttype] diff --git a/neurons/src/lib.rs b/neurons/src/lib.rs index 21b390b..f08ce8c 100644 --- a/neurons/src/lib.rs +++ b/neurons/src/lib.rs @@ -8,7 +8,7 @@ pub enum SubmissionCategory { Applications, FinancialProtocols, InfrastructureAndServices, - DeveloperTools, + DeveloperTooling, } #[non_exhaustive] diff --git a/neurons/src/quorum.rs b/neurons/src/quorum.rs index 788cc5d..981d0cc 100644 --- a/neurons/src/quorum.rs +++ b/neurons/src/quorum.rs @@ -13,22 +13,22 @@ pub struct DelegateesForUser { applications: Vec, financial_protocols: Vec, infrastructure_and_services: Vec, - developer_tools: Vec, + developer_tooling: Vec, } impl DelegateesForUser { #[must_use] pub fn new( - application: Vec, + applications: Vec, financial_protocols: Vec, infrastructure_and_services: Vec, - developer_tools: Vec, + developer_tooling: Vec, ) -> Self { Self { - applications: application, + applications, financial_protocols, infrastructure_and_services, - developer_tools, + developer_tooling, } } } @@ -63,7 +63,7 @@ fn delegatees_for_category<'a>( SubmissionCategory::InfrastructureAndServices => { &delegatees_for_user.infrastructure_and_services } - SubmissionCategory::DeveloperTools => &delegatees_for_user.developer_tools, + SubmissionCategory::DeveloperTooling => &delegatees_for_user.developer_tooling, } } From 2d1e99ade6cb670c5c74bed8ac5e2007eb44d88e Mon Sep 17 00:00:00 2001 From: Artur Michalek Date: Fri, 30 Aug 2024 11:52:29 +0200 Subject: [PATCH 09/16] Pass submissions as tuple --- contracts/governance/src/lib.rs | 11 ++++++++--- contracts/governance/tests/e2e/auth.rs | 8 ++++---- contracts/governance/tests/e2e/voting.rs | 16 ++++++++-------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/contracts/governance/src/lib.rs b/contracts/governance/src/lib.rs index 794c923..0a7a304 100644 --- a/contracts/governance/src/lib.rs +++ b/contracts/governance/src/lib.rs @@ -7,7 +7,7 @@ use alloc::string::ToString; use soroban_fixed_point_math::SorobanFixedPoint; use soroban_sdk::{ - contract, contractimpl, contracttype, Address, BytesN, Env, Map, String, Vec, I256, + contract, contractimpl, contracttype, vec, Address, BytesN, Env, Map, String, Vec, I256, }; use admin::{is_set_admin, require_admin}; @@ -24,7 +24,7 @@ use crate::storage::{ write_submissions, write_voting_powers, LayerKeyData, NeuronKeyData, NeuronResultKeyData, SubmissionVotesKeyData, SubmissionsKeyData, VotingPowersKeyData, }; -use crate::types::{Submission, Vote, VotingSystemError, ABSTAIN_VOTING_POWER}; +use crate::types::{Submission, SubmissionCategory, Vote, VotingSystemError, ABSTAIN_VOTING_POWER}; mod admin; mod neural_governance; @@ -95,7 +95,12 @@ impl VotingSystem { } /// Set multiple submissions. - pub fn set_submissions(env: Env, new_submissions: Vec) { + pub fn set_submissions(env: Env, new_submissions_raw: Vec<(String, SubmissionCategory)>) { + let mut new_submissions = vec![&env]; + for (id, category) in new_submissions_raw { + new_submissions.push_back(Submission::new(id, category)); + } + require_admin(&env); let mut submissions = Vec::new(&env); diff --git a/contracts/governance/tests/e2e/auth.rs b/contracts/governance/tests/e2e/auth.rs index b41d454..dd1bbe8 100644 --- a/contracts/governance/tests/e2e/auth.rs +++ b/contracts/governance/tests/e2e/auth.rs @@ -1,5 +1,5 @@ use crate::e2e::common::contract_utils::deploy_contract_without_initialization; -use governance::types::{Submission, SubmissionCategory}; +use governance::types::SubmissionCategory; use soroban_sdk::testutils::{ Address as AddressTrait, AuthorizedFunction, AuthorizedInvocation, MockAuth, MockAuthInvoke, }; @@ -34,7 +34,7 @@ fn auth() { let submission_name = String::from_str(&env, "abc"); contract_client.set_submissions(&vec![ &env, - Submission::new(submission_name.clone(), SubmissionCategory::Applications), + (submission_name.clone(), SubmissionCategory::Applications), ]); dbg!(&env.auths()); @@ -89,7 +89,7 @@ fn transfer_admin() { }]); let result = contract_client.try_set_submissions(&vec![ &env, - Submission::new(submission_name.clone(), SubmissionCategory::Applications), + (submission_name.clone(), SubmissionCategory::Applications), ]); assert!(result.is_err()); @@ -105,7 +105,7 @@ fn transfer_admin() { }]); contract_client.set_submissions(&vec![ &env, - Submission::new(submission_name.clone(), SubmissionCategory::Applications), + (submission_name.clone(), SubmissionCategory::Applications), ]); } diff --git a/contracts/governance/tests/e2e/voting.rs b/contracts/governance/tests/e2e/voting.rs index 66c5708..b2e0f57 100644 --- a/contracts/governance/tests/e2e/voting.rs +++ b/contracts/governance/tests/e2e/voting.rs @@ -31,8 +31,8 @@ fn voting_data_upload() { contract_client.set_submissions(&vec![ &env, - Submission::new(submission1.clone(), SubmissionCategory::Applications), - Submission::new(submission2.clone(), SubmissionCategory::Applications), + (submission1.clone(), SubmissionCategory::Applications), + (submission2.clone(), SubmissionCategory::Applications), ]); let mut votes_submission1 = Map::new(&env); @@ -51,8 +51,8 @@ fn voting_data_upload() { contract_client.set_submissions(&vec![ &env, - Submission::new(submission1.clone(), SubmissionCategory::Applications), - Submission::new(submission2.clone(), SubmissionCategory::Applications), + (submission1.clone(), SubmissionCategory::Applications), + (submission2.clone(), SubmissionCategory::Applications), ]); let mut neuron_result = Map::new(&env); @@ -123,11 +123,11 @@ fn adding_duplicate_submissions() { contract_client.set_submissions(&vec![ &env, - Submission::new( + ( String::from_str(&env, "a"), SubmissionCategory::Applications, ), - Submission::new( + ( String::from_str(&env, "a"), SubmissionCategory::Applications, ), @@ -186,7 +186,7 @@ fn set_bump_round_flow() { // Set votes and results for round 25 contract_client.set_submissions(&vec![ &env, - Submission::new(submission.clone(), SubmissionCategory::Applications), + (submission.clone(), SubmissionCategory::Applications), ]); let mut votes25 = Map::new(&env); @@ -239,7 +239,7 @@ fn set_bump_round_flow() { let new_submission = String::from_str(&env, "sub2"); contract_client.set_submissions(&vec![ &env, - Submission::new(new_submission.clone(), SubmissionCategory::Applications), + (new_submission.clone(), SubmissionCategory::Applications), ]); let mut votes26 = Map::new(&env); From d0aed89ce93ba08d41b6b863862fa6b2d8ac69b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Chali=C5=84ski?= Date: Thu, 14 Nov 2024 02:14:04 +0100 Subject: [PATCH 10/16] cleanup --- contracts/governance/src/lib.rs | 13 +++++++------ contracts/governance/src/storage.rs | 6 +++--- neurons/src/lib.rs | 6 +++--- neurons/src/quorum.rs | 2 +- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/contracts/governance/src/lib.rs b/contracts/governance/src/lib.rs index 0a7a304..62d30fa 100644 --- a/contracts/governance/src/lib.rs +++ b/contracts/governance/src/lib.rs @@ -24,7 +24,7 @@ use crate::storage::{ write_submissions, write_voting_powers, LayerKeyData, NeuronKeyData, NeuronResultKeyData, SubmissionVotesKeyData, SubmissionsKeyData, VotingPowersKeyData, }; -use crate::types::{Submission, SubmissionCategory, Vote, VotingSystemError, ABSTAIN_VOTING_POWER}; +use crate::types::{Vote, VotingSystemError, ABSTAIN_VOTING_POWER}; mod admin; mod neural_governance; @@ -95,10 +95,10 @@ impl VotingSystem { } /// Set multiple submissions. - pub fn set_submissions(env: Env, new_submissions_raw: Vec<(String, SubmissionCategory)>) { + pub fn set_submissions(env: Env, new_submissions_raw: Vec<(String, String)>) { let mut new_submissions = vec![&env]; - for (id, category) in new_submissions_raw { - new_submissions.push_back(Submission::new(id, category)); + for (name, category) in new_submissions_raw { + new_submissions.push_back((name, category)); } require_admin(&env); @@ -116,7 +116,7 @@ impl VotingSystem { } /// Get submissions for the active round. - pub fn get_submissions(env: &Env) -> Vec { + pub fn get_submissions(env: &Env) -> Vec<(String, String)> { read_submissions(env, Self::get_current_round(env)) } @@ -130,11 +130,12 @@ impl VotingSystem { if !read_submissions(env, Self::get_current_round(env)) .iter() - .any(|sub| sub.id == submission_id) + .any(|(name, _category)| name == submission_id) { return Err(VotingSystemError::SubmissionDoesNotExist); } + // this causes timeout god knows why write_submission_votes(env, &submission_id, Self::get_current_round(env), &votes); Ok(()) } diff --git a/contracts/governance/src/storage.rs b/contracts/governance/src/storage.rs index 11f1414..336b0e6 100644 --- a/contracts/governance/src/storage.rs +++ b/contracts/governance/src/storage.rs @@ -5,7 +5,7 @@ use crate::storage::key_data::{ get_layer_key, get_neuron_key, get_neuron_result_key, get_submission_votes_key, get_submissions_key, get_voting_powers_key, }; -use crate::types::{Submission, Vote, VotingSystemError}; +use crate::types::{Vote, VotingSystemError}; use crate::{ContractResult, DataKey}; pub use crate::storage::key_data::{ @@ -101,7 +101,7 @@ pub(crate) fn write_submission_votes( env.storage().persistent().set(&key, votes); } -pub(crate) fn read_submissions(env: &Env, round: u32) -> Vec { +pub(crate) fn read_submissions(env: &Env, round: u32) -> Vec<(String, String)> { let key = get_submissions_key(round); env.storage() .persistent() @@ -109,7 +109,7 @@ pub(crate) fn read_submissions(env: &Env, round: u32) -> Vec { .unwrap_or_else(|| Vec::new(env)) } -pub(crate) fn write_submissions(env: &Env, round: u32, submissions: &Vec) { +pub(crate) fn write_submissions(env: &Env, round: u32, submissions: &Vec<(String, String)>) { let key = get_submissions_key(round); env.storage().persistent().set(&key, submissions); } diff --git a/neurons/src/lib.rs b/neurons/src/lib.rs index f08ce8c..a59ce49 100644 --- a/neurons/src/lib.rs +++ b/neurons/src/lib.rs @@ -14,14 +14,14 @@ pub enum SubmissionCategory { #[non_exhaustive] #[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] pub struct Submission { - id: String, - category: SubmissionCategory, + pub name: String, + pub category: SubmissionCategory, } impl Submission { #[must_use] pub fn new(name: String, category: SubmissionCategory) -> Self { - Self { id: name, category } + Self { name, category } } } diff --git a/neurons/src/quorum.rs b/neurons/src/quorum.rs index 981d0cc..3fab32d 100644 --- a/neurons/src/quorum.rs +++ b/neurons/src/quorum.rs @@ -44,7 +44,7 @@ pub fn normalize_votes( .map(|(submission_name, submission_votes)| { let submission = submissions .iter() - .find(|sub| sub.id == submission_name) + .find(|sub| sub.name == submission_name) .expect("Missing details for submission"); let submission_votes = normalize_votes_for_submission(submission, &submission_votes, delegatees_for_user)?; From 10356b84aa2ba925918f09789b1c89feb0bd3918 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Chali=C5=84ski?= Date: Thu, 14 Nov 2024 02:29:01 +0100 Subject: [PATCH 11/16] run contract offchain as a test --- contracts/governance/src/lib.rs | 1 + contracts/governance/src/offchain.rs | 112 ++++++++++++++++++++ neurons/src/main.rs | 2 +- neurons/src/offchain_data_generator.rs | 137 +++++++++++++++++++++++++ 4 files changed, 251 insertions(+), 1 deletion(-) create mode 100644 contracts/governance/src/offchain.rs create mode 100644 neurons/src/offchain_data_generator.rs diff --git a/contracts/governance/src/lib.rs b/contracts/governance/src/lib.rs index 62d30fa..5b9a721 100644 --- a/contracts/governance/src/lib.rs +++ b/contracts/governance/src/lib.rs @@ -28,6 +28,7 @@ use crate::types::{Vote, VotingSystemError, ABSTAIN_VOTING_POWER}; mod admin; mod neural_governance; +mod offchain; mod storage; pub mod types; diff --git a/contracts/governance/src/offchain.rs b/contracts/governance/src/offchain.rs new file mode 100644 index 0000000..0ed145a --- /dev/null +++ b/contracts/governance/src/offchain.rs @@ -0,0 +1,112 @@ +#[cfg(test)] +mod tests { + use crate::types::Vote; + use crate::{LayerAggregator, VotingSystem, VotingSystemClient}; + use soroban_sdk::log; + use soroban_sdk::testutils::Address as AddressTrait; + use soroban_sdk::{map, vec, Address, Env, Map, String, Vec, I256}; + pub fn deploy_contract_without_initialization(env: &Env) -> VotingSystemClient { + let contract_id = env.register_contract(None, VotingSystem); + let contract_client = VotingSystemClient::new(env, &contract_id); + + contract_client + } + + pub fn deploy_contract(env: &Env) -> VotingSystemClient { + let contract_client = deploy_contract_without_initialization(env); + + env.mock_all_auths(); + let admin = Address::generate(env); + contract_client.initialize(&admin, &30); + + contract_client + } + + #[test] + fn offchain_manual_tally() { + // setup contract + let env = Env::default(); + env.budget().reset_unlimited(); + let contract_client = deploy_contract(&env); + + // setup neural governance + let neurons_sum = vec![ + &env, + ( + String::from_str(&env, "TrustGraph"), + I256::from_i128(&env, 1000000000000000000), + ), + ( + String::from_str(&env, "AssignedReputation"), + I256::from_i128(&env, 1000000000000000000), + ), + ]; + contract_client.add_layer(&neurons_sum, &LayerAggregator::Sum); + + let neurons_product = vec![ + &env, + ( + String::from_str(&env, "PriorVotingHistory"), + I256::from_i128(&env, 1000000000000000000), + ), + ]; + contract_client.add_layer(&neurons_product, &LayerAggregator::Product); + + // migrate submissions + let new_submissions_raw: Vec<(String, String)> = vec![&env]; + contract_client.set_submissions(&new_submissions_raw); + + // upload normalized votes + let normalized_submissions_votes: Map> = map![&env]; + for (submission_id, votes) in normalized_submissions_votes.clone() { + contract_client.set_votes_for_submission(&submission_id, &votes); + } + + // upload trust graph neuron results + let trust_graph_values = vec![&env]; + let mut trust_graph_neuron_map: Map = Map::new(&env); + for (key, value) in trust_graph_values { + trust_graph_neuron_map.set(key, value); + } + + contract_client.set_neuron_result( + &String::from_str(&env, "0"), + &String::from_str(&env, "0"), + &trust_graph_neuron_map, + ); + + // upload trust graph neuron results + let assigned_reputation_values = vec![&env]; + let mut assigned_reputation_neuron_map: Map = Map::new(&env); + for (key, value) in assigned_reputation_values { + assigned_reputation_neuron_map.set(key, value); + } + + contract_client.set_neuron_result( + &String::from_str(&env, "0"), + &String::from_str(&env, "1"), + &assigned_reputation_neuron_map, + ); + + // upload prior voting history neuron results + let prior_voting_history_values = vec![&env]; + let mut prior_voting_history_neuron_map: Map = Map::new(&env); + for (key, value) in prior_voting_history_values { + prior_voting_history_neuron_map.set(key, value); + } + + contract_client.set_neuron_result( + &String::from_str(&env, "1"), + &String::from_str(&env, "0"), + &prior_voting_history_neuron_map, + ); + + contract_client.calculate_voting_powers(); + + // tally + for (submission_id, _votes) in normalized_submissions_votes { + let result = contract_client.tally_submission(&submission_id); + log!(&env, "result", submission_id, result); + } + } +} diff --git a/neurons/src/main.rs b/neurons/src/main.rs index 2d54ff0..19f29e1 100644 --- a/neurons/src/main.rs +++ b/neurons/src/main.rs @@ -8,7 +8,7 @@ use neurons::{Submission, Vote}; use serde::Serialize; use std::collections::{BTreeMap, HashMap}; use std::fs; - +mod offchain_data_generator; pub const DECIMALS: i64 = 1_000_000_000_000_000_000; fn write_result(file_name: &str, data: &T) diff --git a/neurons/src/offchain_data_generator.rs b/neurons/src/offchain_data_generator.rs new file mode 100644 index 0000000..8b6335e --- /dev/null +++ b/neurons/src/offchain_data_generator.rs @@ -0,0 +1,137 @@ +use neurons::Submission; +use serde_json::{Map, Value}; +use std::fs; + +fn normalized_votes() { + println!("-- normalized votes neuron results --"); + let normalized_votes_json = fs::read_to_string("result/normalized_votes.json").unwrap(); + let normalized_votes: Map = + serde_json::from_str(normalized_votes_json.as_str()).unwrap(); + let normalized_votes_mapped: Vec<(String, Vec<(String, String)>)> = normalized_votes + .iter() + .map(|(sumbmission_id, votes)| { + let mapped_votes: Vec<(String, String)> = match votes.as_object() { + Some(votes) => votes + .iter() + .map(|(public_key, vote_str)| { + let vv = match vote_str.as_str().unwrap() { + "Abstain" => "Vote::Abstain", + "Yes" => "Vote::Yes", + "No" => "Vote::No", + _ => "error", + }; + (public_key.to_string(), vv.to_string()) + }) + .collect(), + None => vec![], + }; + (sumbmission_id.to_string(), mapped_votes) + }) + .collect(); + for (submission_id, votes) in normalized_votes_mapped { + let votes_map_string: Vec = votes + .iter() + .map(|(public_key, vote)| { + format!( + "(String::from_str(&env, {:#?}), {})", + public_key.to_string(), + vote + ) + }) + .collect(); + println!( + "(String::from_str(&env, {:?}), map![&env, {:#?}])", + submission_id, votes_map_string + ); + } +} +fn submissions() { + println!("-- submissions --"); + let submissions_raw = fs::read_to_string("data/submissions.json").unwrap(); + let submissions: Vec = serde_json::from_str(submissions_raw.as_str()).unwrap(); + let mapped: Vec<(String, String)> = submissions + .iter() + .map(|s| match s.category { + neurons::SubmissionCategory::Applications => { + (s.name.clone(), "Applications".to_string()) + } + neurons::SubmissionCategory::FinancialProtocols => { + (s.name.clone(), "FinancialProtocols".to_string()) + } + neurons::SubmissionCategory::InfrastructureAndServices => { + (s.name.clone(), "InfrastructureAndServices".to_string()) + } + neurons::SubmissionCategory::DeveloperTooling => { + (s.name.clone(), "DeveloperTooling".to_string()) + } + }) + .collect(); + for (name, category) in mapped { + println!( + "(String::from_str(&env,{:?}), String::from_str(&env,{:?})),", + name, category + ); + } +} +fn trust() { + println!("-- trust graph neuron results --"); + let trust_graph_neuron_raw = fs::read_to_string("result/trust_graph_neuron.json").unwrap(); + let trust_graph_neuron: Map = + serde_json::from_str(trust_graph_neuron_raw.as_str()).unwrap(); + let trust_graph_mapped: Vec<(String, i128)> = trust_graph_neuron + .iter() + .map(|(public_key, value)| { + let n = value.as_str().unwrap().to_string(); + let int = n.parse::().unwrap(); + (public_key.to_string(), int) + }) + .collect(); + for (public_key, value) in trust_graph_mapped { + println!( + "(String::from_str(&env,{:?}), I256::from_i128(&env,{})),", + public_key, value + ); + } +} +fn reputation() { + println!("-- assigned reputation neuron results --"); + let assigned_reputation_neuron_raw = + fs::read_to_string("result/assigned_reputation_neuron.json").unwrap(); + let assigned_reputation_neuron: Map = + serde_json::from_str(assigned_reputation_neuron_raw.as_str()).unwrap(); + let assigned_reputation_neuron_mapped: Vec<(String, i128)> = assigned_reputation_neuron + .iter() + .map(|(public_key, value)| { + let n = value.as_str().unwrap().to_string(); + let int = n.parse::().unwrap(); + (public_key.to_string(), int) + }) + .collect(); + for (public_key, value) in assigned_reputation_neuron_mapped { + println!( + "(String::from_str(&env,{:?}), I256::from_i128(&env,{})),", + public_key, value + ); + } +} +fn voting_history() { + println!("-- prior voting history neuron results --"); + let prior_voting_history_neuron_raw = + fs::read_to_string("result/prior_voting_history_neuron.json").unwrap(); + let prior_voting_history_neuron: Map = + serde_json::from_str(prior_voting_history_neuron_raw.as_str()).unwrap(); + let prior_voting_history_neuron_mapped: Vec<(String, i128)> = prior_voting_history_neuron + .iter() + .map(|(public_key, value)| { + let n = value.as_str().unwrap().to_string(); + let int = n.parse::().unwrap(); + (public_key.to_string(), int) + }) + .collect(); + for (public_key, value) in prior_voting_history_neuron_mapped { + println!( + "(String::from_str(&env,{:?}), I256::from_i128(&env,{})),", + public_key, value + ); + } +} \ No newline at end of file From 795408a04f3df5a9d8a1e58aa0a31114c5eb7d1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Chali=C5=84ski?= Date: Wed, 20 Nov 2024 11:36:19 +0100 Subject: [PATCH 12/16] format --- neurons/src/offchain_data_generator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neurons/src/offchain_data_generator.rs b/neurons/src/offchain_data_generator.rs index 8b6335e..f90d0cd 100644 --- a/neurons/src/offchain_data_generator.rs +++ b/neurons/src/offchain_data_generator.rs @@ -134,4 +134,4 @@ fn voting_history() { public_key, value ); } -} \ No newline at end of file +} From d0798e8ddd54a8c44edd4a05a6cc7286badf9c62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Chali=C5=84ski?= Date: Wed, 20 Nov 2024 11:48:58 +0100 Subject: [PATCH 13/16] lint fix --- contracts/governance/Cargo.toml | 2 +- contracts/governance/src/offchain.rs | 6 +++--- contracts/governance/tests/e2e/voting.rs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/contracts/governance/Cargo.toml b/contracts/governance/Cargo.toml index b1abd1e..7962428 100644 --- a/contracts/governance/Cargo.toml +++ b/contracts/governance/Cargo.toml @@ -10,7 +10,7 @@ crate-type = ["cdylib", "lib"] soroban-sdk = { workspace = true, features = ["alloc"] } soroban-fixed-point-math.workspace = true -[dev_dependencies] +[dev-dependencies] soroban-sdk = { workspace = true, features = ["testutils", "alloc"] } mocks = { path = "mocks" } diff --git a/contracts/governance/src/offchain.rs b/contracts/governance/src/offchain.rs index 0ed145a..6f55be8 100644 --- a/contracts/governance/src/offchain.rs +++ b/contracts/governance/src/offchain.rs @@ -34,11 +34,11 @@ mod tests { &env, ( String::from_str(&env, "TrustGraph"), - I256::from_i128(&env, 1000000000000000000), + I256::from_i128(&env, 1_000_000_000_000_000_000), ), ( String::from_str(&env, "AssignedReputation"), - I256::from_i128(&env, 1000000000000000000), + I256::from_i128(&env, 1_000_000_000_000_000_000), ), ]; contract_client.add_layer(&neurons_sum, &LayerAggregator::Sum); @@ -47,7 +47,7 @@ mod tests { &env, ( String::from_str(&env, "PriorVotingHistory"), - I256::from_i128(&env, 1000000000000000000), + I256::from_i128(&env, 1_000_000_000_000_000_000), ), ]; contract_client.add_layer(&neurons_product, &LayerAggregator::Product); diff --git a/contracts/governance/tests/e2e/voting.rs b/contracts/governance/tests/e2e/voting.rs index a932d6a..315f38d 100644 --- a/contracts/governance/tests/e2e/voting.rs +++ b/contracts/governance/tests/e2e/voting.rs @@ -1,6 +1,6 @@ use soroban_sdk::{vec, Env, Map, String, Vec, I256}; -use governance::types::{Submission, SubmissionCategory, Vote, VotingSystemError}; +use governance::types::{Vote, VotingSystemError}; use governance::{LayerAggregator, DECIMALS}; use crate::e2e::common::contract_utils::deploy_contract; From d698efa618d2a4ff1be9b3099e6337dee350bd58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Chali=C5=84ski?= Date: Wed, 20 Nov 2024 13:08:16 +0100 Subject: [PATCH 14/16] separate project for offchain --- contracts/governance/src/lib.rs | 1 - contracts/governance/src/offchain.rs | 112 -- neurons/src/main.rs | 1 - offchain/Cargo.lock | 1555 +++++++++++++++++ offchain/Cargo.toml | 11 + .../src/data_generator.rs | 0 offchain/src/main.rs | 8 + offchain/src/offchain.rs | 107 ++ 8 files changed, 1681 insertions(+), 114 deletions(-) delete mode 100644 contracts/governance/src/offchain.rs create mode 100644 offchain/Cargo.lock create mode 100644 offchain/Cargo.toml rename neurons/src/offchain_data_generator.rs => offchain/src/data_generator.rs (100%) create mode 100644 offchain/src/main.rs create mode 100644 offchain/src/offchain.rs diff --git a/contracts/governance/src/lib.rs b/contracts/governance/src/lib.rs index 5b9a721..62d30fa 100644 --- a/contracts/governance/src/lib.rs +++ b/contracts/governance/src/lib.rs @@ -28,7 +28,6 @@ use crate::types::{Vote, VotingSystemError, ABSTAIN_VOTING_POWER}; mod admin; mod neural_governance; -mod offchain; mod storage; pub mod types; diff --git a/contracts/governance/src/offchain.rs b/contracts/governance/src/offchain.rs deleted file mode 100644 index 6f55be8..0000000 --- a/contracts/governance/src/offchain.rs +++ /dev/null @@ -1,112 +0,0 @@ -#[cfg(test)] -mod tests { - use crate::types::Vote; - use crate::{LayerAggregator, VotingSystem, VotingSystemClient}; - use soroban_sdk::log; - use soroban_sdk::testutils::Address as AddressTrait; - use soroban_sdk::{map, vec, Address, Env, Map, String, Vec, I256}; - pub fn deploy_contract_without_initialization(env: &Env) -> VotingSystemClient { - let contract_id = env.register_contract(None, VotingSystem); - let contract_client = VotingSystemClient::new(env, &contract_id); - - contract_client - } - - pub fn deploy_contract(env: &Env) -> VotingSystemClient { - let contract_client = deploy_contract_without_initialization(env); - - env.mock_all_auths(); - let admin = Address::generate(env); - contract_client.initialize(&admin, &30); - - contract_client - } - - #[test] - fn offchain_manual_tally() { - // setup contract - let env = Env::default(); - env.budget().reset_unlimited(); - let contract_client = deploy_contract(&env); - - // setup neural governance - let neurons_sum = vec![ - &env, - ( - String::from_str(&env, "TrustGraph"), - I256::from_i128(&env, 1_000_000_000_000_000_000), - ), - ( - String::from_str(&env, "AssignedReputation"), - I256::from_i128(&env, 1_000_000_000_000_000_000), - ), - ]; - contract_client.add_layer(&neurons_sum, &LayerAggregator::Sum); - - let neurons_product = vec![ - &env, - ( - String::from_str(&env, "PriorVotingHistory"), - I256::from_i128(&env, 1_000_000_000_000_000_000), - ), - ]; - contract_client.add_layer(&neurons_product, &LayerAggregator::Product); - - // migrate submissions - let new_submissions_raw: Vec<(String, String)> = vec![&env]; - contract_client.set_submissions(&new_submissions_raw); - - // upload normalized votes - let normalized_submissions_votes: Map> = map![&env]; - for (submission_id, votes) in normalized_submissions_votes.clone() { - contract_client.set_votes_for_submission(&submission_id, &votes); - } - - // upload trust graph neuron results - let trust_graph_values = vec![&env]; - let mut trust_graph_neuron_map: Map = Map::new(&env); - for (key, value) in trust_graph_values { - trust_graph_neuron_map.set(key, value); - } - - contract_client.set_neuron_result( - &String::from_str(&env, "0"), - &String::from_str(&env, "0"), - &trust_graph_neuron_map, - ); - - // upload trust graph neuron results - let assigned_reputation_values = vec![&env]; - let mut assigned_reputation_neuron_map: Map = Map::new(&env); - for (key, value) in assigned_reputation_values { - assigned_reputation_neuron_map.set(key, value); - } - - contract_client.set_neuron_result( - &String::from_str(&env, "0"), - &String::from_str(&env, "1"), - &assigned_reputation_neuron_map, - ); - - // upload prior voting history neuron results - let prior_voting_history_values = vec![&env]; - let mut prior_voting_history_neuron_map: Map = Map::new(&env); - for (key, value) in prior_voting_history_values { - prior_voting_history_neuron_map.set(key, value); - } - - contract_client.set_neuron_result( - &String::from_str(&env, "1"), - &String::from_str(&env, "0"), - &prior_voting_history_neuron_map, - ); - - contract_client.calculate_voting_powers(); - - // tally - for (submission_id, _votes) in normalized_submissions_votes { - let result = contract_client.tally_submission(&submission_id); - log!(&env, "result", submission_id, result); - } - } -} diff --git a/neurons/src/main.rs b/neurons/src/main.rs index 19f29e1..4efe5fb 100644 --- a/neurons/src/main.rs +++ b/neurons/src/main.rs @@ -8,7 +8,6 @@ use neurons::{Submission, Vote}; use serde::Serialize; use std::collections::{BTreeMap, HashMap}; use std::fs; -mod offchain_data_generator; pub const DECIMALS: i64 = 1_000_000_000_000_000_000; fn write_result(file_name: &str, data: &T) diff --git a/offchain/Cargo.lock b/offchain/Cargo.lock new file mode 100644 index 0000000..90b9e66 --- /dev/null +++ b/offchain/Cargo.lock @@ -0,0 +1,1555 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "addr2line" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" + +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "anyhow" +version = "1.0.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c95c10ba0b00a02636238b814946408b1322d5ac4760326e6fb8ec956d85775" + +[[package]] +name = "arbitrary" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d5a26814d8dcb93b0e5a0ff3c6d80a8843bafb21b39e8e18a6f05471870e110" +dependencies = [ + "derive_arbitrary", +] + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "backtrace" +version = "0.3.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets", +] + +[[package]] +name = "base16ct" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c7f02d4ea65f2c1853089ffd8d2787bdbc63de2f0d29dedbcf8ccdfa0ccd4cf" + +[[package]] +name = "base32" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23ce669cd6c8588f79e15cf450314f9638f967fc5770ff1c7c1deb0925ea7cfa" + +[[package]] +name = "base64" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "base64ct" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "bytes-lit" +version = "0.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0adabf37211a5276e46335feabcbb1530c95eb3fdf85f324c7db942770aa025d" +dependencies = [ + "num-bigint", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "camino" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b96ec4966b5813e2c0507c1f86115c8c5abaadc3980879c3424042a02fd1ad3" + +[[package]] +name = "cc" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd9de9f2205d5ef3fd67e685b0df337994ddd4495e2a28d185500d0e1edfea47" +dependencies = [ + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "chrono" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "num-traits", + "serde", + "windows-targets", +] + +[[package]] +name = "const-oid" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "cpufeatures" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ca741a962e1b0bff6d724a1a0958b686406e853bb14061f218562e1896f95e6" +dependencies = [ + "libc", +] + +[[package]] +name = "crate-git-revision" +version = "0.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c521bf1f43d31ed2f73441775ed31935d77901cb3451e44b38a1c1612fcbaf98" +dependencies = [ + "serde", + "serde_derive", + "serde_json", +] + +[[package]] +name = "crypto-bigint" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" +dependencies = [ + "generic-array", + "rand_core", + "subtle", + "zeroize", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "ctor" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edb49164822f3ee45b17acd4a208cfc1251410cf0cad9a833234c9890774dd9f" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "curve25519-dalek" +version = "4.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97fb8b7c4503de7d6ae7b42ab72a5a59857b4c937ec27a3d4539dba95b5ab2be" +dependencies = [ + "cfg-if", + "cpufeatures", + "curve25519-dalek-derive", + "digest", + "fiat-crypto", + "rustc_version", + "subtle", + "zeroize", +] + +[[package]] +name = "curve25519-dalek-derive" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "darling" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" +dependencies = [ + "darling_core", + "quote", + "syn", +] + +[[package]] +name = "der" +version = "0.7.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f55bf8e7b65898637379c1b74eb1551107c8294ed26d855ceb9fd1a09cfc9bc0" +dependencies = [ + "const-oid", + "zeroize", +] + +[[package]] +name = "deranged" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" +dependencies = [ + "powerfmt", + "serde", +] + +[[package]] +name = "derive_arbitrary" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67e77553c4162a157adbf834ebae5b415acbecbeafc7a74b0e886657506a7611" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "const-oid", + "crypto-common", + "subtle", +] + +[[package]] +name = "downcast-rs" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" + +[[package]] +name = "ecdsa" +version = "0.16.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee27f32b5c5292967d2d4a9d7f1e0b0aed2c15daded5a60300e4abb9d8020bca" +dependencies = [ + "der", + "digest", + "elliptic-curve", + "rfc6979", + "signature", +] + +[[package]] +name = "ed25519" +version = "2.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53" +dependencies = [ + "pkcs8", + "signature", +] + +[[package]] +name = "ed25519-dalek" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a3daa8e81a3963a60642bcc1f90a670680bd4a77535faa384e9d1c79d620871" +dependencies = [ + "curve25519-dalek", + "ed25519", + "rand_core", + "serde", + "sha2", + "subtle", + "zeroize", +] + +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + +[[package]] +name = "elliptic-curve" +version = "0.13.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6043086bf7973472e0c7dff2142ea0b680d30e18d9cc40f267efbf222bd47" +dependencies = [ + "base16ct", + "crypto-bigint", + "digest", + "ff", + "generic-array", + "group", + "rand_core", + "sec1", + "subtle", + "zeroize", +] + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "escape-bytes" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bfcf67fea2815c2fc3b90873fae90957be12ff417335dfadc7f52927feb03b2" + +[[package]] +name = "ethnum" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b90ca2580b73ab6a1f724b76ca11ab632df820fd6040c336200d2c1df7b3c82c" + +[[package]] +name = "ff" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ded41244b729663b1e574f1b4fb731469f69f79c17667b5d776b16cda0479449" +dependencies = [ + "rand_core", + "subtle", +] + +[[package]] +name = "fiat-crypto" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28dea519a9695b9977216879a3ebfddf92f1c08c05d984f8996aecd6ecdc811d" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", + "zeroize", +] + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "wasi", + "wasm-bindgen", +] + +[[package]] +name = "gimli" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" + +[[package]] +name = "governance" +version = "0.1.0" +dependencies = [ + "soroban-fixed-point-math", + "soroban-sdk", +] + +[[package]] +name = "group" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0f9ef7462f7c099f518d754361858f86d8a07af53ba9af0fe635bbccb151a63" +dependencies = [ + "ff", + "rand_core", + "subtle", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "hashbrown" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a9bfc1af68b1726ea47d3d5109de126281def866b33970e10fbab11b5dafab3" + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +dependencies = [ + "serde", +] + +[[package]] +name = "hex-literal" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + +[[package]] +name = "iana-time-zone" +version = "0.1.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown 0.12.3", + "serde", +] + +[[package]] +name = "indexmap" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" +dependencies = [ + "equivalent", + "hashbrown 0.15.1", + "serde", +] + +[[package]] +name = "indexmap-nostd" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e04e2fd2b8188ea827b32ef11de88377086d690286ab35747ef7f9bf3ccb590" + +[[package]] +name = "itertools" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a73e9fe3c49d7afb2ace819fa181a287ce54a0983eda4e0eb05c22f82ffe534" + +[[package]] +name = "js-sys" +version = "0.3.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "k256" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6e3919bbaa2945715f0bb6d3934a173d1e9a59ac23767fbaaef277265a7411b" +dependencies = [ + "cfg-if", + "ecdsa", + "elliptic-curve", + "sha2", +] + +[[package]] +name = "keccak" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc2af9a1119c51f12a14607e783cb977bde58bc069ff0c3da1095e635d70654" +dependencies = [ + "cpufeatures", +] + +[[package]] +name = "libc" +version = "0.2.164" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f" + +[[package]] +name = "libm" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8355be11b20d696c8f18f6cc018c4e372165b1fa8126cef092399c9951984ffa" + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "miniz_oxide" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" +dependencies = [ + "adler2", +] + +[[package]] +name = "neurons" +version = "0.1.0" +dependencies = [ + "anyhow", + "camino", + "serde", + "serde_json", + "serde_repr", +] + +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + +[[package]] +name = "num-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "object" +version = "0.36.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e" +dependencies = [ + "memchr", +] + +[[package]] +name = "offchain" +version = "0.1.0" +dependencies = [ + "governance", + "neurons", + "serde_json", + "soroban-fixed-point-math", + "soroban-sdk", +] + +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + +[[package]] +name = "p256" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9863ad85fa8f4460f9c48cb909d38a0d689dba1f6f6988a5e3e0d31071bcd4b" +dependencies = [ + "ecdsa", + "elliptic-curve", + "primeorder", + "sha2", +] + +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "pkcs8" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" +dependencies = [ + "der", + "spki", +] + +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "prettyplease" +version = "0.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64d1ec885c64d0457d564db4ec299b2dae3f9c02808b8ad9c3a089c591b18033" +dependencies = [ + "proc-macro2", + "syn", +] + +[[package]] +name = "primeorder" +version = "0.13.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "353e1ca18966c16d9deb1c69278edbc5f194139612772bd9537af60ac231e1e6" +dependencies = [ + "elliptic-curve", +] + +[[package]] +name = "proc-macro2" +version = "1.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rfc6979" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dd2a808d456c4a54e300a23e9f5a67e122c3024119acbfd73e3bf664491cb2" +dependencies = [ + "hmac", + "subtle", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "sec1" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3e97a565f76233a6003f9f5c54be1d9c5bdfa3eccfb189469f11ec4901c47dc" +dependencies = [ + "base16ct", + "der", + "generic-array", + "subtle", + "zeroize", +] + +[[package]] +name = "semver" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" + +[[package]] +name = "serde" +version = "1.0.215" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.215" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.133" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "serde_repr" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_with" +version = "3.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e28bdad6db2b8340e449f7108f020b3b092e8583a9e3fb82713e1d4e71fe817" +dependencies = [ + "base64 0.22.1", + "chrono", + "hex", + "indexmap 1.9.3", + "indexmap 2.6.0", + "serde", + "serde_derive", + "serde_json", + "serde_with_macros", + "time", +] + +[[package]] +name = "serde_with_macros" +version = "3.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d846214a9854ef724f3da161b426242d8de7c1fc7de2f89bb1efcb154dca79d" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "sha3" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60" +dependencies = [ + "digest", + "keccak", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "signature" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" +dependencies = [ + "digest", + "rand_core", +] + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + +[[package]] +name = "soroban-builtin-sdk-macros" +version = "21.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f57a68ef8777e28e274de0f3a88ad9a5a41d9a2eb461b4dd800b086f0e83b80" +dependencies = [ + "itertools", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "soroban-env-common" +version = "21.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fd1c89463835fe6da996318156d39f424b4f167c725ec692e5a7a2d4e694b3d" +dependencies = [ + "arbitrary", + "crate-git-revision", + "ethnum", + "num-derive", + "num-traits", + "serde", + "soroban-env-macros", + "soroban-wasmi", + "static_assertions", + "stellar-xdr", + "wasmparser", +] + +[[package]] +name = "soroban-env-guest" +version = "21.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bfb2536811045d5cd0c656a324cbe9ce4467eb734c7946b74410d90dea5d0ce" +dependencies = [ + "soroban-env-common", + "static_assertions", +] + +[[package]] +name = "soroban-env-host" +version = "21.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b7a32c28f281c423189f1298960194f0e0fc4eeb72378028171e556d8cd6160" +dependencies = [ + "backtrace", + "curve25519-dalek", + "ecdsa", + "ed25519-dalek", + "elliptic-curve", + "generic-array", + "getrandom", + "hex-literal", + "hmac", + "k256", + "num-derive", + "num-integer", + "num-traits", + "p256", + "rand", + "rand_chacha", + "sec1", + "sha2", + "sha3", + "soroban-builtin-sdk-macros", + "soroban-env-common", + "soroban-wasmi", + "static_assertions", + "stellar-strkey", + "wasmparser", +] + +[[package]] +name = "soroban-env-macros" +version = "21.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "242926fe5e0d922f12d3796cd7cd02dd824e5ef1caa088f45fce20b618309f64" +dependencies = [ + "itertools", + "proc-macro2", + "quote", + "serde", + "serde_json", + "stellar-xdr", + "syn", +] + +[[package]] +name = "soroban-fixed-point-math" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9d386a1ca0a148121b21331f9da68f33bf3dfb6de69646f719935d2dec3d49c" +dependencies = [ + "soroban-sdk", +] + +[[package]] +name = "soroban-ledger-snapshot" +version = "21.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6edf92749fd8399b417192d301c11f710b9cdce15789a3d157785ea971576fa" +dependencies = [ + "serde", + "serde_json", + "serde_with", + "soroban-env-common", + "soroban-env-host", + "thiserror", +] + +[[package]] +name = "soroban-sdk" +version = "21.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dcdf04484af7cc731a7a48ad1d9f5f940370edeea84734434ceaf398a6b862e" +dependencies = [ + "arbitrary", + "bytes-lit", + "ctor", + "derive_arbitrary", + "ed25519-dalek", + "rand", + "rustc_version", + "serde", + "serde_json", + "soroban-env-guest", + "soroban-env-host", + "soroban-ledger-snapshot", + "soroban-sdk-macros", + "stellar-strkey", +] + +[[package]] +name = "soroban-sdk-macros" +version = "21.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0974e413731aeff2443f2305b344578b3f1ffd18335a7ba0f0b5d2eb4e94c9ce" +dependencies = [ + "crate-git-revision", + "darling", + "itertools", + "proc-macro2", + "quote", + "rustc_version", + "sha2", + "soroban-env-common", + "soroban-spec", + "soroban-spec-rust", + "stellar-xdr", + "syn", +] + +[[package]] +name = "soroban-spec" +version = "21.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2c70b20e68cae3ef700b8fa3ae29db1c6a294b311fba66918f90cb8f9fd0a1a" +dependencies = [ + "base64 0.13.1", + "stellar-xdr", + "thiserror", + "wasmparser", +] + +[[package]] +name = "soroban-spec-rust" +version = "21.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2dafbde981b141b191c6c036abc86097070ddd6eaaa33b273701449501e43d3" +dependencies = [ + "prettyplease", + "proc-macro2", + "quote", + "sha2", + "soroban-spec", + "stellar-xdr", + "syn", + "thiserror", +] + +[[package]] +name = "soroban-wasmi" +version = "0.31.1-soroban.20.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "710403de32d0e0c35375518cb995d4fc056d0d48966f2e56ea471b8cb8fc9719" +dependencies = [ + "smallvec", + "spin", + "wasmi_arena", + "wasmi_core", + "wasmparser-nostd", +] + +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" + +[[package]] +name = "spki" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" +dependencies = [ + "base64ct", + "der", +] + +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + +[[package]] +name = "stellar-strkey" +version = "0.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12d2bf45e114117ea91d820a846fd1afbe3ba7d717988fee094ce8227a3bf8bd" +dependencies = [ + "base32", + "crate-git-revision", + "thiserror", +] + +[[package]] +name = "stellar-xdr" +version = "21.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2675a71212ed39a806e415b0dbf4702879ff288ec7f5ee996dda42a135512b50" +dependencies = [ + "arbitrary", + "base64 0.13.1", + "crate-git-revision", + "escape-bytes", + "hex", + "serde", + "serde_with", + "stellar-strkey", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "2.0.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "time" +version = "0.3.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" +dependencies = [ + "deranged", + "itoa", + "num-conv", + "powerfmt", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" + +[[package]] +name = "time-macros" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" +dependencies = [ + "num-conv", + "time-core", +] + +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]] +name = "unicode-ident" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "wasm-bindgen" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e" +dependencies = [ + "cfg-if", + "once_cell", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" + +[[package]] +name = "wasmi_arena" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "104a7f73be44570cac297b3035d76b169d6599637631cf37a1703326a0727073" + +[[package]] +name = "wasmi_core" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcf1a7db34bff95b85c261002720c00c3a6168256dcb93041d3fa2054d19856a" +dependencies = [ + "downcast-rs", + "libm", + "num-traits", + "paste", +] + +[[package]] +name = "wasmparser" +version = "0.116.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a58e28b80dd8340cb07b8242ae654756161f6fc8d0038123d679b7b99964fa50" +dependencies = [ + "indexmap 2.6.0", + "semver", +] + +[[package]] +name = "wasmparser-nostd" +version = "0.100.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5a015fe95f3504a94bb1462c717aae75253e39b9dd6c3fb1062c934535c64aa" +dependencies = [ + "indexmap-nostd", +] + +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zeroize" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" diff --git a/offchain/Cargo.toml b/offchain/Cargo.toml new file mode 100644 index 0000000..6e1e261 --- /dev/null +++ b/offchain/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "offchain" +version = "0.1.0" +edition = "2021" + +[dependencies] +serde_json = "1.0.114" +soroban-sdk = { version = "21.0.1-preview.3", features = ["testutils", "alloc"] } +soroban-fixed-point-math = "1.1.1" +governance = { path = "../contracts/governance" } +neurons = { path = "../neurons"} \ No newline at end of file diff --git a/neurons/src/offchain_data_generator.rs b/offchain/src/data_generator.rs similarity index 100% rename from neurons/src/offchain_data_generator.rs rename to offchain/src/data_generator.rs diff --git a/offchain/src/main.rs b/offchain/src/main.rs new file mode 100644 index 0000000..9bf75fe --- /dev/null +++ b/offchain/src/main.rs @@ -0,0 +1,8 @@ +mod data_generator; +mod offchain; +fn main() { + // prepare all data + // pass it to contract + // tally + println!("offline tally"); +} diff --git a/offchain/src/offchain.rs b/offchain/src/offchain.rs new file mode 100644 index 0000000..89475ab --- /dev/null +++ b/offchain/src/offchain.rs @@ -0,0 +1,107 @@ +use governance::{LayerAggregator, VotingSystem, VotingSystemClient, types::Vote}; +use soroban_sdk::log; +use soroban_sdk::testutils::Address as AddressTrait; +use soroban_sdk::{map, vec, Address, Env, Map, String, Vec, I256}; +pub fn deploy_contract_without_initialization(env: &Env) -> VotingSystemClient { + let contract_id = env.register_contract(None, VotingSystem); + let contract_client = VotingSystemClient::new(env, &contract_id); + + contract_client +} + +pub fn deploy_contract(env: &Env) -> VotingSystemClient { + let contract_client = deploy_contract_without_initialization(env); + + env.mock_all_auths(); + let admin = Address::generate(env); + contract_client.initialize(&admin, &30); + + contract_client +} + +fn offchain_manual_tally() { + // setup contract + let env = Env::default(); + env.budget().reset_unlimited(); + let contract_client = deploy_contract(&env); + + // setup neural governance + let neurons_sum = vec![ + &env, + ( + String::from_str(&env, "TrustGraph"), + I256::from_i128(&env, 1_000_000_000_000_000_000), + ), + ( + String::from_str(&env, "AssignedReputation"), + I256::from_i128(&env, 1_000_000_000_000_000_000), + ), + ]; + contract_client.add_layer(&neurons_sum, &LayerAggregator::Sum); + + let neurons_product = vec![ + &env, + ( + String::from_str(&env, "PriorVotingHistory"), + I256::from_i128(&env, 1_000_000_000_000_000_000), + ), + ]; + contract_client.add_layer(&neurons_product, &LayerAggregator::Product); + + // migrate submissions + let new_submissions_raw: Vec<(String, String)> = vec![&env]; + contract_client.set_submissions(&new_submissions_raw); + + // upload normalized votes + let normalized_submissions_votes: Map> = map![&env]; + for (submission_id, votes) in normalized_submissions_votes.clone() { + contract_client.set_votes_for_submission(&submission_id, &votes); + } + + // upload trust graph neuron results + let trust_graph_values = vec![&env]; + let mut trust_graph_neuron_map: Map = Map::new(&env); + for (key, value) in trust_graph_values { + trust_graph_neuron_map.set(key, value); + } + + contract_client.set_neuron_result( + &String::from_str(&env, "0"), + &String::from_str(&env, "0"), + &trust_graph_neuron_map, + ); + + // upload trust graph neuron results + let assigned_reputation_values = vec![&env]; + let mut assigned_reputation_neuron_map: Map = Map::new(&env); + for (key, value) in assigned_reputation_values { + assigned_reputation_neuron_map.set(key, value); + } + + contract_client.set_neuron_result( + &String::from_str(&env, "0"), + &String::from_str(&env, "1"), + &assigned_reputation_neuron_map, + ); + + // upload prior voting history neuron results + let prior_voting_history_values = vec![&env]; + let mut prior_voting_history_neuron_map: Map = Map::new(&env); + for (key, value) in prior_voting_history_values { + prior_voting_history_neuron_map.set(key, value); + } + + contract_client.set_neuron_result( + &String::from_str(&env, "1"), + &String::from_str(&env, "0"), + &prior_voting_history_neuron_map, + ); + + contract_client.calculate_voting_powers(); + + // tally + for (submission_id, _votes) in normalized_submissions_votes { + let result = contract_client.tally_submission(&submission_id); + log!(&env, "result", submission_id, result); + } +} From 26725375b78e33addddaaf76040085036904a9bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Chali=C5=84ski?= Date: Wed, 20 Nov 2024 15:00:49 +0100 Subject: [PATCH 15/16] automate data generation and voting tallying --- offchain/src/data_generator.rs | 227 +++++++++++++++------------------ offchain/src/main.rs | 27 +++- offchain/src/offchain.rs | 90 ++++++------- 3 files changed, 165 insertions(+), 179 deletions(-) diff --git a/offchain/src/data_generator.rs b/offchain/src/data_generator.rs index f90d0cd..6978e53 100644 --- a/offchain/src/data_generator.rs +++ b/offchain/src/data_generator.rs @@ -1,137 +1,114 @@ +use governance::types::Vote; use neurons::Submission; use serde_json::{Map, Value}; +use soroban_sdk::{ + map, vec, Env, Map as SorobanMap, String as SorobanString, Vec as SorobanVec, I256, +}; use std::fs; -fn normalized_votes() { - println!("-- normalized votes neuron results --"); - let normalized_votes_json = fs::read_to_string("result/normalized_votes.json").unwrap(); - let normalized_votes: Map = - serde_json::from_str(normalized_votes_json.as_str()).unwrap(); - let normalized_votes_mapped: Vec<(String, Vec<(String, String)>)> = normalized_votes - .iter() - .map(|(sumbmission_id, votes)| { - let mapped_votes: Vec<(String, String)> = match votes.as_object() { - Some(votes) => votes - .iter() - .map(|(public_key, vote_str)| { - let vv = match vote_str.as_str().unwrap() { - "Abstain" => "Vote::Abstain", - "Yes" => "Vote::Yes", - "No" => "Vote::No", - _ => "error", - }; - (public_key.to_string(), vv.to_string()) - }) - .collect(), - None => vec![], - }; - (sumbmission_id.to_string(), mapped_votes) - }) - .collect(); - for (submission_id, votes) in normalized_votes_mapped { - let votes_map_string: Vec = votes - .iter() - .map(|(public_key, vote)| { - format!( - "(String::from_str(&env, {:#?}), {})", - public_key.to_string(), - vote - ) - }) - .collect(); - println!( - "(String::from_str(&env, {:?}), map![&env, {:#?}])", - submission_id, votes_map_string - ); +fn vote_from_str(s: &str) -> Vote { + match s { + "Abstain" => Vote::Abstain, + "Yes" => Vote::Yes, + "No" => Vote::No, + _ => panic!("invalid vote"), } } -fn submissions() { - println!("-- submissions --"); - let submissions_raw = fs::read_to_string("data/submissions.json").unwrap(); +fn parse_i256(env: &Env, value: &Value) -> I256 { + // TODO fix this parsing, now there is possible data loss + I256::from_i128( + &env, + value.as_str().unwrap().to_string().parse::().unwrap(), + ) +} +pub fn submissions(env: &Env) -> SorobanVec<(SorobanString, SorobanString)> { + let submissions_raw = fs::read_to_string("../neurons/data/submissions.json").unwrap(); let submissions: Vec = serde_json::from_str(submissions_raw.as_str()).unwrap(); - let mapped: Vec<(String, String)> = submissions - .iter() - .map(|s| match s.category { - neurons::SubmissionCategory::Applications => { - (s.name.clone(), "Applications".to_string()) - } - neurons::SubmissionCategory::FinancialProtocols => { - (s.name.clone(), "FinancialProtocols".to_string()) - } - neurons::SubmissionCategory::InfrastructureAndServices => { - (s.name.clone(), "InfrastructureAndServices".to_string()) - } - neurons::SubmissionCategory::DeveloperTooling => { - (s.name.clone(), "DeveloperTooling".to_string()) - } - }) - .collect(); - for (name, category) in mapped { - println!( - "(String::from_str(&env,{:?}), String::from_str(&env,{:?})),", - name, category - ); - } + let mut submissions_soroban: SorobanVec<(SorobanString, SorobanString)> = vec![&env]; + submissions.iter().for_each(|s| { + submissions_soroban.push_back(match s.category { + neurons::SubmissionCategory::Applications => ( + SorobanString::from_str(&env, &s.name), + SorobanString::from_str(&env, "Applications"), + ), + neurons::SubmissionCategory::FinancialProtocols => ( + SorobanString::from_str(&env, &s.name), + SorobanString::from_str(&env, "FinancialProtocols"), + ), + neurons::SubmissionCategory::InfrastructureAndServices => ( + SorobanString::from_str(&env, &s.name), + SorobanString::from_str(&env, "InfrastructureAndServices"), + ), + neurons::SubmissionCategory::DeveloperTooling => ( + SorobanString::from_str(&env, &s.name), + SorobanString::from_str(&env, "DeveloperTooling"), + ), + }); + }); + submissions_soroban } -fn trust() { - println!("-- trust graph neuron results --"); - let trust_graph_neuron_raw = fs::read_to_string("result/trust_graph_neuron.json").unwrap(); - let trust_graph_neuron: Map = - serde_json::from_str(trust_graph_neuron_raw.as_str()).unwrap(); - let trust_graph_mapped: Vec<(String, i128)> = trust_graph_neuron +pub fn normalized_votes(env: &Env) -> SorobanMap> { + let normalized_votes_raw = + fs::read_to_string("../neurons/result/normalized_votes.json").unwrap(); + let normalized_votes_serde: Map = + serde_json::from_str(normalized_votes_raw.as_str()).unwrap(); + let mut normalized_votes_soroban: SorobanMap> = + map![&env]; + normalized_votes_serde .iter() - .map(|(public_key, value)| { - let n = value.as_str().unwrap().to_string(); - let int = n.parse::().unwrap(); - (public_key.to_string(), int) - }) - .collect(); - for (public_key, value) in trust_graph_mapped { - println!( - "(String::from_str(&env,{:?}), I256::from_i128(&env,{})),", - public_key, value - ); - } + .for_each(|(sumbmission_id, votes)| { + let mut mapped_votes: SorobanMap = map![&env]; + votes + .as_object() + .unwrap() + .iter() + .for_each(|(public_key, vote_str)| { + let vote = vote_from_str(vote_str.as_str().unwrap()); + mapped_votes.set(SorobanString::from_str(&env, public_key), vote); + }); + normalized_votes_soroban + .set(SorobanString::from_str(&env, sumbmission_id), mapped_votes); + }); + normalized_votes_soroban } -fn reputation() { - println!("-- assigned reputation neuron results --"); - let assigned_reputation_neuron_raw = - fs::read_to_string("result/assigned_reputation_neuron.json").unwrap(); - let assigned_reputation_neuron: Map = - serde_json::from_str(assigned_reputation_neuron_raw.as_str()).unwrap(); - let assigned_reputation_neuron_mapped: Vec<(String, i128)> = assigned_reputation_neuron - .iter() - .map(|(public_key, value)| { - let n = value.as_str().unwrap().to_string(); - let int = n.parse::().unwrap(); - (public_key.to_string(), int) - }) - .collect(); - for (public_key, value) in assigned_reputation_neuron_mapped { - println!( - "(String::from_str(&env,{:?}), I256::from_i128(&env,{})),", - public_key, value - ); - } +pub fn trust(env: &Env) -> SorobanMap { + let trust_raw = fs::read_to_string("../neurons/result/trust_graph_neuron.json").unwrap(); + let trust_serde: Map = serde_json::from_str(trust_raw.as_str()).unwrap(); + let mut trust_soroban: SorobanMap = map![&env]; + trust_serde.iter().for_each(|(public_key, value)| { + trust_soroban.set( + SorobanString::from_str(&env, public_key), + parse_i256(&env, value), + ) + }); + trust_soroban } -fn voting_history() { - println!("-- prior voting history neuron results --"); - let prior_voting_history_neuron_raw = - fs::read_to_string("result/prior_voting_history_neuron.json").unwrap(); - let prior_voting_history_neuron: Map = - serde_json::from_str(prior_voting_history_neuron_raw.as_str()).unwrap(); - let prior_voting_history_neuron_mapped: Vec<(String, i128)> = prior_voting_history_neuron - .iter() - .map(|(public_key, value)| { - let n = value.as_str().unwrap().to_string(); - let int = n.parse::().unwrap(); - (public_key.to_string(), int) - }) - .collect(); - for (public_key, value) in prior_voting_history_neuron_mapped { - println!( - "(String::from_str(&env,{:?}), I256::from_i128(&env,{})),", - public_key, value - ); - } +pub fn reputation(env: &Env) -> SorobanMap { + let reputation_raw = + fs::read_to_string("../neurons/result/assigned_reputation_neuron.json").unwrap(); + let reputation_serde: Map = + serde_json::from_str(reputation_raw.as_str()).unwrap(); + let mut reputation_soroban: SorobanMap = map![&env]; + reputation_serde.iter().for_each(|(public_key, value)| { + reputation_soroban.set( + SorobanString::from_str(&env, public_key), + parse_i256(&env, value), + ) + }); + + reputation_soroban +} +pub fn voting_history(env: &Env) -> SorobanMap { + let voting_history_raw = + fs::read_to_string("../neurons/result/prior_voting_history_neuron.json").unwrap(); + let voting_history_serde: Map = + serde_json::from_str(voting_history_raw.as_str()).unwrap(); + let mut voting_history_soroban: SorobanMap = map![&env]; + voting_history_serde.iter().for_each(|(public_key, value)| { + voting_history_soroban.set( + SorobanString::from_str(&env, public_key), + parse_i256(&env, value), + ) + }); + voting_history_soroban } diff --git a/offchain/src/main.rs b/offchain/src/main.rs index 9bf75fe..d7301e0 100644 --- a/offchain/src/main.rs +++ b/offchain/src/main.rs @@ -1,8 +1,27 @@ +use data_generator::{normalized_votes, reputation, submissions, trust, voting_history}; +use governance::types::Vote; +use offchain::manual_tally; +use soroban_sdk::{Env, Map as SorobanMap, String as SorobanString, Vec as SorobanVec, I256}; mod data_generator; mod offchain; + fn main() { - // prepare all data - // pass it to contract - // tally - println!("offline tally"); + let env = Env::default(); + env.budget().reset_unlimited(); + + let submissions: SorobanVec<(SorobanString, SorobanString)> = submissions(&env); + let normalized_votes: SorobanMap> = + normalized_votes(&env); + let trust_graph_neuron_result: SorobanMap = trust(&env); + let assigned_reputation_neuron_result: SorobanMap = reputation(&env); + let prior_voting_history_neuron_result: SorobanMap = voting_history(&env); + + manual_tally( + &env, + submissions, + normalized_votes, + trust_graph_neuron_result, + assigned_reputation_neuron_result, + prior_voting_history_neuron_result, + ); } diff --git a/offchain/src/offchain.rs b/offchain/src/offchain.rs index 89475ab..0deb4d2 100644 --- a/offchain/src/offchain.rs +++ b/offchain/src/offchain.rs @@ -1,7 +1,10 @@ -use governance::{LayerAggregator, VotingSystem, VotingSystemClient, types::Vote}; +use governance::{types::Vote, LayerAggregator, VotingSystem, VotingSystemClient}; use soroban_sdk::log; use soroban_sdk::testutils::Address as AddressTrait; -use soroban_sdk::{map, vec, Address, Env, Map, String, Vec, I256}; +use soroban_sdk::{ + vec, Address, Env, Map as SorobanMap, String as SorobanString, Vec as SorobanVec, I256, +}; + pub fn deploy_contract_without_initialization(env: &Env) -> VotingSystemClient { let contract_id = env.register_contract(None, VotingSystem); let contract_client = VotingSystemClient::new(env, &contract_id); @@ -14,26 +17,20 @@ pub fn deploy_contract(env: &Env) -> VotingSystemClient { env.mock_all_auths(); let admin = Address::generate(env); - contract_client.initialize(&admin, &30); + contract_client.initialize(&admin, &31); contract_client } -fn offchain_manual_tally() { - // setup contract - let env = Env::default(); - env.budget().reset_unlimited(); - let contract_client = deploy_contract(&env); - - // setup neural governance +pub fn setup_layers(env: &Env, contract_client: &VotingSystemClient<'_>) { let neurons_sum = vec![ &env, ( - String::from_str(&env, "TrustGraph"), + SorobanString::from_str(&env, "TrustGraph"), I256::from_i128(&env, 1_000_000_000_000_000_000), ), ( - String::from_str(&env, "AssignedReputation"), + SorobanString::from_str(&env, "AssignedReputation"), I256::from_i128(&env, 1_000_000_000_000_000_000), ), ]; @@ -42,65 +39,58 @@ fn offchain_manual_tally() { let neurons_product = vec![ &env, ( - String::from_str(&env, "PriorVotingHistory"), + SorobanString::from_str(&env, "PriorVotingHistory"), I256::from_i128(&env, 1_000_000_000_000_000_000), ), ]; contract_client.add_layer(&neurons_product, &LayerAggregator::Product); +} - // migrate submissions - let new_submissions_raw: Vec<(String, String)> = vec![&env]; - contract_client.set_submissions(&new_submissions_raw); +pub fn manual_tally( + env: &Env, + submissions: SorobanVec<(SorobanString, SorobanString)>, + normalized_votes: SorobanMap>, + trust_graph_neuron_result: SorobanMap, + assigned_reputation_neuron_result: SorobanMap, + prior_voting_history_neuron_result: SorobanMap, +) { + // setup contract + let contract_client: VotingSystemClient<'_> = deploy_contract(&env); + setup_layers(&env, &contract_client); - // upload normalized votes - let normalized_submissions_votes: Map> = map![&env]; - for (submission_id, votes) in normalized_submissions_votes.clone() { - contract_client.set_votes_for_submission(&submission_id, &votes); - } + // set submissions + contract_client.set_submissions(&submissions); - // upload trust graph neuron results - let trust_graph_values = vec![&env]; - let mut trust_graph_neuron_map: Map = Map::new(&env); - for (key, value) in trust_graph_values { - trust_graph_neuron_map.set(key, value); + // set normalized votes for each submission + for (submission_id, votes) in normalized_votes.clone() { + contract_client.set_votes_for_submission(&submission_id, &votes); } + // set trust graph neuron results contract_client.set_neuron_result( - &String::from_str(&env, "0"), - &String::from_str(&env, "0"), - &trust_graph_neuron_map, + &SorobanString::from_str(&env, "0"), + &SorobanString::from_str(&env, "0"), + &trust_graph_neuron_result, ); - // upload trust graph neuron results - let assigned_reputation_values = vec![&env]; - let mut assigned_reputation_neuron_map: Map = Map::new(&env); - for (key, value) in assigned_reputation_values { - assigned_reputation_neuron_map.set(key, value); - } - + // set assigned reputation neuron results contract_client.set_neuron_result( - &String::from_str(&env, "0"), - &String::from_str(&env, "1"), - &assigned_reputation_neuron_map, + &SorobanString::from_str(&env, "0"), + &SorobanString::from_str(&env, "1"), + &assigned_reputation_neuron_result, ); - // upload prior voting history neuron results - let prior_voting_history_values = vec![&env]; - let mut prior_voting_history_neuron_map: Map = Map::new(&env); - for (key, value) in prior_voting_history_values { - prior_voting_history_neuron_map.set(key, value); - } - + // set prior voting history neuron results contract_client.set_neuron_result( - &String::from_str(&env, "1"), - &String::from_str(&env, "0"), - &prior_voting_history_neuron_map, + &SorobanString::from_str(&env, "1"), + &SorobanString::from_str(&env, "0"), + &prior_voting_history_neuron_result, ); contract_client.calculate_voting_powers(); // tally - for (submission_id, _votes) in normalized_submissions_votes { + for (submission_id, _votes) in normalized_votes { let result = contract_client.tally_submission(&submission_id); log!(&env, "result", submission_id, result); } From 7929eb047f62ec63163b3f88b99391d4060b9ad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Chali=C5=84ski?= Date: Fri, 22 Nov 2024 00:15:59 +0100 Subject: [PATCH 16/16] save voting result to file --- offchain/src/data_generator.rs | 15 +++++++-------- offchain/src/offchain.rs | 27 +++++++++++++++++++-------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/offchain/src/data_generator.rs b/offchain/src/data_generator.rs index 6978e53..79ead4b 100644 --- a/offchain/src/data_generator.rs +++ b/offchain/src/data_generator.rs @@ -14,8 +14,7 @@ fn vote_from_str(s: &str) -> Vote { _ => panic!("invalid vote"), } } -fn parse_i256(env: &Env, value: &Value) -> I256 { - // TODO fix this parsing, now there is possible data loss +fn i256_from_json_value(env: &Env, value: &Value) -> I256 { I256::from_i128( &env, value.as_str().unwrap().to_string().parse::().unwrap(), @@ -78,8 +77,8 @@ pub fn trust(env: &Env) -> SorobanMap { trust_serde.iter().for_each(|(public_key, value)| { trust_soroban.set( SorobanString::from_str(&env, public_key), - parse_i256(&env, value), - ) + i256_from_json_value(&env, value), + ); }); trust_soroban } @@ -92,8 +91,8 @@ pub fn reputation(env: &Env) -> SorobanMap { reputation_serde.iter().for_each(|(public_key, value)| { reputation_soroban.set( SorobanString::from_str(&env, public_key), - parse_i256(&env, value), - ) + i256_from_json_value(&env, value), + ); }); reputation_soroban @@ -107,8 +106,8 @@ pub fn voting_history(env: &Env) -> SorobanMap { voting_history_serde.iter().for_each(|(public_key, value)| { voting_history_soroban.set( SorobanString::from_str(&env, public_key), - parse_i256(&env, value), - ) + i256_from_json_value(&env, value), + ); }); voting_history_soroban } diff --git a/offchain/src/offchain.rs b/offchain/src/offchain.rs index 0deb4d2..b7d1a0e 100644 --- a/offchain/src/offchain.rs +++ b/offchain/src/offchain.rs @@ -1,9 +1,13 @@ +use std::fs; + use governance::{types::Vote, LayerAggregator, VotingSystem, VotingSystemClient}; -use soroban_sdk::log; +use serde_json::{json, Value}; use soroban_sdk::testutils::Address as AddressTrait; use soroban_sdk::{ - vec, Address, Env, Map as SorobanMap, String as SorobanString, Vec as SorobanVec, I256, + vec as SorobanVecMacro, Address, Env, Map as SorobanMap, String as SorobanString, + Vec as SorobanVec, I256, }; +const ROUND: &'_ u32 = &31; pub fn deploy_contract_without_initialization(env: &Env) -> VotingSystemClient { let contract_id = env.register_contract(None, VotingSystem); @@ -17,13 +21,13 @@ pub fn deploy_contract(env: &Env) -> VotingSystemClient { env.mock_all_auths(); let admin = Address::generate(env); - contract_client.initialize(&admin, &31); + contract_client.initialize(&admin, &ROUND); contract_client } pub fn setup_layers(env: &Env, contract_client: &VotingSystemClient<'_>) { - let neurons_sum = vec![ + let neurons_sum = SorobanVecMacro![ &env, ( SorobanString::from_str(&env, "TrustGraph"), @@ -36,7 +40,7 @@ pub fn setup_layers(env: &Env, contract_client: &VotingSystemClient<'_>) { ]; contract_client.add_layer(&neurons_sum, &LayerAggregator::Sum); - let neurons_product = vec![ + let neurons_product = SorobanVecMacro![ &env, ( SorobanString::from_str(&env, "PriorVotingHistory"), @@ -89,9 +93,16 @@ pub fn manual_tally( contract_client.calculate_voting_powers(); - // tally + // tally & write result to file + let mut results: Vec = vec![]; for (submission_id, _votes) in normalized_votes { - let result = contract_client.tally_submission(&submission_id); - log!(&env, "result", submission_id, result); + let submission_id_string = submission_id.to_string(); + let result: i128 = match contract_client.tally_submission(&submission_id).to_i128() { + Some(result) => result, + None => panic!("i256 result of [{submission_id_string}] overflow i128"), + }; + results.push(json!({"submission": submission_id_string, "result": result.to_string()})); } + let serialized = serde_json::to_string(&results).unwrap(); + fs::write(format!("voting_result.json"), serialized).unwrap(); }