chainstore

package
v0.1.89 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2025 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CachedValue

type CachedValue[T any] struct {
	// contains filtered or unexported fields
}

CachedValue represents a cached value with TTL

func NewCachedValue

func NewCachedValue[T any](value T, ttl time.Duration) *CachedValue[T]

NewCachedValue creates a new cached value

func (*CachedValue[T]) Get

func (cv *CachedValue[T]) Get() (T, bool)

Get returns the cached value and whether it's valid

func (*CachedValue[T]) IsValid

func (cv *CachedValue[T]) IsValid() bool

IsValid returns true if the cached value is still valid

type CapabilityManager

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

CapabilityManager manages RPC method capability detection

func NewCapabilityManager

func NewCapabilityManager(client *rpc.Client, ttl time.Duration) *CapabilityManager

NewCapabilityManager creates a new capability manager

func (*CapabilityManager) GetSupportedMethods

func (cm *CapabilityManager) GetSupportedMethods() []string

GetSupportedMethods returns all supported methods

func (*CapabilityManager) IsMethodSupported

func (cm *CapabilityManager) IsMethodSupported(method string) bool

IsMethodSupported checks if a method is supported

func (*CapabilityManager) RefreshCapabilities

func (cm *CapabilityManager) RefreshCapabilities(ctx context.Context) error

RefreshCapabilities tests and caches method capabilities

type ChainCache

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

ChainCache manages cached chain information with different TTLs

func NewChainCache

func NewChainCache() *ChainCache

NewChainCache creates a new chain cache

func (*ChainCache) GetBaseFee

func (cc *ChainCache) GetBaseFee(currentBlock *big.Int) (*big.Int, bool)

GetBaseFee gets cached base fee

func (*ChainCache) GetChainID

func (cc *ChainCache) GetChainID() (*big.Int, bool)

GetChainID gets cached chain ID

func (*ChainCache) GetClientVersion

func (cc *ChainCache) GetClientVersion() (string, bool)

GetClientVersion gets cached client version

func (*ChainCache) GetFeeHistory

func (cc *ChainCache) GetFeeHistory(ttl time.Duration) (*FeeHistoryResult, bool)

GetFeeHistory gets cached fee history

func (*ChainCache) GetFinalizedBlock

func (cc *ChainCache) GetFinalizedBlock(ttl time.Duration) (*big.Int, bool)

GetFinalizedBlock gets cached finalized block

func (*ChainCache) GetGasPrice

func (cc *ChainCache) GetGasPrice(ttl time.Duration) (*big.Int, bool)

GetGasPrice gets cached gas price

func (*ChainCache) GetPendingTxCount

func (cc *ChainCache) GetPendingTxCount(ttl time.Duration) (*big.Int, bool)

GetPendingTxCount gets cached pending transaction count

func (*ChainCache) GetQueuedTxCount

func (cc *ChainCache) GetQueuedTxCount(ttl time.Duration) (*big.Int, bool)

GetQueuedTxCount gets cached queued transaction count

func (*ChainCache) GetSafeBlock

func (cc *ChainCache) GetSafeBlock(ttl time.Duration) (*big.Int, bool)

GetSafeBlock gets cached safe block

func (*ChainCache) GetSignatures

func (cc *ChainCache) GetSignatures(hexSignature string, ttl time.Duration) ([]Signature, bool)

GetSignatures gets cached signatures for a hex signature

func (*ChainCache) GetSyncStatus

func (cc *ChainCache) GetSyncStatus(ttl time.Duration) (interface{}, bool)

GetSyncStatus gets cached sync status

func (*ChainCache) SetBaseFee

func (cc *ChainCache) SetBaseFee(baseFee *big.Int, blockNumber *big.Int)

SetBaseFee caches base fee for a specific block

func (*ChainCache) SetChainID

func (cc *ChainCache) SetChainID(chainID *big.Int)

SetChainID caches chain ID (never expires)

func (*ChainCache) SetClientVersion

func (cc *ChainCache) SetClientVersion(clientVersion string)

SetClientVersion caches client version (never expires)

func (*ChainCache) SetFeeHistory

func (cc *ChainCache) SetFeeHistory(feeHistory *FeeHistoryResult, ttl time.Duration)

SetFeeHistory caches fee history

func (*ChainCache) SetFinalizedBlock

func (cc *ChainCache) SetFinalizedBlock(block *big.Int, ttl time.Duration)

SetFinalizedBlock caches finalized block

func (*ChainCache) SetGasPrice

func (cc *ChainCache) SetGasPrice(gasPrice *big.Int, ttl time.Duration)

SetGasPrice caches gas price

func (*ChainCache) SetPendingTxCount

func (cc *ChainCache) SetPendingTxCount(count *big.Int, ttl time.Duration)

SetPendingTxCount caches pending transaction count

func (*ChainCache) SetQueuedTxCount

func (cc *ChainCache) SetQueuedTxCount(count *big.Int, ttl time.Duration)

SetQueuedTxCount caches queued transaction count

func (*ChainCache) SetSafeBlock

func (cc *ChainCache) SetSafeBlock(block *big.Int, ttl time.Duration)

SetSafeBlock caches safe block

func (*ChainCache) SetSignatures

func (cc *ChainCache) SetSignatures(hexSignature string, signatures []Signature, ttl time.Duration)

SetSignatures caches signatures for a hex signature

func (*ChainCache) SetSyncStatus

func (cc *ChainCache) SetSyncStatus(syncStatus interface{}, ttl time.Duration)

SetSyncStatus caches sync status with TTL

type ChainStore

type ChainStore interface {

	// GetBlock retrieves a block by hash or number
	GetBlock(ctx context.Context, blockHashOrNumber interface{}) (rpctypes.PolyBlock, error)

	// GetTransaction retrieves a transaction by hash
	GetTransaction(ctx context.Context, txHash common.Hash) (rpctypes.PolyTransaction, error)

	// GetReceipt retrieves a transaction receipt by transaction hash
	GetReceipt(ctx context.Context, txHash common.Hash) (rpctypes.PolyReceipt, error)

	// GetLatestBlock retrieves the most recent block
	GetLatestBlock(ctx context.Context) (rpctypes.PolyBlock, error)

	// GetBlockByNumber retrieves a block by its number
	GetBlockByNumber(ctx context.Context, number *big.Int) (rpctypes.PolyBlock, error)

	// GetBlockByHash retrieves a block by its hash
	GetBlockByHash(ctx context.Context, hash common.Hash) (rpctypes.PolyBlock, error)

	// Static info (fetch once, cache indefinitely)
	GetChainID(ctx context.Context) (*big.Int, error)
	GetClientVersion(ctx context.Context) (string, error)

	// Semi-static info (cache for minutes)
	GetSyncStatus(ctx context.Context) (interface{}, error)
	GetSafeBlock(ctx context.Context) (*big.Int, error)
	GetFinalizedBlock(ctx context.Context) (*big.Int, error)

	// Block-aligned info (cache per block)
	GetBaseFee(ctx context.Context) (*big.Int, error)
	GetBaseFeeForBlock(ctx context.Context, blockNumber *big.Int) (*big.Int, error)

	// Frequent info (cache for seconds)
	GetGasPrice(ctx context.Context) (*big.Int, error)
	GetFeeHistory(ctx context.Context, blockCount int, newestBlock string, rewardPercentiles []float64) (*FeeHistoryResult, error)

	// Very frequent info (minimal cache)
	GetPendingTransactionCount(ctx context.Context) (*big.Int, error)
	GetQueuedTransactionCount(ctx context.Context) (*big.Int, error)
	GetTxPoolStatus(ctx context.Context) (map[string]interface{}, error)
	GetNetPeerCount(ctx context.Context) (*big.Int, error)

	// === CAPABILITY & MANAGEMENT ===
	IsMethodSupported(method string) bool
	RefreshCapabilities(ctx context.Context) error
	GetSupportedMethods() []string

	// === CONNECTION INFO ===
	GetRPCURL() string
	MeasureConnectionLatency(ctx context.Context) (time.Duration, error)

	// === SIGNATURE LOOKUP ===
	// GetSignature retrieves function/event signatures from 4byte.directory
	GetSignature(ctx context.Context, hexSignature string) ([]Signature, error)

	// Close closes the store and releases any resources
	Close() error
}

ChainStore defines the unified interface for storing and retrieving all chain-related data

type ChainStoreConfig

type ChainStoreConfig struct {
	// Cache TTLs for different data types
	StaticTTL       time.Duration // Never expire (0)
	SemiStaticTTL   time.Duration // 5 minutes
	FrequentTTL     time.Duration // 30 seconds
	VeryFrequentTTL time.Duration // 5 seconds

	// Capability detection
	CapabilityTTL time.Duration // 1 hour

	// Feature toggles
	EnableTxPoolMonitoring   bool
	EnableFinalityTracking   bool
	EnableFeeHistoryTracking bool

	// Signature lookup configuration
	EnableSignatureLookup      bool          // Enable 4byte.directory lookups
	SignatureLookupTTL         time.Duration // Cache TTL for signatures (1 hour)
	SignatureLookupTimeout     time.Duration // HTTP timeout for API calls (5 seconds)
	SignatureLookupAPIURL      string        // 4byte.directory function signatures API endpoint
	EventSignatureLookupAPIURL string        // 4byte.directory event signatures API endpoint
}

ChainStoreConfig holds configuration for the ChainStore

func DefaultChainStoreConfig

func DefaultChainStoreConfig() *ChainStoreConfig

DefaultChainStoreConfig returns default configuration

type FeeHistoryResult

type FeeHistoryResult struct {
	OldestBlock   *big.Int     `json:"oldestBlock"`
	BaseFeePerGas []*big.Int   `json:"baseFeePerGas"`
	GasUsedRatio  []float64    `json:"gasUsedRatio"`
	Reward        [][]*big.Int `json:"reward,omitempty"`
}

FeeHistoryResult represents the result of eth_feeHistory

type PassthroughStore

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

PassthroughStore is a chainstore implementation that doesn't store anything and passes through requests directly to the RPC endpoint with caching

func NewPassthroughStore

func NewPassthroughStore(rpcURL string) (*PassthroughStore, error)

NewPassthroughStore creates a new passthrough store with the given RPC client

func NewPassthroughStoreWithConfig

func NewPassthroughStoreWithConfig(rpcURL string, config *ChainStoreConfig) (*PassthroughStore, error)

NewPassthroughStoreWithConfig creates a new passthrough store with custom configuration

func (*PassthroughStore) Close

func (s *PassthroughStore) Close() error

Close closes the store and releases any resources

func (*PassthroughStore) GetBaseFee

func (s *PassthroughStore) GetBaseFee(ctx context.Context) (*big.Int, error)

GetBaseFee retrieves the current base fee (cached per block)

func (*PassthroughStore) GetBaseFeeForBlock

func (s *PassthroughStore) GetBaseFeeForBlock(ctx context.Context, blockNumber *big.Int) (*big.Int, error)

GetBaseFeeForBlock retrieves the base fee for a specific block

func (*PassthroughStore) GetBlock

func (s *PassthroughStore) GetBlock(ctx context.Context, blockHashOrNumber interface{}) (rpctypes.PolyBlock, error)

GetBlock retrieves a block by hash or number

func (*PassthroughStore) GetBlockByHash

func (s *PassthroughStore) GetBlockByHash(ctx context.Context, hash common.Hash) (rpctypes.PolyBlock, error)

GetBlockByHash retrieves a block by its hash

func (*PassthroughStore) GetBlockByNumber

func (s *PassthroughStore) GetBlockByNumber(ctx context.Context, number *big.Int) (rpctypes.PolyBlock, error)

GetBlockByNumber retrieves a block by its number

func (*PassthroughStore) GetChainID

func (s *PassthroughStore) GetChainID(ctx context.Context) (*big.Int, error)

GetChainID retrieves the chain ID (cached indefinitely)

func (*PassthroughStore) GetClientVersion

func (s *PassthroughStore) GetClientVersion(ctx context.Context) (string, error)

GetClientVersion retrieves the client version (cached indefinitely)

func (*PassthroughStore) GetFeeHistory

func (s *PassthroughStore) GetFeeHistory(ctx context.Context, blockCount int, newestBlock string, rewardPercentiles []float64) (*FeeHistoryResult, error)

GetFeeHistory retrieves fee history (cached frequently)

func (*PassthroughStore) GetFinalizedBlock

func (s *PassthroughStore) GetFinalizedBlock(ctx context.Context) (*big.Int, error)

GetFinalizedBlock retrieves the finalized block number (cached semi-statically)

func (*PassthroughStore) GetGasPrice

func (s *PassthroughStore) GetGasPrice(ctx context.Context) (*big.Int, error)

GetGasPrice retrieves the current gas price (cached frequently)

func (*PassthroughStore) GetLatestBlock

func (s *PassthroughStore) GetLatestBlock(ctx context.Context) (rpctypes.PolyBlock, error)

GetLatestBlock retrieves the most recent block

func (*PassthroughStore) GetNetPeerCount

func (s *PassthroughStore) GetNetPeerCount(ctx context.Context) (*big.Int, error)

GetNetPeerCount retrieves the number of connected peers (cached very frequently)

func (*PassthroughStore) GetPendingTransactionCount

func (s *PassthroughStore) GetPendingTransactionCount(ctx context.Context) (*big.Int, error)

GetPendingTransactionCount retrieves pending transaction count (cached very frequently)

func (*PassthroughStore) GetQueuedTransactionCount

func (s *PassthroughStore) GetQueuedTransactionCount(ctx context.Context) (*big.Int, error)

GetQueuedTransactionCount retrieves queued transaction count (cached very frequently)

func (*PassthroughStore) GetRPCURL

func (s *PassthroughStore) GetRPCURL() string

GetRPCURL returns the RPC endpoint URL

func (*PassthroughStore) GetReceipt

func (s *PassthroughStore) GetReceipt(ctx context.Context, txHash common.Hash) (rpctypes.PolyReceipt, error)

GetReceipt retrieves a transaction receipt by transaction hash

func (*PassthroughStore) GetSafeBlock

func (s *PassthroughStore) GetSafeBlock(ctx context.Context) (*big.Int, error)

GetSafeBlock retrieves the safe block number (cached semi-statically)

func (*PassthroughStore) GetSignature

func (s *PassthroughStore) GetSignature(ctx context.Context, hexSignature string) ([]Signature, error)

GetSignature retrieves function/event signatures from 4byte.directory

func (*PassthroughStore) GetSupportedMethods

func (s *PassthroughStore) GetSupportedMethods() []string

GetSupportedMethods returns all supported methods

func (*PassthroughStore) GetSyncStatus

func (s *PassthroughStore) GetSyncStatus(ctx context.Context) (interface{}, error)

GetSyncStatus retrieves the sync status (cached semi-statically)

func (*PassthroughStore) GetTransaction

func (s *PassthroughStore) GetTransaction(ctx context.Context, txHash common.Hash) (rpctypes.PolyTransaction, error)

GetTransaction retrieves a transaction by hash

func (*PassthroughStore) GetTxPoolStatus

func (s *PassthroughStore) GetTxPoolStatus(ctx context.Context) (map[string]interface{}, error)

GetTxPoolStatus retrieves the full txpool status (cached very frequently)

func (*PassthroughStore) IsMethodSupported

func (s *PassthroughStore) IsMethodSupported(method string) bool

IsMethodSupported checks if a method is supported

func (*PassthroughStore) MeasureConnectionLatency

func (s *PassthroughStore) MeasureConnectionLatency(ctx context.Context) (time.Duration, error)

MeasureConnectionLatency measures the connection latency to the RPC endpoint

func (*PassthroughStore) RefreshCapabilities

func (s *PassthroughStore) RefreshCapabilities(ctx context.Context) error

RefreshCapabilities refreshes the capability cache

type Signature

type Signature struct {
	ID             int       `json:"id"`
	CreatedAt      time.Time `json:"created_at"`
	TextSignature  string    `json:"text_signature"`
	HexSignature   string    `json:"hex_signature"`
	BytesSignature string    `json:"bytes_signature"`
}

Signature represents a function or event signature from 4byte.directory

type SignatureResponse

type SignatureResponse struct {
	Count    int         `json:"count"`
	Next     *string     `json:"next"`
	Previous *string     `json:"previous"`
	Results  []Signature `json:"results"`
}

SignatureResponse represents the paginated response from 4byte.directory API

Jump to

Keyboard shortcuts

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