input/script_utils_test: refactor TestHTLCSenderSpendValidation

To be able to change more than the witness used for each test case, we
extract commit and sweep tx generation into own methods that can be
called from each test case.

We do the same for TestHTLCReceiverSpendValidation
This commit is contained in:
Johan T. Halseth 2020-03-06 16:11:45 +01:00
parent b56c7e308b
commit b228681a02
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -212,43 +212,6 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
// we'll be using Bob's base point for the revocation key. // we'll be using Bob's base point for the revocation key.
revocationKey := DeriveRevocationPubkey(bobKeyPub, commitPoint) revocationKey := DeriveRevocationPubkey(bobKeyPub, commitPoint)
// Generate the raw HTLC redemption scripts, and its p2wsh counterpart.
htlcWitnessScript, err := SenderHTLCScript(aliceLocalKey, bobLocalKey,
revocationKey, paymentHash[:])
if err != nil {
t.Fatalf("unable to create htlc sender script: %v", err)
}
htlcPkScript, err := WitnessScriptHash(htlcWitnessScript)
if err != nil {
t.Fatalf("unable to create p2wsh htlc script: %v", err)
}
// This will be Alice's commitment transaction. In this scenario Alice
// is sending an HTLC to a node she has a path to (could be Bob, could
// be multiple hops down, it doesn't really matter).
htlcOutput := &wire.TxOut{
Value: int64(paymentAmt),
PkScript: htlcPkScript,
}
senderCommitTx := wire.NewMsgTx(2)
senderCommitTx.AddTxIn(fakeFundingTxIn)
senderCommitTx.AddTxOut(htlcOutput)
prevOut := &wire.OutPoint{
Hash: senderCommitTx.TxHash(),
Index: 0,
}
sweepTx := wire.NewMsgTx(2)
sweepTx.AddTxIn(wire.NewTxIn(prevOut, nil, nil))
sweepTx.AddTxOut(
&wire.TxOut{
PkScript: []byte("doesn't matter"),
Value: 1 * 10e8,
},
)
sweepTxSigHashes := txscript.NewTxSigHashes(sweepTx)
bobCommitTweak := SingleTweakBytes(commitPoint, bobKeyPub) bobCommitTweak := SingleTweakBytes(commitPoint, bobKeyPub)
aliceCommitTweak := SingleTweakBytes(commitPoint, aliceKeyPub) aliceCommitTweak := SingleTweakBytes(commitPoint, aliceKeyPub)
@ -258,23 +221,81 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
bobSigner := &MockSigner{Privkeys: []*btcec.PrivateKey{bobKeyPriv}} bobSigner := &MockSigner{Privkeys: []*btcec.PrivateKey{bobKeyPriv}}
aliceSigner := &MockSigner{Privkeys: []*btcec.PrivateKey{aliceKeyPriv}} aliceSigner := &MockSigner{Privkeys: []*btcec.PrivateKey{aliceKeyPriv}}
// We'll also generate a signature on the sweep transaction above var (
// that will act as Bob's signature to Alice for the second level HTLC htlcWitnessScript, htlcPkScript []byte
// transaction. htlcOutput *wire.TxOut
bobSignDesc := SignDescriptor{ sweepTxSigHashes *txscript.TxSigHashes
KeyDesc: keychain.KeyDescriptor{ senderCommitTx, sweepTx *wire.MsgTx
PubKey: bobKeyPub, bobRecvrSig []byte
}, )
SingleTweak: bobCommitTweak,
WitnessScript: htlcWitnessScript, // genCommitTx generates a commitment tx.
Output: htlcOutput, genCommitTx := func() {
HashType: txscript.SigHashAll, // Generate the raw HTLC redemption scripts, and its p2wsh
SigHashes: sweepTxSigHashes, // counterpart.
InputIndex: 0, htlcWitnessScript, err = SenderHTLCScript(
aliceLocalKey, bobLocalKey, revocationKey,
paymentHash[:],
)
if err != nil {
t.Fatalf("unable to create htlc sender script: %v", err)
}
htlcPkScript, err = WitnessScriptHash(htlcWitnessScript)
if err != nil {
t.Fatalf("unable to create p2wsh htlc script: %v", err)
}
// This will be Alice's commitment transaction. In this
// scenario Alice is sending an HTLC to a node she has a path
// to (could be Bob, could be multiple hops down, it doesn't
// really matter).
htlcOutput = &wire.TxOut{
Value: int64(paymentAmt),
PkScript: htlcPkScript,
}
senderCommitTx = wire.NewMsgTx(2)
senderCommitTx.AddTxIn(fakeFundingTxIn)
senderCommitTx.AddTxOut(htlcOutput)
} }
bobRecvrSig, err := bobSigner.SignOutputRaw(sweepTx, &bobSignDesc)
if err != nil { // genSweepTx generates a sweep of the senderCommitTx.
t.Fatalf("unable to generate alice signature: %v", err) genSweepTx := func() {
prevOut := &wire.OutPoint{
Hash: senderCommitTx.TxHash(),
Index: 0,
}
sweepTx = wire.NewMsgTx(2)
sweepTx.AddTxIn(wire.NewTxIn(prevOut, nil, nil))
sweepTx.AddTxOut(
&wire.TxOut{
PkScript: []byte("doesn't matter"),
Value: 1 * 10e8,
},
)
sweepTxSigHashes = txscript.NewTxSigHashes(sweepTx)
// We'll also generate a signature on the sweep transaction above
// that will act as Bob's signature to Alice for the second level HTLC
// transaction.
bobSignDesc := SignDescriptor{
KeyDesc: keychain.KeyDescriptor{
PubKey: bobKeyPub,
},
SingleTweak: bobCommitTweak,
WitnessScript: htlcWitnessScript,
Output: htlcOutput,
HashType: txscript.SigHashAll,
SigHashes: sweepTxSigHashes,
InputIndex: 0,
}
bobRecvrSig, err = bobSigner.SignOutputRaw(sweepTx, &bobSignDesc)
if err != nil {
t.Fatalf("unable to generate alice signature: %v", err)
}
} }
testCases := []struct { testCases := []struct {
@ -285,6 +306,9 @@ 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()
genSweepTx()
signDesc := &SignDescriptor{ signDesc := &SignDescriptor{
KeyDesc: keychain.KeyDescriptor{ KeyDesc: keychain.KeyDescriptor{
PubKey: bobKeyPub, PubKey: bobKeyPub,
@ -305,6 +329,9 @@ 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()
genSweepTx()
signDesc := &SignDescriptor{ signDesc := &SignDescriptor{
KeyDesc: keychain.KeyDescriptor{ KeyDesc: keychain.KeyDescriptor{
PubKey: bobKeyPub, PubKey: bobKeyPub,
@ -328,6 +355,9 @@ 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()
genSweepTx()
signDesc := &SignDescriptor{ signDesc := &SignDescriptor{
KeyDesc: keychain.KeyDescriptor{ KeyDesc: keychain.KeyDescriptor{
PubKey: bobKeyPub, PubKey: bobKeyPub,
@ -350,6 +380,9 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
// 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()
genSweepTx()
signDesc := &SignDescriptor{ signDesc := &SignDescriptor{
KeyDesc: keychain.KeyDescriptor{ KeyDesc: keychain.KeyDescriptor{
PubKey: aliceKeyPub, PubKey: aliceKeyPub,
@ -441,46 +474,6 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
// be using Alice's base point for the revocation key. // be using Alice's base point for the revocation key.
revocationKey := DeriveRevocationPubkey(aliceKeyPub, commitPoint) revocationKey := DeriveRevocationPubkey(aliceKeyPub, commitPoint)
// Generate the raw HTLC redemption scripts, and its p2wsh counterpart.
htlcWitnessScript, err := ReceiverHTLCScript(cltvTimeout, aliceLocalKey,
bobLocalKey, revocationKey, paymentHash[:])
if err != nil {
t.Fatalf("unable to create htlc sender script: %v", err)
}
htlcPkScript, err := WitnessScriptHash(htlcWitnessScript)
if err != nil {
t.Fatalf("unable to create p2wsh htlc script: %v", err)
}
// This will be Bob's commitment transaction. In this scenario Alice is
// sending an HTLC to a node she has a path to (could be Bob, could be
// multiple hops down, it doesn't really matter).
htlcOutput := &wire.TxOut{
Value: int64(paymentAmt),
PkScript: htlcWitnessScript,
}
receiverCommitTx := wire.NewMsgTx(2)
receiverCommitTx.AddTxIn(fakeFundingTxIn)
receiverCommitTx.AddTxOut(htlcOutput)
prevOut := &wire.OutPoint{
Hash: receiverCommitTx.TxHash(),
Index: 0,
}
sweepTx := wire.NewMsgTx(2)
sweepTx.AddTxIn(&wire.TxIn{
PreviousOutPoint: *prevOut,
})
sweepTx.AddTxOut(
&wire.TxOut{
PkScript: []byte("doesn't matter"),
Value: 1 * 10e8,
},
)
sweepTxSigHashes := txscript.NewTxSigHashes(sweepTx)
bobCommitTweak := SingleTweakBytes(commitPoint, bobKeyPub) bobCommitTweak := SingleTweakBytes(commitPoint, bobKeyPub)
aliceCommitTweak := SingleTweakBytes(commitPoint, aliceKeyPub) aliceCommitTweak := SingleTweakBytes(commitPoint, aliceKeyPub)
@ -490,23 +483,79 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
bobSigner := &MockSigner{Privkeys: []*btcec.PrivateKey{bobKeyPriv}} bobSigner := &MockSigner{Privkeys: []*btcec.PrivateKey{bobKeyPriv}}
aliceSigner := &MockSigner{Privkeys: []*btcec.PrivateKey{aliceKeyPriv}} aliceSigner := &MockSigner{Privkeys: []*btcec.PrivateKey{aliceKeyPriv}}
// We'll also generate a signature on the sweep transaction above var (
// that will act as Alice's signature to Bob for the second level HTLC htlcWitnessScript, htlcPkScript []byte
// transaction. htlcOutput *wire.TxOut
aliceSignDesc := SignDescriptor{ receiverCommitTx, sweepTx *wire.MsgTx
KeyDesc: keychain.KeyDescriptor{ sweepTxSigHashes *txscript.TxSigHashes
PubKey: aliceKeyPub, aliceSenderSig []byte
}, )
SingleTweak: aliceCommitTweak,
WitnessScript: htlcWitnessScript, genCommitTx := func() {
Output: htlcOutput, // Generate the raw HTLC redemption scripts, and its p2wsh
HashType: txscript.SigHashAll, // counterpart.
SigHashes: sweepTxSigHashes, htlcWitnessScript, err = ReceiverHTLCScript(
InputIndex: 0, cltvTimeout, aliceLocalKey, bobLocalKey, revocationKey,
paymentHash[:],
)
if err != nil {
t.Fatalf("unable to create htlc sender script: %v", err)
}
htlcPkScript, err = WitnessScriptHash(htlcWitnessScript)
if err != nil {
t.Fatalf("unable to create p2wsh htlc script: %v", err)
}
// This will be Bob's commitment transaction. In this scenario Alice is
// sending an HTLC to a node she has a path to (could be Bob, could be
// multiple hops down, it doesn't really matter).
htlcOutput = &wire.TxOut{
Value: int64(paymentAmt),
PkScript: htlcWitnessScript,
}
receiverCommitTx = wire.NewMsgTx(2)
receiverCommitTx.AddTxIn(fakeFundingTxIn)
receiverCommitTx.AddTxOut(htlcOutput)
} }
aliceSenderSig, err := aliceSigner.SignOutputRaw(sweepTx, &aliceSignDesc)
if err != nil { genSweepTx := func() {
t.Fatalf("unable to generate alice signature: %v", err) prevOut := &wire.OutPoint{
Hash: receiverCommitTx.TxHash(),
Index: 0,
}
sweepTx = wire.NewMsgTx(2)
sweepTx.AddTxIn(&wire.TxIn{
PreviousOutPoint: *prevOut,
})
sweepTx.AddTxOut(
&wire.TxOut{
PkScript: []byte("doesn't matter"),
Value: 1 * 10e8,
},
)
sweepTxSigHashes = txscript.NewTxSigHashes(sweepTx)
// We'll also generate a signature on the sweep transaction above
// that will act as Alice's signature to Bob for the second level HTLC
// transaction.
aliceSignDesc := SignDescriptor{
KeyDesc: keychain.KeyDescriptor{
PubKey: aliceKeyPub,
},
SingleTweak: aliceCommitTweak,
WitnessScript: htlcWitnessScript,
Output: htlcOutput,
HashType: txscript.SigHashAll,
SigHashes: sweepTxSigHashes,
InputIndex: 0,
}
aliceSenderSig, err = aliceSigner.SignOutputRaw(sweepTx, &aliceSignDesc)
if err != nil {
t.Fatalf("unable to generate alice signature: %v", err)
}
} }
// TODO(roasbeef): modify valid to check precise script errors? // TODO(roasbeef): modify valid to check precise script errors?
@ -517,6 +566,9 @@ 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()
genSweepTx()
signDesc := &SignDescriptor{ signDesc := &SignDescriptor{
KeyDesc: keychain.KeyDescriptor{ KeyDesc: keychain.KeyDescriptor{
PubKey: bobKeyPub, PubKey: bobKeyPub,
@ -539,6 +591,9 @@ 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()
genSweepTx()
signDesc := &SignDescriptor{ signDesc := &SignDescriptor{
KeyDesc: keychain.KeyDescriptor{ KeyDesc: keychain.KeyDescriptor{
PubKey: bobKeyPub, PubKey: bobKeyPub,
@ -560,6 +615,9 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
{ {
// revoke w/ sig // revoke w/ sig
makeWitnessTestCase(t, func() (wire.TxWitness, error) { makeWitnessTestCase(t, func() (wire.TxWitness, error) {
genCommitTx()
genSweepTx()
signDesc := &SignDescriptor{ signDesc := &SignDescriptor{
KeyDesc: keychain.KeyDescriptor{ KeyDesc: keychain.KeyDescriptor{
PubKey: aliceKeyPub, PubKey: aliceKeyPub,
@ -580,6 +638,9 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
{ {
// refund w/ invalid lock time // refund w/ invalid lock time
makeWitnessTestCase(t, func() (wire.TxWitness, error) { makeWitnessTestCase(t, func() (wire.TxWitness, error) {
genCommitTx()
genSweepTx()
signDesc := &SignDescriptor{ signDesc := &SignDescriptor{
KeyDesc: keychain.KeyDescriptor{ KeyDesc: keychain.KeyDescriptor{
PubKey: aliceKeyPub, PubKey: aliceKeyPub,
@ -600,6 +661,9 @@ 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()
genSweepTx()
signDesc := &SignDescriptor{ signDesc := &SignDescriptor{
KeyDesc: keychain.KeyDescriptor{ KeyDesc: keychain.KeyDescriptor{
PubKey: aliceKeyPub, PubKey: aliceKeyPub,