common

package
v0.0.0-...-f58d8a1 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2021 License: LGPL-3.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// AddressLen length in bytes
	AddressLen = 20

	// AddressTypeExternal is the address type for external account.
	AddressTypeExternal = AddressType(1)

	// AddressTypeContract is the address type for contract account.
	AddressTypeContract = AddressType(2)

	// AddressTypeReserved is the reserved address type for system contract.
	// Note, the address type (4 bits) value ranges [0,15], so the system reserved
	// address type value should greater than 15.
	AddressTypeReserved = AddressType(16)
)
View Source
const (

	// ScdoProtoName protoName of Scdo service
	ScdoProtoName = "scdo"

	// ScdoVersion Version number of Scdo protocol
	ScdoVersion uint = 1

	// ScdoNodeVersion for simpler display
	ScdoNodeVersion string = "Scdo_V1.0.0"

	// ShardCount represents the total number of shards.
	ShardCount = 4

	// ShardByte represents the number of bytes used for shard information, must be smaller than 8
	ShardByte = 1

	// MetricsRefreshTime is the time of metrics sleep 1 minute
	MetricsRefreshTime = time.Minute

	// CPUMetricsRefreshTime is the time of metrics monitor cpu
	CPUMetricsRefreshTime = time.Second

	// ConfirmedBlockNumber is the block number for confirmed a block, it should be more than 12 in product
	ConfirmedBlockNumber = 120

	ScdoForkHeight = 2979594

	// emery hard fork: update zpow consensus and evm
	EmeryForkHeight = ScdoForkHeight

	// ForkHeight after this height we change the content of block: hardFork
	ForkHeight = ScdoForkHeight

	// ForkHeight after this height we change the content of block: hardFork
	SecondForkHeight = ScdoForkHeight

	// ForkHeight after this height we change the validation of tx: hardFork
	ThirdForkHeight = ScdoForkHeight

	SmartContractNonceForkHeight = ScdoForkHeight

	// SmartContractNonceFixHeight fix smart contract nonce bug when user use setNonce
	SmartContractNonceFixHeight = ScdoForkHeight

	// LightChainDir lightchain data directory based on config.DataRoot
	LightChainDir = "/db/lightchain"

	// Sha256Algorithm miner algorithm sha256
	Sha256Algorithm = "sha256"

	// zpow miner algorithm
	ZpowAlgorithm = "zpow"

	// BFT mineralgorithm
	BFTEngine = "bft"

	// BFT data folder
	BFTDataFolder = "bftdata"

	// EVMStackLimit increase evm stack limit to 8192
	EVMStackLimit = 8192

	// BlockPackInterval it's an estimate time.
	BlockPackInterval = 15 * time.Second

	// Height: fix the issue caused by forking from collapse database
	HeightFloor = uint64(707989)
	HeightRoof  = uint64(707996)

	WindowsPipeDir = `\\.\pipe\`
)
View Source
const (
	// HashLength the length of hash
	HashLength = 32
)
View Source
const UndefinedShardNumber = uint(0)

UndefinedShardNumber is the default value if shard number not specified.

Variables

View Source
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)
)

Common big integers often used

View Source
var EmptyAddress = Address{}

EmptyAddress presents an empty address

View Source
var EmptyHash = Hash{}

EmptyHash empty hash

View Source
var ErrOnly0xPrefix = errors.New("the string is invalid length only 0x or 0X prefix")

ErrOnly0xPrefix the string is invalid length only 0x or 0X prefix

View Source
var LocalShardNumber uint

LocalShardNumber defines the shard number of coinbase. Generally, it must be initialized during program startup.

View Source
var MaxSystemContractAddress = BytesToAddress([]byte{4, 255})

MaxSystemContractAddress max system contract address

View Source
var (
	//ScdoToWen base wen coin number
	ScdoToWen = big.NewInt(100000000)
)

Functions

func BigToDecimal

func BigToDecimal(amount *big.Int) string

BigToDecimal simply changes big int to decimal which will miss additional 0 in the last

func ConvertInt64ToBytes

func ConvertInt64ToBytes(num int64) []byte

ConvertInt64ToBytes converts a int64 value into a byte array

func CopyBytes

func CopyBytes(src []byte) []byte

CopyBytes copies and returns a new bytes from the specified source bytes.

func Deserialize

func Deserialize(data []byte, value interface{}) error

Deserialize wrapper decode

func FileOrFolderExists

func FileOrFolderExists(fileOrFolder string) bool

FileOrFolderExists checks if a file or folder exists

func GetDefaultDataFolder

func GetDefaultDataFolder() string

GetDefaultDataFolder gets the default data Folder

func GetDefaultIPCPath

func GetDefaultIPCPath() string

GetDefaultIPCPath gets the default IPC path

func GetPassword

func GetPassword() (string, error)

GetPassword ask user for password interactively

func GetTempFolder

func GetTempFolder() string

GetTempFolder gets the temp folder

func IsShardEnabled

func IsShardEnabled() bool

IsShardEnabled returns true if the LocalShardNumber is set. Otherwise, false.

func LeftPadBytes

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

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

func MustNewCache

func MustNewCache(size int) *lru.Cache

MustNewCache creates a LRU cache with specified size. Panics on any error.

func RightPadBytes

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

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

func SaveFile

func SaveFile(filePath string, content []byte) error

SaveFile save file

func Serialize

func Serialize(in interface{}) ([]byte, error)

Serialize wrapper encode

func SerializePanic

func SerializePanic(in interface{}) []byte

SerializePanic serialize the input data to byte array. Panics on error, e.g. unsupported data type for RLP encoding.

func SetPassword

func SetPassword() (string, error)

SetPassword ask user input password twice and get the password interactively

func Shuffle

func Shuffle(slice interface{})

Shuffle shuffles items in slice

func ValidAccountHex

func ValidAccountHex(account string) bool

ValidAccountHex returns true if it is a valid account string

func ValidShard

func ValidShard(shard uint) bool

ValidShard returns true if it is a valid shard number

Types

type Address

type Address [AddressLen]byte

Address we use public key as node id

func BigToAddress

func BigToAddress(b *big.Int) Address

BigToAddress converts a big int to address.

func BytesToAddress

func BytesToAddress(bs []byte) Address

BytesToAddress converts the specified byte array to Address.

func HexMustToAddres

func HexMustToAddres(id string) Address

HexMustToAddres converts the specified HEX string to address. Panics on any error.

func HexToAddress

func HexToAddress(id string) (Address, error)

HexToAddress converts the specified HEX string to address.

func NewAddress

func NewAddress(b []byte) (Address, error)

NewAddress converts a byte slice to a Address

func PubKeyToAddress

func PubKeyToAddress(pubKey *ecdsa.PublicKey, shard uint, hashFunc func(interface{}) Hash) (Address, error)

PubKeyToAddress converts a ECC public key to an external address.

func PubKeyToAddressOld

func PubKeyToAddressOld(pubKey *ecdsa.PublicKey, hashFunc func(interface{}) Hash) Address

PubKeyToAddressOld converts a ECC public key to an external address.

func (Address) Big

func (id Address) Big() *big.Int

Big converts address to a big int.

func (*Address) Bytes

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

Bytes get the actual bytes

Note: if we want to use pointer type, need to change the code snippet in unit test:

BytesToAddress([]byte{1, 2}).Bytes()
->
addrBytes := BytesToAddress([]byte{1, 2})
(&addrBytes).Bytes()

refer link: https://stackoverflow.com/questions/10535743/address-of-a-temporary-in-go

func (*Address) CreateContractAddress

func (id *Address) CreateContractAddress(nonce uint64, hashFunc func(interface{}) Hash) Address

CreateContractAddress returns a contract address that in the same shard of this address.

func (*Address) CreateContractAddressWithHash

func (id *Address) CreateContractAddressWithHash(h Hash) Address

CreateContractAddressWithHash returns a contract address that in the same shard of this address.

func (*Address) Equal

func (id *Address) Equal(b Address) bool

Equal checks if this address is the same with the specified address b.

func (Address) Hex

func (id Address) Hex() string

Hex converts address to S account string.

func (*Address) IsEVMContract

func (id *Address) IsEVMContract() bool

IsEVMContract indicates whether the address is EVM contract address.

func (*Address) IsEmpty

func (id *Address) IsEmpty() bool

IsEmpty returns true if this address is empty. Otherwise, false.

func (*Address) IsReserved

func (id *Address) IsReserved() bool

IsReserved returns true if the address is reserved

func (Address) MarshalText

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

MarshalText marshals the address to byte array

func (*Address) Shard

func (id *Address) Shard() uint

Shard returns shard number of this address

func (*Address) ShardOld

func (id *Address) ShardOld() uint

ShardOld returns the shard number of this address.

func (Address) String

func (id Address) String() string

String implements the fmt.Stringer interface

func (*Address) Type

func (id *Address) Type() AddressType

Type returns the address type

func (*Address) UnmarshalText

func (id *Address) UnmarshalText(json []byte) error

UnmarshalText unmarshals address from HEX string.

func (*Address) Validate

func (id *Address) Validate() error

Validate check whether the address type is valid. Two sources of address: external string address, internal publickey conversion external Address length enforced in HexToAddress->NewAddress internal Address length enforced in LoadECDSAFromString->ToECDSA->toECDSA

type AddressType

type AddressType byte

AddressType represents the address type

type BaseHeapItem

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

BaseHeapItem is base struct of any heap item.

func (*BaseHeapItem) GetHeapIndex

func (item *BaseHeapItem) GetHeapIndex() int

GetHeapIndex returns the item index in heap.

type Bytes

type Bytes []byte

Bytes is a array byte that is converted to hex string display format when marshal

func (Bytes) MarshalText

func (b Bytes) MarshalText() ([]byte, error)

MarshalText implement the TextMarshaler interface

func (*Bytes) UnmarshalText

func (b *Bytes) UnmarshalText(hex []byte) error

UnmarshalText implement the TextUnmarshaler interface

type Hash

type Hash [HashLength]byte

Hash the hash value generated by sha-3

func BigToHash

func BigToHash(b *big.Int) Hash

BigToHash converts a big int to Hash.

func BytesToHash

func BytesToHash(b []byte) Hash

BytesToHash converts bytes to hash value

func HexToHash

func HexToHash(hex string) (Hash, error)

HexToHash return the hash form of the hex

func MustHexToHash

func MustHexToHash(hex string) Hash

MustHexToHash return the hash form of the hex; Panic if there is an error

func StringToHash

func StringToHash(s string) Hash

StringToHash converts a string to the hash

func (Hash) Big

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

Big converts this Hash to a big int.

func (Hash) Bytes

func (a Hash) Bytes() []byte

Bytes returns its actual bits

func (*Hash) Equal

func (a *Hash) Equal(b Hash) bool

Equal returns a boolean value indicating whether the hash a is equal to the input hash b.

func (Hash) Hex

func (a Hash) Hex() string

Hex returns the hex form of the hash

func (Hash) IsEmpty

func (a Hash) IsEmpty() bool

IsEmpty return true if this hash is empty. Otherwise, false.

func (Hash) MarshalText

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

MarshalText marshals the Hash to HEX string.

func (*Hash) SetBytes

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

SetBytes sets the hash to the value of b.

func (Hash) String

func (a Hash) String() string

String returns the string representation of the hash

func (*Hash) UnmarshalText

func (a *Hash) UnmarshalText(json []byte) error

UnmarshalText unmarshals Hash from HEX string.

type Heap

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

Heap is a common used heap for generic type purpose.

func NewHeap

func NewHeap(lessFunc func(HeapItem, HeapItem) bool) *Heap

NewHeap creates a new heap with specified Less func.

func (*Heap) Len

func (h *Heap) Len() int

Len implements the heap.Interface

func (*Heap) Less

func (h *Heap) Less(i, j int) bool

Less implements the heap.Interface

func (*Heap) Peek

func (h *Heap) Peek() interface{}

Peek returns the top value in heap if any. Otherwise, return nil.

func (*Heap) Pop

func (h *Heap) Pop() interface{}

Pop implements the heap.Interface

func (*Heap) Push

func (h *Heap) Push(x interface{})

Push implements the heap.Interface

func (*Heap) Swap

func (h *Heap) Swap(i, j int)

Swap implements the heap.Interface

type HeapItem

type HeapItem interface {
	GetHeapIndex() int // to support delete item from heap by index.
	// contains filtered or unexported methods
}

HeapItem is implemented by any type that support heap manipulations.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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