Documentation
¶
Overview ¶
Package sync syncs a merkle trie over the p2p network: a Syncer (client) requests range and change proofs that a ProofHandler (server) generates.
Metrics are split by component: [syncerMetrics] is updated only by Syncer and [handlerMetrics] only by ProofHandler, so each component registers only the collectors it updates.
Index ¶
Constants ¶
View Source
const ( DefaultRequestKeyLimit = MaxKeyValuesLimit DefaultRequestByteSizeLimit = maxByteSizeLimit )
View Source
const ( // Maximum number of key-value pairs to return in a proof. // This overrides any other Limit specified in a RangeProofRequest // or ChangeProofRequest if the given Limit is greater. MaxKeyValuesLimit = 2048 )
Variables ¶
View Source
var ( ErrInsufficientHistory = errors.New("insufficient history to generate proof") ErrNoEndRoot = fmt.Errorf("%w: end root not found", ErrInsufficientHistory) )
View Source
var ( ErrAlreadyStarted = errors.New("cannot start a Syncer that has already been started") ErrAlreadyClosed = errors.New("Syncer is closed") ErrNoRangeProofMarshalerProvided = errors.New("range proof marshaler is a required field of the sync config") ErrNoChangeProofMarshalerProvided = errors.New("change proof marshaler is a required field of the sync config") ErrNoProofClientProvided = errors.New("proof client is a required field of the sync config") ErrNoDatabaseProvided = errors.New("sync database is a required field of the sync config") ErrNoLogProvided = errors.New("log is a required field of the sync config") ErrZeroWorkLimit = errors.New("simultaneous work limit must be greater than 0") ErrFinishedWithUnexpectedRoot = errors.New("finished syncing with an unexpected root") )
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config[R any, C any] struct { RangeProofMarshaler Marshaler[R] ChangeProofMarshaler Marshaler[C] ProofClient *peertracker.Client SimultaneousWorkLimit int Log logging.Logger TargetRoot ids.ID EmptyRoot ids.ID StateSyncNodes []ids.NodeID }
TODO remove non-config values out of this struct
type DB ¶
type DB[R any, C any] interface { // GetMerkleRoot returns the current root of the trie. // If the trie is empty, returns ids.Empty. GetMerkleRoot(context.Context) (ids.ID, error) // Clear removes all key/value pairs from the trie. Clear() error // GetChangeProof returns a proof for a subset of the key/value changes in key range // [start, end] that occurred between [startRootID] and [endRootID]. // Returns at most [maxLength] key/value pairs. // Returns [sync.ErrStartRootNotFound] if this node has insufficient // history to generate the proof. // Returns ErrEmptyProof if [endRootID] is ids.Empty. // Note that [endRootID] == ids.Empty means the trie is empty // (i.e. we don't need a change proof.) // Returns [ErrNoEndRoot], if the history doesn't contain the [endRootID]. GetChangeProof( ctx context.Context, startRootID ids.ID, endRootID ids.ID, start maybe.Maybe[[]byte], end maybe.Maybe[[]byte], maxLength int, ) (C, error) // Returns nil iff all the following hold: // - [start] <= [end]. // - [proof] is non-empty. // - [proof] is well-formed. // - The keys in [proof] is not longer than [maxLength]. // - All keys in [proof] and [proof] are in [start, end]. // If [start] is nothing, all keys are considered > [start]. // If [end] is nothing, all keys are considered < [end]. // - When the changes in [proof] are applied, // the root ID of the database is [expectedEndRootID]. VerifyChangeProof( ctx context.Context, proof C, start maybe.Maybe[[]byte], end maybe.Maybe[[]byte], expectedEndRootID ids.ID, maxLength int, ) error // CommitChangeProof commits the key/value pairs within the [proof] to the db. // [end] is the largest possible key in the range this [proof] covers. // Returns the next key that should be requested, or Nothing if all keys // have been are correct prior to [end]. CommitChangeProof(ctx context.Context, end maybe.Maybe[[]byte], proof C) (maybe.Maybe[[]byte], error) // GetRangeProofAtRoot returns a proof for the key/value pairs in this trie within the range // [start, end] when the root of the trie was [rootID]. // If [start] is Nothing, there's no lower bound on the range. // If [end] is Nothing, there's no upper bound on the range. // Returns an error if [rootID] is ids.Empty. // Returns [ErrInsufficientHistory] if the root is not found. // Note that [rootID] == ids.Empty means the trie is empty // (i.e. we don't need a range proof.) GetRangeProofAtRoot( ctx context.Context, rootID ids.ID, start maybe.Maybe[[]byte], end maybe.Maybe[[]byte], maxLength int, ) (R, error) // Returns nil iff all the following hold: // - [start] <= [end]. // - [proof] is non-empty. // - [proof] is well-formed. // - The keys in [proof] is not longer than [maxLength]. // - All keys in [proof] and [proof] are in [start, end]. // If [start] is nothing, all keys are considered > [start]. // If [end] is nothing, all keys are considered < [end]. // - When the changes in [proof] are applied, // the root ID of the database is [expectedEndRootID]. VerifyRangeProof( ctx context.Context, proof R, start maybe.Maybe[[]byte], end maybe.Maybe[[]byte], expectedEndRootID ids.ID, maxLength int, ) error // CommitRangeProof commits the key/value pairs within the [proof] to the db. // [start] is the smallest possible key in the range this [proof] covers. // [end] is the largest possible key in the range this [proof] covers. // Returns the next key that should be requested, or Nothing if all keys // have been are correct prior to [end]. CommitRangeProof(ctx context.Context, start, end maybe.Maybe[[]byte], proof R) (maybe.Maybe[[]byte], error) }
type ProofHandler ¶ added in v1.15.0
func NewProofHandler ¶ added in v1.15.0
func NewProofHandler[R any, C any]( db DB[R, C], rangeProofMarshaler Marshaler[R], changeProofMarshaler Marshaler[C], registerer prometheus.Registerer, ) (*ProofHandler[R, C], error)
type Syncer ¶
func NewSyncer ¶
func NewSyncer[R any, C any]( db DB[R, C], config Config[R, C], registerer prometheus.Registerer, ) (*Syncer[R, C], error)
func (*Syncer[_, _]) Sync ¶
Sync initiates the trie syncing process and blocks until one of the following occurs:
- Syncer.Sync is complete.
- Syncer.Sync fatally errored.
- `ctx` is canceled.
If `ctx` is canceled, returns context.Context.Err.
Click to show internal directories.
Click to hide internal directories.