chaintracker

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: Apache-2.0 Imports: 28 Imported by: 1 module, 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

View Source
const (
	BACKOFF_MAX_TIME = 10 * time.Minute

	GoodStabilityThreshold         = 0.3
	PollingUpdateLength            = 10
	MostFrequentPollingMultiplier  = 16
	LavaPollingMultiplierFrequency = 4
	PollingMultiplierFlagName      = "polling-multiplier"
)
View Source
const (
	DefaultAssumedBlockMemory      = 20
	DefaultBlockCheckpointDistance = 100
)
View Source
const (
	CacheMaxCost     = 100000 // each item cost would be 1
	CacheNumCounters = 100000 // expect 100000 items

)
View Source
const (
	// ChainTrackerDefaultMemory is the default number of blocks the chain tracker keeps in memory.
	ChainTrackerDefaultMemory = 100
)
View Source
const DummyChainTrackerLatestBlock = int64(math.MaxInt64)

Variables

View Source
var (
	InvalidConfigErrorBlocksToSave  = errors.New("blocks to save wasn't defined in config")
	InvalidConfigBlockTime          = errors.New("average block time wasn't defined in config")
	InvalidLatestBlockNumValue      = errors.New("returned latest block num should be greater than 0, but it's not")
	InvalidReturnedHashes           = errors.New("returned requestedHashes key count should be greater than 0, but it's not")
	ErrorFailedToFetchLatestBlock   = errors.New("Failed to fetch latest block from node")
	InvalidRequestedBlocks          = errors.New("provided requested blocks for function do not compose a valid request")
	RequestedBlocksOutOfRange       = errors.New("requested blocks are outside the supported range by the state tracker")
	ErrorFailedToFetchTooEarlyBlock = errors.New("server memory protection triggered, requested block is too early")
	InvalidRequestedSpecificBlock   = errors.New("provided requested specific blocks for function do not compose a stored entry")
)
View Source
var PollingMultiplier = uint64(1)

Functions

func BuildProofFromBlocks

func BuildProofFromBlocks(requestedHashes []*BlockStore) map[int64]interface{}

func FindRequestedBlockHash

func FindRequestedBlockHash(requestedHashes []*BlockStore, requestBlock, toBlock, fromBlock int64, finalizedBlockHashes map[int64]interface{}) (requestedBlockHash []byte, finalizedBlockHashesMapRet map[int64]interface{})

func LatestArgToBlockNum

func LatestArgToBlockNum(request, latestBlock int64) int64

func RegisterChainTrackerServiceServer

func RegisterChainTrackerServiceServer(s *grpc.Server, srv ChainTrackerServiceServer)

RegisterChainTrackerServiceServer registers srv with the gRPC server s.

Types

type BlockRange

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

func NewBlockRange

func NewBlockRange(fromBlock, toBlock, earliestBlockSaved, latestBlock int64) (br *BlockRange, err error)

func (*BlockRange) IsWanted

func (br *BlockRange) IsWanted(blockNum int64) bool

func (*BlockRange) IterationIndexes

func (br *BlockRange) IterationIndexes() []int

type BlockStore

type BlockStore struct {
	Block int64  `json:"block,omitempty"`
	Hash  string `json:"hash,omitempty"`
}

BlockStore holds a block number and its hash.

func (*BlockStore) GetBlock

func (m *BlockStore) GetBlock() int64

func (*BlockStore) GetHash

func (m *BlockStore) GetHash() string

func (*BlockStore) Marshal

func (m *BlockStore) Marshal() ([]byte, error)

func (*BlockStore) ProtoMessage

func (m *BlockStore) ProtoMessage()

func (*BlockStore) Reset

func (m *BlockStore) Reset()

func (*BlockStore) String

func (m *BlockStore) String() string

func (*BlockStore) Unmarshal

func (m *BlockStore) Unmarshal(b []byte) error

type ChainFetcher

type ChainFetcher interface {
	FetchLatestBlockNum(ctx context.Context) (int64, error)
	FetchBlockHashByNum(ctx context.Context, blockNum int64) (string, error)
	FetchEndpoint() lavasession.RPCProviderEndpoint
	CustomMessage(ctx context.Context, path string, data []byte, connectionType string, apiName string) ([]byte, error)
}

type ChainTracker

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

func (*ChainTracker) AddBlockGap

func (ct *ChainTracker) AddBlockGap(newData time.Duration, blocks uint64)

func (*ChainTracker) GetAtomicLatestBlockNum

func (cs *ChainTracker) GetAtomicLatestBlockNum() int64

func (*ChainTracker) GetLatestBlockData

func (cs *ChainTracker) GetLatestBlockData(fromBlock, toBlock, specificBlock int64) (latestBlock int64, requestedHashes []*BlockStore, changeTime time.Time, err error)

this function returns block hashes of the blocks: [from block - to block] inclusive. an additional specific block hash can be provided. order is sorted ascending it supports requests for [spectypes.LATEST_BLOCK-distance1, spectypes.LATEST_BLOCK-distance2) spectypes.NOT_APPLICABLE in fromBlock or toBlock results in only returning specific block. if specific block is spectypes.NOT_APPLICABLE it is ignored

func (*ChainTracker) GetLatestBlockNum

func (cs *ChainTracker) GetLatestBlockNum() (int64, time.Time)

func (*ChainTracker) GetServerBlockMemory

func (cs *ChainTracker) GetServerBlockMemory() uint64

func (*ChainTracker) IsDummy

func (cs *ChainTracker) IsDummy() bool

func (*ChainTracker) RegisterForBlockTimeUpdates

func (cs *ChainTracker) RegisterForBlockTimeUpdates(updatable blockTimeUpdatable)

func (*ChainTracker) ResetLatestBlock added in v1.0.1

func (cs *ChainTracker) ResetLatestBlock()

ResetLatestBlock zeroes the cached latest block under blockQueueMu. After reset, consistency pre-validation skips the lag check until the next poll repopulates the value (validation_consistency.go treats endpointLatestBlock <= 0 as "unknown, do not gate"). May be invoked concurrently with the poll goroutine, so accesses outside this method's own locked region must go through the locked helpers below.

func (*ChainTracker) StartAndServe

func (ct *ChainTracker) StartAndServe(ctx context.Context) error

type ChainTrackerConfig

type ChainTrackerConfig struct {
	ForkCallback             func(block int64)                                 // a function to be called when a fork is detected
	NewLatestCallback        func(blockFrom int64, blockTo int64, hash string) // a function to be called when a new block is detected
	ConsistencyCallback      func(oldBlock int64, block int64)
	OldBlockCallback         func(latestBlockTime time.Time)
	FetchErrorCallback       func() // a function to be called when the latest-block fetch fails
	ServerAddress            string // if not empty will open up a grpc server for that address
	BlocksToSave             uint64
	AverageBlockTime         time.Duration // how often to query latest block
	ServerBlockMemory        uint64
	BlocksCheckpointDistance uint64 // this causes the chainTracker to trigger it's checkpoint every X blocks
	Pmetrics                 *metrics.ProviderMetricsManager
	PollingTimeMultiplier    int
	ChainId                  string
	ParseDirectiveEnabled    bool
}

type ChainTrackerService

type ChainTrackerService struct {
	UnimplementedChainTrackerServiceServer
	ChainTracker *ChainTracker
}

func (*ChainTrackerService) GetLatestBlockData

func (cts *ChainTrackerService) GetLatestBlockData(ctx context.Context, latestBlockData *LatestBlockData) (*LatestBlockDataResponse, error)

func (*ChainTrackerService) GetLatestBlockNum

type ChainTrackerServiceServer

type ChainTrackerServiceServer interface {
	GetLatestBlockNum(context.Context, *emptypb.Empty) (*GetLatestBlockNumResponse, error)
	GetLatestBlockData(context.Context, *LatestBlockData) (*LatestBlockDataResponse, error)
}

ChainTrackerServiceServer is the server API for ChainTrackerService.

type DefaultChainTrackerFetcher

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

func (*DefaultChainTrackerFetcher) FetchBlockHashByNum

func (cs *DefaultChainTrackerFetcher) FetchBlockHashByNum(ctx context.Context, blockNum int64) (string, error)

func (*DefaultChainTrackerFetcher) FetchLatestBlockNum

func (cs *DefaultChainTrackerFetcher) FetchLatestBlockNum(ctx context.Context) (int64, error)

type DummyChainTracker

type DummyChainTracker struct{}

func (*DummyChainTracker) AddBlockGap

func (dct *DummyChainTracker) AddBlockGap(newData time.Duration, blocks uint64)

AddBlockGap adds a new block gap measurement

func (*DummyChainTracker) GetAtomicLatestBlockNum

func (dct *DummyChainTracker) GetAtomicLatestBlockNum() int64

GetAtomicLatestBlockNum returns the current latest block number atomically

func (*DummyChainTracker) GetLatestBlockData

func (dct *DummyChainTracker) GetLatestBlockData(fromBlock, toBlock, specificBlock int64) (latestBlock int64, requestedHashes []*BlockStore, changeTime time.Time, err error)

GetLatestBlockData returns block hashes for the specified block range and a specific block

func (*DummyChainTracker) GetLatestBlockNum

func (dct *DummyChainTracker) GetLatestBlockNum() (int64, time.Time)

GetLatestBlockNum returns the current latest block number and the time it was last changed

func (*DummyChainTracker) IsDummy

func (dct *DummyChainTracker) IsDummy() bool

func (*DummyChainTracker) RegisterForBlockTimeUpdates

func (dct *DummyChainTracker) RegisterForBlockTimeUpdates(updatable blockTimeUpdatable)

RegisterForBlockTimeUpdates registers an updatable to receive block time updates

func (*DummyChainTracker) ResetLatestBlock added in v1.0.1

func (dct *DummyChainTracker) ResetLatestBlock()

ResetLatestBlock is a no-op for the dummy tracker; it has no cached state to clear.

func (*DummyChainTracker) StartAndServe

func (dct *DummyChainTracker) StartAndServe(ctx context.Context) error

StartAndServe starts the chain tracker and serves gRPC if configured

type GetLatestBlockNumResponse

type GetLatestBlockNumResponse struct {
	Block     uint64 `json:"block,omitempty"`
	Timestamp int64  `json:"timestamp,omitempty"`
}

GetLatestBlockNumResponse is the response for GetLatestBlockNum.

func (*GetLatestBlockNumResponse) GetBlock

func (m *GetLatestBlockNumResponse) GetBlock() uint64

func (*GetLatestBlockNumResponse) GetTimestamp

func (m *GetLatestBlockNumResponse) GetTimestamp() int64

func (*GetLatestBlockNumResponse) Marshal

func (m *GetLatestBlockNumResponse) Marshal() ([]byte, error)

func (*GetLatestBlockNumResponse) ProtoMessage

func (m *GetLatestBlockNumResponse) ProtoMessage()

func (*GetLatestBlockNumResponse) Reset

func (m *GetLatestBlockNumResponse) Reset()

func (*GetLatestBlockNumResponse) String

func (m *GetLatestBlockNumResponse) String() string

func (*GetLatestBlockNumResponse) Unmarshal

func (m *GetLatestBlockNumResponse) Unmarshal(b []byte) error

type IChainFetcherWrapper

type IChainFetcherWrapper interface {
	FetchLatestBlockNum(ctx context.Context) (int64, error)
	FetchBlockHashByNum(ctx context.Context, blockNum int64) (string, error)
}

type IChainTracker

type IChainTracker interface {
	// GetLatestBlockData returns block hashes for the specified block range and a specific block
	GetLatestBlockData(fromBlock, toBlock, specificBlock int64) (latestBlock int64, requestedHashes []*BlockStore, changeTime time.Time, err error)

	// RegisterForBlockTimeUpdates registers an updatable to receive block time updates
	RegisterForBlockTimeUpdates(updatable blockTimeUpdatable)

	// GetLatestBlockNum returns the current latest block number and the time it was last changed
	GetLatestBlockNum() (int64, time.Time)

	// GetAtomicLatestBlockNum returns the current latest block number atomically
	GetAtomicLatestBlockNum() int64

	// ResetLatestBlock zeroes the cached latest block so the next consistency
	// pre-validation skips the lag check (validation_consistency.go treats
	// endpointLatestBlock <= 0 as "unknown, do not gate"). The poll loop
	// repopulates the value on its next successful fetch.
	ResetLatestBlock()

	// StartAndServe starts the chain tracker and serves gRPC if configured
	StartAndServe(ctx context.Context) error

	// AddBlockGap adds a new block gap measurement
	AddBlockGap(newData time.Duration, blocks uint64)

	// IsDummy gets the chain tracker weight - a way to differntiate between trackers (dummy tracker with weight 0)
	IsDummy() bool
}

IChainTracker represents the interface for chain tracking functionality

func NewChainTracker

func NewChainTracker(ctx context.Context, chainFetcher ChainFetcher, config ChainTrackerConfig) (chainTracker IChainTracker, err error)

type IChainTrackerDataFetcher

type IChainTrackerDataFetcher interface {
	GetAtomicLatestBlockNum() int64
	GetServerBlockMemory() uint64
}

type LatestBlockData

type LatestBlockData struct {
	FromBlock     int64 `json:"fromBlock,omitempty"`
	ToBlock       int64 `json:"toBlock,omitempty"`
	SpecificBlock int64 `json:"specificBlock,omitempty"`
}

LatestBlockData is the request for GetLatestBlockData.

func (*LatestBlockData) GetFromBlock

func (m *LatestBlockData) GetFromBlock() int64

func (*LatestBlockData) GetSpecificBlock

func (m *LatestBlockData) GetSpecificBlock() int64

func (*LatestBlockData) GetToBlock

func (m *LatestBlockData) GetToBlock() int64

func (*LatestBlockData) Marshal

func (m *LatestBlockData) Marshal() ([]byte, error)

func (*LatestBlockData) ProtoMessage

func (m *LatestBlockData) ProtoMessage()

func (*LatestBlockData) Reset

func (m *LatestBlockData) Reset()

func (*LatestBlockData) String

func (m *LatestBlockData) String() string

func (*LatestBlockData) Unmarshal

func (m *LatestBlockData) Unmarshal(b []byte) error

type LatestBlockDataResponse

type LatestBlockDataResponse struct {
	LatestBlock     int64         `json:"latestBlock,omitempty"`
	RequestedHashes []*BlockStore `json:"requestedHashes,omitempty"`
}

LatestBlockDataResponse is the response for GetLatestBlockData.

func (*LatestBlockDataResponse) GetLatestBlock

func (m *LatestBlockDataResponse) GetLatestBlock() int64

func (*LatestBlockDataResponse) GetRequestedHashes

func (m *LatestBlockDataResponse) GetRequestedHashes() []*BlockStore

func (*LatestBlockDataResponse) Marshal

func (m *LatestBlockDataResponse) Marshal() ([]byte, error)

func (*LatestBlockDataResponse) ProtoMessage

func (m *LatestBlockDataResponse) ProtoMessage()

func (*LatestBlockDataResponse) Reset

func (m *LatestBlockDataResponse) Reset()

func (*LatestBlockDataResponse) String

func (m *LatestBlockDataResponse) String() string

func (*LatestBlockDataResponse) Unmarshal

func (m *LatestBlockDataResponse) Unmarshal(b []byte) error

type SVMChainTracker

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

func (*SVMChainTracker) FetchBlockHashByNum

func (cs *SVMChainTracker) FetchBlockHashByNum(ctx context.Context, slot int64) (string, error)

On Solana the interface's `blockNum` parameter is a slot.

func (*SVMChainTracker) FetchLatestBlockNum

func (cs *SVMChainTracker) FetchLatestBlockNum(ctx context.Context) (int64, error)

type SVMLatestBlockResponse

type SVMLatestBlockResponse struct {
	Result struct {
		Context struct {
			Slot int64 `json:"slot"`
		} `json:"context"`
		Value struct {
			BlockHash string `json:"blockhash"`
		} `json:"value"`
	} `json:"result"`
}

type UnimplementedChainTrackerServiceServer

type UnimplementedChainTrackerServiceServer struct{}

UnimplementedChainTrackerServiceServer can be embedded for forward compatibility.

func (*UnimplementedChainTrackerServiceServer) GetLatestBlockData

func (*UnimplementedChainTrackerServiceServer) GetLatestBlockNum

type WantedBlocksData

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

func (*WantedBlocksData) IsWanted

func (wbd *WantedBlocksData) IsWanted(blockNum int64) bool

func (*WantedBlocksData) IterationIndexes

func (wbd *WantedBlocksData) IterationIndexes() (returnedIdxs []int)

func (*WantedBlocksData) New

func (wbd *WantedBlocksData) New(fromBlock, toBlock, specificBlock, latestBlock, earliestBlockSaved int64) (err error)

func (*WantedBlocksData) String

func (wbd *WantedBlocksData) String() string

Jump to

Keyboard shortcuts

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