2018-02-16 12:32:47 -07:00
|
|
|
|
[](https://crates.io/crates/silk)
|
|
|
|
|
[](https://docs.rs/silk)
|
2018-02-16 11:49:21 -07:00
|
|
|
|
[](https://travis-ci.org/loomprotocol/silk)
|
|
|
|
|
[](https://codecov.io/gh/loomprotocol/silk)
|
2018-02-14 15:25:49 -07:00
|
|
|
|
|
2018-02-16 12:44:00 -07:00
|
|
|
|
# Silk, a silky smooth implementation of the Loom specification
|
2018-02-15 13:59:33 -07:00
|
|
|
|
|
2018-02-16 11:30:36 -07:00
|
|
|
|
Loom is a new achitecture for a high performance blockchain. Its whitepaper boasts a theoretical
|
2018-02-16 12:51:57 -07:00
|
|
|
|
throughput of 710k transactions per second on a 1 gbps network. The specification is implemented
|
|
|
|
|
in two git repositories. Reserach is performed in the loom repository. That work drives the
|
|
|
|
|
Loom specification forward. This repository, on the other hand, aims to implement the specification
|
|
|
|
|
as-is. We care a great deal about quality, clarity and short learning curve. We avoid the use
|
2018-02-16 12:53:44 -07:00
|
|
|
|
of `unsafe` Rust and write tests for *everything*. Optimizations are only added when
|
2018-02-21 10:05:55 -07:00
|
|
|
|
corresponding benchmarks are also added that demonstrate real performance boosts. We expect the
|
2018-02-16 12:51:57 -07:00
|
|
|
|
feature set here will always be a ways behind the loom repo, but that this is an implementation
|
|
|
|
|
you can take to the bank, literally.
|
2018-02-15 16:09:11 -07:00
|
|
|
|
|
2018-02-19 09:19:26 -07:00
|
|
|
|
# Usage
|
|
|
|
|
|
2018-02-19 12:12:45 -07:00
|
|
|
|
Add the latest [silk package](https://crates.io/crates/silk) to the `[dependencies]` section
|
2018-02-19 09:19:26 -07:00
|
|
|
|
of your Cargo.toml.
|
|
|
|
|
|
|
|
|
|
Create a *Historian* and send it *events* to generate an *event log*, where each log *entry*
|
|
|
|
|
is tagged with the historian's latest *hash*. Then ensure the order of events was not tampered
|
|
|
|
|
with by verifying each entry's hash can be generated from the hash in the previous entry:
|
|
|
|
|
|
2018-02-24 11:09:00 -07:00
|
|
|
|

|
2018-02-21 09:33:46 -07:00
|
|
|
|
|
2018-02-19 09:19:26 -07:00
|
|
|
|
```rust
|
2018-02-19 12:00:56 -07:00
|
|
|
|
extern crate silk;
|
2018-02-19 09:19:26 -07:00
|
|
|
|
|
2018-02-19 12:00:56 -07:00
|
|
|
|
use silk::historian::Historian;
|
2018-02-19 16:48:29 -07:00
|
|
|
|
use silk::log::{verify_slice, Entry, Event, Sha256Hash};
|
2018-02-21 11:52:03 -07:00
|
|
|
|
use std::thread::sleep;
|
|
|
|
|
use std::time::Duration;
|
2018-02-19 12:25:57 -07:00
|
|
|
|
use std::sync::mpsc::SendError;
|
2018-02-19 09:19:26 -07:00
|
|
|
|
|
2018-02-19 12:25:57 -07:00
|
|
|
|
fn create_log(hist: &Historian) -> Result<(), SendError<Event>> {
|
2018-02-21 11:52:03 -07:00
|
|
|
|
sleep(Duration::from_millis(15));
|
2018-02-24 11:15:03 -07:00
|
|
|
|
let data = Sha256Hash::default();
|
|
|
|
|
hist.sender.send(Event::Discovery { data })?;
|
2018-02-21 11:52:03 -07:00
|
|
|
|
sleep(Duration::from_millis(10));
|
2018-02-19 12:25:57 -07:00
|
|
|
|
Ok(())
|
2018-02-19 12:00:56 -07:00
|
|
|
|
}
|
2018-02-19 09:19:26 -07:00
|
|
|
|
|
2018-02-19 12:00:56 -07:00
|
|
|
|
fn main() {
|
2018-02-19 16:48:29 -07:00
|
|
|
|
let seed = Sha256Hash::default();
|
2018-02-21 11:52:03 -07:00
|
|
|
|
let hist = Historian::new(&seed, Some(10));
|
2018-02-19 12:25:57 -07:00
|
|
|
|
create_log(&hist).expect("send error");
|
|
|
|
|
drop(hist.sender);
|
|
|
|
|
let entries: Vec<Entry> = hist.receiver.iter().collect();
|
2018-02-19 12:00:56 -07:00
|
|
|
|
for entry in &entries {
|
|
|
|
|
println!("{:?}", entry);
|
|
|
|
|
}
|
2018-02-20 13:07:54 -07:00
|
|
|
|
|
|
|
|
|
// Proof-of-History: Verify the historian learned about the events
|
|
|
|
|
// in the same order they appear in the vector.
|
2018-02-19 16:48:29 -07:00
|
|
|
|
assert!(verify_slice(&entries, &seed));
|
2018-02-19 09:19:26 -07:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-19 12:00:56 -07:00
|
|
|
|
Running the program should produce a log similar to:
|
|
|
|
|
|
2018-02-19 16:48:29 -07:00
|
|
|
|
```rust
|
2018-02-19 16:53:58 -07:00
|
|
|
|
Entry { num_hashes: 0, end_hash: [0, ...], event: Tick }
|
2018-02-24 11:15:03 -07:00
|
|
|
|
Entry { num_hashes: 2, end_hash: [67, ...], event: Discovery { data: [37, ...] } }
|
2018-02-21 10:05:55 -07:00
|
|
|
|
Entry { num_hashes: 3, end_hash: [123, ...], event: Tick }
|
2018-02-19 12:00:56 -07:00
|
|
|
|
```
|
|
|
|
|
|
2018-02-20 13:07:54 -07:00
|
|
|
|
Proof-of-History
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
Take note of the last line:
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
assert!(verify_slice(&entries, &seed));
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
[It's a proof!](https://en.wikipedia.org/wiki/Curry–Howard_correspondence) For each entry returned by the
|
|
|
|
|
historian, we can verify that `end_hash` is the result of applying a sha256 hash to the previous `end_hash`
|
|
|
|
|
exactly `num_hashes` times, and then hashing then event data on top of that. Because the event data is
|
|
|
|
|
included in the hash, the events cannot be reordered without regenerating all the hashes.
|
2018-02-19 12:00:56 -07:00
|
|
|
|
|
2018-02-15 16:09:11 -07:00
|
|
|
|
# Developing
|
|
|
|
|
|
2018-02-16 09:38:12 -08:00
|
|
|
|
Building
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
Install rustc, cargo and rustfmt:
|
2018-02-15 16:09:11 -07:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
$ curl https://sh.rustup.rs -sSf | sh
|
2018-02-16 09:38:12 -08:00
|
|
|
|
$ source $HOME/.cargo/env
|
|
|
|
|
$ rustup component add rustfmt-preview
|
2018-02-15 16:09:11 -07:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Download the source code:
|
|
|
|
|
|
|
|
|
|
```bash
|
2018-02-16 11:49:21 -07:00
|
|
|
|
$ git clone https://github.com/loomprotocol/silk.git
|
|
|
|
|
$ cd silk
|
2018-02-15 16:09:11 -07:00
|
|
|
|
```
|
|
|
|
|
|
2018-02-16 09:38:12 -08:00
|
|
|
|
Testing
|
|
|
|
|
---
|
|
|
|
|
|
2018-02-15 16:09:11 -07:00
|
|
|
|
Run the test suite:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
cargo test
|
|
|
|
|
```
|
2018-02-16 09:38:12 -08:00
|
|
|
|
|
|
|
|
|
Benchmarking
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
First install the nightly build of rustc. `cargo bench` requires unstable features:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
$ rustup install nightly
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Run the benchmarks:
|
|
|
|
|
|
|
|
|
|
```bash
|
2018-02-19 16:51:32 -07:00
|
|
|
|
$ cargo +nightly bench --features="asm,unstable"
|
2018-02-16 09:38:12 -08:00
|
|
|
|
```
|