Remove deprecated TxShas func from btcutil.Block.

This commit removes the previously deprecated TxShas function from
btcutil.Block.  The preferred method to access transaction hashes is via
the Sha function on each btcutil.Tx contained within the block.

For example, the following illustrates how convert the old TxShas approach
to the new method:

OLD:

for i, sha := range block.TxShas() {
	// use sha
}

NEW:

for i, tx := range block.Transactions() {
	// use tx.Sha()
}

This commit also updates the tests for the removed function.
This commit is contained in:
Dave Collins 2014-03-24 13:21:44 -05:00
parent c8b172c394
commit 2db41b1f56
3 changed files with 27 additions and 126 deletions

View file

@ -29,14 +29,12 @@ func (e OutOfRangeError) Error() string {
// transactions on their first access so subsequent accesses don't have to
// repeat the relatively expensive hashing operations.
type Block struct {
msgBlock *btcwire.MsgBlock // Underlying MsgBlock
serializedBlock []byte // Serialized bytes for the block
blockSha *btcwire.ShaHash // Cached block hash
blockHeight int64 // Height in the main block chain
txShas []*btcwire.ShaHash // Cached transaction hashes
txShasGenerated bool // ALL transaction hashes generated
transactions []*Tx // Transactions
txnsGenerated bool // ALL wrapped transactions generated
msgBlock *btcwire.MsgBlock // Underlying MsgBlock
serializedBlock []byte // Serialized bytes for the block
blockSha *btcwire.ShaHash // Cached block hash
blockHeight int64 // Height in the main block chain
transactions []*Tx // Transactions
txnsGenerated bool // ALL wrapped transactions generated
}
// MsgBlock returns the underlying btcwire.MsgBlock for the Block.
@ -167,42 +165,6 @@ func (b *Block) TxSha(txNum int) (*btcwire.ShaHash, error) {
return tx.Sha(), nil
}
// TxShas returns a slice of hashes for all transactions in the Block. This is
// equivalent to calling TxSha on each underlying btcwire.MsgTx, however it
// caches the result so subsequent calls are more efficient.
//
// DEPRECATED - This function will be removed in the next version and
// should not be used. Instead, use Transactions() and .Sha() on each
// transaction.
func (b *Block) TxShas() ([]*btcwire.ShaHash, error) {
// Return cached hashes if they have ALL already been generated. This
// flag is necessary because the transaction hashes are lazily generated
// in a sparse fashion.
if b.txShasGenerated {
return b.txShas, nil
}
// Generate slice to hold all of the transaction hashes if needed.
if len(b.txShas) == 0 {
b.txShas = make([]*btcwire.ShaHash, len(b.msgBlock.Transactions))
}
// Generate and cache the transaction hashes for all that haven't already
// been done.
for i, hash := range b.txShas {
if hash == nil {
// Ignore the errors since the only way these can fail
// is if the index is out of range which is not possible
// here due to the range.
tx, _ := b.Tx(i)
b.txShas[i] = tx.Sha()
}
}
b.txShasGenerated = true
return b.txShas, nil
}
// TxLoc returns the offsets and lengths of each transaction in a raw block.
// It is used to allow fast indexing into transactions within the raw byte
// stream.

View file

@ -61,29 +61,6 @@ func TestBlock(t *testing.T) {
"e9a66845e05d5abc0ad04ec80f774a7e585c6e8db975962d069a522137b80c1d",
}
// Request sha for all transactions one at a time via TxSha.
for i, txSha := range wantTxShas {
wantSha, err := btcwire.NewShaHashFromStr(txSha)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
// Request the sha multiple times to test generation and caching.
for j := 0; j < 2; j++ {
sha, err := b.TxSha(i)
if err != nil {
t.Errorf("TxSha: %v", err)
continue
}
if !sha.IsEqual(wantSha) {
t.Errorf("TxSha #%d mismatched sha - got %v, "+
"want %v", j, sha, wantSha)
continue
}
}
}
// Create a new block to nuke all cached data.
b = btcutil.NewBlock(&Block100000)
@ -114,43 +91,6 @@ func TestBlock(t *testing.T) {
// Create a new block to nuke all cached data.
b = btcutil.NewBlock(&Block100000)
// Request slice of all transaction shas multiple times to test
// generation and caching.
for i := 0; i < 2; i++ {
txShas, err := b.TxShas()
if err != nil {
t.Errorf("TxShas: %v", err)
continue
}
// Ensure we get the expected number of transaction shas.
if len(txShas) != len(wantTxShas) {
t.Errorf("TxShas #%d mismatched number of shas -"+
"got %d, want %d", i, len(txShas),
len(wantTxShas))
continue
}
// Ensure all of the shas match.
for j, txSha := range wantTxShas {
wantSha, err := btcwire.NewShaHashFromStr(txSha)
if err != nil {
t.Errorf("NewShaHashFromStr: %v", err)
}
if !txShas[j].IsEqual(wantSha) {
t.Errorf("TxShas #%d mismatched shas - "+
"got %v, want %v", j,
spew.Sdump(txShas),
spew.Sdump(wantTxShas))
continue
}
}
}
// Create a new block to nuke all cached data.
b = btcutil.NewBlock(&Block100000)
// Request slice of all transactions multiple times to test generation
// and caching.
for i := 0; i < 2; i++ {

View file

@ -3,35 +3,33 @@ github.com/conformal/btcutil/base58.go Base58Decode 100.00% (20/20)
github.com/conformal/btcutil/base58.go Base58Encode 100.00% (15/15)
github.com/conformal/btcutil/block.go Block.Tx 100.00% (12/12)
github.com/conformal/btcutil/block.go Block.Transactions 100.00% (11/11)
github.com/conformal/btcutil/block.go Block.TxShas 100.00% (10/10)
github.com/conformal/btcutil/address.go encodeAddress 100.00% (9/9)
github.com/conformal/btcutil/block.go NewBlockFromBytes 100.00% (7/7)
github.com/conformal/btcutil/tx.go NewTxFromBytes 100.00% (7/7)
github.com/conformal/btcutil/block.go NewBlockFromBytes 100.00% (7/7)
github.com/conformal/btcutil/block.go Block.Sha 100.00% (5/5)
github.com/conformal/btcutil/tx.go Tx.Sha 100.00% (5/5)
github.com/conformal/btcutil/block.go Block.TxSha 100.00% (4/4)
github.com/conformal/btcutil/hash160.go calcHash 100.00% (2/2)
github.com/conformal/btcutil/address.go NewAddressScriptHash 100.00% (2/2)
github.com/conformal/btcutil/address.go AddressPubKey.ScriptAddress 100.00% (1/1)
github.com/conformal/btcutil/tx.go Tx.MsgTx 100.00% (1/1)
github.com/conformal/btcutil/address.go AddressPubKeyHash.EncodeAddress 100.00% (1/1)
github.com/conformal/btcutil/address.go AddressPubKeyHash.String 100.00% (1/1)
github.com/conformal/btcutil/address.go AddressScriptHash.EncodeAddress 100.00% (1/1)
github.com/conformal/btcutil/hash160.go calcHash 100.00% (2/2)
github.com/conformal/btcutil/address.go AddressScriptHash.ScriptAddress 100.00% (1/1)
github.com/conformal/btcutil/address.go AddressScriptHash.String 100.00% (1/1)
github.com/conformal/btcutil/address.go AddressPubKey.EncodeAddress 100.00% (1/1)
github.com/conformal/btcutil/address.go AddressPubKey.String 100.00% (1/1)
github.com/conformal/btcutil/tx.go NewTx 100.00% (1/1)
github.com/conformal/btcutil/hash160.go Hash160 100.00% (1/1)
github.com/conformal/btcutil/address.go AddressScriptHash.String 100.00% (1/1)
github.com/conformal/btcutil/tx.go Tx.MsgTx 100.00% (1/1)
github.com/conformal/btcutil/address.go AddressPubKeyHash.String 100.00% (1/1)
github.com/conformal/btcutil/tx.go Tx.SetIndex 100.00% (1/1)
github.com/conformal/btcutil/address.go AddressPubKey.ScriptAddress 100.00% (1/1)
github.com/conformal/btcutil/address.go AddressPubKey.String 100.00% (1/1)
github.com/conformal/btcutil/address.go AddressPubKeyHash.ScriptAddress 100.00% (1/1)
github.com/conformal/btcutil/block.go NewBlock 100.00% (1/1)
github.com/conformal/btcutil/block.go Block.SetHeight 100.00% (1/1)
github.com/conformal/btcutil/block.go Block.Height 100.00% (1/1)
github.com/conformal/btcutil/hash160.go Hash160 100.00% (1/1)
github.com/conformal/btcutil/address.go AddressPubKeyHash.EncodeAddress 100.00% (1/1)
github.com/conformal/btcutil/tx.go NewTx 100.00% (1/1)
github.com/conformal/btcutil/block.go NewBlockFromBlockAndBytes 100.00% (1/1)
github.com/conformal/btcutil/address.go AddressScriptHash.EncodeAddress 100.00% (1/1)
github.com/conformal/btcutil/tx.go Tx.Index 100.00% (1/1)
github.com/conformal/btcutil/block.go OutOfRangeError.Error 100.00% (1/1)
github.com/conformal/btcutil/block.go Block.MsgBlock 100.00% (1/1)
github.com/conformal/btcutil/tx.go Tx.Index 100.00% (1/1)
github.com/conformal/btcutil/block.go Block.Height 100.00% (1/1)
github.com/conformal/btcutil/block.go Block.SetHeight 100.00% (1/1)
github.com/conformal/btcutil/block.go NewBlock 100.00% (1/1)
github.com/conformal/btcutil/block.go NewBlockFromBlockAndBytes 100.00% (1/1)
github.com/conformal/btcutil/address.go AddressPubKeyHash.ScriptAddress 100.00% (1/1)
github.com/conformal/btcutil/address.go DecodeAddress 95.65% (22/23)
github.com/conformal/btcutil/appdata.go appDataDir 92.00% (23/25)
github.com/conformal/btcutil/address.go NewAddressPubKeyHash 91.67% (11/12)
@ -43,13 +41,14 @@ github.com/conformal/btcutil/address.go AddressPubKey.serialize 85.71% (6/7)
github.com/conformal/btcutil/address.go NewAddressPubKey 83.33% (15/18)
github.com/conformal/btcutil/address.go checkBitcoinNet 83.33% (5/6)
github.com/conformal/btcutil/address.go DecodePrivateKey 82.61% (19/23)
github.com/conformal/btcutil/block.go Block.TxSha 75.00% (3/4)
github.com/conformal/btcutil/address.go AddressPubKeyHash.IsForNet 60.00% (3/5)
github.com/conformal/btcutil/address.go AddressScriptHash.IsForNet 60.00% (3/5)
github.com/conformal/btcutil/address.go AddressPubKey.IsForNet 60.00% (3/5)
github.com/conformal/btcutil/address.go AddressScriptHash.IsForNet 60.00% (3/5)
github.com/conformal/btcutil/certgen.go NewTLSCertPair 0.00% (0/50)
github.com/conformal/btcutil/address.go AddressPubKey.AddressPubKeyHash 0.00% (0/3)
github.com/conformal/btcutil/appdata.go AppDataDir 0.00% (0/1)
github.com/conformal/btcutil/address.go AddressPubKey.SetFormat 0.00% (0/1)
github.com/conformal/btcutil/appdata.go AppDataDir 0.00% (0/1)
github.com/conformal/btcutil/address.go AddressPubKey.Format 0.00% (0/1)
github.com/conformal/btcutil ------------------------------- 78.14% (286/366)
github.com/conformal/btcutil ------------------------------- 77.25% (275/356)