SortitionSumTreeFactory

Git Source

Author: Enrique Piqueras - epiquerass@gmail.com

A factory of trees that keep track of staked values for sortition.

Functions

createTree

Public

Create a sortition sum tree at the specified key.

function createTree(SortitionSumTrees storage self, bytes32 _key, uint256 _K) public;

Parameters

NameTypeDescription
selfSortitionSumTrees
_keybytes32The key of the new tree.
_Kuint256The number of children each node in the tree should have.

set

Set a value of a tree.

function set(SortitionSumTrees storage self, bytes32 _key, uint256 _value, bytes32 _ID) public;

Parameters

NameTypeDescription
selfSortitionSumTrees
_keybytes32The key of the tree.
_valueuint256The new value.
_IDbytes32The ID of the value. O(log_k(n)) where k is the maximum number of childs per node in the tree, and n is the maximum number of nodes ever appended.

queryLeafs

Public Views

Query the leaves of a tree. Note that if startIndex == 0, the tree is empty and the root node will be returned.

function queryLeafs(SortitionSumTrees storage self, bytes32 _key, uint256 _cursor, uint256 _count)
    public
    view
    returns (uint256 startIndex, uint256[] memory values, bool hasMore);

Parameters

NameTypeDescription
selfSortitionSumTrees
_keybytes32The key of the tree to get the leaves from.
_cursoruint256The pagination cursor.
_countuint256The number of items to return.

Returns

NameTypeDescription
startIndexuint256The index at which leaves start.
valuesuint256[]The values of the returned leaves.
hasMoreboolWhether there are more for pagination. O(n) where n is the maximum number of nodes ever appended.

draw

Draw an ID from a tree using a number. Note that this function reverts if the sum of all values in the tree is 0.

function draw(SortitionSumTrees storage self, bytes32 _key, uint256 _drawnNumber) public view returns (bytes32 ID);

Parameters

NameTypeDescription
selfSortitionSumTrees
_keybytes32The key of the tree.
_drawnNumberuint256The drawn number.

Returns

NameTypeDescription
IDbytes32The drawn ID. O(k * log_k(n)) where k is the maximum number of childs per node in the tree, and n is the maximum number of nodes ever appended.

stakeOf

Gets a specified ID's associated value.

function stakeOf(SortitionSumTrees storage self, bytes32 _key, bytes32 _ID) public view returns (uint256 value);

Parameters

NameTypeDescription
selfSortitionSumTrees
_keybytes32The key of the tree.
_IDbytes32The ID of the value.

Returns

NameTypeDescription
valueuint256The associated value.

updateParents

Private

Update all the parents of a node.

function updateParents(
    SortitionSumTrees storage self,
    bytes32 _key,
    uint256 _treeIndex,
    bool _plusOrMinus,
    uint256 _value
) private;

Parameters

NameTypeDescription
selfSortitionSumTrees
_keybytes32The key of the tree to update.
_treeIndexuint256The index of the node to start from.
_plusOrMinusboolWether to add (true) or substract (false).
_valueuint256The value to add or substract. O(log_k(n)) where k is the maximum number of childs per node in the tree, and n is the maximum number of nodes ever appended.

Structs

SortitionSumTree

Structs

struct SortitionSumTree {
    uint256 K;
    uint256[] stack;
    uint256[] nodes;
    mapping(bytes32 => uint256) IDsToNodeIndexes;
    mapping(uint256 => bytes32) nodeIndexesToIDs;
}

SortitionSumTrees

Storage

struct SortitionSumTrees {
    mapping(bytes32 => SortitionSumTree) sortitionSumTrees;
}