Skip to content

Commit

Permalink
fix tests in multisig template
Browse files Browse the repository at this point in the history
  • Loading branch information
poszu committed Jan 9, 2025
1 parent 4871455 commit 850e82d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
2 changes: 1 addition & 1 deletion vm/templates/multisig/elf/multisig
Git LFS file not shown
23 changes: 5 additions & 18 deletions vm/templates/multisig/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! The Spacemesh standard multi-signature wallet template.
extern crate alloc;
use athena_interface::Address;
use athena_vm_declare::{callable, template};
Expand All @@ -10,25 +9,13 @@ use parity_scale_codec::{Decode, Encode, IoReader};
#[derive(Encode, Decode)]
struct Contract {
required: u8,
keys: alloc::vec::Vec<Pubkey>,
}

#[derive(Decode)]
struct SpawnArguments {
required: u8,
keys: alloc::vec::Vec<Pubkey>,
}

#[derive(Encode, Decode)]
struct Signature {
id: u8,
sig: [u8; 64],
keys: Vec<Pubkey>,
}

#[template]
impl Contract {
#[callable]
fn spawn(args: SpawnArguments) -> Address {
fn spawn(args: multisig::SpawnArguments) -> Address {
let wallet = Contract {
required: args.required,
keys: args.keys,
Expand All @@ -43,7 +30,7 @@ impl Contract {
}

#[callable]
fn deploy(&self, code: alloc::vec::Vec<u8>) -> Address {
fn deploy(&self, code: Vec<u8>) -> Address {
athena_vm_sdk::deploy(&code)
}

Expand All @@ -56,11 +43,11 @@ impl Contract {
fn verify() -> bool {
let mut io = IoReader(athena_vm::io::Io::default());
let state = Contract::decode(&mut io).unwrap();
let tx = alloc::vec::Vec::<u8>::decode(&mut io).unwrap();
let tx = Vec::<u8>::decode(&mut io).unwrap();

let mut last_id = None;
for _ in 0..state.required {
let sig = if let Ok(s) = Signature::decode(&mut io) {
let sig = if let Ok(s) = multisig::Signature::decode(&mut io) {
s
} else {
return false;
Expand Down
14 changes: 14 additions & 0 deletions vm/templates/multisig/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use athena_vm_sdk::Pubkey;
use parity_scale_codec::{Decode, Encode};

#[derive(Encode, Decode)]
pub struct SpawnArguments {
pub required: u8,
pub keys: Vec<Pubkey>,
}

#[derive(Encode, Decode)]
pub struct Signature {
pub id: u8,
pub sig: [u8; 64],
}
22 changes: 10 additions & 12 deletions vm/templates/multisig/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::error::Error;

use athena_interface::{Address, MethodSelector};
use athena_sdk::{AthenaStdin, ExecutionClient};
use athena_sdk::{host::MockHostInterface, AthenaStdin, ExecutionClient};
use athena_vm_sdk::Pubkey;
use multisig::SpawnArguments;
use parity_scale_codec::Encode;

use ed25519_dalek::ed25519::signature::Signer;
Expand All @@ -11,12 +12,6 @@ use rand::rngs::OsRng;
pub const PROGRAM: &[u8] = include_bytes!("../elf/multisig");
pub const ADDRESS_ALICE: [u8; 24] = [1u8; 24];

#[derive(Encode)]
struct SpawnArguments {
required: u8,
keys: Vec<Pubkey>,
}

#[derive(Clone)]
struct SigningKey {
id: u8,
Expand All @@ -26,7 +21,7 @@ struct SigningKey {
fn spawn(required: u8, keys: Vec<Pubkey>) -> Result<(Address, Vec<u8>), Box<dyn Error>> {
let mut stdin = AthenaStdin::new();
let (state_w, state_r) = std::sync::mpsc::channel();
let mut host = athena_interface::MockHostInterface::new();
let mut host = MockHostInterface::new();
host.expect_spawn().returning_st(move |s| {
state_w.send(s).unwrap();
Address::from(ADDRESS_ALICE)
Expand All @@ -48,7 +43,8 @@ fn spawn(required: u8, keys: Vec<Pubkey>) -> Result<(Address, Vec<u8>), Box<dyn
)?;

let state = state_r.recv().unwrap();
Ok((result.read(), state))
let address: [u8; 24] = result.read();
Ok((Address(address), state))
}

fn verify(state: Vec<u8>, keys: &[SigningKey]) -> bool {
Expand All @@ -59,9 +55,11 @@ fn verify(state: Vec<u8>, keys: &[SigningKey]) -> bool {
stdin.write_vec(tx.as_slice().encode());

for key in keys.iter() {
let signature = key.key.sign(tx);
stdin.write_vec(key.id.encode());
stdin.write_vec(signature.to_bytes().encode());
let signature = multisig::Signature {
id: key.id,
sig: key.key.sign(tx).to_bytes(),
};
stdin.write_vec(signature.encode());
}

let result = ExecutionClient::new().execute_function(
Expand Down

0 comments on commit 850e82d

Please sign in to comment.