Skip to main content

PowStrategy

Trait PowStrategy 

Source
pub trait PowStrategy: Clone {
    type Nonce: From<u64> + Into<u64> + Encoding + FromNarg;

    // Required methods
    fn challenge_len(&self) -> usize;
    fn accepts(&self, nonce: u64, challenge: &[u8]) -> bool;

    // Provided methods
    fn enabled(&self) -> bool { ... }
    fn last_nonce(&self) -> u64 { ... }
    fn solve<E: Fn(&[u8], &mut [u8]) + Clone>(self, evaluate: E) -> Option<u64> { ... }
    fn solve_parallel<E>(self, evaluate: E) -> Option<u64>
       where Self: Send,
             E: Fn(&[u8], &mut [u8]) + Clone + Send { ... }
}
Expand description

The configuration for a proof of work.

A PowStrategy contains the parameters for the proof of work. Parameters and acceptance must remain equivalent across calls and clones.

§Security

When work is requested, the grinding is expected to amplify soundness for the current round. In particular, trait implementors must be wary of preprocessing attacks that might occur when seeding the PoW with a fixed seed, or creating inadvertly a new round.

Failed nonce checks must use Transcript::check to permanently reject the verifier.

The difficulty, placement and solution codec must be fixed by the protocol and properly accounted for in the session identifier.

The helper absorbs the nonce before deriving challenge_len() verifier bytes.

Required Associated Types§

Source

type Nonce: From<u64> + Into<u64> + Encoding + FromNarg

The canonical, self-delimiting nonce codec. Integers use fixed-width encoding.

Conversion to and from u64 must round-trip every permitted nonce.

Required Methods§

Source

fn challenge_len(&self) -> usize

Number of verifier bytes derived after each candidate nonce.

This must be positive when work is enabled and fixed by the protocol.

Source

fn accepts(&self, nonce: u64, challenge: &[u8]) -> bool

Test the nonce and its verifier bytes without changing the puzzle. Acceptance must not depend on evaluation order. challenge must contain exactly Self::challenge_len bytes.

For seeded PoW these bytes are the seed, derived anew for every nonce. This predicate must enforce the protocol’s difficulty; it cannot establish a soundness claim by itself.

Provided Methods§

Source

fn enabled(&self) -> bool

Whether to exchange a nonce and derive check bytes.

Returning false skips all work and immediately derives the protected message.

Source

fn last_nonce(&self) -> u64

The inclusive nonce bound, starting at zero.

Source

fn solve<E: Fn(&[u8], &mut [u8]) + Clone>(self, evaluate: E) -> Option<u64>

Return the least accepted nonce in 0..=last_nonce(), or None on exhaustion.

Synchronous and potentially blocking. Implementations must not panic for valid strategy parameters and a non-panicking evaluator. evaluate must fill its output with verifier bytes after the encoded nonce; calls and clones must agree. It must not advance the live transcript.

Source

fn solve_parallel<E>(self, evaluate: E) -> Option<u64>
where Self: Send, E: Fn(&[u8], &mut [u8]) + Clone + Send,

Return the same least nonce as Self::solve, or None on exhaustion.

Synchronous and potentially blocking. Default implementations use Rayon with parallel and otherwise calls Self::solve. Parallelism may reduce elapsed time but can evaluate extra candidates; speedup is not guaranteed. Only strategy and evaluator state require Send, without Sync. Overrides must not panic for valid parameters and a non-panicking evaluator. Callback panics propagate; Rayon may panic if pool initialization fails.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§