tbcd

package
v0.2.10 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DeleteUtxo = [8]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}

DeleteUtxo is the max uint64 value which is used as a sentinel to indicate that a utxo should be reaped. The remaining fields must remain untouched since they are part of the lookup key of the utxo balance.

Functions

func B2H added in v0.2.0

func B2H(header []byte) (*wire.BlockHeader, error)

B2H converts a raw block header to a wire block header structure.

func HeaderHash added in v0.2.0

func HeaderHash(header []byte) *chainhash.Hash

HeaderHash return the block hash from a raw block header.

func HeaderParentHash added in v0.2.0

func HeaderParentHash(header []byte) *chainhash.Hash

HeaderHash return the parent block hash from a raw block header.

func NewTxSpent

func NewTxSpent(blockHash, txId, inPrevHash *chainhash.Hash, inPrevIndex, txInIndex uint32) (txKey TxKey, txValue TxValue)

NewTxSpent returns a TxKey and TxValue that maps a spent transaction to a location in a block.

func TxIdBlockHashFromTxKey added in v0.2.8

func TxIdBlockHashFromTxKey(txKey TxKey) (*TxId, *BlockHash, error)

Types

type Block

type Block struct {
	Hash  database.ByteArray
	Block database.ByteArray
}

Block contains a raw bitcoin block and its corresponding hash.

type BlockHash

type BlockHash [32]byte

BlockHash is a bitcoin transaction id. The underlying slice is reversed, only when using the stringer does it apear in human readable format.

func NewBlockHash

func NewBlockHash(x [32]byte) (blockHash BlockHash)

func NewBlockHashFromBytes

func NewBlockHashFromBytes(x []byte) (blockHash BlockHash, err error)

func (BlockHash) Hash added in v0.2.8

func (bh BlockHash) Hash() *chainhash.Hash

func (BlockHash) String

func (bh BlockHash) String() string

type BlockHeader

type BlockHeader struct {
	Hash       database.ByteArray
	Height     uint64
	Header     database.ByteArray
	Difficulty big.Int
}

BlockHeader contains the first 80 raw bytes of a bitcoin block plus its location information (hash+height) and the cumulative difficulty.

func (BlockHeader) BlockHash added in v0.2.0

func (bh BlockHeader) BlockHash() *chainhash.Hash

func (BlockHeader) ParentHash added in v0.2.0

func (bh BlockHeader) ParentHash() *chainhash.Hash

func (BlockHeader) String

func (bh BlockHeader) String() string

func (BlockHeader) Timestamp

func (bh BlockHeader) Timestamp() time.Time

func (BlockHeader) Wire

func (bh BlockHeader) Wire() (*wire.BlockHeader, error)

type BlockIdentifier

type BlockIdentifier struct {
	Height uint64
	Hash   database.ByteArray
}

BlockIdentifier uniquely identifies a block using it's hash and height.

type CacheOutput

type CacheOutput [32 + 8 + 4]byte // script_hash + value + out_idx

CacheOutput is a densely packed representation of a bitcoin UTXo. The fields are script_hash + value + out_index. It is packed for memory conservation reasons.

func NewCacheOutput

func NewCacheOutput(hash [32]byte, value uint64, outIndex uint32) (co CacheOutput)

func NewDeleteCacheOutput

func NewDeleteCacheOutput(hash [32]byte, outIndex uint32) (co CacheOutput)

func (CacheOutput) Equal

func (c CacheOutput) Equal(x CacheOutput) bool

func (CacheOutput) IsDelete

func (c CacheOutput) IsDelete() bool

func (CacheOutput) OutputIndex

func (c CacheOutput) OutputIndex() uint32

func (CacheOutput) OutputIndexBytes

func (c CacheOutput) OutputIndexBytes() []byte

func (CacheOutput) ScriptHash

func (c CacheOutput) ScriptHash() (hash [32]byte)

func (CacheOutput) ScriptHashSlice

func (c CacheOutput) ScriptHashSlice() []byte

func (CacheOutput) String

func (c CacheOutput) String() string

String reutrns pretty printable CacheOutput. Hash is not reversed since it is an opaque pointer. It prints satoshis@script_hash:output_index

func (CacheOutput) Value

func (c CacheOutput) Value() uint64

func (CacheOutput) ValueBytes

func (c CacheOutput) ValueBytes() []byte

type Database

type Database interface {
	database.Database

	// Metadata
	Version(ctx context.Context) (int, error)
	MetadataGet(ctx context.Context, key []byte) ([]byte, error)
	MetadataPut(ctx context.Context, key, value []byte) error

	// Block header
	BlockHeaderBest(ctx context.Context) (*BlockHeader, error) // return canonical
	BlockHeaderByHash(ctx context.Context, hash []byte) (*BlockHeader, error)
	BlockHeaderGenesisInsert(ctx context.Context, bh [80]byte) error

	// Block headers
	BlockHeadersByHeight(ctx context.Context, height uint64) ([]BlockHeader, error)
	BlockHeadersInsert(ctx context.Context, bhs [][80]byte) (InsertType, *BlockHeader, *BlockHeader, error)

	// Block
	BlocksMissing(ctx context.Context, count int) ([]BlockIdentifier, error)
	BlockInsert(ctx context.Context, b *Block) (int64, error)
	// XXX replace BlockInsert with plural version
	// BlocksInsert(ctx context.Context, bs []*Block) (int64, error)
	BlockByHash(ctx context.Context, hash []byte) (*Block, error)

	// Transactions
	BlockUtxoUpdate(ctx context.Context, direction int, utxos map[Outpoint]CacheOutput) error
	BlockTxUpdate(ctx context.Context, direction int, txs map[TxKey]*TxValue) error
	BlocksByTxId(ctx context.Context, txId []byte) ([]BlockHash, error)
	SpentOutputsByTxId(ctx context.Context, txId []byte) ([]SpentInfo, error)

	// Peer manager
	PeersStats(ctx context.Context) (int, int)               // good, bad count
	PeersInsert(ctx context.Context, peers []Peer) error     // insert or update
	PeerDelete(ctx context.Context, host, port string) error // remove peer
	PeersRandom(ctx context.Context, count int) ([]Peer, error)

	// ScriptHash returns the sha256 of PkScript for the provided outpoint.
	BalanceByScriptHash(ctx context.Context, sh ScriptHash) (uint64, error)
	ScriptHashByOutpoint(ctx context.Context, op Outpoint) (*ScriptHash, error)
	UtxosByScriptHash(ctx context.Context, sh ScriptHash, start uint64, count uint64) ([]Utxo, error)
}

type InsertType added in v0.2.0

type InsertType int
const (
	ITInvalid     InsertType = 0 // Invalid insert
	ITChainExtend InsertType = 1 // Normal insert, does not require further action.
	ITChainFork   InsertType = 2 // Chain forked, unwind and rewind indexes.
	ITForkExtend  InsertType = 3 // Extended a fork, does not require further action.
)

func (InsertType) String added in v0.2.0

func (it InsertType) String() string

type Outpoint

type Outpoint [1 + 32 + 4]byte

Outpoint is a bitcoin structure that points to a transaction in a block. It is expressed as an array of bytes in order to pack it as dense as possible for memory conservation reasons.

The bytes contained by Outpoint is 'u' + txid + index.

func NewOutpoint

func NewOutpoint(txid [32]byte, index uint32) (op Outpoint)

func (Outpoint) String

func (o Outpoint) String() string

String returns a reversed pretty printed outpoint.

func (Outpoint) TxId

func (o Outpoint) TxId() []byte

func (Outpoint) TxIndex

func (o Outpoint) TxIndex() uint32

func (Outpoint) TxIndexBytes

func (o Outpoint) TxIndexBytes() []byte

type Peer

type Peer struct {
	Host      string
	Port      string
	LastAt    database.Timestamp `deep:"-"` // Last time connected
	CreatedAt database.Timestamp `deep:"-"`
}

Peer

type ScriptHash

type ScriptHash [32]byte

ScriptHash is a bitcoin transaction id. The underlying slice is reversed, only when using the stringer does it apear in human readable format.

func NewScriptHash

func NewScriptHash(x [32]byte) (scriptHash ScriptHash)

func NewScriptHashFromBytes

func NewScriptHashFromBytes(x []byte) (scriptHash ScriptHash, err error)

func (ScriptHash) String

func (bh ScriptHash) String() string

type SpentInfo added in v0.2.8

type SpentInfo struct {
	BlockHash  BlockHash
	TxId       TxId
	InputIndex uint32
}

type TxId

type TxId [32]byte

TxId is a bitcoin transaction id. The underlying slice is reversed, only when using the stringer does it apear in human readable format.

func NewTxId

func NewTxId(x [32]byte) (txId TxId)

func NewTxIdFromBytes

func NewTxIdFromBytes(x []byte) (txId TxId, err error)

func (TxId) Hash added in v0.2.8

func (t TxId) Hash() *chainhash.Hash

func (TxId) String

func (t TxId) String() string

type TxKey

type TxKey [69]byte // Allocate max sized key, the prefix byte determines the lengths

Spent Transaction:

s + txin.PrevOutPoint.Hash + txin.PrevOutPoint.Index + blockhash = txid + txin_index | [1 + 32 + 4 + 32] = [32 + 4]

Transaction ID to Block mapping:

t + txid + blockhash = nil | [1 + 32 + 32] = nil

func NewTxMapping

func NewTxMapping(txId, blockHash *chainhash.Hash) (txKey TxKey)

NewTxMapping returns a TxKey and TxValue that maps a tx id to a block hash.

type TxValue

type TxValue [36]byte // allocate max sized value

Spent Transaction:

s + txin.PrevOutPoint.Hash + txin.PrevOutPoint.Index + blockhash = txid + txin_index | [1 + 32 + 4 + 32] = [32 + 4]

Transaction ID to Block mapping:

t + txid + blockhash = nil | [1 + 32 + 32] = nil

type Utxo

type Utxo [32 + 8 + 4]byte // tx_id + value + out_idx

Utxo packs a transaction id, the value and the out index.

func NewUtxo

func NewUtxo(hash [32]byte, value uint64, outIndex uint32) (u Utxo)

func (Utxo) Equal

func (u Utxo) Equal(x CacheOutput) bool

func (Utxo) OutputIndex

func (u Utxo) OutputIndex() uint32

func (Utxo) OutputIndexBytes

func (u Utxo) OutputIndexBytes() []byte

func (Utxo) ScriptHash

func (u Utxo) ScriptHash() (hash [32]byte)

func (Utxo) ScriptHashSlice

func (u Utxo) ScriptHashSlice() []byte

func (Utxo) String

func (u Utxo) String() string

String reutrns pretty printable CacheOutput. Hash is not reversed since it is an opaque pointer. It prints satoshis@script_hash:output_index

func (Utxo) Value

func (u Utxo) Value() uint64

func (Utxo) ValueBytes

func (u Utxo) ValueBytes() []byte

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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