Update for recent chainhash-related API changes. (#78)

This updates all code in the main package and subpackages to make use of
the new chainhash package since the old wire.ShaHash type and functions
have been removed in favor of the abstracted package.

Also, since this required API changes anyways and the hash algorithm is
no longer tied specifically to SHA, all other functions throughout the
code base which had "Sha" in their name have been changed to Hash so
they are not incorrectly implying the hash algorithm.

The following is an overview of the changes:

- Update all references to wire.ShaHash to the new chainhash.Hash type
- Rename the following functions and update all references:
  - Block.Sha -> Hash
  - Block.TxSha -> TxHash
  - Tx.Sha -> Hash
  - bloom.Filter.AddShaHash -> AddHash
- Rename all variables that included sha in their name to include hash
  instead
- Add license headers to coinset package files
This commit is contained in:
Dave Collins 2016-08-08 12:38:16 -05:00 committed by GitHub
parent 2c26dd81a5
commit 22c91fa80a
15 changed files with 194 additions and 175 deletions

View file

@ -1,4 +1,4 @@
// Copyright (c) 2013-2014 The btcsuite developers // Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC // Use of this source code is governed by an ISC
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
@ -9,6 +9,7 @@ import (
"fmt" "fmt"
"io" "io"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
) )
@ -31,12 +32,12 @@ func (e OutOfRangeError) Error() string {
// transactions on their first access so subsequent accesses don't have to // transactions on their first access so subsequent accesses don't have to
// repeat the relatively expensive hashing operations. // repeat the relatively expensive hashing operations.
type Block struct { type Block struct {
msgBlock *wire.MsgBlock // Underlying MsgBlock msgBlock *wire.MsgBlock // Underlying MsgBlock
serializedBlock []byte // Serialized bytes for the block serializedBlock []byte // Serialized bytes for the block
blockSha *wire.ShaHash // Cached block hash blockHash *chainhash.Hash // Cached block hash
blockHeight int32 // Height in the main block chain blockHeight int32 // Height in the main block chain
transactions []*Tx // Transactions transactions []*Tx // Transactions
txnsGenerated bool // ALL wrapped transactions generated txnsGenerated bool // ALL wrapped transactions generated
} }
// MsgBlock returns the underlying wire.MsgBlock for the Block. // MsgBlock returns the underlying wire.MsgBlock for the Block.
@ -67,19 +68,19 @@ func (b *Block) Bytes() ([]byte, error) {
return serializedBlock, nil return serializedBlock, nil
} }
// Sha returns the block identifier hash for the Block. This is equivalent to // Hash returns the block identifier hash for the Block. This is equivalent to
// calling BlockSha on the underlying wire.MsgBlock, however it caches the // calling BlockHash on the underlying wire.MsgBlock, however it caches the
// result so subsequent calls are more efficient. // result so subsequent calls are more efficient.
func (b *Block) Sha() *wire.ShaHash { func (b *Block) Hash() *chainhash.Hash {
// Return the cached block hash if it has already been generated. // Return the cached block hash if it has already been generated.
if b.blockSha != nil { if b.blockHash != nil {
return b.blockSha return b.blockHash
} }
// Cache the block hash and return it. // Cache the block hash and return it.
sha := b.msgBlock.BlockSha() hash := b.msgBlock.BlockHash()
b.blockSha = &sha b.blockHash = &hash
return &sha return &hash
} }
// Tx returns a wrapped transaction (btcutil.Tx) for the transaction at the // Tx returns a wrapped transaction (btcutil.Tx) for the transaction at the
@ -145,12 +146,12 @@ func (b *Block) Transactions() []*Tx {
return b.transactions return b.transactions
} }
// TxSha returns the hash for the requested transaction number in the Block. // TxHash returns the hash for the requested transaction number in the Block.
// The supplied index is 0 based. That is to say, the first transaction in the // The supplied index is 0 based. That is to say, the first transaction in the
// block is txNum 0. This is equivalent to calling TxSha on the underlying // block is txNum 0. This is equivalent to calling TxHash on the underlying
// wire.MsgTx, however it caches the result so subsequent calls are more // wire.MsgTx, however it caches the result so subsequent calls are more
// efficient. // efficient.
func (b *Block) TxSha(txNum int) (*wire.ShaHash, error) { func (b *Block) TxHash(txNum int) (*chainhash.Hash, error) {
// Attempt to get a wrapped transaction for the specified index. It // Attempt to get a wrapped transaction for the specified index. It
// will be created lazily if needed or simply return the cached version // will be created lazily if needed or simply return the cached version
// if it has already been generated. // if it has already been generated.
@ -161,7 +162,7 @@ func (b *Block) TxSha(txNum int) (*wire.ShaHash, error) {
// Defer to the wrapped transaction which will return the cached hash if // Defer to the wrapped transaction which will return the cached hash if
// it has already been generated. // it has already been generated.
return tx.Sha(), nil return tx.Hash(), nil
} }
// TxLoc returns the offsets and lengths of each transaction in a raw block. // TxLoc returns the offsets and lengths of each transaction in a raw block.

View file

@ -1,4 +1,4 @@
// Copyright (c) 2013-2014 The btcsuite developers // Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC // Use of this source code is governed by an ISC
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
@ -11,6 +11,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
@ -35,23 +36,23 @@ func TestBlock(t *testing.T) {
} }
// Hash for block 100,000. // Hash for block 100,000.
wantShaStr := "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506" wantHashStr := "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
wantSha, err := wire.NewShaHashFromStr(wantShaStr) wantHash, err := chainhash.NewHashFromStr(wantHashStr)
if err != nil { if err != nil {
t.Errorf("NewShaHashFromStr: %v", err) t.Errorf("NewHashFromStr: %v", err)
} }
// Request the sha multiple times to test generation and caching. // Request the hash multiple times to test generation and caching.
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
sha := b.Sha() hash := b.Hash()
if !sha.IsEqual(wantSha) { if !hash.IsEqual(wantHash) {
t.Errorf("Sha #%d mismatched sha - got %v, want %v", i, t.Errorf("Hash #%d mismatched hash - got %v, want %v",
sha, wantSha) i, hash, wantHash)
} }
} }
// Shas for the transactions in Block100000. // Hashes for the transactions in Block100000.
wantTxShas := []string{ wantTxHashes := []string{
"8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87", "8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87",
"fff2525b8931402dd09222c50775608f75787bd2b87e56995a7bdd30f79702c4", "fff2525b8931402dd09222c50775608f75787bd2b87e56995a7bdd30f79702c4",
"6359f0868171b1d194cbee1af2f16ea598ae8fad666d9b012c8ed2b79a236ec4", "6359f0868171b1d194cbee1af2f16ea598ae8fad666d9b012c8ed2b79a236ec4",
@ -61,14 +62,15 @@ func TestBlock(t *testing.T) {
// Create a new block to nuke all cached data. // Create a new block to nuke all cached data.
b = btcutil.NewBlock(&Block100000) b = btcutil.NewBlock(&Block100000)
// Request sha for all transactions one at a time via Tx. // Request hash for all transactions one at a time via Tx.
for i, txSha := range wantTxShas { for i, txHash := range wantTxHashes {
wantSha, err := wire.NewShaHashFromStr(txSha) wantHash, err := chainhash.NewHashFromStr(txHash)
if err != nil { if err != nil {
t.Errorf("NewShaHashFromStr: %v", err) t.Errorf("NewHashFromStr: %v", err)
} }
// Request the sha multiple times to test generation and caching. // Request the hash multiple times to test generation and
// caching.
for j := 0; j < 2; j++ { for j := 0; j < 2; j++ {
tx, err := b.Tx(i) tx, err := b.Tx(i)
if err != nil { if err != nil {
@ -76,10 +78,10 @@ func TestBlock(t *testing.T) {
continue continue
} }
sha := tx.Sha() hash := tx.Hash()
if !sha.IsEqual(wantSha) { if !hash.IsEqual(wantHash) {
t.Errorf("Sha #%d mismatched sha - got %v, "+ t.Errorf("Hash #%d mismatched hash - got %v, "+
"want %v", j, sha, wantSha) "want %v", j, hash, wantHash)
continue continue
} }
} }
@ -94,24 +96,24 @@ func TestBlock(t *testing.T) {
transactions := b.Transactions() transactions := b.Transactions()
// Ensure we get the expected number of transactions. // Ensure we get the expected number of transactions.
if len(transactions) != len(wantTxShas) { if len(transactions) != len(wantTxHashes) {
t.Errorf("Transactions #%d mismatched number of "+ t.Errorf("Transactions #%d mismatched number of "+
"transactions - got %d, want %d", i, "transactions - got %d, want %d", i,
len(transactions), len(wantTxShas)) len(transactions), len(wantTxHashes))
continue continue
} }
// Ensure all of the shas match. // Ensure all of the hashes match.
for j, tx := range transactions { for j, tx := range transactions {
wantSha, err := wire.NewShaHashFromStr(wantTxShas[j]) wantHash, err := chainhash.NewHashFromStr(wantTxHashes[j])
if err != nil { if err != nil {
t.Errorf("NewShaHashFromStr: %v", err) t.Errorf("NewHashFromStr: %v", err)
} }
sha := tx.Sha() hash := tx.Hash()
if !sha.IsEqual(wantSha) { if !hash.IsEqual(wantHash) {
t.Errorf("Transactions #%d mismatched shas - "+ t.Errorf("Transactions #%d mismatched hashes "+
"got %v, want %v", j, sha, wantSha) "- got %v, want %v", j, hash, wantHash)
continue continue
} }
} }
@ -262,15 +264,15 @@ func TestBlockErrors(t *testing.T) {
"got %v, want %v", err, io.EOF) "got %v, want %v", err, io.EOF)
} }
// Ensure TxSha returns expected error on invalid indices. // Ensure TxHash returns expected error on invalid indices.
_, err = b.TxSha(-1) _, err = b.TxHash(-1)
if _, ok := err.(btcutil.OutOfRangeError); !ok { if _, ok := err.(btcutil.OutOfRangeError); !ok {
t.Errorf("TxSha: wrong error - got: %v <%T>, "+ t.Errorf("TxHash: wrong error - got: %v <%T>, "+
"want: <%T>", err, err, btcutil.OutOfRangeError("")) "want: <%T>", err, err, btcutil.OutOfRangeError(""))
} }
_, err = b.TxSha(len(Block100000.Transactions) + 1) _, err = b.TxHash(len(Block100000.Transactions) + 1)
if _, ok := err.(btcutil.OutOfRangeError); !ok { if _, ok := err.(btcutil.OutOfRangeError); !ok {
t.Errorf("TxSha: wrong error - got: %v <%T>, "+ t.Errorf("TxHash: wrong error - got: %v <%T>, "+
"want: <%T>", err, err, btcutil.OutOfRangeError("")) "want: <%T>", err, err, btcutil.OutOfRangeError(""))
} }
@ -302,13 +304,13 @@ func TestBlockErrors(t *testing.T) {
var Block100000 = wire.MsgBlock{ var Block100000 = wire.MsgBlock{
Header: wire.BlockHeader{ Header: wire.BlockHeader{
Version: 1, Version: 1,
PrevBlock: wire.ShaHash([32]byte{ // Make go vet happy. PrevBlock: chainhash.Hash([32]byte{ // Make go vet happy.
0x50, 0x12, 0x01, 0x19, 0x17, 0x2a, 0x61, 0x04, 0x50, 0x12, 0x01, 0x19, 0x17, 0x2a, 0x61, 0x04,
0x21, 0xa6, 0xc3, 0x01, 0x1d, 0xd3, 0x30, 0xd9, 0x21, 0xa6, 0xc3, 0x01, 0x1d, 0xd3, 0x30, 0xd9,
0xdf, 0x07, 0xb6, 0x36, 0x16, 0xc2, 0xcc, 0x1f, 0xdf, 0x07, 0xb6, 0x36, 0x16, 0xc2, 0xcc, 0x1f,
0x1c, 0xd0, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0xd0, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
}), // 000000000002d01c1fccc21636b607dfd930d31d01c3a62104612a1719011250 }), // 000000000002d01c1fccc21636b607dfd930d31d01c3a62104612a1719011250
MerkleRoot: wire.ShaHash([32]byte{ // Make go vet happy. MerkleRoot: chainhash.Hash([32]byte{ // Make go vet happy.
0x66, 0x57, 0xa9, 0x25, 0x2a, 0xac, 0xd5, 0xc0, 0x66, 0x57, 0xa9, 0x25, 0x2a, 0xac, 0xd5, 0xc0,
0xb2, 0x94, 0x09, 0x96, 0xec, 0xff, 0x95, 0x22, 0xb2, 0x94, 0x09, 0x96, 0xec, 0xff, 0x95, 0x22,
0x28, 0xc3, 0x06, 0x7c, 0xc3, 0x8d, 0x48, 0x85, 0x28, 0xc3, 0x06, 0x7c, 0xc3, 0x8d, 0x48, 0x85,
@ -324,7 +326,7 @@ var Block100000 = wire.MsgBlock{
TxIn: []*wire.TxIn{ TxIn: []*wire.TxIn{
{ {
PreviousOutPoint: wire.OutPoint{ PreviousOutPoint: wire.OutPoint{
Hash: wire.ShaHash{}, Hash: chainhash.Hash{},
Index: 0xffffffff, Index: 0xffffffff,
}, },
SignatureScript: []byte{ SignatureScript: []byte{
@ -358,7 +360,7 @@ var Block100000 = wire.MsgBlock{
TxIn: []*wire.TxIn{ TxIn: []*wire.TxIn{
{ {
PreviousOutPoint: wire.OutPoint{ PreviousOutPoint: wire.OutPoint{
Hash: wire.ShaHash([32]byte{ // Make go vet happy. Hash: chainhash.Hash([32]byte{ // Make go vet happy.
0x03, 0x2e, 0x38, 0xe9, 0xc0, 0xa8, 0x4c, 0x60, 0x03, 0x2e, 0x38, 0xe9, 0xc0, 0xa8, 0x4c, 0x60,
0x46, 0xd6, 0x87, 0xd1, 0x05, 0x56, 0xdc, 0xac, 0x46, 0xd6, 0x87, 0xd1, 0x05, 0x56, 0xdc, 0xac,
0xc4, 0x1d, 0x27, 0x5e, 0xc5, 0x5f, 0xc0, 0x07, 0xc4, 0x1d, 0x27, 0x5e, 0xc5, 0x5f, 0xc0, 0x07,
@ -427,7 +429,7 @@ var Block100000 = wire.MsgBlock{
TxIn: []*wire.TxIn{ TxIn: []*wire.TxIn{
{ {
PreviousOutPoint: wire.OutPoint{ PreviousOutPoint: wire.OutPoint{
Hash: wire.ShaHash([32]byte{ // Make go vet happy. Hash: chainhash.Hash([32]byte{ // Make go vet happy.
0xc3, 0x3e, 0xbf, 0xf2, 0xa7, 0x09, 0xf1, 0x3d, 0xc3, 0x3e, 0xbf, 0xf2, 0xa7, 0x09, 0xf1, 0x3d,
0x9f, 0x9a, 0x75, 0x69, 0xab, 0x16, 0xa3, 0x27, 0x9f, 0x9a, 0x75, 0x69, 0xab, 0x16, 0xa3, 0x27,
0x86, 0xaf, 0x7d, 0x7e, 0x2d, 0xe0, 0x92, 0x65, 0x86, 0xaf, 0x7d, 0x7e, 0x2d, 0xe0, 0x92, 0x65,
@ -495,7 +497,7 @@ var Block100000 = wire.MsgBlock{
TxIn: []*wire.TxIn{ TxIn: []*wire.TxIn{
{ {
PreviousOutPoint: wire.OutPoint{ PreviousOutPoint: wire.OutPoint{
Hash: wire.ShaHash([32]byte{ // Make go vet happy. Hash: chainhash.Hash([32]byte{ // Make go vet happy.
0x0b, 0x60, 0x72, 0xb3, 0x86, 0xd4, 0xa7, 0x73, 0x0b, 0x60, 0x72, 0xb3, 0x86, 0xd4, 0xa7, 0x73,
0x23, 0x52, 0x37, 0xf6, 0x4c, 0x11, 0x26, 0xac, 0x23, 0x52, 0x37, 0xf6, 0x4c, 0x11, 0x26, 0xac,
0x3b, 0x24, 0x0c, 0x84, 0xb9, 0x17, 0xa3, 0x90, 0x3b, 0x24, 0x0c, 0x84, 0xb9, 0x17, 0xa3, 0x90,

View file

@ -1,4 +1,4 @@
// Copyright (c) 2014 The btcsuite developers // Copyright (c) 2014-2016 The btcsuite developers
// Use of this source code is governed by an ISC // Use of this source code is governed by an ISC
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
@ -9,6 +9,7 @@ import (
"math/rand" "math/rand"
"time" "time"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil/bloom" "github.com/btcsuite/btcutil/bloom"
) )
@ -28,12 +29,12 @@ func ExampleNewFilter() {
// trasaction is the first transaction in block 310,000 of the main // trasaction is the first transaction in block 310,000 of the main
// bitcoin block chain. // bitcoin block chain.
txHashStr := "fd611c56ca0d378cdcd16244b45c2ba9588da3adac367c4ef43e808b280b8a45" txHashStr := "fd611c56ca0d378cdcd16244b45c2ba9588da3adac367c4ef43e808b280b8a45"
txHash, err := wire.NewShaHashFromStr(txHashStr) txHash, err := chainhash.NewHashFromStr(txHashStr)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
filter.AddShaHash(txHash) filter.AddHash(txHash)
// Show that the filter matches. // Show that the filter matches.
matches := filter.Matches(txHash[:]) matches := filter.Matches(txHash[:])

View file

@ -1,4 +1,4 @@
// Copyright (c) 2014, 2016 The btcsuite developers // Copyright (c) 2014-2016 The btcsuite developers
// Use of this source code is governed by an ISC // Use of this source code is governed by an ISC
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
@ -9,6 +9,7 @@ import (
"math" "math"
"sync" "sync"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
@ -166,9 +167,9 @@ func (bf *Filter) Matches(data []byte) bool {
// This function MUST be called with the filter lock held. // This function MUST be called with the filter lock held.
func (bf *Filter) matchesOutPoint(outpoint *wire.OutPoint) bool { func (bf *Filter) matchesOutPoint(outpoint *wire.OutPoint) bool {
// Serialize // Serialize
var buf [wire.HashSize + 4]byte var buf [chainhash.HashSize + 4]byte
copy(buf[:], outpoint.Hash[:]) copy(buf[:], outpoint.Hash[:])
binary.LittleEndian.PutUint32(buf[wire.HashSize:], outpoint.Index) binary.LittleEndian.PutUint32(buf[chainhash.HashSize:], outpoint.Index)
return bf.matches(buf[:]) return bf.matches(buf[:])
} }
@ -214,10 +215,10 @@ func (bf *Filter) Add(data []byte) {
bf.mtx.Unlock() bf.mtx.Unlock()
} }
// AddShaHash adds the passed wire.ShaHash to the Filter. // AddHash adds the passed chainhash.Hash to the Filter.
// //
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (bf *Filter) AddShaHash(hash *wire.ShaHash) { func (bf *Filter) AddHash(hash *chainhash.Hash) {
bf.mtx.Lock() bf.mtx.Lock()
bf.add(hash[:]) bf.add(hash[:])
bf.mtx.Unlock() bf.mtx.Unlock()
@ -228,9 +229,9 @@ func (bf *Filter) AddShaHash(hash *wire.ShaHash) {
// This function MUST be called with the filter lock held. // This function MUST be called with the filter lock held.
func (bf *Filter) addOutPoint(outpoint *wire.OutPoint) { func (bf *Filter) addOutPoint(outpoint *wire.OutPoint) {
// Serialize // Serialize
var buf [wire.HashSize + 4]byte var buf [chainhash.HashSize + 4]byte
copy(buf[:], outpoint.Hash[:]) copy(buf[:], outpoint.Hash[:])
binary.LittleEndian.PutUint32(buf[wire.HashSize:], outpoint.Index) binary.LittleEndian.PutUint32(buf[chainhash.HashSize:], outpoint.Index)
bf.add(buf[:]) bf.add(buf[:])
} }
@ -249,7 +250,7 @@ func (bf *Filter) AddOutPoint(outpoint *wire.OutPoint) {
// script. // script.
// //
// This function MUST be called with the filter lock held. // This function MUST be called with the filter lock held.
func (bf *Filter) maybeAddOutpoint(pkScript []byte, outHash *wire.ShaHash, outIdx uint32) { func (bf *Filter) maybeAddOutpoint(pkScript []byte, outHash *chainhash.Hash, outIdx uint32) {
switch bf.msgFilterLoad.Flags { switch bf.msgFilterLoad.Flags {
case wire.BloomUpdateAll: case wire.BloomUpdateAll:
outpoint := wire.NewOutPoint(outHash, outIdx) outpoint := wire.NewOutPoint(outHash, outIdx)
@ -272,7 +273,7 @@ func (bf *Filter) maybeAddOutpoint(pkScript []byte, outHash *wire.ShaHash, outId
func (bf *Filter) matchTxAndUpdate(tx *btcutil.Tx) bool { func (bf *Filter) matchTxAndUpdate(tx *btcutil.Tx) bool {
// Check if the filter matches the hash of the transaction. // Check if the filter matches the hash of the transaction.
// This is useful for finding transactions when they appear in a block. // This is useful for finding transactions when they appear in a block.
matched := bf.matches(tx.Sha()[:]) matched := bf.matches(tx.Hash()[:])
// Check if the filter matches any data elements in the public key // Check if the filter matches any data elements in the public key
// scripts of any of the outputs. When it does, add the outpoint that // scripts of any of the outputs. When it does, add the outpoint that
@ -294,7 +295,7 @@ func (bf *Filter) matchTxAndUpdate(tx *btcutil.Tx) bool {
} }
matched = true matched = true
bf.maybeAddOutpoint(txOut.PkScript, tx.Sha(), uint32(i)) bf.maybeAddOutpoint(txOut.PkScript, tx.Hash(), uint32(i))
break break
} }
} }

View file

@ -1,4 +1,4 @@
// Copyright (c) 2013, 2014, 2016 The btcsuite developers // Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC // Use of this source code is governed by an ISC
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
@ -9,6 +9,7 @@ import (
"encoding/hex" "encoding/hex"
"testing" "testing"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/bloom" "github.com/btcsuite/btcutil/bloom"
@ -126,9 +127,9 @@ func TestFilterFPRange(t *testing.T) {
for _, test := range tests { for _, test := range tests {
// Convert test input to appropriate types. // Convert test input to appropriate types.
hash, err := wire.NewShaHashFromStr(test.hash) hash, err := chainhash.NewHashFromStr(test.hash)
if err != nil { if err != nil {
t.Errorf("NewShaHashFromStr unexpected error: %v", err) t.Errorf("NewHashFromStr unexpected error: %v", err)
continue continue
} }
want, err := hex.DecodeString(test.want) want, err := hex.DecodeString(test.want)
@ -140,7 +141,7 @@ func TestFilterFPRange(t *testing.T) {
// Add the test hash to the bloom filter and ensure the // Add the test hash to the bloom filter and ensure the
// filter serializes to the expected bytes. // filter serializes to the expected bytes.
f := test.filter f := test.filter
f.AddShaHash(hash) f.AddHash(hash)
got := bytes.NewBuffer(nil) got := bytes.NewBuffer(nil)
err = f.MsgFilterLoad().BtcEncode(got, wire.ProtocolVersion) err = f.MsgFilterLoad().BtcEncode(got, wire.ProtocolVersion)
if err != nil { if err != nil {
@ -300,38 +301,38 @@ func TestFilterBloomMatch(t *testing.T) {
f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)
inputStr := "b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b" inputStr := "b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b"
sha, err := wire.NewShaHashFromStr(inputStr) hash, err := chainhash.NewHashFromStr(inputStr)
if err != nil { if err != nil {
t.Errorf("TestFilterBloomMatch NewShaHashFromStr failed: %v\n", err) t.Errorf("TestFilterBloomMatch NewHashFromStr failed: %v\n", err)
return return
} }
f.AddShaHash(sha) f.AddHash(hash)
if !f.MatchTxAndUpdate(tx) { if !f.MatchTxAndUpdate(tx) {
t.Errorf("TestFilterBloomMatch didn't match sha %s", inputStr) t.Errorf("TestFilterBloomMatch didn't match hash %s", inputStr)
} }
f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)
inputStr = "6bff7fcd4f8565ef406dd5d63d4ff94f318fe82027fd4dc451b04474019f74b4" inputStr = "6bff7fcd4f8565ef406dd5d63d4ff94f318fe82027fd4dc451b04474019f74b4"
shaBytes, err := hex.DecodeString(inputStr) hashBytes, err := hex.DecodeString(inputStr)
if err != nil { if err != nil {
t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err)
return return
} }
f.Add(shaBytes) f.Add(hashBytes)
if !f.MatchTxAndUpdate(tx) { if !f.MatchTxAndUpdate(tx) {
t.Errorf("TestFilterBloomMatch didn't match sha %s", inputStr) t.Errorf("TestFilterBloomMatch didn't match hash %s", inputStr)
} }
f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)
inputStr = "30450220070aca44506c5cef3a16ed519d7c3c39f8aab192c4e1c90d065" + inputStr = "30450220070aca44506c5cef3a16ed519d7c3c39f8aab192c4e1c90d065" +
"f37b8a4af6141022100a8e160b856c2d43d27d8fba71e5aef6405b8643" + "f37b8a4af6141022100a8e160b856c2d43d27d8fba71e5aef6405b8643" +
"ac4cb7cb3c462aced7f14711a01" "ac4cb7cb3c462aced7f14711a01"
shaBytes, err = hex.DecodeString(inputStr) hashBytes, err = hex.DecodeString(inputStr)
if err != nil { if err != nil {
t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err)
return return
} }
f.Add(shaBytes) f.Add(hashBytes)
if !f.MatchTxAndUpdate(tx) { if !f.MatchTxAndUpdate(tx) {
t.Errorf("TestFilterBloomMatch didn't match input signature %s", inputStr) t.Errorf("TestFilterBloomMatch didn't match input signature %s", inputStr)
} }
@ -340,24 +341,24 @@ func TestFilterBloomMatch(t *testing.T) {
inputStr = "046d11fee51b0e60666d5049a9101a72741df480b96ee26488a4d3466b95" + inputStr = "046d11fee51b0e60666d5049a9101a72741df480b96ee26488a4d3466b95" +
"c9a40ac5eeef87e10a5cd336c19a84565f80fa6c547957b7700ff4dfbdefe" + "c9a40ac5eeef87e10a5cd336c19a84565f80fa6c547957b7700ff4dfbdefe" +
"76036c339" "76036c339"
shaBytes, err = hex.DecodeString(inputStr) hashBytes, err = hex.DecodeString(inputStr)
if err != nil { if err != nil {
t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err)
return return
} }
f.Add(shaBytes) f.Add(hashBytes)
if !f.MatchTxAndUpdate(tx) { if !f.MatchTxAndUpdate(tx) {
t.Errorf("TestFilterBloomMatch didn't match input pubkey %s", inputStr) t.Errorf("TestFilterBloomMatch didn't match input pubkey %s", inputStr)
} }
f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)
inputStr = "04943fdd508053c75000106d3bc6e2754dbcff19" inputStr = "04943fdd508053c75000106d3bc6e2754dbcff19"
shaBytes, err = hex.DecodeString(inputStr) hashBytes, err = hex.DecodeString(inputStr)
if err != nil { if err != nil {
t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err)
return return
} }
f.Add(shaBytes) f.Add(hashBytes)
if !f.MatchTxAndUpdate(tx) { if !f.MatchTxAndUpdate(tx) {
t.Errorf("TestFilterBloomMatch didn't match output address %s", inputStr) t.Errorf("TestFilterBloomMatch didn't match output address %s", inputStr)
} }
@ -367,24 +368,24 @@ func TestFilterBloomMatch(t *testing.T) {
f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)
inputStr = "a266436d2965547608b9e15d9032a7b9d64fa431" inputStr = "a266436d2965547608b9e15d9032a7b9d64fa431"
shaBytes, err = hex.DecodeString(inputStr) hashBytes, err = hex.DecodeString(inputStr)
if err != nil { if err != nil {
t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err)
return return
} }
f.Add(shaBytes) f.Add(hashBytes)
if !f.MatchTxAndUpdate(tx) { if !f.MatchTxAndUpdate(tx) {
t.Errorf("TestFilterBloomMatch didn't match output address %s", inputStr) t.Errorf("TestFilterBloomMatch didn't match output address %s", inputStr)
} }
f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)
inputStr = "90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b" inputStr = "90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"
sha, err = wire.NewShaHashFromStr(inputStr) hash, err = chainhash.NewHashFromStr(inputStr)
if err != nil { if err != nil {
t.Errorf("TestFilterBloomMatch NewShaHashFromStr failed: %v\n", err) t.Errorf("TestFilterBloomMatch NewHashFromStr failed: %v\n", err)
return return
} }
outpoint := wire.NewOutPoint(sha, 0) outpoint := wire.NewOutPoint(hash, 0)
f.AddOutPoint(outpoint) f.AddOutPoint(outpoint)
if !f.MatchTxAndUpdate(tx) { if !f.MatchTxAndUpdate(tx) {
t.Errorf("TestFilterBloomMatch didn't match outpoint %s", inputStr) t.Errorf("TestFilterBloomMatch didn't match outpoint %s", inputStr)
@ -392,36 +393,36 @@ func TestFilterBloomMatch(t *testing.T) {
f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)
inputStr = "00000009e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436" inputStr = "00000009e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436"
sha, err = wire.NewShaHashFromStr(inputStr) hash, err = chainhash.NewHashFromStr(inputStr)
if err != nil { if err != nil {
t.Errorf("TestFilterBloomMatch NewShaHashFromStr failed: %v\n", err) t.Errorf("TestFilterBloomMatch NewHashFromStr failed: %v\n", err)
return return
} }
f.AddShaHash(sha) f.AddHash(hash)
if f.MatchTxAndUpdate(tx) { if f.MatchTxAndUpdate(tx) {
t.Errorf("TestFilterBloomMatch matched sha %s", inputStr) t.Errorf("TestFilterBloomMatch matched hash %s", inputStr)
} }
f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)
inputStr = "0000006d2965547608b9e15d9032a7b9d64fa431" inputStr = "0000006d2965547608b9e15d9032a7b9d64fa431"
shaBytes, err = hex.DecodeString(inputStr) hashBytes, err = hex.DecodeString(inputStr)
if err != nil { if err != nil {
t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err) t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err)
return return
} }
f.Add(shaBytes) f.Add(hashBytes)
if f.MatchTxAndUpdate(tx) { if f.MatchTxAndUpdate(tx) {
t.Errorf("TestFilterBloomMatch matched address %s", inputStr) t.Errorf("TestFilterBloomMatch matched address %s", inputStr)
} }
f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)
inputStr = "90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b" inputStr = "90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"
sha, err = wire.NewShaHashFromStr(inputStr) hash, err = chainhash.NewHashFromStr(inputStr)
if err != nil { if err != nil {
t.Errorf("TestFilterBloomMatch NewShaHashFromStr failed: %v\n", err) t.Errorf("TestFilterBloomMatch NewHashFromStr failed: %v\n", err)
return return
} }
outpoint = wire.NewOutPoint(sha, 1) outpoint = wire.NewOutPoint(hash, 1)
f.AddOutPoint(outpoint) f.AddOutPoint(outpoint)
if f.MatchTxAndUpdate(tx) { if f.MatchTxAndUpdate(tx) {
t.Errorf("TestFilterBloomMatch matched outpoint %s", inputStr) t.Errorf("TestFilterBloomMatch matched outpoint %s", inputStr)
@ -429,12 +430,12 @@ func TestFilterBloomMatch(t *testing.T) {
f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)
inputStr = "000000d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b" inputStr = "000000d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"
sha, err = wire.NewShaHashFromStr(inputStr) hash, err = chainhash.NewHashFromStr(inputStr)
if err != nil { if err != nil {
t.Errorf("TestFilterBloomMatch NewShaHashFromStr failed: %v\n", err) t.Errorf("TestFilterBloomMatch NewHashFromStr failed: %v\n", err)
return return
} }
outpoint = wire.NewOutPoint(sha, 0) outpoint = wire.NewOutPoint(hash, 0)
f.AddOutPoint(outpoint) f.AddOutPoint(outpoint)
if f.MatchTxAndUpdate(tx) { if f.MatchTxAndUpdate(tx) {
t.Errorf("TestFilterBloomMatch matched outpoint %s", inputStr) t.Errorf("TestFilterBloomMatch matched outpoint %s", inputStr)
@ -465,12 +466,12 @@ func TestFilterInsertUpdateNone(t *testing.T) {
f.Add(inputBytes) f.Add(inputBytes)
inputStr = "147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b" inputStr = "147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"
sha, err := wire.NewShaHashFromStr(inputStr) hash, err := chainhash.NewHashFromStr(inputStr)
if err != nil { if err != nil {
t.Errorf("TestFilterInsertUpdateNone NewShaHashFromStr failed: %v", err) t.Errorf("TestFilterInsertUpdateNone NewHashFromStr failed: %v", err)
return return
} }
outpoint := wire.NewOutPoint(sha, 0) outpoint := wire.NewOutPoint(hash, 0)
if f.MatchesOutPoint(outpoint) { if f.MatchesOutPoint(outpoint) {
t.Errorf("TestFilterInsertUpdateNone matched outpoint %s", inputStr) t.Errorf("TestFilterInsertUpdateNone matched outpoint %s", inputStr)
@ -478,12 +479,12 @@ func TestFilterInsertUpdateNone(t *testing.T) {
} }
inputStr = "02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041" inputStr = "02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"
sha, err = wire.NewShaHashFromStr(inputStr) hash, err = chainhash.NewHashFromStr(inputStr)
if err != nil { if err != nil {
t.Errorf("TestFilterInsertUpdateNone NewShaHashFromStr failed: %v", err) t.Errorf("TestFilterInsertUpdateNone NewHashFromStr failed: %v", err)
return return
} }
outpoint = wire.NewOutPoint(sha, 0) outpoint = wire.NewOutPoint(hash, 0)
if f.MatchesOutPoint(outpoint) { if f.MatchesOutPoint(outpoint) {
t.Errorf("TestFilterInsertUpdateNone matched outpoint %s", inputStr) t.Errorf("TestFilterInsertUpdateNone matched outpoint %s", inputStr)
@ -617,12 +618,12 @@ func TestFilterInsertP2PubKeyOnly(t *testing.T) {
// We should match the generation pubkey // We should match the generation pubkey
inputStr = "147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b" inputStr = "147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"
sha, err := wire.NewShaHashFromStr(inputStr) hash, err := chainhash.NewHashFromStr(inputStr)
if err != nil { if err != nil {
t.Errorf("TestMerkleBlockP2PubKeyOnly NewShaHashFromStr failed: %v", err) t.Errorf("TestMerkleBlockP2PubKeyOnly NewHashFromStr failed: %v", err)
return return
} }
outpoint := wire.NewOutPoint(sha, 0) outpoint := wire.NewOutPoint(hash, 0)
if !f.MatchesOutPoint(outpoint) { if !f.MatchesOutPoint(outpoint) {
t.Errorf("TestMerkleBlockP2PubKeyOnly didn't match the generation "+ t.Errorf("TestMerkleBlockP2PubKeyOnly didn't match the generation "+
"outpoint %s", inputStr) "outpoint %s", inputStr)
@ -631,12 +632,12 @@ func TestFilterInsertP2PubKeyOnly(t *testing.T) {
// We should not match the 4th transaction, which is not p2pk // We should not match the 4th transaction, which is not p2pk
inputStr = "02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041" inputStr = "02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"
sha, err = wire.NewShaHashFromStr(inputStr) hash, err = chainhash.NewHashFromStr(inputStr)
if err != nil { if err != nil {
t.Errorf("TestMerkleBlockP2PubKeyOnly NewShaHashFromStr failed: %v", err) t.Errorf("TestMerkleBlockP2PubKeyOnly NewHashFromStr failed: %v", err)
return return
} }
outpoint = wire.NewOutPoint(sha, 0) outpoint = wire.NewOutPoint(hash, 0)
if f.MatchesOutPoint(outpoint) { if f.MatchesOutPoint(outpoint) {
t.Errorf("TestMerkleBlockP2PubKeyOnly matched outpoint %s", inputStr) t.Errorf("TestMerkleBlockP2PubKeyOnly matched outpoint %s", inputStr)
return return

View file

@ -1,4 +1,4 @@
// Copyright (c) 2013, 2014 The btcsuite developers // Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC // Use of this source code is governed by an ISC
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
@ -6,6 +6,7 @@ package bloom
import ( import (
"github.com/btcsuite/btcd/blockchain" "github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
) )
@ -14,8 +15,8 @@ import (
// wire.MsgMerkleBlock according to a filter. // wire.MsgMerkleBlock according to a filter.
type merkleBlock struct { type merkleBlock struct {
numTx uint32 numTx uint32
allHashes []*wire.ShaHash allHashes []*chainhash.Hash
finalHashes []*wire.ShaHash finalHashes []*chainhash.Hash
matchedBits []byte matchedBits []byte
bits []byte bits []byte
} }
@ -28,12 +29,12 @@ func (m *merkleBlock) calcTreeWidth(height uint32) uint32 {
// calcHash returns the hash for a sub-tree given a depth-first height and // calcHash returns the hash for a sub-tree given a depth-first height and
// node position. // node position.
func (m *merkleBlock) calcHash(height, pos uint32) *wire.ShaHash { func (m *merkleBlock) calcHash(height, pos uint32) *chainhash.Hash {
if height == 0 { if height == 0 {
return m.allHashes[pos] return m.allHashes[pos]
} }
var right *wire.ShaHash var right *chainhash.Hash
left := m.calcHash(height-1, pos*2) left := m.calcHash(height-1, pos*2)
if pos*2+1 < m.calcTreeWidth(height-1) { if pos*2+1 < m.calcTreeWidth(height-1) {
right = m.calcHash(height-1, pos*2+1) right = m.calcHash(height-1, pos*2+1)
@ -82,7 +83,7 @@ func NewMerkleBlock(block *btcutil.Block, filter *Filter) (*wire.MsgMerkleBlock,
numTx := uint32(len(block.Transactions())) numTx := uint32(len(block.Transactions()))
mBlock := merkleBlock{ mBlock := merkleBlock{
numTx: numTx, numTx: numTx,
allHashes: make([]*wire.ShaHash, 0, numTx), allHashes: make([]*chainhash.Hash, 0, numTx),
matchedBits: make([]byte, 0, numTx), matchedBits: make([]byte, 0, numTx),
} }
@ -95,7 +96,7 @@ func NewMerkleBlock(block *btcutil.Block, filter *Filter) (*wire.MsgMerkleBlock,
} else { } else {
mBlock.matchedBits = append(mBlock.matchedBits, 0x00) mBlock.matchedBits = append(mBlock.matchedBits, 0x00)
} }
mBlock.allHashes = append(mBlock.allHashes, tx.Sha()) mBlock.allHashes = append(mBlock.allHashes, tx.Hash())
} }
// Calculate the number of merkle branches (height) in the tree. // Calculate the number of merkle branches (height) in the tree.
@ -111,11 +112,11 @@ func NewMerkleBlock(block *btcutil.Block, filter *Filter) (*wire.MsgMerkleBlock,
msgMerkleBlock := wire.MsgMerkleBlock{ msgMerkleBlock := wire.MsgMerkleBlock{
Header: block.MsgBlock().Header, Header: block.MsgBlock().Header,
Transactions: uint32(mBlock.numTx), Transactions: uint32(mBlock.numTx),
Hashes: make([]*wire.ShaHash, 0, len(mBlock.finalHashes)), Hashes: make([]*chainhash.Hash, 0, len(mBlock.finalHashes)),
Flags: make([]byte, (len(mBlock.bits)+7)/8), Flags: make([]byte, (len(mBlock.bits)+7)/8),
} }
for _, sha := range mBlock.finalHashes { for _, hash := range mBlock.finalHashes {
msgMerkleBlock.AddTxHash(sha) msgMerkleBlock.AddTxHash(hash)
} }
for i := uint32(0); i < uint32(len(mBlock.bits)); i++ { for i := uint32(0); i < uint32(len(mBlock.bits)); i++ {
msgMerkleBlock.Flags[i/8] |= mBlock.bits[i] << (i % 8) msgMerkleBlock.Flags[i/8] |= mBlock.bits[i] << (i % 8)

View file

@ -1,4 +1,4 @@
// Copyright (c) 2013, 2014 The btcsuite developers // Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC // Use of this source code is governed by an ISC
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
@ -9,6 +9,7 @@ import (
"encoding/hex" "encoding/hex"
"testing" "testing"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/bloom" "github.com/btcsuite/btcutil/bloom"
@ -37,13 +38,13 @@ func TestMerkleBlock3(t *testing.T) {
f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll) f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)
inputStr := "63194f18be0af63f2c6bc9dc0f777cbefed3d9415c4af83f3ee3a3d669c00cb5" inputStr := "63194f18be0af63f2c6bc9dc0f777cbefed3d9415c4af83f3ee3a3d669c00cb5"
sha, err := wire.NewShaHashFromStr(inputStr) hash, err := chainhash.NewHashFromStr(inputStr)
if err != nil { if err != nil {
t.Errorf("TestMerkleBlock3 NewShaHashFromStr failed: %v", err) t.Errorf("TestMerkleBlock3 NewHashFromStr failed: %v", err)
return return
} }
f.AddShaHash(sha) f.AddHash(hash)
mBlock, _ := bloom.NewMerkleBlock(blk, f) mBlock, _ := bloom.NewMerkleBlock(blk, f)

View file

@ -1,3 +1,7 @@
// Copyright (c) 2014-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package coinset package coinset
import ( import (
@ -5,13 +9,14 @@ import (
"errors" "errors"
"sort" "sort"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
) )
// Coin represents a spendable transaction outpoint // Coin represents a spendable transaction outpoint
type Coin interface { type Coin interface {
Hash() *wire.ShaHash Hash() *chainhash.Hash
Index() uint32 Index() uint32
Value() btcutil.Amount Value() btcutil.Amount
PkScript() []byte PkScript() []byte
@ -354,8 +359,8 @@ type SimpleCoin struct {
var _ Coin = &SimpleCoin{} var _ Coin = &SimpleCoin{}
// Hash returns the hash value of the transaction on which the Coin is an output // Hash returns the hash value of the transaction on which the Coin is an output
func (c *SimpleCoin) Hash() *wire.ShaHash { func (c *SimpleCoin) Hash() *chainhash.Hash {
return c.Tx.Sha() return c.Tx.Hash()
} }
// Index returns the index of the output on the transaction which the Coin represents // Index returns the index of the output on the transaction which the Coin represents

View file

@ -1,3 +1,7 @@
// Copyright (c) 2014-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package coinset_test package coinset_test
import ( import (
@ -6,20 +10,20 @@ import (
"fmt" "fmt"
"testing" "testing"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/coinset" "github.com/btcsuite/btcutil/coinset"
"github.com/btcsuite/fastsha256" "github.com/btcsuite/fastsha256"
) )
type TestCoin struct { type TestCoin struct {
TxHash *wire.ShaHash TxHash *chainhash.Hash
TxIndex uint32 TxIndex uint32
TxValue btcutil.Amount TxValue btcutil.Amount
TxNumConfs int64 TxNumConfs int64
} }
func (c *TestCoin) Hash() *wire.ShaHash { return c.TxHash } func (c *TestCoin) Hash() *chainhash.Hash { return c.TxHash }
func (c *TestCoin) Index() uint32 { return c.TxIndex } func (c *TestCoin) Index() uint32 { return c.TxIndex }
func (c *TestCoin) Value() btcutil.Amount { return c.TxValue } func (c *TestCoin) Value() btcutil.Amount { return c.TxValue }
func (c *TestCoin) PkScript() []byte { return nil } func (c *TestCoin) PkScript() []byte { return nil }
@ -29,7 +33,7 @@ func (c *TestCoin) ValueAge() int64 { return int64(c.TxValue) * c.TxNumCon
func NewCoin(index int64, value btcutil.Amount, numConfs int64) coinset.Coin { func NewCoin(index int64, value btcutil.Amount, numConfs int64) coinset.Coin {
h := fastsha256.New() h := fastsha256.New()
h.Write([]byte(fmt.Sprintf("%d", index))) h.Write([]byte(fmt.Sprintf("%d", index)))
hash, _ := wire.NewShaHash(h.Sum(nil)) hash, _ := chainhash.NewHash(h.Sum(nil))
c := &TestCoin{ c := &TestCoin{
TxHash: hash, TxHash: hash,
TxIndex: 0, TxIndex: 0,

View file

@ -1,4 +1,4 @@
// Copyright (c) 2014 The btcsuite developers // Copyright (c) 2014-2016 The btcsuite developers
// Use of this source code is governed by an ISC // Use of this source code is governed by an ISC
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
@ -20,7 +20,7 @@ import (
"github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/base58" "github.com/btcsuite/btcutil/base58"
) )
@ -395,7 +395,7 @@ func (k *ExtendedKey) String() string {
serializedBytes = append(serializedBytes, k.pubKeyBytes()...) serializedBytes = append(serializedBytes, k.pubKeyBytes()...)
} }
checkSum := wire.DoubleSha256(serializedBytes)[:4] checkSum := chainhash.DoubleHashB(serializedBytes)[:4]
serializedBytes = append(serializedBytes, checkSum...) serializedBytes = append(serializedBytes, checkSum...)
return base58.Encode(serializedBytes) return base58.Encode(serializedBytes)
} }
@ -496,7 +496,7 @@ func NewKeyFromString(key string) (*ExtendedKey, error) {
// Split the payload and checksum up and ensure the checksum matches. // Split the payload and checksum up and ensure the checksum matches.
payload := decoded[:len(decoded)-4] payload := decoded[:len(decoded)-4]
checkSum := decoded[len(decoded)-4:] checkSum := decoded[len(decoded)-4:]
expectedCheckSum := wire.DoubleSha256(payload)[:4] expectedCheckSum := chainhash.DoubleHashB(payload)[:4]
if !bytes.Equal(checkSum, expectedCheckSum) { if !bytes.Equal(checkSum, expectedCheckSum) {
return nil, ErrBadChecksum return nil, ErrBadChecksum
} }

25
tx.go
View file

@ -1,4 +1,4 @@
// Copyright (c) 2013-2014 The btcsuite developers // Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC // Use of this source code is governed by an ISC
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
@ -8,6 +8,7 @@ import (
"bytes" "bytes"
"io" "io"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
) )
@ -21,9 +22,9 @@ const TxIndexUnknown = -1
// transaction on its first access so subsequent accesses don't have to repeat // transaction on its first access so subsequent accesses don't have to repeat
// the relatively expensive hashing operations. // the relatively expensive hashing operations.
type Tx struct { type Tx struct {
msgTx *wire.MsgTx // Underlying MsgTx msgTx *wire.MsgTx // Underlying MsgTx
txSha *wire.ShaHash // Cached transaction hash txHash *chainhash.Hash // Cached transaction hash
txIndex int // Position within a block or TxIndexUnknown txIndex int // Position within a block or TxIndexUnknown
} }
// MsgTx returns the underlying wire.MsgTx for the transaction. // MsgTx returns the underlying wire.MsgTx for the transaction.
@ -32,19 +33,19 @@ func (t *Tx) MsgTx() *wire.MsgTx {
return t.msgTx return t.msgTx
} }
// Sha returns the hash of the transaction. This is equivalent to // Hash returns the hash of the transaction. This is equivalent to
// calling TxSha on the underlying wire.MsgTx, however it caches the // calling TxHash on the underlying wire.MsgTx, however it caches the
// result so subsequent calls are more efficient. // result so subsequent calls are more efficient.
func (t *Tx) Sha() *wire.ShaHash { func (t *Tx) Hash() *chainhash.Hash {
// Return the cached hash if it has already been generated. // Return the cached hash if it has already been generated.
if t.txSha != nil { if t.txHash != nil {
return t.txSha return t.txHash
} }
// Cache the hash and return it. // Cache the hash and return it.
sha := t.msgTx.TxSha() hash := t.msgTx.TxHash()
t.txSha = &sha t.txHash = &hash
return &sha return &hash
} }
// Index returns the saved index of the transaction within a block. This value // Index returns the saved index of the transaction within a block. This value

View file

@ -1,4 +1,4 @@
// Copyright (c) 2013-2014 The btcsuite developers // Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC // Use of this source code is governed by an ISC
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
@ -10,7 +10,7 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
) )
@ -35,18 +35,18 @@ func TestTx(t *testing.T) {
} }
// Hash for block 100,000 transaction 0. // Hash for block 100,000 transaction 0.
wantShaStr := "8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87" wantHashStr := "8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87"
wantSha, err := wire.NewShaHashFromStr(wantShaStr) wantHash, err := chainhash.NewHashFromStr(wantHashStr)
if err != nil { if err != nil {
t.Errorf("NewShaHashFromStr: %v", err) t.Errorf("NewHashFromStr: %v", err)
} }
// Request the sha multiple times to test generation and caching. // Request the hash multiple times to test generation and caching.
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
sha := tx.Sha() hash := tx.Hash()
if !sha.IsEqual(wantSha) { if !hash.IsEqual(wantHash) {
t.Errorf("Sha #%d mismatched sha - got %v, want %v", i, t.Errorf("Hash #%d mismatched hash - got %v, want %v", i,
sha, wantSha) hash, wantHash)
} }
} }
} }

View file

@ -1,4 +1,4 @@
// Copyright (c) 2015 The btcsuite developers // Copyright (c) 2015-2016 The btcsuite developers
// Use of this source code is governed by an ISC // Use of this source code is governed by an ISC
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
@ -11,6 +11,7 @@ import (
"bytes" "bytes"
"sort" "sort"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
) )
@ -76,7 +77,7 @@ func (s sortableInputSlice) Less(i, j int) bool {
// At this point, the hashes are not equal, so reverse them to // At this point, the hashes are not equal, so reverse them to
// big-endian and return the result of the comparison. // big-endian and return the result of the comparison.
const hashSize = wire.HashSize const hashSize = chainhash.HashSize
for b := 0; b < hashSize/2; b++ { for b := 0; b < hashSize/2; b++ {
ihash[b], ihash[hashSize-1-b] = ihash[hashSize-1-b], ihash[b] ihash[b], ihash[hashSize-1-b] = ihash[hashSize-1-b], ihash[b]
jhash[b], jhash[hashSize-1-b] = jhash[hashSize-1-b], jhash[b] jhash[b], jhash[hashSize-1-b] = jhash[hashSize-1-b], jhash[b]

View file

@ -1,4 +1,4 @@
// Copyright (c) 2015 The btcsuite developers // Copyright (c) 2015-2016 The btcsuite developers
// Use of this source code is governed by an ISC // Use of this source code is governed by an ISC
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
@ -96,7 +96,7 @@ func TestSort(t *testing.T) {
// Sort the transaction and ensure the resulting hash is the // Sort the transaction and ensure the resulting hash is the
// expected value. // expected value.
sortedTx := txsort.Sort(&tx) sortedTx := txsort.Sort(&tx)
if got := sortedTx.TxSha().String(); got != test.sortedHash { if got := sortedTx.TxHash().String(); got != test.sortedHash {
t.Errorf("Sort (%s): sorted hash does not match "+ t.Errorf("Sort (%s): sorted hash does not match "+
"expected - got %v, want %v", test.name, got, "expected - got %v, want %v", test.name, got,
test.sortedHash) test.sortedHash)
@ -104,7 +104,7 @@ func TestSort(t *testing.T) {
} }
// Ensure the original transaction is not modified. // Ensure the original transaction is not modified.
if got := tx.TxSha().String(); got != test.unsortedHash { if got := tx.TxHash().String(); got != test.unsortedHash {
t.Errorf("Sort (%s): unsorted hash does not match "+ t.Errorf("Sort (%s): unsorted hash does not match "+
"expected - got %v, want %v", test.name, got, "expected - got %v, want %v", test.name, got,
test.unsortedHash) test.unsortedHash)
@ -114,7 +114,7 @@ func TestSort(t *testing.T) {
// Now sort the transaction using the mutable version and ensure // Now sort the transaction using the mutable version and ensure
// the resulting hash is the expected value. // the resulting hash is the expected value.
txsort.InPlaceSort(&tx) txsort.InPlaceSort(&tx)
if got := tx.TxSha().String(); got != test.sortedHash { if got := tx.TxHash().String(); got != test.sortedHash {
t.Errorf("SortMutate (%s): sorted hash does not match "+ t.Errorf("SortMutate (%s): sorted hash does not match "+
"expected - got %v, want %v", test.name, got, "expected - got %v, want %v", test.name, got,
test.sortedHash) test.sortedHash)

8
wif.go
View file

@ -1,4 +1,4 @@
// Copyright (c) 2013, 2014 The btcsuite developers // Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC // Use of this source code is governed by an ISC
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
@ -10,7 +10,7 @@ import (
"github.com/btcsuite/btcd/btcec" "github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcutil/base58" "github.com/btcsuite/btcutil/base58"
) )
@ -110,7 +110,7 @@ func DecodeWIF(wif string) (*WIF, error) {
} else { } else {
tosum = decoded[:1+btcec.PrivKeyBytesLen] tosum = decoded[:1+btcec.PrivKeyBytesLen]
} }
cksum := wire.DoubleSha256(tosum)[:4] cksum := chainhash.DoubleHashB(tosum)[:4]
if !bytes.Equal(cksum, decoded[decodedLen-4:]) { if !bytes.Equal(cksum, decoded[decodedLen-4:]) {
return nil, ErrChecksumMismatch return nil, ErrChecksumMismatch
} }
@ -142,7 +142,7 @@ func (w *WIF) String() string {
if w.CompressPubKey { if w.CompressPubKey {
a = append(a, compressMagic) a = append(a, compressMagic)
} }
cksum := wire.DoubleSha256(a)[:4] cksum := chainhash.DoubleHashB(a)[:4]
a = append(a, cksum...) a = append(a, cksum...)
return base58.Encode(a) return base58.Encode(a)
} }