Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
fix(MultiSig)!: change interface and substitute adaptive_address for …
Browse files Browse the repository at this point in the history
…autonomy (#384)

* doc: fix gql_api.sh and update graphql_api.md

* fix(multi-sig): change interface and substitute adaptive_address for autonomy

* chore: clippy

* chore: remove redundant binding
  • Loading branch information
LycrusHamster authored Aug 4, 2020
1 parent 43da977 commit a58831e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 47 deletions.
37 changes: 12 additions & 25 deletions built-in-services/multi-signature/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ use crate::types::{
const MAX_MULTI_SIGNATURE_RECURSION_DEPTH: u8 = 8;
const MAX_PERMISSION_ACCOUNTS: u8 = 16;

lazy_static::lazy_static! {
// FIXME:
pub static ref ADEPTIVE_ADDRESS: Address = "muta14e0lmgck835vm2dfm0w3ckv6svmez8fdgdl705".parse().unwrap();
}

macro_rules! impl_multisig {
($self: expr, $method: ident, $ctx: expr) => {{
let res = $self.$method($ctx.clone());
Expand Down Expand Up @@ -119,15 +114,9 @@ impl<SDK: ServiceSDK> MultiSignatureService<SDK> {
})
.collect::<Vec<_>>();

let owner = if payload.owner == ADEPTIVE_ADDRESS.clone() {
address.clone()
} else {
payload.owner.clone()
};

let permission = MultiSigPermission {
accounts,
owner,
owner: payload.owner,
threshold: payload.threshold,
memo: payload.memo,
};
Expand Down Expand Up @@ -194,7 +183,7 @@ impl<SDK: ServiceSDK> MultiSignatureService<SDK> {
})
.collect::<Vec<_>>();

let owner = if payload.owner == ADEPTIVE_ADDRESS.clone() {
let owner = if payload.autonomy {
address.clone()
} else {
payload.owner.clone()
Expand Down Expand Up @@ -285,10 +274,8 @@ impl<SDK: ServiceSDK> MultiSignatureService<SDK> {
return ServiceResponse::<()>::from_error(118, "invalid owner".to_owned());
}

let new_info = payload.new_account_info.clone();

// check if account contains itself
if new_info
if payload
.addr_with_weight
.iter()
.map(|a| a.address.clone())
Expand All @@ -301,31 +288,31 @@ impl<SDK: ServiceSDK> MultiSignatureService<SDK> {
}

// check sum of weight
if new_info.addr_with_weight.is_empty()
|| new_info.addr_with_weight.len() > MAX_PERMISSION_ACCOUNTS as usize
if payload.addr_with_weight.is_empty()
|| payload.addr_with_weight.len() > MAX_PERMISSION_ACCOUNTS as usize
{
return ServiceResponse::<()>::from_error(
110,
"accounts length must be [1,16]".to_owned(),
);
}

let weight_sum = new_info
let weight_sum = payload
.addr_with_weight
.iter()
.map(|item| item.weight as u32)
.sum::<u32>();

// check if sum of the weights is above threshold
if new_info.threshold == 0 || weight_sum < new_info.threshold {
if payload.threshold == 0 || weight_sum < payload.threshold {
return ServiceResponse::<()>::from_error(
111,
"accounts weight or threshold not valid".to_owned(),
);
}

// check the recursion depth
if new_info
if payload
.addr_with_weight
.iter()
.map(|s| self._is_recursion_depth_overflow(&s.address, 0))
Expand All @@ -337,7 +324,7 @@ impl<SDK: ServiceSDK> MultiSignatureService<SDK> {
);
}

let accounts = new_info
let accounts = payload
.addr_with_weight
.iter()
.map(|item| Account {
Expand All @@ -354,9 +341,9 @@ impl<SDK: ServiceSDK> MultiSignatureService<SDK> {
self.sdk
.set_account_value(&payload.account_address, 0u8, MultiSigPermission {
accounts,
owner: new_info.owner,
threshold: new_info.threshold,
memo: new_info.memo,
owner: payload.owner,
threshold: payload.threshold,
memo: payload.memo,
});
return ServiceResponse::<()>::from_succeed(());
}
Expand Down
32 changes: 18 additions & 14 deletions built-in-services/multi-signature/src/tests/curd_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::types::{
MultiSigPermission, RemoveAccountPayload, SetAccountWeightPayload, SetThresholdPayload,
UpdateAccountPayload,
};
use crate::ADEPTIVE_ADDRESS;

use super::*;

Expand All @@ -26,6 +25,7 @@ fn test_generate_multi_signature() {
let multi_sig_address =
service.generate_account(context.clone(), GenerateMultiSigAccountPayload {
owner: owner.clone(),
autonomy: false,
addr_with_weight: accounts,
threshold: 12,
memo: String::new(),
Expand All @@ -40,6 +40,7 @@ fn test_generate_multi_signature() {
let multi_sig_address =
service.generate_account(context.clone(), GenerateMultiSigAccountPayload {
owner: owner.clone(),
autonomy: false,
addr_with_weight: accounts,
threshold: 12,
memo: String::new(),
Expand All @@ -54,6 +55,7 @@ fn test_generate_multi_signature() {
let multi_sig_address =
service.generate_account(context.clone(), GenerateMultiSigAccountPayload {
owner: owner.clone(),
autonomy: false,
addr_with_weight: accounts.clone(),
threshold: 3,
memo: String::new(),
Expand Down Expand Up @@ -89,6 +91,7 @@ fn test_set_threshold() {
let multi_sig_address = service
.generate_account(context.clone(), GenerateMultiSigAccountPayload {
owner: owner_address,
autonomy: false,
addr_with_weight: account_pubkeys,
threshold: 3,
memo: String::new(),
Expand Down Expand Up @@ -128,7 +131,8 @@ fn test_adeption_address() {
.collect::<Vec<_>>();
let multi_sig_address = service
.generate_account(context.clone(), GenerateMultiSigAccountPayload {
owner: ADEPTIVE_ADDRESS.clone(),
owner: Address::default(),
autonomy: true,
addr_with_weight: account_pubkeys,
threshold: 3,
memo: String::new(),
Expand Down Expand Up @@ -157,6 +161,7 @@ fn test_add_account() {
let multi_sig_address = service
.generate_account(context.clone(), GenerateMultiSigAccountPayload {
owner: owner_address.clone(),
autonomy: false,
addr_with_weight: account_pubkeys.clone(),
threshold: 3,
memo: String::new(),
Expand Down Expand Up @@ -210,6 +215,7 @@ fn test_update_account() {
let multi_sig_address = service
.generate_account(context, GenerateMultiSigAccountPayload {
owner: owner_address.clone(),
autonomy: false,
addr_with_weight: account_pubkeys,
threshold: 4,
memo: String::new(),
Expand All @@ -226,12 +232,10 @@ fn test_update_account() {
}];
let res = service.update_account(context.clone(), UpdateAccountPayload {
account_address: multi_sig_address.clone(),
new_account_info: GenerateMultiSigAccountPayload {
owner: new_owner_address.clone(),
addr_with_weight: account_pubkeys,
threshold: 1,
memo: String::new(),
},
owner: new_owner_address.clone(),
addr_with_weight: account_pubkeys,
threshold: 1,
memo: String::new(),
});
assert!(res.is_error());

Expand All @@ -242,12 +246,10 @@ fn test_update_account() {
.collect::<Vec<_>>();
let res = service.update_account(context, UpdateAccountPayload {
account_address: multi_sig_address,
new_account_info: GenerateMultiSigAccountPayload {
owner: new_owner_address,
addr_with_weight: account_pubkeys,
threshold: 1,
memo: String::new(),
},
owner: new_owner_address,
addr_with_weight: account_pubkeys,
threshold: 1,
memo: String::new(),
});
assert_eq!(res.is_error(), false);
}
Expand All @@ -267,6 +269,7 @@ fn test_set_weight() {
let multi_sig_address = service
.generate_account(context.clone(), GenerateMultiSigAccountPayload {
owner: owner_address.clone(),
autonomy: false,
addr_with_weight: account_pubkeys.clone(),
threshold: 4,
memo: String::new(),
Expand Down Expand Up @@ -321,6 +324,7 @@ fn test_remove_account() {
let multi_sig_address = service
.generate_account(context.clone(), GenerateMultiSigAccountPayload {
owner: owner_address.clone(),
autonomy: false,
addr_with_weight: account_pubkeys.clone(),
threshold: 3,
memo: String::new(),
Expand Down
5 changes: 5 additions & 0 deletions built-in-services/multi-signature/src/tests/recursion_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn test_recursion_verify_signature() {
mock_context(cycles_limit, caller.clone()),
GenerateMultiSigAccountPayload {
owner: owner.clone(),
autonomy: false,
addr_with_weight: init_multi_sig_account,
threshold: 4,
memo: String::new(),
Expand All @@ -46,6 +47,7 @@ fn test_recursion_verify_signature() {
mock_context(cycles_limit, caller.clone()),
GenerateMultiSigAccountPayload {
owner,
autonomy: false,
addr_with_weight: multi_sig_account,
threshold: 4,
memo: String::new(),
Expand Down Expand Up @@ -100,6 +102,7 @@ fn test_recursion_depth() {
mock_context(cycles_limit, caller.clone()),
GenerateMultiSigAccountPayload {
owner: owner.clone(),
autonomy: false,
addr_with_weight: init_multi_sig_account,
threshold: 4,
memo: String::new(),
Expand All @@ -125,6 +128,7 @@ fn test_recursion_depth() {
mock_context(cycles_limit, caller.clone()),
GenerateMultiSigAccountPayload {
owner: owner.clone(),
autonomy: false,
addr_with_weight: multi_sig_account,
threshold: 4,
memo: String::new(),
Expand All @@ -139,6 +143,7 @@ fn test_recursion_depth() {
mock_context(cycles_limit, caller),
GenerateMultiSigAccountPayload {
owner,
autonomy: false,
addr_with_weight: vec![AddressWithWeight {
address: sender,
weight: 4u8,
Expand Down
6 changes: 5 additions & 1 deletion built-in-services/multi-signature/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct InitGenesisPayload {
#[derive(RlpFixedCodec, Deserialize, Serialize, Clone, Debug, PartialEq, Eq)]
pub struct GenerateMultiSigAccountPayload {
pub owner: Address,
pub autonomy: bool,
pub addr_with_weight: Vec<AddressWithWeight>,
pub threshold: u32,
pub memo: String,
Expand Down Expand Up @@ -101,7 +102,10 @@ pub struct SetThresholdPayload {
#[derive(RlpFixedCodec, Deserialize, Serialize, Clone, Debug)]
pub struct UpdateAccountPayload {
pub account_address: Address,
pub new_account_info: GenerateMultiSigAccountPayload,
pub owner: Address,
pub addr_with_weight: Vec<AddressWithWeight>,
pub threshold: u32,
pub memo: String,
}

#[derive(RlpFixedCodec, Deserialize, Serialize, Clone, Debug, Default, PartialEq, Eq)]
Expand Down
6 changes: 5 additions & 1 deletion tests/e2e/multisig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface AddressWithWeight {

interface GenerateMultiSigAccountPayload {
owner: Address,
autonomy: boolean,
addr_with_weight: Vec<AddressWithWeight>,
threshold: u32,
memo: string,
Expand All @@ -30,7 +31,10 @@ interface GetMultiSigAccountResponse {

interface UpdateAccountPayload {
account_address: Address,
new_account_info: GenerateMultiSigAccountPayload
owner: Address,
addr_with_weight: Vec<AddressWithWeight>,
threshold: u32,
memo: string,
}

interface MultiSigPermission {
Expand Down
10 changes: 4 additions & 6 deletions tests/e2e/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ describe("API test via @mutadev/muta-sdk-js", () => {

var GenerateMultiSigAccountPayload = {
owner: wangYe.address,
autonomy: false,
addr_with_weight: [{ address: wangYe.address, weight: 1 }, { address: qing.address, weight: 1 }],
threshold: 2,
memo: 'welcome to BiYouCun'
Expand Down Expand Up @@ -127,18 +128,14 @@ describe("API test via @mutadev/muta-sdk-js", () => {
expect(Number(balance.code)).toBe(0);
expect(Number(balance.succeedData.balance)).toBe(2077);

const newMultiSigAccountPayload = {
const updateAccountPayload = {
account_address: multiSigAddress,
owner: wangYe.address,
addr_with_weight: [{ address: wangYe.address, weight: 3 }, { address: qing.address, weight: 1 }],
threshold: 4,
memo: 'welcome to BiYouCun'
};

const updateAccountPayload = {
account_address: multiSigAddress,
new_account_info: newMultiSigAccountPayload,
};

const update = await multiSigService.write.update_account(updateAccountPayload);
expect(Number(update.response.response.code)).toBe(0);

Expand All @@ -148,6 +145,7 @@ describe("API test via @mutadev/muta-sdk-js", () => {

var GenerateMultiSigAccountPayload = {
owner: wangYe.address,
autonomy: false,
addr_with_weight: [{ address: multiSigAddress, weight: 2 }, { address: fei.address, weight: 1 }],
threshold: 2,
memo: 'welcome to CiYouCun'
Expand Down

0 comments on commit a58831e

Please sign in to comment.