bc

package
v0.0.0-...-0b9d80d Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2016 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package bc provides the fundamental blockchain data structures used in the Chain Protocol.

Index

Constants

View Source
const (
	SerBlockWitness      = 1
	SerBlockTransactions = 2

	SerBlockSigHash = 0
	SerBlockHeader  = SerBlockWitness
	SerBlockFull    = SerBlockWitness | SerBlockTransactions
)
View Source
const (
	SerWitness uint8 = 1 << iota
	SerPrevout
	SerMetadata

	// Bit mask for accepted serialization flags.
	// All other flag bits must be 0.
	SerValid = 0x7
)

These flags are part of the wire protocol; they must not change.

View Source
const CurrentTransactionVersion = 1

CurrentTransactionVersion is the current latest supported transaction version.

View Source
const NewBlockVersion = 1

NewBlockVersion is the version to use when creating new blocks.

Variables

This section is empty.

Functions

func DurationMillis

func DurationMillis(d time.Duration) uint64

DurationMillis converts a time.Duration to a number of milliseconds.

func Millis

func Millis(t time.Time) uint64

Millis converts a time.Time to a number of milliseconds since 1970.

Types

type AssetAmount

type AssetAmount struct {
	AssetID AssetID `json:"asset_id"`
	Amount  uint64  `json:"amount"`
}

type AssetID

type AssetID [32]byte

AssetID is the Hash256 of the issuance script for the asset and the initial block of the chain where it appears.

func ComputeAssetID

func ComputeAssetID(issuanceProgram []byte, initialHash [32]byte, vmVersion uint64) (assetID AssetID)

ComputeAssetID computes the asset ID of the asset defined by the given issuance program and initial block hash.

func (AssetID) MarshalText

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

func (*AssetID) Scan

func (a *AssetID) Scan(b interface{}) error

func (AssetID) String

func (a AssetID) String() string

func (*AssetID) UnmarshalJSON

func (a *AssetID) UnmarshalJSON(b []byte) error

func (*AssetID) UnmarshalText

func (a *AssetID) UnmarshalText(b []byte) error

func (AssetID) Value

func (a AssetID) Value() (driver.Value, error)

type Block

type Block struct {
	BlockHeader
	Transactions []*Tx
}

Block describes a complete block, including its header and the transactions it contains.

func (*Block) MarshalText

func (b *Block) MarshalText() ([]byte, error)

MarshalText fulfills the json.Marshaler interface. This guarantees that blocks will get deserialized correctly when being parsed from HTTP requests.

func (*Block) Scan

func (b *Block) Scan(val interface{}) error

Scan fulfills the sql.Scanner interface.

func (*Block) UnmarshalText

func (b *Block) UnmarshalText(text []byte) error

UnmarshalText fulfills the encoding.TextUnmarshaler interface.

func (*Block) Value

func (b *Block) Value() (driver.Value, error)

Value fulfills the sql.driver.Valuer interface.

func (*Block) WriteTo

func (b *Block) WriteTo(w io.Writer) (int64, error)

type BlockHeader

type BlockHeader struct {
	// Version of the block.
	Version uint64

	// Height of the block in the block chain.
	// Initial block has height 1.
	Height uint64

	// Hash of the previous block in the block chain.
	PreviousBlockHash Hash

	// Time of the block in milliseconds.
	// Must grow monotonically and can be equal
	// to the time in the previous block.
	TimestampMS uint64

	// TransactionsMerkleRoot is the root hash of the Merkle binary hash
	// tree formed by the transaction witness hashes of all transactions
	// included in the block.
	TransactionsMerkleRoot Hash

	// AssetsMerkleRoot is the root hash of the Merkle Patricia Tree of
	// the set of unspent outputs with asset version 1 after applying
	// the block.
	AssetsMerkleRoot Hash

	// ConsensusProgram is the predicate for validating the next block.
	ConsensusProgram []byte

	// Witness is a vector of arguments to the previous block's
	// ConsensusProgram for validating this block.
	Witness [][]byte
}

BlockHeader describes necessary data of the block.

func (*BlockHeader) Hash

func (bh *BlockHeader) Hash() Hash

Hash returns complete hash of the block header.

func (*BlockHeader) HashForSig

func (bh *BlockHeader) HashForSig() Hash

HashForSig returns a hash of the block header without witness. This hash is used for signing the block and verifying the signature.

func (*BlockHeader) Scan

func (bh *BlockHeader) Scan(val interface{}) error

func (*BlockHeader) Time

func (bh *BlockHeader) Time() time.Time

Time returns the time represented by the Timestamp in bh.

func (*BlockHeader) Value

func (bh *BlockHeader) Value() (driver.Value, error)

func (*BlockHeader) WriteForSigTo

func (bh *BlockHeader) WriteForSigTo(w io.Writer) (int64, error)

WriteForSigTo writes bh to w in a format suitable for signing.

func (*BlockHeader) WriteTo

func (bh *BlockHeader) WriteTo(w io.Writer) (int64, error)

type Hash

type Hash [32]byte

Hash represents a 256-bit hash. By convention, Hash objects are typically passed as values, not as pointers.

func ParseHash

func ParseHash(s string) (h Hash, err error)

ParseHash takes a hex-encoded hash and returns a 32 byte array.

func (Hash) MarshalText

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

MarshalText satisfies the TextMarshaler interface. It returns the bytes of h encoded in hex, for formats that can't hold arbitrary binary data. It never returns an error.

func (*Hash) Scan

func (h *Hash) Scan(val interface{}) error

Scan satisfies the driver.Scanner interface

func (Hash) String

func (h Hash) String() string

String returns the bytes of h encoded in hex.

func (*Hash) UnmarshalJSON

func (h *Hash) UnmarshalJSON(b []byte) error

UnmarshalJSON satisfies the json.Unmarshaler interface. If b is a JSON-encoded null, it copies the zero-value into h. Othwerwise, it decodes hex data from b into h.

func (*Hash) UnmarshalText

func (h *Hash) UnmarshalText(b []byte) error

UnmarshalText satisfies the TextUnmarshaler interface. It decodes hex data from b into h.

func (Hash) Value

func (h Hash) Value() (driver.Value, error)

Value satisfies the driver.Valuer interface

type IssuanceInput

type IssuanceInput struct {
	// Commitment
	Nonce  []byte
	Amount uint64

	// Witness
	InitialBlock    Hash
	VMVersion       uint64
	IssuanceProgram []byte
	Arguments       [][]byte
}

func (IssuanceInput) AssetID

func (ii IssuanceInput) AssetID() AssetID

func (IssuanceInput) IsIssuance

func (ii IssuanceInput) IsIssuance() bool

type Outpoint

type Outpoint struct {
	Hash  Hash   `json:"hash"`
	Index uint32 `json:"index"`
}

Outpoint defines a bitcoin data type that is used to track previous transaction outputs.

func (Outpoint) String

func (p Outpoint) String() string

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

func (Outpoint) WriteTo

func (p Outpoint) WriteTo(w io.Writer) (int64, error)

WriteTo writes p to w. It assumes w has sticky errors.

type OutputCommitment

type OutputCommitment struct {
	AssetAmount
	VMVersion      uint64
	ControlProgram []byte
}

type SigHasher

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

SigHasher caches a txhash for reuse with multiple inputs.

func NewSigHasher

func NewSigHasher(txData *TxData) *SigHasher

func (*SigHasher) Hash

func (s *SigHasher) Hash(idx int) Hash

type SpendInput

type SpendInput struct {
	// Commitment
	Outpoint
	OutputCommitment

	// Witness
	Arguments [][]byte
}

func (SpendInput) IsIssuance

func (si SpendInput) IsIssuance() bool

type Tx

type Tx struct {
	TxData
	Hash Hash
}

Tx holds a transaction along with its hash.

func NewTx

func NewTx(data TxData) *Tx

NewTx returns a new Tx containing data and its hash. If you have already computed the hash, use struct literal notation to make a Tx object directly.

func (*Tx) UnmarshalText

func (tx *Tx) UnmarshalText(p []byte) error

type TxData

type TxData struct {
	Version       uint64
	Inputs        []*TxInput
	Outputs       []*TxOutput
	MinTime       uint64
	MaxTime       uint64
	ReferenceData []byte
}

TxData encodes a transaction in the blockchain. Most users will want to use Tx instead; it includes the hash.

func (*TxData) HasIssuance

func (tx *TxData) HasIssuance() bool

HasIssuance returns true if this transaction has an issuance input.

func (*TxData) Hash

func (tx *TxData) Hash() Hash

Hash computes the hash of the transaction with reference data fields replaced by their hashes, and stores the result in Hash.

func (*TxData) HashForSig

func (tx *TxData) HashForSig(idx int) Hash

HashForSig generates the hash required for the specified input's signature.

func (*TxData) IssuanceHash

func (tx *TxData) IssuanceHash(n int) (h Hash, err error)

func (*TxData) MarshalText

func (tx *TxData) MarshalText() ([]byte, error)

func (*TxData) Scan

func (tx *TxData) Scan(val interface{}) error

func (*TxData) UnmarshalText

func (tx *TxData) UnmarshalText(p []byte) error

func (*TxData) Value

func (tx *TxData) Value() (driver.Value, error)

func (*TxData) WitnessHash

func (tx *TxData) WitnessHash() Hash

WitnessHash is the combined hash of the transactions hash and signature data hash. It is used to compute the TxRoot of a block.

func (*TxData) WriteTo

func (tx *TxData) WriteTo(w io.Writer) (int64, error)

WriteTo writes tx to w.

type TxInput

type TxInput struct {
	AssetVersion  uint64
	ReferenceData []byte
	TypedInput
}

func NewIssuanceInput

func NewIssuanceInput(nonce []byte, amount uint64, referenceData []byte, initialBlock Hash, issuanceProgram []byte, arguments [][]byte) *TxInput

func NewSpendInput

func NewSpendInput(txhash Hash, index uint32, arguments [][]byte, assetID AssetID, amount uint64, controlProgram, referenceData []byte) *TxInput

func (TxInput) Amount

func (t TxInput) Amount() uint64

func (TxInput) Arguments

func (t TxInput) Arguments() [][]byte

func (TxInput) AssetAmount

func (t TxInput) AssetAmount() AssetAmount

func (TxInput) AssetID

func (t TxInput) AssetID() AssetID

func (TxInput) ControlProgram

func (t TxInput) ControlProgram() []byte

func (TxInput) InputCommitmentBytes

func (t TxInput) InputCommitmentBytes() []byte

func (TxInput) IssuanceProgram

func (t TxInput) IssuanceProgram() []byte

func (TxInput) Outpoint

func (t TxInput) Outpoint() (o Outpoint)

func (*TxInput) SetArguments

func (t *TxInput) SetArguments(args [][]byte)

func (TxInput) WitnessHash

func (t TxInput) WitnessHash() Hash

type TxOutput

type TxOutput struct {
	AssetVersion uint64
	OutputCommitment
	ReferenceData []byte
}

func NewTxOutput

func NewTxOutput(assetID AssetID, amount uint64, controlProgram, referenceData []byte) *TxOutput

func (TxOutput) Commitment

func (to TxOutput) Commitment() []byte

func (TxOutput) WitnessHash

func (to TxOutput) WitnessHash() Hash

type TypedInput

type TypedInput interface {
	IsIssuance() bool
}

Jump to

Keyboard shortcuts

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