-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Raymond
committed
Dec 2, 2019
1 parent
e9fd781
commit 27a745e
Showing
9 changed files
with
265 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
#[cfg(test)] | ||
mod mock; | ||
#[cfg(test)] | ||
mod tests; | ||
|
||
pub mod tezosworker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2019 Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate. | ||
|
||
// Substrate is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Substrate is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
//! Test utilities | ||
#![cfg(test)] | ||
extern crate substrate_primitives as primitives; | ||
extern crate sr_io as runtime_io; | ||
|
||
use crate::tezosworker::{Module, Trait}; | ||
use sr_primitives::Perbill; | ||
use sr_primitives::testing::{Header, UintAuthorityId, TestXt}; | ||
use sr_primitives::traits::{IdentityLookup, BlakeTwo256}; | ||
use primitives::H256; | ||
use support::{impl_outer_origin, impl_outer_dispatch, parameter_types}; | ||
use system; | ||
|
||
use node_primitives::{Moment}; | ||
use node_primitives::constants::time::*; | ||
|
||
impl_outer_origin!{ | ||
pub enum Origin for Runtime {} | ||
} | ||
|
||
impl_outer_dispatch! { | ||
pub enum Call for Runtime where origin: Origin { | ||
tezosworker::TezosWorker, | ||
} | ||
} | ||
|
||
type DummyValidatorId = u64; | ||
|
||
/// An extrinsic type used for tests. | ||
pub type Extrinsic = TestXt<Call, ()>; | ||
type SubmitTransaction = system::offchain::TransactionSubmitter<(), Call, Extrinsic>; | ||
|
||
#[derive(Clone, PartialEq, Eq, Debug)] | ||
pub struct Runtime; | ||
|
||
parameter_types! { | ||
pub const BlockHashCount: u64 = 250; | ||
pub const MaximumBlockWeight: u32 = 1024; | ||
pub const MaximumBlockLength: u32 = 2 * 1024; | ||
pub const AvailableBlockRatio: Perbill = Perbill::one(); | ||
pub const MinimumPeriod: u64 = 1; | ||
} | ||
|
||
impl system::Trait for Runtime { | ||
type Origin = Origin; | ||
type Index = u64; | ||
type BlockNumber = u64; | ||
type Call = Call; | ||
type Hash = H256; | ||
type Hashing = BlakeTwo256; | ||
type AccountId = u64; | ||
type Lookup = IdentityLookup<Self::AccountId>; | ||
type Header = Header; | ||
type Event = (); | ||
type BlockHashCount = BlockHashCount; | ||
type MaximumBlockWeight = MaximumBlockWeight; | ||
type MaximumBlockLength = MaximumBlockLength; | ||
type AvailableBlockRatio = AvailableBlockRatio; | ||
type Version = (); | ||
} | ||
|
||
parameter_types! { | ||
pub const EpochDuration: u64 = EPOCH_DURATION_IN_SLOTS; | ||
pub const ExpectedBlockTime: Moment = MILLISECS_PER_BLOCK; | ||
} | ||
|
||
impl babe::Trait for Runtime { | ||
type EpochDuration = EpochDuration; | ||
type ExpectedBlockTime = ExpectedBlockTime; | ||
type EpochChangeTrigger = babe::ExternalTrigger; | ||
} | ||
|
||
impl timestamp::Trait for Runtime { | ||
type Moment = u64; | ||
type OnTimestampSet = (); | ||
type MinimumPeriod = MinimumPeriod; | ||
} | ||
|
||
impl stafi_staking_storage::Trait for Runtime { | ||
|
||
} | ||
|
||
impl Trait for Runtime { | ||
type Event = (); | ||
type Call = Call; | ||
type SubmitTransaction = SubmitTransaction; | ||
} | ||
|
||
pub fn new_test_ext(authorities: Vec<DummyValidatorId>) -> runtime_io::TestExternalities { | ||
let mut t = system::GenesisConfig::default().build_storage::<Runtime>().unwrap(); | ||
|
||
babe::GenesisConfig { | ||
authorities: authorities.into_iter().map(|a| (UintAuthorityId(a).to_public_key(), 1)).collect(), | ||
}.assimilate_storage::<Runtime>(&mut t).unwrap(); | ||
|
||
t.into() | ||
} | ||
|
||
/// TezosWorker module. | ||
pub type TezosWorker = Module<Runtime>; | ||
pub type System = system::Module<Runtime>; | ||
pub type Timestamp = timestamp::Module<Runtime>; | ||
pub type Babe = babe::Module<Runtime>; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2019 Parity Technologies (UK) Ltd. | ||
// This file is part of Substrate. | ||
|
||
// Substrate is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Substrate is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
//! Tests for the tezos worker module. | ||
#![cfg(test)] | ||
extern crate substrate_primitives as primitives; | ||
|
||
use super::*; | ||
use crate::mock::*; | ||
use primitives::offchain::{ | ||
OffchainExt, | ||
TransactionPoolExt, | ||
testing::{TestOffchainExt, TestTransactionPoolExt}, | ||
}; | ||
|
||
use node_primitives::VerifyStatus; | ||
use tezosworker::tezos; | ||
|
||
#[test] | ||
fn test_offchain_local_storage() { | ||
let mut ext = new_test_ext(vec![0, 1, 2, 3]); | ||
let (offchain, _state) = TestOffchainExt::new(); | ||
let (pool, _state) = TestTransactionPoolExt::new(); | ||
ext.register_extension(OffchainExt::new(offchain)); | ||
ext.register_extension(TransactionPoolExt::new(pool)); | ||
|
||
ext.execute_with(|| { | ||
let key = "my_key".as_bytes().to_vec(); | ||
let value = (15700 as u64).to_be_bytes().to_vec(); | ||
tezos::set_value(&key, &value); | ||
|
||
assert_eq!(true, tezos::get_value(&key).is_some()); | ||
let val = tezos::get_value(&key).unwrap(); | ||
assert_eq!(true, tezos::vec8_to_u64(val) == 15700); | ||
|
||
let key = "your_key".as_bytes().to_vec(); | ||
assert_eq!(true, tezos::get_value(&key).is_none()); | ||
}); | ||
} | ||
|
||
#[test] | ||
#[ignore] | ||
fn test_offchain_request_tezos() { | ||
let mut ext = new_test_ext(vec![0, 1, 2, 3]); | ||
let (offchain, _state) = TestOffchainExt::new(); | ||
let (pool, _state) = TestTransactionPoolExt::new(); | ||
ext.register_extension(OffchainExt::new(offchain)); | ||
ext.register_extension(TransactionPoolExt::new(pool)); | ||
|
||
ext.execute_with(|| { | ||
let host = "https://rpc.tezrpc.me".as_bytes().to_vec(); | ||
let blockhash = "BKsxzJMXPxxJWRZcsgWG8AAegXNp2uUuUmMr8gzQcoEiGnNeCA6".as_bytes().to_vec(); | ||
let txhash = "onv7i9LSacMXjhTdpgzmY4q6PxiZ18TZPq7KrRBRUVX7XJicSDi".as_bytes().to_vec(); | ||
let from = "tz1SYq214SCBy9naR6cvycQsYcUGpBqQAE8d".as_bytes().to_vec(); | ||
let to = "tz1S4MTpEV356QcuzkjQUdyZdAy36gPwPWXa".as_bytes().to_vec(); | ||
let amount = 710391; | ||
|
||
let mut level = 0; | ||
let result = tezos::request_tezos(host, blockhash, txhash, from, to, amount, &mut level); | ||
|
||
assert_eq!(true, result == VerifyStatus::Confirmed); | ||
assert_eq!(true, level == 642208); | ||
}); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters