Crate exonum_testkit [−] [src]
Testkit for Exonum blockchain framework, allowing to test service APIs synchronously and in the same process as the testkit.
Example
#[macro_use] extern crate exonum; #[macro_use] extern crate exonum_testkit; extern crate serde_json; use exonum::crypto::{gen_keypair, Hash, PublicKey}; use exonum::blockchain::{Block, Schema, Service, Transaction}; use exonum::messages::{Message, RawTransaction}; use exonum::storage::{Snapshot, Fork}; use exonum::encoding; use exonum_testkit::{ApiKind, TestKitBuilder}; // Simple service implementation. const SERVICE_ID: u16 = 1; const TX_TIMESTAMP_ID: u16 = 1; message! { struct TxTimestamp { const TYPE = SERVICE_ID; const ID = TX_TIMESTAMP_ID; from: &PublicKey, msg: &str, } } struct TimestampingService; impl Transaction for TxTimestamp { fn verify(&self) -> bool { self.verify_signature(self.from()) } fn execute(&self, _fork: &mut Fork) {} } impl Service for TimestampingService { fn service_name(&self) -> &'static str { "timestamping" } fn state_hash(&self, _: &Snapshot) -> Vec<Hash> { Vec::new() } fn service_id(&self) -> u16 { SERVICE_ID } fn tx_from_raw(&self, raw: RawTransaction) -> Result<Box<Transaction>, encoding::Error> { let trans: Box<Transaction> = match raw.message_type() { TX_TIMESTAMP_ID => Box::new(TxTimestamp::from_raw(raw)?), _ => { return Err(encoding::Error::IncorrectMessageType { message_type: raw.message_type(), }); } }; Ok(trans) } } fn main() { // Create testkit for network with four validators. let mut testkit = TestKitBuilder::validator() .with_validators(4) .with_service(TimestampingService) .create(); // Create few transactions. let keypair = gen_keypair(); let tx1 = TxTimestamp::new(&keypair.0, "Down To Earth", &keypair.1); let tx2 = TxTimestamp::new(&keypair.0, "Cry Over Spilt Milk", &keypair.1); let tx3 = TxTimestamp::new(&keypair.0, "Dropping Like Flies", &keypair.1); // Commit them into blockchain. testkit.create_block_with_transactions(txvec![ tx1.clone(), tx2.clone(), tx3.clone() ]); // Add a single transaction. let tx4 = TxTimestamp::new(&keypair.0, "Barking up the wrong tree", &keypair.1); testkit.create_block_with_transaction(tx4.clone()); // Check results with schema. let snapshot = testkit.snapshot(); let schema = Schema::new(&snapshot); assert!(schema.transactions().contains(&tx1.hash())); assert!(schema.transactions().contains(&tx2.hash())); assert!(schema.transactions().contains(&tx3.hash())); assert!(schema.transactions().contains(&tx4.hash())); // Check results with api. let api = testkit.api(); let blocks: Vec<Block> = api.get(ApiKind::Explorer, "v1/blocks?count=10"); assert_eq!(blocks.len(), 3); api.get::<serde_json::Value>( ApiKind::System, &format!("v1/transactions/{}", tx1.hash().to_string()), ); }
Reexports
pub use compare::ComparableSnapshot; |
Modules
compare |
Routines for comparison between 2 states. |
Macros
txvec |
Creates a |
Structs
TestKit |
Testkit for testing blockchain services. It offers simple network configuration emulation (with no real network setup). |
TestKitApi |
API encapsulation for the testkit. Allows to execute and synchronously retrieve results for REST-ful endpoints of services. |
TestKitBuilder |
Builder for |
TestNetwork |
Emulated test network. |
TestNetworkConfiguration |
A configuration of the test network. |
TestNode |
An emulated node in the test network. |
Enums
ApiKind |
Kind of public or private REST API of an Exonum node. |