Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wallet 2/3]: refactor funding logic to use allocation code #1407

Merged
merged 13 commits into from
Mar 6, 2025

Conversation

guggero
Copy link
Member

@guggero guggero commented Feb 24, 2025

Refactors the wallet's funding code to use the pre-existing allocation logic to distribute coins from grouped assets.

Part of the group key support saga:


This change is Reviewable

@guggero guggero added this to the v0.6 milestone Feb 24, 2025
@guggero guggero changed the title wallet: refactor funding logic to use allocation code [wallet]: refactor funding logic to use allocation code Feb 24, 2025
@coveralls
Copy link

coveralls commented Feb 24, 2025

Pull Request Test Coverage Report for Build 13661304934

Details

  • 247 of 516 (47.87%) changed or added relevant lines in 10 files are covered.
  • 6312 unchanged lines in 93 files lost coverage.
  • Overall coverage decreased (-6.8%) to 47.726%

Changes Missing Coverage Covered Lines Changed/Added Lines %
tapsend/allocation_sort.go 0 1 0.0%
itest/assertions.go 17 22 77.27%
tapfreighter/fund.go 57 82 69.51%
tapchannel/aux_funding_controller.go 0 26 0.0%
tapsend/allocation.go 163 197 82.74%
tapchannel/aux_sweeper.go 0 36 0.0%
tapchannel/aux_closer.go 0 44 0.0%
tapchannel/commitment.go 0 98 0.0%
Files with Coverage Reduction New Missed Lines %
fn/errors.go 2 48.39%
tapfreighter/fund.go 3 70.38%
universe/interface.go 3 70.13%
commitment/encoding.go 4 68.75%
tapgarden/batch.go 4 74.23%
fn/recv.go 5 60.47%
rfqmsg/messages.go 5 85.52%
proof/courier.go 6 79.04%
tapdb/test_sqlite.go 6 0.0%
asset/records.go 7 86.67%
Totals Coverage Status
Change from base Build 13579304854: -6.8%
Covered Lines: 42671
Relevant Lines: 89408

💛 - Coveralls

@guggero guggero force-pushed the allocation-refactor branch from 08b7805 to 941d26e Compare February 25, 2025 07:54
@guggero guggero requested review from Roasbeef and ffranr February 25, 2025 08:32
@guggero guggero marked this pull request as ready for review February 25, 2025 19:49
@guggero guggero changed the title [wallet]: refactor funding logic to use allocation code [wallet 2/3]: refactor funding logic to use allocation code Feb 26, 2025
@guggero guggero force-pushed the allocation-refactor branch from 941d26e to 06ea429 Compare February 26, 2025 15:12
@guggero guggero force-pushed the group-key-refactor branch 2 times, most recently from 8c3d631 to c28aacf Compare February 26, 2025 15:33
@guggero guggero force-pushed the allocation-refactor branch from 06ea429 to 8939b42 Compare February 26, 2025 15:33
@guggero guggero force-pushed the allocation-refactor branch 2 times, most recently from ee66eb9 to 86112a5 Compare February 27, 2025 16:48
@guggero guggero changed the base branch from group-key-refactor to main February 28, 2025 07:32
@@ -328,7 +332,7 @@ func sortPiecesWithProofs(pieces []*piece) {
// asset outputs (asset UTXOs of different sizes from different tranches/asset
// IDs) according to the distribution rules provided as "allocations".
func DistributeCoins(inputs []*proof.Proof, allocations []*Allocation,
chainParams *address.ChainParams,
chainParams *address.ChainParams, interactive bool,
Copy link
Member

Choose a reason for hiding this comment

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

Still getting through the PR, but is the idea here that we'll start to use DistributeCoins more widely in the codebase? As otherwise right now, it's always used in an interactive setting.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, exactly. We'll want to use the allocation code for the wallet's funding logic, which we'll use for the channel funding and in the future for the grouped asset on-chain send.

@guggero guggero force-pushed the allocation-refactor branch from 86112a5 to cdc7cb2 Compare March 4, 2025 13:40
@guggero guggero requested review from Roasbeef and ffranr March 4, 2025 13:40
guggero added 5 commits March 4, 2025 16:52
We'll generalize the allocation code in future commits so we can re-use
it. We need to make sure we don't create circular package dependencies,
so we move the code to a more appropriate location.
The version of the virtual packet has an influence on the commitment
versions of the outputs created from it, so it's important that it can
be configured in a more generic use of the allocation code.
This is a preparatory commit that refactors the piece allocation code a
bit. This will make the actual changes in the next commit
more easy to understand.
This commit changes nothing in the behavior of the code.
The allocation code was previously only used for channel outputs, where
all virtual transactions are always interactive.
But we want to use the same code for non-interactive transfers (address
based send logic) as well, which requires a couple of tweaks.
Since we're going to be using the allocation code for all send logic, we
need to add all fields of the vOutput to the allocation, so the values
will be applied correctly.
Since we also add the explicit LockTime, we need to rename the CLTV
field so it's more clear that this field is only used for the HTLC
tiebreaking sort procedure.
Finally, we add a new Validate method to the allocation that makes sure
all required input fields are set.
@guggero guggero force-pushed the allocation-refactor branch from cdc7cb2 to be499e2 Compare March 4, 2025 16:19
Copy link
Contributor

@ffranr ffranr left a comment

Choose a reason for hiding this comment

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

🎆

guggero added 7 commits March 4, 2025 20:21
Previously, we generated one allocation per channel asset. That was
wrong and only worked because we never actually had multiple asset
pieces in a channel. The idea of an allocation is that you only have one
per on-chain output and that the different asset pieces are then
distributed between the different outputs automatically.

Because that means we need a way to give each virtual output of each
asset piece a unique script key (where required), we need to turn the
field into a functional type.
In some places (e.g. the channel funding output) each vOutput of each
different vPacket will have the same script key. In other places (e.g.
cooperative close), we derived script keys per asset ID upfront so we
need to use those for things to work correctly.
Because we'll need to convert a funding template that's potentially
given to us over the RPC interface into an allocation, we need to add
custom code for that.
This will be used in the freighter to interpret the user-provided
virtual packet into an allocation in the normal send logic.
With the new funding logic now potentially adding the change output
first, we need to make sure the channel funding logic doesn't use
hard-coded indexes.
We are now validating the input amount and types when creating the
allocations and no longer need this function.
@guggero guggero force-pushed the allocation-refactor branch from be499e2 to bb88417 Compare March 4, 2025 19:22
@Roasbeef Roasbeef requested a review from ffranr March 6, 2025 00:44
Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

LGTM 🐎

Reviewed 4 of 16 files at r4, 8 of 9 files at r8, 2 of 2 files at r9, 8 of 8 files at r10, all commit messages.
Reviewable status: all files reviewed, 10 unresolved discussions (waiting on @ffranr and @guggero)


tapchannel/aux_closer.go line 272 at r10 (raw file):

		return accu + o.Amount.Val
	}
	localSum := fn.Reduce(commitState.LocalAssets.Val.Outputs, sumAmounts)

😎

@Roasbeef Roasbeef merged commit c09bc46 into main Mar 6, 2025
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

4 participants