blockchain: Cleanup a few sequence lock tests.

This cleans up the test in TestCalcSequenceLock in the following ways:
- Use calculated values instead of magic value so it is easier to update
  the tests as needed
- Make tests match the comments
- Change comments to be more consistent and fix some grammar errors
- Set mempool flag for unconfirmed tx tests since they are intended to
  mimic transactions in the mempool
This commit is contained in:
Dave Collins 2017-02-02 00:21:46 -06:00
parent 614b799198
commit 7e5e6b4610
No known key found for this signature in database
GPG key ID: B8904D9D9C93D1F2

View file

@ -200,26 +200,31 @@ func TestCalcSequenceLock(t *testing.T) {
}
utxoView.SetBestHash(blocksWithMTP[len(blocksWithMTP)-1].block.Hash())
// The median time calculated from the PoV of the best block in our
// test chain. For unconfirmed inputs, this value will be used since
// the MTP will be calculated from the PoV of the yet-to-be-mined
// block.
nextMedianTime := int64(1401292712)
// We'll refer to this utxo within each input in the transactions
// created below. This utxo has an age of 4 blocks and was mined within
// block 297
targetTx := blocksWithMTP[len(blocksWithMTP)-4].block.Transactions()[0]
// created below. This utxo has an age of 4 blocks. Note that the
// sequence lock heights are always calculated from the same point of
// view that they were originally calculated from for a given utxo.
// That is to say, the height prior to it.
targetBlock := blocksWithMTP[len(blocksWithMTP)-4].block
targetTx := targetBlock.Transactions()[0]
utxo := wire.OutPoint{
Hash: *targetTx.Hash(),
Index: 0,
}
prevUtxoHeight := targetBlock.Height() - 1
// Obtain the median time past from the PoV of the input created above.
// The MTP for the input is the MTP from the PoV of the block *prior*
// to the one that included it.
medianTime := blocksWithMTP[len(blocksWithMTP)-5].mtp.Unix()
// The median time calculated from the PoV of the best block in our
// test chain. For unconfirmed inputs, this value will be used since
// the MTP will be calculated from the PoV of the yet-to-be-mined
// block.
nextMedianTime := blocksWithMTP[len(blocksWithMTP)-1].mtp.Unix() + 1
nextBlockHeight := blocksWithMTP[len(blocksWithMTP)-1].block.Height() + 1
// Add an additional transaction which will serve as our unconfirmed
// output.
var fakeScript []byte
@ -241,8 +246,8 @@ func TestCalcSequenceLock(t *testing.T) {
tests := []struct {
tx *btcutil.Tx
view *blockchain.UtxoViewpoint
want *blockchain.SequenceLock
mempool bool
want *blockchain.SequenceLock
}{
// A transaction of version one should disable sequence locks
// as the new sequence number semantics only apply to
@ -261,9 +266,9 @@ func TestCalcSequenceLock(t *testing.T) {
BlockHeight: -1,
},
},
// A transaction with a single input, that a max int sequence
// number. This sequence number has the high bit set, so
// sequence locks should be disabled.
// A transaction with a single input with max sequence number.
// This sequence number has the high bit set, so sequence locks
// should be disabled.
{
tx: btcutil.NewTx(&wire.MsgTx{
Version: 2,
@ -317,11 +322,11 @@ func TestCalcSequenceLock(t *testing.T) {
},
},
// A transaction with multiple inputs. The first input has a
// lock time expressed in seconds. The second input has a
// sequence lock in blocks with a value of 4. The last input
// has a sequence number with a value of 5, but has the disable
// bit set. So the first lock should be selected as it's the
// target lock as its the furthest in the future lock that
// isn't disabled.
// latest lock that isn't disabled.
{
tx: btcutil.NewTx(&wire.MsgTx{
Version: 2,
@ -330,24 +335,23 @@ func TestCalcSequenceLock(t *testing.T) {
Sequence: blockchain.LockTimeToSequence(true, 2560),
}, {
PreviousOutPoint: utxo,
Sequence: blockchain.LockTimeToSequence(false, 5) |
wire.SequenceLockTimeDisabled,
Sequence: blockchain.LockTimeToSequence(false, 4),
}, {
PreviousOutPoint: utxo,
Sequence: blockchain.LockTimeToSequence(false, 4),
Sequence: blockchain.LockTimeToSequence(false, 5) |
wire.SequenceLockTimeDisabled,
}},
}),
view: utxoView,
want: &blockchain.SequenceLock{
Seconds: medianTime + (5 << wire.SequenceLockTimeGranularity) - 1,
BlockHeight: 299,
BlockHeight: prevUtxoHeight + 3,
},
},
// Transaction has a single input spending the genesis block
// transaction. The input's sequence number is encodes a
// relative lock-time in blocks (3 blocks). The sequence lock
// should have a value of -1 for seconds, but a block height of
// 298 meaning it can be included at height 299.
// Transaction with a single input. The input's sequence number
// encodes a relative lock-time in blocks (3 blocks). The
// sequence lock should have a value of -1 for seconds, but a
// height of 2 meaning it can be included at height 3.
{
tx: btcutil.NewTx(&wire.MsgTx{
Version: 2,
@ -359,7 +363,7 @@ func TestCalcSequenceLock(t *testing.T) {
view: utxoView,
want: &blockchain.SequenceLock{
Seconds: -1,
BlockHeight: 298,
BlockHeight: prevUtxoHeight + 2,
},
},
// A transaction with two inputs with lock times expressed in
@ -383,10 +387,9 @@ func TestCalcSequenceLock(t *testing.T) {
},
},
// A transaction with two inputs with lock times expressed in
// seconds. The selected sequence lock value for blocks should
// be the height further in the future. The converted absolute
// block height should be 302, meaning it can be included in
// block 303.
// blocks. The selected sequence lock value for blocks should
// be the height further in the future, so a height of 10
// indicating it can be included at height 11.
{
tx: btcutil.NewTx(&wire.MsgTx{
Version: 2,
@ -395,19 +398,18 @@ func TestCalcSequenceLock(t *testing.T) {
Sequence: blockchain.LockTimeToSequence(false, 1),
}, {
PreviousOutPoint: utxo,
Sequence: blockchain.LockTimeToSequence(false, 7),
Sequence: blockchain.LockTimeToSequence(false, 11),
}},
}),
view: utxoView,
want: &blockchain.SequenceLock{
Seconds: -1,
BlockHeight: 302,
BlockHeight: prevUtxoHeight + 10,
},
},
// A transaction with multiple inputs. Two inputs are time
// based, and the other two are input maturity based. The lock
// lying further into the future for both inputs should be
// chosen.
// based, and the other two are block based. The lock lying
// further into the future for both inputs should be chosen.
{
tx: btcutil.NewTx(&wire.MsgTx{
Version: 2,
@ -428,15 +430,15 @@ func TestCalcSequenceLock(t *testing.T) {
view: utxoView,
want: &blockchain.SequenceLock{
Seconds: medianTime + (13 << wire.SequenceLockTimeGranularity) - 1,
BlockHeight: 304,
BlockHeight: prevUtxoHeight + 8,
},
},
// A transaction with a single unconfirmed input. As the input
// is confirmed, the height of the input should be interpreted
// as the height of the *next* block. The current block height
// is 300, so the lock time should be calculated using height
// 301 as a base. A 2 block relative lock means the transaction
// can be included after block 302, so in 303.
// as the height of the *next* block. So, a 2 block relative
// lock means the sequence lock should be for 1 block after the
// *next* block height, indicating it can be included 2 blocks
// after that.
{
tx: btcutil.NewTx(&wire.MsgTx{
Version: 2,
@ -446,9 +448,10 @@ func TestCalcSequenceLock(t *testing.T) {
}},
}),
view: utxoView,
mempool: true,
want: &blockchain.SequenceLock{
Seconds: -1,
BlockHeight: 302,
BlockHeight: nextBlockHeight + 1,
},
},
// A transaction with a single unconfirmed input. The input has
@ -463,6 +466,7 @@ func TestCalcSequenceLock(t *testing.T) {
}},
}),
view: utxoView,
mempool: true,
want: &blockchain.SequenceLock{
Seconds: nextMedianTime + 1023,
BlockHeight: -1,