utils

package
v2.0.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2025 License: GPL-3.0, LGPL-3.0 Imports: 24 Imported by: 3

Documentation

Index

Constants

View Source
const (
	Wei   = 1
	GWei  = 1_000_000_000 * Wei
	Ether = 1_000_000_000 * GWei
)

Variables

This section is empty.

Functions

func BigEqual

func BigEqual(a, b *big.Int) bool

BigEqual returns true if a is equal to b. If a and b are nil, it returns true.

func BigEqualUint64

func BigEqualUint64(a *big.Int, b uint64) bool

BigEqualUint64 returns true if a is equal to b. If a is nil or not a uint64, it returns false.

func BigLessOrEqualUint64

func BigLessOrEqualUint64(a *big.Int, b uint64) bool

BigLessOrEqualUint64 returns true if a is less than or equal to b. If a is nil or not a uint64, it returns false.

func BigNumEqual

func BigNumEqual(x, y *big.Int) bool

BigNumEqual returns true if x and y are equivalent ie. both nil or both contain the same value.

func BytesToHashSlice

func BytesToHashSlice(b []byte) []common.Hash

BytesToHashSlice packs [b] into a slice of hash values with zero padding to the right if the length of b is not a multiple of 32.

func ComputeHash256

func ComputeHash256(data []byte) []byte

ComputeHash256 computes SHA256 hash and returns it as bytes

func ComputeHash256Array

func ComputeHash256Array(data []byte) ids.ID

ComputeHash256Array computes SHA256 hash and returns it as an ID

func ConvertToChainContext

func ConvertToChainContext(ctx *quasar.Context) *commontype.ChainContext

ConvertToChainContext converts a quasar.Context to commontype.ChainContext

func HashSliceToBytes

func HashSliceToBytes(hashes []common.Hash) []byte

HashSliceToBytes serializes a []common.Hash into a tightly packed byte array.

func IncrOne

func IncrOne(bytes []byte)

IncrOne increments bytes value by one

func IsBlockForked

func IsBlockForked(s, head *big.Int) bool

IsBlockForked returns whether a fork scheduled at block s is active at the given head block. Whilst this method is the same as isTimestampForked, they are explicitly separate for clearer reading.

func IsTimestampForked

func IsTimestampForked(s *uint64, head uint64) bool

IsTimestampForked returns whether a fork scheduled at timestamp s is active at the given head timestamp.

func NewUint64

func NewUint64(val uint64) *uint64

func TestChainContext

func TestChainContext() *commontype.ChainContext

TestChainContext returns a test ChainContext

func TestConsensusContext

func TestConsensusContext() *quasar.Context

func TimeToNewUint64

func TimeToNewUint64(time time.Time) *uint64

func Uint64PtrEqual

func Uint64PtrEqual(x, y *uint64) bool

Uint64PtrEqual returns true if x and y pointers are equivalent ie. both nil or both contain the same value.

func Uint64ToTime

func Uint64ToTime(val *uint64) time.Time

Types

type AddressRange

type AddressRange struct {
	Start common.Address
	End   common.Address
}

AddressRange represents a continuous range of addresses

func (*AddressRange) Contains

func (a *AddressRange) Contains(addr common.Address) bool

Contains returns true iff [addr] is contained within the (inclusive) range of addresses defined by [a].

type BoundedWorkers

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

func NewBoundedWorkers

func NewBoundedWorkers(max int) *BoundedWorkers

NewBoundedWorkers returns an instance of BoundedWorkers that will spawn up to [max] goroutines.

func (*BoundedWorkers) Execute

func (b *BoundedWorkers) Execute(f func())

Execute the given function on an existing goroutine waiting for more work, a new goroutine, or return if the context is canceled.

Execute must not be called after Wait, otherwise it might panic.

func (*BoundedWorkers) Wait

func (b *BoundedWorkers) Wait() int

Wait returns after all enqueued work finishes and all goroutines to exit. Wait returns the number of workers that were spawned during the run.

Wait can only be called after ALL calls to [Execute] have returned.

It is safe to call Wait multiple times but not safe to call [Execute] after [Wait] has been called.

type Cacher

type Cacher[K comparable, V any] interface {
	Put(key K, value V)
	Get(key K) (V, bool)
	Evict(key K)
	Flush()
	Len() int
}

Cacher is a generic cache interface

func NewLRUCache

func NewLRUCache[K comparable, V any](capacity int) Cacher[K, V]

NewLRUCache creates a new LRU cache with the specified capacity

type EmptyCache

type EmptyCache[K comparable, V any] struct{}

EmptyCache is a cache that never stores anything

func (*EmptyCache[K, V]) Evict

func (e *EmptyCache[K, V]) Evict(key K)

Evict does nothing

func (*EmptyCache[K, V]) Flush

func (e *EmptyCache[K, V]) Flush()

Flush does nothing

func (*EmptyCache[K, V]) Get

func (e *EmptyCache[K, V]) Get(key K) (V, bool)

Get always returns false

func (*EmptyCache[K, V]) Len

func (e *EmptyCache[K, V]) Len() int

Len always returns 0

func (*EmptyCache[K, V]) Put

func (e *EmptyCache[K, V]) Put(key K, value V)

Put does nothing

type GenericSet

type GenericSet[T comparable] interface {
	Add(items ...T)
	Remove(item T)
	Contains(item T) bool
	Clear()
	Len() int
	List() []T
}

GenericSet is a generic set interface

func NewSet

func NewSet[T comparable](capacity ...int) GenericSet[T]

NewSet creates a new set with optional initial capacity

type LRUCache

type LRUCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

LRUCache implements an LRU cache using a simple map with a fixed size

func (*LRUCache[K, V]) Evict

func (c *LRUCache[K, V]) Evict(key K)

Evict removes a key from the cache

func (*LRUCache[K, V]) Flush

func (c *LRUCache[K, V]) Flush()

Flush clears the cache

func (*LRUCache[K, V]) Get

func (c *LRUCache[K, V]) Get(key K) (V, bool)

Get retrieves a value from the cache

func (*LRUCache[K, V]) Len

func (c *LRUCache[K, V]) Len() int

Len returns the number of items in the cache

func (*LRUCache[K, V]) Put

func (c *LRUCache[K, V]) Put(key K, value V)

Put adds a value to the cache

type MeteredCache

type MeteredCache struct {
	*fastcache.Cache
	// contains filtered or unexported fields
}

MeteredCache wraps *fastcache.Cache and periodically pulls stats from it.

func NewMeteredCache

func NewMeteredCache(size int, namespace string, updateFrequency uint64) *MeteredCache

NewMeteredCache returns a new MeteredCache that will update stats to the provided namespace once per each [updateFrequency] operations. Note: if [updateFrequency] is passed as 0, it will be treated as 1.

func (*MeteredCache) Del

func (mc *MeteredCache) Del(k []byte)

func (*MeteredCache) Get

func (mc *MeteredCache) Get(dst, k []byte) []byte

func (*MeteredCache) GetBig

func (mc *MeteredCache) GetBig(dst, k []byte) []byte

func (*MeteredCache) Has

func (mc *MeteredCache) Has(k []byte) bool

func (*MeteredCache) HasGet

func (mc *MeteredCache) HasGet(dst, k []byte) ([]byte, bool)

func (*MeteredCache) Set

func (mc *MeteredCache) Set(k, v []byte)

func (*MeteredCache) SetBig

func (mc *MeteredCache) SetBig(k, v []byte)

type MockableClock

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

MockableClock implements MockableTimer

func (*MockableClock) Advance

func (c *MockableClock) Advance(d time.Duration)

Advance advances the time by duration

func (*MockableClock) Set

func (c *MockableClock) Set(t time.Time)

Set sets the current time

func (*MockableClock) Time

func (c *MockableClock) Time() time.Time

Time returns the current time

type MockableTimer

type MockableTimer interface {
	Time() time.Time
	Set(time time.Time)
	Advance(duration time.Duration)
}

MockableTimer is an interface for a mockable clock

func NewMockableClock

func NewMockableClock() MockableTimer

NewMockableClock creates a new mockable clock

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set implements a generic set data structure

func (*Set[T]) Add

func (s *Set[T]) Add(items ...T)

Add adds items to the set

func (*Set[T]) Clear

func (s *Set[T]) Clear()

Clear clears all items

func (*Set[T]) Contains

func (s *Set[T]) Contains(item T) bool

Contains checks if an item is in the set

func (*Set[T]) Len

func (s *Set[T]) Len() int

Len returns the number of items

func (*Set[T]) List

func (s *Set[T]) List() []T

List returns all items as a slice

func (*Set[T]) Remove

func (s *Set[T]) Remove(item T)

Remove removes an item from the set

type TestValidatorState

type TestValidatorState struct {
	GetMinimumHeightF       func(context.Context) (uint64, error)
	GetCurrentHeightF       func(context.Context) (uint64, error)
	GetSubnetIDF            func(context.Context, ids.ID) (ids.ID, error)
	GetValidatorSetF        func(context.Context, uint64, ids.ID) (map[ids.NodeID]*validators.GetValidatorOutput, error)
	GetCurrentValidatorSetF func(context.Context, ids.ID) (map[ids.ID]*validators.GetCurrentValidatorOutput, uint64, error)
}

TestValidatorState is a test implementation of validator state

func NewTestValidatorState

func NewTestValidatorState() *TestValidatorState

func (*TestValidatorState) ApplyValidatorPublicKeyDiffs

func (tvs *TestValidatorState) ApplyValidatorPublicKeyDiffs(
	ctx context.Context,
	validators map[ids.NodeID]*validators.GetValidatorOutput,
	startHeight uint64,
	endHeight uint64,
	subnetID ids.ID,
) error

func (*TestValidatorState) ApplyValidatorWeightDiffs

func (tvs *TestValidatorState) ApplyValidatorWeightDiffs(
	ctx context.Context,
	validators map[ids.NodeID]*validators.GetValidatorOutput,
	startHeight uint64,
	endHeight uint64,
	subnetID ids.ID,
) error

func (*TestValidatorState) GetCurrentHeight

func (tvs *TestValidatorState) GetCurrentHeight(ctx context.Context) (uint64, error)

func (*TestValidatorState) GetCurrentValidatorSet

func (tvs *TestValidatorState) GetCurrentValidatorSet(ctx context.Context, subnetID ids.ID) (map[ids.ID]*validators.GetCurrentValidatorOutput, uint64, error)

func (*TestValidatorState) GetMinimumHeight

func (tvs *TestValidatorState) GetMinimumHeight(ctx context.Context) (uint64, error)

func (*TestValidatorState) GetSubnetID

func (tvs *TestValidatorState) GetSubnetID(ctx context.Context, chainID ids.ID) (ids.ID, error)

func (*TestValidatorState) GetValidatorSet

func (tvs *TestValidatorState) GetValidatorSet(ctx context.Context, height uint64, subnetID ids.ID) (map[ids.NodeID]*validators.GetValidatorOutput, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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