Skip to content

Commit

Permalink
blockchain: Consolidate tests into package.
Browse files Browse the repository at this point in the history
Putting the test code in the same package makes it easier for forks
since they don't have to change the import paths as much and it also
gets rid of the need for internal_test.go to bridge.

While here, remove the reorganization test since it is much better
handled by the full block tests and is no longer needed and do some
light cleanup on a few other tests.

The full block tests had to remain in the separate test package since it
is a circular dependency otherwise.  This did require duplicating some
of the chain setup code, but given the other benefits this is
acceptable.
  • Loading branch information
davecgh committed Aug 20, 2017
1 parent f4fe6c3 commit e02fbcf
Show file tree
Hide file tree
Showing 14 changed files with 366 additions and 381 deletions.
7 changes: 3 additions & 4 deletions blockchain/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package blockchain_test
package blockchain

import (
"testing"

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcutil"
)

Expand All @@ -17,7 +16,7 @@ func BenchmarkIsCoinBase(b *testing.B) {
tx, _ := btcutil.NewBlock(&Block100000).Tx(1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
blockchain.IsCoinBase(tx)
IsCoinBase(tx)
}
}

Expand All @@ -27,6 +26,6 @@ func BenchmarkIsCoinBaseTx(b *testing.B) {
tx := Block100000.Transactions[1]
b.ResetTimer()
for i := 0; i < b.N; i++ {
blockchain.IsCoinBaseTx(tx)
IsCoinBaseTx(tx)
}
}
80 changes: 40 additions & 40 deletions blockchain/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package blockchain_test
package blockchain

import (
"testing"
"time"

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
Expand Down Expand Up @@ -49,7 +48,7 @@ func TestHaveBlock(t *testing.T) {
chain.TstSetCoinbaseMaturity(1)

for i := 1; i < len(blocks); i++ {
_, isOrphan, err := chain.ProcessBlock(blocks[i], blockchain.BFNone)
_, isOrphan, err := chain.ProcessBlock(blocks[i], BFNone)
if err != nil {
t.Errorf("ProcessBlock fail on block %v: %v\n", i, err)
return
Expand All @@ -63,7 +62,7 @@ func TestHaveBlock(t *testing.T) {

// Insert an orphan block.
_, isOrphan, err := chain.ProcessBlock(btcutil.NewBlock(&Block100000),
blockchain.BFNone)
BFNone)
if err != nil {
t.Errorf("Unable to process block: %v", err)
return
Expand Down Expand Up @@ -125,12 +124,15 @@ func TestCalcSequenceLock(t *testing.T) {
blockVersion := int32(0x20000000 | (uint32(1) << csvBit))

// Generate enough synthetic blocks to activate CSV.
chain, node := blockchain.TstNewFakeChain(netParams)
chain := newFakeChain(netParams)
node := chain.bestNode
blockTime := node.Header().Timestamp
numBlocksToActivate := (netParams.MinerConfirmationWindow * 3)
for i := uint32(0); i < numBlocksToActivate; i++ {
blockTime = blockTime.Add(time.Second)
node = chain.TstNewFakeNode(node, blockVersion, 0, blockTime)
node = newFakeNode(node, blockVersion, 0, blockTime)
chain.index.AddNode(node)
chain.bestNode = node
}

// Create a utxo view with a fake utxo for the inputs used in the
Expand All @@ -142,11 +144,9 @@ func TestCalcSequenceLock(t *testing.T) {
Value: 10,
}},
})
utxoView := blockchain.NewUtxoViewpoint()
utxoView := NewUtxoViewpoint()
utxoView.AddTxOuts(targetTx, int32(numBlocksToActivate)-4)
bestHeader := node.Header()
bestHash := bestHeader.BlockHash()
utxoView.SetBestHash(&bestHash)
utxoView.SetBestHash(&node.hash)

// Create a utxo that spends the fake utxo created above for use in the
// transactions created in the tests. It has an age of 4 blocks. Note
Expand Down Expand Up @@ -190,9 +190,9 @@ func TestCalcSequenceLock(t *testing.T) {

tests := []struct {
tx *wire.MsgTx
view *blockchain.UtxoViewpoint
view *UtxoViewpoint
mempool bool
want *blockchain.SequenceLock
want *SequenceLock
}{
// A transaction of version one should disable sequence locks
// as the new sequence number semantics only apply to
Expand All @@ -202,11 +202,11 @@ func TestCalcSequenceLock(t *testing.T) {
Version: 1,
TxIn: []*wire.TxIn{{
PreviousOutPoint: utxo,
Sequence: blockchain.LockTimeToSequence(false, 3),
Sequence: LockTimeToSequence(false, 3),
}},
},
view: utxoView,
want: &blockchain.SequenceLock{
want: &SequenceLock{
Seconds: -1,
BlockHeight: -1,
},
Expand All @@ -223,7 +223,7 @@ func TestCalcSequenceLock(t *testing.T) {
}},
},
view: utxoView,
want: &blockchain.SequenceLock{
want: &SequenceLock{
Seconds: -1,
BlockHeight: -1,
},
Expand All @@ -239,11 +239,11 @@ func TestCalcSequenceLock(t *testing.T) {
Version: 2,
TxIn: []*wire.TxIn{{
PreviousOutPoint: utxo,
Sequence: blockchain.LockTimeToSequence(true, 2),
Sequence: LockTimeToSequence(true, 2),
}},
},
view: utxoView,
want: &blockchain.SequenceLock{
want: &SequenceLock{
Seconds: medianTime - 1,
BlockHeight: -1,
},
Expand All @@ -257,11 +257,11 @@ func TestCalcSequenceLock(t *testing.T) {
Version: 2,
TxIn: []*wire.TxIn{{
PreviousOutPoint: utxo,
Sequence: blockchain.LockTimeToSequence(true, 1024),
Sequence: LockTimeToSequence(true, 1024),
}},
},
view: utxoView,
want: &blockchain.SequenceLock{
want: &SequenceLock{
Seconds: medianTime + 1023,
BlockHeight: -1,
},
Expand All @@ -277,18 +277,18 @@ func TestCalcSequenceLock(t *testing.T) {
Version: 2,
TxIn: []*wire.TxIn{{
PreviousOutPoint: utxo,
Sequence: blockchain.LockTimeToSequence(true, 2560),
Sequence: LockTimeToSequence(true, 2560),
}, {
PreviousOutPoint: utxo,
Sequence: blockchain.LockTimeToSequence(false, 4),
Sequence: LockTimeToSequence(false, 4),
}, {
PreviousOutPoint: utxo,
Sequence: blockchain.LockTimeToSequence(false, 5) |
Sequence: LockTimeToSequence(false, 5) |
wire.SequenceLockTimeDisabled,
}},
},
view: utxoView,
want: &blockchain.SequenceLock{
want: &SequenceLock{
Seconds: medianTime + (5 << wire.SequenceLockTimeGranularity) - 1,
BlockHeight: prevUtxoHeight + 3,
},
Expand All @@ -302,11 +302,11 @@ func TestCalcSequenceLock(t *testing.T) {
Version: 2,
TxIn: []*wire.TxIn{{
PreviousOutPoint: utxo,
Sequence: blockchain.LockTimeToSequence(false, 3),
Sequence: LockTimeToSequence(false, 3),
}},
},
view: utxoView,
want: &blockchain.SequenceLock{
want: &SequenceLock{
Seconds: -1,
BlockHeight: prevUtxoHeight + 2,
},
Expand All @@ -319,14 +319,14 @@ func TestCalcSequenceLock(t *testing.T) {
Version: 2,
TxIn: []*wire.TxIn{{
PreviousOutPoint: utxo,
Sequence: blockchain.LockTimeToSequence(true, 5120),
Sequence: LockTimeToSequence(true, 5120),
}, {
PreviousOutPoint: utxo,
Sequence: blockchain.LockTimeToSequence(true, 2560),
Sequence: LockTimeToSequence(true, 2560),
}},
},
view: utxoView,
want: &blockchain.SequenceLock{
want: &SequenceLock{
Seconds: medianTime + (10 << wire.SequenceLockTimeGranularity) - 1,
BlockHeight: -1,
},
Expand All @@ -340,14 +340,14 @@ func TestCalcSequenceLock(t *testing.T) {
Version: 2,
TxIn: []*wire.TxIn{{
PreviousOutPoint: utxo,
Sequence: blockchain.LockTimeToSequence(false, 1),
Sequence: LockTimeToSequence(false, 1),
}, {
PreviousOutPoint: utxo,
Sequence: blockchain.LockTimeToSequence(false, 11),
Sequence: LockTimeToSequence(false, 11),
}},
},
view: utxoView,
want: &blockchain.SequenceLock{
want: &SequenceLock{
Seconds: -1,
BlockHeight: prevUtxoHeight + 10,
},
Expand All @@ -360,20 +360,20 @@ func TestCalcSequenceLock(t *testing.T) {
Version: 2,
TxIn: []*wire.TxIn{{
PreviousOutPoint: utxo,
Sequence: blockchain.LockTimeToSequence(true, 2560),
Sequence: LockTimeToSequence(true, 2560),
}, {
PreviousOutPoint: utxo,
Sequence: blockchain.LockTimeToSequence(true, 6656),
Sequence: LockTimeToSequence(true, 6656),
}, {
PreviousOutPoint: utxo,
Sequence: blockchain.LockTimeToSequence(false, 3),
Sequence: LockTimeToSequence(false, 3),
}, {
PreviousOutPoint: utxo,
Sequence: blockchain.LockTimeToSequence(false, 9),
Sequence: LockTimeToSequence(false, 9),
}},
},
view: utxoView,
want: &blockchain.SequenceLock{
want: &SequenceLock{
Seconds: medianTime + (13 << wire.SequenceLockTimeGranularity) - 1,
BlockHeight: prevUtxoHeight + 8,
},
Expand All @@ -389,12 +389,12 @@ func TestCalcSequenceLock(t *testing.T) {
Version: 2,
TxIn: []*wire.TxIn{{
PreviousOutPoint: unConfUtxo,
Sequence: blockchain.LockTimeToSequence(false, 2),
Sequence: LockTimeToSequence(false, 2),
}},
},
view: utxoView,
mempool: true,
want: &blockchain.SequenceLock{
want: &SequenceLock{
Seconds: -1,
BlockHeight: nextBlockHeight + 1,
},
Expand All @@ -407,12 +407,12 @@ func TestCalcSequenceLock(t *testing.T) {
Version: 2,
TxIn: []*wire.TxIn{{
PreviousOutPoint: unConfUtxo,
Sequence: blockchain.LockTimeToSequence(true, 1024),
Sequence: LockTimeToSequence(true, 1024),
}},
},
view: utxoView,
mempool: true,
want: &blockchain.SequenceLock{
want: &SequenceLock{
Seconds: nextMedianTime + 1023,
BlockHeight: -1,
},
Expand Down
Loading

0 comments on commit e02fbcf

Please sign in to comment.