merkletree

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2021 License: MIT Imports: 6 Imported by: 0

README

Go Reference

MerkleTree Golang

MerkleTree is a hash tree which hash the node context from the leaf to the root. all the parent hash value has relation with its child node, this data struct can use for easily verify data set in the O(logN) time complexity. In this implement have the add new leaf, get the root hash value, check the context is correct feature.

Install

go get github.com/Noahnut/merkletree

Example Usage

package main

import (
	"fmt"

	"github.com/Noahnut/merkletree"
)

func main() {
	m := merkletree.CreateMerkleTree()
	a := "testString"
	b := "testStringTwo"
	c := "testStringThree"
	d := "testStringFour"
	e := "testStringFive"

	// Add the new data to the tree
	m.AddNewBlock([]byte(a))
	m.AddNewBlock([]byte(b))
	m.AddNewBlock([]byte(c))
	m.AddNewBlock([]byte(d))
	m.AddNewBlock([]byte(e))

	// Verify the data is exist in the tree or not
	result := m.ContextValidator([]byte(e))

	// Get the root hash value 
	rootHash := m.GetRootHash()

	// Check tree from top to down hash value is correct
	result := m.CheckTreeCorrect()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MerkleTree

type MerkleTree struct {
	Leafs sync.Map
	// contains filtered or unexported fields
}

func CreateMerkleTree

func CreateMerkleTree() *MerkleTree

Create the new Merkle Tree

func (*MerkleTree) AddNewBlock

func (m *MerkleTree) AddNewBlock(context []byte)

Add new data block to the Merkle Tree

func (*MerkleTree) CheckTreeCorrect

func (m *MerkleTree) CheckTreeCorrect() bool

Check Tree all the hash value is follow the Merkle Tree Properity

func (*MerkleTree) ContextValidator

func (m *MerkleTree) ContextValidator(context []byte) bool

Check the context exist and legal in the tree make sure all the hash from leaf to root is correct

func (*MerkleTree) GetDifferentContextFromTree added in v1.0.3

func (m *MerkleTree) GetDifferentContextFromTree(compareTree *MerkleTree) [][]byte

func (*MerkleTree) GetRootHash

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

Get the Tree root hash value

func (*MerkleTree) PrintCurrTree

func (m *MerkleTree) PrintCurrTree()

helper function

func (*MerkleTree) PrintlnAllLeafs

func (m *MerkleTree) PrintlnAllLeafs()

helper function

Jump to

Keyboard shortcuts

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