common

package
v0.0.0-...-168a2c3 Latest Latest
Warning

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

Go to latest
Published: May 30, 2019 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package common contains various helper functions.

Index

Constants

View Source
const (
	// HashLength is the expected length of the hash
	HashLength = sha256.Size
)

Variables

View Source
var (
	// NodeNameDict is an optional dictionary to make node address human readable in log.
	NodeNameDict = make(map[Address]string)
)

Functions

func Bytes2Hex

func Bytes2Hex(d []byte) string

Bytes2Hex returns the hexadecimal encoding of d.

func CopyBytes

func CopyBytes(b []byte) (copiedBytes []byte)

CopyBytes returns an exact copy of the provided bytes.

func FromECDSAPub

func FromECDSAPub(pub *ecdsa.PublicKey) []byte

FromECDSAPub create bytes from ECDSA public key. NOTE: deprecated

func FromHex

func FromHex(s string) []byte

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

func Hash64

func Hash64(data []byte) uint64

Hash64 TODO

func Hex2Bytes

func Hex2Bytes(str string) []byte

Hex2Bytes returns the bytes represented by the hexadecimal string str.

func Hex2BytesFixed

func Hex2BytesFixed(str string, flen int) []byte

Hex2BytesFixed returns bytes of a specified fixed length flen.

func Is

func Is(err error, t StoreErrType) bool

Is checks if store error type

func LeftPadBytes

func LeftPadBytes(slice []byte, l int) []byte

LeftPadBytes zero-pads slice to the left up to length l.

func NewTestLogger

func NewTestLogger(t testing.TB) *logrus.Logger

NewTestLogger constructor

func RightPadBytes

func RightPadBytes(slice []byte, l int) []byte

RightPadBytes zero-pads slice to the right up to length l.

func ToECDSAPub

func ToECDSAPub(pub []byte) *ecdsa.PublicKey

ToECDSAPub convert to ECDSA public key from bytes. NOTE: deprecated

func ToHex deprecated

func ToHex(b []byte) string

ToHex returns the hex representation of b, prefixed with '0x'. For empty slices, the return value is "0x0".

Deprecated: use hexutil.Encode instead.

func ToHexArray

func ToHexArray(b [][]byte) []string

ToHexArray creates a array of hex-string based on []byte

Types

type Address

type Address Hash

Address is a unique identifier of Node. It is a hash of node's PubKey.

func BytesToAddress

func BytesToAddress(b []byte) Address

BytesToAddress sets b to address. If b is larger than len(h), b will be cropped from the left.

func FakeAddress

func FakeAddress() Address

FakeAddress generates random fake address for testing purpose.

func (*Address) Bytes

func (a *Address) Bytes() []byte

Bytes returns value as byte slice.

func (*Address) String

func (a *Address) String() string

String returns human readable string representation.

type Hash

type Hash [HashLength]byte

Hash represents the 32 byte Keccak256 hash of arbitrary data.

func BigToHash

func BigToHash(b *big.Int) Hash

BigToHash sets byte representation of b to hash. If b is larger than len(h), b will be cropped from the left.

func BytesToHash

func BytesToHash(b []byte) Hash

BytesToHash sets b to hash. If b is larger than len(h), b will be cropped from the left.

func FakeHash

func FakeHash() (h Hash)

FakeHash generates random fake hash for testing purpose.

func HexToHash

func HexToHash(s string) Hash

HexToHash sets byte representation of s to hash. If b is larger than len(h), b will be cropped from the left.

func (Hash) Big

func (h Hash) Big() *big.Int

Big converts a hash to a big integer.

func (Hash) Bytes

func (h Hash) Bytes() []byte

Bytes gets the byte representation of the underlying hash.

func (Hash) Format

func (h Hash) Format(s fmt.State, c rune)

Format implements fmt.Formatter, forcing the byte slice to be formatted as is, without going through the stringer interface used for logging.

func (Hash) Generate

func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value

Generate implements testing/quick.Generator.

func (Hash) Hex

func (h Hash) Hex() string

Hex converts a hash to a hex string.

func (Hash) MarshalText

func (h Hash) MarshalText() ([]byte, error)

MarshalText returns the hex representation of h.

func (*Hash) Scan

func (h *Hash) Scan(src interface{}) error

Scan implements Scanner for database/sql.

func (*Hash) SetBytes

func (h *Hash) SetBytes(raw []byte)

SetBytes sets the hash to the value of b.

func (Hash) ShortString

func (h Hash) ShortString() string

ShortString returns short string representation.

func (Hash) String

func (h Hash) String() string

String implements the stringer interface and is used also by the logger when doing full logging into a file.

func (Hash) TerminalString

func (h Hash) TerminalString() string

TerminalString implements log.TerminalStringer, formatting a string for console output during logging.

func (*Hash) UnmarshalJSON

func (h *Hash) UnmarshalJSON(input []byte) error

UnmarshalJSON parses a hash in hex syntax.

func (*Hash) UnmarshalText

func (h *Hash) UnmarshalText(input []byte) error

UnmarshalText parses a hash in hex syntax.

func (Hash) Value

func (h Hash) Value() (driver.Value, error)

Value implements valuer for database/sql.

type Int64Slice

type Int64Slice []int64

Int64Slice allows sorting of 64-bit integer slices, like PendingRoundReceived

func (Int64Slice) Len

func (a Int64Slice) Len() int

func (Int64Slice) Less

func (a Int64Slice) Less(i, j int) bool

func (Int64Slice) Swap

func (a Int64Slice) Swap(i, j int)

type PrivateKey

type PrivateKey ecdsa.PrivateKey

PrivateKey is a private key wrapper.

func (*PrivateKey) Public

func (key *PrivateKey) Public() *PublicKey

Public returns public part of key.

func (*PrivateKey) Sign

func (key *PrivateKey) Sign(hash []byte) (r, s *big.Int, err error)

Sign signs with key.

type PublicKey

type PublicKey ecdsa.PublicKey

PublicKey is a public key wrapper.

func Base64ToPubkey

func Base64ToPubkey(s string) (*PublicKey, error)

Base64ToPubkey decodes public key from base64.

func BytesToPubkey

func BytesToPubkey(pub []byte) *PublicKey

BytesToPubkey decodes public key from bytes.

func StringToPubkey

func StringToPubkey(pub string) (*PublicKey, error)

StringToPubkey decode public key from base64 to common.PublicKey NOTE: deprecated

func (*PublicKey) Base64

func (pub *PublicKey) Base64() string

Base64 encodes public key to base64.

func (*PublicKey) Bytes

func (pub *PublicKey) Bytes() []byte

Bytes encodes public key to bytes.

func (*PublicKey) Verify

func (pub *PublicKey) Verify(hash []byte, r, s *big.Int) bool

Verify verifies the signatures.

type RollingIndex

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

RollingIndex struct

func NewRollingIndex

func NewRollingIndex(name string, size int) *RollingIndex

NewRollingIndex constructor

func (*RollingIndex) Get

func (r *RollingIndex) Get(skipIndex int64) ([]interface{}, error)

Get with skipIndex option

func (*RollingIndex) GetItem

func (r *RollingIndex) GetItem(index int64) (interface{}, error)

GetItem get item for a given index

func (*RollingIndex) GetLastWindow

func (r *RollingIndex) GetLastWindow() (lastWindow []interface{}, lastIndex int64)

GetLastWindow getter

func (*RollingIndex) Set

func (r *RollingIndex) Set(item interface{}, index int64) error

Set item for given index

type RollingIndexMap

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

RollingIndexMap struct

func NewRollingIndexMap

func NewRollingIndexMap(name string, size int, keys []uint64) *RollingIndexMap

NewRollingIndexMap constructor

func (*RollingIndexMap) AddKey

func (rim *RollingIndexMap) AddKey(key uint64) error

AddKey adds a key to map if it doesn't already exist, returns an error if it already exists

func (*RollingIndexMap) Get

func (rim *RollingIndexMap) Get(key uint64, skipIndex int64) ([]interface{}, error)

Get return key items with index > skip

func (*RollingIndexMap) GetItem

func (rim *RollingIndexMap) GetItem(key uint64, index int64) (interface{}, error)

GetItem returns the item in rolling index for given key

func (*RollingIndexMap) GetLast

func (rim *RollingIndexMap) GetLast(key uint64) (interface{}, error)

GetLast get last item for given key

func (*RollingIndexMap) Import

func (rim *RollingIndexMap) Import(other *RollingIndexMap)

Import from another index

func (*RollingIndexMap) Known

func (rim *RollingIndexMap) Known() map[uint64]int64

Known returns [key] => lastKnownIndex

func (*RollingIndexMap) Reset

func (rim *RollingIndexMap) Reset() error

Reset resets the map

func (*RollingIndexMap) Set

func (rim *RollingIndexMap) Set(key uint64, item interface{}, index int64) error

Set sets a given key for the index

type StorageSize

type StorageSize float64

StorageSize is a wrapper around a float value that supports user friendly formatting.

func (StorageSize) String

func (s StorageSize) String() string

String implements the stringer interface.

func (StorageSize) TerminalString

func (s StorageSize) TerminalString() string

TerminalString implements log.TerminalStringer, formatting a string for console output during logging.

type StoreErr

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

StoreErr storage error

func NewStoreErr

func NewStoreErr(dataType string, errType StoreErrType, key string) StoreErr

NewStoreErr constructor

func (StoreErr) Error

func (e StoreErr) Error() string

type StoreErrType

type StoreErrType uint32

StoreErrType store error type

const (
	// KeyNotFound PEM key not found
	KeyNotFound StoreErrType = iota
	// TooLate TODO
	TooLate
	// PassedIndex passed the index lookup
	PassedIndex
	// SkippedIndex skipped the index lookup
	SkippedIndex
	// NoRoot no root to load
	NoRoot
	// UnknownParticipant signed participant is not known
	UnknownParticipant
	// Empty TODO
	Empty
	// KeyAlreadyExists key already exists in the store
	KeyAlreadyExists
)

Directories

Path Synopsis
Package hexutil implements hex encoding with 0x prefix.
Package hexutil implements hex encoding with 0x prefix.

Jump to

Keyboard shortcuts

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