common

package
v1.2.19 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2024 License: GPL-3.0 Imports: 15 Imported by: 14

Documentation

Overview

Package common contains various helper functions.

Index

Constants

View Source
const (
	StartBlockTopic    = "start-block"
	EndBlockTopic      = "end-block"
	FinalizeBlockTopic = "finalize-block"
	UnpackedTxnsTopic  = "unpacked-txns"
)
View Source
const (
	FullNode int = iota
	LightNode
	ArchiveNode
)
View Source
const (
	StartBlockStage    = "Start Block"
	ExecuteTxnsStage   = "Execute Txns"
	EndBlockStage      = "End Block"
	FinalizeBlockStage = "Finalize Block"
)
View Source
const (
	// HashLength is the expected length of the hash
	HashLen = 32
	// AddressLength is the expected length of the address
	AddressLen = 20
	// len(BlockNum) + HashLen = 40
	BlockIdLen = 40
)
View Source
const Separator = "|"

Variables

View Source
var (
	NullHash    Hash    = [HashLen]byte{}
	NullAddress Address = [AddressLen]byte{}
)

Functions

func BindJsonParams

func BindJsonParams(params string, v interface{}) error

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 FromHex

func FromHex(s string) []byte

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

func HashesToBytes

func HashesToBytes(hs []Hash) []byte

func HashesToHex

func HashesToHex(hs []Hash) string

HashesToHex returns hash_hex|hash_hex|...

func HashesToTwoBytes

func HashesToTwoBytes(hs []Hash) (byts [][]byte)

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 IsHexAddress

func IsHexAddress(s string) bool

IsHexAddress verifies whether a string can represent a valid hex-encoded Ethereum address or not.

func Keccak256

func Keccak256(data ...[]byte) []byte

func LeftPadBytes

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

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

func RightPadBytes

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

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

func Sha256

func Sha256(data ...[]byte) []byte

func ToHex

func ToHex(b []byte) string

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

func ToHexArray

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

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

Types

type Address

type Address [AddressLen]byte

func BigToAddress

func BigToAddress(b *big.Int) Address

BigToAddress returns Address with byte values of b. If b is larger than len(h), b will be cropped from the left.

func BytesToAddress

func BytesToAddress(b []byte) Address

BytesToAddress returns Address with value b. If b is larger than len(h), b will be cropped from the left.

func HexToAddress

func HexToAddress(s string) Address

HexToAddress returns Address with byte values of s. If s is larger than len(h), s will be cropped from the left.

func (Address) Bytes

func (a Address) Bytes() []byte

Bytes gets the string representation of the underlying address.

func (Address) Format

func (a Address) 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 (Address) Hash

func (a Address) Hash() Hash

Hash converts an address to a hash by left-padding it with zeros.

func (Address) Hex

func (a Address) Hex() string

Hex returns an EIP55-compliant hex string representation of the address.

func (Address) ImplementsGraphQLType

func (a Address) ImplementsGraphQLType(name string) bool

ImplementsGraphQLType returns true if Hash implements the specified GraphQL type.

func (Address) MarshalText

func (a Address) MarshalText() ([]byte, error)

MarshalText returns the hex representation of a.

func (*Address) Scan

func (a *Address) Scan(src interface{}) error

Scan implements Scanner for database/sql.

func (*Address) SetBytes

func (a *Address) SetBytes(b []byte)

SetBytes sets the address to the value of b. If b is larger than len(a) it will panic.

func (Address) String

func (a Address) String() string

String implements fmt.Stringer.

func (*Address) UnmarshalGraphQL

func (a *Address) UnmarshalGraphQL(input interface{}) error

UnmarshalGraphQL unmarshals the provided GraphQL query data.

func (*Address) UnmarshalJSON

func (a *Address) UnmarshalJSON(input []byte) error

UnmarshalJSON parses a hash in hex syntax.

func (*Address) UnmarshalText

func (a *Address) UnmarshalText(input []byte) error

UnmarshalText parses a hash in hex syntax.

func (Address) Value

func (a Address) Value() (driver.Value, error)

Value implements valuer for database/sql.

type BlockId

type BlockId [BlockIdLen]byte

BlockId Uses to be a Key to store into KVDB. Add BlockHash to the BlockNum's end.

func DecodeBlockId

func DecodeBlockId(byt []byte) (id BlockId)

func NewBlockId

func NewBlockId(bn BlockNum, hash Hash) BlockId

func (BlockId) Bytes

func (bi BlockId) Bytes() []byte

func (BlockId) Separate

func (bi BlockId) Separate() (bn BlockNum, hash Hash)

type BlockNum

type BlockNum uint32

func BytesToBlockNum

func BytesToBlockNum(byt []byte) BlockNum

func StrToBlockNum

func StrToBlockNum(str string) (BlockNum, error)

func (BlockNum) Bytes

func (bn BlockNum) Bytes() []byte

type CallType

type CallType int

CallType is Writing or Reading

type Hash

type Hash [HashLen]byte

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 BytesToHashes

func BytesToHashes(data []byte) []Hash

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 HexToHashes

func HexToHashes(s string) (hs []Hash)

func Keccak256Hash

func Keccak256Hash(data ...[]byte) (h Hash)

Keccak256Hash calculates and returns the Keccak256 hash of the input data, converting it to an internal Hash data structure.

func TwoBytesToHashes

func TwoBytesToHashes(byts [][]byte) (hs []Hash)

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) ImplementsGraphQLType

func (_ Hash) ImplementsGraphQLType(name string) bool

ImplementsGraphQLType returns true if Hash implements the specified GraphQL type.

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(b []byte)

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

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) UnmarshalGraphQL

func (h *Hash) UnmarshalGraphQL(input interface{}) error

UnmarshalGraphQL unmarshals the provided GraphQL query data.

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 MerkleHash

type MerkleHash []byte

type MixedcaseAddress

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

MixedcaseAddress retains the original string, which may or may not be correctly checksummed

func NewMixedcaseAddress

func NewMixedcaseAddress(addr Address) MixedcaseAddress

NewMixedcaseAddress constructor (mainly for testing)

func NewMixedcaseAddressFromString

func NewMixedcaseAddressFromString(hexaddr string) (*MixedcaseAddress, error)

NewMixedcaseAddressFromString is mainly meant for unit-testing

func (*MixedcaseAddress) Address

func (ma *MixedcaseAddress) Address() Address

Address returns the address

func (*MixedcaseAddress) MarshalJSON

func (ma *MixedcaseAddress) MarshalJSON() ([]byte, error)

MarshalJSON marshals the original value

func (*MixedcaseAddress) Original

func (ma *MixedcaseAddress) Original() string

Original returns the mixed-case input string

func (*MixedcaseAddress) String

func (ma *MixedcaseAddress) String() string

String implements fmt.Stringer

func (*MixedcaseAddress) UnmarshalJSON

func (ma *MixedcaseAddress) UnmarshalJSON(input []byte) error

UnmarshalJSON parses MixedcaseAddress

func (*MixedcaseAddress) ValidChecksum

func (ma *MixedcaseAddress) ValidChecksum() bool

ValidChecksum returns true if the address has valid checksum

type RdCall

type RdCall struct {
	TripodName string `json:"tripod_name"`
	FuncName   string `json:"func_name"`
	Params     string `json:"params"`
	BlockHash  string `json:"block_hash,omitempty"`
}

RdCall from clients, it is an instance of an 'Read'.

type RunMode

type RunMode int
const (
	LocalNode RunMode = iota
	MasterWorker
)

type UnprefixedAddress

type UnprefixedAddress Address

UnprefixedAddress allows marshaling an Address without 0x prefix.

func (UnprefixedAddress) MarshalText

func (a UnprefixedAddress) MarshalText() ([]byte, error)

MarshalText encodes the address as hex.

func (*UnprefixedAddress) UnmarshalText

func (a *UnprefixedAddress) UnmarshalText(input []byte) error

UnmarshalText decodes the address from hex. The 0x prefix is optional.

type UnprefixedHash

type UnprefixedHash Hash

UnprefixedHash allows marshaling a Hash without 0x prefix.

func (UnprefixedHash) MarshalText

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

MarshalText encodes the hash as hex.

func (*UnprefixedHash) UnmarshalText

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

UnmarshalText decodes the hash from hex. The 0x prefix is optional.

type WrCall

type WrCall struct {
	ChainID    uint64 `json:"chain_id"`
	TripodName string `json:"tripod_name"`
	FuncName   string `json:"func_name"`
	Params     string `json:"params"`
	// TODO: make LeiPrice and Tips as a sortable interface.
	LeiPrice uint64 `json:"lei_price,omitempty"`
	Tips     uint64 `json:"tips,omitempty"`
}

WrCall from clients, it is an instance of an 'Writing'.

func (*WrCall) BindJsonParams

func (e *WrCall) BindJsonParams(v interface{}) error

func (*WrCall) Hash

func (e *WrCall) Hash() ([]byte, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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