Skip to content

Commit

Permalink
add members_len field (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
sczembor authored Nov 2, 2023
1 parent 1102f52 commit 59e81ad
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 63 deletions.
4 changes: 4 additions & 0 deletions congress/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ Change log entries are to be added to the Unreleased section. Example entry:

### Features

- `members_len` view method

### Breaking changes

- added `members_len` field to smart contract

### Bug Fixes

## v1.0.0 (2023-11-01)
Expand Down
14 changes: 12 additions & 2 deletions congress/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub struct Contract {
// We can use single object rather than LookupMap because the maximum amount of members
// is 17 (for HoM: 15 + 2)
pub members: LazyOption<(Vec<AccountId>, Vec<PropPerm>)>,
/// length of members
pub members_len: u8,
/// minimum amount of members to approve the proposal
pub threshold: u8,

Expand Down Expand Up @@ -83,15 +85,17 @@ impl Contract {
) -> Self {
// we can support up to 255 with the limitation of the proposal type, but setting 100
// here because this is more than enough for what we need to test for Congress.
near_sdk::require!(members.len() <= 100, "max amount of members is 100");
let threshold = (members.len() / 2) as u8 + 1;
let members_len = members.len() as u8;
near_sdk::require!(members_len <= 100, "max amount of members is 100");
let threshold = (members_len / 2) + 1;
members.sort();
Self {
community_fund,
dissolved: false,
prop_counter: 0,
proposals: LookupMap::new(StorageKey::Proposals),
members: LazyOption::new(StorageKey::Members, Some(&(members, member_perms))),
members_len,
threshold,
hook_auth: LazyOption::new(StorageKey::HookAuth, Some(&hook_auth)),
start_time,
Expand Down Expand Up @@ -1381,4 +1385,10 @@ mod unit_tests {
prop = ctr.get_proposal(id);
assert_eq!(prop.unwrap().proposal.status, ProposalStatus::Approved);
}

#[test]
fn members_len() {
let (_, ctr, _) = setup_ctr(100);
assert_eq!(ctr.members_len(), 4);
}
}
64 changes: 9 additions & 55 deletions congress/src/migrate.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
use crate::*;
use near_sdk::serde::{Deserialize, Serialize};

#[near_bindgen]
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize)]
#[serde(crate = "near_sdk::serde")]
pub struct OldProposal {
pub proposer: AccountId,
pub description: String,
pub kind: PropKind,
pub status: ProposalStatus,
pub approve: u8,
pub reject: u8,
pub abstain: u8,
pub votes: HashMap<AccountId, Vote>,
pub submission_time: u64,
pub approved_at: Option<u64>,
}

#[derive(BorshDeserialize, BorshSerialize)]
pub struct OldState {
pub community_fund: AccountId,
pub registry: AccountId,
pub dissolved: bool,
pub prop_counter: u32,
pub proposals: LookupMap<u32, OldProposal>,
pub proposals: LookupMap<u32, Proposal>,
pub members: LazyOption<(Vec<AccountId>, Vec<PropPerm>)>,
pub threshold: u8,
pub hook_auth: LazyOption<HashMap<AccountId, Vec<HookPerm>>>,
pub start_time: u64,
pub end_time: u64,
pub cooldown: u64,
pub voting_duration: u64,
pub min_voting_duration: u64,
pub budget_spent: Balance,
pub budget_cap: Balance,
pub big_funding_threshold: Balance,
Expand All @@ -41,58 +24,29 @@ impl Contract {
#[private]
#[init(ignore_state)]
/* pub */
pub fn migrate(min_voting_duration: u64) -> Self {
let mut old_state: OldState = env::state_read().expect("failed");
pub fn migrate() -> Self {
let old_state: OldState = env::state_read().expect("failed");
// new field in the smart contract :
// + min_voting_duration: u64,
// + members_len: u8,
// new field field in proposal
// votes HashMap<AccountId, Vote> -> HashMap<AccountId, VoteRecord>, where
// pub struct VoteRecord {
// pub timestamp: u64,
// pub vote: Vote,
// }

for id in 1..=old_state.prop_counter {
if let Some(proposal) = old_state.proposals.get(&id) {
let new_votes = proposal
.votes
.into_iter()
.map(|(a, vote)| (a, VoteRecord { timestamp: 0, vote }))
.collect();
old_state.proposals.insert_raw(
&id.try_to_vec().unwrap(),
&Proposal {
proposer: proposal.proposer,
description: proposal.description,
kind: proposal.kind,
status: proposal.status,
approve: proposal.approve,
reject: proposal.reject,
abstain: proposal.abstain,
votes: new_votes,
submission_time: proposal.submission_time,
approved_at: proposal.approved_at,
}
.try_to_vec()
.unwrap(),
);
}
}
let members_len = old_state.members.get().unwrap().0.len() as u8;

Self {
community_fund: old_state.community_fund,
registry: old_state.registry,
dissolved: old_state.dissolved,
prop_counter: old_state.prop_counter,
proposals: LookupMap::new(StorageKey::Proposals),
proposals: old_state.proposals,
members: old_state.members,
members_len,
threshold: old_state.threshold,
hook_auth: old_state.hook_auth,
start_time: old_state.start_time,
end_time: old_state.end_time,
cooldown: old_state.cooldown,
voting_duration: old_state.voting_duration,
min_voting_duration,
min_voting_duration: old_state.min_voting_duration,
budget_spent: old_state.budget_spent,
budget_cap: old_state.budget_cap,
big_funding_threshold: old_state.big_funding_threshold,
Expand Down
4 changes: 4 additions & 0 deletions congress/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ impl Contract {
self.dissolved
}

pub fn members_len(&self) -> u8 {
self.members_len
}

/// Returns all members with permissions
pub fn get_members(&self) -> MembersOutput {
let (members, permissions) = self.members.get().unwrap();
Expand Down
23 changes: 17 additions & 6 deletions congress/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,13 @@ async fn migration_mainnet() -> anyhow::Result<()> {
.await?
.json()?;

let members: MembersOutput = congress
.call("get_members")
.max_gas()
.transact()
.await?
.json()?;

// deploy the new contract
let new_congress = congress
.as_account()
Expand All @@ -595,12 +602,7 @@ async fn migration_mainnet() -> anyhow::Result<()> {
.into_result()?;

// call the migrate method
let res = new_congress
.call("migrate")
.args_json(json!({"min_voting_duration": 0}))
.max_gas()
.transact()
.await?;
let res = new_congress.call("migrate").max_gas().transact().await?;
assert!(res.is_success(), "{:?}", res.receipt_failures());

let res: u64 = new_congress
Expand All @@ -619,6 +621,15 @@ async fn migration_mainnet() -> anyhow::Result<()> {
.await?
.json()?;

let members_len: u8 = new_congress
.call("members_len")
.max_gas()
.transact()
.await?
.json()?;

assert_eq!(members.members.len() as u8, members_len);

print!("{:?}", prop.unwrap().proposal);

Ok(())
Expand Down

0 comments on commit 59e81ad

Please sign in to comment.