common

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2019 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package math provides integer math utilities.

Index

Constants

View Source
const (
	// HashLength is the expected length of the hash
	HashLength = 32
	// AddressLength is the expected length of the address
	AddressLength = 20
)

Variables

View Source
var (
	MaxBig256 = new(big.Int).Set(tt256m1)
	MaxBig63  = new(big.Int).Sub(tt63, big.NewInt(1))
)

Various big integer limit values.

Functions

func BigMax

func BigMax(x, y *big.Int) *big.Int

BigMax returns the larger of x or y.

func BigMin

func BigMin(x, y *big.Int) *big.Int

BigMin returns the smaller of x or y.

func BigPow

func BigPow(a, b int64) *big.Int

BigPow returns a ** b as a big integer.

func Byte

func Byte(bigint *big.Int, padlength, n int) byte

Byte returns the byte at position n, with the supplied padlength in Little-Endian encoding. n==0 returns the MSB Example: bigint '5', padlength 32, n=31 => 5

func Exp

func Exp(base, exponent *big.Int) *big.Int

Exp implements exponentiation by squaring. Exp returns a newly-allocated big integer and does not change base or exponent. The result is truncated to 256 bits.

Courtesy @karalabe and @chfast

func FirstBitSet

func FirstBitSet(v *big.Int) int

FirstBitSet returns the index of the first 1 bit in v, counting from LSB.

func MustParseBig256

func MustParseBig256(s string) *big.Int

MustParseBig256 parses s as a 256 bit big integer and panics if the string is invalid.

func PaddedBigBytes

func PaddedBigBytes(bigint *big.Int, n int) []byte

PaddedBigBytes encodes a big integer as a big-endian byte slice. The length of the slice is at least n bytes.

func ParseBig256

func ParseBig256(s string) (*big.Int, bool)

ParseBig256 parses s as a 256 bit integer in decimal or hexadecimal syntax. Leading zeros are accepted. The empty string parses as zero.

func ReadBits

func ReadBits(bigint *big.Int, buf []byte)

ReadBits encodes the absolute value of bigint as big-endian bytes. Callers must ensure that buf has enough space. If buf is too short the result will be incomplete.

func S256

func S256(x *big.Int) *big.Int

S256 interprets x as a two's complement number. x must not exceed 256 bits (the result is undefined if it does) and is not modified.

S256(0)        = 0
S256(1)        = 1
S256(2**255)   = -2**255
S256(2**256-1) = -1

func TransNodeIDToString added in v0.1.1

func TransNodeIDToString(nodeID NodeID) string

func TransPubKeyToStringID added in v0.1.1

func TransPubKeyToStringID(pubKey crypto.PubKey) string

func U256

func U256(x *big.Int) *big.Int

U256 encodes as a 256 bit two's complement number. This operation is destructive.

Types

type Config

type Config struct {
	// These settings are required and configure the UDP listener:
	PrivateKey crypto.PrivKey

	// These settings are optional:
	NetRestrict *netutil.Netlist // network whitelist
	SeedNodes   []*Node          // list of bootstrap nodes
}

Config holds Table-related settings.

type DiscoverTable

type DiscoverTable interface {
	Start()
	Stop()
	GetMaxDialOutNum() int                        //Maximum number of connections to be actively connected outward
	GetMaxConNumFromCache() int                   //Get the maximum number of nodes from cache
	LookupRandom() []*Node                        //Get some nodes in real time from the network
	ReadRandomNodes([]*Node, map[string]bool) int //Get some random nodes from local memory
	IsDhtTable() bool
}

type Hash

type Hash [HashLength]byte

type HexOrDecimal256

type HexOrDecimal256 big.Int

HexOrDecimal256 marshals big.Int as hex or decimal.

func NewHexOrDecimal256

func NewHexOrDecimal256(x int64) *HexOrDecimal256

NewHexOrDecimal256 creates a new HexOrDecimal256

func (*HexOrDecimal256) MarshalText

func (i *HexOrDecimal256) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*HexOrDecimal256) UnmarshalText

func (i *HexOrDecimal256) UnmarshalText(input []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Node

type Node struct {
	IP       net.IP `json:"ip"`       // len 4 for IPv4 or 16 for IPv6
	UDP_Port uint16 `json:"udp_port"` // port numbers
	TCP_Port uint16 `json:"tcp_port"` // port numbers
	ID       NodeID `json:"id"`       // the node's public key
}

func (*Node) Incomplete

func (n *Node) Incomplete() bool

Incomplete returns true for nodes with no IP address.

func (*Node) ValidateComplete

func (n *Node) ValidateComplete() error

ValidateComplete checks whether n has a valid IP and UDP port.

type NodeID

type NodeID [32]byte //crypto.Keccak256Hash(pubkey)

ID is a unique identifier for each node.

func TransPkbyteToNodeID added in v0.1.1

func TransPkbyteToNodeID(pubKey []byte) NodeID

func TransPubKeyToNodeID added in v0.1.1

func TransPubKeyToNodeID(pubKey crypto.PubKey) NodeID

func (NodeID) Bytes

func (n NodeID) Bytes() []byte

func (*NodeID) Copy added in v0.1.1

func (n *NodeID) Copy(buffer []byte)

func (NodeID) MarshalJSON added in v0.1.1

func (n NodeID) MarshalJSON() ([]byte, error)

func (NodeID) String

func (n NodeID) String() string

ID prints as a long hexadecimal number.

func (*NodeID) UnmarshalJSON added in v0.1.1

func (n *NodeID) UnmarshalJSON(data []byte) error

type P2pDBManager

type P2pDBManager interface {
	QuerySeeds(n int, maxAge time.Duration) []*Node
	LastPingReceived(id NodeID, ip net.IP) time.Time
	LastPongReceived(id NodeID, ip net.IP) time.Time
	UpdateNode(node *Node) //Store Node in DB
	UpdateLastPingReceived(id NodeID, ip net.IP, instance time.Time)
	UpdateLastPongReceived(id NodeID, ip net.IP, instance time.Time)
	UpdateFindFails(id NodeID, ip net.IP, fails int)
	FindFails(id NodeID, ip net.IP) int
	Close()
}

type ReadPacket

type ReadPacket struct {
	Data []byte
	Addr *net.UDPAddr
}

ReadPacket is a packet that couldn't be handled. Those packets are sent to the unhandled channel if configured.

type UDPConn

type UDPConn interface {
	ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error)
	WriteToUDP(b []byte, addr *net.UDPAddr) (n int, err error)
	Close() error
	LocalAddr() net.Addr
}

Jump to

Keyboard shortcuts

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