Tests for preliminary bridge work

This commit is contained in:
Drew Stone 2018-11-30 23:55:19 +02:00
parent aaa89cfbc5
commit 0bc6cc4735
2 changed files with 181 additions and 17 deletions

View File

@ -77,10 +77,13 @@ decl_module! {
}
// Create new deposit record
let mut deposits = <Deposits<T>>::get();
deposits.push(transaction_hash);
<Deposits<T>>::put(deposits);
// Insert deposit record and send event
let index = Self::deposit_count();
<DepositCount<T>>::mutate(|i| *i += 1);
// Deposit record and send event
<DepositOf<T>>::insert(transaction_hash, (index, target.clone(), quantity, signers));
Self::deposit_event(RawEvent::Deposit(target, transaction_hash, quantity));
},
@ -104,8 +107,8 @@ decl_module! {
// Ensure senders can't sign twice
ensure!(!signers.iter().any(|id| id == &_sender), "Invalid duplicate signings");
// Add record update with new signer
let mut new_signers = signers;
new_signers.push(_sender);
let mut new_signers = signers.clone();
new_signers.push(_sender.clone());
<DepositOf<T>>::insert(transaction_hash, (inx, tgt.clone(), qty, new_signers.clone()));
// Check if we have reached enough signers for the deposit
@ -150,10 +153,13 @@ decl_module! {
ensure!(<balances::Module<T>>::total_balance(&_sender) >= quantity, "Invalid balance for withdraw");
// Create new withdraw record
let mut withdraws = <Withdraws<T>>::get();
withdraws.push(key);
<Withdraws<T>>::put(withdraws);
// Insert withdraw record and send event
let index = Self::withdraw_count();
<WithdrawCount<T>>::mutate(|i| *i += 1);
// Withdraw record and send event
<WithdrawOf<T>>::insert(key, (index, _sender.clone(), quantity, signers));
Self::deposit_event(RawEvent::Withdraw(_sender.clone(), quantity));
},
@ -177,7 +183,7 @@ decl_module! {
// Ensure sender is a bridge authority if record exists
ensure!(Self::authorities().iter().any(|id| id == &_sender), "Invalid non-authority sender");
// Ensure senders can't sign twice
ensure!(!signers.iter().any(|s| s.0 == _sender), "Invalid duplicate deposit signings");
ensure!(!signers.iter().any(|s| s.0 == _sender), "Invalid duplicate signings");
// Add record update with new signer
let mut new_signers = signers;
new_signers.push((_sender, signed_cross_chain_tx));
@ -200,7 +206,7 @@ decl_module! {
};
}
},
None => { return Err("Invalid transaction hash") },
None => { return Err("Invalid record hash") },
}
Ok(())
@ -208,6 +214,12 @@ decl_module! {
}
}
impl<T: Trait> Module<T> {
pub fn withdraw_record_hash(index: usize) -> T::Hash {
return <Withdraws<T>>::get()[index];
}
}
impl<X, T> session::OnSessionChange<X> for Module<T> where T: Trait, T: session::Trait {
fn on_session_change(_: X, _: bool) {
let next_authorities = <session::Module<T>>::validators()

View File

@ -49,7 +49,6 @@ extern crate srml_consensus as consensus;
// use council::{voting, motions, seats};
use rstd::prelude::*;
use runtime_support::dispatch::Result;
// use primitives::ed25519;
@ -164,12 +163,12 @@ mod tests {
Bridge::sign_deposit(Origin::signed(who), target, transaction_hash, quantity)
}
fn withdraw(who: u64, quantity: u64, signed_cross_chain_tx: Vec<u8>) -> super::Result {
Bridge::withdraw(Origin::signed(who), quantity, signed_cross_chain_tx)
fn withdraw(who: u64, quantity: u64, signed_cross_chain_tx: &[u8]) -> super::Result {
Bridge::withdraw(Origin::signed(who), quantity, signed_cross_chain_tx.to_vec())
}
fn sign_withdraw(who: u64, target: u64, record_hash: H256, quantity: u64, signed_cross_chain_tx: Vec<u8>) -> super::Result {
Bridge::sign_withdraw(Origin::signed(who), target, record_hash, quantity, signed_cross_chain_tx)
fn sign_withdraw(who: u64, target: u64, record_hash: H256, quantity: u64, signed_cross_chain_tx: &[u8]) -> super::Result {
Bridge::sign_withdraw(Origin::signed(who), target, record_hash, quantity, signed_cross_chain_tx.to_vec())
}
#[test]
@ -206,19 +205,98 @@ mod tests {
with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let hash = Blake2Hasher::hash(b"a sends money to b");
assert_ok!(deposit(5, 5, hash, 10));
assert_eq!(deposit(5, 5, hash, 10), Err("Deposit should not exist"));
let quantity = 10;
assert_ok!(deposit(5, 5, hash, quantity));
assert_eq!(deposit(5, 5, hash, quantity), Err("Deposit should not exist"));
});
}
#[test]
fn sign_deposit_as_bridge_authority_should_work() {
with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let hash = Blake2Hasher::hash(b"a sends money to b");
let quantity = 10;
assert_ok!(deposit(5, 5, hash, quantity));
assert_ok!(sign_deposit(1, 5, hash, quantity));
});
}
// FIXME: This works but I'm confused why it's a supermajority
#[test]
fn sign_deposit_supermajority_should_work() {
with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let hash = Blake2Hasher::hash(b"a sends money to b");
let quantity = 10;
assert_ok!(deposit(5, 5, hash, quantity));
assert_eq!(Balances::total_balance(&5), 100);
assert_ok!(sign_deposit(1, 5, hash, quantity));
assert_eq!(Balances::total_balance(&5), 110);
});
}
#[test]
fn sign_non_existent_deposit_as_bridge_authority_should_not_work() {
with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let hash = Blake2Hasher::hash(b"a sends money to b");
let quantity = 10;
assert_eq!(sign_deposit(1, 5, hash, quantity), Err("Invalid transaction hash"));
});
}
#[test]
fn sign_deposit_with_wrong_quantity_should_not_work() {
with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let hash = Blake2Hasher::hash(b"a sends money to b");
let quantity = 10;
assert_ok!(deposit(5, 5, hash, quantity));
assert_eq!(sign_deposit(1, 5, hash, quantity - 1), Err("Quantities don't match"));
});
}
#[test]
fn sign_deposit_with_wrong_target_should_not_work() {
with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let hash = Blake2Hasher::hash(b"a sends money to b");
let quantity = 10;
assert_ok!(deposit(5, 5, hash, quantity));
assert_eq!(sign_deposit(1, 4, hash, quantity), Err("Accounts do not match"));
});
}
#[test]
fn sign_deposit_as_non_authority_should_not_work() {
with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let hash = Blake2Hasher::hash(b"a sends money to b");
let quantity = 10;
assert_ok!(deposit(5, 5, hash, quantity));
assert_eq!(sign_deposit(5, 5, hash, quantity), Err("Invalid non-authority sender"));
});
}
#[test]
fn sign_deposit_twice_should_not_work() {
with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let hash = Blake2Hasher::hash(b"a sends money to b");
let quantity = 10;
assert_ok!(deposit(5, 5, hash, quantity));
assert_ok!(sign_deposit(1, 5, hash, quantity));
assert_eq!(sign_deposit(1, 5, hash, quantity), Err("Invalid duplicate signings"))
});
}
#[test]
fn withdraw_as_a_function_should_work() {
with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let signed_tx = b"a sends money to b on Ethereum";
assert_ok!(withdraw(5, 10, signed_tx.to_vec()));
assert_ok!(withdraw(5, 10, signed_tx));
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::ApplyExtrinsic(0),
@ -234,7 +312,81 @@ mod tests {
System::set_block_number(1);
let signed_tx = b"a sends money to b on Ethereum";
assert_eq!(Balances::total_balance(&4), 100);
assert_eq!(withdraw(4, 101,signed_tx.to_vec()), Err("Invalid balance for withdraw"));
assert_eq!(withdraw(4, 101, signed_tx), Err("Invalid balance for withdraw"));
});
}
#[test]
fn sign_withdraw_supermajority_should_work() {
with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let cross_chain_proof = b"a sent b 1 ETH";
let quantity = 10;
assert_ok!(withdraw(5, quantity, cross_chain_proof));
assert_eq!(Balances::total_balance(&5), 100);
let hash = Bridge::withdraw_record_hash(0);
assert_ok!(sign_withdraw(1, 5, hash, quantity, cross_chain_proof));
assert_eq!(Balances::total_balance(&5), 100 - quantity);
});
}
#[test]
fn sign_withdraw_with_wrong_quantity_should_not_work() {
with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let cross_chain_proof = b"a sent b 1 ETH";
let quantity = 10;
assert_ok!(withdraw(5, quantity, cross_chain_proof));
let hash = Bridge::withdraw_record_hash(0);
assert_eq!(sign_withdraw(1, 5, hash, quantity - 1, cross_chain_proof), Err("Quantities don't match"));
});
}
#[test]
fn sign_withdraw_with_wrong_target_should_not_work() {
with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let cross_chain_proof = b"a sent b 1 ETH";
let quantity = 10;
assert_ok!(withdraw(5, quantity, cross_chain_proof));
let hash = Bridge::withdraw_record_hash(0);
assert_eq!(sign_withdraw(1, 4, hash, quantity, cross_chain_proof), Err("Accounts do not match"));
});
}
#[test]
fn sign_withdraw_as_non_authority_should_not_work() {
with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let cross_chain_proof = b"a sent b 1 ETH";
let quantity = 10;
assert_ok!(withdraw(5, quantity, cross_chain_proof));
let hash = Bridge::withdraw_record_hash(0);
assert_eq!(sign_withdraw(5, 5, hash, quantity, cross_chain_proof), Err("Invalid non-authority sender"));
});
}
#[test]
fn sign_withdraw_twice_should_not_work() {
with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let cross_chain_proof = b"a sent b 1 ETH";
let quantity = 10;
assert_ok!(withdraw(5, quantity, cross_chain_proof));
let hash = Bridge::withdraw_record_hash(0);
assert_ok!(sign_withdraw(1, 5, hash, quantity, cross_chain_proof));
assert_eq!(sign_withdraw(1, 5, hash, quantity, cross_chain_proof), Err("Invalid duplicate signings"))
});
}
#[test]
fn sign_withdraw_with_non_existent_record_hash_should_not_work() {
with_externalities(&mut new_test_ext(), || {
System::set_block_number(1);
let cross_chain_proof = b"a sent b 1 ETH";
let quantity = 10;
let hash = Blake2Hasher::hash(b"drew stone was here");
assert_eq!(sign_withdraw(1, 4, hash, quantity, cross_chain_proof), Err("Invalid record hash"));
});
}
}