Skip to content

Commit

Permalink
Fixed borsh serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaoticTempest committed Jul 26, 2024
1 parent d854736 commit d41ed2a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 54 deletions.
1 change: 0 additions & 1 deletion chain-signatures/contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,6 @@ impl VersionedMpcContract {
&mut self,
#[serializer(borsh)] args: ProposeUpdateArgs,
) -> Result<UpdateId, MpcContractError> {
log!("propose_update: args={:?}", args,);
// Only voters can propose updates:
let proposer = self.voter()?;

Expand Down
2 changes: 1 addition & 1 deletion chain-signatures/contract/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub enum Update {
Contract(Vec<u8>),
}

#[derive(BorshDeserialize, BorshSerialize, Debug)]
#[derive(BorshDeserialize, BorshSerialize, Clone, Debug, Default)]
pub struct ProposeUpdateArgs {
pub code: Option<Vec<u8>>,
pub config: Option<Config>,
Expand Down
80 changes: 28 additions & 52 deletions chain-signatures/contract/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use borsh::{BorshDeserialize, BorshSerialize};
use crypto_shared::kdf::{check_ec_signature, derive_secret_key};
use crypto_shared::{
derive_epsilon, derive_key, ScalarExt as _, SerializableAffinePoint, SerializableScalar,
Expand Down Expand Up @@ -28,54 +27,26 @@ const CONTRACT_FILE_PATH: &str = "../../target/wasm32-unknown-unknown/release/mp
const INVALID_CONTRACT: &str = "../res/mpc_test_contract.wasm";
const PARTICIPANT_LEN: usize = 3;

#[derive(BorshSerialize, BorshDeserialize)]
pub struct ProposeUpdateArguments {
args: ProposeUpdateArgs,
}

impl ProposeUpdateArguments {
fn new_config() -> Self {
Self {
args: ProposeUpdateArgs {
code: None,
config: Some(Config {
protocol: ProtocolConfig {
max_concurrent_introduction: 2,
..ProtocolConfig::default()
},
..Config::default()
}),
},
}
fn dummy_contract() -> ProposeUpdateArgs {
ProposeUpdateArgs {
code: Some(vec![1, 2, 3]),
config: None,
}
}

fn dummy_contract() -> Self {
Self {
args: ProposeUpdateArgs {
code: Some(vec![1, 2, 3]),
config: None,
},
}
}

fn current_contract() -> Self {
let new_wasm = std::fs::read(CONTRACT_FILE_PATH).unwrap();
Self {
args: ProposeUpdateArgs {
code: Some(new_wasm),
config: None,
},
}
fn current_contract() -> ProposeUpdateArgs {
let new_wasm = std::fs::read(CONTRACT_FILE_PATH).unwrap();
ProposeUpdateArgs {
code: Some(new_wasm),
config: None,
}
}

fn invalid_contract() -> Self {
let new_wasm = std::fs::read(INVALID_CONTRACT).unwrap();
Self {
args: ProposeUpdateArgs {
code: Some(new_wasm),
config: None,
},
}
fn invalid_contract() -> ProposeUpdateArgs {
let new_wasm = std::fs::read(INVALID_CONTRACT).unwrap();
ProposeUpdateArgs {
code: Some(new_wasm),
config: None,
}
}

Expand Down Expand Up @@ -464,7 +435,7 @@ async fn test_propose_update_config(contract: &Contract, accounts: &[Account]) {
// contract should not be able to propose updates unless it's a part of the participant/voter set.
let execution = contract
.call("propose_update")
.args_borsh(ProposeUpdateArguments::dummy_contract())
.args_borsh((dummy_contract(),))
.transact()
.await
.unwrap();
Expand All @@ -476,18 +447,22 @@ async fn test_propose_update_config(contract: &Contract, accounts: &[Account]) {
.contains(&MpcContractError::from(errors::VoteError::VoterNotParticipant).to_string()));

// have each participant propose a new update:
let new_config = serde_json::json!(Config {
let new_config = Config {
protocol: ProtocolConfig {
max_concurrent_introduction: 2,
..ProtocolConfig::default()
},
..Config::default()
});
};

let mut proposals = Vec::with_capacity(accounts.len());
for account in accounts {
let propose_execution = account
.call(contract.id(), "propose_update")
.args_borsh(ProposeUpdateArguments::new_config())
.args_borsh((ProposeUpdateArgs {
code: None,
config: Some(new_config.clone()),
},))
.deposit(NearToken::from_millinear(100))
.transact()
.await
Expand Down Expand Up @@ -530,6 +505,7 @@ async fn test_propose_update_config(contract: &Contract, accounts: &[Account]) {
);
}
}
let new_config = serde_json::json!(new_config);
// check that the proposal executed since the threshold got changed.
let config: serde_json::Value = contract.view("config").await.unwrap().json().unwrap();
assert_ne!(config, old_config);
Expand All @@ -548,14 +524,14 @@ async fn test_propose_update_config(contract: &Contract, accounts: &[Account]) {
}

async fn test_propose_update_contract(contract: &Contract, accounts: &[Account]) {
const CONTRACT_DEPLOY: NearToken = NearToken::from_near(8);
const CONTRACT_DEPLOY: NearToken = NearToken::from_millinear(8500);
let state: mpc_contract::ProtocolContractState =
contract.view("state").await.unwrap().json().unwrap();

// Let's propose a contract update instead now.
let execution = accounts[0]
.call(contract.id(), "propose_update")
.args_borsh(ProposeUpdateArguments::current_contract())
.args_borsh((current_contract(),))
.max_gas()
.deposit(CONTRACT_DEPLOY)
.transact()
Expand Down Expand Up @@ -614,7 +590,7 @@ async fn test_invalid_contract_deploy(contract: &Contract, accounts: &[Account])
// Let's propose a contract update instead now.
let execution = accounts[0]
.call(contract.id(), "propose_update")
.args_borsh(ProposeUpdateArguments::invalid_contract())
.args_borsh((invalid_contract(),))
.max_gas()
.deposit(CONTRACT_DEPLOY)
.transact()
Expand Down

0 comments on commit d41ed2a

Please sign in to comment.