lightclient

package
v0.0.0-...-5d5fcec Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockID

type BlockID struct {
	Hash          string        `json:"hash"`
	PartSetHeader PartSetHeader `json:"part_set_header"`
}

BlockID represents a block ID.

type Commit

type Commit struct {
	Height     int64       `json:"height"`
	Round      int32       `json:"round"`
	BlockID    BlockID     `json:"block_id"`
	Signatures []CommitSig `json:"signatures"`
}

Commit represents a commit with signatures.

type CommitSig

type CommitSig struct {
	BlockIDFlag      int       `json:"block_id_flag"`
	ValidatorAddress string    `json:"validator_address"`
	Timestamp        time.Time `json:"timestamp"`
	Signature        string    `json:"signature"`
}

CommitSig represents a commit signature.

func (*CommitSig) IsAbsent

func (cs *CommitSig) IsAbsent() bool

IsAbsent returns true if the signature is absent.

func (*CommitSig) IsCommit

func (cs *CommitSig) IsCommit() bool

IsCommit returns true if the signature is a commit signature.

type Config

type Config struct {
	// ChainID is the chain ID to verify against.
	ChainID string

	// ValidatorEndpoints are the RPC endpoints of trusted validators.
	ValidatorEndpoints []string

	// TrustThreshold is the minimum fraction of validators required for consensus.
	// Default is 2/3.
	TrustThreshold TrustThreshold

	// TrustingPeriod is the duration for which headers are considered valid.
	// Default is 24 hours.
	TrustingPeriod time.Duration

	// MaxClockDrift is the maximum allowed clock drift between header time and local time.
	// Default is 10 seconds.
	MaxClockDrift time.Duration

	// MinValidatorsForConsensus is the minimum number of validators required.
	// Default is 1.
	MinValidatorsForConsensus int

	// AutoSync enables automatic background synchronization.
	AutoSync bool

	// SyncInterval is the interval between sync attempts when AutoSync is enabled.
	// Default is 5 minutes.
	SyncInterval time.Duration

	// RPCTimeout is the timeout for RPC requests.
	// Default is 10 seconds.
	RPCTimeout time.Duration

	// MaxRetries is the maximum number of retries for failed requests.
	MaxRetries int
}

Config contains configuration for the light client.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default light client configuration.

type Header struct {
	Version            Version   `json:"version"`
	ChainID            string    `json:"chain_id"`
	Height             int64     `json:"height"`
	Time               time.Time `json:"time"`
	LastBlockID        *BlockID  `json:"last_block_id,omitempty"`
	LastCommitHash     string    `json:"last_commit_hash"`
	DataHash           string    `json:"data_hash"`
	ValidatorsHash     string    `json:"validators_hash"`
	NextValidatorsHash string    `json:"next_validators_hash"`
	ConsensusHash      string    `json:"consensus_hash"`
	AppHash            string    `json:"app_hash"`
	LastResultsHash    string    `json:"last_results_hash"`
	EvidenceHash       string    `json:"evidence_hash"`
	ProposerAddress    string    `json:"proposer_address"`
}

Header represents a block header.

type LightBlock

type LightBlock struct {
	Header        Header       `json:"header"`
	ValidatorSet  ValidatorSet `json:"validator_set"`
	Commit        Commit       `json:"commit"`
	TrustedHeight int64        `json:"trusted_height,omitempty"`
	TrustedAt     time.Time    `json:"trusted_at,omitempty"`
}

LightBlock represents a light block with header, validators, and commit.

type LightClient

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

LightClient keeps a CometBFT header of the Willow chain that it trusts: one pinned with InitializeWithTrustedHeader or ImportTrustedState, or the latest header an endpoint reports (InitializeWithTrustOnFirstUse), and then each later header SyncToLatest accepts through VerifyHeader.

VerifyHeader doesn't verify the commit's signatures, so a header the light client moves to is only as trustworthy as the endpoints that serve it, and its app hash isn't verified against the validators.

func NewLightClient

func NewLightClient(config Config) (*LightClient, error)

NewLightClient creates a new light client with the given configuration.

func (*LightClient) ExportTrustedState

func (lc *LightClient) ExportTrustedState() (*TrustedState, error)

ExportTrustedState exports the trusted state for persistence.

func (*LightClient) GetLatestAppHash

func (lc *LightClient) GetLatestAppHash(ctx context.Context) (string, error)

GetLatestAppHash returns the app_hash of the light client's latest header (see GetLatestTrustedHeader). If the light client is not initialized, it is initialized by trust on first use: the header is the one the first responsive endpoint reports.

The hash isn't verified against the validators' commit signatures (see LightClient): it is as trustworthy as the header pinned, or the endpoints the header came from.

func (*LightClient) GetLatestTrustedHeader

func (lc *LightClient) GetLatestTrustedHeader() (*TrustedHeader, error)

GetLatestTrustedHeader returns the latest trusted header.

func (*LightClient) GetSyncStatus

func (lc *LightClient) GetSyncStatus() *SyncStatus

GetSyncStatus returns the current synchronization status.

func (*LightClient) GetVerifiedRootHash deprecated

func (lc *LightClient) GetVerifiedRootHash(ctx context.Context) (string, error)

GetVerifiedRootHash returns GetLatestAppHash.

Deprecated: the hash isn't verified against the validators' commit signatures; use GetLatestAppHash.

func (*LightClient) ImportTrustedState

func (lc *LightClient) ImportTrustedState(state *TrustedState) error

ImportTrustedState imports a previously exported trusted state.

func (*LightClient) InitializeWithTrustOnFirstUse

func (lc *LightClient) InitializeWithTrustOnFirstUse(ctx context.Context) error

InitializeWithTrustOnFirstUse initializes the light client by fetching and trusting the latest block from validators. Every later header SyncToLatest takes is checked by VerifyHeader against that initial trusted state. Pin a known-good checkpoint header in production deployments.

func (*LightClient) InitializeWithTrustedHeader

func (lc *LightClient) InitializeWithTrustedHeader(block LightBlock) error

InitializeWithTrustedHeader initializes the light client with a trusted header.

func (*LightClient) SelectBestEndpoint

func (lc *LightClient) SelectBestEndpoint(ctx context.Context) (string, error)

SelectBestEndpoint selects the best validator endpoint based on responsiveness.

func (*LightClient) Start

func (lc *LightClient) Start() error

Start starts background synchronization if enabled.

func (*LightClient) Stop

func (lc *LightClient) Stop()

Stop stops the light client and background sync.

func (*LightClient) SyncToLatest

func (lc *LightClient) SyncToLatest(ctx context.Context) error

SyncToLatest syncs to the latest block.

func (*LightClient) VerifyHeader

func (lc *LightClient) VerifyHeader(ctx context.Context, untrusted LightBlock) (*VerificationResult, error)

VerifyHeader checks an untrusted header against the trusted state: its chain id, a height and time after the trusted header's, the trusting period, clock drift, an adjacent header's validator-set hash, and that the validators its commit lists as signing hold the trust threshold of the voting power of the validator set the untrusted block itself carries.

It verifies no signature, and doesn't tie that validator set to the header's validators hash or to the trusted validators, so a header anyone can build passes: ChecksPassed in the result means these checks passed, not that the validators signed the header.

type PartSetHeader

type PartSetHeader struct {
	Total uint32 `json:"total"`
	Hash  string `json:"hash"`
}

PartSetHeader represents a part set header.

type ProofVerificationResult

type ProofVerificationResult struct {
	Verified     bool   `json:"verified"`
	RootHash     string `json:"root_hash"`
	ExpectedHash string `json:"expected_hash"`
	Height       int64  `json:"height"`
	Error        string `json:"error,omitempty"`
}

ProofVerificationResult represents the result of proof verification.

type SyncStatus

type SyncStatus struct {
	LatestTrustedHeight int64     `json:"latest_trusted_height"`
	LatestTrustedTime   time.Time `json:"latest_trusted_time"`
	LatestKnownHeight   int64     `json:"latest_known_height"`
	IsSynced            bool      `json:"is_synced"`
	LastSyncAttempt     time.Time `json:"last_sync_attempt"`
	LastSyncError       string    `json:"last_sync_error,omitempty"`
}

SyncStatus represents the synchronization status.

type TrustThreshold

type TrustThreshold struct {
	Numerator   int
	Denominator int
}

TrustThreshold represents the trust threshold as a fraction.

func (TrustThreshold) Validate

func (t TrustThreshold) Validate(votingPower, totalPower int64) bool

Validate checks if voting power meets the trust threshold.

type TrustedHeader

type TrustedHeader struct {
	Header    Header    `json:"header"`
	AppHash   string    `json:"app_hash"`
	Height    int64     `json:"height"`
	TrustedAt time.Time `json:"trusted_at"`
}

TrustedHeader represents a trusted header with metadata.

type TrustedState

type TrustedState struct {
	Header       TrustedHeader `json:"header"`
	ValidatorSet ValidatorSet  `json:"validator_set"`
}

TrustedState represents the trusted state of the light client.

type Validator

type Validator struct {
	Address          string `json:"address"`
	PublicKey        string `json:"pub_key"`
	PublicKeyType    string `json:"pub_key_type"`
	VotingPower      int64  `json:"voting_power"`
	ProposerPriority int64  `json:"proposer_priority"`
}

Validator represents a validator.

type ValidatorSet

type ValidatorSet struct {
	Validators       []Validator `json:"validators"`
	TotalVotingPower int64       `json:"total_voting_power"`
}

ValidatorSet represents a set of validators.

func (*ValidatorSet) GetTotalVotingPower

func (vs *ValidatorSet) GetTotalVotingPower() int64

GetTotalVotingPower calculates the total voting power.

type VerificationResult

type VerificationResult struct {
	// ChecksPassed is true when the header passed VerifyHeader's checks: its
	// chain id; a height and time after the trusted header's; the trusting
	// period and clock drift; for the next height, a validators hash equal to
	// the trusted header's next validators hash; and the voting power its
	// commit lists as signing meeting the trust threshold of the validator
	// set the untrusted block itself carries. No check verifies a signature
	// or ties that validator set to the header's validators hash or to the
	// trusted validators, so a header anyone can build passes: ChecksPassed
	// doesn't mean the validators signed the header.
	ChecksPassed bool `json:"checks_passed"`
	// Verified is ChecksPassed. It doesn't mean the header is verified.
	//
	// Deprecated: no signature is verified; use ChecksPassed, which says
	// what passed.
	Verified       bool   `json:"verified"`
	Height         int64  `json:"height"`
	AppHash        string `json:"app_hash"`
	Error          string `json:"error,omitempty"`
	VotingPower    int64  `json:"voting_power"`
	TotalPower     int64  `json:"total_power"`
	SignatureCount int    `json:"signature_count"`
}

VerificationResult is what VerifyHeader found of a header. VotingPower is the voting power of the validators the commit lists as signing, SignatureCount how many it lists, and TotalPower the validator set's, all from the validator set the untrusted block carries; no signature is verified.

type Version

type Version struct {
	Block uint64 `json:"block"`
	App   uint64 `json:"app"`
}

Version represents the block version.

Jump to

Keyboard shortcuts

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