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

static addr loopin: add support for sweepless sweep batching #844

Merged

Conversation

sputn1ck
Copy link
Member

@sputn1ck sputn1ck commented Nov 4, 2024

This PR adds support for serverside sweepless sweep batching, by listening for notifications from the server and responding
with the signature.

Only the 8 last commits are for review

@sputn1ck sputn1ck marked this pull request as draft November 4, 2024 20:23
@sputn1ck sputn1ck force-pushed the static_sweep_batching branch from 1f5c270 to 92f540b Compare November 4, 2024 20:57
@hieblmi hieblmi force-pushed the static-addr-staging branch 3 times, most recently from 60ff75b to ec3070f Compare November 5, 2024 18:22
@sputn1ck sputn1ck force-pushed the static_sweep_batching branch 4 times, most recently from bbff3d0 to 2cbd456 Compare November 8, 2024 09:10
@sputn1ck sputn1ck marked this pull request as ready for review November 12, 2024 16:17
Copy link
Collaborator

@starius starius left a comment

Choose a reason for hiding this comment

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

LGTM! It's very cool!

Added several comments.

@@ -1617,7 +1617,7 @@ message ListStaticAddressDepositsRequest {
/*
Filters the list of all stored deposits by deposit state.
*/
DepositState state_filter = 1;
string state_filter = 1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this needed? Now it is not clear from the schema what are the possible values here. Also it is easy to confuse DepositState and StaticAddressLoopInSwapState.

Comment on lines 187 to 218
message FetchSweeplessSweepTxRequest {
// The swap hash of the swap that the client wants to fetch the sweepless
// sweep tx for.
bytes swap_hash = 1;
}

message FetchSweeplessSweepTxResponse {
// The address that the server wants to sweep the static address deposits
// to.
string sweep_addr = 1;

// A map of fee rate to the nonces.
map<uint64, ServerSweeplessSigningInfo> fee_rate_to_nonces = 2;
}

message ServerSweeplessSigningInfo {
// The nonces that the server used to generate the partial sweepless tx
// sigs.
repeated bytes nonces = 1;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think, this should be removed later, in commit "staticaddr/loopin: remove unused code".

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure, as this commit changes the functionality of what we do


// FetchLoopInByHash returns the loop-in swap with the given hash.
FetchLoopInByHash(ctx context.Context, swapHash lntypes.Hash) (
*StaticAddressLoopIn, error)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This commit does not build:

# github.com/lightninglabs/loop/staticaddr/loopin
staticaddr/loopin/actions.go:791:23: undefined: looprpc.FetchSweeplessSweepTxRequest

I think it should include SQL changes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1, the failure starts with the previous commit

Copy link
Member Author

Choose a reason for hiding this comment

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

as I'm changing the proto messages, should I add the changes in fsm/actions etc. to the proto commit?

case <-ctx.Done():
return ctx.Err()
}
}
}

// notifyNotFinished notifies the server that a swap is not finished by
// sending an empty signature map.
Copy link
Collaborator

Choose a reason for hiding this comment

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

"by sending an empty signature map"
This is a part of API.

I propose to add this to description of PushStaticAddressSweeplessSigsRequest.signing_info.

Also, we can make this more explicit by making a oneof: a map or an error (string). If the swap is not finished or any other error happened during signing, we would send PushStaticAddressSweeplessSigsRequest with an error message instead of a map.


// If the loop-in is not in the Succeeded state we return an
// error.
if loopIn.state != Succeeded {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should it also accept FetchSignPushSweeplessSweepTx and SucceededSweeplessSigFailed states?

They are removed in a subsequent commit. Maybe it worth adding them here and removing in commit "staticaddr/loopin: update fsm for server ntfn sigs".


// We'll now sign for every deposit that is part of the loop-in.
responseMap := make(map[string]*looprpc.ClientSweeplessSigningInfo)
for depositOutpoint, nonce := range req.DepositToNonces {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I propose to factor out the body of this for to a function.

// Check if all the deposits requested are part of the loop-in and
// find them in the requested sweep.
depositToIdxMap := make(map[string]int)
for reqOutpoint := range req.DepositToNonces {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I propose to factor out the body of this for to a function.

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1


for i, txIn := range sweepTx.TxIn {
if txIn.PreviousOutPoint.String() == reqOutpoint {
depositToIdxMap[reqOutpoint] = i
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's also check that req.DepositToNonces doesn't include duplicate deposits. We can add a check that depositToIdxMap[reqOutpoint] does not pre-exist here.

staticaddr/loopin/manager.go Show resolved Hide resolved
_, err = m.cfg.Server.PushStaticAddressSweeplessSigs(
ctx, &looprpc.PushStaticAddressSweeplessSigsRequest{
SwapHash: loopIn.SwapHash[:],
Txid: txHash[:],
Copy link
Collaborator

Choose a reason for hiding this comment

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

IIUC, server knows txhash already, because it depends on unsigned parts only. Can we remove the field?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd rather keep it, as the txhash is a unique identifier for the explicit sweep right?

Copy link
Collaborator

@hieblmi hieblmi left a comment

Choose a reason for hiding this comment

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

This is great work @sputn1ck, the client notifications are super nice and will surely be further extended.
I left some nits and potential simplifications. Will test it now.

swapserverrpc/server.proto Outdated Show resolved Hide resolved
@@ -287,48 +287,9 @@ func listDeposits(ctx *cli.Context) error {
}
defer cleanup()

var filterState looprpc.DepositState
Copy link
Collaborator

Choose a reason for hiding this comment

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

this commit to be reverted.

swapserverrpc/staticaddr.proto Outdated Show resolved Hide resolved
notifications/manager.go Outdated Show resolved Hide resolved
notifications/manager.go Outdated Show resolved Hide resolved
staticaddr/loopin/manager.go Outdated Show resolved Hide resolved
staticaddr/loopin/manager.go Outdated Show resolved Hide resolved
// Check if all the deposits requested are part of the loop-in and
// find them in the requested sweep.
depositToIdxMap := make(map[string]int)
for reqOutpoint := range req.DepositToNonces {
Copy link
Collaborator

Choose a reason for hiding this comment

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

+1

Index: prevout.OutputIndex,
}] = &wire.TxOut{
Value: int64(req.PrevoutInfo[i].Value),
PkScript: req.PrevoutInfo[i].PkScript,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't the pkscript the same for all deposits. If so couldn't we just call

loopIn.toPrevOuts(loopIn.Deposits, req.PrevoutInfo[0].PkScript)

staticaddr/loopin/manager.go Show resolved Hide resolved
@hieblmi hieblmi force-pushed the static-addr-staging branch from 44092a7 to e54ccfd Compare November 14, 2024 15:31
@sputn1ck sputn1ck force-pushed the static_sweep_batching branch from 985d154 to 86c65c0 Compare November 14, 2024 15:34
@hieblmi hieblmi self-requested a review November 15, 2024 13:32
Copy link
Collaborator

@hieblmi hieblmi left a comment

Choose a reason for hiding this comment

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

tAck thanks for the batching preparations!

We should fix the mentioned clean-up of musig sessions, they get auto-cleaned if the signing runs smoothly, and then cleaning fails with the mentioned error. We should only cancel sessions if the signing doesn't take place.

We should also fix the minor issues from the previous review.

staticaddr/loopin/manager.go Show resolved Hide resolved

// FetchLoopInByHash returns the loop-in swap with the given hash.
FetchLoopInByHash(ctx context.Context, swapHash lntypes.Hash) (
*StaticAddressLoopIn, error)
Copy link
Collaborator

Choose a reason for hiding this comment

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

+1, the failure starts with the previous commit

@sputn1ck sputn1ck force-pushed the static_sweep_batching branch 2 times, most recently from e3ec4e5 to 7d50c70 Compare November 18, 2024 13:30
@hieblmi hieblmi force-pushed the static-addr-staging branch from e54ccfd to 0d87b69 Compare November 18, 2024 14:58
This commit adds a new notification to the proto, which is used by the
server to request signatures for a deposit of a static loop in.
This commit adds support for the new notification of type
NotificationTypeStaticLoopInSweepRequest. This notification is sent when
a sweep request is received from the server.
This commit adds the listening for sweep requests from the server.
On a sweep request the loopin manager will fetch the loop in from the
db, do sanity and safety checks and then sign the psbt for the input
and send it back to the server.
This commit updates the loop in FSM to handle the new server based
@sputn1ck sputn1ck force-pushed the static_sweep_batching branch from 7d50c70 to 144b62c Compare November 18, 2024 16:12
@sputn1ck sputn1ck merged commit 627a830 into lightninglabs:static-addr-staging Nov 18, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants