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