Skip to content

Commit

Permalink
Added contract function call for moving funds commitment
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Nov 30, 2023
1 parent 2172896 commit 4909a25
Showing 1 changed file with 59 additions and 6 deletions.
65 changes: 59 additions & 6 deletions pkg/tbtc/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,12 +679,12 @@ func (n *node) handleMovingFundsProposal(proposal *MovingFundsProposal) {
)

// Make sure the wallet meets the criteria for moving funds proposal.
_, found := n.walletRegistry.getWalletByPublicKeyHash(
walletRegistryData, found := n.walletRegistry.getWalletByPublicKeyHash(
sourceWalletPublicKeyHash,
)
if !found {
logger.Errorf(
"skipping moving funds proposal for wallet with PKH"+
"skipping moving funds proposal for wallet with PKH "+
"[0x%x] as the node does not control it",
sourceWalletPublicKeyHash,
)
Expand Down Expand Up @@ -719,7 +719,7 @@ func (n *node) handleMovingFundsProposal(proposal *MovingFundsProposal) {

walletBalance := walletMainUtxo.Value

// Check if the wallet meets the condition for moving funds
// Check if the wallet meets the conditions for moving funds
// commitment.
if sourceWalletChainData.State != StateMovingFunds ||
sourceWalletChainData.PendingRedemptionsValue > 0 ||
Expand Down Expand Up @@ -749,7 +749,9 @@ func (n *node) handleMovingFundsProposal(proposal *MovingFundsProposal) {

if liveWalletsCount == 0 {
logger.Infof(
"skipping moving funds proposal due to lack of live wallets",
"skipping moving funds proposal for wallet with PKH [0x%x] due"+
"to lack of live wallets",
sourceWalletPublicKeyHash,
)
return
}
Expand Down Expand Up @@ -803,7 +805,11 @@ func (n *node) handleMovingFundsProposal(proposal *MovingFundsProposal) {

logger.Infof("found [%v] live wallets", len(liveWallets))

_, _, _, _, _, walletMaxBtcTransfer, _, _ := n.chain.GetWalletParameters()
_, _, _, _, _, walletMaxBtcTransfer, _, err := n.chain.GetWalletParameters()
if err != nil {
logger.Errorf("failed to get wallet parameters: [%v]", err)
return
}

ceilingDivide := func(x, y uint64) uint64 {
return (x + y - 1) / y
Expand All @@ -823,14 +829,61 @@ func (n *node) handleMovingFundsProposal(proposal *MovingFundsProposal) {
targetWallets := liveWallets[0:targetWalletsCount]
logger.Infof("Target wallets length [%v]", len(targetWallets))

// TODO: Continue with moving funds commitment
walletMemberIDs := make([]uint32, 0)
for _, operatorAddress := range walletRegistryData.signingGroupOperators {
operatorId, err := n.chain.GetOperatorID(operatorAddress)
if err != nil {
logger.Errorf(
"failed to get operator ID for operator [%v] belonging to "+
"wallet with PKH [0x%x]: [%v]",
operatorAddress,
sourceWalletPublicKeyHash,
err,
)
return
}
walletMemberIDs = append(walletMemberIDs, operatorId)
}

latestBlockHeight, err := n.btcChain.GetLatestBlockHeight()
if err != nil {
logger.Errorf(
"failed to get latest Bitcoin block height: [%v]",
err,
)
return
}

// Use the latest Bitcoin block height to determine the wallet member
// index. Increase the result of the modulo operation by one since the
// wallet member index must be within range [1, len(walletMemberIDs)].
walletMemberIndex := (int(latestBlockHeight) % len(walletMemberIDs)) + 1

err = n.chain.SubmitMovingFundsCommitment(
sourceWalletPublicKeyHash,
*walletMainUtxo,
walletMemberIDs,
uint32(walletMemberIndex),
targetWallets,
)
if err != nil {
logger.Errorf(
"failed to submit moving funds commitment for wallet wit PKH "+
"[0x%x]: [%v]",
sourceWalletPublicKeyHash,
err,
)
return
}

logger.Infof(
"Finished moving funds commitment for wallet with PKH [0x%x]",
sourceWalletPublicKeyHash,
)

// TODO: Add construction of the move funds Bitcoin transaction.
// Before proceeding with the Bitcoin transaction, check if the
// commitment was successfully submitted.
}()
}

Expand Down

0 comments on commit 4909a25

Please sign in to comment.