merkle

package module
v0.0.0-...-f99036b Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2015 License: MIT Imports: 5 Imported by: 0

README

merkle

Build Status

Golang Merkle Tree Implementation, with hash.Hash interface for streaming support.

Docs

What's Next?

  • More tests and review
  • Streaming HashTreeer type, that can validate provided Nodes, for streaming validation

License

See LICENSE

Documentation

Index

Constants

View Source
const (
	// MaxBlockSize reasonable max byte size for blocks that are checksummed for
	// a Node
	MaxBlockSize = 1024 * 16
)

Variables

View Source
var (
	// DefaultHashMaker is for checksum of blocks and nodes
	DefaultHashMaker = func() hash.Hash { return sha1.New() }
)

Functions

func DetermineBlockSize

func DetermineBlockSize(blockSize int) int

DetermineBlockSize returns a reasonable block size to use, based on the provided size

Types

type ErrNoChecksumAvailable

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

ErrNoChecksumAvailable is for nodes that do not have the means to provide their checksum

func (ErrNoChecksumAvailable) Error

func (err ErrNoChecksumAvailable) Error() string

Error shows the message with information on the node

type HashMaker

type HashMaker func() hash.Hash

HashMaker produces a new has for use in making checksums

type HashTreeer

type HashTreeer interface {
	hash.Hash
	Treeer
}

HashTreeer can be used as a hash.Hash but also provide access to the Merkle tree internals

func NewHash

func NewHash(hm HashMaker, merkleBlockLength int) HashTreeer

NewHash provides a hash.Hash to generate a merkle.Tree checksum, given a HashMaker for the checksums of the blocks written and the blockSize of each block per node in the tree.

type Node

type Node struct {
	Parent, Left, Right *Node
	// contains filtered or unexported fields
}

Node is a fundamental part of the tree.

func NewNode

func NewNode() *Node

NewNode returns a new Node with the DefaultHashMaker for checksums

func NewNodeHash

func NewNodeHash(h HashMaker) *Node

NewNodeHash returns a new Node using the provided crypto.Hash for checksums

func NewNodeHashBlock

func NewNodeHashBlock(h HashMaker, b []byte) (*Node, error)

NewNodeHashBlock returns a new Node using the provided crypto.Hash, and calculates the block's checksum

func (Node) Checksum

func (n Node) Checksum() ([]byte, error)

Checksum returns the checksum of the block, or the checksum of this nodes children (left.checksum + right.checksum) If it is a leaf (no children) Node, then the Checksum is of the block of a payload. Otherwise, the Checksum is of it's two children's Checksum.

func (Node) IsLeaf

func (n Node) IsLeaf() bool

IsLeaf indicates this node is for specific block (and has no children)

type Tree

type Tree struct {
	Nodes       []*Node `json:"pieces"`
	BlockLength int     `json:"piece length"`
}

Tree is the information on the structure of a set of nodes

TODO more docs here

func (*Tree) Pieces

func (t *Tree) Pieces() []byte

Pieces returns the concatenation of hash values of all blocks

TODO integrate with hash size

func (*Tree) Root

func (t *Tree) Root() *Node

Root generates a hash tree bash on the current nodes, and returns the root of the tree

type Treeer

type Treeer interface {
	Nodes() []*Node
	Root() *Node
}

Treeer (Tree-er) provides access to the Merkle tree internals

Jump to

Keyboard shortcuts

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