Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lnrpc/lightning.proto
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,9 @@ message Transaction {

// PreviousOutpoints/Inputs of this transaction.
repeated PreviousOutPoint previous_outpoints = 12;

// The weight of the transaction in weight units.
int64 weight = 13;
}

message GetTransactionsRequest {
Expand Down
5 changes: 5 additions & 0 deletions lnrpc/lightning.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -7976,6 +7976,11 @@
"$ref": "#/definitions/lnrpcPreviousOutPoint"
},
"description": "PreviousOutpoints/Inputs of this transaction."
},
"weight": {
"type": "string",
"format": "int64",
"description": "The weight of the transaction in weight units."
}
}
},
Expand Down
19 changes: 18 additions & 1 deletion lnrpc/rpc_utils.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package lnrpc

import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"sort"

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/sweep"
Expand Down Expand Up @@ -62,6 +66,18 @@ func RPCTransaction(tx *lnwallet.TransactionDetail) *Transaction {
var destAddresses []string
// Re-package destination output information.
var outputDetails []*OutputDetail

var txWeight int64

if len(tx.RawTx) > 0 {
reader := bytes.NewReader(tx.RawTx)
var msgTx wire.MsgTx
if err := msgTx.Deserialize(reader); err == nil {
txWeight = blockchain.GetTransactionWeight(
btcutil.NewTx(&msgTx),
)
}
}
Comment on lines +72 to +80

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This new block for calculating transaction weight can be improved in two ways:

  1. Error Handling: The error from msgTx.Deserialize(reader) is silently ignored. If deserialization fails, txWeight will be 0, which could be misleading for the RPC caller. It's better to log this error to aid in debugging potential issues with transaction data.
  2. Commenting: Per the LND Style Guide (lines 51-52), in-body comments should explain the intention of the code. This block would benefit from a comment explaining its purpose.

Here is a suggested implementation addressing both points. Note that you'll need to make the package logger available to use log.Errorf.

	// If the raw transaction is available, we'll deserialize it to
	// calculate its weight.
	if len(tx.RawTx) > 0 {
		reader := bytes.NewReader(tx.RawTx)
		var msgTx wire.MsgTx
		if err := msgTx.Deserialize(reader); err != nil {
			// If we can't deserialize the transaction, we'll log
			// it and return a weight of 0. This should not
			// happen with valid data.
			log.Errorf("unable to deserialize tx %v to get "+
				"weight: %v", tx.Hash, err)
		} else {
			txWeight = blockchain.GetTransactionWeight(
				btcutil.NewTx(&msgTx),
			)
		}
	}
References
  1. In-body comments should explain the intention of the code. (link)

for _, o := range tx.OutputDetails {
// Note: DestAddresses is deprecated but we keep
// populating it with addresses for backwards
Expand Down Expand Up @@ -113,6 +129,7 @@ func RPCTransaction(tx *lnwallet.TransactionDetail) *Transaction {
RawTxHex: hex.EncodeToString(tx.RawTx),
Label: tx.Label,
PreviousOutpoints: previousOutpoints,
Weight: txWeight,
}
}

Expand Down Expand Up @@ -226,7 +243,7 @@ func GetChannelOutPoint(chanPoint *ChannelPoint) (*OutPoint, error) {
OutputIndex: chanPoint.OutputIndex,
}, nil
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This line contains unnecessary whitespace. It should be a blank line to separate logical code blocks, as per the style guide.

References
  1. Segment code into logical stanzas separated by newlines. (link)

// CalculateFeeRate uses either satPerByte or satPerVByte, but not both, from a
// request to calculate the fee rate. It provides compatibility for the
// deprecated field, satPerByte. Once the field is safe to be removed, the
Expand Down
5 changes: 5 additions & 0 deletions lnrpc/walletrpc/walletkit.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,11 @@
"$ref": "#/definitions/lnrpcPreviousOutPoint"
},
"description": "PreviousOutpoints/Inputs of this transaction."
},
"weight": {
"type": "string",
"format": "int64",
"description": "The weight of the transaction in weight units."
}
}
},
Expand Down
Loading