37 releases

0.16.2 Mar 16, 2023
0.16.0 Jun 13, 2022
0.15.0 Feb 8, 2022
0.14.4 Sep 2, 2021
0.4.0 Nov 23, 2018

#1431 in Magic Beans

Download history 33476/week @ 2023-12-13 23752/week @ 2023-12-20 13810/week @ 2023-12-27 27019/week @ 2024-01-03 31357/week @ 2024-01-10 36852/week @ 2024-01-17 35239/week @ 2024-01-24 30811/week @ 2024-01-31 38555/week @ 2024-02-07 40630/week @ 2024-02-14 38671/week @ 2024-02-21 37573/week @ 2024-02-28 33817/week @ 2024-03-06 33235/week @ 2024-03-13 42461/week @ 2024-03-20 27543/week @ 2024-03-27

143,178 downloads per month
Used in 164 crates (11 directly)

Apache-2.0

230KB
5.5K SLoC

finality-grandpa

crates.io link Build Status Coverage Status

GRANDPA, GHOST-based Recursive ANcestor Deriving Prefix Agreement, is a finality gadget for blockchains, implemented in Rust. It allows a set of nodes to come to BFT agreement on what is the canonical chain, which is produced by some external block production mechanism. It works under the assumption of a partially synchronous network model and with the presence of up to 1/3 Byzantine nodes.

Build & Test

The only dependency required to build and run the tests is to have a stable version of Rust installed.

git clone https://github.com/paritytech/finality-grandpa
cd finality-grandpa
cargo build
cargo test

Usage

Add this to your Cargo.toml:

[dependencies]
finality-grandpa = "0.16"

Features:

  • derive-codec - Derive Decode/Encode instances of parity-scale-codec for all the protocol messages.
  • test-helpers - Expose some opaque types for testing purposes.

Integration

This crate only implements the state machine for the GRANDPA protocol. In order to use this crate it is necessary to implement some traits to do the integration which are responsible for providing access to the underyling blockchain and setting up all the network communication.

Chain

The Chain trait allows the GRANDPA voter to check ancestry of a given block and also to query the best block in a given chain (which will be used for voting on).

Environment

The Environment trait defines the types that will be used for the input and output stream to receive and broadcast messages. It is also responsible for setting these up for a given round (through round_data), as well as timers which are used for timeouts in the protocol.

The trait exposes callbacks for the full lifecycle of a round:

  • proposed
  • prevoted
  • precommitted
  • completed

As well as callbacks for notifying about block finality and voter misbehavior (equivocations).

Substrate

The main user of this crate is Substrate and should be the main resource used to look into how the integration is done. The substrate-finality-grandpa crate should have most of the relevant integration code.

Most importantly this crate does not deal with authority set changes. It assumes that the set of authorities is always the same. Authority set handoffs are handled in Substrate by listening to signals emitted on the underlying blockchain.

Fuzzing

To run the fuzzing test harness you need to install either afl or cargo-fuzz (you'll need a nightly Rust toolchain for this):

cargo install cargo-fuzz
cargo install afl

libfuzzer

cargo fuzz run graph
cargo fuzz run round

afl

cd fuzz
cargo afl build --features afl --bin graph_afl
cargo afl build --features afl --bin round_afl

# create some random input
mkdir afl_in && dd if=/dev/urandom of=afl_in/seed bs=1024 count=4

cargo afl fuzz -i afl_in -o afl_out target/debug/graph_afl
cargo afl fuzz -i afl_in -o afl_out target/debug/round_afl

Resources

License

Usage is provided under the Apache License (Version 2.0). See LICENSE for the full details.

Dependencies

~0.8–8.5MB
~41K SLoC