Documentation ¶
Overview ¶
Package common contains various helper functions.
Index ¶
- Constants
- Variables
- func AllZero(b []byte) bool
- func AppDataDir(appName string, roaming bool) string
- func BigIntMustFromString(number string) *big.Int
- func Bytes2Hex(d []byte) string
- func CalcMemSize(off, l *big.Int) *big.Int
- func CopyBytes(b []byte) (copiedBytes []byte)
- func Decode(input string) ([]byte, error)
- func DecodeBig(input string) (*big.Int, error)
- func DecodeUint64(input string) (uint64, error)
- func Encode(b []byte) string
- func EncodeBig(bigint *big.Int) string
- func EncodeUint64(i uint64) string
- func ExpandPath(p string) string
- func FileExist(filePath string) bool
- func FromHex(s string) []byte
- func GenUnique() ([]byte, error)
- func GetData(data []byte, start uint64, size uint64) []byte
- func GetDataBig(data []byte, start *big.Int, size *big.Int) []byte
- func GlobalBig(ctx *cli.Context, name string) *big.Int
- func HasHexPrefix(str string) bool
- func Hex2Bytes(str string) []byte
- func Hex2BytesFixed(str string, flen int) []byte
- func HmAC(message, key []byte) []byte
- func HomeDir() string
- func IsHex(str string) bool
- func IsHexCharacter(c byte) bool
- func LeftPadBytes(slice []byte, l int) []byte
- func MustDecode(input string) []byte
- func MustDecodeBig(input string) *big.Int
- func MustDecodeUint64(input string) uint64
- func MustParseUint64(s string) uint64
- func ParseUint64(s string) (uint64, bool)
- func RightPadBytes(slice []byte, l int) []byte
- func SafeAdd(x, y uint64) (uint64, bool)
- func SafeMul(x, y uint64) (uint64, bool)
- func SafeSub(x, y uint64) (uint64, bool)
- func SliceEqual(a []interface{}, b []interface{}, cp func(interface{}, interface{}) bool) bool
- func ToHex(b []byte) stringdeprecated
- func ToHexArray(b [][]byte) []string
- func ToWordSize(size uint64) uint64
- func UnmarshalFixedJSON(typ reflect.Type, input, out []byte) error
- func UnmarshalFixedText(typname string, input, out []byte) error
- func UnmarshalFixedUnprefixedText(typname string, input, out []byte) error
- type Big
- func (m Big) MarshalBSONValue() (bsontype.Type, []byte, error)
- func (b Big) MarshalText() ([]byte, error)
- func (m *Big) SetMathBig(v mathBig.Int)
- func (b *Big) String() string
- func (b *Big) ToInt() *mathBig.Int
- func (b *Big) UnmarshalBSON(data []byte) error
- func (b *Big) UnmarshalJSON(input []byte) error
- func (b *Big) UnmarshalText(input []byte) error
- type BigFlag
- type BlockNumber
- type Bytes
- type ConnectionError
- type ConsensusError
- type DataError
- type DefaultError
- type DirectoryFlag
- type DirectoryString
- type DupOpError
- type HexOrDecimal64
- type Merkle
- type MerkleLayer
- type MerkleNode
- type MyError
- type OfflineError
- type PrettyDuration
- type StorageSize
- type TextMarshaler
- type TextMarshalerFlag
- type TimeoutError
- type TransmissionError
- type Uint
- type Uint64
Constants ¶
const ( PendingBlockNumber = BlockNumber(-2) LatestBlockNumber = BlockNumber(-1) EarliestBlockNumber = BlockNumber(0) )
const ( MaxInt8 = 1<<7 - 1 MinInt8 = -1 << 7 MaxInt16 = 1<<15 - 1 MinInt16 = -1 << 15 MaxInt32 = 1<<31 - 1 MinInt32 = -1 << 31 MaxInt64 = 1<<63 - 1 MinInt64 = -1 << 63 MaxUint8 = 1<<8 - 1 MaxUint16 = 1<<16 - 1 MaxUint32 = 1<<32 - 1 MaxUint64 = 1<<64 - 1 )
Integer limit values.
Variables ¶
var ( Big1 = big.NewInt(1) Big2 = big.NewInt(2) Big3 = big.NewInt(3) Big0 = big.NewInt(0) Big32 = big.NewInt(32) Big256 = big.NewInt(256) Big257 = big.NewInt(257) )
var ( ErrEmptyString = &decError{"empty hex string"} ErrSyntax = &decError{"invalid hex string"} ErrMissingPrefix = &decError{"hex string without 0x prefix"} ErrOddLength = &decError{"hex string of odd length"} ErrEmptyNumber = &decError{"hex string \"0x\""} ErrLeadingZero = &decError{"hex number with leading zero digits"} ErrUint64Range = &decError{"hex number > 64 bits"} ErrUintRange = &decError{fmt.Sprintf("hex number > %d bits", uintBits)} ErrBig256Range = &decError{"hex number > 256 bits"} )
Errors
var Version int32 = 1
Functions ¶
func AppDataDir ¶
AppDataDir returns an operating system specific directory to be used for storing application data for an application.
The appName parameter is the name of the application the data directory is being requested for. This function will prepend a period to the appName for POSIX style operating systems since that is standard practice. An empty appName or one with a single dot is treated as requesting the current directory so only "." will be returned. Further, the first character of appName will be made lowercase for POSIX style operating systems and uppercase for Mac and Windows since that is standard practice.
The roaming parameter only applies to Windows where it specifies the roaming application data profile (%APPDATA%) should be used instead of the local one (%LOCALAPPDATA%) that is used by default.
Example results:
dir := AppDataDir("myapp", false) POSIX (Linux/BSD): ~/.myapp Mac OS: $HOME/Library/Application Support/Myapp Windows: %LOCALAPPDATA%\Myapp Plan 9: $home/myapp
func BigIntMustFromString ¶
func DecodeBig ¶
DecodeBig decodes a hex string with 0x prefix as a quantity. Numbers larger than 256 bits are not accepted.
func DecodeUint64 ¶
DecodeUint64 decodes a hex string with 0x prefix as a quantity.
func EncodeBig ¶
EncodeBig encodes bigint as a hex string with 0x prefix. The sign of the integer is ignored.
func EncodeUint64 ¶
EncodeUint64 encodes i as a hex string with 0x prefix.
func ExpandPath ¶
Expands a file path 1. replace tilde with users home dir 2. expands embedded environment variables 3. cleans the path, e.g. /a/b/../c -> /a/c Note, it has limitations, e.g. ~someuser/tmp will not be expanded
func FromHex ¶
FromHex returns the bytes represented by the hexadecimal string s. s may be prefixed with "0x".
func HasHexPrefix ¶
hasHexPrefix validates str begins with '0x' or '0X'.
func Hex2BytesFixed ¶
Hex2BytesFixed returns bytes of a specified fixed length flen.
func IsHexCharacter ¶
isHexCharacter returns bool of c being a valid hexadecimal.
func LeftPadBytes ¶
LeftPadBytes zero-pads slice to the left up to length l.
func MustDecode ¶
MustDecode decodes a hex string with 0x prefix. It panics for invalid input.
func MustDecodeBig ¶
MustDecodeBig decodes a hex string with 0x prefix as a quantity. It panics for invalid input.
func MustDecodeUint64 ¶
MustDecodeUint64 decodes a hex string with 0x prefix as a quantity. It panics for invalid input.
func MustParseUint64 ¶
MustParseUint64 parses s as an integer and panics if the string is invalid.
func ParseUint64 ¶
ParseUint64 parses s as an integer in decimal or hexadecimal syntax. Leading zeros are accepted. The empty string parses as zero.
func RightPadBytes ¶
RightPadBytes zero-pads slice to the right up to length l.
func SliceEqual ¶
func ToHexArray ¶
ToHexArray creates a array of hex-string based on []byte
func ToWordSize ¶
func UnmarshalFixedJSON ¶
UnmarshalFixedJSON decodes the input as a string with 0x prefix. The length of out determines the required input length. This function is commonly used to implement the UnmarshalJSON method for fixed-size types.
func UnmarshalFixedText ¶
UnmarshalFixedText decodes the input as a string with 0x prefix. The length of out determines the required input length. This function is commonly used to implement the UnmarshalText method for fixed-size types.
func UnmarshalFixedUnprefixedText ¶
UnmarshalFixedUnprefixedText decodes the input as a string with optional 0x prefix. The length of out determines the required input length. This function is commonly used to implement the UnmarshalText method for fixed-size types.
Types ¶
type Big ¶
Big marshals/unmarshals as a JSON string with 0x prefix. The zero value marshals as "0x0".
Negative integers are not supported at this time. Attempting to marshal them will return an error. Values larger than 256bits are rejected by Unmarshal but will be marshaled without error.
func (Big) MarshalText ¶
MarshalText implements encoding.TextMarshaler
func (*Big) SetMathBig ¶
func (*Big) UnmarshalBSON ¶
func (*Big) UnmarshalJSON ¶
func (b Big)MarshalJSON()([]byte,error){ buf, err:= b.MarshalText() //bigJson := `"` + string(buf) + `"` return []byte(buf), err }
UnmarshalJSON implements json.Unmarshaler.
func (*Big) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler
type BigFlag ¶
BigFlag is a command line flag that accepts 256 bit big integers in decimal or hexadecimal syntax.
type BlockNumber ¶
type BlockNumber int64
func (BlockNumber) Int64 ¶
func (bn BlockNumber) Int64() int64
func (*BlockNumber) UnmarshalJSON ¶
func (bn *BlockNumber) UnmarshalJSON(data []byte) error
UnmarshalJSON parses the given JSON fragment into a BlockNumber. It supports: - "latest", "earliest" or "pending" as string arguments - the block number Returned errors: - an invalid block number error when the given argument isn't a known strings - an out of range error when the given block number is either too little or too large
type Bytes ¶
type Bytes []byte
Bytes marshals/unmarshals as a JSON string with 0x prefix. The empty slice marshals as "0x".
func (Bytes) MarshalText ¶
MarshalText implements encoding.TextMarshaler
func (*Bytes) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Bytes) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type ConnectionError ¶
type ConnectionError struct {
MyError
}
type ConsensusError ¶
type ConsensusError struct {
MyError
}
type DefaultError ¶
type DefaultError struct {
MyError
}
type DirectoryFlag ¶
type DirectoryFlag struct { Name string Value DirectoryString Usage string }
Custom cli.Flag type which expand the received string to an absolute path. e.g. ~/.ethereum -> /home/username/.ethereum
func (DirectoryFlag) Apply ¶
func (self DirectoryFlag) Apply(set *flag.FlagSet)
called by cli library, grabs variable from environment (if in env) and adds variable to flag set for parsing.
func (DirectoryFlag) GetName ¶
func (self DirectoryFlag) GetName() string
func (*DirectoryFlag) Set ¶
func (self *DirectoryFlag) Set(value string)
func (DirectoryFlag) String ¶
func (self DirectoryFlag) String() string
type DirectoryString ¶
type DirectoryString struct {
Value string
}
Custom type which is registered in the flags library which cli uses for argument parsing. This allows us to expand Value to an absolute path when the argument is parsed
func (*DirectoryString) Set ¶
func (self *DirectoryString) Set(value string) error
func (*DirectoryString) String ¶
func (self *DirectoryString) String() string
type DupOpError ¶
type DupOpError struct {
MyError
}
type HexOrDecimal64 ¶
type HexOrDecimal64 uint64
HexOrDecimal64 marshals uint64 as hex or decimal.
func (HexOrDecimal64) MarshalText ¶
func (i HexOrDecimal64) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler.
func (*HexOrDecimal64) UnmarshalText ¶
func (i *HexOrDecimal64) UnmarshalText(input []byte) error
UnmarshalText implements encoding.TextUnmarshaler.
type Merkle ¶
type Merkle struct { Root *MerkleNode Leaves MerkleLayer Height int }
type MerkleLayer ¶
type MerkleLayer []*MerkleNode
type MerkleNode ¶
type MerkleNode struct { Parent *MerkleNode LeftChild *MerkleNode RightChild *MerkleNode Neighbour *MerkleNode Hash []byte Subscript int }
type OfflineError ¶
type OfflineError struct {
MyError
}
type PrettyDuration ¶
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 TextMarshaler ¶
type TextMarshaler interface { encoding.TextMarshaler encoding.TextUnmarshaler }
func GlobalTextMarshaler ¶
func GlobalTextMarshaler(ctx *cli.Context, name string) TextMarshaler
GlobalTextMarshaler returns the value of a TextMarshalerFlag from the global flag set.
type TextMarshalerFlag ¶
type TextMarshalerFlag struct { Name string Value TextMarshaler Usage string }
TextMarshalerFlag wraps a TextMarshaler value.
func (TextMarshalerFlag) Apply ¶
func (f TextMarshalerFlag) Apply(set *flag.FlagSet)
func (TextMarshalerFlag) GetName ¶
func (f TextMarshalerFlag) GetName() string
func (TextMarshalerFlag) String ¶
func (f TextMarshalerFlag) String() string
type TimeoutError ¶
type TimeoutError struct {
MyError
}
type TransmissionError ¶
type TransmissionError struct {
MyError
}
type Uint ¶
type Uint uint
Uint marshals/unmarshals as a JSON string with 0x prefix. The zero value marshals as "0x0".
func (Uint) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Uint) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Uint) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Uint64 ¶
type Uint64 uint64
Uint64 marshals/unmarshals as a JSON string with 0x prefix. The zero value marshals as "0x0".
func (Uint64) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Uint64) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Uint64) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package bitutil implements fast bitwise operations.
|
Package bitutil implements fast bitwise operations. |
Package bloombits implements bloom filtering on batches of data.
|
Package bloombits implements bloom filtering on batches of data. |
Package event deals with subscriptions to real-time events.
|
Package event deals with subscriptions to real-time events. |
Package hexutil implements hex encoding with 0x prefix.
|
Package hexutil implements hex encoding with 0x prefix. |
Package math provides integer math utilities.
|
Package math provides integer math utilities. |
Package mclock is a wrapper for a monotonic clock source
|
Package mclock is a wrapper for a monotonic clock source |
Package trie implements Merkle Patricia Tries.
|
Package trie implements Merkle Patricia Tries. |