[−][src]Crate exonum_testkit
Testkit for Exonum blockchain framework, allowing to test service APIs synchronously and in the same process as the testkit.
Example
extern crate exonum; #[macro_use] extern crate exonum_derive; #[macro_use] extern crate exonum_testkit; extern crate serde_json; #[macro_use] extern crate serde_derive; extern crate failure; use serde_json::Value; use exonum::api::node::public::explorer::{BlocksQuery, BlocksRange, TransactionQuery}; use exonum::blockchain::{Block, Schema, Service, Transaction, TransactionContext, TransactionSet, ExecutionResult}; use exonum::crypto::{gen_keypair, Hash, PublicKey, SecretKey, CryptoHash}; use exonum::explorer::TransactionInfo; use exonum::helpers::Height; use exonum::messages::{Signed, RawTransaction, Message}; use exonum::storage::{Snapshot, Fork}; use exonum_testkit::{ApiKind, TestKitBuilder}; // Simple service implementation. const SERVICE_ID: u16 = 1; #[derive(Debug, Clone, Serialize, Deserialize, ProtobufConvert)] #[exonum(pb = "exonum_testkit::proto::examples::TxTimestamp")] struct TxTimestamp { message: String, } #[derive(Debug, Clone, Serialize, Deserialize, TransactionSet)] enum TimestampingTransactions { TxTimestamp(TxTimestamp), } impl TxTimestamp { fn sign(author: &PublicKey, msg: &str, key: &SecretKey) -> Signed<RawTransaction> { let tx = TxTimestamp{ message: msg.to_owned() }; Message::sign_transaction(tx, SERVICE_ID, *author, key) } } struct TimestampingService; impl Transaction for TxTimestamp { fn execute(&self, _context: TransactionContext) -> ExecutionResult { Ok(()) } } impl Service for TimestampingService { fn service_name(&self) -> &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>, failure::Error> { let tx = TimestampingTransactions::tx_from_raw(raw)?; Ok(tx.into()) } } 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::sign(&keypair.0, "Down To Earth", &keypair.1); let tx2 = TxTimestamp::sign(&keypair.0, "Cry Over Spilt Milk", &keypair.1); let tx3 = TxTimestamp::sign(&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::sign(&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 explorer_api = api.public(ApiKind::Explorer); let response: BlocksRange = explorer_api .query(&BlocksQuery { count: 10, ..Default::default() }) .get("v1/blocks") .unwrap(); let (blocks, range) = (response.blocks, response.range); assert_eq!(blocks.len(), 3); assert_eq!(range.start, Height(0)); assert_eq!(range.end, Height(3)); let info = explorer_api .query(&TransactionQuery::new(tx1.hash())) .get::<TransactionInfo>("v1/transactions") .unwrap(); }
Re-exports
pub use crate::compare::ComparableSnapshot; |
Modules
compare | Routines for comparison between 2 states. |
proto | Module of the rust-protobuf generated files. |
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. |