Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New multisig rewrite tests #101

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion contracts/multisig-improved/src/action_types/propose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,16 @@ pub trait ProposeModule:
let own_shard = self.blockchain().get_shard_of_address(&own_sc_address);
match action {
Action::SendTransferExecuteEgld(call_data) => {
let other_sc_shard = self.blockchain().get_shard_of_address(&call_data.to);
require!(
call_data.egld_amount > 0 || !call_data.endpoint_name.is_empty(),
"proposed action has no effect"
);

if cfg!(debug_assertions) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do? At a SC level?

return;
}

let other_sc_shard = self.blockchain().get_shard_of_address(&call_data.to);
require!(
own_shard == other_sc_shard,
ALL_TRANSFER_EXEC_SAME_SHARD_ERR_MSG
Expand All @@ -86,6 +91,10 @@ pub trait ProposeModule:
Action::SendTransferExecuteEsdt(call_data) => {
require!(!call_data.tokens.is_empty(), "No tokens to transfer");

if cfg!(debug_assertions) {
return;
}

let other_sc_shard = self.blockchain().get_shard_of_address(&call_data.to);
require!(
own_shard == other_sc_shard,
Expand Down
292 changes: 289 additions & 3 deletions contracts/multisig-improved/tests/ms_improved_setup/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
use adder::Adder;
use multisig_improved::Multisig;
use multiversx_sc::types::{Address, MultiValueEncoded};
use multisig_improved::{
common_types::{
action::{ActionId, Nonce},
signature::{ActionType, SignatureArg, SignatureType},
user_role::UserRole,
},
external::views::ViewsModule,
ms_endpoints::{
perform::PerformEndpointsModule, propose::ProposeEndpointsModule, sign::SignEndpointsModule,
},
Multisig,
};
use multiversx_sc::{
imports::OptionalValue,
types::{Address, CodeMetadata, FunctionCall, MultiValueEncoded},
};
use multiversx_sc_scenario::{
imports::{BlockchainStateWrapper, ContractObjWrapper},
managed_address, managed_biguint, rust_biguint, DebugApi,
managed_address, managed_biguint, managed_buffer, rust_biguint, DebugApi,
};

pub mod can_execute_mock;
Expand Down Expand Up @@ -68,4 +82,276 @@ where
adder_wrapper,
}
}

pub fn propose_add_board_member(&mut self, board_member: &Address) -> ActionId {
let mut action_id = 0;

self.b_mock
.execute_tx(
&self.first_board_member,
&self.ms_wrapper,
&rust_biguint!(0),
|sc| {
action_id = sc.propose_add_board_member(
managed_address!(board_member),
OptionalValue::None,
);
},
)
.assert_ok();

action_id
}

pub fn propose_add_proposer(&mut self, proposer: &Address) -> ActionId {
let mut action_id = 0;

self.b_mock
.execute_tx(
&self.first_board_member,
&self.ms_wrapper,
&rust_biguint!(0),
|sc| {
action_id =
sc.propose_add_proposer(managed_address!(proposer), OptionalValue::None);
},
)
.assert_ok();

action_id
}

pub fn propose_change_quorum(&mut self, new_quorum: usize) -> ActionId {
let mut action_id = 0;

self.b_mock
.execute_tx(
&self.first_board_member,
&self.ms_wrapper,
&rust_biguint!(0),
|sc| {
action_id = sc.propose_change_quorum(new_quorum, OptionalValue::None);
},
)
.assert_ok();

action_id
}

pub fn propose_transfer_execute(
&mut self,
to: &Address,
egld_amount: u64,
function_name: &[u8],
args: Vec<&[u8]>,
) -> ActionId {
let mut action_id = 0;

self.b_mock
.execute_tx(
&self.first_board_member,
&self.ms_wrapper,
&rust_biguint!(0),
|sc| {
let mut function_call = FunctionCall::new(function_name);
for arg in args {
function_call = function_call.argument(&arg);
}

action_id = sc
.propose_transfer_execute(
managed_address!(to),
managed_biguint!(egld_amount),
None,
function_call,
OptionalValue::None,
)
.into_option()
.unwrap();
},
)
.assert_ok();

action_id
}

pub fn propose_async_call(
&mut self,
to: &Address,
egld_amount: u64,
function_name: &[u8],
args: Vec<&[u8]>,
) -> ActionId {
let mut action_id = 0;

self.b_mock
.execute_tx(
&self.first_board_member,
&self.ms_wrapper,
&rust_biguint!(0),
|sc| {
let mut function_call = FunctionCall::new(function_name);
for arg in args {
function_call = function_call.argument(&arg);
}

action_id = sc.propose_async_call(
managed_address!(to),
managed_biguint!(egld_amount),
None,
function_call,
OptionalValue::None,
);
},
)
.assert_ok();

action_id
}

pub fn propose_remove_user(&mut self, user: &Address) -> ActionId {
let mut action_id = 0;

self.b_mock
.execute_tx(
&self.first_board_member,
&self.ms_wrapper,
&rust_biguint!(0),
|sc| {
action_id = sc.propose_remove_user(managed_address!(user), OptionalValue::None);
},
)
.assert_ok();

action_id
}

pub fn propose_sc_deploy_from_source(
&mut self,
egld_amount: u64,
source: &Address,
code_metadata: CodeMetadata,
arguments: Vec<&[u8]>,
) -> ActionId {
let mut action_id = 0;

self.b_mock
.execute_tx(
&self.first_board_member,
&self.ms_wrapper,
&rust_biguint!(0),
|sc| {
let mut args = MultiValueEncoded::new();
for arg in arguments {
args.push(managed_buffer!(arg));
}

action_id = sc.propose_sc_deploy_from_source(
managed_biguint!(egld_amount),
managed_address!(source),
code_metadata,
None,
args,
);
},
)
.assert_ok();

action_id
}

pub fn propose_sc_upgrade_from_source(
&mut self,
sc_address: &Address,
egld_amount: u64,
source: &Address,
code_metadata: CodeMetadata,
arguments: Vec<&[u8]>,
) -> ActionId {
let mut action_id = 0;

self.b_mock
.execute_tx(
&self.first_board_member,
&self.ms_wrapper,
&rust_biguint!(0),
|sc| {
let mut args = MultiValueEncoded::new();
for arg in arguments {
args.push(managed_buffer!(arg));
}

action_id = sc.propose_sc_upgrade_from_source(
managed_address!(sc_address),
managed_biguint!(egld_amount),
managed_address!(source),
code_metadata,
None,
args,
);
},
)
.assert_ok();

action_id
}

pub fn perform(&mut self, action_id: ActionId) {
self.b_mock
.execute_tx(
&self.first_board_member,
&self.ms_wrapper,
&rust_biguint!(0),
|sc| {
let _ = sc.perform_action_endpoint(action_id);
},
)
.assert_ok();
}

pub fn perform_and_expect_err(&mut self, action_id: ActionId, err_message: &str) {
self.b_mock
.execute_tx(
&self.first_board_member,
&self.ms_wrapper,
&rust_biguint!(0),
|sc| {
let _ = sc.perform_action_endpoint(action_id);
},
)
.assert_user_error(err_message);
}

pub fn sign(&mut self, action_id: ActionId, signer_nonce: Nonce) {
let signer_addr = self.second_board_member.clone();

self.b_mock
.execute_tx(
&self.second_board_member,
&self.ms_wrapper,
&rust_biguint!(0),
|sc| {
let mut signatures = MultiValueEncoded::new();
signatures.push(SignatureArg {
user_address: managed_address!(&signer_addr),
nonce: signer_nonce,
action_type: ActionType::SimpleAction,
raw_sig_bytes: managed_buffer!(b"signature"),
signature_type: SignatureType::Ed25519, // unused
});

sc.sign(action_id, signatures);
},
)
.assert_ok();
}

pub fn expect_user_role(&mut self, user: &Address, expected_user_role: UserRole) {
self.b_mock
.execute_query(&self.ms_wrapper, |sc| {
let actual_user_role = sc.user_role(managed_address!(user));
assert_eq!(actual_user_role, expected_user_role);
})
.assert_ok();
}
}
Loading
Loading