use exonum::{
crypto::KeyPair,
merkledb::ObjectHash,
runtime::{ExecutionContext, ExecutionError, SnapshotExt},
};
use exonum_derive::*;
use exonum_rust_runtime::{spec::Spec, Service};
use exonum_testkit::TestKitBuilder;
#[exonum_interface(auto_ids)]
trait TimestampingInterface<Ctx> {
type Output;
fn timestamp(&self, ctx: Ctx, arg: String) -> Self::Output;
}
#[derive(Debug, ServiceDispatcher, ServiceFactory)]
#[service_factory(artifact_name = "timestamping", artifact_version = "1.0.0")]
#[service_dispatcher(implements("TimestampingInterface"))]
struct TimestampingService;
impl TimestampingInterface<ExecutionContext<'_>> for TimestampingService {
type Output = Result<(), ExecutionError>;
fn timestamp(&self, _ctx: ExecutionContext<'_>, _arg: String) -> Self::Output {
Ok(())
}
}
impl Service for TimestampingService {}
fn main() {
let instance_id = 512;
let service = TimestampingService;
let mut testkit = TestKitBuilder::validator()
.with_validators(4)
.with(Spec::new(service).with_instance(instance_id, "timestamping", ()))
.build();
let keypair = KeyPair::random();
let tx1 = keypair.timestamp(instance_id, "Down To Earth".to_owned());
let tx2 = keypair.timestamp(instance_id, "Cry Over Spilt Milk".to_owned());
let tx3 = keypair.timestamp(instance_id, "Dropping Like Flies".to_owned());
let block = testkit.create_block_with_transactions(vec![tx1.clone(), tx2.clone(), tx3.clone()]);
assert_eq!(block.len(), 3);
assert!(block.iter().all(|transaction| transaction.status().is_ok()));
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()));
}