state

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package state is designed to handle current synchronization state. It is basically here to help out with progress, re-syncing, knowing exactly what we downloaded at which time, so we can efficiently handle downloading and download resumption.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockEntry

type BlockEntry struct {
	NetworkId                   utils.NetworkID          `json:"networkId" msgpack:"networkId"`
	BlockNumber                 *big.Int                 `json:"blockNumber" msgpack:"blockNumber"`
	TotalTransactionsCount      int                      `json:"totalTransactionsCount" msgpack:"totalTransactionsCount"`
	SuccessfulTransactionsCount int                      `json:"successfulTransactionsCount" msgpack:"successfulTransactionsCount"`
	FailedTransactionsCount     int                      `json:"failedTransactionsCount" msgpack:"failedTransactionsCount"`
	FailedTransactions          map[common.Hash]struct{} `json:"failedTransactions" msgpack:"failedTransactions"`
}

func (*BlockEntry) MarshalBinary

func (e *BlockEntry) MarshalBinary() ([]byte, error)

func (*BlockEntry) UnmarshalBinary

func (e *BlockEntry) UnmarshalBinary(data []byte) error

type Descriptor

type Descriptor struct {
	CurrentBlockHeadNumber      *big.Int
	LatestInspectedHeadBlock    *big.Int
	LatestInspectedArchiveBlock *big.Int
	StartBlockNumber            *big.Int
	EndBlockNumber              *big.Int
	PercentageCompleted         *big.Int
}

type Key

type Key string

Key defines a string type used for state keys within the application. It provides a method for easy conversion back to a native string type, facilitating interactions with external storage systems.

const (
	CurrentBlockHead            Key = "inspector:state:current-block-head-5" // CurrentBlockHead represents the current head of the blockchain.
	UnknownHeadBlock            Key = "unknown-head-key"
	LatestInspectedHeadBlock    Key = "inspector:state:latest-inspected-head-block-5"    // LatestInspectedHeadBlock represents the latest head block that has been inspected by the application.
	LatestInspectedArchiveBlock Key = "inspector:state:latest-inspected-archive-block-5" // LatestInspectedArchiveBlock represents the latest archive block that has been inspected by the application.
	ArchiveStartBlockNumber     Key = "inspector:state:archive-start-block-number-5"
	ArchiveEndBlockNumber       Key = "inspector:state:archive-end-block-number-5"
)

Predefined state keys for common blockchain-related values.

func GetHeadBlockKeyByDirection

func GetHeadBlockKeyByDirection(direction string) (Key, error)

func (Key) String

func (s Key) String() string

String returns the string representation of the Key. This method ensures the Key type can easily be converted back to a native string, allowing for seamless integration with systems expecting string keys.

type State

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

State encapsulates the application's state with context awareness and caching capabilities. It integrates with a Redis cache to store arbitrary large numbers, commonly used for storing and retrieving blockchain-related numeric data.

func New

func New(ctx context.Context, cache *cache.Redis) (*State, error)

New initializes a new State instance with a given context and cache. It returns a pointer to the created State and any error encountered during its creation.

func (*State) Descriptor

func (s *State) Descriptor() *Descriptor

func (*State) Exists

func (s *State) Exists(ctx context.Context, key Key) (bool, error)

Exists checks for the existence of a key within the state cache. It returns a boolean indicating the presence of the key and any error encountered.

func (*State) Get

func (s *State) Get(ctx context.Context, key Key) (*big.Int, error)

Get retrieves a value based on a key from the state cache. It converts the byte slice response back into a big.Int for usage within the application.

func (*State) GetBlockEntry

func (s *State) GetBlockEntry(ctx context.Context, networkId utils.NetworkID, block *types.Block) (*BlockEntry, error)

func (*State) GetBlockKey

func (s *State) GetBlockKey(networkId utils.NetworkID, blockNumber *big.Int) Key

func (*State) GetLatestInspectedBlock

func (s *State) GetLatestInspectedBlock(key Key) (*big.Int, error)

func (*State) IncrementBlock

func (s *State) IncrementBlock(entry *BlockEntry, direction Key, success bool, hashes ...common.Hash) error

func (*State) IsBlockCompleted

func (s *State) IsBlockCompleted(ctx context.Context, networkId utils.NetworkID, block *types.Block) (bool, error)

func (*State) Load

func (s *State) Load() error

func (*State) Monitor

func (s *State) Monitor(ctx context.Context) error

func (*State) Set

func (s *State) Set(ctx context.Context, key Key, value *big.Int) error

Set stores a value associated with a key within the state cache. It converts the value to a byte slice before writing to the cache for flexibility and efficiency.

func (*State) SetDescriptorKey

func (s *State) SetDescriptorKey(key Key, value any) error

Jump to

Keyboard shortcuts

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