Skip to content

lnrpc: added weight field to the transaction return for getTransactio…#10620

Open
kushagra0902 wants to merge 1 commit intolightningnetwork:masterfrom
kushagra0902:master
Open

lnrpc: added weight field to the transaction return for getTransactio…#10620
kushagra0902 wants to merge 1 commit intolightningnetwork:masterfrom
kushagra0902:master

Conversation

@kushagra0902
Copy link

Change Description

Added a "weight" field to the transactions returned by the RPCs like listchaintxns for cli and other related RPC in gRPC and REST. This can be used to calculate fee_per_vbyte for unconfirmed transactions. This PR solves the issue #9833.

Steps to Test

  1. Run make rpc to ensure that the generated files are according to new definitions edited in protobuf file.
  2. Run make install to update the lnd and lncli binaries with new RPC definitions.
  3. Make dummy transactions on either regtest network or any other network.
  4. Use lncli --network=<your_selected_network> listchaintxns to get all the transactions. The listed transactions should have "Weights" field

##Implementation Method

  1. Added the required field in lnrpc/lightning.proto file so that the field is added to all the RPC response structs at once.
  2. In lnrpc/rpc_util.go in RPCTransaction function, used blockchain.GetTransactionWeight() to get the weight involved and populated the corresponding field.

@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the LND RPC interface by introducing a weight field to transaction responses. This addition allows clients to accurately determine the weight of transactions, which is crucial for calculating fee_per_vbyte, particularly for unconfirmed transactions. The change provides greater transparency and utility for fee estimation within the LND ecosystem.

Highlights

  • New weight field for transactions: A weight field has been added to the Transaction message in lightning.proto, making transaction weight available via RPCs like listchaintxns.
  • Transaction weight calculation: Logic was implemented in rpc_utils.go to deserialize raw transaction data and calculate its weight using btcsuite/btcd/blockchain.GetTransactionWeight, populating the new weight field.
  • API documentation updates: The Swagger definitions (lightning.swagger.json and walletkit.swagger.json) were updated to reflect the addition of the weight field to transaction objects.
  • Issue resolution: This change addresses issue [feature]: GetTransaction: return fee_per_vbyte #9833, enabling the calculation of fee_per_vbyte for unconfirmed transactions.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • lnrpc/lightning.proto
    • Added an int64 weight field to the Transaction message definition.
  • lnrpc/lightning.swagger.json
    • Included a new weight property (type string, format int64) in the lnrpcTransaction definition.
  • lnrpc/rpc_utils.go
    • Imported bytes, blockchain, btcutil, and wire packages.
    • Added code to deserialize RawTx into wire.MsgTx and calculate its weight using blockchain.GetTransactionWeight.
    • Populated the Weight field of the Transaction RPC response with the calculated value.
  • lnrpc/walletrpc/walletkit.swagger.json
    • Included a new weight property (type string, format int64) in the walletrpcTransaction definition.
Activity
  • No specific activity (comments, reviews, etc.) has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@lightninglabs-deploy lightninglabs-deploy added the severity-high Requires knowledgeable engineer review label Mar 2, 2026
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds a weight field to the Transaction message, which is a useful addition for fee calculation. The changes to the protobuf definition and Swagger files are correct. I've identified a couple of areas for improvement in lnrpc/rpc_utils.go related to error handling, commenting, and code style, and have provided specific suggestions for them.

Note: Security Review is unavailable for this PR.

Comment on lines +72 to +80
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),
)
}
}

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)

}, 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)

@lightninglabs-deploy
Copy link
Collaborator

@kushagra0902, remember to re-request review from reviewers when ready

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

severity-high Requires knowledgeable engineer review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants