Exonum-time is a time oracle service for Exonum blockchain framework. This service allows to determine time, import it from the external world to the blockchain and keep its current value in the blockchain.
Include exonum-time
as a dependency in your Cargo.toml
:
toml
[dependencies]
exonum = "0.13.0-rc.2"
exonum-cli = "0.13.0-rc.2"
exonum-time = "0.13.0-rc.2"
Add the time oracle service to the blockchain in the main project file:
```rust extern crate exonum; extern crate exonumcli; extern crate exonumtime;
use exonumcli::NodeBuilder; use exonumtime::TimeServiceFactory; use simple_service::MarkerService;
fn main() -> Result<(), failure::Error> { exonum::helpers::initlogger().unwrap(); NodeBuilder::new() .withservice(TimeServiceFactory::default()) .with_service(MarkerService) .run() } ```
Typical usage of the service boils down to importing the schema and calling its
time()
or validators_time()
methods.
Below is an example of a method for processing a transaction, which must be executed no later than the specified time (this time is written in the transaction body in a separate field):
``rust
/// The argument of the
MarkerInterface::mark` method.
pub struct TxMarker {
mark: i32,
time: DateTime
/// Marker service transactions interface definition.
pub trait MarkerTransactions {
/// Transaction, which must be executed no later
/// than the specified time (field time
).
fn mark(&self, context: CallContext<'_>, arg: TxMarker) -> Result<(), ExecutionError>;
}
artifact_name = "marker",
artifact_version = "0.1.0",
proto_sources = "proto"
)]
struct MarkerService;
/// Marker service database schema.
pub struct MarkerSchema
impl
impl MarkerTransactions for MarkerService {
fn mark(
&self,
context: CallContext<'_>,
arg: TxMarker
) -> Result<(), ExecutionError> {
let author = context
.caller()
.author()
.expect("Wrong TxMarker
initiator");
let data = context.data();
let time_service_data = data
.for_service(TIME_SERVICE_NAME)
.expect("No time service data");
let time = TimeSchema::new(time_service_data).time.get();
match time {
Some(current_time) if current_time <= arg.time => {
let mut schema = MarkerSchema::new(context.service_data());
schema.marks.put(&author, arg.mark);
}
_ => {}
}
Ok(())
}
}
impl Service for MarkerService {
fn statehash(&self, data: BlockchainData<&dyn Snapshot>) -> Vec
See the full implementation of the service, which uses the time oracle.
You can get the time of each validator node in the same manner the consolidated time of the system is obtained:
rust
// Gets the data of time service instance
let data = context.data();
let time_service_data = data
.for_service("time_service_name")
.expect("No time service data");
let time_schema = TimeSchema::new(time_service_data);
// Gets the times of all validators.
let validators_time = time_schema.time.get();
// Gets the time of validator with a public key equal to `public_key`.
let validator_time = time_schema.validators_times.get(&public_key);
Consult the crate docs for more details about the service Rust API, and the service description in Exonum docs for a more high-level perspective, in particular, the design rationale and the proof of correctness.
exonum-time
is licensed under the Apache License (Version 2.0).
See LICENSE for details.