-
Notifications
You must be signed in to change notification settings - Fork 10
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
feat: implement of btc-staker improvement 107 #134
feat: implement of btc-staker improvement 107 #134
Conversation
Commit Summary 0233d1a
@KonradStaniec |
staker/stakerapp.go
Outdated
if err != nil { | ||
return nil, nil, fmt.Errorf("cannot spend staking output. Error converting fpBtcPkList to btcPkList: %w", err) | ||
} | ||
|
||
spendStakeTxInfo, err := createSpendStakeTxFromStoredTx( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this functions has inside the follwing checks:
if storedtx.StakingTxConfirmedOnBtc() && !storedtx.UnbondingTxConfirmedOnBtc() {
and storedtx.StakingTxConfirmedOnBtc()
was depended on the old post approval flow.
I think now we must remove this check.
This also touches:
func (app *App) WithdrawableTransactions(limit, offset uint64) (*stakerdb.StoredTransactionQueryResult, error) {
function i.e this function currently returns results based on this field.
I think now delegation is withdrawable if:
- the staking transaction is still on BTC and its timelock has expired
- the unbonding transaction is still on BTC and its timelock has expired
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I modified the createSpendStakeTxFromStoredTx function and its calling part to align with your suggestion.
fixed 380b429
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this functions has inside the follwing checks:
if storedtx.StakingTxConfirmedOnBtc() && !storedtx.UnbondingTxConfirmedOnBtc() {
and
storedtx.StakingTxConfirmedOnBtc()
was depended on the old post approval flow.I think now we must remove this check.
This also touches:
func (app *App) WithdrawableTransactions(limit, offset uint64) (*stakerdb.StoredTransactionQueryResult, error) {
function i.e this function currently returns results based on this field.
I think now delegation is withdrawable if:
- the staking transaction is still on BTC and its timelock has expired
- the unbonding transaction is still on BTC and its timelock has expired
We need to discuss the WithdrawableTransactions function.
Currently, this function queries all transactions stored in the database and checks whether StakingTxConfirmedOnBtc() or UnbondingTxConfirmedOnBtc() is used. Based on this, it sets the corresponding values and then verifies whether timeLockExpired is used.
Due to this structure, we had to maintain StakingTxConfirmationInfo and UnbondingStoreData.UnbondingTxConfirmationInfo in the database. Additionally, we also store the StakingTime and UnbondingTime fields in the database.
If these fields are removed, we would need to call the RPC for each transaction to fetch the required values, which seems highly inefficient. Moreover, adding an RPC call inside the QueryStoredTransactions function does not seem structurally ideal.
If querying all withdrawable transactions is not necessary, it might be a better approach to implement a function that checks whether specific requested transactions are withdrawable. Please share your thoughts on this.
However, with this commit, spent transactions are removed from the database, meaning only actively staked transactions remain. This might reduce the overall burden on the database. I will proceed with this approach and provide further comments after testing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If querying all withdrawable transactions is not necessary, it might be a better approach to implement a function that checks whether specific requested transactions are withdrawable. Please share your thoughts on this.
Tbh I feel this function is hard requiremnt for the users i.e If I am a user and have multiple delegations (each in different stage) I should have way to check which of those I can withdraw to my wallet.
In the future such functionality can be automatic based on this information i.e
- periodically query all withdrawable transactions
- and withdraw them to user wallet
If these fields are removed, we would need to call the RPC for each transaction to fetch the required values, which seems highly inefficient. Moreover, adding an RPC call inside the QueryStoredTransactions function does not seem structurally ideal.
ahh agreed on that. this means we need to fill StakingTxConfirmedOnBtc()
field in db, after tx delegation is active on Babylon
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed cb94a22
@KonradStaniec
I modified WithdrawableTransaction
to retrieve the necessary data via RPC instead of the database.
As a result, since the database is no longer used, I created it as a separate function rather than passing a query statement to QueryStoredTransactions
. After retrieving the status from the Babylon chain, I then obtained the confirmation information from Bitcoin based on the transaction status.
if unbondingTxStatus == walletcontroller.TxNotFound { | ||
// no unbonding tx on chain and staking output already spent, most probably | ||
// staking transaction has been withdrawn, update state in db | ||
return nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder what would be best handling of this 🤔 maybe we need to add some operations to prune db from spent staking transactions to not have them in db forever or maybe we need to add some bool field to db, like withdrawn bool
wdyt ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be a good idea to remove that transaction content from the database. Otherwise, functions that query the entire database, such as QueryStoredTransactions, will retrieve unnecessary items.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed 380b429
stakerservice/service.go
Outdated
} | ||
|
||
details := storedTxToStakingDetails(storedTx) | ||
details.StakingState = di.BtcDelegation.GetStatusDesc() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
those details are also filed in withdrawableTransactions
endpoint
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed 380b429
staker/stakerapp.go
Outdated
stakingTxHash *chainhash.Hash, | ||
) { | ||
defer app.wg.Done() | ||
defer waitEv.Cancel() | ||
unbondingTxHash := unbondingData.UnbondingTx.TxHash() | ||
// unbondingTxHash := unbondingData.UnbondingTx.TxHash() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
commented out code ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed 380b429
Commit Summary 380b429
|
Commit Summary cb94a22
At this stage, the database will look like this: type StoredTransaction struct {
StoredTransactionIdx uint64
StakingTx *wire.MsgTx
StakerAddress string
} |
Commit Summary 8628a24
|
staker/stakerapp.go
Outdated
|
||
var totalTxCount int | ||
// add stored transactions to slice | ||
if err := app.txTracker.ScanTrackedTransactions(func(tx *stakerdb.StoredTransaction) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think here we need to:
- first return all the hashes in db (similarity like in
checkTransactionsStatus
) - and then iterate over all the hashes to check the status on Babylon and BTC
Otherwise we will take the read lock on db for a long time given all the queries we are making.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right.
I will try modifying the function by applying offset and limit as described below.
Previously, I returned storedTransaction from ScanTrackedTransactions
and processed it directly in RPC, so I didn’t feel the need for offset and limit.
I will change the logic as follows:
- Apply offset and limit to the
StoredTransactions
function to first retrieve the transaction list. - Process the retrieved transactions one by one based on their status.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed 2c2adee
// Contains information about btc confirmation | ||
message BTCConfirmationInfo { | ||
uint32 block_height = 1; | ||
bytes block_hash = 2; | ||
} | ||
|
||
message CovenantSig { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think BTCConfirmationInfo
is not longer used
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed 2c2adee
stakerservice/service.go
Outdated
"btc_delegation_from_btc_staking_tx": rpc.NewRPCFunc(s.btcDelegationFromBtcStakingTx, "stakerAddress,btcStkTxHash,tag,covenantPksHex,covenantQuorum"), | ||
"staking_details": rpc.NewRPCFunc(s.stakingDetails, "stakingTxHash"), | ||
"spend_stake": rpc.NewRPCFunc(s.spendStake, "stakingTxHash"), | ||
"list_staking_transactions": rpc.NewRPCFunc(s.listStakingTransactions, "offset,limit"), | ||
"unbond_staking": rpc.NewRPCFunc(s.unbondStaking, "stakingTxHash"), | ||
"withdrawable_transactions": rpc.NewRPCFunc(s.withdrawableTransactions, "offset,limit"), | ||
"withdrawable_transactions": rpc.NewRPCFunc(s.withdrawableTransactions, ""), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you think pagination does not make sense here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed 2c2adee
Commit Summary 2c2adee
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good ! thanks for this contribution !
1101a57
into
babylonlabs-io:feat/b-harvest-improve-btc-staker
* feat: implement of btc-staker improvement 107 (#134) * feat: remove watch-staking endpoint, post-approval flow * feat: remove db fields - state, fpkey and pop * feat: update CHANGELOG * feat: remove stakingoutputIdx, btcTxHash. refactoring UnbondingTxData and etc * feat: remove confirmation fields and modify WithdrawableTransactions * feat: cleanup codes including comments, error wrapping etc. * feat: change WithdrawableTransactions logic and set pagination * Konradstaniec/bump changelog (#141) Co-authored-by: RafilxTenfen <[email protected]> --------- Co-authored-by: wonjoon <[email protected]> Co-authored-by: RafilxTenfen <[email protected]>
This PR includes the implementation for the btc-staker improvement related to issue #107 .
The removal of the watch-staking endpoint and the post-approval flow were approved in the previous PR(#131). This PR focuses on 'reducing the amount of state stored in the database'.
The details of the implementation are as follows:
Reduce the amount of state held in the Database
Previously, btc-staker had the following structural shortcomings due to the inclusion of watch-staking endpoints and post-approval flows:
In the previous PR(#131), the watch-staking endpoint and post-approval flow were removed, simplifying the database structure. Additionally, information that can be directly queried from the chain (e.g., transaction progress status) is no longer stored in the database.
The existing database structure is as follows:
The reduced db looks like this:
The deleted fields can be retrieved directly from Babylon or Bitcoin when needed. Additionally, some fields are no longer used due to the removal of the post-approval flow.