[][src]Crate exonum_testkit

Testkit for Exonum blockchain framework, allowing to test service APIs synchronously and in the same process as the testkit.

Example

use exonum::{
    blockchain::{Block, Schema},
    crypto::{Hash, KeyPair},
    helpers::Height,
    runtime::{BlockchainData, SnapshotExt, ExecutionError},
};
use serde_derive::*;
use exonum_derive::*;
use exonum_merkledb::{ObjectHash, Snapshot};
use exonum_testkit::{ApiKind, Spec, TestKitBuilder};
use exonum_rust_runtime::{ServiceFactory, ExecutionContext, Service};

// Simple service implementation.

const SERVICE_ID: u32 = 1;

#[derive(Clone, Default, Debug, ServiceFactory, ServiceDispatcher)]
#[service_dispatcher(implements("TimestampingInterface"))]
#[service_factory(
    artifact_name = "timestamping",
    artifact_version = "1.0.0",
)]
struct TimestampingService;

impl Service for TimestampingService {}

#[exonum_interface]
pub trait TimestampingInterface<Ctx> {
    type Output;
    #[interface_method(id = 0)]
    fn timestamp(&self, _: Ctx, arg: String) -> Self::Output;
}

impl TimestampingInterface<ExecutionContext<'_>> for TimestampingService {
    type Output = Result<(), ExecutionError>;

    fn timestamp(&self, _: ExecutionContext<'_>, arg: String) -> Self::Output {
        Ok(())
    }
}

// Create testkit for network with four validators
// and add a builtin timestamping service with ID=1.
let service = Spec::new(TimestampingService)
    .with_instance(SERVICE_ID, "timestamping", ());
let mut testkit = TestKitBuilder::validator()
    .with_validators(4)
    .with(service)
    .build();

// Create a few transactions.
let keys = KeyPair::random();
let id = SERVICE_ID;
let tx1 = keys.timestamp(id, "Down To Earth".into());
let tx2 = keys.timestamp(id, "Cry Over Spilt Milk".into());
let tx3 = keys.timestamp(id, "Dropping Like Flies".into());
// Commit them into blockchain.
testkit.create_block_with_transactions(vec![
    tx1.clone(), tx2.clone(), tx3.clone()
]);

// Add a single transaction.
let tx4 = keys.timestamp(id, "Barking up the wrong tree".into());
testkit.create_block_with_transaction(tx4.clone());

// Check results with schema.
let snapshot = testkit.snapshot();
let schema = snapshot.for_core();
assert!(schema.transactions().contains(&tx1.object_hash()));
assert!(schema.transactions().contains(&tx2.object_hash()));
assert!(schema.transactions().contains(&tx3.object_hash()));
assert!(schema.transactions().contains(&tx4.object_hash()));

Re-exports

pub use exonum_explorer as explorer;

Modules

migrations

Testing framework for data migrations.

server

Types used by the testkit server.

Structs

RequestBuilder

An HTTP requests builder. This type can be used to send requests to the appropriate TestKitApi handlers.

Spec

Deploy specification for a Rust artifact. The spec can include zero or more instantiated services.

StoppedTestKit

Persistent state of an Exonum node allowing to emulate node restart.

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 asynchronously retrieve results for REST-ful endpoints of services.

TestKitApiClient

An asynchronous API client to make Requests with.

TestKitBuilder

Builder for TestKit.

TestNetwork

Emulated test network.

TestNode

An emulated node in the test network.

Enums

ApiKind

Kind of public or private REST API of an Exonum node.