memiavl

package
v0.0.47 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2025 License: Apache-2.0 Imports: 31 Imported by: 0

README

MemIAVL

Changelog

The Design

The idea of MemIAVL is to keep the whole chain state in memory as much as possible to speed up reads and writes.

  • MemIAVL uses a write-ahead-log(WAL) to persist the changeset from transaction commit to speed up writes.
  • Instead of updating and flushing nodes to disk, state changes at every height are actually only written to WAL file
  • MemIAVL snapshots are taken periodically and written to disk to materialize the tree at some given height H
  • Each snapshot is composed of 3 files per module, one for key/value pairs, one for leaf nodes and one for branch nodes
  • After snapshot is taken, the snapshot files are then loaded with mmap for faster reads and lazy loading via page cache. At the same time, older WAL files will be truncated till the snapshot height
  • Each MemIAVL tree is composed of 2 types of node: MemNode and Persistent Node
    • All nodes are persistent nodes to start with. Each persistent node maps to some data stored on file
    • During updates or insertion, persistent nodes will turn into MemNode
    • MemNodes are nodes stores only in memory for all future read and writes
  • If a node crash in the middle of commit, it will be able to load from the last snapshot and replay the WAL file to catch up to the last committed height
Advantages
  • Better write amplification, we only need to write the change sets in real time which is much more compact than IAVL nodes, IAVL snapshot can be created in much lower frequency.
  • Better read amplification, the IAVL snapshot is a plain file, the nodes are referenced with offset, the read amplification is simply 1.
  • Better space amplification, the archived change sets are much more compact than current IAVL tree, in our test case, the ratio could be as large as 1:100. We don't need to keep too old IAVL snapshots, because versiondb will handle the historical key-value queries, IAVL tree only takes care of merkle proof generations for blocks within an unbonding period. In very rare cases that do need IAVL tree of very old version, you can always replay the change sets from the genesis.
  • Facilitate async commit which improves commit latency by huge amount
Trade-offs
  • Performance can degrade when state size grows much larger than memory
  • MemIAVL makes historical proof much slower
  • Periodic snapshot creation is a very heavy operation and could become a bottleneck
IAVL Snapshot

IAVL snapshot is composed by four files:

  • metadata, 16bytes:

    magic: 4
    format: 4
    version: 4
    root node index: 4
    
  • nodes, array of fixed size(16+32bytes) nodes, the node format is like this:

    # branch
    height   : 1
    _padding : 3
    version  : 4
    size     : 4
    key node : 4
    hash     : [32]byte
    
    # leaf
    height      : 1
    _padding    : 3
    version     : 4
    key offset  : 8
    hash        : [32]byte
    

    The node has fixed length, can be indexed directly. The nodes references each other with the node index, nodes are written with post-order depth-first traversal, so the root node is always placed at the end.

    For branch node, the key node field reference the smallest leaf node in the right branch, the key slice is fetched from there indirectly, the leaf nodes stores the offset into the kvs file, where the key and value slices can be built.

    The branch node's left/child node indexes are inferenced from existing information and properties of post-order traversal:

    right child index = self index - 1
    left child index = key node - 1
    

    The version/size/node indexes are encoded with 4 bytes, should be enough in foreseeable future, but could be changed to more bytes in the future.

    The implementation will read the mmap-ed content in a zero-copy way, won't use extra node cache, it will only rely on the OS page cache.

  • kvs, sequence of leaf node key-value pairs, the keys are ordered and no duplication.

    keyLen: varint-uint64
    key
    valueLen: varint-uint64
    value
    *repeat*
    

Documentation

Index

Constants

View Source
const (
	SnapshotPrefix = "snapshot-"
	SnapshotDirLen = len(SnapshotPrefix) + 20
)
View Source
const (
	OffsetHeight   = 0
	OffsetPreTrees = OffsetHeight + 1
	OffsetVersion  = OffsetHeight + 4
	OffsetSize     = OffsetVersion + 4
	OffsetKeyLeaf  = OffsetSize + 4

	OffsetHash          = OffsetKeyLeaf + 4
	SizeHash            = sha256.Size
	SizeNodeWithoutHash = OffsetHash
	SizeNode            = SizeNodeWithoutHash + SizeHash

	OffsetLeafVersion   = 0
	OffsetLeafKeyLen    = OffsetLeafVersion + 4
	OffsetLeafKeyOffset = OffsetLeafKeyLen + 4
	OffsetLeafHash      = OffsetLeafKeyOffset + 8
	SizeLeafWithoutHash = OffsetLeafHash
	SizeLeaf            = SizeLeafWithoutHash + SizeHash
)
View Source
const (
	// SnapshotFileMagic is little endian encoded b"IAVL"
	SnapshotFileMagic = 1280721225

	// the initial snapshot format
	SnapshotFormat = 0

	// magic: uint32, format: uint32, version: uint32
	SizeMetadata = 12

	FileNameNodes    = "nodes"
	FileNameLeaves   = "leaves"
	FileNameKVs      = "kvs"
	FileNameMetadata = "metadata"
)
View Source
const LockFileName = "LOCK"
View Source
const MetadataFileName = "__metadata"

Variables

This section is empty.

Functions

func EncodeBytes

func EncodeBytes(w io.Writer, bz []byte) error

EncodeBytes writes a varint length-prefixed byte slice to the writer, it's used for hash computation, must be compactible with the official IAVL implementation.

func GetLatestVersion

func GetLatestVersion(dir string) (int64, error)

GetLatestVersion finds the latest version number without loading the whole db, it's needed for upgrade module to check store upgrades, it returns 0 if db doesn't exist or is empty.

func HashNode

func HashNode(node Node) []byte

HashNode computes the hash of the node.

func Mmap

func Mmap(f *os.File) ([]byte, *[mmap.MaxMapSize]byte, error)

func VerifyHash

func VerifyHash(node Node) bool

VerifyHash compare node's cached hash with computed one

func WriteFileSync

func WriteFileSync(name string, data []byte) error

WriteFileSync calls `f.Sync` after before closing the file

Types

type DB

type DB struct {
	MultiTree
	// contains filtered or unexported fields
}

DB implements DB-like functionalities on top of MultiTree: - async snapshot rewriting - Write-ahead-log

The memiavl.db directory looks like this: ``` > current -> snapshot-N > snapshot-N > bank > kvs > nodes > metadata > acc > ... other stores > rlog ```

func OpenDB

func OpenDB(logger logger.Logger, targetVersion int64, opts Options) (*DB, error)

func (*DB) ApplyChangeSet

func (db *DB) ApplyChangeSet(name string, changeSet iavl.ChangeSet) error

ApplyChangeSet wraps MultiTree.ApplyChangeSet, it also appends the changesets in the pending log, which will be persisted to the rlog in next Commit call.

func (*DB) ApplyChangeSets

func (db *DB) ApplyChangeSets(changeSets []*proto.NamedChangeSet) error

ApplyChangeSets wraps MultiTree.ApplyChangeSets, it also appends the changesets in the pending log, which will be persisted to the rlog in next Commit call.

func (*DB) ApplyUpgrades

func (db *DB) ApplyUpgrades(upgrades []*proto.TreeNameUpgrade) error

ApplyUpgrades wraps MultiTree.ApplyUpgrades, it also appends the upgrades in a pending log, which will be persisted to the rlog in next Commit call.

func (*DB) Close

func (db *DB) Close() error

func (*DB) Commit

func (db *DB) Commit() (int64, error)

Commit wraps SaveVersion to bump the version and writes the pending changes into log files to persist on disk

func (*DB) CommittedVersion

func (db *DB) CommittedVersion() (int64, error)

CommittedVersion returns the latest version written in rlog file, or snapshot version if rlog is empty.

func (*DB) Copy

func (db *DB) Copy() *DB

func (*DB) LastCommitInfo

func (db *DB) LastCommitInfo() *proto.CommitInfo

LastCommitInfo returns the last commit info.

func (*DB) ReadOnly

func (db *DB) ReadOnly() bool

ReadOnly returns whether the DB is opened in read-only mode.

func (*DB) Reload

func (db *DB) Reload() error

func (*DB) RewriteSnapshot

func (db *DB) RewriteSnapshot(ctx context.Context) error

RewriteSnapshot writes the current version of memiavl into a snapshot, and update the `current` symlink.

func (*DB) RewriteSnapshotBackground

func (db *DB) RewriteSnapshotBackground() error

RewriteSnapshotBackground rewrite snapshot in a background goroutine, `Commit` will check the complete status, and switch to the new snapshot.

func (*DB) SaveVersion

func (db *DB) SaveVersion(updateCommitInfo bool) (int64, error)

func (*DB) SetInitialVersion

func (db *DB) SetInitialVersion(initialVersion int64) error

SetInitialVersion wraps `MultiTree.SetInitialVersion`. it will do a snapshot rewrite, because we can't use rlog to record this change, we need it to convert versions to rlog index in the first place.

func (*DB) TreeByName

func (db *DB) TreeByName(name string) *Tree

TreeByName wraps MultiTree.TreeByName to add a lock.

func (*DB) UpdateCommitInfo

func (db *DB) UpdateCommitInfo()

UpdateCommitInfo wraps MultiTree.UpdateCommitInfo to add a lock.

func (*DB) Version

func (db *DB) Version() int64

Version wraps MultiTree.Version to add a lock.

func (*DB) WorkingCommitInfo

func (db *DB) WorkingCommitInfo() *proto.CommitInfo

func (*DB) WriteSnapshot

func (db *DB) WriteSnapshot(dir string) error

WriteSnapshot wraps MultiTree.WriteSnapshot to add a lock.

type Exporter

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

func (*Exporter) Close

func (e *Exporter) Close()

Close closes the exporter. It is safe to call multiple times.

func (*Exporter) Next

func (e *Exporter) Next() (*types.SnapshotNode, error)

type FileLock

type FileLock interface {
	Unlock() error
	Destroy() error
}

func LockFile

func LockFile(fname string) (FileLock, error)

type Iterator

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

func NewIterator

func NewIterator(start, end []byte, ascending bool, root Node, zeroCopy bool) *Iterator

func (*Iterator) Close

func (iter *Iterator) Close() error

Close implements dbm.Iterator

func (*Iterator) Domain

func (iter *Iterator) Domain() ([]byte, []byte)

func (*Iterator) Error

func (iter *Iterator) Error() error

Error implements dbm.Iterator

func (*Iterator) Key

func (iter *Iterator) Key() []byte

Key implements dbm.Iterator

func (*Iterator) Next

func (iter *Iterator) Next()

Next implements dbm.Iterator

func (*Iterator) Valid

func (iter *Iterator) Valid() bool

Valid implements dbm.Iterator.

func (*Iterator) Value

func (iter *Iterator) Value() []byte

Value implements dbm.Iterator

type LeafLayout

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

func (LeafLayout) Hash

func (leaf LeafLayout) Hash() []byte

func (LeafLayout) KeyLength

func (leaf LeafLayout) KeyLength() uint32

func (LeafLayout) KeyOffset

func (leaf LeafLayout) KeyOffset() uint64

func (LeafLayout) Version

func (leaf LeafLayout) Version() uint32

type Leaves

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

Leaves is a continuously stored IAVL nodes

func NewLeaves

func NewLeaves(data []byte) (Leaves, error)

func (Leaves) Leaf

func (leaves Leaves) Leaf(i uint32) LeafLayout

type MemNode

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

func (*MemNode) Get

func (node *MemNode) Get(key []byte) ([]byte, uint32)

func (*MemNode) GetByIndex

func (node *MemNode) GetByIndex(index uint32) ([]byte, []byte)

func (*MemNode) Hash

func (node *MemNode) Hash() []byte

Computes the hash of the node without computing its descendants. Must be called on nodes which have descendant node hashes already computed.

func (*MemNode) Height

func (node *MemNode) Height() uint8

func (*MemNode) IsLeaf

func (node *MemNode) IsLeaf() bool

func (*MemNode) Key

func (node *MemNode) Key() []byte

func (*MemNode) Left

func (node *MemNode) Left() Node

func (*MemNode) Mutate

func (node *MemNode) Mutate(version, cowVersion uint32) *MemNode

Mutate clones the node if it's version is smaller than or equal to cowVersion, otherwise modify in-place

func (*MemNode) Right

func (node *MemNode) Right() Node

func (*MemNode) SafeHash

func (node *MemNode) SafeHash() []byte

func (*MemNode) Size

func (node *MemNode) Size() int64

func (*MemNode) Value

func (node *MemNode) Value() []byte

func (*MemNode) Version

func (node *MemNode) Version() uint32

type MmapFile

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

MmapFile manage the resources of a mmap-ed file

func NewMmap

func NewMmap(path string) (*MmapFile, error)

Open openes the file and create the mmap. the mmap is created with flags: PROT_READ, MAP_SHARED, MADV_RANDOM.

func (*MmapFile) Close

func (m *MmapFile) Close() error

Close closes the file and mmap handles

func (*MmapFile) Data

func (m *MmapFile) Data() []byte

Data returns the mmap-ed buffer

type MultiTree

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

MultiTree manages multiple memiavl tree together, all the trees share the same latest version, the snapshots are always created at the same version.

The snapshot structure is like this: ``` > snapshot-V > metadata > bank > kvs > nodes > metadata > acc > other stores... ```

func LoadMultiTree

func LoadMultiTree(dir string, zeroCopy bool, cacheSize int) (*MultiTree, error)

func NewEmptyMultiTree

func NewEmptyMultiTree(initialVersion uint32, cacheSize int) *MultiTree

func (*MultiTree) ApplyChangeSet

func (t *MultiTree) ApplyChangeSet(name string, changeSet iavl.ChangeSet) error

ApplyChangeSet applies change set for a single tree.

func (*MultiTree) ApplyChangeSets

func (t *MultiTree) ApplyChangeSets(changeSets []*proto.NamedChangeSet) error

ApplyChangeSets applies change sets for multiple trees.

func (*MultiTree) ApplyUpgrades

func (t *MultiTree) ApplyUpgrades(upgrades []*proto.TreeNameUpgrade) error

ApplyUpgrades store name upgrades

func (*MultiTree) Catchup

func (t *MultiTree) Catchup(stream types.Stream[proto.ChangelogEntry], endVersion int64) error

Catchup replay the new entries in the Rlog file on the tree to catch up to the target or latest version.

func (*MultiTree) Close

func (t *MultiTree) Close() error

func (*MultiTree) Copy

func (t *MultiTree) Copy(cacheSize int) *MultiTree

Copy returns a snapshot of the tree which won't be corrupted by further modifications on the main tree.

func (*MultiTree) LastCommitInfo

func (t *MultiTree) LastCommitInfo() *proto.CommitInfo

func (*MultiTree) ReplaceWith

func (t *MultiTree) ReplaceWith(other *MultiTree) error

func (*MultiTree) SaveVersion

func (t *MultiTree) SaveVersion(updateCommitInfo bool) (int64, error)

SaveVersion bumps the versions of all the stores and optionally returns the new app hash

func (*MultiTree) SetInitialVersion

func (t *MultiTree) SetInitialVersion(initialVersion int64) error

func (*MultiTree) SetZeroCopy

func (t *MultiTree) SetZeroCopy(zeroCopy bool)

func (*MultiTree) SnapshotVersion

func (t *MultiTree) SnapshotVersion() int64

func (*MultiTree) TreeByName

func (t *MultiTree) TreeByName(name string) *Tree

TreeByName returns the tree by name, returns nil if not found

func (*MultiTree) Trees

func (t *MultiTree) Trees() []NamedTree

Trees returns all the trees together with the name, ordered by name.

func (*MultiTree) UpdateCommitInfo

func (t *MultiTree) UpdateCommitInfo()

UpdateCommitInfo update lastCommitInfo based on current status of trees. it's needed if `updateCommitInfo` is set to `false` in `ApplyChangeSet`.

func (*MultiTree) Version

func (t *MultiTree) Version() int64

func (*MultiTree) WorkingCommitInfo

func (t *MultiTree) WorkingCommitInfo() *proto.CommitInfo

WorkingCommitInfo returns the commit info for the working tree

func (*MultiTree) WriteSnapshot

func (t *MultiTree) WriteSnapshot(ctx context.Context, dir string, wp *pond.WorkerPool) error

type MultiTreeExporter

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

func NewMultiTreeExporter

func NewMultiTreeExporter(dir string, version uint32, supportExportNonSnapshotVersion bool) (exporter *MultiTreeExporter, err error)

func (*MultiTreeExporter) Close

func (mte *MultiTreeExporter) Close() error

func (*MultiTreeExporter) Next

func (mte *MultiTreeExporter) Next() (interface{}, error)

type MultiTreeImporter

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

func NewMultiTreeImporter

func NewMultiTreeImporter(dir string, height uint64) (*MultiTreeImporter, error)

func (*MultiTreeImporter) Add

func (mti *MultiTreeImporter) Add(item interface{}) error

func (*MultiTreeImporter) AddNode

func (mti *MultiTreeImporter) AddNode(node *types.SnapshotNode)

func (*MultiTreeImporter) AddTree

func (mti *MultiTreeImporter) AddTree(name string) error

func (*MultiTreeImporter) Close

func (mti *MultiTreeImporter) Close() error

type NamedTree

type NamedTree struct {
	*Tree
	Name string
}

type Node

type Node interface {
	Height() uint8
	IsLeaf() bool
	Size() int64
	Version() uint32
	Key() []byte
	Value() []byte
	Left() Node
	Right() Node
	Hash() []byte

	// SafeHash returns byte slice that's safe to retain
	SafeHash() []byte

	// PersistedNode clone a new node, MemNode modify in place
	Mutate(version, cowVersion uint32) *MemNode

	// Get query the value for a key, it's put into interface because a specialized implementation is more efficient.
	Get(key []byte) ([]byte, uint32)
	GetByIndex(uint32) ([]byte, []byte)
}

Node interface encapsulate the interface of both PersistedNode and MemNode.

type NodeLayout

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

see comment of `PersistedNode`

func (NodeLayout) Hash

func (node NodeLayout) Hash() []byte

func (NodeLayout) Height

func (node NodeLayout) Height() uint8

func (NodeLayout) KeyLeaf

func (node NodeLayout) KeyLeaf() uint32

func (NodeLayout) PreTrees

func (node NodeLayout) PreTrees() uint8

func (NodeLayout) Size

func (node NodeLayout) Size() uint32

func (NodeLayout) Version

func (node NodeLayout) Version() uint32

type Nodes

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

Nodes is a continuously stored IAVL nodes

func NewNodes

func NewNodes(data []byte) (Nodes, error)

func (Nodes) Node

func (nodes Nodes) Node(i uint32) NodeLayout

type Options

type Options struct {
	Dir             string
	CreateIfMissing bool
	InitialVersion  uint32
	ReadOnly        bool
	// the initial stores when initialize the empty instance
	InitialStores []string
	// keep how many snapshots
	SnapshotKeepRecent uint32
	// how often to take a snapshot
	SnapshotInterval uint32
	// Buffer size for the asynchronous commit queue, -1 means synchronous commit,
	// default to 0.
	AsyncCommitBuffer int
	// ZeroCopy if true, the get and iterator methods could return a slice pointing to mmaped blob files.
	ZeroCopy bool
	// CacheSize defines the cache's max entry size for each memiavl store.
	CacheSize int
	// LoadForOverwriting if true rollbacks the state, specifically the OpenDB method will
	// truncate the versions after the `TargetVersion`, the `TargetVersion` becomes the latest version.
	// it do nothing if the target version is `0`.
	LoadForOverwriting bool

	// Limit the number of concurrent snapshot writers
	SnapshotWriterLimit int
}

func (*Options) FillDefaults

func (opts *Options) FillDefaults()

func (Options) Validate

func (opts Options) Validate() error

type PersistedNode

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

PersistedNode is backed by serialized byte array, usually mmap-ed from disk file. Encoding format (all integers are encoded in little endian):

Branch node: - height : 1 - preTrees : 1 - _padding : 2 - version : 4 - size : 4 - key node : 4 // node index of the smallest leaf in right branch - hash : 32 Leaf node: - version : 4 - key len : 4 - key offset : 8 - hash : 32

func (PersistedNode) Get

func (node PersistedNode) Get(key []byte) ([]byte, uint32)

func (PersistedNode) GetByIndex

func (node PersistedNode) GetByIndex(leafIndex uint32) ([]byte, []byte)

func (PersistedNode) Hash

func (node PersistedNode) Hash() []byte

func (PersistedNode) Height

func (node PersistedNode) Height() uint8

func (PersistedNode) IsLeaf

func (node PersistedNode) IsLeaf() bool

func (PersistedNode) Key

func (node PersistedNode) Key() []byte

func (PersistedNode) Left

func (node PersistedNode) Left() Node

Left result is not defined for leaf nodes.

func (PersistedNode) Mutate

func (node PersistedNode) Mutate(version, _ uint32) *MemNode

func (PersistedNode) Right

func (node PersistedNode) Right() Node

Right result is not defined for leaf nodes.

func (PersistedNode) SafeHash

func (node PersistedNode) SafeHash() []byte

func (PersistedNode) Size

func (node PersistedNode) Size() int64

func (PersistedNode) Value

func (node PersistedNode) Value() []byte

Value returns nil for non-leaf node.

func (PersistedNode) Version

func (node PersistedNode) Version() uint32

type Snapshot

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

Snapshot manage the lifecycle of mmap-ed files for the snapshot, it must out live the objects that derived from it.

func NewEmptySnapshot

func NewEmptySnapshot(version uint32) *Snapshot

func OpenSnapshot

func OpenSnapshot(snapshotDir string) (*Snapshot, error)

OpenSnapshot parse the version number and the root node index from metadata file, and mmap the other files.

func (*Snapshot) Close

func (snapshot *Snapshot) Close() error

Close closes the file and mmap handles, clears the buffers.

func (*Snapshot) Export

func (snapshot *Snapshot) Export() *Exporter

Export exports the nodes from snapshot file sequentially, more efficient than a post-order traversal.

func (*Snapshot) IsEmpty

func (snapshot *Snapshot) IsEmpty() bool

IsEmpty returns if the snapshot is an empty tree.

func (*Snapshot) Key

func (snapshot *Snapshot) Key(offset uint64) []byte

Key returns a zero-copy slice of key by offset

func (*Snapshot) KeyValue

func (snapshot *Snapshot) KeyValue(offset uint64) ([]byte, []byte)

KeyValue returns a zero-copy slice of key/value pair by offset

func (*Snapshot) Leaf

func (snapshot *Snapshot) Leaf(index uint32) PersistedNode

Leaf returns the leaf node by index

func (*Snapshot) LeafKey

func (snapshot *Snapshot) LeafKey(index uint32) []byte

func (*Snapshot) LeafKeyValue

func (snapshot *Snapshot) LeafKeyValue(index uint32) ([]byte, []byte)

func (*Snapshot) Node

func (snapshot *Snapshot) Node(index uint32) PersistedNode

Node returns the branch node by index

func (*Snapshot) RootHash

func (snapshot *Snapshot) RootHash() []byte

func (*Snapshot) RootNode

func (snapshot *Snapshot) RootNode() PersistedNode

RootNode returns the root node

func (*Snapshot) ScanNodes

func (snapshot *Snapshot) ScanNodes(callback func(node PersistedNode) error) error

ScanNodes iterate over the nodes in the snapshot order (depth-first post-order, leaf nodes before branch nodes)

func (*Snapshot) Version

func (snapshot *Snapshot) Version() uint32

Version returns the version of the snapshot

type Tree

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

verify change sets by replay them to rebuild iavl tree and verify the root hashes

func New

func New(_ int) *Tree

New creates an empty tree at genesis version

func NewEmptyTree

func NewEmptyTree(version uint64, initialVersion uint32) *Tree

NewEmptyTree creates an empty tree at an arbitrary version.

func NewFromSnapshot

func NewFromSnapshot(snapshot *Snapshot, zeroCopy bool, _ int) *Tree

NewFromSnapshot mmap the blob files and create the root node.

func NewWithInitialVersion

func NewWithInitialVersion(initialVersion uint32) *Tree

NewWithInitialVersion creates an empty tree with initial-version, it happens when a new store created at the middle of the chain.

func (*Tree) ApplyChangeSet

func (t *Tree) ApplyChangeSet(changeSet iavl.ChangeSet)

ApplyChangeSet apply the change set of a whole version, and update hashes.

func (*Tree) Close

func (t *Tree) Close() error

func (*Tree) Copy

func (t *Tree) Copy(_ int) *Tree

Copy returns a snapshot of the tree which won't be modified by further modifications on the main tree, the returned new tree can be accessed concurrently with the main tree.

func (*Tree) Export

func (t *Tree) Export() *Exporter

Export returns a snapshot of the tree which won't be corrupted by further modifications on the main tree.

func (*Tree) Get

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

func (*Tree) GetByIndex

func (t *Tree) GetByIndex(index int64) ([]byte, []byte)

func (*Tree) GetMembershipProof

func (t *Tree) GetMembershipProof(key []byte) (*ics23.CommitmentProof, error)

GetMembershipProof will produce a CommitmentProof that the given key (and queries value) exists in the iavl tree. If the key doesn't exist in the tree, this will return an error.

func (*Tree) GetNonMembershipProof

func (t *Tree) GetNonMembershipProof(key []byte) (*ics23.CommitmentProof, error)

GetNonMembershipProof will produce a CommitmentProof that the given key doesn't exist in the iavl tree. If the key exists in the tree, this will return an error.

func (*Tree) GetProof

func (t *Tree) GetProof(key []byte) *ics23.CommitmentProof

GetProof takes a key for creating existence or absence proof and returns the appropriate merkle.Proof. Since this must be called after querying for the value, this function should never error Thus, it will panic on error rather than returning it

func (*Tree) GetWithIndex

func (t *Tree) GetWithIndex(key []byte) (int64, []byte)

func (*Tree) Has

func (t *Tree) Has(key []byte) bool

func (*Tree) IsEmpty

func (t *Tree) IsEmpty() bool

func (*Tree) Iterator

func (t *Tree) Iterator(start, end []byte, ascending bool) dbm.Iterator

func (*Tree) Remove

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

func (*Tree) ReplaceWith

func (t *Tree) ReplaceWith(other *Tree) error

ReplaceWith is used during reload to replace the current tree with the newly loaded snapshot

func (*Tree) RootHash

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

RootHash updates the hashes and return the current root hash, it clones the persisted node's bytes, so the returned bytes is safe to retain.

func (*Tree) SaveVersion

func (t *Tree) SaveVersion(updateHash bool) ([]byte, int64, error)

SaveVersion increases the version number and optionally updates the hashes

func (*Tree) ScanPostOrder

func (t *Tree) ScanPostOrder(callback func(node Node) bool)

ScanPostOrder scans the tree in post-order, and call the callback function on each node. If the callback function returns false, the scan will be stopped.

func (*Tree) Set

func (t *Tree) Set(key, value []byte)

func (*Tree) SetInitialVersion

func (t *Tree) SetInitialVersion(initialVersion int64) error

func (*Tree) SetZeroCopy

func (t *Tree) SetZeroCopy(zeroCopy bool)

func (*Tree) VerifyMembership

func (t *Tree) VerifyMembership(proof *ics23.CommitmentProof, key []byte) bool

VerifyMembership returns true iff proof is an ExistenceProof for the given key.

func (*Tree) VerifyNonMembership

func (t *Tree) VerifyNonMembership(proof *ics23.CommitmentProof, key []byte) bool

VerifyNonMembership returns true iff proof is a NonExistenceProof for the given key.

func (*Tree) Version

func (t *Tree) Version() int64

Version returns the current tree version

func (*Tree) WriteSnapshot

func (t *Tree) WriteSnapshot(ctx context.Context, snapshotDir string) error

WriteSnapshot save the IAVL tree to a new snapshot directory.

type TreeImporter

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

TreeImporter import a single memiavl tree from state-sync snapshot

func NewTreeImporter

func NewTreeImporter(dir string, version int64) *TreeImporter

func (*TreeImporter) Add

func (ai *TreeImporter) Add(node *types.SnapshotNode)

func (*TreeImporter) Close

func (ai *TreeImporter) Close() error

Jump to

Keyboard shortcuts

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