Ensure no side-effects, add events, more tests

This commit is contained in:
Drew Stone 2018-11-29 23:23:41 +02:00
parent d9f30b0f19
commit aaa89cfbc5
2 changed files with 67 additions and 14 deletions

View File

@ -70,19 +70,19 @@ decl_module! {
match <DepositOf<T>>::get(transaction_hash) {
Some(_) => { return Err("Deposit should not exist")},
None => {
// Create new deposit record
let index = Self::deposit_count();
<DepositCount<T>>::mutate(|i| *i += 1);
// If sender is a bridge authority add them to the set of signers
let mut signers = vec![];
if <Authorities<T>>::get().iter().any(|a| a == &_sender) {
signers.push(_sender);
}
// Create new deposit record
let index = Self::deposit_count();
<DepositCount<T>>::mutate(|i| *i += 1);
// Deposit record and send event
<DepositOf<T>>::insert(transaction_hash, (index, target, quantity, signers))
// TODO: Fire event
<DepositOf<T>>::insert(transaction_hash, (index, target.clone(), quantity, signers));
Self::deposit_event(RawEvent::Deposit(target, transaction_hash, quantity));
},
}
@ -140,10 +140,6 @@ decl_module! {
match <WithdrawOf<T>>::get(key) {
Some(_) => { return Err("Withdraw already exists")},
None => {
// Create new withdraw record
let index = Self::withdraw_count();
<WithdrawCount<T>>::mutate(|i| *i += 1);
// If sender is a bridge authority add them to the set of signers
let mut signers = vec![];
if <Authorities<T>>::get().iter().any(|a| a == &_sender) {
@ -153,9 +149,13 @@ decl_module! {
// Ensure sender has enough balance to withdraw from
ensure!(<balances::Module<T>>::total_balance(&_sender) >= quantity, "Invalid balance for withdraw");
// Create new withdraw record
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));
// TODO: Fire event
Self::deposit_event(RawEvent::Withdraw(_sender.clone(), quantity));
},
}
@ -217,7 +217,8 @@ impl<X, T> session::OnSessionChange<X> for Module<T> where T: Trait, T: session:
// instant changes
let last_authorities = <Authorities<T>>::get();
if next_authorities != last_authorities {
<Authorities<T>>::put(next_authorities);
<Authorities<T>>::put(next_authorities.clone());
Self::deposit_event(RawEvent::NewAuthorities(next_authorities));
}
}
}
@ -232,7 +233,7 @@ decl_event!(
// Withdraw event for an account, and an amount
Withdraw(AccountId, Balance),
/// New authority set has been applied.
NewAuthorities(Vec<(AccountId, u64)>),
NewAuthorities(Vec<AccountId>),
}
);

View File

@ -62,7 +62,7 @@ mod tests {
use super::*;
use runtime_io::with_externalities;
use system::{EventRecord, Phase};
use primitives::{H256, Blake2Hasher};
use primitives::{H256, Blake2Hasher, Hasher};
use runtime_primitives::{BuildStorage};
use runtime_primitives::traits::{BlakeTwo256, Identity};
use runtime_primitives::testing::{Digest, DigestItem, Header};
@ -185,4 +185,56 @@ mod tests {
assert_eq!(Bridge::authorities(), vec![1, 2, 3]);
});
}
#[test]
fn deposit_as_a_function_should_work() {
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!(System::events(), vec![
EventRecord {
phase: Phase::ApplyExtrinsic(0),
event: Event::bridge(RawEvent::Deposit(5, hash, 10)),
}]
);
});
}
#[test]
fn deposit_with_same_tx_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");
assert_ok!(deposit(5, 5, hash, 10));
assert_eq!(deposit(5, 5, hash, 10), Err("Deposit should not exist"));
});
}
#[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_eq!(System::events(), vec![
EventRecord {
phase: Phase::ApplyExtrinsic(0),
event: Event::bridge(RawEvent::Withdraw(5, 10)),
}]
);
});
}
#[test]
fn withdraw_with_not_enough_balance_not_work() {
with_externalities(&mut new_test_ext(), || {
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"));
});
}
}