Add blockchain example
relax type bounds on struct definition
This commit is contained in:
parent
b3175a79b1
commit
75bafcf99a
4 changed files with 25 additions and 4 deletions
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "pow"
|
||||
version = "0.1.4"
|
||||
version = "0.1.5"
|
||||
authors = ["Andrew Dirksen <andrew@dirksen.com>"]
|
||||
description = """
|
||||
Generate or verify sha256 based proofs of work over arbitrary typed data.
|
||||
|
|
10
readme.md
10
readme.md
|
@ -30,6 +30,16 @@ let pw = Pow::prove_work(&now, difficulty).unwrap();
|
|||
assert!(pw.score(&now).unwrap() >= difficulty);
|
||||
```
|
||||
|
||||
Define a blockchain block.
|
||||
|
||||
```rust
|
||||
struct Block<T> {
|
||||
prev: [u8; 32], // hash of last block
|
||||
payload: T, // generic data
|
||||
proof_of_work: Pow<([u8; 32], T)>,
|
||||
}
|
||||
```
|
||||
|
||||
# Score scheme
|
||||
|
||||
To score a proof of work for a given (target, Pow) pair:
|
||||
|
|
15
src/lib.rs
15
src/lib.rs
|
@ -33,7 +33,18 @@
|
|||
//! let pw = Pow::prove_work(&now, difficulty).unwrap();
|
||||
//! assert!(pw.score(&now).unwrap() >= difficulty);
|
||||
//! ```
|
||||
|
||||
//!
|
||||
//! Define a blockchain block.
|
||||
//!
|
||||
//! ```rust
|
||||
//! # use pow::Pow;
|
||||
//! struct Block<T> {
|
||||
//! prev: [u8; 32], // hash of last block
|
||||
//! payload: T, // generic data
|
||||
//! proof_of_work: Pow<([u8; 32], T)>,
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! # Score scheme
|
||||
//!
|
||||
//! To score a proof of work for a given (target, Pow) pair:
|
||||
|
@ -91,7 +102,7 @@
|
|||
//! let m = u128::max_value();
|
||||
//! if difficulty == m {
|
||||
//! return m;
|
||||
//! }
|
||||
//! }
|
||||
//! m / (m - difficulty)
|
||||
//! }
|
||||
//! ```
|
||||
|
|
|
@ -6,7 +6,7 @@ const SALT: &str = "35af8f4890981391c191e6df45b5f780812ddf0213f29299576ac1c98e18
|
|||
|
||||
/// Proof of work over concrete type T. T can be any type that implements serde::Serialize.
|
||||
#[derive(Serialize, Deserialize, PartialEq, Clone, Copy, Debug)]
|
||||
pub struct Pow<T: Serialize> {
|
||||
pub struct Pow<T> {
|
||||
proof: u128,
|
||||
_spook: PhantomData<T>,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue