From b3afa0c9ed990b3b687e8abd93eabb7eea153b80 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Thu, 19 Mar 2020 16:51:32 +0100 Subject: [PATCH] sweep: log sweep tx id and full list of inputs To facilitate debugging. --- sweep/txgenerator.go | 38 +++++++++++++++----------------------- sweep/txgenerator_test.go | 15 ++++++++++----- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/sweep/txgenerator.go b/sweep/txgenerator.go index 229ecf3e..1e47dad2 100644 --- a/sweep/txgenerator.go +++ b/sweep/txgenerator.go @@ -138,10 +138,6 @@ func createSweepTx(inputs []input.Input, outputPkScript []byte, txFee := feePerKw.FeeForWeight(txWeight) - log.Infof("Creating sweep transaction for %v inputs (%s) "+ - "using %v sat/kw, tx_fee=%v", len(inputs), - inputTypeSummary(inputs), int64(feePerKw), txFee) - // Sum up the total value contained in the inputs. var totalSum btcutil.Amount for _, o := range inputs { @@ -211,6 +207,10 @@ func createSweepTx(inputs []input.Input, outputPkScript []byte, } } + log.Infof("Creating sweep transaction %v for %v inputs (%s) "+ + "using %v sat/kw, tx_fee=%v", sweepTx.TxHash(), len(inputs), + inputTypeSummary(inputs), int64(feePerKw), txFee) + return sweepTx, nil } @@ -254,27 +254,19 @@ func getWeightEstimate(inputs []input.Input) ([]input.Input, int64) { // inputSummary returns a string containing a human readable summary about the // witness types of a list of inputs. func inputTypeSummary(inputs []input.Input) string { - // Count each input by the string representation of its witness type. - // We also keep track of the keys so we can later sort by them to get - // a stable output. - counts := make(map[string]uint32) - keys := make([]string, 0, len(inputs)) - for _, i := range inputs { - key := i.WitnessType().String() - _, ok := counts[key] - if !ok { - counts[key] = 0 - keys = append(keys, key) - } - counts[key]++ - } - sort.Strings(keys) + // Sort inputs by witness type. + sortedInputs := make([]input.Input, len(inputs)) + copy(sortedInputs, inputs) + sort.Slice(sortedInputs, func(i, j int) bool { + return sortedInputs[i].WitnessType().String() < + sortedInputs[j].WitnessType().String() + }) - // Return a nice string representation of the counts by comma joining a - // slice. var parts []string - for _, witnessType := range keys { - part := fmt.Sprintf("%d %s", counts[witnessType], witnessType) + for _, i := range sortedInputs { + part := fmt.Sprintf("%v (%v)", + *i.OutPoint(), i.WitnessType()) + parts = append(parts, part) } return strings.Join(parts, ", ") diff --git a/sweep/txgenerator_test.go b/sweep/txgenerator_test.go index 726347ec..91641e5e 100644 --- a/sweep/txgenerator_test.go +++ b/sweep/txgenerator_test.go @@ -3,6 +3,7 @@ package sweep import ( "testing" + "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" "github.com/lightningnetwork/lnd/input" ) @@ -15,9 +16,10 @@ var ( input.WitnessKeyHash, } expectedWeight = int64(1462) - expectedSummary = "1 CommitmentTimeLock, 1 " + - "HtlcAcceptedSuccessSecondLevel, 1 HtlcOfferedRemoteTimeout, " + - "1 WitnessKeyHash" + expectedSummary = "0000000000000000000000000000000000000000000000000000000000000000:10 (CommitmentTimeLock), " + + "0000000000000000000000000000000000000000000000000000000000000001:11 (HtlcAcceptedSuccessSecondLevel), " + + "0000000000000000000000000000000000000000000000000000000000000002:12 (HtlcOfferedRemoteTimeout), " + + "0000000000000000000000000000000000000000000000000000000000000003:13 (WitnessKeyHash)" ) // TestWeightEstimate tests that the estimated weight and number of CSVs/CLTVs @@ -27,9 +29,12 @@ func TestWeightEstimate(t *testing.T) { t.Parallel() var inputs []input.Input - for _, witnessType := range witnessTypes { + for i, witnessType := range witnessTypes { inputs = append(inputs, input.NewBaseInput( - &wire.OutPoint{}, witnessType, + &wire.OutPoint{ + Hash: chainhash.Hash{byte(i)}, + Index: uint32(i) + 10, + }, witnessType, &input.SignDescriptor{}, 0, )) }