asmtree

package module
v0.0.0-...-3e47da5 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2021 License: MIT Imports: 10 Imported by: 0

README

asmt

asmt is a GoLang sparse merkle tree.

A forked of Aergo SMT with minor style code modifications and some new methods: Walk() WalkWithRoot() and GetWithRoot().

This module includes an abstraction layer to make the SMT more developer friendly with a very easy to use API:

  • Init(name, dataDir string) error
  • LastAccess() (timestamp int64)
  • Add(key, value []byte) error
  • AddBatch(keys, values [][]byte) (failedIndexes []int, err error)
  • GenProof(key, value []byte) (mproof []byte, err error)
  • CheckProof(key, value, root, mproof []byte) (included bool, err error)
  • Root() []byte
  • HashExists(hash []byte) (bool, error)
  • Dump(root []byte) (data []byte, err error) // Dump must return a format compatible with ImportDump
  • DumpPlain(root []byte) (keys [][]byte, values [][]byte, err error)
  • ImportDump(data []byte) error
  • Size(root []byte) (int64, error)
  • Snapshot(root []byte) (Tree, error) // Snapshot is a frozen trie on a specific Root hash that cannot be modified

Documentation

Index

Constants

View Source
const (
	MaxKeySize   = 32
	MaxValueSize = 64
)

Variables

This section is empty.

Functions

func NewTree

func NewTree(name, storageDir string) (censustree.Tree, error)

NewTree initializes a new AergoSMT tree following the censustree.Tree interface specification.

Types

type Proof

type Proof struct {
	Bitmap   []byte
	Length   int
	Siblings [][]byte
	Value    []byte
}

type Tree

type Tree struct {
	Tree *asmt.Trie
	// contains filtered or unexported fields
}

func (*Tree) Add

func (t *Tree) Add(index, value []byte) error

Add adds a new claim to the merkle tree A claim is composed of two parts: index and value

1.index is mandatory, the data will be used for indexing the claim into to merkle tree
2.value is optional, the data will not affect the indexing

func (*Tree) AddBatch

func (t *Tree) AddBatch(indexes, values [][]byte) ([]int, error)

AddBatch adds a list of indexes and values. The commit to disk is executed only once. The values slince could be empty or as long as indexes.

func (*Tree) CheckProof

func (t *Tree) CheckProof(index, value, root, mproof []byte) (bool, error)

CheckProof validates a merkle proof and its data.

func (*Tree) Close

func (t *Tree) Close() error

func (*Tree) Commit

func (t *Tree) Commit() error

Commit saves permanently the tree on disk

func (*Tree) Dump

func (t *Tree) Dump(root []byte) ([]byte, error)

Dump returns the whole merkle tree serialized in a format that can be used on Import. Byte seralization is performed using bare message protocol, it is a 40% size win over JSON.

func (*Tree) DumpPlain

func (t *Tree) DumpPlain(root []byte) ([][]byte, [][]byte, error)

DumpPlain returns the entire list of added claims for a specific root hash. First return parametre are the indexes and second the values. If root is not specified, the last one is used.

func (*Tree) GenProof

func (t *Tree) GenProof(index, value []byte) ([]byte, error)

GenProof generates a merkle tree proof that can be later used on CheckProof() to validate it.

func (*Tree) Get

func (t *Tree) Get(key []byte) []byte

Get returns the value of a key

func (*Tree) HashExists

func (t *Tree) HashExists(hash []byte) (bool, error)

HashExists checks if a hash exists as a node in the merkle tree

func (*Tree) ImportDump

func (t *Tree) ImportDump(data []byte) error

ImportDump imports a partial or whole tree previously exported with Dump()

func (*Tree) Init

func (t *Tree) Init(name, storageDir string) error

Init initializes a new asmt tree

func (*Tree) IsPublic

func (t *Tree) IsPublic() bool

IsPublic returns true if the tree is available

func (*Tree) LastAccess

func (t *Tree) LastAccess() int64

LastAccess returns the last time the Tree was accessed, in the form of a unix timestamp.

func (*Tree) MaxKeySize

func (t *Tree) MaxKeySize() int

func (*Tree) Publish

func (t *Tree) Publish()

Publish makes a merkle tree available for queries. Application layer should check IsPublish() before considering the Tree available.

func (*Tree) Root

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

Root returns the current root hash of the merkle tree

func (*Tree) Size

func (t *Tree) Size(root []byte) (int64, error)

Size returns the number of leaf nodes on the merkle tree. TO-DO: root is currently ignored

func (*Tree) Snapshot

func (t *Tree) Snapshot(root []byte) (censustree.Tree, error)

Snapshot returns a Tree instance of a exiting merkle root. A Snapshot cannot be modified.

func (*Tree) String

func (t *Tree) String() string

String returns a human readable representation of the tree.

func (*Tree) UnPublish

func (t *Tree) UnPublish()

UnPublish makes a merkle tree not available for queries

Directories

Path Synopsis
Package db is an wrapper of key-value database implementations.
Package db is an wrapper of key-value database implementations.

Jump to

Keyboard shortcuts

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