watchtower/lookout/justice_descriptor_test: use require

This commit is contained in:
Conner Fromknecht 2020-09-15 12:43:52 -04:00
parent ac2e1d7d96
commit cfbde5d2ce
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7

@ -1,7 +1,6 @@
package lookout_test package lookout_test
import ( import (
"reflect"
"testing" "testing"
"time" "time"
@ -11,7 +10,6 @@ import (
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/txsort" "github.com/btcsuite/btcutil/txsort"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
@ -20,6 +18,7 @@ import (
"github.com/lightningnetwork/lnd/watchtower/wtdb" "github.com/lightningnetwork/lnd/watchtower/wtdb"
"github.com/lightningnetwork/lnd/watchtower/wtmock" "github.com/lightningnetwork/lnd/watchtower/wtmock"
"github.com/lightningnetwork/lnd/watchtower/wtpolicy" "github.com/lightningnetwork/lnd/watchtower/wtpolicy"
"github.com/stretchr/testify/require"
) )
const csvDelay uint32 = 144 const csvDelay uint32 = 144
@ -106,21 +105,15 @@ func testJusticeDescriptor(t *testing.T, blobType blob.Type) {
toLocalScript, err := input.CommitScriptToSelf( toLocalScript, err := input.CommitScriptToSelf(
csvDelay, toLocalPK, revPK, csvDelay, toLocalPK, revPK,
) )
if err != nil { require.Nil(t, err)
t.Fatalf("unable to create to-local script: %v", err)
}
// Compute the to-local witness script hash. // Compute the to-local witness script hash.
toLocalScriptHash, err := input.WitnessScriptHash(toLocalScript) toLocalScriptHash, err := input.WitnessScriptHash(toLocalScript)
if err != nil { require.Nil(t, err)
t.Fatalf("unable to create to-local witness script hash: %v", err)
}
// Compute the to-remote witness script hash. // Compute the to-remote witness script hash.
toRemoteScriptHash, err := input.CommitScriptUnencumbered(toRemotePK) toRemoteScriptHash, err := input.CommitScriptUnencumbered(toRemotePK)
if err != nil { require.Nil(t, err)
t.Fatalf("unable to create to-remote script: %v", err)
}
// Construct the breaching commitment txn, containing the to-local and // Construct the breaching commitment txn, containing the to-local and
// to-remote outputs. We don't need any inputs for this test. // to-remote outputs. We don't need any inputs for this test.
@ -207,9 +200,7 @@ func testJusticeDescriptor(t *testing.T, blobType blob.Type) {
totalAmount, int64(txWeight), justiceKit.SweepAddress, totalAmount, int64(txWeight), justiceKit.SweepAddress,
sessionInfo.RewardAddress, sessionInfo.RewardAddress,
) )
if err != nil { require.Nil(t, err)
t.Fatalf("unable to compute justice txouts: %v", err)
}
// Attach the txouts and BIP69 sort the resulting transaction. // Attach the txouts and BIP69 sort the resulting transaction.
justiceTxn.TxOut = outputs justiceTxn.TxOut = outputs
@ -244,15 +235,12 @@ func testJusticeDescriptor(t *testing.T, blobType blob.Type) {
// Verify that our test justice transaction is sane. // Verify that our test justice transaction is sane.
btx := btcutil.NewTx(justiceTxn) btx := btcutil.NewTx(justiceTxn)
if err := blockchain.CheckTransactionSanity(btx); err != nil { err = blockchain.CheckTransactionSanity(btx)
t.Fatalf("justice txn is not sane: %v", err) require.Nil(t, err)
}
// Compute a DER-encoded signature for the to-local input. // Compute a DER-encoded signature for the to-local input.
toLocalSigRaw, err := signer.SignOutputRaw(justiceTxn, toLocalSignDesc) toLocalSigRaw, err := signer.SignOutputRaw(justiceTxn, toLocalSignDesc)
if err != nil { require.Nil(t, err)
t.Fatalf("unable to sign to-local input: %v", err)
}
// Compute the witness for the to-remote input. The first element is a // Compute the witness for the to-remote input. The first element is a
// DER-encoded signature under the to-remote pubkey. The sighash flag is // DER-encoded signature under the to-remote pubkey. The sighash flag is
@ -260,22 +248,16 @@ func testJusticeDescriptor(t *testing.T, blobType blob.Type) {
toRemoteWitness, err := input.CommitSpendNoDelay( toRemoteWitness, err := input.CommitSpendNoDelay(
signer, toRemoteSignDesc, justiceTxn, false, signer, toRemoteSignDesc, justiceTxn, false,
) )
if err != nil { require.Nil(t, err)
t.Fatalf("unable to sign to-remote input: %v", err)
}
toRemoteSigRaw := toRemoteWitness[0][:len(toRemoteWitness[0])-1] toRemoteSigRaw := toRemoteWitness[0][:len(toRemoteWitness[0])-1]
// Convert the DER to-local sig into a fixed-size signature. // Convert the DER to-local sig into a fixed-size signature.
toLocalSig, err := lnwire.NewSigFromSignature(toLocalSigRaw) toLocalSig, err := lnwire.NewSigFromSignature(toLocalSigRaw)
if err != nil { require.Nil(t, err)
t.Fatalf("unable to parse to-local signature: %v", err)
}
// Convert the DER to-remote sig into a fixed-size signature. // Convert the DER to-remote sig into a fixed-size signature.
toRemoteSig, err := lnwire.NewSigFromRawSignature(toRemoteSigRaw) toRemoteSig, err := lnwire.NewSigFromRawSignature(toRemoteSigRaw)
if err != nil { require.Nil(t, err)
t.Fatalf("unable to parse to-remote signature: %v", err)
}
// Complete our justice kit by copying the signatures into the payload. // Complete our justice kit by copying the signatures into the payload.
copy(justiceKit.CommitToLocalSig[:], toLocalSig[:]) copy(justiceKit.CommitToLocalSig[:], toLocalSig[:])
@ -300,9 +282,7 @@ func testJusticeDescriptor(t *testing.T, blobType blob.Type) {
// Exact retribution on the offender. If no error is returned, we expect // Exact retribution on the offender. If no error is returned, we expect
// the justice transaction to be published via the channel. // the justice transaction to be published via the channel.
err = punisher.Punish(justiceDesc, nil) err = punisher.Punish(justiceDesc, nil)
if err != nil { require.Nil(t, err)
t.Fatalf("unable to punish breach: %v", err)
}
// Retrieve the published justice transaction. // Retrieve the published justice transaction.
var wtJusticeTxn *wire.MsgTx var wtJusticeTxn *wire.MsgTx
@ -326,9 +306,5 @@ func testJusticeDescriptor(t *testing.T, blobType blob.Type) {
justiceTxn.TxIn[1].Witness[1] = toRemotePK.SerializeCompressed() justiceTxn.TxIn[1].Witness[1] = toRemotePK.SerializeCompressed()
// Assert that the watchtower derives the same justice txn. // Assert that the watchtower derives the same justice txn.
if !reflect.DeepEqual(justiceTxn, wtJusticeTxn) { require.Equal(t, justiceTxn, wtJusticeTxn)
t.Fatalf("expected justice txn: %v\ngot %v",
spew.Sdump(justiceTxn),
spew.Sdump(wtJusticeTxn))
}
} }