wire

package
v0.0.0-...-be1c10e Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2021 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BlockVersionV1 = 1
	BlockVersionV2 = 2

	BlockVersion = BlockVersionV2
)

BlockVersion is the current latest supported block version.

View Source
const (
	// TxVersion is the current latest supported transaction version.
	TxVersion = 1

	// MaxTxInSequenceNum is the maximum sequence number the sequence field
	// of a transaction input can be.
	MaxTxInSequenceNum uint64 = math.MaxUint64

	// MaxPrevOutIndex is the maximum index the index field of a previous
	// outpoint can be.
	MaxPrevOutIndex uint32 = math.MaxUint32

	// SequenceLockTimeDisabled is a flag that if set on a transaction
	// input's sequence number, the sequence number will not be interpreted
	// as a relative locktime.
	SequenceLockTimeDisabled uint64 = 1 << 63

	// SequenceLockTimeIsSeconds is a flag thabt if set on a transaction
	// input's sequence number, the relative locktime has units of 32
	// seconds(close to block interval).
	SequenceLockTimeIsSeconds uint64 = 1 << 38

	// SequenceLockTimeMask is a mask that extracts the relative locktime
	// when masked against the transaction input sequence number.
	SequenceLockTimeMask uint64 = 0x00000000ffffffff

	// SequenceLockTimeGranularity is the defined time based granularity
	// for seconds-based relative time locks. When converting from seconds
	// to a sequence number, the value is right shifted by this amount,
	// therefore the granularity of relative time locks in 32 or 2^5
	// seconds. Enforced relative lock times are multiples of 32 seconds.
	SequenceLockTimeGranularity = 5
)
View Source
const (
	ProposalVersion          = 1
	HeadersPerProposal       = 2
	HeaderSizePerPlaceHolder = 500
	PlaceHolderSize          = HeadersPerProposal * HeaderSizePerPlaceHolder
)
View Source
const HashSize = 32

HashSize is the array size used to store sha hashes. See Hash.

View Source
const MaxBlockPayload = 2000000

MaxBlockPayload is the maximum bytes a block message can be in bytes. 2MB.

View Source
const MaxHashStringSize = HashSize * 2

MaxHashStringSize is the maximum length of a Hash hash string.

View Source
const MaxTxPerBlock = (MaxBlockPayload / minTxPayload) + 1

maxTxPerBlock is the maximum number of transactions that could possibly fit into a block.

View Source
const MinBlockHeaderPayload = blockHeaderMinPlainSize

Variables

View Source
var ErrHashStrSize = fmt.Errorf("max hash string length is %v bytes", MaxHashStringSize)

ErrHashStrSize describes an error that indicates the caller specified a hash string that has too many characters.

View Source
var (
	ErrInvalidCodecMode = errors.New("invalid codec mode for wire")
)

Functions

func Decode

func Decode(dst *Hash, src string) error

Decode decodes the byte-reversed hexadecimal string encoding of a Hash to a destination.

func DoubleHashB

func DoubleHashB(b []byte) []byte

DoubleHashB calculates sha256(sha256(b)) and returns the resulting bytes.

func Hash2CommonHash

func Hash2CommonHash(h Hash) common.Hash

func HashB

func HashB(b []byte) []byte

HashB calculates hash(b) and returns the resulting bytes.

func IsValidFrozenPeriod

func IsValidFrozenPeriod(height uint64) bool

func IsValidStakingValue

func IsValidStakingValue(value int64) bool

func MoveBytes

func MoveBytes(bs []byte) []byte

func NewEmptyBLSPublicKey

func NewEmptyBLSPublicKey() *chiapos.G1Element

func NewEmptyBLSSignature

func NewEmptyBLSSignature() *chiapos.G2Element

func NewEmptyBigInt

func NewEmptyBigInt() *big.Int

func NewEmptyPoCPublicKey

func NewEmptyPoCPublicKey() *pocec.PublicKey

func NewEmptyPoCSignature

func NewEmptyPoCSignature() *pocec.Signature

func ReadUint64

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

ReadUint64 reads a variable from r for 8 Bytes and returns it as a uint64.

func WriteTxOut

func WriteTxOut(w io.Writer, to *TxOut) error

WriteTxOut encodes to into the mass protocol encoding for a transaction output (TxOut) to w.

func WriteUint64

func WriteUint64(w io.Writer, val uint64) (int, error)

WriteUint64 serializes val to 8 Bytes

func WriteVarBytes

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

Types

type BlockBase

type BlockBase struct {
	Header    BlockHeader
	Proposals ProposalArea
}

func NewBlockBaseFromProto

func NewBlockBaseFromProto(pb *wirepb.BlockBase) (*BlockBase, error)

func (*BlockBase) FromProto

func (base *BlockBase) FromProto(pb *wirepb.BlockBase) error

func (*BlockBase) ToProto

func (base *BlockBase) ToProto() (*wirepb.BlockBase, error)

type BlockHeader

type BlockHeader struct {
	ChainID         Hash
	Version         uint64
	Height          uint64
	Timestamp       time.Time
	Previous        Hash
	TransactionRoot Hash
	WitnessRoot     Hash
	ProposalRoot    Hash
	Target          *big.Int
	Challenge       Hash
	PubKey          interfaces.PublicKey
	Proof           poc.Proof
	Signature       interfaces.Signature
	BanList         []interfaces.PublicKey
	BindingRoot     common.Hash // use this while header.Version >= 2
}

func MockHeader

func MockHeader(version uint64, proofType poc.ProofType) *BlockHeader

func NewBlockHeaderFromBytes

func NewBlockHeaderFromBytes(bhBytes []byte, mode CodecMode) (*BlockHeader, error)

func NewBlockHeaderFromProto

func NewBlockHeaderFromProto(pb *wirepb.BlockHeader) (*BlockHeader, error)

NewBlockHeaderFromProto get wire BlockHeader from proto BlockHeader

func NewEmptyBlockHeader

func NewEmptyBlockHeader() *BlockHeader

func (*BlockHeader) BannedPublicKeys

func (h *BlockHeader) BannedPublicKeys() (banned []interfaces.PublicKey)

Both old and new public keys are allowed packed

func (*BlockHeader) BlockHash

func (h *BlockHeader) BlockHash() Hash

BlockHash computes the block identifier hash for the given block header.

func (*BlockHeader) Bytes

func (h *BlockHeader) Bytes(mode CodecMode) ([]byte, error)

func (*BlockHeader) CheckVersionConstraint

func (h *BlockHeader) CheckVersionConstraint() error

func (*BlockHeader) Decode

func (h *BlockHeader) Decode(r io.Reader, mode CodecMode) (n int, err error)

Decode decodes r using the given protocol encoding into the receiver.

func (*BlockHeader) Encode

func (h *BlockHeader) Encode(w io.Writer, mode CodecMode) (n int, err error)

Encode encodes the receiver to w using the given protocol encoding.

func (*BlockHeader) FromProto

func (h *BlockHeader) FromProto(pb *wirepb.BlockHeader) error

FromProto load proto BlockHeader into wire BlockHeader

func (*BlockHeader) GetChainID

func (h *BlockHeader) GetChainID() (Hash, error)

GetChainID calc chainID, only block with 0 height can be calc

func (*BlockHeader) PlainSize

func (h *BlockHeader) PlainSize() int

func (*BlockHeader) PoCHash

func (h *BlockHeader) PoCHash() (Hash, error)

PoCHash generate hash of all PoC needed elements in block header

func (*BlockHeader) PublicKey

func (h *BlockHeader) PublicKey() interfaces.PublicKey

func (*BlockHeader) Quality

func (h *BlockHeader) Quality() *big.Int

Quality

func (*BlockHeader) SetBanListFromProposals

func (h *BlockHeader) SetBanListFromProposals(proposals []*FaultPubKey) error

func (*BlockHeader) SetBytes

func (h *BlockHeader) SetBytes(bs []byte, mode CodecMode) error

func (*BlockHeader) ToProto

func (h *BlockHeader) ToProto() *wirepb.BlockHeader

ToProto get proto BlockHeader from wire BlockHeader

func (*BlockHeader) VerifySig

func (h *BlockHeader) VerifySig() (bool, error)

type CodecMode

type CodecMode uint64

Mode to encode/decode data

const (
	DB CodecMode = iota
	Packet
	Plain
	ID
	WitnessID
	PoCID
	ChainID
)

type FaultPubKey

type FaultPubKey struct {
	PubKey    interfaces.PublicKey
	Testimony [HeadersPerProposal]*BlockHeader
	// contains filtered or unexported fields
}

FaultPubKey implements Proposal, representing for punishment-double-mining-pubKey

func NewEmptyFaultPubKey

func NewEmptyFaultPubKey() *FaultPubKey

func NewFaultPubKeyFromBytes

func NewFaultPubKeyFromBytes(bs []byte, mode CodecMode) (*FaultPubKey, error)

func (*FaultPubKey) Bytes

func (fpk *FaultPubKey) Bytes(mode CodecMode) ([]byte, error)

func (*FaultPubKey) Decode

func (fpk *FaultPubKey) Decode(r io.Reader, mode CodecMode) (n int, err error)

func (*FaultPubKey) Encode

func (fpk *FaultPubKey) Encode(w io.Writer, mode CodecMode) (n int, err error)

func (*FaultPubKey) FromProto

func (fpk *FaultPubKey) FromProto(pb *wirepb.Punishment) error

func (*FaultPubKey) Hash

func (fpk *FaultPubKey) Hash(index int) Hash

func (*FaultPubKey) IsValid

func (fpk *FaultPubKey) IsValid() error

func (*FaultPubKey) PlainSize

func (fpk *FaultPubKey) PlainSize() int

PlainSize returns the number of bytes the FaultPubKey contains.

func (*FaultPubKey) SetBytes

func (fpk *FaultPubKey) SetBytes(bs []byte, mode CodecMode) error

func (*FaultPubKey) ToProto

func (fpk *FaultPubKey) ToProto() *wirepb.Punishment

func (*FaultPubKey) Type

func (fpk *FaultPubKey) Type() ProposalType

func (*FaultPubKey) Version

func (fpk *FaultPubKey) Version() uint32

type Hash

type Hash [HashSize]byte

Hash is used in several of the mass messages and common structures. It typically represents the double sha256 of data.

func BuildMerkleTreeStoreForProposal

func BuildMerkleTreeStoreForProposal(proposals *ProposalArea) []*Hash

func BuildMerkleTreeStoreTransactions

func BuildMerkleTreeStoreTransactions(transactions []*MsgTx, witness bool) []*Hash

func CommonHash2Hash

func CommonHash2Hash(h *common.Hash) *Hash

func DoubleHashH

func DoubleHashH(b []byte) Hash

DoubleHashH calculates sha256(sha256(b)) and returns the resulting bytes as a Hash.

func GetMerkleRootFromCache

func GetMerkleRootFromCache(baseHash *Hash, cache []*Hash) Hash

func GetProposalHash

func GetProposalHash(proposal Proposal, index int) Hash

func HashH

func HashH(b []byte) Hash

HashH calculates hash(b) and returns the resulting bytes as a Hash.

func HashMerkleBranches

func HashMerkleBranches(left *Hash, right *Hash) *Hash

HashMerkleBranches takes two hashes, treated as the left and right tree nodes, and returns the hash of their concatenation. This is a helper function used to aid in the generation of a merkle tree.

func NewHash

func NewHash(newHash []byte) (*Hash, error)

NewHash returns a new Hash from a byte slice. An error is returned if the number of bytes passed in is not HashSize.

func NewHashFromHash

func NewHashFromHash(h Hash) *Hash

func NewHashFromProto

func NewHashFromProto(pb *wirepb.Hash) (*Hash, error)

NewHashFromProto get Hash From proto Hash

func NewHashFromStr

func NewHashFromStr(hash string) (*Hash, error)

NewHashFromStr creates a Hash from a hash string. The string should be the hexadecimal string of a byte-reversed hash, but any missing characters result in zero padding at the end of the Hash.

func (*Hash) Bytes

func (hash *Hash) Bytes() []byte

Bytes returns the bytes which represent the hash as a byte slice.

NOTE: This makes a copy of the bytes and should have probably been named CloneBytes. It is generally cheaper to just slice the hash directly thereby reusing the same bytes rather than calling this method.

func (*Hash) FromProto

func (hash *Hash) FromProto(pb *wirepb.Hash) error

FromProto load proto Hash into wire Hash

func (*Hash) IsEqual

func (hash *Hash) IsEqual(target *Hash) bool

IsEqual returns true if target is the same as hash.

func (Hash) Ptr

func (hash Hash) Ptr() *Hash

func (*Hash) SetBytes

func (hash *Hash) SetBytes(newHash []byte) error

SetBytes sets the bytes which represent the hash. An error is returned if the number of bytes passed in is not HashSize.

func (Hash) String

func (hash Hash) String() string

String returns the Hash as the hexadecimal string of the byte-reversed hash.

func (*Hash) ToProto

func (hash *Hash) ToProto() *wirepb.Hash

ToProto get proto Hash from Hash

type Message

type Message interface {
	Encode(io.Writer, CodecMode) (int, error)
	Decode(io.Reader, CodecMode) (int, error)
	Bytes(CodecMode) ([]byte, error)
	SetBytes([]byte, CodecMode) error
	PlainSize() int
}

type MsgBlock

type MsgBlock struct {
	Header       BlockHeader
	Proposals    ProposalArea
	Transactions []*MsgTx
}

MsgBlock implements the Message interface and represents a mass block message. It is used to deliver block and transaction information in response to a getdata message (MsgGetData) for a given block hash.

func NewBlockFromProto

func NewBlockFromProto(pb *wirepb.Block) (*MsgBlock, error)

NewBlockFromProto get wire Block from proto Block

func NewEmptyMsgBlock

func NewEmptyMsgBlock() *MsgBlock

func NewMsgBlock

func NewMsgBlock(blockHeader *BlockHeader) *MsgBlock

NewMsgBlock returns a new mass block message that conforms to the Message interface. See MsgBlock for details.

func (*MsgBlock) AddTransaction

func (msg *MsgBlock) AddTransaction(tx *MsgTx)

AddTransaction adds a transaction to the message.

func (*MsgBlock) BlockHash

func (msg *MsgBlock) BlockHash() Hash

BlockHash computes the block identifier hash for this block.

func (*MsgBlock) Bytes

func (msg *MsgBlock) Bytes(mode CodecMode) ([]byte, error)

func (*MsgBlock) ClearTransactions

func (msg *MsgBlock) ClearTransactions()

ClearTransactions removes all transactions from the message.

func (*MsgBlock) Decode

func (msg *MsgBlock) Decode(r io.Reader, mode CodecMode) (n int, err error)

Decode decodes r using the given protocol encoding into the receiver.

func (*MsgBlock) DeserializeTxLoc

func (msg *MsgBlock) DeserializeTxLoc(r *bytes.Buffer) ([]TxLoc, error)

func (*MsgBlock) Encode

func (msg *MsgBlock) Encode(w io.Writer, mode CodecMode) (n int, err error)

Encode encodes the receiver to w using the given protocol encoding.

func (*MsgBlock) FromProto

func (msg *MsgBlock) FromProto(pb *wirepb.Block) error

FromProto load proto Block into wire Block, if error happens, old content is still immutable

func (*MsgBlock) PlainSize

func (msg *MsgBlock) PlainSize() int

PlainSize returns the number of plain bytes the block contains.

func (*MsgBlock) SetBytes

func (msg *MsgBlock) SetBytes(bs []byte, mode CodecMode) error

func (*MsgBlock) ToProto

func (msg *MsgBlock) ToProto() (*wirepb.Block, error)

ToProto get proto Block from wire Block

func (*MsgBlock) TxHashes

func (msg *MsgBlock) TxHashes() ([]Hash, error)

TxHashes returns a slice of hashes of all of transactions in this block.

type MsgTx

type MsgTx struct {
	Version  uint32
	TxIn     []*TxIn
	TxOut    []*TxOut
	LockTime uint64
	Payload  []byte
}

MsgTx implements the Message interface and represents a mass tx message. It is used to deliver transaction information in response to a getdata message (MsgGetData) for a given transaction.

Use the AddTxIn and AddTxOut functions to build up the list of transaction inputs and outputs.

func NewMsgTx

func NewMsgTx() *MsgTx

NewMsgTx returns a new mass tx message that conforms to the Message interface. The return instance has a default version of TxVersion and there are no transaction inputs or outputs. Also, the lock time is set to zero to indicate the transaction is valid immediately as opposed to some time in future.

func NewTxFromProto

func NewTxFromProto(pb *wirepb.Tx) (*MsgTx, error)

NewTxFromProto get wire Tx from proto Tx

func (*MsgTx) AddTxIn

func (msg *MsgTx) AddTxIn(ti *TxIn)

AddTxIn adds a transaction input to the message.

func (*MsgTx) AddTxOut

func (msg *MsgTx) AddTxOut(to *TxOut)

AddTxOut adds a transaction output to the message.

func (*MsgTx) Bytes

func (msg *MsgTx) Bytes(mode CodecMode) ([]byte, error)

func (*MsgTx) Decode

func (msg *MsgTx) Decode(r io.Reader, mode CodecMode) (n int, err error)

func (*MsgTx) Encode

func (msg *MsgTx) Encode(w io.Writer, mode CodecMode) (n int, err error)

func (*MsgTx) FromProto

func (msg *MsgTx) FromProto(pb *wirepb.Tx) error

FromProto load proto TX into wire Tx

func (*MsgTx) IsCoinBaseTx

func (msg *MsgTx) IsCoinBaseTx() bool

IsCoinBaseTx determines whether or not a transaction is a coinbase. A coinbase is a special transaction created by miners that has no inputs. This is represented in the block chain by a transaction with a single input that has a previous output transaction index set to the maximum value along with a zero hash.

This function only differs from IsCoinBase in that it works with a raw wire transaction as opposed to a higher level util transaction.

func (*MsgTx) PlainSize

func (msg *MsgTx) PlainSize() int

PlainSize returns the number of bytes the transaction contains.

func (*MsgTx) RemoveAllTxOut

func (msg *MsgTx) RemoveAllTxOut()

func (*MsgTx) RemoveTxOut

func (msg *MsgTx) RemoveTxOut(index int)

func (*MsgTx) SetBytes

func (msg *MsgTx) SetBytes(bs []byte, mode CodecMode) error

func (*MsgTx) SetPayload

func (msg *MsgTx) SetPayload(payload []byte)

func (*MsgTx) ToProto

func (msg *MsgTx) ToProto() *wirepb.Tx

ToProto get proto Tx from wire Tx

func (*MsgTx) TxHash

func (msg *MsgTx) TxHash() Hash

TxHash generates the Hash for the transaction.

func (*MsgTx) WitnessHash

func (msg *MsgTx) WitnessHash() Hash

WitnessHash generates the hash of the transaction serialized according to the new witness serialization defined in BIP0141 and BIP0144. The final output is used within the Segregated Witness commitment of all the witnesses within a block. If a transaction has no witness data, then the witness hash, is the same as its txid.

type NormalProposal

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

NormalProposal implements Proposal, representing for all proposals with no consensus proposalType.

func NewPlaceHolder

func NewPlaceHolder() *NormalProposal

PlaceHolder prevents miner from eliminating all punishment proposals, because that is more profitable to save block size for transactions.

func (*NormalProposal) Bytes

func (np *NormalProposal) Bytes(mode CodecMode) ([]byte, error)

func (*NormalProposal) Content

func (np *NormalProposal) Content() []byte

func (*NormalProposal) Decode

func (np *NormalProposal) Decode(r io.Reader, mode CodecMode) (n int, err error)

func (*NormalProposal) Encode

func (np *NormalProposal) Encode(w io.Writer, mode CodecMode) (n int, err error)

func (*NormalProposal) FromProto

func (np *NormalProposal) FromProto(pb *wirepb.Proposal) error

func (*NormalProposal) Hash

func (np *NormalProposal) Hash(index int) Hash

func (*NormalProposal) PlainSize

func (np *NormalProposal) PlainSize() int

func (*NormalProposal) SetBytes

func (np *NormalProposal) SetBytes(bs []byte, mode CodecMode) error

func (*NormalProposal) ToProto

func (np *NormalProposal) ToProto() *wirepb.Proposal

func (*NormalProposal) Type

func (np *NormalProposal) Type() ProposalType

func (*NormalProposal) Version

func (np *NormalProposal) Version() uint32

type OutPoint

type OutPoint struct {
	Hash  Hash
	Index uint32
}

OutPoint defines a mass data type that is used to track previous transaction outputs.

func NewOutPoint

func NewOutPoint(hash *Hash, index uint32) *OutPoint

NewOutPoint returns a new mass transaction outpoint point with the provided hash and index.

func (OutPoint) String

func (o OutPoint) String() string

String returns the OutPoint in the human-readable form "hash:index".

type Proposal

type Proposal interface {
	Bytes(CodecMode) ([]byte, error)
	SetBytes([]byte, CodecMode) error
	Hash(int) Hash
}

General Proposal Interface

type ProposalArea

type ProposalArea struct {
	PunishmentArea []*FaultPubKey
	OtherArea      []*NormalProposal
}

ProposalArea represents for proposals in blocks.

func NewProposalArea

func NewProposalArea(punishmentArea []*FaultPubKey, otherArea []*NormalProposal) (*ProposalArea, error)

func NewProposalAreaFromProto

func NewProposalAreaFromProto(pb *wirepb.ProposalArea) (*ProposalArea, error)

NewProposalAreaFromProto get wire ProposalArea from proto ProposalArea

func (*ProposalArea) Bytes

func (pa *ProposalArea) Bytes(mode CodecMode) ([]byte, error)

func (*ProposalArea) Count

func (pa *ProposalArea) Count() int

func (*ProposalArea) Decode

func (pa *ProposalArea) Decode(r io.Reader, mode CodecMode) (n int, err error)

func (*ProposalArea) Encode

func (pa *ProposalArea) Encode(w io.Writer, mode CodecMode) (n int, err error)

func (*ProposalArea) FromProto

func (pa *ProposalArea) FromProto(pb *wirepb.ProposalArea) error

FromProto load proto ProposalArea into wire ProposalArea, if error happens, old content is still immutable

func (*ProposalArea) OtherCount

func (pa *ProposalArea) OtherCount() int

func (*ProposalArea) PlainSize

func (pa *ProposalArea) PlainSize() int

func (*ProposalArea) PunishmentCount

func (pa *ProposalArea) PunishmentCount() int

func (*ProposalArea) SetBytes

func (pa *ProposalArea) SetBytes(bs []byte, mode CodecMode) error

func (*ProposalArea) ToProto

func (pa *ProposalArea) ToProto() (*wirepb.ProposalArea, error)

ToProto get proto ProposalArea from wire ProposalArea

type ProposalType

type ProposalType uint32

type TxIn

type TxIn struct {
	PreviousOutPoint OutPoint
	Witness          TxWitness
	Sequence         uint64
}

TxIn defines a mass transaction input.

func NewTxIn

func NewTxIn(prevOut *OutPoint, witness [][]byte) *TxIn

NewTxIn returns a new mass transaction input with the provided previous outpoint point and signature script with a default sequence of MaxTxInSequenceNum.

func (*TxIn) PlainSize

func (t *TxIn) PlainSize() int

PlainSize returns the number of bytes it would take to serialize the the transaction input.

type TxLoc

type TxLoc struct {
	TxStart int
	TxLen   int
}

TxLoc holds locator data for the offset and length of where a transaction is located within a MsgBlock data buffer.

type TxOut

type TxOut struct {
	Value    int64
	PkScript []byte
}

TxOut defines a mass transaction output.

func NewTxOut

func NewTxOut(value int64, pkScript []byte) *TxOut

NewTxOut returns a new mass transaction output with the provided transaction value and public key script.

func (*TxOut) PlainSize

func (t *TxOut) PlainSize() int

PlainSize returns the number of bytes it would take to serialize the the transaction output.

type TxWitness

type TxWitness [][]byte

TxWitness defines the witness for a TxIn. A witness is to be interpreted as a slice of byte slices, or a stack with one or many elements.

func (TxWitness) PlainSize

func (t TxWitness) PlainSize() int

PlainSize returns the number of bytes it would take to serialize the the transaction input's witness.

type WitnessRedeemScriptHash

type WitnessRedeemScriptHash [sha256.Size]byte

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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