utils

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2021 License: Apache-2.0 Imports: 50 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxConnections      = 1000
	DefTaskPoolCount    = 200
	TaskSize            = 1024
	ReadTimeOut         = 180             // in seconds
	WriteTimeOut        = 180             // in seconds
	ClientSendHeartTime = 60              // in seconds
	MsgHeaderLen        = 16              // in bytes
	MessageBeatLen      = 4 * 1024 * 1024 // in bytes
)
View Source
const (
	DistrictIdBits = uint(5) // district id bits size
	NodeIdBits     = uint(9) // node id bits size

	DistrictIdShift = sequenceBits + NodeIdBits
)

Variables

View Source
var (
	ErrNotFoundCallBack = errors.New("not found callBack function")
	ErrServerClosed     = errors.New("server has been closed")
	ErrWouldBlock       = errors.New("would block")
)

Functions

func ByteToString

func ByteToString(p []byte) string

ByteToString

func BytesToInt16

func BytesToInt16(b []byte) int16

BytesToInt16

func BytesToUInt32

func BytesToUInt32(b []byte) uint32

BytesToUInt32

func BytesToUint16

func BytesToUint16(b []byte) uint16

BytesToUint16

func CalcCRC32

func CalcCRC32(data []byte) uint32

CalcCRC32

func CalcFileCRC32

func CalcFileCRC32(filePath string) uint32

CalcFileCRC32

func CalcFileHash

func CalcFileHash(filePath string) string

CalcFileHash @notice keccak256(md5(file))

func CalcFileMD5

func CalcFileMD5(filePath string) []byte

CalcFileMD5

func CalcHash

func CalcHash(data []byte) string

CalcHash

func CalcMD5

func CalcMD5(data []byte) []byte

CalcMD5

func CalcSliceHash

func CalcSliceHash(data []byte, fileHash string) string

CalcHash

func Camel2Snake

func Camel2Snake(s string) string

Camel2Snake eg. HelloWorld => hello_world

func ChangePassword

func ChangePassword(walletAddress, dir, auth string, scryptN, scryptP int, key *AccountKey) error

ChangePassword

func CheckError

func CheckError(err error) bool

CheckError TODO This is a bad way to call error log, as you cannot know where this method is called in your error log This give log line like this : [ERROR]2021/04/13 22:39:11 log.go:150: Fatal error: address 127.0.0.1: missing port in address it always refer to this file and this line If time allows, a better logging tool like "zerolog" can be used to replace these methods.

func CheckStructField

func CheckStructField(field string, structName interface{}) bool

CheckStructField

func ConvertCoronaryUtf8

func ConvertCoronaryUtf8(in string) string

ConvertCoronaryUtf8

func Crc32IEEE

func Crc32IEEE(data []byte) uint32

func CreateP2PKey

func CreateP2PKey(dir, nickname, password, hrp string, scryptN, scryptP int) (types.Address, error)

CreateP2PKey creates a P2P key to be used by one of the SDS nodes, and saves the key data into the dir folder

func CreateWallet

func CreateWallet(dir, nickname, password, hrp, mnemonic, bip39Passphrase, hdPath string, scryptN, scryptP int) (types.Address, error)

CreateWallet creates a new stratos-chain wallet with the given nickname and password, and saves the key data into the dir folder

func DebugLog

func DebugLog(v ...interface{})

DebugLog calls default logger and output debug log

func DebugLogf

func DebugLogf(template string, v ...interface{})

DebugLog calls default logger and output debug log

func ECCSign

func ECCSign(text []byte, prk *ecdsa.PrivateKey) ([]byte, error)

ECCSign signs the given text

func ECCSignBytes

func ECCSignBytes(text, privateKey []byte) ([]byte, error)

ECCSignBytes converts the private key bytes to an ecdsa.PrivateKey and then signs the given text

func ECCVerify

func ECCVerify(text []byte, signature []byte, key *ecdsa.PublicKey) bool

ECCVerify verifies the given signature

func ECCVerifyBytes

func ECCVerifyBytes(text, signature, publicKey []byte) bool

ECCVerifyBytes converts the public key bytes to an ecdsa.PublicKey and then verifies the given signature

func EncryptKey

func EncryptKey(key *AccountKey, auth string, scryptN, scryptP int) ([]byte, error)

EncryptKey encrypts a key using the specified scrypt parameters into a json blob that can be decrypted later on.

func ErrorLog

func ErrorLog(v ...interface{})

ErrorLog call default logger and output error log

func ErrorLogf

func ErrorLogf(template string, v ...interface{})

ErrorLogf call default logger and output error log

func GenerateRandomNumber

func GenerateRandomNumber(start int, end int, count int) []int

GenerateRandomNumber generate (count) random numbers between (start) to (end).

func Get16MD5

func Get16MD5(data string) string

func Get8BitUUID

func Get8BitUUID() string

Get8BitUUID

func GetElement

func GetElement(key string, themap map[interface{}]interface{}) string

GetElement

func GetMD5

func GetMD5(data string) string

func GetRandomString

func GetRandomString(length int) string

GetRandomString between [0-9a-zA-Z]

func GetYamlConfig

func GetYamlConfig(path string) map[interface{}]interface{}

GetYamlConfig

func ImageCommpress

func ImageCommpress(pathStr string) (string, error)

ImageCommpress ImageCommpress

func Int16ToBytes

func Int16ToBytes(n int16) []byte

Int16ToBytes

func Int64ToByte

func Int64ToByte(n int64) []byte

Int64ToByte

func IntToString

func IntToString(i int) string

IntToString

func LcFirst

func LcFirst(str string) string

LcFirst first letter lower case

func LoadYamlConfig

func LoadYamlConfig(s interface{}, path string) error

LoadYamlConfig

func Log

func Log(v ...interface{})

Log calls default logger and output info log

func MergeByte

func MergeByte(b ...[]byte) []byte

MergeByte

func MergeBytes

func MergeBytes(a, b []byte) []byte

func NewMnemonic

func NewMnemonic() (string, error)

func Snake2Camel

func Snake2Camel(s string) string

Snake2Camel eg. hello_world => HelloWorld

func StrInSlices

func StrInSlices(slices []string, key string) bool

StrInSlices

func StringToInt

func StringToInt(s string) (int, error)

StringToInt

func Struct2Map

func Struct2Map(obj interface{}) map[string]interface{}

Struct2Map

func UcFirst

func UcFirst(str string) string

UcFirst first letter upper case

func Uint16ToBytes

func Uint16ToBytes(n uint16) []byte

Uint16ToBytes

func Uint32ToBytes

func Uint32ToBytes(n uint32) []byte

Uint32ToBytes

func WriteConfig

func WriteConfig(data interface{}, filePath string) error

func WriteKeyFile

func WriteKeyFile(file string, content []byte) error

Types

type AccountKey

type AccountKey struct {
	Id uuid.UUID // Version 4 "random" for unique id not derived from key data
	// to simplify lookups we also store the address
	Address types.Address
	// The HD path to use to derive this key
	HdPath string
	// The mnemonic for the underlying HD wallet
	Mnemonic string
	// a nickname
	Name string
	// The bip39 passphrase for the underlying HD wallet
	Passphrase string
	// we only store privkey as pubkey/address can be derived from it
	// privkey in this struct is always in plaintext
	PrivateKey []byte
}

func DecryptKey

func DecryptKey(keyjson []byte, auth string) (*AccountKey, error)

DecryptKey decrypts a key from a json blob, returning the private key itself.

type AtomicInt32

type AtomicInt32 int32

AtomicInt32

func CreateAtomicInt32

func CreateAtomicInt32(initialValue int32) *AtomicInt32

CreateAtomicInt32

func (*AtomicInt32) AddAndGetNew

func (a *AtomicInt32) AddAndGetNew(delta int32) int32

AddAndGetNew

func (*AtomicInt32) CompareAndSet

func (a *AtomicInt32) CompareAndSet(expect, update int32) bool

CompareAndSet

func (*AtomicInt32) DecrementAndGetNew

func (a *AtomicInt32) DecrementAndGetNew() int32

DecrementAndGetNew

func (*AtomicInt32) GetAndSetAtomic

func (a *AtomicInt32) GetAndSetAtomic(newValue int32) int32

GetAndSetAtomic

func (*AtomicInt32) GetAtomic

func (a *AtomicInt32) GetAtomic() int32

GetAtomic

func (*AtomicInt32) GetOldAndAdd

func (a *AtomicInt32) GetOldAndAdd(delta int32) int32

GetOldAndAdd

func (*AtomicInt32) GetOldAndDecrement

func (a *AtomicInt32) GetOldAndDecrement() int32

GetOldAndDecrement

func (*AtomicInt32) GetOldAndIncrement

func (a *AtomicInt32) GetOldAndIncrement() int32

GetOldAndIncrement

func (*AtomicInt32) IncrementAndGetNew

func (a *AtomicInt32) IncrementAndGetNew() int32

IncrementAndGetNew

func (*AtomicInt32) SetAtomic

func (a *AtomicInt32) SetAtomic(newValue int32)

SetAtomic

func (*AtomicInt32) String

func (a *AtomicInt32) String() string

type AtomicInt64

type AtomicInt64 int64

AtomicInt64

func CreateAtomicInt64

func CreateAtomicInt64(initialValue int64) *AtomicInt64

CreateAtomicInt64 with initial value

func (*AtomicInt64) AddAndGetNew

func (a *AtomicInt64) AddAndGetNew(delta int64) int64

AddAndGetNew

func (*AtomicInt64) CompareAndSet

func (a *AtomicInt64) CompareAndSet(expect, update int64) bool

CompareAndSet

func (*AtomicInt64) DecrementAndGetNew

func (a *AtomicInt64) DecrementAndGetNew() int64

DecrementAndGetNew

func (*AtomicInt64) GetAndSetAtomic

func (a *AtomicInt64) GetAndSetAtomic(newValue int64) int64

GetAndSetAtomic return current and set new

func (*AtomicInt64) GetAtomic

func (a *AtomicInt64) GetAtomic() int64

GetAtomic

func (*AtomicInt64) GetNewAndSetAtomic

func (a *AtomicInt64) GetNewAndSetAtomic(newValue int64) int64

GetNewAndSetAtomic set new and return new

func (*AtomicInt64) GetOldAndAdd

func (a *AtomicInt64) GetOldAndAdd(delta int64) int64

GetOldAndAdd return current and add delta to atomic

func (*AtomicInt64) GetOldAndDecrement

func (a *AtomicInt64) GetOldAndDecrement() int64

GetOldAndDecrement return current and minus 1 to atomic

func (*AtomicInt64) GetOldAndIncrement

func (a *AtomicInt64) GetOldAndIncrement() int64

GetOldAndIncrement return current and add 1 to atomic

func (*AtomicInt64) IncrementAndGetNew

func (a *AtomicInt64) IncrementAndGetNew() int64

IncrementAndGetNew

func (*AtomicInt64) SetAtomic

func (a *AtomicInt64) SetAtomic(newValue int64)

SetAtomic

func (*AtomicInt64) String

func (a *AtomicInt64) String() string

type AutoCleanMap

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

func NewAutoCleanMap

func NewAutoCleanMap(delay time.Duration) *AutoCleanMap

func (*AutoCleanMap) Delete

func (m *AutoCleanMap) Delete(key interface{})

func (*AutoCleanMap) Load

func (m *AutoCleanMap) Load(key interface{}) (interface{}, bool)

func (*AutoCleanMap) Store

func (m *AutoCleanMap) Store(key, value interface{})

type ErrUndefined

type ErrUndefined int32

ErrUndefined

func (ErrUndefined) Error

func (e ErrUndefined) Error() string

type IdWorker

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

func NewIdWorker

func NewIdWorker(NodeId int64) (*IdWorker, error)

NewIdWorker new a snowflake id generator object.

func (*IdWorker) NextId

func (id *IdWorker) NextId() (int64, error)

NextId get a snowflake id.

func (*IdWorker) NextIds

func (id *IdWorker) NextIds(num int) ([]int64, error)

NextIds get snowflake ids.

type KeyStorePassphrase

type KeyStorePassphrase struct {
	KeysDirPath string
	ScryptN     int
	ScryptP     int
}

func (KeyStorePassphrase) StoreKey

func (ks KeyStorePassphrase) StoreKey(filename string, key *AccountKey, auth string) error

type LimitRate

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

LimitRate

func (*LimitRate) GetRate

func (l *LimitRate) GetRate() uint64

GetRate

func (*LimitRate) Limit

func (l *LimitRate) Limit() bool

func (*LimitRate) SetRate

func (l *LimitRate) SetRate(r uint64)

SetRate

type LogLevel

type LogLevel int
const (
	Detail LogLevel = 1
	Debug  LogLevel = 10
	Info   LogLevel = 20
	Warn   LogLevel = 30
	Error  LogLevel = 40
	Fatal  LogLevel = 50
)

log level

type Logger

type Logger struct {
	// contains filtered or unexported fields
}
var MyLogger *Logger

func NewDefaultLogger

func NewDefaultLogger(filepath string, enableStd, enableFile bool) *Logger

func NewLogger

func NewLogger(filepath string, enableStd, enableFile bool) *Logger

func (*Logger) ErrorLog

func (l *Logger) ErrorLog(v ...interface{})

func (*Logger) Log

func (l *Logger) Log(level LogLevel, v ...interface{})

func (*Logger) SetEnablefile

func (l *Logger) SetEnablefile(b bool)

func (*Logger) SetEnablestd

func (l *Logger) SetEnablestd(b bool)

func (*Logger) SetLogLevel

func (l *Logger) SetLogLevel(lv LogLevel)

SetLogLevel

type MyValue

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

type SysInfo

type SysInfo struct {
	DiskSize   uint64
	MemorySize uint64
	OSInfo     string
	CPUInfo    string
	MacAddress string
}

SysInfo

func GetSysInfo

func GetSysInfo() *SysInfo

GetSysInfo

Directories

Path Synopsis
math
Package math provides integer math utilities.
Package math provides integer math utilities.
rlp
Package rlp implements the RLP serialization format.
Package rlp implements the RLP serialization format.
secp256k1
Package secp256k1 wraps the bitcoin secp256k1 C library.
Package secp256k1 wraps the bitcoin secp256k1 C library.
sha3
Package sha3 implements the SHA-3 fixed-output-length hash functions and the SHAKE variable-output-length hash functions defined by FIPS-202.
Package sha3 implements the SHA-3 fixed-output-length hash functions and the SHAKE variable-output-length hash functions defined by FIPS-202.
sql

Jump to

Keyboard shortcuts

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