2018-11-01 06:42:17 +03:00
|
|
|
package lookout_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2019-02-02 04:26:54 +03:00
|
|
|
"github.com/btcsuite/btcutil/txsort"
|
2019-01-16 17:47:43 +03:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2018-11-01 06:42:17 +03:00
|
|
|
"github.com/lightningnetwork/lnd/keychain"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
"github.com/lightningnetwork/lnd/watchtower/blob"
|
|
|
|
"github.com/lightningnetwork/lnd/watchtower/lookout"
|
|
|
|
"github.com/lightningnetwork/lnd/watchtower/wtdb"
|
2019-02-01 05:16:02 +03:00
|
|
|
"github.com/lightningnetwork/lnd/watchtower/wtmock"
|
2019-01-11 02:35:11 +03:00
|
|
|
"github.com/lightningnetwork/lnd/watchtower/wtpolicy"
|
2020-09-15 19:43:52 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-11-01 06:42:17 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const csvDelay uint32 = 144
|
|
|
|
|
|
|
|
var (
|
|
|
|
revPrivBytes = []byte{
|
|
|
|
0x8f, 0x4b, 0x51, 0x83, 0xa9, 0x34, 0xbd, 0x5f,
|
|
|
|
0x74, 0x6c, 0x9d, 0x5c, 0xae, 0x88, 0x2d, 0x31,
|
|
|
|
0x06, 0x90, 0xdd, 0x8c, 0x9b, 0x31, 0xbc, 0xd1,
|
|
|
|
0x78, 0x91, 0x88, 0x2a, 0xf9, 0x74, 0xa0, 0xef,
|
|
|
|
}
|
|
|
|
|
|
|
|
toLocalPrivBytes = []byte{
|
|
|
|
0xde, 0x17, 0xc1, 0x2f, 0xdc, 0x1b, 0xc0, 0xc6,
|
|
|
|
0x59, 0x5d, 0xf9, 0xc1, 0x3e, 0x89, 0xbc, 0x6f,
|
|
|
|
0x01, 0x85, 0x45, 0x76, 0x26, 0xce, 0x9c, 0x55,
|
|
|
|
0x3b, 0xc9, 0xec, 0x3d, 0xd8, 0x8b, 0xac, 0xa8,
|
|
|
|
}
|
|
|
|
|
|
|
|
toRemotePrivBytes = []byte{
|
|
|
|
0x28, 0x59, 0x6f, 0x36, 0xb8, 0x9f, 0x19, 0x5d,
|
|
|
|
0xcb, 0x07, 0x48, 0x8a, 0xe5, 0x89, 0x71, 0x74,
|
|
|
|
0x70, 0x4c, 0xff, 0x1e, 0x9c, 0x00, 0x93, 0xbe,
|
|
|
|
0xe2, 0x2e, 0x68, 0x08, 0x4c, 0xb4, 0x0f, 0x4f,
|
|
|
|
}
|
2019-02-02 04:26:54 +03:00
|
|
|
|
|
|
|
rewardCommitType = blob.TypeFromFlags(
|
|
|
|
blob.FlagReward, blob.FlagCommitOutputs,
|
|
|
|
)
|
|
|
|
|
|
|
|
altruistCommitType = blob.FlagCommitOutputs.Type()
|
2020-09-15 19:44:00 +03:00
|
|
|
|
|
|
|
altruistAnchorCommitType = blob.TypeAltruistAnchorCommit
|
2018-11-01 06:42:17 +03:00
|
|
|
)
|
|
|
|
|
2019-02-02 04:26:54 +03:00
|
|
|
// TestJusticeDescriptor asserts that a JusticeDescriptor is able to produce the
|
|
|
|
// correct justice transaction for different blob types.
|
2018-11-01 06:42:17 +03:00
|
|
|
func TestJusticeDescriptor(t *testing.T) {
|
2019-02-02 04:26:54 +03:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
blobType blob.Type
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "reward and commit type",
|
|
|
|
blobType: rewardCommitType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "altruist and commit type",
|
|
|
|
blobType: altruistCommitType,
|
|
|
|
},
|
2020-09-15 19:44:00 +03:00
|
|
|
{
|
|
|
|
name: "altruist anchor commit type",
|
|
|
|
blobType: altruistAnchorCommitType,
|
|
|
|
},
|
2019-02-02 04:26:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
testJusticeDescriptor(t, test.blobType)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testJusticeDescriptor(t *testing.T, blobType blob.Type) {
|
2020-09-15 19:44:00 +03:00
|
|
|
isAnchorChannel := blobType.IsAnchorChannel()
|
|
|
|
|
2018-11-01 06:42:17 +03:00
|
|
|
const (
|
|
|
|
localAmount = btcutil.Amount(100000)
|
|
|
|
remoteAmount = btcutil.Amount(200000)
|
|
|
|
totalAmount = localAmount + remoteAmount
|
|
|
|
)
|
|
|
|
|
|
|
|
// Parse the key pairs for all keys used in the test.
|
|
|
|
revSK, revPK := btcec.PrivKeyFromBytes(
|
|
|
|
btcec.S256(), revPrivBytes,
|
|
|
|
)
|
|
|
|
_, toLocalPK := btcec.PrivKeyFromBytes(
|
|
|
|
btcec.S256(), toLocalPrivBytes,
|
|
|
|
)
|
|
|
|
toRemoteSK, toRemotePK := btcec.PrivKeyFromBytes(
|
|
|
|
btcec.S256(), toRemotePrivBytes,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Create the signer, and add the revocation and to-remote privkeys.
|
2019-02-01 05:16:02 +03:00
|
|
|
signer := wtmock.NewMockSigner()
|
2018-11-01 06:42:17 +03:00
|
|
|
var (
|
2019-02-01 05:16:02 +03:00
|
|
|
revKeyLoc = signer.AddPrivKey(revSK)
|
|
|
|
toRemoteKeyLoc = signer.AddPrivKey(toRemoteSK)
|
2018-11-01 06:42:17 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Construct the to-local witness script.
|
2019-01-16 17:47:43 +03:00
|
|
|
toLocalScript, err := input.CommitScriptToSelf(
|
2018-11-01 06:42:17 +03:00
|
|
|
csvDelay, toLocalPK, revPK,
|
|
|
|
)
|
2020-09-15 19:43:52 +03:00
|
|
|
require.Nil(t, err)
|
2018-11-01 06:42:17 +03:00
|
|
|
|
|
|
|
// Compute the to-local witness script hash.
|
2019-01-16 17:47:43 +03:00
|
|
|
toLocalScriptHash, err := input.WitnessScriptHash(toLocalScript)
|
2020-09-15 19:43:52 +03:00
|
|
|
require.Nil(t, err)
|
2018-11-01 06:42:17 +03:00
|
|
|
|
2020-09-15 19:44:00 +03:00
|
|
|
// Compute the to-remote redeem script, witness script hash, and
|
|
|
|
// sequence numbers.
|
|
|
|
//
|
|
|
|
// NOTE: This is pretty subtle.
|
|
|
|
//
|
|
|
|
// The actual redeem script for a p2wkh output is just the pubkey, but
|
|
|
|
// the witness sighash calculation injects the classic p2kh script:
|
|
|
|
// OP_DUP OP_HASH160 <pubkey-hash160> OP_EQUALVERIFY OP_CHECKSIG. When
|
|
|
|
// signing for p2wkh we don't pass the raw pubkey as the witness script
|
|
|
|
// to the sign descriptor (since that's also not a valid script).
|
|
|
|
// Instead we give it the _pkscript_ of the form OP_0 <pubkey-hash160>
|
|
|
|
// from which pubkey-hash160 is extracted during sighash calculation.
|
|
|
|
//
|
|
|
|
// On the other hand, signing for the anchor p2wsh to-remote outputs
|
|
|
|
// requires the sign descriptor to contain the redeem script ver batim.
|
|
|
|
// This difference in behavior forces us to use a distinct
|
|
|
|
// toRemoteSigningScript to handle both cases.
|
|
|
|
var (
|
|
|
|
toRemoteSequence uint32
|
|
|
|
toRemoteRedeemScript []byte
|
|
|
|
toRemoteScriptHash []byte
|
|
|
|
toRemoteSigningScript []byte
|
|
|
|
)
|
|
|
|
if isAnchorChannel {
|
|
|
|
toRemoteSequence = 1
|
|
|
|
toRemoteRedeemScript, err = input.CommitScriptToRemoteConfirmed(
|
|
|
|
toRemotePK,
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
toRemoteScriptHash, err = input.WitnessScriptHash(
|
|
|
|
toRemoteRedeemScript,
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
// As it should be.
|
|
|
|
toRemoteSigningScript = toRemoteRedeemScript
|
|
|
|
|
|
|
|
} else {
|
|
|
|
toRemoteRedeemScript = toRemotePK.SerializeCompressed()
|
|
|
|
toRemoteScriptHash, err = input.CommitScriptUnencumbered(
|
|
|
|
toRemotePK,
|
|
|
|
)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
// NOTE: This is the _pkscript_.
|
|
|
|
toRemoteSigningScript = toRemoteScriptHash
|
|
|
|
}
|
2018-11-01 06:42:17 +03:00
|
|
|
|
|
|
|
// Construct the breaching commitment txn, containing the to-local and
|
|
|
|
// to-remote outputs. We don't need any inputs for this test.
|
|
|
|
breachTxn := &wire.MsgTx{
|
|
|
|
Version: 2,
|
|
|
|
TxIn: []*wire.TxIn{},
|
|
|
|
TxOut: []*wire.TxOut{
|
|
|
|
{
|
|
|
|
Value: int64(localAmount),
|
|
|
|
PkScript: toLocalScriptHash,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: int64(remoteAmount),
|
|
|
|
PkScript: toRemoteScriptHash,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
breachTxID := breachTxn.TxHash()
|
|
|
|
|
|
|
|
// Compute the weight estimate for our justice transaction.
|
2019-01-16 17:47:43 +03:00
|
|
|
var weightEstimate input.TxWeightEstimator
|
2020-04-06 03:07:14 +03:00
|
|
|
|
|
|
|
// An older ToLocalPenaltyWitnessSize constant used to underestimate the
|
|
|
|
// size by one byte. The diferrence in weight can cause different output
|
|
|
|
// values on the sweep transaction, so we mimic the original bug and
|
2020-09-15 19:48:23 +03:00
|
|
|
// create signatures using the original weight estimate. For anchor
|
|
|
|
// channels we fix this and use the correct witness size.
|
|
|
|
if isAnchorChannel {
|
|
|
|
weightEstimate.AddWitnessInput(input.ToLocalPenaltyWitnessSize)
|
|
|
|
} else {
|
|
|
|
weightEstimate.AddWitnessInput(input.ToLocalPenaltyWitnessSize - 1)
|
|
|
|
}
|
2020-04-06 03:07:14 +03:00
|
|
|
|
2020-09-15 19:44:00 +03:00
|
|
|
if isAnchorChannel {
|
|
|
|
weightEstimate.AddWitnessInput(input.ToRemoteConfirmedWitnessSize)
|
|
|
|
} else {
|
|
|
|
weightEstimate.AddWitnessInput(input.P2WKHWitnessSize)
|
|
|
|
}
|
2019-02-02 04:26:54 +03:00
|
|
|
weightEstimate.AddP2WKHOutput()
|
|
|
|
if blobType.Has(blob.FlagReward) {
|
|
|
|
weightEstimate.AddP2WKHOutput()
|
|
|
|
}
|
2018-11-01 06:42:17 +03:00
|
|
|
txWeight := weightEstimate.Weight()
|
|
|
|
|
|
|
|
// Create a session info so that simulate agreement of the sweep
|
|
|
|
// parameters that should be used in constructing the justice
|
|
|
|
// transaction.
|
2019-02-02 04:26:54 +03:00
|
|
|
policy := wtpolicy.Policy{
|
2019-06-14 03:33:06 +03:00
|
|
|
TxPolicy: wtpolicy.TxPolicy{
|
|
|
|
BlobType: blobType,
|
|
|
|
SweepFeeRate: 2000,
|
|
|
|
RewardRate: 900000,
|
|
|
|
},
|
2019-02-02 04:26:54 +03:00
|
|
|
}
|
2018-11-01 06:42:17 +03:00
|
|
|
sessionInfo := &wtdb.SessionInfo{
|
2019-02-02 04:26:54 +03:00
|
|
|
Policy: policy,
|
2018-11-01 06:42:17 +03:00
|
|
|
RewardAddress: makeAddrSlice(22),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Begin to assemble the justice kit, starting with the sweep address,
|
|
|
|
// pubkeys, and csv delay.
|
|
|
|
justiceKit := &blob.JusticeKit{
|
2020-09-15 19:44:00 +03:00
|
|
|
BlobType: blobType,
|
2018-11-01 06:42:17 +03:00
|
|
|
SweepAddress: makeAddrSlice(22),
|
|
|
|
CSVDelay: csvDelay,
|
|
|
|
}
|
|
|
|
copy(justiceKit.RevocationPubKey[:], revPK.SerializeCompressed())
|
|
|
|
copy(justiceKit.LocalDelayPubKey[:], toLocalPK.SerializeCompressed())
|
|
|
|
copy(justiceKit.CommitToRemotePubKey[:], toRemotePK.SerializeCompressed())
|
|
|
|
|
|
|
|
// Create a transaction spending from the outputs of the breach
|
|
|
|
// transaction created earlier. The inputs are always ordered w/
|
|
|
|
// to-local and then to-remote. The outputs are always added as the
|
|
|
|
// sweep address then reward address.
|
|
|
|
justiceTxn := &wire.MsgTx{
|
|
|
|
Version: 2,
|
|
|
|
TxIn: []*wire.TxIn{
|
|
|
|
{
|
|
|
|
PreviousOutPoint: wire.OutPoint{
|
|
|
|
Hash: breachTxID,
|
|
|
|
Index: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PreviousOutPoint: wire.OutPoint{
|
|
|
|
Hash: breachTxID,
|
|
|
|
Index: 1,
|
|
|
|
},
|
2020-09-15 19:44:00 +03:00
|
|
|
Sequence: toRemoteSequence,
|
2018-11-01 06:42:17 +03:00
|
|
|
},
|
|
|
|
},
|
2019-02-02 04:26:54 +03:00
|
|
|
}
|
2018-11-01 06:42:17 +03:00
|
|
|
|
2019-02-02 04:26:54 +03:00
|
|
|
outputs, err := policy.ComputeJusticeTxOuts(
|
|
|
|
totalAmount, int64(txWeight), justiceKit.SweepAddress,
|
|
|
|
sessionInfo.RewardAddress,
|
|
|
|
)
|
2020-09-15 19:43:52 +03:00
|
|
|
require.Nil(t, err)
|
2018-11-01 06:42:17 +03:00
|
|
|
|
2019-02-02 04:26:54 +03:00
|
|
|
// Attach the txouts and BIP69 sort the resulting transaction.
|
|
|
|
justiceTxn.TxOut = outputs
|
|
|
|
txsort.InPlaceSort(justiceTxn)
|
|
|
|
|
2018-11-01 06:42:17 +03:00
|
|
|
hashCache := txscript.NewTxSigHashes(justiceTxn)
|
|
|
|
|
|
|
|
// Create the sign descriptor used to sign for the to-local input.
|
2019-01-16 17:47:43 +03:00
|
|
|
toLocalSignDesc := &input.SignDescriptor{
|
2018-11-01 06:42:17 +03:00
|
|
|
KeyDesc: keychain.KeyDescriptor{
|
|
|
|
KeyLocator: revKeyLoc,
|
|
|
|
},
|
|
|
|
WitnessScript: toLocalScript,
|
|
|
|
Output: breachTxn.TxOut[0],
|
|
|
|
SigHashes: hashCache,
|
|
|
|
InputIndex: 0,
|
|
|
|
HashType: txscript.SigHashAll,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the sign descriptor used to sign for the to-remote input.
|
2019-01-16 17:47:43 +03:00
|
|
|
toRemoteSignDesc := &input.SignDescriptor{
|
2018-11-01 06:42:17 +03:00
|
|
|
KeyDesc: keychain.KeyDescriptor{
|
|
|
|
KeyLocator: toRemoteKeyLoc,
|
|
|
|
PubKey: toRemotePK,
|
|
|
|
},
|
2020-09-15 19:44:00 +03:00
|
|
|
WitnessScript: toRemoteSigningScript,
|
2018-11-01 06:42:17 +03:00
|
|
|
Output: breachTxn.TxOut[1],
|
|
|
|
SigHashes: hashCache,
|
|
|
|
InputIndex: 1,
|
|
|
|
HashType: txscript.SigHashAll,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that our test justice transaction is sane.
|
|
|
|
btx := btcutil.NewTx(justiceTxn)
|
2020-09-15 19:43:52 +03:00
|
|
|
err = blockchain.CheckTransactionSanity(btx)
|
|
|
|
require.Nil(t, err)
|
2018-11-01 06:42:17 +03:00
|
|
|
|
|
|
|
// Compute a DER-encoded signature for the to-local input.
|
|
|
|
toLocalSigRaw, err := signer.SignOutputRaw(justiceTxn, toLocalSignDesc)
|
2020-09-15 19:43:52 +03:00
|
|
|
require.Nil(t, err)
|
2018-11-01 06:42:17 +03:00
|
|
|
|
|
|
|
// 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
|
|
|
|
// also present, so we trim it.
|
2020-09-15 19:44:00 +03:00
|
|
|
toRemoteSigRaw, err := signer.SignOutputRaw(justiceTxn, toRemoteSignDesc)
|
2020-09-15 19:43:52 +03:00
|
|
|
require.Nil(t, err)
|
2018-11-01 06:42:17 +03:00
|
|
|
|
|
|
|
// Convert the DER to-local sig into a fixed-size signature.
|
2020-04-06 03:06:38 +03:00
|
|
|
toLocalSig, err := lnwire.NewSigFromSignature(toLocalSigRaw)
|
2020-09-15 19:43:52 +03:00
|
|
|
require.Nil(t, err)
|
2018-11-01 06:42:17 +03:00
|
|
|
|
|
|
|
// Convert the DER to-remote sig into a fixed-size signature.
|
2020-09-15 19:44:00 +03:00
|
|
|
toRemoteSig, err := lnwire.NewSigFromSignature(toRemoteSigRaw)
|
2020-09-15 19:43:52 +03:00
|
|
|
require.Nil(t, err)
|
2018-11-01 06:42:17 +03:00
|
|
|
|
|
|
|
// Complete our justice kit by copying the signatures into the payload.
|
|
|
|
copy(justiceKit.CommitToLocalSig[:], toLocalSig[:])
|
|
|
|
copy(justiceKit.CommitToRemoteSig[:], toRemoteSig[:])
|
|
|
|
|
|
|
|
justiceDesc := &lookout.JusticeDescriptor{
|
|
|
|
BreachedCommitTx: breachTxn,
|
|
|
|
SessionInfo: sessionInfo,
|
|
|
|
JusticeKit: justiceKit,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a breach punisher that will feed published transactions
|
|
|
|
// over the buffered channel.
|
|
|
|
publications := make(chan *wire.MsgTx, 1)
|
|
|
|
punisher := lookout.NewBreachPunisher(&lookout.PunisherConfig{
|
2020-05-18 15:13:23 +03:00
|
|
|
PublishTx: func(tx *wire.MsgTx, _ string) error {
|
2018-11-01 06:42:17 +03:00
|
|
|
publications <- tx
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// Exact retribution on the offender. If no error is returned, we expect
|
|
|
|
// the justice transaction to be published via the channel.
|
|
|
|
err = punisher.Punish(justiceDesc, nil)
|
2020-09-15 19:43:52 +03:00
|
|
|
require.Nil(t, err)
|
2018-11-01 06:42:17 +03:00
|
|
|
|
|
|
|
// Retrieve the published justice transaction.
|
|
|
|
var wtJusticeTxn *wire.MsgTx
|
|
|
|
select {
|
|
|
|
case wtJusticeTxn = <-publications:
|
|
|
|
case <-time.After(50 * time.Millisecond):
|
|
|
|
t.Fatalf("punisher did not publish justice txn")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct the test's to-local witness.
|
|
|
|
justiceTxn.TxIn[0].Witness = make([][]byte, 3)
|
2020-04-06 03:06:38 +03:00
|
|
|
justiceTxn.TxIn[0].Witness[0] = append(toLocalSigRaw.Serialize(),
|
2018-11-01 06:42:17 +03:00
|
|
|
byte(txscript.SigHashAll))
|
|
|
|
justiceTxn.TxIn[0].Witness[1] = []byte{1}
|
|
|
|
justiceTxn.TxIn[0].Witness[2] = toLocalScript
|
|
|
|
|
|
|
|
// Construct the test's to-remote witness.
|
|
|
|
justiceTxn.TxIn[1].Witness = make([][]byte, 2)
|
2020-09-15 19:44:00 +03:00
|
|
|
justiceTxn.TxIn[1].Witness[0] = append(toRemoteSigRaw.Serialize(),
|
2018-11-01 06:42:17 +03:00
|
|
|
byte(txscript.SigHashAll))
|
2020-09-15 19:44:00 +03:00
|
|
|
justiceTxn.TxIn[1].Witness[1] = toRemoteRedeemScript
|
2018-11-01 06:42:17 +03:00
|
|
|
|
|
|
|
// Assert that the watchtower derives the same justice txn.
|
2020-09-15 19:43:52 +03:00
|
|
|
require.Equal(t, justiceTxn, wtJusticeTxn)
|
2018-11-01 06:42:17 +03:00
|
|
|
}
|