common

package module
v0.0.0-...-5608e42 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2025 License: ISC Imports: 10 Imported by: 17

Documentation

Index

Constants

View Source
const (
	// MaxVarIntPayload is the maximum payload size for a variable length integer.
	MaxVarIntPayload = 9

	// MinTxOutPayload is the minimum payload size for a transaction output.
	// Value 8 bytes + Varint for PkScript length 1 byte.
	MinTxOutPayload = 9

	// binaryFreeListMaxItems is the number of buffers to keep in the free
	// list to use for binary serialization and deserialization.
	BinaryFreeListMaxItems = 1024
)
View Source
const (
	// InvWitnessFlag denotes that the inventory vector type is requesting,
	// or sending a version which includes witness data.
	InvWitnessFlag = 1 << 30
	InvTempFlag    = 1 << 29
)

These constants define the various supported inventory vector types.

View Source
const CommandSize = 12
View Source
const MaxMessagePayload = (1024 * 1024 * 32) // 32MB

MaxMessagePayload is the maximum bytes a message can be regardless of other individual limits imposed by messages themselves.

View Source
const (
	MaxUint64 = 1<<64 - 1
)

Variables

View Source
var (
	// littleEndian is a convenience variable since binary.LittleEndian is
	// quite long.
	LittleEndian = binary.LittleEndian

	// bigEndian is a convenience variable since binary.BigEndian is quite
	// long.
	BigEndian = binary.BigEndian
)
View Source
var BinarySerializer binaryFreeList = make(chan []byte, BinaryFreeListMaxItems)

binarySerializer provides a free list of buffers to use for serializing and deserializing primitive integer values to and from io.Readers and io.Writers.

Functions

func HashToBig

func HashToBig(hash *chainhash.Hash) *big.Int

HashToBig converts a chainhash.Hash into a big.Int that can be used to perform math comparisons.

func RandomUint64

func RandomUint64() (uint64, error)

RandomUint64 returns a cryptographically random uint64 value.

func ReadElement

func ReadElement(r io.Reader, element interface{}) error

ReadElement reads the next sequence of bytes from r using little endian depending on the concrete type of element pointed to.

func ReadElements

func ReadElements(r io.Reader, elements ...interface{}) error

ReadElements reads multiple items from r. It is equivalent to multiple calls to readElement.

func ReadVarBytes

func ReadVarBytes(r io.Reader, pver uint32, maxAllowed uint32,
	fieldName string) ([]byte, error)

ReadVarBytes reads a variable length byte array. A byte array is encoded as a varInt containing the length of the array followed by the bytes themselves. An error is returned if the length is greater than the passed maxAllowed parameter which helps protect against memory exhaustion attacks and forced panics through malformed messages. The fieldName parameter is only used for the error message so it provides more context in the error.

func ReadVarInt

func ReadVarInt(r io.Reader, pver uint32) (uint64, error)

ReadVarInt reads a variable length integer from r and returns it as a uint64.

func ReadVarString

func ReadVarString(r io.Reader, pver uint32) (string, error)

ReadVarString reads a variable length string from r and returns it as a Go string. A variable length string is encoded as a variable length integer containing the length of the string followed by the bytes that represent the string itself. An error is returned if the length is greater than the maximum block payload size since it helps protect against memory exhaustion attacks and forced panics through malformed messages.

func SafeMul

func SafeMul(x, y uint64) (uint64, bool)

SafeMul returns multiplication result and whether overflow occurred.

func VarIntSerializeSize

func VarIntSerializeSize(val uint64) int

VarIntSerializeSize returns the number of bytes it would take to serialize val as a variable length integer.

func WriteElement

func WriteElement(w io.Writer, element interface{}) error

writeElement writes the little endian representation of element to w.

func WriteElements

func WriteElements(w io.Writer, elements ...interface{}) error

writeElements writes multiple items to w. It is equivalent to multiple calls to writeElement.

func WriteVarBytes

func WriteVarBytes(w io.Writer, pver uint32, bytes []byte) error

WriteVarBytes serializes a variable length byte array to w as a varInt containing the number of bytes, followed by the bytes themselves.

func WriteVarInt

func WriteVarInt(w io.Writer, pver uint32, val uint64) error

WriteVarInt serializes val to w using a variable number of bytes depending on its value.

func WriteVarString

func WriteVarString(w io.Writer, pver uint32, str string) error

WriteVarString serializes str to w as a variable length integer containing the length of the string followed by the bytes that represent the string itself.

Types

type BloomUpdateType

type BloomUpdateType uint8

BloomUpdateType specifies how the filter is updated when a match is found

type Int64Time

type Int64Time time.Time

int64Time represents a unix timestamp encoded with an int64. It is used as a way to signal the readElement function how to decode a timestamp into a Go time.Time since it is otherwise ambiguous.

type InvType

type InvType uint32

InvType represents the allowed types of inventory vectors. See InvVect.

const (
	InvTypeError         InvType = 0
	InvTypeTx            InvType = 1
	InvTypeBlock         InvType = 2
	InvTypeFilteredBlock InvType = 3
	InvTypeTempBlock     InvType = InvTypeBlock | InvTempFlag
	InvTypeMinerBlock    InvType = 5
	InvTypeWitnessBlock  InvType = InvTypeBlock | InvWitnessFlag
	InvTypeMask          InvType = 7
	InvTypeWitnessTx     InvType = InvTypeTx | InvWitnessFlag
)

These constants define the various supported inventory vector types.

func (InvType) String

func (invtype InvType) String() string

String returns the InvType in human-readable form.

type MessageError

type MessageError struct {
	Func        string // Function name
	Description string // Human readable description of the issue
}

MessageError describes an issue with a message. An example of some potential issues are messages from the wrong bitcoin network, invalid commands, mismatched checksums, and exceeding max payloads.

This provides a mechanism for the caller to type assert the error to differentiate between general io errors such as io.EOF and issues that resulted from malformed messages.

func NewMessageError

func NewMessageError(f string, desc string) *MessageError

messageError creates an error for the given function and description.

func (*MessageError) Error

func (e *MessageError) Error() string

Error satisfies the error interface and prints human-readable errors.

type OmegaNet

type OmegaNet uint32

OmegaNet represents which bitcoin network a message belongs to.

const (
	// MainNet represents the main bitcoin network.
	MainNet OmegaNet = 0x956ca366

	// RegNet represents the regression test network.
	RegNet OmegaNet = 0x6241456c

	// TestNet represents the test network.
	TestNet OmegaNet = 0x709c5fed

	// SimNet represents the simulation test network.
	SimNet OmegaNet = 0xe10b70ad
)

Constants used to indicate the message network. They can also be used to seek to the next message when a stream's state is unknown, but this package does not provide that functionality since it's generally a better idea to simply disconnect clients that are misbehaving over TCP.

func (OmegaNet) String

func (n OmegaNet) String() string

String returns the OmegaNet in human-readable form.

type RejectCode

type RejectCode uint8

RejectCode represents a numeric value by which a remote peer indicates why a message was rejected.

const (
	RejectMalformed   RejectCode = 0x01
	RejectInvalid     RejectCode = 0x10
	RejectObsolete    RejectCode = 0x11
	RejectDuplicate   RejectCode = 0x12
	RejectNonstandard RejectCode = 0x40
	//	RejectDust            RejectCode = 0x41
	RejectInsufficientFee RejectCode = 0x42
	RejectCheckpoint      RejectCode = 0x43
)

These constants define the various supported reject codes.

func (RejectCode) String

func (code RejectCode) String() string

String returns the RejectCode in human-readable form.

type ServiceFlag

type ServiceFlag uint64

ServiceFlag identifies services supported by a bitcoin peer.

const (
	// SFNodeNetwork is a flag used to indicate a peer is a full node.
	SFNodeNetwork ServiceFlag = 1 << iota

	// SFNodeGetUTXO is a flag used to indicate a peer supports the
	// getutxos and utxos commands (BIP0064).
	SFNodeGetUTXO

	// SFNodeBloom is a flag used to indicate a peer supports bloom
	// filtering.
	SFNodeBloom

	// SFNodeXthin is a flag used to indicate a peer supports xthin blocks.
	SFNodeXthin

	// SFNodeBit5 is a flag used to indicate a peer supports a service
	// defined by bit 5.
	SFNodeBit5

	// SFNodeCF is a flag used to indicate a peer supports committed
	// filters (CFs).
	SFNodeCF

	// SFNode2X is a flag used to indicate a peer is running the Segwit2X
	// software.
	SFNode2X
)

func (ServiceFlag) String

func (f ServiceFlag) String() string

String returns the ServiceFlag in human-readable form.

type Uint32Time

type Uint32Time time.Time

uint32Time represents a unix timestamp encoded with a uint32. It is used as a way to signal the readElement function how to decode a timestamp into a Go time.Time since it is otherwise ambiguous.

Jump to

Keyboard shortcuts

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