2016-08-08 19:38:16 +02:00
|
|
|
// Copyright (c) 2013-2016 The btcsuite developers
|
2013-05-29 00:32:35 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package btcutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2014-06-24 05:01:21 +02:00
|
|
|
"io"
|
2014-07-03 02:29:48 +02:00
|
|
|
|
2016-08-08 19:38:16 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2015-02-05 21:48:38 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2013-05-29 00:32:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// OutOfRangeError describes an error due to accessing an element that is out
|
|
|
|
// of range.
|
|
|
|
type OutOfRangeError string
|
|
|
|
|
|
|
|
// BlockHeightUnknown is the value returned for a block height that is unknown.
|
|
|
|
// This is typically because the block has not been inserted into the main chain
|
|
|
|
// yet.
|
2015-08-08 05:28:15 +02:00
|
|
|
const BlockHeightUnknown = int32(-1)
|
2013-05-29 00:32:35 +02:00
|
|
|
|
|
|
|
// Error satisfies the error interface and prints human-readable errors.
|
|
|
|
func (e OutOfRangeError) Error() string {
|
|
|
|
return string(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block defines a bitcoin block that provides easier and more efficient
|
2013-08-05 19:41:31 +02:00
|
|
|
// manipulation of raw blocks. It also memoizes hashes for the block and its
|
|
|
|
// transactions on their first access so subsequent accesses don't have to
|
|
|
|
// repeat the relatively expensive hashing operations.
|
2013-05-29 00:32:35 +02:00
|
|
|
type Block struct {
|
2016-10-13 21:55:05 +02:00
|
|
|
msgBlock *wire.MsgBlock // Underlying MsgBlock
|
|
|
|
serializedBlock []byte // Serialized bytes for the block
|
|
|
|
serializedBlockNoWitness []byte // Serialized bytes for block w/o witness data
|
|
|
|
blockHash *chainhash.Hash // Cached block hash
|
|
|
|
blockHeight int32 // Height in the main block chain
|
|
|
|
transactions []*Tx // Transactions
|
|
|
|
txnsGenerated bool // ALL wrapped transactions generated
|
2013-05-29 00:32:35 +02:00
|
|
|
}
|
|
|
|
|
2015-02-05 21:48:38 +01:00
|
|
|
// MsgBlock returns the underlying wire.MsgBlock for the Block.
|
|
|
|
func (b *Block) MsgBlock() *wire.MsgBlock {
|
2013-05-29 00:32:35 +02:00
|
|
|
// Return the cached block.
|
|
|
|
return b.msgBlock
|
|
|
|
}
|
|
|
|
|
2013-08-05 19:41:31 +02:00
|
|
|
// Bytes returns the serialized bytes for the Block. This is equivalent to
|
2015-02-05 21:48:38 +01:00
|
|
|
// calling Serialize on the underlying wire.MsgBlock, however it caches the
|
2013-08-05 19:41:31 +02:00
|
|
|
// result so subsequent calls are more efficient.
|
|
|
|
func (b *Block) Bytes() ([]byte, error) {
|
|
|
|
// Return the cached serialized bytes if it has already been generated.
|
|
|
|
if len(b.serializedBlock) != 0 {
|
|
|
|
return b.serializedBlock, nil
|
2013-05-29 00:32:35 +02:00
|
|
|
}
|
|
|
|
|
2013-08-05 19:41:31 +02:00
|
|
|
// Serialize the MsgBlock.
|
2016-11-16 18:40:24 +01:00
|
|
|
w := bytes.NewBuffer(make([]byte, 0, b.msgBlock.SerializeSize()))
|
|
|
|
err := b.msgBlock.Serialize(w)
|
2013-05-29 00:32:35 +02:00
|
|
|
if err != nil {
|
2013-08-05 19:41:31 +02:00
|
|
|
return nil, err
|
2013-05-29 00:32:35 +02:00
|
|
|
}
|
2013-08-05 19:41:31 +02:00
|
|
|
serializedBlock := w.Bytes()
|
2013-05-29 00:32:35 +02:00
|
|
|
|
2013-08-05 19:41:31 +02:00
|
|
|
// Cache the serialized bytes and return them.
|
|
|
|
b.serializedBlock = serializedBlock
|
|
|
|
return serializedBlock, nil
|
2013-05-29 00:32:35 +02:00
|
|
|
}
|
|
|
|
|
2016-10-13 21:55:05 +02:00
|
|
|
// BytesNoWitness returns the serialized bytes for the block with transactions
|
|
|
|
// encoded without any witness data.
|
|
|
|
func (b *Block) BytesNoWitness() ([]byte, error) {
|
|
|
|
// Return the cached serialized bytes if it has already been generated.
|
|
|
|
if len(b.serializedBlockNoWitness) != 0 {
|
|
|
|
return b.serializedBlockNoWitness, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize the MsgBlock.
|
|
|
|
var w bytes.Buffer
|
|
|
|
err := b.msgBlock.SerializeNoWitness(&w)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
serializedBlock := w.Bytes()
|
|
|
|
|
|
|
|
// Cache the serialized bytes and return them.
|
|
|
|
b.serializedBlockNoWitness = serializedBlock
|
|
|
|
return serializedBlock, nil
|
|
|
|
}
|
|
|
|
|
2016-08-08 19:38:16 +02:00
|
|
|
// Hash returns the block identifier hash for the Block. This is equivalent to
|
|
|
|
// calling BlockHash on the underlying wire.MsgBlock, however it caches the
|
2013-05-29 00:32:35 +02:00
|
|
|
// result so subsequent calls are more efficient.
|
2016-08-08 19:38:16 +02:00
|
|
|
func (b *Block) Hash() *chainhash.Hash {
|
2013-05-29 00:32:35 +02:00
|
|
|
// Return the cached block hash if it has already been generated.
|
2016-08-08 19:38:16 +02:00
|
|
|
if b.blockHash != nil {
|
|
|
|
return b.blockHash
|
2013-05-29 00:32:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the block hash and return it.
|
2016-08-08 19:38:16 +02:00
|
|
|
hash := b.msgBlock.BlockHash()
|
|
|
|
b.blockHash = &hash
|
|
|
|
return &hash
|
2013-05-29 00:32:35 +02:00
|
|
|
}
|
|
|
|
|
2013-10-28 16:32:41 +01:00
|
|
|
// Tx returns a wrapped transaction (btcutil.Tx) for the transaction at the
|
|
|
|
// specified index in the Block. The supplied index is 0 based. That is to
|
|
|
|
// say, the first transaction in the block is txNum 0. This is nearly
|
2015-02-05 21:48:38 +01:00
|
|
|
// equivalent to accessing the raw transaction (wire.MsgTx) from the
|
|
|
|
// underlying wire.MsgBlock, however the wrapped transaction has some helpful
|
2013-10-28 16:32:41 +01:00
|
|
|
// properties such as caching the hash so subsequent calls are more efficient.
|
|
|
|
func (b *Block) Tx(txNum int) (*Tx, error) {
|
2013-05-29 00:32:35 +02:00
|
|
|
// Ensure the requested transaction is in range.
|
2014-01-19 03:59:07 +01:00
|
|
|
numTx := uint64(len(b.msgBlock.Transactions))
|
2013-05-29 00:32:35 +02:00
|
|
|
if txNum < 0 || uint64(txNum) > numTx {
|
|
|
|
str := fmt.Sprintf("transaction index %d is out of range - max %d",
|
|
|
|
txNum, numTx-1)
|
|
|
|
return nil, OutOfRangeError(str)
|
|
|
|
}
|
|
|
|
|
2013-10-28 16:32:41 +01:00
|
|
|
// Generate slice to hold all of the wrapped transactions if needed.
|
|
|
|
if len(b.transactions) == 0 {
|
|
|
|
b.transactions = make([]*Tx, numTx)
|
2013-05-29 00:32:35 +02:00
|
|
|
}
|
|
|
|
|
2013-10-28 16:32:41 +01:00
|
|
|
// Return the wrapped transaction if it has already been generated.
|
|
|
|
if b.transactions[txNum] != nil {
|
|
|
|
return b.transactions[txNum], nil
|
2013-05-29 00:32:35 +02:00
|
|
|
}
|
|
|
|
|
2013-10-28 16:32:41 +01:00
|
|
|
// Generate and cache the wrapped transaction and return it.
|
|
|
|
newTx := NewTx(b.msgBlock.Transactions[txNum])
|
|
|
|
newTx.SetIndex(txNum)
|
|
|
|
b.transactions[txNum] = newTx
|
|
|
|
return newTx, nil
|
|
|
|
}
|
2013-05-29 00:32:35 +02:00
|
|
|
|
2013-10-28 16:32:41 +01:00
|
|
|
// Transactions returns a slice of wrapped transactions (btcutil.Tx) for all
|
|
|
|
// transactions in the Block. This is nearly equivalent to accessing the raw
|
2015-02-05 21:48:38 +01:00
|
|
|
// transactions (wire.MsgTx) in the underlying wire.MsgBlock, however it
|
2013-10-28 16:32:41 +01:00
|
|
|
// instead provides easy access to wrapped versions (btcutil.Tx) of them.
|
|
|
|
func (b *Block) Transactions() []*Tx {
|
|
|
|
// Return transactions if they have ALL already been generated. This
|
|
|
|
// flag is necessary because the wrapped transactions are lazily
|
|
|
|
// generated in a sparse fashion.
|
|
|
|
if b.txnsGenerated {
|
|
|
|
return b.transactions
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate slice to hold all of the wrapped transactions if needed.
|
|
|
|
if len(b.transactions) == 0 {
|
2014-01-19 03:59:07 +01:00
|
|
|
b.transactions = make([]*Tx, len(b.msgBlock.Transactions))
|
2013-10-28 16:32:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate and cache the wrapped transactions for all that haven't
|
|
|
|
// already been done.
|
|
|
|
for i, tx := range b.transactions {
|
|
|
|
if tx == nil {
|
|
|
|
newTx := NewTx(b.msgBlock.Transactions[i])
|
|
|
|
newTx.SetIndex(i)
|
|
|
|
b.transactions[i] = newTx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.txnsGenerated = true
|
|
|
|
return b.transactions
|
|
|
|
}
|
|
|
|
|
2016-08-08 19:38:16 +02:00
|
|
|
// TxHash returns the hash for the requested transaction number in the Block.
|
2013-10-28 16:32:41 +01:00
|
|
|
// The supplied index is 0 based. That is to say, the first transaction in the
|
2016-08-08 19:38:16 +02:00
|
|
|
// block is txNum 0. This is equivalent to calling TxHash on the underlying
|
2015-02-05 21:48:38 +01:00
|
|
|
// wire.MsgTx, however it caches the result so subsequent calls are more
|
2013-10-28 16:32:41 +01:00
|
|
|
// efficient.
|
2016-08-08 19:38:16 +02:00
|
|
|
func (b *Block) TxHash(txNum int) (*chainhash.Hash, error) {
|
2013-10-28 16:32:41 +01:00
|
|
|
// Attempt to get a wrapped transaction for the specified index. It
|
|
|
|
// will be created lazily if needed or simply return the cached version
|
|
|
|
// if it has already been generated.
|
|
|
|
tx, err := b.Tx(txNum)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defer to the wrapped transaction which will return the cached hash if
|
|
|
|
// it has already been generated.
|
2016-08-08 19:38:16 +02:00
|
|
|
return tx.Hash(), nil
|
2013-05-29 00:32:35 +02:00
|
|
|
}
|
|
|
|
|
2013-05-30 18:39:43 +02:00
|
|
|
// TxLoc returns the offsets and lengths of each transaction in a raw block.
|
2013-05-29 01:43:13 +02:00
|
|
|
// It is used to allow fast indexing into transactions within the raw byte
|
|
|
|
// stream.
|
2015-02-05 21:48:38 +01:00
|
|
|
func (b *Block) TxLoc() ([]wire.TxLoc, error) {
|
2013-08-05 19:41:31 +02:00
|
|
|
rawMsg, err := b.Bytes()
|
2013-05-29 00:32:35 +02:00
|
|
|
if err != nil {
|
2013-05-30 18:31:22 +02:00
|
|
|
return nil, err
|
2013-05-29 00:32:35 +02:00
|
|
|
}
|
|
|
|
rbuf := bytes.NewBuffer(rawMsg)
|
|
|
|
|
2015-02-05 21:48:38 +01:00
|
|
|
var mblock wire.MsgBlock
|
2013-08-05 19:41:31 +02:00
|
|
|
txLocs, err := mblock.DeserializeTxLoc(rbuf)
|
2013-05-29 00:32:35 +02:00
|
|
|
if err != nil {
|
2013-05-30 18:31:22 +02:00
|
|
|
return nil, err
|
2013-05-29 00:32:35 +02:00
|
|
|
}
|
2013-05-30 18:13:08 +02:00
|
|
|
return txLocs, err
|
2013-05-29 00:32:35 +02:00
|
|
|
}
|
|
|
|
|
2013-08-05 19:41:31 +02:00
|
|
|
// Height returns the saved height of the block in the block chain. This value
|
2013-05-29 00:32:35 +02:00
|
|
|
// will be BlockHeightUnknown if it hasn't already explicitly been set.
|
2015-08-08 05:28:15 +02:00
|
|
|
func (b *Block) Height() int32 {
|
2013-05-29 00:32:35 +02:00
|
|
|
return b.blockHeight
|
|
|
|
}
|
|
|
|
|
2013-08-05 19:41:31 +02:00
|
|
|
// SetHeight sets the height of the block in the block chain.
|
2015-08-08 05:28:15 +02:00
|
|
|
func (b *Block) SetHeight(height int32) {
|
2013-05-29 00:32:35 +02:00
|
|
|
b.blockHeight = height
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBlock returns a new instance of a bitcoin block given an underlying
|
2015-02-05 21:48:38 +01:00
|
|
|
// wire.MsgBlock. See Block.
|
|
|
|
func NewBlock(msgBlock *wire.MsgBlock) *Block {
|
2013-05-29 00:32:35 +02:00
|
|
|
return &Block{
|
2013-08-05 19:41:31 +02:00
|
|
|
msgBlock: msgBlock,
|
|
|
|
blockHeight: BlockHeightUnknown,
|
2013-05-29 00:32:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBlockFromBytes returns a new instance of a bitcoin block given the
|
2013-08-05 19:41:31 +02:00
|
|
|
// serialized bytes. See Block.
|
|
|
|
func NewBlockFromBytes(serializedBlock []byte) (*Block, error) {
|
2014-06-24 05:01:21 +02:00
|
|
|
br := bytes.NewReader(serializedBlock)
|
|
|
|
b, err := NewBlockFromReader(br)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b.serializedBlock = serializedBlock
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBlockFromReader returns a new instance of a bitcoin block given a
|
|
|
|
// Reader to deserialize the block. See Block.
|
|
|
|
func NewBlockFromReader(r io.Reader) (*Block, error) {
|
2013-08-05 19:41:31 +02:00
|
|
|
// Deserialize the bytes into a MsgBlock.
|
2015-02-05 21:48:38 +01:00
|
|
|
var msgBlock wire.MsgBlock
|
2014-06-24 05:01:21 +02:00
|
|
|
err := msgBlock.Deserialize(r)
|
2013-05-29 00:32:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
b := Block{
|
2014-06-24 05:01:21 +02:00
|
|
|
msgBlock: &msgBlock,
|
|
|
|
blockHeight: BlockHeightUnknown,
|
2013-05-29 00:32:35 +02:00
|
|
|
}
|
|
|
|
return &b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBlockFromBlockAndBytes returns a new instance of a bitcoin block given
|
2015-02-05 21:48:38 +01:00
|
|
|
// an underlying wire.MsgBlock and the serialized bytes for it. See Block.
|
|
|
|
func NewBlockFromBlockAndBytes(msgBlock *wire.MsgBlock, serializedBlock []byte) *Block {
|
2013-05-29 00:32:35 +02:00
|
|
|
return &Block{
|
|
|
|
msgBlock: msgBlock,
|
2013-08-05 19:41:31 +02:00
|
|
|
serializedBlock: serializedBlock,
|
2013-05-29 00:32:35 +02:00
|
|
|
blockHeight: BlockHeightUnknown,
|
|
|
|
}
|
|
|
|
}
|