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:
parent
dea1bac359
commit
d90740728e
6 changed files with 19 additions and 38 deletions
|
@ -50,7 +50,7 @@ type BlockHeader struct {
|
||||||
const blockHashLen = 80
|
const blockHashLen = 80
|
||||||
|
|
||||||
// BlockSha computes the block identifier hash for the given block header.
|
// 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
|
// Encode the header and run double sha256 everything prior to the
|
||||||
// number of transactions. Ignore the error returns since there is no
|
// number of transactions. Ignore the error returns since there is no
|
||||||
// way the encode could fail except being out of memory which would
|
// 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.
|
// regardless of input.
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
var sha ShaHash
|
var sha ShaHash
|
||||||
_ = writeBlockHeader(&buf, pver, h)
|
_ = writeBlockHeader(&buf, 0, h)
|
||||||
_ = sha.SetBytes(DoubleSha256(buf.Bytes()[0:blockHashLen]))
|
_ = sha.SetBytes(DoubleSha256(buf.Bytes()[0:blockHashLen]))
|
||||||
|
|
||||||
// Even though this function can't currently fail, it still returns
|
// Even though this function can't currently fail, it still returns
|
||||||
|
|
|
@ -14,11 +14,9 @@ import (
|
||||||
// TestGenesisBlock tests the genesis block of the main network for validity by
|
// TestGenesisBlock tests the genesis block of the main network for validity by
|
||||||
// checking the encoded bytes and hashes.
|
// checking the encoded bytes and hashes.
|
||||||
func TestGenesisBlock(t *testing.T) {
|
func TestGenesisBlock(t *testing.T) {
|
||||||
pver := uint32(60002)
|
|
||||||
|
|
||||||
// Encode the genesis block to raw bytes.
|
// Encode the genesis block to raw bytes.
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err := btcwire.GenesisBlock.BtcEncode(&buf, pver)
|
err := btcwire.GenesisBlock.Serialize(&buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("TestGenesisBlock: %v", err)
|
t.Errorf("TestGenesisBlock: %v", err)
|
||||||
return
|
return
|
||||||
|
@ -33,7 +31,7 @@ func TestGenesisBlock(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check hash of the block against expected hash.
|
// Check hash of the block against expected hash.
|
||||||
hash, err := btcwire.GenesisBlock.Header.BlockSha(pver)
|
hash, err := btcwire.GenesisBlock.BlockSha()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("BlockSha: %v", err)
|
t.Errorf("BlockSha: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -48,11 +46,9 @@ func TestGenesisBlock(t *testing.T) {
|
||||||
// TestTestNetGenesisBlock tests the genesis block of the regression test
|
// TestTestNetGenesisBlock tests the genesis block of the regression test
|
||||||
// network for validity by checking the encoded bytes and hashes.
|
// network for validity by checking the encoded bytes and hashes.
|
||||||
func TestTestNetGenesisBlock(t *testing.T) {
|
func TestTestNetGenesisBlock(t *testing.T) {
|
||||||
pver := uint32(60002)
|
|
||||||
|
|
||||||
// Encode the genesis block to raw bytes.
|
// Encode the genesis block to raw bytes.
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err := btcwire.TestNetGenesisBlock.BtcEncode(&buf, pver)
|
err := btcwire.TestNetGenesisBlock.Serialize(&buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("TestTestNetGenesisBlock: %v", err)
|
t.Errorf("TestTestNetGenesisBlock: %v", err)
|
||||||
return
|
return
|
||||||
|
@ -68,7 +64,7 @@ func TestTestNetGenesisBlock(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check hash of the block against expected hash.
|
// Check hash of the block against expected hash.
|
||||||
hash, err := btcwire.TestNetGenesisBlock.Header.BlockSha(pver)
|
hash, err := btcwire.TestNetGenesisBlock.BlockSha()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("BlockSha: %v", err)
|
t.Errorf("BlockSha: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -83,11 +79,9 @@ func TestTestNetGenesisBlock(t *testing.T) {
|
||||||
// TestTestNet3GenesisBlock tests the genesis block of the test network (version
|
// TestTestNet3GenesisBlock tests the genesis block of the test network (version
|
||||||
// 3) for validity by checking the encoded bytes and hashes.
|
// 3) for validity by checking the encoded bytes and hashes.
|
||||||
func TestTestNet3GenesisBlock(t *testing.T) {
|
func TestTestNet3GenesisBlock(t *testing.T) {
|
||||||
pver := uint32(60002)
|
|
||||||
|
|
||||||
// Encode the genesis block to raw bytes.
|
// Encode the genesis block to raw bytes.
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err := btcwire.TestNet3GenesisBlock.BtcEncode(&buf, pver)
|
err := btcwire.TestNet3GenesisBlock.Serialize(&buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("TestTestNet3GenesisBlock: %v", err)
|
t.Errorf("TestTestNet3GenesisBlock: %v", err)
|
||||||
return
|
return
|
||||||
|
@ -103,7 +97,7 @@ func TestTestNet3GenesisBlock(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check hash of the block against expected hash.
|
// Check hash of the block against expected hash.
|
||||||
hash, err := btcwire.TestNet3GenesisBlock.Header.BlockSha(pver)
|
hash, err := btcwire.TestNet3GenesisBlock.BlockSha()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("BlockSha: %v", err)
|
t.Errorf("BlockSha: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -206,17 +206,17 @@ func (msg *MsgBlock) MaxPayloadLength(pver uint32) uint32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
// BlockSha computes the block identifier hash for this block.
|
// BlockSha computes the block identifier hash for this block.
|
||||||
func (msg *MsgBlock) BlockSha(pver uint32) (ShaHash, error) {
|
func (msg *MsgBlock) BlockSha() (ShaHash, error) {
|
||||||
return msg.Header.BlockSha(pver)
|
return msg.Header.BlockSha()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TxShas returns a slice of hashes of all of transactions in this block.
|
// 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
|
var shaList []ShaHash
|
||||||
for _, tx := range msg.Transactions {
|
for _, tx := range msg.Transactions {
|
||||||
// Ignore error here since TxSha can't fail in the current
|
// Ignore error here since TxSha can't fail in the current
|
||||||
// implementation except due to run-time panics.
|
// implementation except due to run-time panics.
|
||||||
sha, _ := tx.TxSha(pver)
|
sha, _ := tx.TxSha()
|
||||||
shaList = append(shaList, sha)
|
shaList = append(shaList, sha)
|
||||||
}
|
}
|
||||||
return shaList, nil
|
return shaList, nil
|
||||||
|
|
|
@ -71,11 +71,6 @@ func TestBlock(t *testing.T) {
|
||||||
// TestBlockTxShas tests the ability to generate a slice of all transaction
|
// TestBlockTxShas tests the ability to generate a slice of all transaction
|
||||||
// hashes from a block accurately.
|
// hashes from a block accurately.
|
||||||
func TestBlockTxShas(t *testing.T) {
|
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.
|
// Block 1, transaction 1 hash.
|
||||||
hashStr := "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
|
hashStr := "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
|
||||||
wantHash, err := btcwire.NewShaHashFromStr(hashStr)
|
wantHash, err := btcwire.NewShaHashFromStr(hashStr)
|
||||||
|
@ -85,7 +80,7 @@ func TestBlockTxShas(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
wantShas := []btcwire.ShaHash{*wantHash}
|
wantShas := []btcwire.ShaHash{*wantHash}
|
||||||
shas, err := blockOne.TxShas(pver)
|
shas, err := blockOne.TxShas()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("TxShas: %v", err)
|
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.
|
// TestBlockSha tests the ability to generate the hash of a block accurately.
|
||||||
func TestBlockSha(t *testing.T) {
|
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.
|
// Block 1 hash.
|
||||||
hashStr := "839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048"
|
hashStr := "839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048"
|
||||||
wantHash, err := btcwire.NewShaHashFromStr(hashStr)
|
wantHash, err := btcwire.NewShaHashFromStr(hashStr)
|
||||||
|
@ -110,7 +100,7 @@ func TestBlockSha(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure the hash produced is expected.
|
// Ensure the hash produced is expected.
|
||||||
blockHash, err := blockOne.BlockSha(pver)
|
blockHash, err := blockOne.BlockSha()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("BlockSha: %v", err)
|
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{
|
var blockOneBytes = []byte{
|
||||||
0x01, 0x00, 0x00, 0x00, // Version 1
|
0x01, 0x00, 0x00, 0x00, // Version 1
|
||||||
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
|
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
|
||||||
|
@ -519,8 +509,7 @@ var blockOneBytes = []byte{
|
||||||
0x00, 0x00, 0x00, 0x00, // Lock time
|
0x00, 0x00, 0x00, 0x00, // Lock time
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transaction location information for block one trasnactions as encoded with
|
// Transaction location information for block one transactions.
|
||||||
// protocol version 60002.
|
|
||||||
var blockOneTxLocs = []btcwire.TxLoc{
|
var blockOneTxLocs = []btcwire.TxLoc{
|
||||||
btcwire.TxLoc{TxStart: 81, TxLen: 134},
|
btcwire.TxLoc{TxStart: 81, TxLen: 134},
|
||||||
}
|
}
|
||||||
|
|
4
msgtx.go
4
msgtx.go
|
@ -89,7 +89,7 @@ func (msg *MsgTx) AddTxOut(to *TxOut) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TxSha generates the ShaHash name for the transaction.
|
// 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.
|
// Encode the transaction and calculate double sha256 on the result.
|
||||||
// Ignore the error returns since the only way the encode could fail
|
// 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
|
// 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.
|
// regardless of input.
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
var sha ShaHash
|
var sha ShaHash
|
||||||
_ = tx.BtcEncode(&buf, pver)
|
_ = tx.Serialize(&buf)
|
||||||
_ = sha.SetBytes(DoubleSha256(buf.Bytes()))
|
_ = sha.SetBytes(DoubleSha256(buf.Bytes()))
|
||||||
|
|
||||||
// Even though this function can't currently fail, it still returns
|
// Even though this function can't currently fail, it still returns
|
||||||
|
|
|
@ -121,8 +121,6 @@ func TestTx(t *testing.T) {
|
||||||
|
|
||||||
// TestTxSha tests the ability to generate the hash of a transaction accurately.
|
// TestTxSha tests the ability to generate the hash of a transaction accurately.
|
||||||
func TestTxSha(t *testing.T) {
|
func TestTxSha(t *testing.T) {
|
||||||
pver := btcwire.ProtocolVersion
|
|
||||||
|
|
||||||
// Hash of first transaction from block 113875.
|
// Hash of first transaction from block 113875.
|
||||||
hashStr := "f051e59b5e2503ac626d03aaeac8ab7be2d72ba4b7e97119c5852d70d52dcb86"
|
hashStr := "f051e59b5e2503ac626d03aaeac8ab7be2d72ba4b7e97119c5852d70d52dcb86"
|
||||||
wantHash, err := btcwire.NewShaHashFromStr(hashStr)
|
wantHash, err := btcwire.NewShaHashFromStr(hashStr)
|
||||||
|
@ -162,7 +160,7 @@ func TestTxSha(t *testing.T) {
|
||||||
msgTx.LockTime = 0
|
msgTx.LockTime = 0
|
||||||
|
|
||||||
// Ensure the hash produced is expected.
|
// Ensure the hash produced is expected.
|
||||||
txHash, err := msgTx.TxSha(pver)
|
txHash, err := msgTx.TxSha()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("TxSha: %v", err)
|
t.Errorf("TxSha: %v", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue