Remove a few dead error checks.
The functions for generating transaction and block hashes contained a few error checks for conditions which could never fail without run-time panics. This commit removes those superfluous checks and adds explanatory comments.
This commit is contained in:
parent
1ca8015ea0
commit
1bab947596
4 changed files with 73 additions and 65 deletions
|
@ -50,19 +50,22 @@ type BlockHeader struct {
|
|||
const blockHashLen = 80
|
||||
|
||||
// BlockSha computes the block identifier hash for the given block header.
|
||||
func (h *BlockHeader) BlockSha(pver uint32) (sha ShaHash, err error) {
|
||||
func (h *BlockHeader) BlockSha(pver uint32) (ShaHash, error) {
|
||||
// Encode the header and run double sha256 everything prior to the
|
||||
// number of transactions. Ignore the error returns since there is no
|
||||
// way the encode could fail except being out of memory which would
|
||||
// cause a run-time panic. Also, SetBytes can't fail here due to the
|
||||
// fact DoubleSha256 always returns a []byte of the right size
|
||||
// regardless of input.
|
||||
var buf bytes.Buffer
|
||||
err = writeBlockHeader(&buf, pver, h)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var sha ShaHash
|
||||
_ = writeBlockHeader(&buf, pver, h)
|
||||
_ = sha.SetBytes(DoubleSha256(buf.Bytes()[0:blockHashLen]))
|
||||
|
||||
err = sha.SetBytes(DoubleSha256(buf.Bytes()[0:blockHashLen]))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
// Even though this function can't currently fail, it still returns
|
||||
// a potential error to help future proof the API should a failure
|
||||
// become possible.
|
||||
return sha, nil
|
||||
}
|
||||
|
||||
// NewBlockHeader returns a new BlockHeader using the provided previous block
|
||||
|
|
11
msgblock.go
11
msgblock.go
|
@ -73,8 +73,8 @@ func (msg *MsgBlock) BtcDecode(r io.Reader, pver uint32) error {
|
|||
}
|
||||
|
||||
// BtcDecodeTxLoc decodes r using the bitcoin protocol encoding into the
|
||||
// receiver and returns a slice containing the start and length each transaction
|
||||
// within the raw data.
|
||||
// receiver and returns a slice containing the start and length of each
|
||||
// transaction within the raw data.
|
||||
func (msg *MsgBlock) BtcDecodeTxLoc(r *bytes.Buffer, pver uint32) ([]TxLoc, error) {
|
||||
var fullLen int
|
||||
fullLen = r.Len()
|
||||
|
@ -144,10 +144,9 @@ func (msg *MsgBlock) BlockSha(pver uint32) (ShaHash, error) {
|
|||
func (msg *MsgBlock) TxShas(pver uint32) ([]ShaHash, error) {
|
||||
var shaList []ShaHash
|
||||
for _, tx := range msg.Transactions {
|
||||
sha, err := tx.TxSha(pver)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Ignore error here since TxSha can't fail in the current
|
||||
// implementation except due to run-time panics.
|
||||
sha, _ := tx.TxSha(pver)
|
||||
shaList = append(shaList, sha)
|
||||
}
|
||||
return shaList, nil
|
||||
|
|
22
msgtx.go
22
msgtx.go
|
@ -87,15 +87,21 @@ func (msg *MsgTx) AddTxOut(to *TxOut) {
|
|||
|
||||
// TxSha generates the ShaHash name for the transaction.
|
||||
func (tx *MsgTx) TxSha(pver uint32) (ShaHash, error) {
|
||||
var txsha ShaHash
|
||||
var wbuf bytes.Buffer
|
||||
err := tx.BtcEncode(&wbuf, pver)
|
||||
if err != nil {
|
||||
return txsha, err
|
||||
}
|
||||
txsha.SetBytes(DoubleSha256(wbuf.Bytes()))
|
||||
// Encode the transaction and calculate double sha256 on the result.
|
||||
// Ignore the error returns since the only way the encode could fail
|
||||
// is being out of memory or due to nil pointers, both of which would
|
||||
// cause a run-time panic. Also, SetBytes can't fail here due to the
|
||||
// fact DoubleSha256 always returns a []byte of the right size
|
||||
// regardless of input.
|
||||
var buf bytes.Buffer
|
||||
var sha ShaHash
|
||||
_ = tx.BtcEncode(&buf, pver)
|
||||
_ = sha.SetBytes(DoubleSha256(buf.Bytes()))
|
||||
|
||||
return txsha, nil
|
||||
// Even though this function can't currently fail, it still returns
|
||||
// a potential error to help future proof the API should a failure
|
||||
// become possible.
|
||||
return sha, nil
|
||||
}
|
||||
|
||||
// Copy creates a deep copy of a transaction so that the original does not get
|
||||
|
|
|
@ -3,84 +3,87 @@ github.com/conformal/btcwire/message.go ReadMessage 100.00% (37/37)
|
|||
github.com/conformal/btcwire/message.go WriteMessage 100.00% (27/27)
|
||||
github.com/conformal/btcwire/msgtx.go MsgTx.Copy 100.00% (24/24)
|
||||
github.com/conformal/btcwire/common.go readVarInt 100.00% (24/24)
|
||||
github.com/conformal/btcwire/netaddress.go readNetAddress 100.00% (20/20)
|
||||
github.com/conformal/btcwire/message.go makeEmptyMessage 100.00% (20/20)
|
||||
github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.BtcDecode 100.00% (19/19)
|
||||
github.com/conformal/btcwire/netaddress.go readNetAddress 100.00% (20/20)
|
||||
github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.BtcDecode 100.00% (19/19)
|
||||
github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.BtcDecode 100.00% (19/19)
|
||||
github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.BtcEncode 100.00% (18/18)
|
||||
github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.BtcEncode 100.00% (18/18)
|
||||
github.com/conformal/btcwire/msgheaders.go MsgHeaders.BtcDecode 100.00% (16/16)
|
||||
github.com/conformal/btcwire/common.go writeVarInt 100.00% (16/16)
|
||||
github.com/conformal/btcwire/msgblock.go MsgBlock.BtcDecodeTxLoc 100.00% (16/16)
|
||||
github.com/conformal/btcwire/common.go writeVarInt 100.00% (16/16)
|
||||
github.com/conformal/btcwire/msgheaders.go MsgHeaders.BtcDecode 100.00% (16/16)
|
||||
github.com/conformal/btcwire/msgaddr.go MsgAddr.BtcEncode 100.00% (15/15)
|
||||
github.com/conformal/btcwire/msgheaders.go MsgHeaders.BtcEncode 100.00% (15/15)
|
||||
github.com/conformal/btcwire/shahash.go NewShaHashFromStr 100.00% (15/15)
|
||||
github.com/conformal/btcwire/netaddress.go writeNetAddress 100.00% (14/14)
|
||||
github.com/conformal/btcwire/msgaddr.go MsgAddr.BtcDecode 100.00% (13/13)
|
||||
github.com/conformal/btcwire/msginv.go MsgInv.BtcDecode 100.00% (13/13)
|
||||
github.com/conformal/btcwire/msgnotfound.go MsgNotFound.BtcDecode 100.00% (13/13)
|
||||
github.com/conformal/btcwire/msggetdata.go MsgGetData.BtcDecode 100.00% (13/13)
|
||||
github.com/conformal/btcwire/msggetdata.go MsgGetData.BtcEncode 100.00% (12/12)
|
||||
github.com/conformal/btcwire/protocol.go ServiceFlag.String 100.00% (12/12)
|
||||
github.com/conformal/btcwire/msgnotfound.go MsgNotFound.BtcDecode 100.00% (13/13)
|
||||
github.com/conformal/btcwire/msgaddr.go MsgAddr.BtcDecode 100.00% (13/13)
|
||||
github.com/conformal/btcwire/msginv.go MsgInv.BtcEncode 100.00% (12/12)
|
||||
github.com/conformal/btcwire/msggetdata.go MsgGetData.BtcEncode 100.00% (12/12)
|
||||
github.com/conformal/btcwire/msgnotfound.go MsgNotFound.BtcEncode 100.00% (12/12)
|
||||
github.com/conformal/btcwire/blockheader.go readBlockHeader 100.00% (10/10)
|
||||
github.com/conformal/btcwire/protocol.go ServiceFlag.String 100.00% (12/12)
|
||||
github.com/conformal/btcwire/msgblock.go MsgBlock.BtcDecode 100.00% (10/10)
|
||||
github.com/conformal/btcwire/blockheader.go readBlockHeader 100.00% (10/10)
|
||||
github.com/conformal/btcwire/message.go discardInput 100.00% (10/10)
|
||||
github.com/conformal/btcwire/msgblock.go MsgBlock.BtcEncode 100.00% (9/9)
|
||||
github.com/conformal/btcwire/blockheader.go writeBlockHeader 100.00% (8/8)
|
||||
github.com/conformal/btcwire/common.go readVarString 100.00% (8/8)
|
||||
github.com/conformal/btcwire/msgalert.go MsgAlert.BtcEncode 100.00% (8/8)
|
||||
github.com/conformal/btcwire/blockheader.go writeBlockHeader 100.00% (8/8)
|
||||
github.com/conformal/btcwire/msgalert.go MsgAlert.BtcDecode 100.00% (8/8)
|
||||
github.com/conformal/btcwire/common.go readVarString 100.00% (8/8)
|
||||
github.com/conformal/btcwire/msgpong.go MsgPong.BtcEncode 100.00% (7/7)
|
||||
github.com/conformal/btcwire/common.go randomUint64 100.00% (7/7)
|
||||
github.com/conformal/btcwire/common.go writeVarString 100.00% (7/7)
|
||||
github.com/conformal/btcwire/msgversion.go NewMsgVersionFromConn 100.00% (7/7)
|
||||
github.com/conformal/btcwire/msgpong.go MsgPong.BtcDecode 100.00% (7/7)
|
||||
github.com/conformal/btcwire/message.go readMessageHeader 100.00% (7/7)
|
||||
github.com/conformal/btcwire/common.go DoubleSha256 100.00% (7/7)
|
||||
github.com/conformal/btcwire/msgpong.go MsgPong.BtcDecode 100.00% (7/7)
|
||||
github.com/conformal/btcwire/msgpong.go MsgPong.BtcEncode 100.00% (7/7)
|
||||
github.com/conformal/btcwire/msgversion.go NewMsgVersionFromConn 100.00% (7/7)
|
||||
github.com/conformal/btcwire/common.go writeVarString 100.00% (7/7)
|
||||
github.com/conformal/btcwire/shahash.go NewShaHash 100.00% (5/5)
|
||||
github.com/conformal/btcwire/common.go writeElements 100.00% (5/5)
|
||||
github.com/conformal/btcwire/msginv.go MsgInv.AddInvVect 100.00% (5/5)
|
||||
github.com/conformal/btcwire/msgnotfound.go MsgNotFound.AddInvVect 100.00% (5/5)
|
||||
github.com/conformal/btcwire/msgping.go MsgPing.BtcDecode 100.00% (5/5)
|
||||
github.com/conformal/btcwire/msgping.go MsgPing.BtcEncode 100.00% (5/5)
|
||||
github.com/conformal/btcwire/msgheaders.go MsgHeaders.AddBlockHeader 100.00% (5/5)
|
||||
github.com/conformal/btcwire/shahash.go ShaHash.SetBytes 100.00% (5/5)
|
||||
github.com/conformal/btcwire/common.go writeElements 100.00% (5/5)
|
||||
github.com/conformal/btcwire/msgblock.go MsgBlock.TxShas 100.00% (5/5)
|
||||
github.com/conformal/btcwire/common.go readElements 100.00% (5/5)
|
||||
github.com/conformal/btcwire/shahash.go NewShaHash 100.00% (5/5)
|
||||
github.com/conformal/btcwire/msgtx.go MsgTx.TxSha 100.00% (5/5)
|
||||
github.com/conformal/btcwire/shahash.go ShaHash.SetBytes 100.00% (5/5)
|
||||
github.com/conformal/btcwire/blockheader.go BlockHeader.BlockSha 100.00% (5/5)
|
||||
github.com/conformal/btcwire/netaddress.go NewNetAddress 100.00% (5/5)
|
||||
github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.AddBlockLocatorHash 100.00% (5/5)
|
||||
github.com/conformal/btcwire/msggetdata.go MsgGetData.AddInvVect 100.00% (5/5)
|
||||
github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddress 100.00% (5/5)
|
||||
github.com/conformal/btcwire/msggetdata.go MsgGetData.AddInvVect 100.00% (5/5)
|
||||
github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.AddBlockLocatorHash 100.00% (5/5)
|
||||
github.com/conformal/btcwire/msginv.go MsgInv.AddInvVect 100.00% (5/5)
|
||||
github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddresses 100.00% (5/5)
|
||||
github.com/conformal/btcwire/msgmempool.go MsgMemPool.BtcEncode 100.00% (4/4)
|
||||
github.com/conformal/btcwire/invvect.go readInvVect 100.00% (4/4)
|
||||
github.com/conformal/btcwire/msgping.go MsgPing.MaxPayloadLength 100.00% (4/4)
|
||||
github.com/conformal/btcwire/invvect.go writeInvVect 100.00% (4/4)
|
||||
github.com/conformal/btcwire/msgpong.go MsgPong.MaxPayloadLength 100.00% (4/4)
|
||||
github.com/conformal/btcwire/netaddress.go maxNetAddressPayload 100.00% (4/4)
|
||||
github.com/conformal/btcwire/msgmempool.go MsgMemPool.BtcDecode 100.00% (4/4)
|
||||
github.com/conformal/btcwire/msgheaders.go MsgHeaders.AddBlockHeader 100.00% (5/5)
|
||||
github.com/conformal/btcwire/shahash.go ShaHash.String 100.00% (4/4)
|
||||
github.com/conformal/btcwire/error.go MessageError.Error 100.00% (3/3)
|
||||
github.com/conformal/btcwire/netaddress.go NetAddress.HasService 100.00% (3/3)
|
||||
github.com/conformal/btcwire/invvect.go InvType.String 100.00% (3/3)
|
||||
github.com/conformal/btcwire/msgversion.go MsgVersion.HasService 100.00% (3/3)
|
||||
github.com/conformal/btcwire/msgaddr.go MsgAddr.MaxPayloadLength 100.00% (3/3)
|
||||
github.com/conformal/btcwire/shahash.go ShaHash.Bytes 100.00% (3/3)
|
||||
github.com/conformal/btcwire/invvect.go writeInvVect 100.00% (4/4)
|
||||
github.com/conformal/btcwire/netaddress.go maxNetAddressPayload 100.00% (4/4)
|
||||
github.com/conformal/btcwire/invvect.go readInvVect 100.00% (4/4)
|
||||
github.com/conformal/btcwire/msgmempool.go MsgMemPool.BtcDecode 100.00% (4/4)
|
||||
github.com/conformal/btcwire/msgping.go MsgPing.MaxPayloadLength 100.00% (4/4)
|
||||
github.com/conformal/btcwire/msgpong.go MsgPong.MaxPayloadLength 100.00% (4/4)
|
||||
github.com/conformal/btcwire/msgmempool.go MsgMemPool.BtcEncode 100.00% (4/4)
|
||||
github.com/conformal/btcwire/msgblock.go MsgBlock.AddTransaction 100.00% (3/3)
|
||||
github.com/conformal/btcwire/invvect.go InvType.String 100.00% (3/3)
|
||||
github.com/conformal/btcwire/error.go MessageError.Error 100.00% (3/3)
|
||||
github.com/conformal/btcwire/shahash.go ShaHash.Bytes 100.00% (3/3)
|
||||
github.com/conformal/btcwire/msgaddr.go MsgAddr.MaxPayloadLength 100.00% (3/3)
|
||||
github.com/conformal/btcwire/netaddress.go NetAddress.HasService 100.00% (3/3)
|
||||
github.com/conformal/btcwire/msgversion.go MsgVersion.HasService 100.00% (3/3)
|
||||
github.com/conformal/btcwire/netaddress.go NetAddress.SetAddress 100.00% (2/2)
|
||||
github.com/conformal/btcwire/msgblock.go MsgBlock.ClearTransactions 100.00% (2/2)
|
||||
github.com/conformal/btcwire/msgalert.go MsgAlert.Command 100.00% (1/1)
|
||||
github.com/conformal/btcwire/invvect.go NewInvVect 100.00% (1/1)
|
||||
github.com/conformal/btcwire/blockheader.go NewBlockHeader 100.00% (1/1)
|
||||
github.com/conformal/btcwire/common.go readElement 100.00% (1/1)
|
||||
github.com/conformal/btcwire/common.go writeElement 100.00% (1/1)
|
||||
github.com/conformal/btcwire/common.go RandomUint64 100.00% (1/1)
|
||||
github.com/conformal/btcwire/error.go messageError 100.00% (1/1)
|
||||
github.com/conformal/btcwire/invvect.go NewInvVect 100.00% (1/1)
|
||||
github.com/conformal/btcwire/msgaddr.go MsgAddr.ClearAddresses 100.00% (1/1)
|
||||
github.com/conformal/btcwire/msgaddr.go MsgAddr.Command 100.00% (1/1)
|
||||
github.com/conformal/btcwire/msgaddr.go NewMsgAddr 100.00% (1/1)
|
||||
github.com/conformal/btcwire/msgalert.go MsgAlert.Command 100.00% (1/1)
|
||||
github.com/conformal/btcwire/msgalert.go MsgAlert.MaxPayloadLength 100.00% (1/1)
|
||||
github.com/conformal/btcwire/msgalert.go NewMsgAlert 100.00% (1/1)
|
||||
github.com/conformal/btcwire/msgblock.go MsgBlock.Command 100.00% (1/1)
|
||||
|
@ -136,18 +139,15 @@ github.com/conformal/btcwire/msgversion.go MsgVersion.MaxPayloadLength 100.00
|
|||
github.com/conformal/btcwire/msgversion.go NewMsgVersion 100.00% (1/1)
|
||||
github.com/conformal/btcwire/netaddress.go NetAddress.AddService 100.00% (1/1)
|
||||
github.com/conformal/btcwire/shahash.go ShaHash.IsEqual 100.00% (1/1)
|
||||
github.com/conformal/btcwire/msgblock.go MsgBlock.TxShas 85.71% (6/7)
|
||||
github.com/conformal/btcwire/msgtx.go MsgTx.TxSha 85.71% (6/7)
|
||||
github.com/conformal/btcwire/msgtx.go MsgTx.BtcDecode 80.00% (20/25)
|
||||
github.com/conformal/btcwire/msgtx.go MsgTx.BtcEncode 78.26% (18/23)
|
||||
github.com/conformal/btcwire/msgtx.go readTxIn 76.47% (13/17)
|
||||
github.com/conformal/btcwire/msgtx.go readTxOut 75.00% (9/12)
|
||||
github.com/conformal/btcwire/blockheader.go BlockHeader.BlockSha 75.00% (6/8)
|
||||
github.com/conformal/btcwire/msgtx.go writeOutPoint 75.00% (3/4)
|
||||
github.com/conformal/btcwire/msgtx.go readOutPoint 75.00% (3/4)
|
||||
github.com/conformal/btcwire/msgtx.go writeTxIn 73.33% (11/15)
|
||||
github.com/conformal/btcwire/msgtx.go writeTxOut 72.73% (8/11)
|
||||
github.com/conformal/btcwire/msgversion.go MsgVersion.BtcDecode 68.00% (17/25)
|
||||
github.com/conformal/btcwire/msgversion.go MsgVersion.BtcEncode 63.64% (14/22)
|
||||
github.com/conformal/btcwire --------------------------------- 95.05% (884/930)
|
||||
github.com/conformal/btcwire --------------------------------- 95.45% (881/923)
|
||||
|
||||
|
|
Loading…
Reference in a new issue