merkle

package
v0.0.0-...-ee99c87 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2018 License: LGPL-3.0 Imports: 4 Imported by: 0

README

NOTE

This implementation is forked from cbergoon's implementation. I claim no authorship over this code apart from some minor modifications.

An implementation of a Merkle Tree written in Go. A Merkle Tree is a hash tree that provides an efficient way to verify the contents of a set data are present and untampered with.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Content

type Content interface {
	CalculateHash() common.Hash
	Equals(other Content) bool
}

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. Note, it is not thread safe

func NewTree

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

NewTree creates a new Merkle Tree using the content cs.

func (*MerkleTree) MerkleRoot

func (m *MerkleTree) MerkleRoot() common.Hash

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

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(expectedMerkleRoot []byte, content Content) bool

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

VerifyTree 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; otherwise, returns false.

Jump to

Keyboard shortcuts

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