utils

package
v0.0.25 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2022 License: LGPL-3.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddLogFile

func AddLogFile(filepath string, maxSize int, rotateCount int, rotateMaxAge int)

AddLogFile creates a StreamHandler that outputs JSON logs into rotating files with specified max file size and storing at max rotateCount files

func AddLogHandler

func AddLogHandler(handler log.Handler)

AddLogHandler add a log handler

func AnalysisEnd

func AnalysisEnd(name string, more ...interface{})

AnalysisEnd ..

func AnalysisStart

func AnalysisStart(name string, more ...interface{})

AnalysisStart ..

func AppendIfMissing

func AppendIfMissing(slice []common.Address, addr common.Address) ([]common.Address, bool)

AppendIfMissing appends an item if it's missing in the slice, returns appended slice and true Otherwise, return the original slice and false

func AskForPassphrase

func AskForPassphrase(prompt string) string

AskForPassphrase return passphrase using password input

func CountOneBits

func CountOneBits(arr []byte) int64

CountOneBits counts the number of 1 bit in byte array

func FatalErrMsg

func FatalErrMsg(err error, format string, args ...interface{})

FatalErrMsg prints the given error wrapped with the given message in the extended format (%+v) onto stderr, then exits with status 1.

func FatalError

func FatalError(err error)

FatalError prints the given error in the extended format (%+v) onto stderr, then exits with status 1.

func FromHex

func FromHex(s string) []byte

FromHex returns the bytes represented by the hexadecimal string s. s may be prefixed with "0x".

func GenKeyP2P

func GenKeyP2P(ip, port string) (p2p_crypto.PrivKey, p2p_crypto.PubKey, error)

GenKeyP2P generates a pair of RSA keys used in libp2p host

func GenKeyP2PRand

func GenKeyP2PRand() (p2p_crypto.PrivKey, p2p_crypto.PubKey, error)

GenKeyP2PRand generates a pair of RSA keys used in libp2p host, using random seed

func GetAddressFromBLSPubKeyBytes

func GetAddressFromBLSPubKeyBytes(pubKeyBytes []byte) common.Address

GetAddressFromBLSPubKeyBytes return the address object from bls pub key.

func GetCallStackInfo

func GetCallStackInfo(depthList ...int) string

GetCallStackInfo return a string containing the file name, function name and the line number of a specified entry on the call stack. Inspired by https://github.com/jimlawless/whereami

func GetLogInstance

func GetLogInstance() log.Logger

GetLogInstance returns logging singleton.

func GetLogger

func GetLogger() log.Logger

GetLogger is a shorthand for WithCaller(GetLogInstance()).

func GetPassphraseFromSource

func GetPassphraseFromSource(src string) (pass string, err error)

GetPassphraseFromSource reads a passphrase such as a key-encrypting one non-interactively from the given source.

The source can be "pass:password", "env:var", "file:pathname", "fd:number", or "stdin". See “PASS PHRASE ARGUMENTS” section of openssl(1) for details.

When "stdin" or "fd:" is used, the standard input or the given file descriptor is exhausted. Therefore, this function should be called at most once per program invocation; the second call, if any, may return an empty string if "stdin" or "fd" is used.

func GetPendingCXKey

func GetPendingCXKey(shardID uint32, blockNum uint64) string

GetPendingCXKey creates pending CXReceiptsProof key given shardID and blockNum it is to avoid adding duplicated CXReceiptsProof from the same source shard

func GetUniqueIDFromIPPort

func GetUniqueIDFromIPPort(ip, port string) uint32

GetUniqueIDFromIPPort --

func Hex2Bytes

func Hex2Bytes(str string) []byte

Hex2Bytes returns the bytes represented by the hexadecimal string str.

func IsPrivateIP

func IsPrivateIP(ip net.IP) bool

IsPrivateIP checks if an IP address is private or not

func Load

func Load(path string, v interface{}) error

Load loads the file at path into v.

func LoadKeyFromFile

func LoadKeyFromFile(keyfile string) (key p2p_crypto.PrivKey, pk p2p_crypto.PubKey, err error)

LoadKeyFromFile load private key from keyfile If the private key is not loadable or no file, it will generate a new random private key

func LoadPrivateKey

func LoadPrivateKey(key string) (p2p_crypto.PrivKey, p2p_crypto.PubKey, error)

LoadPrivateKey parses the key string in base64 format and return PrivKey

func Logger

func Logger() *zerolog.Logger

Logger returns a zerolog.Logger singleton

func Marshal

func Marshal(v interface{}) (io.Reader, error)

Marshal is a function that marshals the object into an io.Reader.

func PrintError

func PrintError(err error)

PrintError prints the given error in the extended format (%+v) onto stderr.

func SampledLogger

func SampledLogger() *zerolog.Logger

SampledLogger returns a sampled zerolog singleton to be used in criticial path like p2p message handling

func Save

func Save(path string, v interface{}) error

Save saves a representation of v to the file at path.

func SaveKeyToFile

func SaveKeyToFile(keyfile string, key p2p_crypto.PrivKey) (err error)

SaveKeyToFile save private key to keyfile

func SavePrivateKey

func SavePrivateKey(key p2p_crypto.PrivKey) (string, error)

SavePrivateKey convert the PrivKey to base64 format and return string

func SetLogContext

func SetLogContext(_port, _ip string)

SetLogContext used to print out loggings of node with port and ip. Every instance (node, etc..) needs to set this for logging.

func SetLogVerbosity

func SetLogVerbosity(verbosity log.Lvl)

SetLogVerbosity specifies the verbosity of global logger

func Unmarshal

func Unmarshal(r io.Reader, v interface{}) error

Unmarshal is a function that unmarshals the data from the reader into the specified value.

func WithCaller

func WithCaller(logger log.Logger) log.Logger

WithCaller logs the caller info in three context items "funcFile", "funcLine", and "funcName".

func WithCallerSkip

func WithCallerSkip(logger log.Logger, skip int) log.Logger

WithCallerSkip logs the caller info in three context items "funcFile", "funcLine", and "funcName". skip is the number of frames to skip. 0 adds the info of the caller of this WithCallerSkip.

Types

type PrivKeyStore

type PrivKeyStore struct {
	Key string `json:"key"`
}

PrivKeyStore is used to persist private key to/from file

type Timeout

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

Timeout is the implementation of timeout

func NewTimeout

func NewTimeout(d time.Duration) *Timeout

NewTimeout creates a new timeout class

func (*Timeout) CheckExpire

func (timeout *Timeout) CheckExpire() bool

CheckExpire checks whether the timeout is reached/expired

func (*Timeout) Duration

func (timeout *Timeout) Duration() time.Duration

Duration returns the duration period of timeout

func (*Timeout) IsActive

func (timeout *Timeout) IsActive() bool

IsActive checks whether timeout clock is active; A timeout is active means it's not stopped caused by stop and also not expired with time elapses longer than duration from start

func (*Timeout) SetDuration

func (timeout *Timeout) SetDuration(nd time.Duration)

SetDuration set new duration for the timer

func (*Timeout) Start

func (timeout *Timeout) Start()

Start starts the timeout clock

func (*Timeout) Stop

func (timeout *Timeout) Stop()

Stop stops the timeout clock

type TimeoutState

type TimeoutState int

TimeoutState indicates the state of Timeout class

const (
	Active TimeoutState = iota
	Inactive
	Expired
)

Enum for different TimeoutState

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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