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,6 +221,63 @@ 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}}
var (
htlcWitnessScript, htlcPkScript []byte
htlcOutput *wire.TxOut
sweepTxSigHashes *txscript.TxSigHashes
senderCommitTx, sweepTx *wire.MsgTx
bobRecvrSig []byte
)
// genCommitTx generates a commitment tx.
genCommitTx := func() {
// 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)
}
// genSweepTx generates a sweep of the senderCommitTx.
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 // 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 // that will act as Bob's signature to Alice for the second level HTLC
// transaction. // transaction.
@ -272,10 +292,11 @@ func TestHTLCSenderSpendValidation(t *testing.T) {
SigHashes: sweepTxSigHashes, SigHashes: sweepTxSigHashes,
InputIndex: 0, InputIndex: 0,
} }
bobRecvrSig, err := bobSigner.SignOutputRaw(sweepTx, &bobSignDesc) bobRecvrSig, err = bobSigner.SignOutputRaw(sweepTx, &bobSignDesc)
if err != nil { if err != nil {
t.Fatalf("unable to generate alice signature: %v", err) t.Fatalf("unable to generate alice signature: %v", err)
} }
}
testCases := []struct { testCases := []struct {
witness func() wire.TxWitness witness func() wire.TxWitness
@ -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,6 +483,61 @@ 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}}
var (
htlcWitnessScript, htlcPkScript []byte
htlcOutput *wire.TxOut
receiverCommitTx, sweepTx *wire.MsgTx
sweepTxSigHashes *txscript.TxSigHashes
aliceSenderSig []byte
)
genCommitTx := func() {
// 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)
}
genSweepTx := func() {
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 // 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 // that will act as Alice's signature to Bob for the second level HTLC
// transaction. // transaction.
@ -504,10 +552,11 @@ func TestHTLCReceiverSpendValidation(t *testing.T) {
SigHashes: sweepTxSigHashes, SigHashes: sweepTxSigHashes,
InputIndex: 0, InputIndex: 0,
} }
aliceSenderSig, err := aliceSigner.SignOutputRaw(sweepTx, &aliceSignDesc) aliceSenderSig, err = aliceSigner.SignOutputRaw(sweepTx, &aliceSignDesc)
if err != nil { if err != nil {
t.Fatalf("unable to generate alice signature: %v", err) 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?
testCases := []struct { testCases := []struct {
@ -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,