utils

package
v0.6.65 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2020 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultFilePermissions specifies that the user can
	// read and write the file.
	DefaultFilePermissions = 0600

	// AllFilePermissions specifies anyone can do anything
	// to the file.
	AllFilePermissions = 0777

	// NanosecondsInMillisecond is the number
	// of nanoseconds in a millisecond.
	NanosecondsInMillisecond = 1000000

	// MillisecondsInSecond is the number
	// of milliseconds in a second.
	MillisecondsInSecond = 1000

	// OneHundred is the number 100.
	OneHundred = 100
)

Variables

View Source
var (
	// ErrNetworkNotSupported is returned when the network
	// you are attempting to connect to is not supported.
	ErrNetworkNotSupported = errors.New("network not supported")

	// OneHundredInt is a big.Int of value 100.
	OneHundredInt = big.NewInt(OneHundred)

	// ZeroInt is a big.Int of value 0.
	ZeroInt = big.NewInt(0)
)

Functions

func AtTip

func AtTip(
	tipDelay int64,
	blockTimestamp int64,
) bool

AtTip returns a boolean indicating if a block timestamp is within tipDelay from the current time.

func BigPow10

func BigPow10(e int32) *big.Float

BigPow10 computes the value of 10^e. Inspired by: https://steemit.com/tutorial/@gopher23/power-and-root-functions-using-big-float-in-golang

func BtoMb

func BtoMb(b float64) float64

BtoMb converts B to MB.

func CheckAtTip

func CheckAtTip(
	ctx context.Context,
	networkIdentifier *types.NetworkIdentifier,
	helper FetcherHelper,
	tipDelay int64,
) (bool, error)

CheckAtTip returns a boolean indicating if a Rosetta implementation is at tip.

func CheckNetworkSupported

func CheckNetworkSupported(
	ctx context.Context,
	networkIdentifier *types.NetworkIdentifier,
	helper FetcherHelper,
) (*types.NetworkStatusResponse, error)

CheckNetworkSupported checks if a Rosetta implementation supports a given *types.NetworkIdentifier. If it does, the current network status is returned.

func ContainsAccountIdentifier

func ContainsAccountIdentifier(arr []*types.AccountIdentifier, s *types.AccountIdentifier) bool

ContainsAccountIdentifier returns a boolean indicating whether the struct s is in arr.

func ContainsString

func ContainsString(arr []string, s string) bool

ContainsString returns a boolean indicating whether the string s is in arr.

func ContextSleep

func ContextSleep(ctx context.Context, duration time.Duration) error

ContextSleep sleeps for the provided duration and returns an error if context is canceled.

func CreateCommandPath

func CreateCommandPath(
	dataDirectory string,
	cmd string,
	network *types.NetworkIdentifier,
) (string, error)

CreateCommandPath creates a unique path for a command and network within a data directory. This is used to avoid collision when using multiple commands on multiple networks when the same storage resources are used. If the derived path does not exist, we run os.MkdirAll on the path.

func CreateTempDir

func CreateTempDir() (string, error)

CreateTempDir creates a directory in /tmp for usage within testing.

func CurrencyBalance

func CurrencyBalance(
	ctx context.Context,
	network *types.NetworkIdentifier,
	helper FetcherHelper,
	account *types.AccountIdentifier,
	currency *types.Currency,
	index int64,
) (*types.Amount, *types.BlockIdentifier, error)

CurrencyBalance returns the balance of an account for a particular currency at a particular height. It is up to the caller to determine if the retrieved block has the expected hash for the requested index.

func EnsurePathExists

func EnsurePathExists(path string) error

EnsurePathExists creates directories along a path if they do not exist.

func Equal

func Equal(a interface{}, b interface{}) bool

Equal returns a boolean indicating if two interfaces are equal.

func LoadAndParse

func LoadAndParse(filePath string, output interface{}) error

LoadAndParse reads the file at the provided path and attempts to unmarshal it into output.

func Milliseconds

func Milliseconds() int64

Milliseconds gets the current time in milliseconds.

func PrettyAmount

func PrettyAmount(amount *big.Int, currency *types.Currency) string

PrettyAmount returns a currency amount in native format with its symbol.

func RandomNumber

func RandomNumber(minimum *big.Int, maximum *big.Int) *big.Int

RandomNumber returns some number in the range [minimum, maximum). Source: https://golang.org/pkg/crypto/rand/#Int

func RemoveTempDir

func RemoveTempDir(dir string)

RemoveTempDir deletes a directory at a provided path for usage within testing.

func SerializeAndWrite

func SerializeAndWrite(filePath string, object interface{}) error

SerializeAndWrite attempts to serialize the provided object into a file at filePath.

func SizeOf

func SizeOf(v interface{}) int

SizeOf returns the size of 'v' in bytes. If there is an error during calculation, Of returns -1.

func TimeToTip

func TimeToTip(
	blocksPerSecond float64,
	lastSyncedIndex int64,
	tipIndex int64,
) time.Duration

TimeToTip returns the estimate time to tip given the current sync speed.

func Zero

func Zero() *big.Float

Zero returns a float with 256 bit precision.

Types

type AccountBalance

type AccountBalance struct {
	Account *types.AccountIdentifier
	Amount  *types.Amount
	Coins   []*types.Coin
	Block   *types.BlockIdentifier
}

AccountBalance defines an account's balance, including either balance or coins, as well as the block which this balance was fetched at.

func GetAccountBalances

func GetAccountBalances(
	ctx context.Context,
	fetcher FetcherHelper,
	balanceRequests []*AccountBalanceRequest,
) ([]*AccountBalance, error)

GetAccountBalances returns an array of AccountBalances for an array of AccountBalanceRequests

type AccountBalanceRequest

type AccountBalanceRequest struct {
	Account  *types.AccountIdentifier
	Network  *types.NetworkIdentifier
	Currency *types.Currency
}

AccountBalanceRequest defines the required information to get an account's balance.

type BST

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

BST is an implementation of a binary search tree.

func (*BST) Delete

func (t *BST) Delete(key int64)

Delete removes the key from the BST.

func (*BST) Empty

func (t *BST) Empty() bool

Empty returns a boolean indicating if there are any nodes in the BST.

func (*BST) Get

func (t *BST) Get(key int64) *Node

Get gets the key in the BST and returns its representative node (so that modifications can be done in constant time).

func (*BST) Min

func (t *BST) Min() *Node

Min returns the smallest node in the BST.

func (*BST) Set

func (t *BST) Set(key int64, value int)

Set stores the key and value in the BST

type FetcherHelper

type FetcherHelper interface {
	NetworkList(
		ctx context.Context,
		metadata map[string]interface{},
	) (*types.NetworkListResponse, *fetcher.Error)

	NetworkStatusRetry(
		ctx context.Context,
		network *types.NetworkIdentifier,
		metadata map[string]interface{},
	) (*types.NetworkStatusResponse, *fetcher.Error)

	AccountBalanceRetry(
		ctx context.Context,
		network *types.NetworkIdentifier,
		account *types.AccountIdentifier,
		block *types.PartialBlockIdentifier,
		currencies []*types.Currency,
	) (*types.BlockIdentifier, []*types.Amount, map[string]interface{}, *fetcher.Error)
}

FetcherHelper is used by util functions to mock Fetcher

type MemoryUsage

type MemoryUsage struct {
	Heap               float64 `json:"heap"`
	Stack              float64 `json:"stack"`
	OtherSystem        float64 `json:"other_system"`
	System             float64 `json:"system"`
	GarbageCollections uint32  `json:"garbage_collections"`
}

MemoryUsage contains memory usage stats converted to MBs.

func MonitorMemoryUsage

func MonitorMemoryUsage(
	ctx context.Context,
	maxHeapUsage int,
) *MemoryUsage

MonitorMemoryUsage returns a collection of memory usage stats in MB. It will also run garbage collection if the heap is greater than maxHeapUsage in MB.

type Node

type Node struct {
	Key   int64
	Value int
	// contains filtered or unexported fields
}

Node is a Node in a BST

Jump to

Keyboard shortcuts

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