utils

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2025 License: GPL-3.0, LGPL-3.0 Imports: 22 Imported by: 25

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 added in v0.8.0

func ComputeHash256(data []byte) []byte

ComputeHash256 computes SHA256 hash and returns it as bytes

func ComputeHash256Array added in v0.8.0

func ComputeHash256Array(data []byte) interfaces.ID

ComputeHash256Array computes SHA256 hash and returns it as an ID

func ConvertToChainContext added in v0.8.0

func ConvertToChainContext(ctx *consensus.Context) *iface.ChainContext

ConvertToChainContext converts a consensus.Context to iface.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 NewLRUCache added in v0.8.0

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

NewLRUCache creates a new LRU cache with the specified capacity

func NewMockableClock added in v0.8.0

func NewMockableClock() interfaces.MockableTimer

NewMockableClock creates a new mockable clock

func NewSet added in v0.8.0

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

NewSet creates a new set with optional initial capacity

func NewUint64

func NewUint64(val uint64) *uint64

func TestChainContext added in v0.8.0

func TestChainContext() *iface.ChainContext

TestChainContext returns a test ChainContext

func TestConsensusContext added in v0.7.10

func TestConsensusContext() *consensus.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 EmptyCache added in v0.8.0

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

EmptyCache is a cache that never stores anything

func (*EmptyCache[K, V]) Evict added in v0.8.0

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

Evict does nothing

func (*EmptyCache[K, V]) Flush added in v0.8.0

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

Flush does nothing

func (*EmptyCache[K, V]) Get added in v0.8.0

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

Get always returns false

func (*EmptyCache[K, V]) Len added in v0.8.0

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

Len always returns 0

func (*EmptyCache[K, V]) Put added in v0.8.0

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

Put does nothing

type LRUCache added in v0.8.0

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 added in v0.8.0

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

Evict removes a key from the cache

func (*LRUCache[K, V]) Flush added in v0.8.0

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

Flush clears the cache

func (*LRUCache[K, V]) Get added in v0.8.0

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

Get retrieves a value from the cache

func (*LRUCache[K, V]) Len added in v0.8.0

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

Len returns the number of items in the cache

func (*LRUCache[K, V]) Put added in v0.8.0

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 added in v0.8.0

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

MockableClock implements interfaces.MockableTimer

func (*MockableClock) Advance added in v0.8.0

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

Advance advances the time by duration

func (*MockableClock) Set added in v0.8.0

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

Set sets the current time

func (*MockableClock) Time added in v0.8.0

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

Time returns the current time

type Set added in v0.8.0

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

Set implements a generic set data structure

func (*Set[T]) Add added in v0.8.0

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

Add adds items to the set

func (*Set[T]) Clear added in v0.8.0

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

Clear clears all items

func (*Set[T]) Contains added in v0.8.0

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

Contains checks if an item is in the set

func (*Set[T]) Len added in v0.8.0

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

Len returns the number of items

func (*Set[T]) List added in v0.8.0

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

List returns all items as a slice

func (*Set[T]) Remove added in v0.8.0

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

Remove removes an item from the set

type TestValidatorState added in v0.8.0

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) GetCurrentHeight added in v0.8.0

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

func (*TestValidatorState) GetCurrentValidatorSet added in v0.8.0

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

func (*TestValidatorState) GetMinimumHeight added in v0.8.0

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

func (*TestValidatorState) GetSubnetID added in v0.8.0

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

func (*TestValidatorState) GetValidatorSet added in v0.8.0

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