Liking cljdoc? Tell your friends :D

urd.consensus.algorithms.snowtree

The Snowball Tree: what avalanchego actually runs under Snowman.

snow/consensus/snowball/tree.go. A patricia trie over the BITS of the candidate ids, deciding the winner one bit-range at a time rather than deciding among the candidates as a set.

Why this exists and the flat form does not suffice

Flat.RecordPoll (flat.go:29-33) is three lines:

pollMode, numVotes := votes.Mode()
f.Nnary.RecordPoll(numVotes, pollMode)

The plurality choice and its count, fed to one n-ary instance. That is what urd's Snowman did, exactly, and NewFlat has no non-test caller anywhere in avalanchego.

The Tree instead feeds each node votes.Len() -- EVERY vote that reached it, whatever choice it names. So votes for two siblings COMBINE at every node above the bit where their ids diverge. Three children X, Y, Z where Y and Z share a leading bit that X does not: a 2/2 split between Y and Z still delivers 4 votes to the not-X side of the top binary node. The flat form sees a plurality of 2 and may not clear alpha at all. This is a real behavioural difference, not a change of representation, and it is why the reference can make progress on a contested slot that the flat form stalls on.

Bit order

avalanchego indexes bit i as bit i mod 8 of byte i div 8, least significant first (ids/id.go:140-155). We index bit i as bit i mod 4 of hex digit i div 4, least significant first.

The difference is immaterial and worth saying why, because it looks like the kind of thing that would matter. The Tree is defined over an arbitrary but FIXED total order on the bits of an id; any such order gives the same algorithm. What changes is which pairs of ids happen to share a prefix, and since ids here are hasch/uuid digests that is uniformly random under either order. What we give up is the ability to replay avalanchego's own test vectors, which are literal ids.ID byte arrays -- and those were never replayable anyway, since urd's choices are not 32-byte avalanche ids. The tests port the structural claims instead.

What a node holds

{:kind :unary
 :preference       key
 :decided-prefix   the first bit this node covers
 :common-prefix    one past the last bit it covers
 :snow             a unary instance
 :should-reset     bool
 :child            node or nil}

{:kind :binary
 :preferences  [key-when-bit-0 key-when-bit-1]
 :bit          the bit index this node decides
 :snow         a binary instance
 :should-reset [bool bool]
 :children     [node-or-nil node-or-nil]}

Keys are fixed-width lowercase hex strings. The tree carries :choices, from key back to whatever the caller called a choice, so a caller keeps using its own ids and never sees a digest.

The Snowball Tree: what avalanchego actually runs under Snowman.

`snow/consensus/snowball/tree.go`. A patricia trie over the BITS of the
candidate ids, deciding the winner one bit-range at a time rather than
deciding among the candidates as a set.

## Why this exists and the flat form does not suffice

`Flat.RecordPoll` (`flat.go:29-33`) is three lines:

    pollMode, numVotes := votes.Mode()
    f.Nnary.RecordPoll(numVotes, pollMode)

The plurality choice and its count, fed to one n-ary instance. That is what
urd's Snowman did, exactly, and `NewFlat` has no non-test caller anywhere in
avalanchego.

The Tree instead feeds each node `votes.Len()` -- EVERY vote that reached it,
whatever choice it names. So votes for two siblings COMBINE at every node
above the bit where their ids diverge. Three children X, Y, Z where Y and Z
share a leading bit that X does not: a 2/2 split between Y and Z still
delivers 4 votes to the not-X side of the top binary node. The flat form sees
a plurality of 2 and may not clear alpha at all. This is a real behavioural
difference, not a change of representation, and it is why the reference can
make progress on a contested slot that the flat form stalls on.

## Bit order

avalanchego indexes bit `i` as bit `i mod 8` of byte `i div 8`, least
significant first (`ids/id.go:140-155`). We index bit `i` as bit `i mod 4` of
hex digit `i div 4`, least significant first.

The difference is immaterial and worth saying why, because it looks like the
kind of thing that would matter. The Tree is defined over an arbitrary but
FIXED total order on the bits of an id; any such order gives the same
algorithm. What changes is which pairs of ids happen to share a prefix, and
since ids here are `hasch/uuid` digests that is uniformly random under either
order. What we give up is the ability to replay avalanchego's own test
vectors, which are literal `ids.ID` byte arrays -- and those were never
replayable anyway, since urd's choices are not 32-byte avalanche ids. The
tests port the structural claims instead.

## What a node holds

    {:kind :unary
     :preference       key
     :decided-prefix   the first bit this node covers
     :common-prefix    one past the last bit it covers
     :snow             a unary instance
     :should-reset     bool
     :child            node or nil}

    {:kind :binary
     :preferences  [key-when-bit-0 key-when-bit-1]
     :bit          the bit index this node decides
     :snow         a binary instance
     :should-reset [bool bool]
     :children     [node-or-nil node-or-nil]}

Keys are fixed-width lowercase hex strings. The tree carries `:choices`, from
key back to whatever the caller called a choice, so a caller keeps using its
own ids and never sees a digest.
raw docstring

avalanche-keyclj/s

(avalanche-key id)

Convert a 32-byte lowercase hex ID to the trie key for AvalancheGo's least-significant-bit-first byte order.

Convert a 32-byte lowercase hex ID to the trie key for AvalancheGo's
least-significant-bit-first byte order.
sourceraw docstring

bit-atclj/s

(bit-at k i)

Bit i of key k. See the namespace docstring on ordering.

Bit `i` of key `k`. See the namespace docstring on ordering.
sourceraw docstring

describeclj/s

(describe t)

A nested summary of the trie, for tests and for reading a failure.

Printable upstream (tree.go:190-200, :556-565). Structure is what is being ported here, so some tests assert on it directly rather than only on behaviour.

A nested summary of the trie, for tests and for reading a failure.

`Printable` upstream (`tree.go:190-200`, `:556-565`). Structure is what is
being ported here, so some tests assert on it directly rather than only on
behaviour.
sourceraw docstring

equal-subset?clj/s

(equal-subset? start stop k1 k2)

Do k1 and k2 agree on every bit in [start, stop)?

ids/bits.go:21-65 does this with byte masks; a loop is the same relation and the widths here are 128 bits, reached only from add.

Do `k1` and `k2` agree on every bit in [start, stop)?

`ids/bits.go:21-65` does this with byte masks; a loop is the same relation and
the widths here are 128 bits, reached only from `add`.
sourceraw docstring

first-differenceclj/s

(first-difference start stop k1 k2)

The lowest index in [start, stop) at which k1 and k2 differ, or nil. ids/bits.go:67-130.

The lowest index in [start, stop) at which `k1` and `k2` differ, or nil.
`ids/bits.go:67-130`.
sourceraw docstring

num-bitsclj/s

(num-bits k)
source

treeclj/s

(tree params choice key)

A fresh Tree preferring choice, whose bit key is key.

The root is a unary node spanning the whole key: with one choice there is nothing to conflict on yet (tree.go:20-32).

A fresh Tree preferring `choice`, whose bit key is `key`.

The root is a unary node spanning the whole key: with one choice there is
nothing to conflict on yet (`tree.go:20-32`).
sourceraw docstring

tree-addclj/s

(tree-add t choice key)

Tree.Add (tree.go:59-65). Adding a choice already decided AGAINST -- one whose key disagrees with the preference somewhere in the settled prefix -- is a no-op, which is how a rejected branch stays rejected.

`Tree.Add` (`tree.go:59-65`). Adding a choice already decided AGAINST -- one
whose key disagrees with the preference somewhere in the settled prefix -- is
a no-op, which is how a rejected branch stays rejected.
sourceraw docstring

tree-finalized?clj/s

(tree-finalized? t)
source

tree-preferenceclj/s

(tree-preference t)
source

tree-record-pollclj/s

(tree-record-poll t votes)

Tree.RecordPoll (tree.go:67-87). votes is {choice -> weight}.

Votes for a branch the settled prefix has already rejected are dropped before anything sees them.

`Tree.RecordPoll` (`tree.go:67-87`). `votes` is {choice -> weight}.

Votes for a branch the settled prefix has already rejected are dropped before
anything sees them.
sourceraw docstring

tree-record-unsuccessful-pollclj/s

(tree-record-unsuccessful-poll t)

Tree.RecordUnsuccessfulPoll: lazy. The reset is handed to the nodes on the next poll rather than walked into them now.

`Tree.RecordUnsuccessfulPoll`: lazy. The reset is handed to the nodes on the
next poll rather than walked into them now.
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close