Skip to content

staticaddr: persist withdrawal info #938

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

Merged
merged 6 commits into from
Jun 10, 2025

Conversation

hieblmi
Copy link
Collaborator

@hieblmi hieblmi commented May 9, 2025

addresses #936

sample output:

loop --network=regtest s listwithdrawals  
{
    "withdrawals": [
        {
            "tx_id": "9379f48176ad724396322018b1a6f04ae6a19e868bd5fb0ef1d44a28889bf6a9",
            "outpoints": [
                "4cf5f636a598ffdb4fcd9ba9f2574a4a4cb2347decce88f25c4b0efa9e7a4e58:0",
                "29062f15a18cd1fc16a7701acbd608c232e3063302716a138f34b908de371d69:1"
            ],
            "total_deposit_amount_satoshis": "500000",
            "withdrawn_amount_satoshis": "483150",
            "change_amount_satoshis": "0",
            "confirmation_height": 129
        },
        {
            "tx_id": "5f2943ef018617cc3b0e3967b6e34a1ca472c5896282425f96c2bae159da25d9",
            "outpoints": [
                "25a16919cc297f21e6d25812dd4cb6d9c5f6b7158dba25522d3b658fefd8bc0a:1",
                "be4f24f4dde6cc51b5f9e06132cf5fcad895afcb0b7b471c627cd3bd71003e53:0",
                "5a5b8c7ea10409c81c6f8637402b83c008a4f3fdf4f5b02b52069b72df50ba11:0",
                "0dc433eef2191ecefdf950e111850f650905d9b9f2350a90766fee137e476858:0",
                "5bed66d98a76bda5fa1e35cd86060e8c15a3d7432a2d3c4708294decc54852d2:0"
            ],
            "total_deposit_amount_satoshis": "1034600",
            "withdrawn_amount_satoshis": "1000000",
            "change_amount_satoshis": "34216",
            "confirmation_height": 150
        }
    ]
}

UPDATE:

 loop --network=regtest s listwithdrawals
{
    "withdrawals": [
        {
            "tx_id": "65034b12a722ca48bbd09cbd587ab0ac8635a0c1caee9fd7fbf0170c632574ae",
            "deposits": [
                {
                    "id": "1de5c33d7504aba6d3fc43775ef32d9102e519a43ff27c5287dcc2b60bab1f45",
                    "state": "WITHDRAWN",
                    "outpoint": "806bbe5581ba17a3600b1f37b4c737f83de0eca0880883c8f1f916af67a9f42d:0",
                    "value": "250000",
                    "confirmation_height": "141",
                    "blocks_until_expiry": "0"
                },
                {
                    "id": "59ea2b6718a1578607227f35581eeec3c4c06372c0c3b960170a71b16be1e852",
                    "state": "WITHDRAWN",
                    "outpoint": "7aafba908731ef1c4187ef889e6c69282dc285449f9857e2a15b2108ff6bf83e:0",
                    "value": "250000",
                    "confirmation_height": "141",
                    "blocks_until_expiry": "0"
                }
            ],
            "total_deposit_amount_satoshis": "500000",
            "withdrawn_amount_satoshis": "498315",
            "change_amount_satoshis": "0",
            "confirmation_height": 147
        },
        {
            "tx_id": "bbcd158b3bf1a6ff0c5061e9971119408686d9cd925b90aec714bde8fbe78a79",
            "deposits": [
                {
                    "id": "94eb9b597ecebc7bb63776698793d078da8280b5a76b6cd7137b1d436a9ed000",
                    "state": "WITHDRAWN",
                    "outpoint": "708d4e59b3059870d030fb3f9b18377253bc46ca73b9b240b2d15b114bf13442:1",
                    "value": "250000",
                    "confirmation_height": "129",
                    "blocks_until_expiry": "0"
                }
            ],
            "total_deposit_amount_satoshis": "250000",
            "withdrawn_amount_satoshis": "249889",
            "change_amount_satoshis": "0",
            "confirmation_height": 135
        }
    ]
}

@hieblmi hieblmi marked this pull request as draft May 9, 2025 13:18
@hieblmi hieblmi self-assigned this May 9, 2025
@hieblmi hieblmi force-pushed the withdraw-info branch 2 times, most recently from aab693b to e22b2b2 Compare May 13, 2025 09:40
@hieblmi hieblmi marked this pull request as ready for review May 13, 2025 09:40
@hieblmi hieblmi requested review from sputn1ck, bhandras and starius May 13, 2025 10:01
@hieblmi
Copy link
Collaborator Author

hieblmi commented May 15, 2025

Rebased.

@hieblmi
Copy link
Collaborator Author

hieblmi commented May 20, 2025

Rebased

Copy link
Member

@sputn1ck sputn1ck left a comment

Choose a reason for hiding this comment

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

LGTM, but I think a quick sql store unit test would be nice


// SqlStore is the backing store for static address withdrawals.
type SqlStore struct {
baseDB *loopdb.BaseDB
Copy link
Member

@sputn1ck sputn1ck May 20, 2025

Choose a reason for hiding this comment

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

this exposes the full queries to this package, which IMO is not neccessary. as we usually define a Querier interface.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed


-- deposit_outpoints is a concatenated list of outpoints that are used for
-- this withdrawal. The list has the format txid1:idx;txid2:idx;...
deposit_outpoints TEXT NOT NULL,
Copy link
Member

Choose a reason for hiding this comment

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

could it make sense to really sqlize this? and reference the db deposit id?
So add a table with:
ID, withdrawal_id REFRERNCES withdrawals.id(or tx_id), deposit_id REFERENCES(deposits.deposit_id)
I noticed we don't do the same in static loop ins, but it would allow for more interesting queries i feel.

Copy link
Member

Choose a reason for hiding this comment

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

+1

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I normalized the withdrawal and depoist tables by adding a new table withdrawal_deposits and removed the deposit outpoints from the withdrawal record.

}

// CreateWithdrawal creates a static address withdrawal record in the database.
func (s *SqlStore) CreateWithdrawal(ctx context.Context, tx *wire.MsgTx,
Copy link
Member

Choose a reason for hiding this comment

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

I propose adding a light unit test for future regressions.

Copy link
Member

Choose a reason for hiding this comment

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

+1

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added sql_store_test.go.

Copy link
Member

@bhandras bhandras left a comment

Choose a reason for hiding this comment

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

Really nice feature, added a couple of comments/suggestions.


-- deposit_outpoints is a concatenated list of outpoints that are used for
-- this withdrawal. The list has the format txid1:idx;txid2:idx;...
deposit_outpoints TEXT NOT NULL,
Copy link
Member

Choose a reason for hiding this comment

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

+1


-- confirmation_height is the block height at which the withdrawal was
-- first confirmed.
confirmation_height BIGINT NOT NULL
Copy link
Member

Choose a reason for hiding this comment

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

Should this be allowed to be NULL until the withdrawal confirms?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good point

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

m.mu.Lock()
delete(m.finalizedWithdrawalTxns, txHash)
m.mu.Unlock()

// Persist info about the finalized withdrawal.
err = m.cfg.Store.CreateWithdrawal(
Copy link
Member

Choose a reason for hiding this comment

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

It'd be nicer to add the withdrawal at the time it is requested then later on set it to confirmed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've reworked this so that we store a withdrawal at user request time with CreateWithdrawal and then when its confirmed its updated with UpdateWithdrawal.

}

// CreateWithdrawal creates a static address withdrawal record in the database.
func (s *SqlStore) CreateWithdrawal(ctx context.Context, tx *wire.MsgTx,
Copy link
Member

Choose a reason for hiding this comment

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

+1

@bhandras bhandras removed the request for review from starius May 20, 2025 11:43
@lightninglabs-deploy
Copy link

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

@hieblmi hieblmi force-pushed the withdraw-info branch 3 times, most recently from c835f14 to ba77fff Compare June 2, 2025 13:57
@hieblmi hieblmi force-pushed the withdraw-info branch 2 times, most recently from 469b6a2 to 8d95a80 Compare June 3, 2025 11:30
@hieblmi hieblmi requested review from bhandras and sputn1ck June 3, 2025 11:31
Copy link
Member

@sputn1ck sputn1ck left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for the sql improvements and tests 💯

Copy link
Member

@bhandras bhandras left a comment

Choose a reason for hiding this comment

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

Looks good, just one question!

createArgs := sqlc.CreateWithdrawalParams{
WithdrawalID: id[:],
TotalDepositAmount: int64(totalAmount),
InitiationTime: time.Now(),
Copy link
Member

Choose a reason for hiding this comment

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

nit: use the clock.

Also I noticed that there's mixed use of UTC vs non UTC timestamps in the staticaddr databases. I think we should always try to use UTC when possible.

@hieblmi hieblmi requested a review from bhandras June 10, 2025 14:33
Copy link
Member

@bhandras bhandras left a comment

Choose a reason for hiding this comment

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

LGTM 🎉

@hieblmi hieblmi merged commit 80828c2 into lightninglabs:master Jun 10, 2025
4 checks passed
@hieblmi hieblmi deleted the withdraw-info branch June 10, 2025 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants