syncer

package
v0.2400.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2024 License: Apache-2.0 Imports: 6 Imported by: 3

Documentation

Overview

Package syncer provides the read-only sync interface.

Index

Constants

View Source
const (
	// MinimumProofVersion is the minimum supported proof version.
	MinimumProofVersion = 0
	// LatestProofVersion is the latest supported proof version.
	LatestProofVersion = 1
)

Variables

View Source
var (
	// ErrDirtyRoot is the error returned when a ReadSyncer tries to sync from a
	// tree with a dirty root (e.g., a root with local modifications).
	ErrDirtyRoot = errors.New("mkvs: root is dirty")
	// ErrInvalidRoot is the error returned when a ReadSyncer tries to sync from a
	// tree with a different root.
	ErrInvalidRoot = errors.New("mkvs: invalid root")
	// ErrUnsupported is the error returned when a ReadSyncer method is not supported.
	ErrUnsupported = errors.New("mkvs: method not supported")
	// ErrUnsupportedProofVersion is the error returned when a ReadSyncer requests an unsuported proof version.
	ErrUnsupportedProofVersion = errors.New("mkvs: unsupported proof version")
)
View Source
var NopReadSyncer = &nopReadSyncer{}

NopReadSyncer is a no-op read syncer.

Functions

This section is empty.

Types

type GetPrefixesRequest

type GetPrefixesRequest struct {
	Tree     TreeID   `json:"tree"`
	Prefixes [][]byte `json:"prefixes"`
	Limit    uint16   `json:"limit"`

	// ProofVersion specifies the proof version to use. If not specified,
	// the default (0) version is used for backwards compatibility.
	ProofVersion uint16 `json:"proof_version,omitempty"`
}

GetPrefixesRequest is a request for the SyncGetPrefixes operation.

type GetRequest

type GetRequest struct {
	Tree            TreeID `json:"tree"`
	Key             []byte `json:"key"`
	IncludeSiblings bool   `json:"include_siblings,omitempty"`

	// ProofVersion specifies the proof version to use. If not specified,
	// the default (0) version is used for backwards compatibility.
	ProofVersion uint16 `json:"proof_version,omitempty"`
}

GetRequest is a request for the SyncGet operation.

type IterateRequest

type IterateRequest struct {
	Tree     TreeID `json:"tree"`
	Key      []byte `json:"key"`
	Prefetch uint16 `json:"prefetch"`

	// ProofVersion specifies the proof version to use. If not specified,
	// the default (0) version is used for backwards compatibility.
	ProofVersion uint16 `json:"proof_version,omitempty"`
}

IterateRequest is a request for the SyncIterate operation.

type Proof

type Proof struct {
	// V is the proof version.
	//
	// Similar to `cbor.Versioned` but the version is omitted if it is 0.
	// We don't use `cbor.Versioned` since we want version 0 proofs to be
	// backwards compatible with the old structure which was not versioned.
	//
	// Version 0:
	// Initial format.
	//
	// Version 1 change:
	// Leaf nodes are included separately, as children. In version 0 the leaf node was
	// serialized within the internal node.  The rationale behind this change is to eliminate
	// the need to serialize all leaf nodes on the path when proving the existence of a
	// specific value.
	V uint16 `json:"v,omitempty"`

	// UntrustedRoot is the root hash this proof is for. This should only be
	// used as a quick sanity check and proof verification MUST use an
	// independently obtained root hash as the prover can provide any root.
	UntrustedRoot hash.Hash `json:"untrusted_root"`
	// Entries are the proof entries in pre-order traversal.
	Entries [][]byte `json:"entries"`
}

Proof is a Merkle proof for a subtree.

type ProofBuilder

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

ProofBuilder is a Merkle proof builder.

func NewProofBuilder

func NewProofBuilder(root, subtree hash.Hash) *ProofBuilder

NewProofBuilder creates a new Merkle proof builder for the given root.

func NewProofBuilderForVersion added in v0.2400.0

func NewProofBuilderForVersion(root, subtree hash.Hash, proofVersion uint16) (*ProofBuilder, error)

NewProofBuilderForVersion creates a new Merkle proof builder for the given root in a given proof version format.

func NewProofBuilderV0 added in v0.2400.0

func NewProofBuilderV0(root, subtree hash.Hash) *ProofBuilder

NewProofBuilderV0 creates a new version 0 proof builder for the given root.

func (*ProofBuilder) Build

func (b *ProofBuilder) Build(ctx context.Context) (*Proof, error)

Build tries to build the proof.

func (*ProofBuilder) GetSubtreeRoot added in v0.2100.0

func (b *ProofBuilder) GetSubtreeRoot() hash.Hash

GetSubtreeRoot returns the subtree root hash for this proof.

func (*ProofBuilder) HasSubtreeRoot added in v0.2100.0

func (b *ProofBuilder) HasSubtreeRoot() bool

HasSubtreeRoot returns true if the subtree root node has already been included.

func (*ProofBuilder) Include

func (b *ProofBuilder) Include(n node.Node)

Include adds a node to the set of included nodes.

The node must be clean.

func (*ProofBuilder) Size

func (b *ProofBuilder) Size() uint64

Size returns the current size of this proof.

func (*ProofBuilder) Version added in v0.2400.0

func (b *ProofBuilder) Version() uint16

Version returns the proof version.

type ProofResponse

type ProofResponse struct {
	Proof Proof `json:"proof"`
}

ProofResponse is a response for requests that produce proofs.

type ProofVerifier

type ProofVerifier struct{}

ProofVerifier enables verifying proofs returned by the ReadSyncer API.

func (*ProofVerifier) VerifyProof

func (pv *ProofVerifier) VerifyProof(ctx context.Context, root hash.Hash, proof *Proof) (*node.Pointer, error)

VerifyProof verifies a proof and generates an in-memory subtree representing the nodes which are included in the proof.

func (*ProofVerifier) VerifyProofToWriteLog added in v0.2400.0

func (pv *ProofVerifier) VerifyProofToWriteLog(ctx context.Context, root hash.Hash, proof *Proof) (writelog.WriteLog, error)

VerifyProofToWriteLog verifies a proof and generates a write log representing the key/value pairs which are included in the proof.

type ReadSyncer

type ReadSyncer interface {
	// SyncGet fetches a single key and returns the corresponding proof.
	SyncGet(ctx context.Context, request *GetRequest) (*ProofResponse, error)

	// SyncGetPrefixes fetches all keys under the given prefixes and returns
	// the corresponding proofs.
	SyncGetPrefixes(ctx context.Context, request *GetPrefixesRequest) (*ProofResponse, error)

	// SyncIterate seeks to a given key and then fetches the specified
	// number of following items based on key iteration order.
	SyncIterate(ctx context.Context, request *IterateRequest) (*ProofResponse, error)
}

ReadSyncer is the interface for synchronizing the in-memory cache with another (potentially untrusted) MKVS.

type StatsCollector

type StatsCollector struct {
	SyncGetCount         int
	SyncGetPrefixesCount int
	SyncIterateCount     int
	// contains filtered or unexported fields
}

StatsCollector is a ReadSyncer which collects call statistics.

func NewStatsCollector

func NewStatsCollector(rs ReadSyncer) *StatsCollector

NewStatsCollector creates a new no-op read syncer.

func (*StatsCollector) SyncGet

func (c *StatsCollector) SyncGet(ctx context.Context, request *GetRequest) (*ProofResponse, error)

func (*StatsCollector) SyncGetPrefixes

func (c *StatsCollector) SyncGetPrefixes(ctx context.Context, request *GetPrefixesRequest) (*ProofResponse, error)

func (*StatsCollector) SyncIterate

func (c *StatsCollector) SyncIterate(ctx context.Context, request *IterateRequest) (*ProofResponse, error)

type SubtreeMerger

type SubtreeMerger struct{}

func (*SubtreeMerger) MergeVerifiedSubtree

func (m *SubtreeMerger) MergeVerifiedSubtree(
	ctx context.Context,
	dst *node.Pointer,
	subtree *node.Pointer,
	committer func(*node.Pointer) error,
) error

MergeVerifiedSubtree merges a previously verified subtree with an existing tree.

type TreeID

type TreeID struct {
	// Root is the Merkle tree root.
	Root node.Root `json:"root"`
	// Position is the caller's position in the tree structure to allow
	// returning partial proofs if possible.
	Position hash.Hash `json:"position"`
}

TreeID identifies a specific tree and a position within that tree.

Jump to

Keyboard shortcuts

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