Remove protocol version param from BlockSha/Txsha.

Both of these depend on the serialized bytes which are dependent on the
version field in the block/transaction.  They must be independent of the
protocol version so there is no need to require it.
This commit is contained in:
Dave Collins 2013-08-05 16:24:50 -05:00
parent dea1bac359
commit d90740728e
6 changed files with 19 additions and 38 deletions

View file

@ -50,7 +50,7 @@ type BlockHeader struct {
const blockHashLen = 80
// BlockSha computes the block identifier hash for the given block header.
func (h *BlockHeader) BlockSha(pver uint32) (ShaHash, error) {
func (h *BlockHeader) BlockSha() (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
@ -59,7 +59,7 @@ func (h *BlockHeader) BlockSha(pver uint32) (ShaHash, error) {
// regardless of input.
var buf bytes.Buffer
var sha ShaHash
_ = writeBlockHeader(&buf, pver, h)
_ = writeBlockHeader(&buf, 0, h)
_ = sha.SetBytes(DoubleSha256(buf.Bytes()[0:blockHashLen]))
// Even though this function can't currently fail, it still returns

View file

@ -14,11 +14,9 @@ import (
// TestGenesisBlock tests the genesis block of the main network for validity by
// checking the encoded bytes and hashes.
func TestGenesisBlock(t *testing.T) {
pver := uint32(60002)
// Encode the genesis block to raw bytes.
var buf bytes.Buffer
err := btcwire.GenesisBlock.BtcEncode(&buf, pver)
err := btcwire.GenesisBlock.Serialize(&buf)
if err != nil {
t.Errorf("TestGenesisBlock: %v", err)
return
@ -33,7 +31,7 @@ func TestGenesisBlock(t *testing.T) {
}
// Check hash of the block against expected hash.
hash, err := btcwire.GenesisBlock.Header.BlockSha(pver)
hash, err := btcwire.GenesisBlock.BlockSha()
if err != nil {
t.Errorf("BlockSha: %v", err)
}
@ -48,11 +46,9 @@ func TestGenesisBlock(t *testing.T) {
// TestTestNetGenesisBlock tests the genesis block of the regression test
// network for validity by checking the encoded bytes and hashes.
func TestTestNetGenesisBlock(t *testing.T) {
pver := uint32(60002)
// Encode the genesis block to raw bytes.
var buf bytes.Buffer
err := btcwire.TestNetGenesisBlock.BtcEncode(&buf, pver)
err := btcwire.TestNetGenesisBlock.Serialize(&buf)
if err != nil {
t.Errorf("TestTestNetGenesisBlock: %v", err)
return
@ -68,7 +64,7 @@ func TestTestNetGenesisBlock(t *testing.T) {
}
// Check hash of the block against expected hash.
hash, err := btcwire.TestNetGenesisBlock.Header.BlockSha(pver)
hash, err := btcwire.TestNetGenesisBlock.BlockSha()
if err != nil {
t.Errorf("BlockSha: %v", err)
}
@ -83,11 +79,9 @@ func TestTestNetGenesisBlock(t *testing.T) {
// TestTestNet3GenesisBlock tests the genesis block of the test network (version
// 3) for validity by checking the encoded bytes and hashes.
func TestTestNet3GenesisBlock(t *testing.T) {
pver := uint32(60002)
// Encode the genesis block to raw bytes.
var buf bytes.Buffer
err := btcwire.TestNet3GenesisBlock.BtcEncode(&buf, pver)
err := btcwire.TestNet3GenesisBlock.Serialize(&buf)
if err != nil {
t.Errorf("TestTestNet3GenesisBlock: %v", err)
return
@ -103,7 +97,7 @@ func TestTestNet3GenesisBlock(t *testing.T) {
}
// Check hash of the block against expected hash.
hash, err := btcwire.TestNet3GenesisBlock.Header.BlockSha(pver)
hash, err := btcwire.TestNet3GenesisBlock.BlockSha()
if err != nil {
t.Errorf("BlockSha: %v", err)
}

View file

@ -206,17 +206,17 @@ func (msg *MsgBlock) MaxPayloadLength(pver uint32) uint32 {
}
// BlockSha computes the block identifier hash for this block.
func (msg *MsgBlock) BlockSha(pver uint32) (ShaHash, error) {
return msg.Header.BlockSha(pver)
func (msg *MsgBlock) BlockSha() (ShaHash, error) {
return msg.Header.BlockSha()
}
// TxShas returns a slice of hashes of all of transactions in this block.
func (msg *MsgBlock) TxShas(pver uint32) ([]ShaHash, error) {
func (msg *MsgBlock) TxShas() ([]ShaHash, error) {
var shaList []ShaHash
for _, tx := range msg.Transactions {
// Ignore error here since TxSha can't fail in the current
// implementation except due to run-time panics.
sha, _ := tx.TxSha(pver)
sha, _ := tx.TxSha()
shaList = append(shaList, sha)
}
return shaList, nil

View file

@ -71,11 +71,6 @@ func TestBlock(t *testing.T) {
// TestBlockTxShas tests the ability to generate a slice of all transaction
// hashes from a block accurately.
func TestBlockTxShas(t *testing.T) {
// Use protocol version 60002 specifically here instead of the latest
// because the test data is using bytes encoded with that protocol
// version.
pver := uint32(60002)
// Block 1, transaction 1 hash.
hashStr := "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
wantHash, err := btcwire.NewShaHashFromStr(hashStr)
@ -85,7 +80,7 @@ func TestBlockTxShas(t *testing.T) {
}
wantShas := []btcwire.ShaHash{*wantHash}
shas, err := blockOne.TxShas(pver)
shas, err := blockOne.TxShas()
if err != nil {
t.Errorf("TxShas: %v", err)
}
@ -97,11 +92,6 @@ func TestBlockTxShas(t *testing.T) {
// TestBlockSha tests the ability to generate the hash of a block accurately.
func TestBlockSha(t *testing.T) {
// Use protocol version 60002 specifically here instead of the latest
// because the test data is using bytes encoded with that protocol
// version.
pver := uint32(60002)
// Block 1 hash.
hashStr := "839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048"
wantHash, err := btcwire.NewShaHashFromStr(hashStr)
@ -110,7 +100,7 @@ func TestBlockSha(t *testing.T) {
}
// Ensure the hash produced is expected.
blockHash, err := blockOne.BlockSha(pver)
blockHash, err := blockOne.BlockSha()
if err != nil {
t.Errorf("BlockSha: %v", err)
}
@ -477,7 +467,7 @@ var blockOne = btcwire.MsgBlock{
},
}
// Block one bytes encoded with protocol version 60002.
// Block one serialized bytes.
var blockOneBytes = []byte{
0x01, 0x00, 0x00, 0x00, // Version 1
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
@ -519,8 +509,7 @@ var blockOneBytes = []byte{
0x00, 0x00, 0x00, 0x00, // Lock time
}
// Transaction location information for block one trasnactions as encoded with
// protocol version 60002.
// Transaction location information for block one transactions.
var blockOneTxLocs = []btcwire.TxLoc{
btcwire.TxLoc{TxStart: 81, TxLen: 134},
}

View file

@ -89,7 +89,7 @@ func (msg *MsgTx) AddTxOut(to *TxOut) {
}
// TxSha generates the ShaHash name for the transaction.
func (tx *MsgTx) TxSha(pver uint32) (ShaHash, error) {
func (tx *MsgTx) TxSha() (ShaHash, error) {
// 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
@ -98,7 +98,7 @@ func (tx *MsgTx) TxSha(pver uint32) (ShaHash, error) {
// regardless of input.
var buf bytes.Buffer
var sha ShaHash
_ = tx.BtcEncode(&buf, pver)
_ = tx.Serialize(&buf)
_ = sha.SetBytes(DoubleSha256(buf.Bytes()))
// Even though this function can't currently fail, it still returns

View file

@ -121,8 +121,6 @@ func TestTx(t *testing.T) {
// TestTxSha tests the ability to generate the hash of a transaction accurately.
func TestTxSha(t *testing.T) {
pver := btcwire.ProtocolVersion
// Hash of first transaction from block 113875.
hashStr := "f051e59b5e2503ac626d03aaeac8ab7be2d72ba4b7e97119c5852d70d52dcb86"
wantHash, err := btcwire.NewShaHashFromStr(hashStr)
@ -162,7 +160,7 @@ func TestTxSha(t *testing.T) {
msgTx.LockTime = 0
// Ensure the hash produced is expected.
txHash, err := msgTx.TxSha(pver)
txHash, err := msgTx.TxSha()
if err != nil {
t.Errorf("TxSha: %v", err)
}