Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
examples Small fixes in exonum-merkledb (#1303) May 1, 2019
src
tests
Cargo.toml
LICENSE
README.md Release 0.11 (#1273) Mar 15, 2019
build.rs

README.md

exonum-time

Travis Build Status CircleCI Build Status Docs.rs License: Apache-2.0 rust 1.33.0+ required

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.

Usage

Include exonum-time as a dependency in your Cargo.toml:

[dependencies]
exonum = "0.11.0"
exonum-time = "0.11.0"

Add the time oracle service to the blockchain in the main project file:

extern crate exonum;
extern crate exonum_time;

use exonum::helpers::fabric::NodeBuilder;
use exonum_time::TimeServiceFactory;

fn main() {
    exonum::helpers::init_logger().unwrap();
    NodeBuilder::new()
        .with_service(Box::new(TimeServiceFactory))
        .run();
}

Importing the data schema

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):

#[derive(Serialize, Deserialize, Debug, Clone, ProtobufConvert)]
#[exonum(pb = "proto::TimeTx")]
/// Transaction, which must be executed no later than the specified time (field `time`).
struct TimeTx {
    time: DateTime<Utc>,
}

#[derive(Serialize, Deserialize, Debug, Clone, TransactionSet)]
enum TimeTransactions {
    TimeTx(TimeTx),
}

impl Transaction for TimeTx {
    fn execute(&self, view: &mut Fork) {
        // Import schema.
        let time_schema = exonum_time::TimeSchema::new(&view);
        // The time in the transaction should be less than in the blockchain.
        match time_schema.time().get() {
            Some(current_time) if current_time <= self.time => {
                // Execute transaction business logic.
            }
            _ => {}
        }
    }
}

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:

let time_schema = exonum_time::TimeSchema::new(&view);
// Gets the times of all validators.
let validators_time = time_schema.validators_time();
// Gets the time of validator with a public key equal to `public_key`.
let validator_time = time_schema.validators_time().get(&public_key);

Further Reading

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.

Other languages support

License

exonum-time is licensed under the Apache License (Version 2.0). See LICENSE for details.

You can’t perform that action at this time.