2016-08-08 21:04:33 +02:00
|
|
|
// Copyright (c) 2013-2016 The btcsuite developers
|
2013-10-10 01:43:22 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-30 21:54:30 +01:00
|
|
|
package blockchain_test
|
2013-10-10 01:43:22 +02:00
|
|
|
|
|
|
|
import (
|
2016-11-13 04:44:41 +01:00
|
|
|
"bytes"
|
2014-07-02 18:04:59 +02:00
|
|
|
"testing"
|
2016-11-13 04:44:41 +01:00
|
|
|
"time"
|
2014-07-02 18:04:59 +02:00
|
|
|
|
2015-01-30 21:54:30 +01:00
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
2016-11-13 04:44:41 +01:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2015-02-06 06:18:27 +01:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
2016-08-08 21:04:33 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2016-10-26 18:55:49 +02:00
|
|
|
"github.com/btcsuite/btcd/integration/rpctest"
|
2016-08-24 23:14:42 +02:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2015-01-15 17:23:47 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
2013-10-10 01:43:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestHaveBlock tests the HaveBlock API to ensure proper functionality.
|
|
|
|
func TestHaveBlock(t *testing.T) {
|
|
|
|
// Load up blocks such that there is a side chain.
|
|
|
|
// (genesis block) -> 1 -> 2 -> 3 -> 4
|
|
|
|
// \-> 3a
|
|
|
|
testFiles := []string{
|
|
|
|
"blk_0_to_4.dat.bz2",
|
|
|
|
"blk_3A.dat.bz2",
|
|
|
|
}
|
|
|
|
|
|
|
|
var blocks []*btcutil.Block
|
|
|
|
for _, file := range testFiles {
|
|
|
|
blockTmp, err := loadBlocks(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error loading file: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2016-11-03 05:02:04 +01:00
|
|
|
blocks = append(blocks, blockTmp...)
|
2013-10-10 01:43:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new database and chain instance to run tests against.
|
2016-09-14 01:11:12 +02:00
|
|
|
chain, teardownFunc, err := chainSetup("haveblock",
|
|
|
|
&chaincfg.MainNetParams)
|
2013-10-10 01:43:22 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to setup chain instance: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer teardownFunc()
|
|
|
|
|
2017-01-18 23:58:38 +01:00
|
|
|
// Since we're not dealing with the real block chain, set the coinbase
|
|
|
|
// maturity to 1.
|
2016-08-10 23:02:23 +02:00
|
|
|
chain.TstSetCoinbaseMaturity(1)
|
2013-10-10 01:43:22 +02:00
|
|
|
|
|
|
|
for i := 1; i < len(blocks); i++ {
|
2016-10-13 02:43:01 +02:00
|
|
|
_, isOrphan, err := chain.ProcessBlock(blocks[i], blockchain.BFNone)
|
2013-10-10 01:43:22 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ProcessBlock fail on block %v: %v\n", i, err)
|
|
|
|
return
|
|
|
|
}
|
2014-06-25 22:47:24 +02:00
|
|
|
if isOrphan {
|
|
|
|
t.Errorf("ProcessBlock incorrectly returned block %v "+
|
|
|
|
"is an orphan\n", i)
|
|
|
|
return
|
|
|
|
}
|
2013-10-10 01:43:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert an orphan block.
|
2016-10-13 02:43:01 +02:00
|
|
|
_, isOrphan, err := chain.ProcessBlock(btcutil.NewBlock(&Block100000),
|
2016-07-14 02:36:36 +02:00
|
|
|
blockchain.BFNone)
|
2014-06-25 22:47:24 +02:00
|
|
|
if err != nil {
|
2013-10-10 01:43:22 +02:00
|
|
|
t.Errorf("Unable to process block: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-06-25 22:47:24 +02:00
|
|
|
if !isOrphan {
|
|
|
|
t.Errorf("ProcessBlock indicated block is an not orphan when " +
|
|
|
|
"it should be\n")
|
|
|
|
return
|
|
|
|
}
|
2013-10-10 01:43:22 +02:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
hash string
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
// Genesis block should be present (in the main chain).
|
2015-02-06 06:18:27 +01:00
|
|
|
{hash: chaincfg.MainNetParams.GenesisHash.String(), want: true},
|
2013-10-10 01:43:22 +02:00
|
|
|
|
|
|
|
// Block 3a should be present (on a side chain).
|
|
|
|
{hash: "00000000474284d20067a4d33f6a02284e6ef70764a3a26d6a5b9df52ef663dd", want: true},
|
|
|
|
|
|
|
|
// Block 100000 should be present (as an orphan).
|
|
|
|
{hash: "000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506", want: true},
|
|
|
|
|
2016-02-25 18:17:12 +01:00
|
|
|
// Random hashes should not be available.
|
2013-10-10 01:43:22 +02:00
|
|
|
{hash: "123", want: false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2016-08-08 21:04:33 +02:00
|
|
|
hash, err := chainhash.NewHashFromStr(test.hash)
|
2013-10-10 01:43:22 +02:00
|
|
|
if err != nil {
|
2016-08-08 21:04:33 +02:00
|
|
|
t.Errorf("NewHashFromStr: %v", err)
|
2013-10-10 01:43:22 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-07-07 18:42:28 +02:00
|
|
|
result, err := chain.HaveBlock(hash)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("HaveBlock #%d unexpected error: %v", i, err)
|
|
|
|
return
|
|
|
|
}
|
2013-10-10 01:43:22 +02:00
|
|
|
if result != test.want {
|
|
|
|
t.Errorf("HaveBlock #%d got %v want %v", i, result,
|
|
|
|
test.want)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-24 23:14:42 +02:00
|
|
|
|
|
|
|
// TestCalcSequenceLock tests the LockTimeToSequence function, and the
|
|
|
|
// CalcSequenceLock method of a Chain instance. The tests exercise several
|
|
|
|
// combinations of inputs to the CalcSequenceLock function in order to ensure
|
|
|
|
// the returned SequenceLocks are correct for each test instance.
|
|
|
|
func TestCalcSequenceLock(t *testing.T) {
|
2016-11-13 04:44:41 +01:00
|
|
|
netParams := &chaincfg.SimNetParams
|
2016-08-24 23:14:42 +02:00
|
|
|
|
|
|
|
// Create a new database and chain instance to run tests against.
|
2016-09-27 04:00:28 +02:00
|
|
|
chain, teardownFunc, err := chainSetup("calcseqlock", netParams)
|
2016-08-24 23:14:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to setup chain instance: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer teardownFunc()
|
|
|
|
|
2017-01-18 23:58:38 +01:00
|
|
|
// Since we're not dealing with the real block chain, set the coinbase
|
|
|
|
// maturity to 1.
|
2016-08-24 23:14:42 +02:00
|
|
|
chain.TstSetCoinbaseMaturity(1)
|
|
|
|
|
2016-11-13 04:44:41 +01:00
|
|
|
// Create a test mining address to use for the blocks we'll generate
|
|
|
|
// shortly below.
|
|
|
|
k := bytes.Repeat([]byte{1}, 32)
|
|
|
|
_, miningPub := btcec.PrivKeyFromBytes(btcec.S256(), k)
|
|
|
|
miningAddr, err := btcutil.NewAddressPubKey(miningPub.SerializeCompressed(),
|
|
|
|
netParams)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to generate mining addr: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll keep track of the previous block for back pointers in blocks
|
|
|
|
// we generated, and also the generated blocks along with the MTP from
|
|
|
|
// their PoV to aide with our relative time lock calculations.
|
|
|
|
var prevBlock *btcutil.Block
|
|
|
|
var blocksWithMTP []struct {
|
|
|
|
block *btcutil.Block
|
|
|
|
mtp time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to activate CSV in order to test the processing logic, so
|
|
|
|
// manually craft the block version that's used to signal the soft-fork
|
|
|
|
// activation.
|
|
|
|
csvBit := netParams.Deployments[chaincfg.DeploymentCSV].BitNumber
|
|
|
|
blockVersion := int32(0x20000000 | (uint32(1) << csvBit))
|
|
|
|
|
|
|
|
// Generate enough blocks to activate CSV, collecting each of the
|
|
|
|
// blocks into a slice for later use.
|
|
|
|
numBlocksToActivate := (netParams.MinerConfirmationWindow * 3)
|
|
|
|
for i := uint32(0); i < numBlocksToActivate; i++ {
|
|
|
|
block, err := rpctest.CreateBlock(prevBlock, nil, blockVersion,
|
|
|
|
time.Time{}, miningAddr, netParams)
|
2016-08-24 23:14:42 +02:00
|
|
|
if err != nil {
|
2016-11-13 04:44:41 +01:00
|
|
|
t.Fatalf("unable to generate block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mtp := chain.BestSnapshot().MedianTime
|
|
|
|
|
|
|
|
_, isOrphan, err := chain.ProcessBlock(block, blockchain.BFNone)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ProcessBlock fail on block %v: %v\n", i, err)
|
2016-08-24 23:14:42 +02:00
|
|
|
}
|
|
|
|
if isOrphan {
|
2016-11-13 04:44:41 +01:00
|
|
|
t.Fatalf("ProcessBlock incorrectly returned block %v "+
|
2016-08-24 23:14:42 +02:00
|
|
|
"is an orphan\n", i)
|
|
|
|
}
|
2016-11-13 04:44:41 +01:00
|
|
|
|
|
|
|
blocksWithMTP = append(blocksWithMTP, struct {
|
|
|
|
block *btcutil.Block
|
|
|
|
mtp time.Time
|
|
|
|
}{
|
|
|
|
block: block,
|
|
|
|
mtp: mtp,
|
|
|
|
})
|
|
|
|
|
|
|
|
prevBlock = block
|
2016-08-24 23:14:42 +02:00
|
|
|
}
|
|
|
|
|
2016-09-27 04:00:28 +02:00
|
|
|
// Create a utxo view with all the utxos within the blocks created
|
|
|
|
// above.
|
2016-08-24 23:14:42 +02:00
|
|
|
utxoView := blockchain.NewUtxoViewpoint()
|
2016-11-13 04:44:41 +01:00
|
|
|
for blockHeight, blockWithMTP := range blocksWithMTP {
|
|
|
|
for _, tx := range blockWithMTP.block.Transactions() {
|
2016-08-24 23:14:42 +02:00
|
|
|
utxoView.AddTxOuts(tx, int32(blockHeight))
|
|
|
|
}
|
|
|
|
}
|
2016-11-13 04:44:41 +01:00
|
|
|
utxoView.SetBestHash(blocksWithMTP[len(blocksWithMTP)-1].block.Hash())
|
2016-08-24 23:14:42 +02:00
|
|
|
|
2016-11-13 04:44:41 +01:00
|
|
|
// The median time calculated from the PoV of the best block in our
|
2016-09-27 04:00:28 +02:00
|
|
|
// 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.
|
2016-11-13 04:44:41 +01:00
|
|
|
nextMedianTime := int64(1401292712)
|
2016-08-24 23:14:42 +02:00
|
|
|
|
|
|
|
// We'll refer to this utxo within each input in the transactions
|
2016-11-13 04:44:41 +01:00
|
|
|
// created below. This utxo has an age of 4 blocks and was mined within
|
|
|
|
// block 297
|
|
|
|
targetTx := blocksWithMTP[len(blocksWithMTP)-4].block.Transactions()[0]
|
2016-08-24 23:14:42 +02:00
|
|
|
utxo := wire.OutPoint{
|
|
|
|
Hash: *targetTx.Hash(),
|
|
|
|
Index: 0,
|
|
|
|
}
|
|
|
|
|
2016-11-13 04:44:41 +01:00
|
|
|
// 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()
|
|
|
|
|
2016-08-24 23:14:42 +02:00
|
|
|
// Add an additional transaction which will serve as our unconfirmed
|
|
|
|
// output.
|
|
|
|
var fakeScript []byte
|
|
|
|
unConfTx := &wire.MsgTx{
|
2016-10-28 06:16:07 +02:00
|
|
|
TxOut: []*wire.TxOut{{
|
|
|
|
PkScript: fakeScript,
|
|
|
|
Value: 5,
|
|
|
|
}},
|
2016-08-24 23:14:42 +02:00
|
|
|
}
|
|
|
|
unConfUtxo := wire.OutPoint{
|
|
|
|
Hash: unConfTx.TxHash(),
|
|
|
|
Index: 0,
|
|
|
|
}
|
2016-11-13 04:44:41 +01:00
|
|
|
|
2016-08-24 23:14:42 +02:00
|
|
|
// Adding a utxo with a height of 0x7fffffff indicates that the output
|
|
|
|
// is currently unmined.
|
|
|
|
utxoView.AddTxOuts(btcutil.NewTx(unConfTx), 0x7fffffff)
|
|
|
|
|
|
|
|
tests := []struct {
|
2016-10-28 06:16:07 +02:00
|
|
|
tx *btcutil.Tx
|
|
|
|
view *blockchain.UtxoViewpoint
|
|
|
|
want *blockchain.SequenceLock
|
2016-08-24 23:14:42 +02:00
|
|
|
mempool bool
|
|
|
|
}{
|
|
|
|
// A transaction of version one should disable sequence locks
|
|
|
|
// as the new sequence number semantics only apply to
|
|
|
|
// transactions version 2 or higher.
|
|
|
|
{
|
|
|
|
tx: btcutil.NewTx(&wire.MsgTx{
|
|
|
|
Version: 1,
|
2016-10-28 06:16:07 +02:00
|
|
|
TxIn: []*wire.TxIn{{
|
|
|
|
PreviousOutPoint: utxo,
|
|
|
|
Sequence: blockchain.LockTimeToSequence(false, 3),
|
|
|
|
}},
|
2016-08-24 23:14:42 +02:00
|
|
|
}),
|
|
|
|
view: utxoView,
|
|
|
|
want: &blockchain.SequenceLock{
|
|
|
|
Seconds: -1,
|
|
|
|
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.
|
|
|
|
{
|
|
|
|
tx: btcutil.NewTx(&wire.MsgTx{
|
|
|
|
Version: 2,
|
2016-10-28 06:16:07 +02:00
|
|
|
TxIn: []*wire.TxIn{{
|
|
|
|
PreviousOutPoint: utxo,
|
|
|
|
Sequence: wire.MaxTxInSequenceNum,
|
|
|
|
}},
|
2016-08-24 23:14:42 +02:00
|
|
|
}),
|
|
|
|
view: utxoView,
|
|
|
|
want: &blockchain.SequenceLock{
|
|
|
|
Seconds: -1,
|
|
|
|
BlockHeight: -1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// A transaction with a single input whose lock time is
|
|
|
|
// expressed in seconds. However, the specified lock time is
|
|
|
|
// below the required floor for time based lock times since
|
|
|
|
// they have time granularity of 512 seconds. As a result, the
|
|
|
|
// seconds lock-time should be just before the median time of
|
|
|
|
// the targeted block.
|
|
|
|
{
|
|
|
|
tx: btcutil.NewTx(&wire.MsgTx{
|
|
|
|
Version: 2,
|
2016-10-28 06:16:07 +02:00
|
|
|
TxIn: []*wire.TxIn{{
|
|
|
|
PreviousOutPoint: utxo,
|
|
|
|
Sequence: blockchain.LockTimeToSequence(true, 2),
|
|
|
|
}},
|
2016-08-24 23:14:42 +02:00
|
|
|
}),
|
|
|
|
view: utxoView,
|
|
|
|
want: &blockchain.SequenceLock{
|
|
|
|
Seconds: medianTime - 1,
|
|
|
|
BlockHeight: -1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// A transaction with a single input whose lock time is
|
|
|
|
// expressed in seconds. The number of seconds should be 1023
|
|
|
|
// seconds after the median past time of the last block in the
|
|
|
|
// chain.
|
|
|
|
{
|
|
|
|
tx: btcutil.NewTx(&wire.MsgTx{
|
|
|
|
Version: 2,
|
2016-10-28 06:16:07 +02:00
|
|
|
TxIn: []*wire.TxIn{{
|
|
|
|
PreviousOutPoint: utxo,
|
|
|
|
Sequence: blockchain.LockTimeToSequence(true, 1024),
|
|
|
|
}},
|
2016-08-24 23:14:42 +02:00
|
|
|
}),
|
|
|
|
view: utxoView,
|
|
|
|
want: &blockchain.SequenceLock{
|
|
|
|
Seconds: medianTime + 1023,
|
|
|
|
BlockHeight: -1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// A transaction with multiple inputs. The first 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.
|
|
|
|
{
|
|
|
|
tx: btcutil.NewTx(&wire.MsgTx{
|
|
|
|
Version: 2,
|
2016-10-28 06:16:07 +02:00
|
|
|
TxIn: []*wire.TxIn{{
|
|
|
|
PreviousOutPoint: utxo,
|
|
|
|
Sequence: blockchain.LockTimeToSequence(true, 2560),
|
|
|
|
}, {
|
|
|
|
PreviousOutPoint: utxo,
|
2016-11-13 04:44:41 +01:00
|
|
|
Sequence: blockchain.LockTimeToSequence(false, 5) |
|
2016-10-28 06:16:07 +02:00
|
|
|
wire.SequenceLockTimeDisabled,
|
|
|
|
}, {
|
|
|
|
PreviousOutPoint: utxo,
|
2016-11-13 04:44:41 +01:00
|
|
|
Sequence: blockchain.LockTimeToSequence(false, 4),
|
2016-10-28 06:16:07 +02:00
|
|
|
}},
|
2016-08-24 23:14:42 +02:00
|
|
|
}),
|
|
|
|
view: utxoView,
|
|
|
|
want: &blockchain.SequenceLock{
|
|
|
|
Seconds: medianTime + (5 << wire.SequenceLockTimeGranularity) - 1,
|
2016-11-13 04:44:41 +01:00
|
|
|
BlockHeight: 299,
|
2016-08-24 23:14:42 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// 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
|
2016-11-13 04:44:41 +01:00
|
|
|
// 298 meaning it can be included at height 299.
|
2016-08-24 23:14:42 +02:00
|
|
|
{
|
|
|
|
tx: btcutil.NewTx(&wire.MsgTx{
|
|
|
|
Version: 2,
|
2016-10-28 06:16:07 +02:00
|
|
|
TxIn: []*wire.TxIn{{
|
|
|
|
PreviousOutPoint: utxo,
|
|
|
|
Sequence: blockchain.LockTimeToSequence(false, 3),
|
|
|
|
}},
|
2016-08-24 23:14:42 +02:00
|
|
|
}),
|
|
|
|
view: utxoView,
|
|
|
|
want: &blockchain.SequenceLock{
|
|
|
|
Seconds: -1,
|
2016-11-13 04:44:41 +01:00
|
|
|
BlockHeight: 298,
|
2016-08-24 23:14:42 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// A transaction with two inputs with lock times expressed in
|
|
|
|
// seconds. The selected sequence lock value for seconds should
|
|
|
|
// be the time further in the future.
|
|
|
|
{
|
|
|
|
tx: btcutil.NewTx(&wire.MsgTx{
|
|
|
|
Version: 2,
|
2016-10-28 06:16:07 +02:00
|
|
|
TxIn: []*wire.TxIn{{
|
|
|
|
PreviousOutPoint: utxo,
|
|
|
|
Sequence: blockchain.LockTimeToSequence(true, 5120),
|
|
|
|
}, {
|
|
|
|
PreviousOutPoint: utxo,
|
|
|
|
Sequence: blockchain.LockTimeToSequence(true, 2560),
|
|
|
|
}},
|
2016-08-24 23:14:42 +02:00
|
|
|
}),
|
|
|
|
view: utxoView,
|
|
|
|
want: &blockchain.SequenceLock{
|
|
|
|
Seconds: medianTime + (10 << wire.SequenceLockTimeGranularity) - 1,
|
|
|
|
BlockHeight: -1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// A transaction with two inputs with lock times expressed in
|
|
|
|
// seconds. The selected sequence lock value for blocks should
|
2016-11-13 04:44:41 +01:00
|
|
|
// be the height further in the future. The converted absolute
|
|
|
|
// block height should be 302, meaning it can be included in
|
|
|
|
// block 303.
|
2016-08-24 23:14:42 +02:00
|
|
|
{
|
|
|
|
tx: btcutil.NewTx(&wire.MsgTx{
|
|
|
|
Version: 2,
|
2016-10-28 06:16:07 +02:00
|
|
|
TxIn: []*wire.TxIn{{
|
|
|
|
PreviousOutPoint: utxo,
|
|
|
|
Sequence: blockchain.LockTimeToSequence(false, 1),
|
|
|
|
}, {
|
|
|
|
PreviousOutPoint: utxo,
|
|
|
|
Sequence: blockchain.LockTimeToSequence(false, 7),
|
|
|
|
}},
|
2016-08-24 23:14:42 +02:00
|
|
|
}),
|
|
|
|
view: utxoView,
|
|
|
|
want: &blockchain.SequenceLock{
|
|
|
|
Seconds: -1,
|
2016-11-13 04:44:41 +01:00
|
|
|
BlockHeight: 302,
|
2016-08-24 23:14:42 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// 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.
|
|
|
|
{
|
|
|
|
tx: btcutil.NewTx(&wire.MsgTx{
|
|
|
|
Version: 2,
|
2016-10-28 06:16:07 +02:00
|
|
|
TxIn: []*wire.TxIn{{
|
|
|
|
PreviousOutPoint: utxo,
|
|
|
|
Sequence: blockchain.LockTimeToSequence(true, 2560),
|
|
|
|
}, {
|
|
|
|
PreviousOutPoint: utxo,
|
|
|
|
Sequence: blockchain.LockTimeToSequence(true, 6656),
|
|
|
|
}, {
|
|
|
|
PreviousOutPoint: utxo,
|
|
|
|
Sequence: blockchain.LockTimeToSequence(false, 3),
|
|
|
|
}, {
|
|
|
|
PreviousOutPoint: utxo,
|
|
|
|
Sequence: blockchain.LockTimeToSequence(false, 9),
|
|
|
|
}},
|
2016-08-24 23:14:42 +02:00
|
|
|
}),
|
|
|
|
view: utxoView,
|
|
|
|
want: &blockchain.SequenceLock{
|
|
|
|
Seconds: medianTime + (13 << wire.SequenceLockTimeGranularity) - 1,
|
2016-11-13 04:44:41 +01:00
|
|
|
BlockHeight: 304,
|
2016-08-24 23:14:42 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// A transaction with a single unconfirmed input. As the input
|
|
|
|
// is confirmed, the height of the input should be interpreted
|
2016-11-13 04:44:41 +01:00
|
|
|
// 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.
|
2016-08-24 23:14:42 +02:00
|
|
|
{
|
|
|
|
tx: btcutil.NewTx(&wire.MsgTx{
|
|
|
|
Version: 2,
|
2016-10-28 06:16:07 +02:00
|
|
|
TxIn: []*wire.TxIn{{
|
|
|
|
PreviousOutPoint: unConfUtxo,
|
|
|
|
Sequence: blockchain.LockTimeToSequence(false, 2),
|
|
|
|
}},
|
2016-08-24 23:14:42 +02:00
|
|
|
}),
|
|
|
|
view: utxoView,
|
|
|
|
want: &blockchain.SequenceLock{
|
|
|
|
Seconds: -1,
|
2016-11-13 04:44:41 +01:00
|
|
|
BlockHeight: 302,
|
2016-08-24 23:14:42 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// A transaction with a single unconfirmed input. The input has
|
|
|
|
// a time based lock, so the lock time should be based off the
|
|
|
|
// MTP of the *next* block.
|
|
|
|
{
|
|
|
|
tx: btcutil.NewTx(&wire.MsgTx{
|
|
|
|
Version: 2,
|
2016-10-28 06:16:07 +02:00
|
|
|
TxIn: []*wire.TxIn{{
|
|
|
|
PreviousOutPoint: unConfUtxo,
|
|
|
|
Sequence: blockchain.LockTimeToSequence(true, 1024),
|
|
|
|
}},
|
2016-08-24 23:14:42 +02:00
|
|
|
}),
|
|
|
|
view: utxoView,
|
|
|
|
want: &blockchain.SequenceLock{
|
|
|
|
Seconds: nextMedianTime + 1023,
|
|
|
|
BlockHeight: -1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Running %v SequenceLock tests", len(tests))
|
|
|
|
for i, test := range tests {
|
|
|
|
seqLock, err := chain.CalcSequenceLock(test.tx, test.view, test.mempool)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("test #%d, unable to calc sequence lock: %v", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if seqLock.Seconds != test.want.Seconds {
|
|
|
|
t.Fatalf("test #%d got %v seconds want %v seconds",
|
|
|
|
i, seqLock.Seconds, test.want.Seconds)
|
|
|
|
}
|
|
|
|
if seqLock.BlockHeight != test.want.BlockHeight {
|
|
|
|
t.Fatalf("test #%d got height of %v want height of %v ",
|
|
|
|
i, seqLock.BlockHeight, test.want.BlockHeight)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|