merkle

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2024 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chain

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

func NewChain

func NewChain(logger log.Logger, store record.Store, key *record.Key, markPower int64, typ ChainType, namefmt string) *Chain

func (*Chain) AddEntry

func (m *Chain) AddEntry(hash []byte, unique bool) error

AddEntry adds a Hash to the Chain controlled by the ChainManager. If unique is true, the hash will not be added if it is already in the chain.

func (*Chain) Commit

func (c *Chain) Commit() error

func (*Chain) Element

func (c *Chain) Element(index uint64) values.Value[[]byte]

func (*Chain) ElementIndex

func (c *Chain) ElementIndex(hash []byte) values.Value[uint64]

func (*Chain) Entries

func (m *Chain) Entries(begin, end int64) ([][]byte, error)

Entries returns the list of hashes with indexes indicated by range: (begin,end) begin must be before or equal to end. The hash with index begin upto but not including end are the hashes returned. Indexes are zero based, so the first hash in the State is at 0

func (*Chain) Entry

func (m *Chain) Entry(element int64) ([]byte, error)

Entry the nth leaf node

func (*Chain) Head

func (c *Chain) Head() values.Value[*State]

func (*Chain) IndexOf

func (m *Chain) IndexOf(hash []byte) (int64, error)

IndexOf Get an Element of a Merkle Tree from the database

func (*Chain) IsDirty

func (c *Chain) IsDirty() bool

func (*Chain) Key

func (c *Chain) Key() *record.Key

func (*Chain) MarkFreq

func (c *Chain) MarkFreq() int64

func (*Chain) MarkMask

func (c *Chain) MarkMask() int64

func (*Chain) MarkPower

func (c *Chain) MarkPower() int64

func (*Chain) Name

func (c *Chain) Name() string

func (*Chain) Receipt

func (c *Chain) Receipt(from, to int64) (*Receipt, error)

Receipt builds a receipt from one index to another

func (*Chain) Resolve

func (c *Chain) Resolve(key *record.Key) (record.Record, *record.Key, error)

func (*Chain) StateAt

func (m *Chain) StateAt(element int64) (ms *State, err error)

StateAt We only store the state at MarkPoints. This function computes a missing state even if one isn't stored for a particular element.

func (*Chain) States

func (c *Chain) States(index uint64) values.Value[*State]

func (*Chain) Type

func (c *Chain) Type() ChainType

func (*Chain) Walk

func (c *Chain) Walk(opts record.WalkOptions, fn record.WalkFunc) error

type ChainIndex

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

func NewChainIndex

func NewChainIndex(logger log.Logger, store database.Store, key *record.Key) *ChainIndex

NewChainIndex constructs a new ChainIndex data model object. ChainIndex supports appending keys in ascending order, e.g. as the chain grows. It does not allow adding keys that would sort before or between existing items.

func (*ChainIndex) Append

func (x *ChainIndex) Append(key *record.Key, index uint64) error

Append appends a (key, index) entry into the chain index. Append returns an error if the key does not come after all other entries.

func (*ChainIndex) Commit

func (c *ChainIndex) Commit() error

func (*ChainIndex) Find

func (x *ChainIndex) Find(target *record.Key) ChainSearchResult

Find searches for the closest index entry to the target.

func (*ChainIndex) IsDirty

func (c *ChainIndex) IsDirty() bool

func (*ChainIndex) Key

func (c *ChainIndex) Key() *record.Key

func (*ChainIndex) Last

func (x *ChainIndex) Last() (*record.Key, uint64, error)

Last returns the last index entry.

func (*ChainIndex) Resolve

func (c *ChainIndex) Resolve(key *record.Key) (record.Record, *record.Key, error)

func (*ChainIndex) Walk

func (c *ChainIndex) Walk(opts record.WalkOptions, fn record.WalkFunc) error

type ChainSearchResult

type ChainSearchResult interface {
	// Before returns the target entry if the match was exact, the entry before
	// the target if one exists, or an error result.
	Before() ChainSearchResult2

	// Exact returns the target entry if the match was exact, or an error
	// result.
	Exact() ChainSearchResult2

	// After returns the target entry if the match was exact, the entry after
	// the target if one exists, or an error result.
	After() ChainSearchResult2
}

type ChainSearchResult2

type ChainSearchResult2 interface {
	// Err returns an error if the search failed.
	Err() error

	// Index returns the index of the target entry.
	Index() (uint64, error)
}

type ChainType

type ChainType uint64

ChainType is the type of a chain belonging to an account.

const ChainTypeAnchor ChainType = 2

ChainTypeAnchor holds chain anchors.

const ChainTypeIndex ChainType = 4

ChainTypeIndex indexes other chains.

const ChainTypeTransaction ChainType = 1

ChainTypeTransaction holds transaction hashes.

const ChainTypeUnknown ChainType = 0

ChainTypeUnknown is used when the chain type is not known.

func ChainTypeByName

func ChainTypeByName(name string) (ChainType, bool)

ChainTypeByName returns the named Chain Type.

func (ChainType) GetEnumValue

func (v ChainType) GetEnumValue() uint64

GetEnumValue returns the value of the Chain Type

func (ChainType) MarshalJSON

func (v ChainType) MarshalJSON() ([]byte, error)

MarshalJSON marshals the Chain Type to JSON as a string.

func (*ChainType) SetEnumValue

func (v *ChainType) SetEnumValue(id uint64) bool

SetEnumValue sets the value. SetEnumValue returns false if the value is invalid.

func (ChainType) String

func (v ChainType) String() string

String returns the name of the Chain Type.

func (*ChainType) UnmarshalJSON

func (v *ChainType) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the Chain Type from JSON as a string.

type Hasher

type Hasher [][]byte

func (*Hasher) AddBigInt

func (h *Hasher) AddBigInt(v *big.Int)

func (*Hasher) AddBool

func (h *Hasher) AddBool(v bool)

func (*Hasher) AddBytes

func (h *Hasher) AddBytes(v []byte)

func (*Hasher) AddDuration

func (h *Hasher) AddDuration(v time.Duration)

func (*Hasher) AddEnum

func (h *Hasher) AddEnum(v interface{ GetEnumValue() uint64 })

func (*Hasher) AddHash

func (h *Hasher) AddHash(v *[32]byte)

func (*Hasher) AddHash2

func (h *Hasher) AddHash2(v [32]byte)

func (*Hasher) AddInt

func (h *Hasher) AddInt(v int64)

func (*Hasher) AddString

func (h *Hasher) AddString(v string)

func (*Hasher) AddTime

func (h *Hasher) AddTime(v time.Time)

func (*Hasher) AddTxID

func (h *Hasher) AddTxID(v *url.TxID)

func (*Hasher) AddUint

func (h *Hasher) AddUint(v uint64)

func (*Hasher) AddUrl

func (h *Hasher) AddUrl(v *url.URL)

func (*Hasher) AddUrl2

func (h *Hasher) AddUrl2(v *url.URL)

func (*Hasher) AddValue

func (h *Hasher) AddValue(v interface{ MerkleHash() []byte })

func (Hasher) MerkleHash

func (h Hasher) MerkleHash() []byte

func (Hasher) Receipt

func (h Hasher) Receipt(start, anchor int) *Receipt

Receipt returns a receipt for the numbered element. Receipt returns nil if either index is out of bounds.

type Receipt

type Receipt struct {

	// Start is the entry for which we want a proof.
	Start      []byte `json:"start,omitempty" form:"start" query:"start" validate:"required"`
	StartIndex int64  `json:"startIndex,omitempty" form:"startIndex" query:"startIndex" validate:"required"`
	// End is the entry at the index where the anchor was created.
	End      []byte `json:"end,omitempty" form:"end" query:"end" validate:"required"`
	EndIndex int64  `json:"endIndex,omitempty" form:"endIndex" query:"endIndex" validate:"required"`
	// Anchor is the root expected once all nodes are applied.
	Anchor []byte `json:"anchor,omitempty" form:"anchor" query:"anchor" validate:"required"`
	// Entries is the list of hashes to apply to create an anchor.
	Entries []*ReceiptEntry `json:"entries,omitempty" form:"entries" query:"entries" validate:"required"`
	// contains filtered or unexported fields
}

func (*Receipt) Combine

func (r *Receipt) Combine(receipts ...*Receipt) (*Receipt, error)

Combine Take a 2nd receipt, attach it to a root receipt, and return the resulting receipt. The idea is that if this receipt is anchored into another chain, Then we can create a receipt that proves the element in this receipt all the way down to an anchor in the root receipt. Note that both this receipt and the root receipt are expected to be good.

func (*Receipt) Contains

func (r *Receipt) Contains(other *Receipt) bool

Contains returns true if the 2nd receipt is equal to or contained within the first.

func (*Receipt) Copy

func (v *Receipt) Copy() *Receipt

func (*Receipt) CopyAsInterface

func (v *Receipt) CopyAsInterface() interface{}

func (*Receipt) Equal

func (v *Receipt) Equal(u *Receipt) bool

func (*Receipt) IsValid

func (v *Receipt) IsValid() error

func (*Receipt) MarshalBinary

func (v *Receipt) MarshalBinary() ([]byte, error)

func (*Receipt) MarshalJSON

func (v *Receipt) MarshalJSON() ([]byte, error)

func (*Receipt) UnmarshalBinary

func (v *Receipt) UnmarshalBinary(data []byte) error

func (*Receipt) UnmarshalBinaryFrom

func (v *Receipt) UnmarshalBinaryFrom(rd io.Reader) error

func (*Receipt) UnmarshalJSON

func (v *Receipt) UnmarshalJSON(data []byte) error

func (*Receipt) Validate

func (r *Receipt) Validate(opts *ValidateOptions) bool

Validate Take a receipt and validate that the element hash progresses to the Merkle Dag Root hash (MDRoot) in the receipt

type ReceiptEntry

type ReceiptEntry struct {
	Right bool   `json:"right,omitempty" form:"right" query:"right" validate:"required"`
	Hash  []byte `json:"hash,omitempty" form:"hash" query:"hash" validate:"required"`
	// contains filtered or unexported fields
}

func (*ReceiptEntry) Copy

func (v *ReceiptEntry) Copy() *ReceiptEntry

func (*ReceiptEntry) CopyAsInterface

func (v *ReceiptEntry) CopyAsInterface() interface{}

func (*ReceiptEntry) Equal

func (v *ReceiptEntry) Equal(u *ReceiptEntry) bool

func (*ReceiptEntry) IsValid

func (v *ReceiptEntry) IsValid() error

func (*ReceiptEntry) MarshalBinary

func (v *ReceiptEntry) MarshalBinary() ([]byte, error)

func (*ReceiptEntry) MarshalJSON

func (v *ReceiptEntry) MarshalJSON() ([]byte, error)

func (*ReceiptEntry) UnmarshalBinary

func (v *ReceiptEntry) UnmarshalBinary(data []byte) error

func (*ReceiptEntry) UnmarshalBinaryFrom

func (v *ReceiptEntry) UnmarshalBinaryFrom(rd io.Reader) error

func (*ReceiptEntry) UnmarshalJSON

func (v *ReceiptEntry) UnmarshalJSON(data []byte) error

type ReceiptList

type ReceiptList struct {

	// MerkleState merkle.State at the beginning of the list.
	MerkleState      *State   `json:"merkleState,omitempty" form:"merkleState" query:"merkleState" validate:"required"`
	Elements         [][]byte `json:"elements,omitempty" form:"elements" query:"elements" validate:"required"`
	Receipt          *Receipt `json:"receipt,omitempty" form:"receipt" query:"receipt" validate:"required"`
	ContinuedReceipt *Receipt `json:"continuedReceipt,omitempty" form:"continuedReceipt" query:"continuedReceipt" validate:"required"`
	// contains filtered or unexported fields
}

func GetReceiptList

func GetReceiptList(manager *Chain, Start int64, End int64) (r *ReceiptList, err error)

GetReceiptList Given a merkle tree with a start point and an end point, create a ReceiptList for all the elements from the start hash to the end hash, inclusive.

func NewReceiptList

func NewReceiptList() *ReceiptList

NewReceiptList Return a new ReceiptList with at least a MerkleState initialized

func (*ReceiptList) Copy

func (v *ReceiptList) Copy() *ReceiptList

func (*ReceiptList) CopyAsInterface

func (v *ReceiptList) CopyAsInterface() interface{}

func (*ReceiptList) Equal

func (v *ReceiptList) Equal(u *ReceiptList) bool

func (*ReceiptList) Included

func (r *ReceiptList) Included(entry []byte) bool

Included Tests an entry for inclusion in the given ReceiptList Note that while a ReceiptList proves inclusion in a Merkle Tree, and the fact that the list of elements proceed in order up to and including the anchor point, the ReceiptList does not necessarily prove the indices of the elements in the Merkle Tree. This could be solved by salting Receipts with the index of the hash at the anchor point.

func (*ReceiptList) IsValid

func (v *ReceiptList) IsValid() error

func (*ReceiptList) MarshalBinary

func (v *ReceiptList) MarshalBinary() ([]byte, error)

func (*ReceiptList) MarshalJSON

func (v *ReceiptList) MarshalJSON() ([]byte, error)

func (*ReceiptList) UnmarshalBinary

func (v *ReceiptList) UnmarshalBinary(data []byte) error

func (*ReceiptList) UnmarshalBinaryFrom

func (v *ReceiptList) UnmarshalBinaryFrom(rd io.Reader) error

func (*ReceiptList) UnmarshalJSON

func (v *ReceiptList) UnmarshalJSON(data []byte) error

func (*ReceiptList) Validate

func (r *ReceiptList) Validate(opts *ValidateOptions) bool

Validate Take a receipt and validate that the element hash progresses to the Merkle Dag Root hash (MDRoot) in the receipt

type State

type State struct {

	// Count is the count of hashes added to the tree.
	Count int64 `json:"count,omitempty" form:"count" query:"count" validate:"required"`
	// Pending is the hashes that represent the left edge of the tree.
	Pending [][]byte `json:"pending,omitempty" form:"pending" query:"pending" validate:"required"`
	// HashList is the hashes added to the tree.
	HashList [][]byte `json:"hashList,omitempty" form:"hashList" query:"hashList" validate:"required"`
}

func (*State) AddEntry

func (m *State) AddEntry(hash_ []byte)

AddEntry a Hash to the merkle tree and incrementally build the ChainHead

func (*State) Anchor

func (m *State) Anchor() (anchor []byte)

Anchor Compute the Merkle Directed Acyclic Graph (Merkle DAG or ChainHead) for the ChainHead at this point We take any trailing hashes in ChainHead, hash them up and combine to create the Merkle Dag Root. Getting the closing ListMDRoot is non-destructive, which is useful for some use cases.

Returns a nil if the MerkleSate is empty.

func (*State) Copy

func (v *State) Copy() *State

func (*State) CopyAsInterface

func (v *State) CopyAsInterface() interface{}

func (*State) Equal

func (m *State) Equal(m2 *State) (isEqual bool)

Equal Compares one State to another, and returns true if they are the same

func (*State) MarshalBinary

func (m *State) MarshalBinary() ([]byte, error)

func (*State) MarshalJSON

func (v *State) MarshalJSON() ([]byte, error)

func (*State) UnmarshalBinary

func (m *State) UnmarshalBinary(data []byte) error

func (*State) UnmarshalBinaryFrom

func (m *State) UnmarshalBinaryFrom(rd io.Reader) error

func (*State) UnmarshalJSON

func (v *State) UnmarshalJSON(data []byte) error

type ValidateOptions

type ValidateOptions struct {
	Relaxed bool
}

Jump to

Keyboard shortcuts

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