input/script_utils test: add test cases for delayed HTLC sender script

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

View File

@ -229,13 +229,14 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
bobRecvrSig []byte bobRecvrSig []byte
) )
// genCommitTx generates a commitment tx. // genCommitTx generates a commitment tx where the htlc output requires
genCommitTx := func() { // confirmation to be spent according to 'confirmed'.
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 = SenderHTLCScript( htlcWitnessScript, err = SenderHTLCScript(
aliceLocalKey, bobLocalKey, revocationKey, 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)
@ -258,8 +259,9 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
senderCommitTx.AddTxOut(htlcOutput) senderCommitTx.AddTxOut(htlcOutput)
} }
// genSweepTx generates a sweep of the senderCommitTx. // genSweepTx generates a sweep of the senderCommitTx, and sets the
genSweepTx := func() { // sequence if confirmed is true.
genSweepTx := func(confirmed bool) {
prevOut := &wire.OutPoint{ prevOut := &wire.OutPoint{
Hash: senderCommitTx.TxHash(), Hash: senderCommitTx.TxHash(),
Index: 0, Index: 0,
@ -268,6 +270,9 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
sweepTx = wire.NewMsgTx(2) sweepTx = wire.NewMsgTx(2)
sweepTx.AddTxIn(wire.NewTxIn(prevOut, nil, nil)) sweepTx.AddTxIn(wire.NewTxIn(prevOut, nil, nil))
if confirmed {
sweepTx.TxIn[0].Sequence = LockTimeToSequence(false, 1)
}
sweepTx.AddTxOut( sweepTx.AddTxOut(
&wire.TxOut{ &wire.TxOut{
@ -306,8 +311,8 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
// revoke w/ sig // revoke w/ sig
// TODO(roasbeef): test invalid revoke // TODO(roasbeef): test invalid revoke
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{
@ -329,8 +334,8 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
{ {
// HTLC with invalid preimage size // HTLC with 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{
@ -355,8 +360,8 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
// HTLC with valid preimage size + sig // HTLC with valid preimage size + sig
// TODO(roasbeef): invalid preimage // TODO(roasbeef): invalid preimage
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{
@ -375,13 +380,71 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
}), }),
true, true,
}, },
{
// HTLC with valid preimage size + sig, and with
// enforced locktime in HTLC script.
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 SenderHtlcSpendRedeem(bobSigner, signDesc,
sweepTx, paymentPreimage)
}),
true,
},
{
// HTLC with valid preimage size + sig, 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 SenderHtlcSpendRedeem(bobSigner, signDesc,
sweepTx, paymentPreimage)
}),
false,
},
{ {
// valid spend to the transition the state of the HTLC // valid spend to the transition the state of the HTLC
// output with the second level HTLC timeout // output with the second level HTLC timeout
// transaction. // transaction.
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{
@ -400,6 +463,65 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
}), }),
true, true,
}, },
{
// valid spend to the transition the state of the HTLC
// output with the second level HTLC timeout
// transaction.
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 SenderHtlcSpendTimeout(bobRecvrSig, aliceSigner,
signDesc, sweepTx)
}),
true,
},
{
// valid spend to the transition the state of the HTLC
// output with the second level HTLC timeout
// transaction.
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 SenderHtlcSpendTimeout(bobRecvrSig, aliceSigner,
signDesc, sweepTx)
}),
false,
},
} }
// TODO(roasbeef): set of cases to ensure able to sign w/ keypath and // TODO(roasbeef): set of cases to ensure able to sign w/ keypath and