merkletree

package module
v0.0.0-...-416581a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2024 License: GPL-3.0 Imports: 6 Imported by: 1

README

merkletree

merkle tree in cortex storage

       |------[1]
    [73]
       |------[0]




                |------[2]
       |------[140]
                |------[2]
    [228]
                |------[1]
       |------[73]
                |------[0]




                |------[3]
       |------[77]
                |------[2]
    [82]
                |------[1]
       |------[73]
                |------[0]




                         |------[4]
                |------[15]
                         |------[4]
       |------[241]
                         |------[4]
                |------[15]
                         |------[4]
    [35]
                         |------[3]
                |------[77]
                         |------[2]
       |------[82]
                         |------[1]
                |------[73]
                         |------[0]




                         |------[5]
                |------[213]
                         |------[4]
       |------[166]
                         |------[5]
                |------[213]
                         |------[4]
    [16]
                         |------[3]
                |------[77]
                         |------[2]
       |------[82]
                         |------[1]
                |------[73]
                         |------[0]




                         |------[6]
                |------[15]
                         |------[6]
       |------[102]
                         |------[5]
                |------[213]
                         |------[4]
    [214]
                         |------[3]
                |------[77]
                         |------[2]
       |------[82]
                         |------[1]
                |------[73]
                         |------[0]




                         |------[7]
                |------[199]
                         |------[6]
       |------[238]
                         |------[5]
                |------[213]
                         |------[4]
    [51]
                         |------[3]
                |------[77]
                         |------[2]
       |------[82]
                         |------[1]
                |------[73]
                         |------[0]




                                  |------[8]
                         |------[205]
                                  |------[8]
                |------[41]
                                  |------[8]
                         |------[205]
                                  |------[8]
       |------[238]
                                  |------[8]
                         |------[205]
                                  |------[8]
                |------[41]
                                  |------[8]
                         |------[205]
                                  |------[8]
    [20]
                                  |------[7]
                         |------[199]
                                  |------[6]
                |------[238]
                                  |------[5]
                         |------[213]
                                  |------[4]
       |------[51]
                                  |------[3]
                         |------[77]
                                  |------[2]
                |------[82]
                                  |------[1]
                         |------[73]
                                  |------[0]




                                  |------[9]
                         |------[138]
                                  |------[8]
                |------[86]
                                  |------[9]
                         |------[138]
                                  |------[8]
       |------[133]
                                  |------[9]
                         |------[138]
                                  |------[8]
                |------[86]
                                  |------[9]
                         |------[138]
                                  |------[8]
    [179]
                                  |------[7]
                         |------[199]
                                  |------[6]
                |------[238]
                                  |------[5]
                         |------[213]
                                  |------[4]
       |------[51]
                                  |------[3]
                         |------[77]
                                  |------[2]
                |------[82]
                                  |------[1]
                         |------[73]
                                  |------[0] 

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockContent

type BlockContent struct {
	// contains filtered or unexported fields
}

func NewContent

func NewContent(x string, n uint64) *BlockContent

func (*BlockContent) CalculateHash

func (t *BlockContent) CalculateHash() ([]byte, error)

func (*BlockContent) Equals

func (t *BlockContent) Equals(other Content) (bool, error)

func (*BlockContent) N

func (t *BlockContent) N() uint64

type Content

type Content interface {
	CalculateHash() ([]byte, error)
	Equals(other Content) (bool, error)
}

Content represents the data that is stored and verified by the tree. A type that implements this interface can be used as an item in the tree.

type MerkleTree

type MerkleTree struct {
	Root *Node

	Leafs []*Node
	// contains filtered or unexported fields
}

MerkleTree is the container for the tree. It holds a pointer to the root of the tree, a list of pointers to the leaf nodes, and the merkle root.

func NewTree

func NewTree(cs []Content) (*MerkleTree, error)

NewTree creates a new Merkle Tree using the content cs.

func (*MerkleTree) AddNode

func (m *MerkleTree) AddNode(c Content) error

func (*MerkleTree) GetMerklePath

func (m *MerkleTree) GetMerklePath(content Content) ([][]byte, []int64, error)

GetMerklePath: Get Merkle path and indexes(left leaf or right leaf)

func (*MerkleTree) MerkleRoot

func (m *MerkleTree) MerkleRoot() []byte

MerkleRoot returns the unverified Merkle Root (hash of the root node) of the tree.

func (*MerkleTree) Purge

func (m *MerkleTree) Purge()

func (*MerkleTree) RebuildTree

func (m *MerkleTree) RebuildTree() error

RebuildTree is a helper function that will rebuild the tree reusing only the content that it holds in the leaves.

func (*MerkleTree) RebuildTreeWith

func (m *MerkleTree) RebuildTreeWith(cs []Content) error

RebuildTreeWith replaces the content of the tree and does a complete rebuild; while the root of the tree will be replaced the MerkleTree completely survives this operation. Returns an error if the list of content cs contains no entries.

func (*MerkleTree) String

func (m *MerkleTree) String() string

String returns a string representation of the tree. Only leaf nodes are included in the output.

func (*MerkleTree) VerifyContent

func (m *MerkleTree) VerifyContent(content Content) (bool, error)

VerifyContent indicates whether a given content is in the tree and the hashes are valid for that content. Returns true if the expected Merkle Root is equivalent to the Merkle root calculated on the critical path for a given content. Returns true if valid and false otherwise.

func (*MerkleTree) VerifyTree

func (m *MerkleTree) VerifyTree() (bool, error)

VerifyTree verify tree validates the hashes at each level of the tree and returns true if the resulting hash at the root of the tree matches the resulting root hash; returns false otherwise.

type Node

type Node struct {
	Tree   *MerkleTree
	Parent *Node
	Left   *Node
	Right  *Node

	Hash []byte
	C    Content
	// contains filtered or unexported fields
}

Node represents a node, root, or leaf in the tree. It stores pointers to its immediate relationships, a hash, the content stored if it is a leaf, and other metadata.

func (*Node) String

func (n *Node) String() string

String returns a string representation of the node.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL