From 6f4f0c1f316644d564109e1954cffe9402109a95 Mon Sep 17 00:00:00 2001 From: Tomasz Slabon Date: Wed, 21 Feb 2024 17:26:15 +0100 Subject: [PATCH] Enabled moved funds sweep task --- pkg/tbtcpg/moved_funds_sweep.go | 110 ++++++++++++++++++++++++++++++++ pkg/tbtcpg/tbtcpg.go | 3 +- 2 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 pkg/tbtcpg/moved_funds_sweep.go diff --git a/pkg/tbtcpg/moved_funds_sweep.go b/pkg/tbtcpg/moved_funds_sweep.go new file mode 100644 index 0000000000..eb3a299865 --- /dev/null +++ b/pkg/tbtcpg/moved_funds_sweep.go @@ -0,0 +1,110 @@ +package tbtcpg + +import ( + "fmt" + + "github.com/ipfs/go-log/v2" + "go.uber.org/zap" + + "github.com/keep-network/keep-core/pkg/bitcoin" + "github.com/keep-network/keep-core/pkg/tbtc" +) + +type MovedFundsSweepTask struct { + chain Chain + btcChain bitcoin.Chain +} + +func NewMovedFundsSweepTask( + chain Chain, + btcChain bitcoin.Chain, +) *MovedFundsSweepTask { + return &MovedFundsSweepTask{ + chain: chain, + btcChain: btcChain, + } +} + +func (mfst *MovedFundsSweepTask) Run(request *tbtc.CoordinationProposalRequest) ( + tbtc.CoordinationProposal, + bool, + error, +) { + walletPublicKeyHash := request.WalletPublicKeyHash + + taskLogger := logger.With( + zap.String("task", mfst.ActionType().String()), + zap.String("walletPKH", fmt.Sprintf("0x%x", walletPublicKeyHash)), + ) + + walletChainData, err := mfst.chain.GetWallet(walletPublicKeyHash) + if err != nil { + return nil, false, fmt.Errorf( + "cannot get wallet's chain data: [%w]", + err, + ) + } + + if walletChainData.State != tbtc.StateLive && + walletChainData.State != tbtc.StateMovingFunds { + taskLogger.Infof("wallet not in Live or MoveFunds state") + return nil, false, nil + } + + if walletChainData.PendingMovedFundsSweepRequestsCount == 0 { + taskLogger.Infof("wallet has no pending moved funds sweep requests") + return nil, false, nil + } + + movingFundsTxHash, movingFundsOutputIdx, err := mfst.FindMovingFundsTxData( + walletPublicKeyHash, + ) + if err != nil { + return nil, false, fmt.Errorf( + "cannot find moving funds transaction data: [%w]", + err, + ) + } + + proposal, err := mfst.ProposeMovedFundsSweep( + taskLogger, + walletPublicKeyHash, + movingFundsTxHash, + movingFundsOutputIdx, + 0, + ) + if err != nil { + return nil, false, fmt.Errorf( + "cannot prepare moved funds sweep proposal: [%w]", + err, + ) + } + + return proposal, true, nil +} + +func (mfst *MovedFundsSweepTask) FindMovingFundsTxData( + walletPublicKeyHash [20]byte, +) ( + txHash bitcoin.Hash, + txOutputIdx uint32, + err error, +) { + // TODO: Implement + return bitcoin.Hash{}, 0, nil +} + +func (mfst *MovedFundsSweepTask) ActionType() tbtc.WalletActionType { + return tbtc.ActionMovedFundsSweep +} + +func (mfst *MovedFundsSweepTask) ProposeMovedFundsSweep( + taskLogger log.StandardLogger, + walletPublicKeyHash [20]byte, + movingFundsTxHash bitcoin.Hash, + movingFundsTxIndex uint32, + fee int64, +) (*tbtc.MovedFundsSweepProposal, error) { + // TODO: Implement + return nil, nil +} diff --git a/pkg/tbtcpg/tbtcpg.go b/pkg/tbtcpg/tbtcpg.go index f14cd1f7f4..9e12735fd9 100644 --- a/pkg/tbtcpg/tbtcpg.go +++ b/pkg/tbtcpg/tbtcpg.go @@ -41,8 +41,7 @@ func NewProposalGenerator( NewRedemptionTask(chain, btcChain), NewHeartbeatTask(chain), NewMovingFundsTask(chain, btcChain), - // TODO: Uncomment when moving funds support is implemented. - // newMovedFundsSweepTask(), + NewMovedFundsSweepTask(chain, btcChain), } return &ProposalGenerator{