Introduce UncheckedTransaction & test.

This commit is contained in:
Gav
2018-01-18 15:14:01 +01:00
parent 712becb205
commit 92d8712b2b
8 changed files with 143 additions and 71 deletions
@@ -2,14 +2,13 @@ use runtime_support::Vec;
use keyedvec::KeyedVec;
use storage::Storage;
use primitives::{AccountID, SessionKey, BlockNumber};
use storage::storage_into;
pub fn set_authority(index: u32, authority: AccountID) {
authority.store(&index.to_keyed_vec(b"con\0aut\0"));
}
fn authority(index: u32) -> AccountID {
storage_into(&index.to_keyed_vec(b"con\0aut\0"))
Storage::into(&index.to_keyed_vec(b"con\0aut\0"))
}
pub fn set_authority_count(count: u32) {
@@ -18,7 +17,7 @@ pub fn set_authority_count(count: u32) {
}
fn authority_count() -> u32 {
storage_into(b"con\0aut\0len")
Storage::into(b"con\0aut\0len")
}
/// Get the current set of authorities. These are the session keys.
@@ -49,7 +48,7 @@ pub fn set_validators(_new: &[AccountID]) {
/// The number of blocks in each session.
pub fn session_length() -> BlockNumber {
storage_into(b"con\0bps")
Storage::into(b"con\0bps")
}
/// Sets the session key of `_transactor` to `_session`. This doesn't take effect until the next
@@ -1,5 +1,5 @@
use keyedvec::KeyedVec;
use storage::{Storage, storage_into};
use storage::Storage;
use primitives::{BlockNumber, Balance, AccountID};
use runtime::consensus;
@@ -10,7 +10,7 @@ pub fn era_length() -> BlockNumber {
/// The length of a staking era in sessions.
pub fn sessions_per_era() -> BlockNumber {
storage_into(b"sta\0spe")
Storage::into(b"sta\0spe")
}
/// The era has changed - enact new staking set.
@@ -22,16 +22,16 @@ pub fn next_era() {
/// The balance of a given account.
pub fn balance(who: &AccountID) -> Balance {
storage_into(&who.to_keyed_vec(b"sta\0bal\0"))
Storage::into(&who.to_keyed_vec(b"sta\0bal\0"))
}
/// Transfer some unlocked staking balance to another staker.
pub fn transfer_stake(transactor: &AccountID, dest: &AccountID, value: Balance) {
let from_key = transactor.to_keyed_vec(b"sta\0bal\0");
let from_balance: Balance = storage_into(&from_key);
let from_balance: Balance = Storage::into(&from_key);
assert!(from_balance >= value);
let to_key = dest.to_keyed_vec(b"sta\0bal\0");
let to_balance: Balance = storage_into(&to_key);
let to_balance: Balance = Storage::into(&to_key);
assert!(to_balance + value > to_balance); // no overflow
(from_balance - value).store(&from_key);
(to_balance + value).store(&to_key);
@@ -1,4 +1,4 @@
use primitives::{Block, BlockNumber, Hash, Transaction};
use primitives::{Block, BlockNumber, Hash, UncheckedTransaction};
use runtime_support::{Vec, swap};
use environment::with_env;
use runtime_support;
@@ -53,7 +53,7 @@ fn final_checks(_block: &Block) {
}
/// Execute a given transaction.
pub fn execute_transaction(_tx: &Transaction) -> Vec<u8> {
pub fn execute_transaction(_tx: &UncheckedTransaction) -> Vec<u8> {
// TODO: decode data and ensure valid
// TODO: ensure signature valid and recover id (use authentication::authenticate)
// TODO: check nonce
@@ -61,7 +61,7 @@ pub fn execute_transaction(_tx: &Transaction) -> Vec<u8> {
// TODO: ensure target_function valid
// TODO: decode parameters
_tx.function.dispatch(&_tx.signed, &_tx.input_data);
_tx.transaction.function.dispatch(&_tx.transaction.signed, &_tx.transaction.input_data);
// TODO: encode any return
Vec::new()
@@ -78,7 +78,7 @@ mod tests {
use function::Function;
use std::collections::HashMap;
use runtime_support::{NoError, with_externalities, Externalities};
use primitives::{AccountID, Transaction};
use primitives::{AccountID, UncheckedTransaction, Transaction};
use runtime::{system, staking};
#[derive(Debug, Default)]
@@ -114,11 +114,14 @@ mod tests {
{ let mut r = b"sta\0bal\0".to_vec(); r.extend_from_slice(&one); r } => vec![111u8, 0, 0, 0, 0, 0, 0, 0]
], };
let tx = Transaction {
signed: one.clone(),
function: Function::StakingTransferStake,
input_data: vec![].join(&two).join(&69u64),
nonce: 0,
let tx = UncheckedTransaction {
transaction: Transaction {
signed: one.clone(),
nonce: 0,
function: Function::StakingTransferStake,
input_data: vec![].join(&two).join(&69u64),
},
signature: [1u8; 64],
};
with_externalities(&mut t, || {
@@ -1,9 +1,10 @@
use primitives::Timestamp;
use storage::Storage;
pub fn timestamp() -> Timestamp {
unimplemented!()
Storage::into(b"tim\0val")
}
pub fn set_timestamp(_now: Timestamp) {
unimplemented!()
pub fn set_timestamp(now: Timestamp) {
now.store(b"tim\0val")
}