input/script_utils test: add test cases for delayed HTLC receiver scrpts

This commit is contained in:
Johan T. Halseth 2020-03-06 16:11:45 +01:00
parent dc271a80cb
commit a309132253
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -613,12 +613,12 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
aliceSenderSig []byte aliceSenderSig []byte
) )
genCommitTx := func() { genCommitTx := func(confirmed bool) {
// Generate the raw HTLC redemption scripts, and its p2wsh // Generate the raw HTLC redemption scripts, and its p2wsh
// counterpart. // counterpart.
htlcWitnessScript, err = ReceiverHTLCScript( htlcWitnessScript, err = ReceiverHTLCScript(
cltvTimeout, aliceLocalKey, bobLocalKey, revocationKey, cltvTimeout, aliceLocalKey, bobLocalKey, revocationKey,
paymentHash[:], false, paymentHash[:], confirmed,
) )
if err != nil { if err != nil {
t.Fatalf("unable to create htlc sender script: %v", err) t.Fatalf("unable to create htlc sender script: %v", err)
@ -641,7 +641,7 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
receiverCommitTx.AddTxOut(htlcOutput) receiverCommitTx.AddTxOut(htlcOutput)
} }
genSweepTx := func() { genSweepTx := func(confirmed bool) {
prevOut := &wire.OutPoint{ prevOut := &wire.OutPoint{
Hash: receiverCommitTx.TxHash(), Hash: receiverCommitTx.TxHash(),
Index: 0, Index: 0,
@ -651,6 +651,9 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
sweepTx.AddTxIn(&wire.TxIn{ sweepTx.AddTxIn(&wire.TxIn{
PreviousOutPoint: *prevOut, PreviousOutPoint: *prevOut,
}) })
if confirmed {
sweepTx.TxIn[0].Sequence = LockTimeToSequence(false, 1)
}
sweepTx.AddTxOut( sweepTx.AddTxOut(
&wire.TxOut{ &wire.TxOut{
@ -688,8 +691,8 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
{ {
// HTLC redemption w/ invalid preimage size // HTLC redemption w/ invalid preimage size
makeWitnessTestCase(t, func() (wire.TxWitness, error) { makeWitnessTestCase(t, func() (wire.TxWitness, error) {
genCommitTx() genCommitTx(false)
genSweepTx() genSweepTx(false)
signDesc := &SignDescriptor{ signDesc := &SignDescriptor{
KeyDesc: keychain.KeyDescriptor{ KeyDesc: keychain.KeyDescriptor{
@ -713,8 +716,8 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
{ {
// HTLC redemption w/ valid preimage size // HTLC redemption w/ valid preimage size
makeWitnessTestCase(t, func() (wire.TxWitness, error) { makeWitnessTestCase(t, func() (wire.TxWitness, error) {
genCommitTx() genCommitTx(false)
genSweepTx() genSweepTx(false)
signDesc := &SignDescriptor{ signDesc := &SignDescriptor{
KeyDesc: keychain.KeyDescriptor{ KeyDesc: keychain.KeyDescriptor{
@ -737,8 +740,8 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
{ {
// revoke w/ sig // revoke w/ sig
makeWitnessTestCase(t, func() (wire.TxWitness, error) { makeWitnessTestCase(t, func() (wire.TxWitness, error) {
genCommitTx() genCommitTx(false)
genSweepTx() genSweepTx(false)
signDesc := &SignDescriptor{ signDesc := &SignDescriptor{
KeyDesc: keychain.KeyDescriptor{ KeyDesc: keychain.KeyDescriptor{
@ -757,11 +760,71 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
}), }),
true, true,
}, },
{
// HTLC redemption w/ valid preimage size, and with
// enforced locktime in HTLC scripts.
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
// Make a commit tx that needs confirmation for
// HTLC output to be spent.
genCommitTx(true)
// Generate a sweep with the locktime set.
genSweepTx(true)
signDesc := &SignDescriptor{
KeyDesc: keychain.KeyDescriptor{
PubKey: bobKeyPub,
},
SingleTweak: bobCommitTweak,
WitnessScript: htlcWitnessScript,
Output: htlcOutput,
HashType: txscript.SigHashAll,
SigHashes: sweepTxSigHashes,
InputIndex: 0,
}
return ReceiverHtlcSpendRedeem(aliceSenderSig,
paymentPreimage, bobSigner,
signDesc, sweepTx)
}),
true,
},
{
// HTLC redemption w/ valid preimage size, but trying
// to spend CSV output without sequence set.
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
// Generate commitment tx with 1 CSV locked
// HTLC.
genCommitTx(true)
// Generate sweep tx that doesn't have locktime
// enabled.
genSweepTx(false)
signDesc := &SignDescriptor{
KeyDesc: keychain.KeyDescriptor{
PubKey: bobKeyPub,
},
SingleTweak: bobCommitTweak,
WitnessScript: htlcWitnessScript,
Output: htlcOutput,
HashType: txscript.SigHashAll,
SigHashes: sweepTxSigHashes,
InputIndex: 0,
}
return ReceiverHtlcSpendRedeem(aliceSenderSig,
paymentPreimage, bobSigner,
signDesc, sweepTx)
}),
false,
},
{ {
// refund w/ invalid lock time // refund w/ invalid lock time
makeWitnessTestCase(t, func() (wire.TxWitness, error) { makeWitnessTestCase(t, func() (wire.TxWitness, error) {
genCommitTx() genCommitTx(false)
genSweepTx() genSweepTx(false)
signDesc := &SignDescriptor{ signDesc := &SignDescriptor{
KeyDesc: keychain.KeyDescriptor{ KeyDesc: keychain.KeyDescriptor{
@ -783,8 +846,8 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
{ {
// refund w/ valid lock time // refund w/ valid lock time
makeWitnessTestCase(t, func() (wire.TxWitness, error) { makeWitnessTestCase(t, func() (wire.TxWitness, error) {
genCommitTx() genCommitTx(false)
genSweepTx() genSweepTx(false)
signDesc := &SignDescriptor{ signDesc := &SignDescriptor{
KeyDesc: keychain.KeyDescriptor{ KeyDesc: keychain.KeyDescriptor{
@ -803,6 +866,63 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
}), }),
true, true,
}, },
{
// refund w/ valid lock time, and enforced locktime in
// HTLC scripts.
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
// Make a commit tx that needs confirmation for
// HTLC output to be spent.
genCommitTx(true)
// Generate a sweep with the locktime set.
genSweepTx(true)
signDesc := &SignDescriptor{
KeyDesc: keychain.KeyDescriptor{
PubKey: aliceKeyPub,
},
SingleTweak: aliceCommitTweak,
WitnessScript: htlcWitnessScript,
Output: htlcOutput,
HashType: txscript.SigHashAll,
SigHashes: sweepTxSigHashes,
InputIndex: 0,
}
return ReceiverHtlcSpendTimeout(aliceSigner, signDesc,
sweepTx, int32(cltvTimeout))
}),
true,
},
{
// refund w/ valid lock time, but no sequence set in
// sweep tx trying to spend CSV locked HTLC output.
makeWitnessTestCase(t, func() (wire.TxWitness, error) {
// Generate commitment tx with 1 CSV locked
// HTLC.
genCommitTx(true)
// Generate sweep tx that doesn't have locktime
// enabled.
genSweepTx(false)
signDesc := &SignDescriptor{
KeyDesc: keychain.KeyDescriptor{
PubKey: aliceKeyPub,
},
SingleTweak: aliceCommitTweak,
WitnessScript: htlcWitnessScript,
Output: htlcOutput,
HashType: txscript.SigHashAll,
SigHashes: sweepTxSigHashes,
InputIndex: 0,
}
return ReceiverHtlcSpendTimeout(aliceSigner, signDesc,
sweepTx, int32(cltvTimeout))
}),
false,
},
} }
for i, testCase := range testCases { for i, testCase := range testCases {