diff --git a/pkg/tbtc/node.go b/pkg/tbtc/node.go index 66d5697ead..fa6fc95ff0 100644 --- a/pkg/tbtc/node.go +++ b/pkg/tbtc/node.go @@ -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, ) @@ -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 || @@ -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 } @@ -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 @@ -823,7 +829,52 @@ 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]", @@ -831,6 +882,8 @@ func (n *node) handleMovingFundsProposal(proposal *MovingFundsProposal) { ) // TODO: Add construction of the move funds Bitcoin transaction. + // Before proceeding with the Bitcoin transaction, check if the + // commitment was successfully submitted. }() }