Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 103 additions & 13 deletions pkg/tbtcpg/moved_funds_sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,50 @@ func (mfst *MovedFundsSweepTask) ProposeMovedFundsSweep(
)
}

estimatedFee, err := EstimateMovedFundsSweepFee(
movedFundsOutputScript, err := movedFundsSweepOutputScript(
mfst.btcChain,
movingFundsTxHash,
movingFundsTxOutputIndex,
)
if err != nil {
return nil, fmt.Errorf(
"cannot resolve moved funds output script: [%w]",
err,
)
}

walletData, err := mfst.chain.GetWallet(walletPublicKeyHash)
if err != nil {
return nil, fmt.Errorf(
"cannot get wallet chain data: [%w]",
err,
)
}
if walletData == nil {
return nil, fmt.Errorf("wallet chain data is nil")
}

walletOutputScript, err := tbtc.WalletOutputScript(
walletPublicKeyHash,
walletData.WalletID,
)
if err != nil {
return nil, fmt.Errorf(
"cannot compute wallet output script: [%w]",
err,
)
}

estimatedFee, err := EstimateMovedFundsSweepFeeForScripts(
mfst.btcChain,
movedFundsOutputScript,
walletOutputScript,
hasMainUtxo,
sweepTxMaxTotalFee,
)
if err != nil {
return nil, fmt.Errorf(
"cannot estimate moving funds transaction fee: [%w]",
"cannot estimate moved funds sweep transaction fee: [%w]",
err,
)
}
Expand Down Expand Up @@ -372,25 +408,79 @@ func (mfst *MovedFundsSweepTask) ProposeMovedFundsSweep(
return proposal, nil
}

// EstimateMovedFundsSweepFee estimates fee for the moved funds sweep transaction
// that merges the received main UTXO from the source wallets with the current
// wallet's main UTXO.
func movedFundsSweepOutputScript(
btcChain bitcoin.Chain,
movingFundsTxHash bitcoin.Hash,
movingFundsTxOutputIndex uint32,
) (bitcoin.Script, error) {
transaction, err := btcChain.GetTransaction(movingFundsTxHash)
if err != nil {
return nil, fmt.Errorf("cannot get moving funds transaction: [%w]", err)
}
if transaction == nil {
return nil, fmt.Errorf("moving funds transaction is nil")
}

if movingFundsTxOutputIndex >= uint32(len(transaction.Outputs)) {
return nil, fmt.Errorf(
"moving funds output index [%d] out of range for transaction with [%d] outputs",
movingFundsTxOutputIndex,
len(transaction.Outputs),
)
}

output := transaction.Outputs[movingFundsTxOutputIndex]
if output == nil {
return nil, fmt.Errorf(
"moving funds transaction output [%d] is nil",
movingFundsTxOutputIndex,
)
}

return output.PublicKeyScript, nil
}

// EstimateMovedFundsSweepFee estimates a legacy P2WPKH moved-funds sweep fee.
// Call EstimateMovedFundsSweepFeeForScripts when either the moved-funds output
// or the wallet output may use another script type.
func EstimateMovedFundsSweepFee(
btcChain bitcoin.Chain,
hasMainUtxo bool,
sweepTxMaxTotalFee uint64,
) (int64, error) {
// The transaction always has an input coming from the moved funds
// transferred by the source wallet. Additionally, it may have the second
// input which is the wallet main UTXO.
inputCount := 1
if hasMainUtxo {
inputCount++
legacyWalletOutputScript, err := bitcoin.PayToWitnessPublicKeyHash([20]byte{})
if err != nil {
return 0, fmt.Errorf("cannot construct legacy wallet output script: [%v]", err)
}

return EstimateMovedFundsSweepFeeForScripts(
btcChain,
legacyWalletOutputScript,
legacyWalletOutputScript,
hasMainUtxo,
sweepTxMaxTotalFee,
)
}

// EstimateMovedFundsSweepFeeForScripts estimates fee for a moved-funds sweep
// transaction using the resolved moved-funds input and wallet output scripts.
func EstimateMovedFundsSweepFeeForScripts(
btcChain bitcoin.Chain,
movedFundsOutputScript bitcoin.Script,
walletOutputScript bitcoin.Script,
hasMainUtxo bool,
sweepTxMaxTotalFee uint64,
) (int64, error) {
sizeEstimator := bitcoin.NewTransactionSizeEstimator().
AddPublicKeyHashInputs(inputCount, true).
AddPublicKeyHashOutputs(1, true)
AddPublicKeyScriptInput(movedFundsOutputScript)

// The transaction may have a second input spending the wallet's main UTXO.
// That UTXO is locked using the same script as the new wallet output.
if hasMainUtxo {
sizeEstimator.AddPublicKeyScriptInput(walletOutputScript)
}

sizeEstimator.AddOutputScript(walletOutputScript)

transactionSize, err := sizeEstimator.VirtualSize()
if err != nil {
Expand Down
136 changes: 108 additions & 28 deletions pkg/tbtcpg/moved_funds_sweep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,28 +346,40 @@ func TestMovedFundsSweepAction_ProposeMovedFundsSweep(t *testing.T) {

movingFundsTxOutputIndex := uint32(1)

hasMainUtxo := true

var tests = map[string]struct {
fee int64
walletID [32]byte
hasMainUtxo bool
expectedProposal *tbtc.MovedFundsSweepProposal
}{
"fee provided": {
fee: 10000,
fee: 10000,
hasMainUtxo: true,
expectedProposal: &tbtc.MovedFundsSweepProposal{
MovingFundsTxHash: movingFundsTxHash,
MovingFundsTxOutputIndex: movingFundsTxOutputIndex,
SweepTxFee: big.NewInt(10000),
},
},
"fee estimated": {
fee: 0, // trigger fee estimation
"legacy fee estimated": {
fee: 0, // trigger fee estimation
hasMainUtxo: true,
expectedProposal: &tbtc.MovedFundsSweepProposal{
MovingFundsTxHash: movingFundsTxHash,
MovingFundsTxOutputIndex: movingFundsTxOutputIndex,
SweepTxFee: big.NewInt(4450),
},
},
"fresh FROST wallet fee estimated": {
fee: 0, // trigger fee estimation
walletID: [32]byte{0x01},
hasMainUtxo: false,
expectedProposal: &tbtc.MovedFundsSweepProposal{
MovingFundsTxHash: movingFundsTxHash,
MovingFundsTxOutputIndex: movingFundsTxOutputIndex,
SweepTxFee: big.NewInt(2775),
},
},
}

for testName, test := range tests {
Expand All @@ -391,7 +403,28 @@ func TestMovedFundsSweepAction_ProposeMovedFundsSweep(t *testing.T) {
0,
)

err := tbtcChain.SetMovedFundsSweepProposalValidationResult(
walletOutputScript, err := tbtc.WalletOutputScript(
walletPublicKeyHash,
test.walletID,
)
if err != nil {
t.Fatal(err)
}

tbtcChain.SetWallet(
walletPublicKeyHash,
&tbtc.WalletChainData{WalletID: test.walletID},
)
movingFundsTxOutputs := make([]*bitcoin.TransactionOutput, 2)
movingFundsTxOutputs[movingFundsTxOutputIndex] = &bitcoin.TransactionOutput{
PublicKeyScript: walletOutputScript,
}
btcChain.SetTransaction(
movingFundsTxHash,
&bitcoin.Transaction{Outputs: movingFundsTxOutputs},
)

err = tbtcChain.SetMovedFundsSweepProposalValidationResult(
walletPublicKeyHash,
test.expectedProposal,
true,
Expand All @@ -407,7 +440,7 @@ func TestMovedFundsSweepAction_ProposeMovedFundsSweep(t *testing.T) {
walletPublicKeyHash,
movingFundsTxHash,
movingFundsTxOutputIndex,
hasMainUtxo,
test.hasMainUtxo,
test.fee,
)
if err != nil {
Expand All @@ -421,30 +454,63 @@ func TestMovedFundsSweepAction_ProposeMovedFundsSweep(t *testing.T) {
}
}

func TestEstimateMovedFundsSweepFee(t *testing.T) {
func TestEstimateMovedFundsSweepFeeForScripts(t *testing.T) {
legacyWalletScript, err := bitcoin.PayToWitnessPublicKeyHash([20]byte{0x01})
if err != nil {
t.Fatal(err)
}
taprootWalletScript, err := bitcoin.PayToTaproot([32]byte{0x02})
if err != nil {
t.Fatal(err)
}

var tests = map[string]struct {
sweepTxMaxTotalFee uint64
hasMainUtxo bool
expectedFee uint64
expectedError error
movedFundsOutputScript bitcoin.Script
walletOutputScript bitcoin.Script
sweepTxMaxTotalFee uint64
hasMainUtxo bool
expectedFee uint64
expectedError error
}{
"estimated fee correct, one input": {
sweepTxMaxTotalFee: 3000,
hasMainUtxo: false,
expectedFee: 1760,
expectedError: nil,
"legacy scripts, one input": {
movedFundsOutputScript: legacyWalletScript,
walletOutputScript: legacyWalletScript,
sweepTxMaxTotalFee: 3000,
hasMainUtxo: false,
expectedFee: 1760,
expectedError: nil,
},
"legacy scripts, two inputs": {
movedFundsOutputScript: legacyWalletScript,
walletOutputScript: legacyWalletScript,
sweepTxMaxTotalFee: 3000,
hasMainUtxo: true,
expectedFee: 2848,
expectedError: nil,
},
"estimated fee correct, two inputs": {
sweepTxMaxTotalFee: 3000,
hasMainUtxo: true,
expectedFee: 2848,
expectedError: nil,
"Taproot scripts, one input": {
movedFundsOutputScript: taprootWalletScript,
walletOutputScript: taprootWalletScript,
sweepTxMaxTotalFee: 3000,
hasMainUtxo: false,
expectedFee: 1776,
expectedError: nil,
},
"estimated fee too high": {
sweepTxMaxTotalFee: 2500,
hasMainUtxo: true,
expectedFee: 0,
expectedError: tbtcpg.ErrSweepTxFeeTooHigh,
"Taproot scripts, two inputs": {
movedFundsOutputScript: taprootWalletScript,
walletOutputScript: taprootWalletScript,
sweepTxMaxTotalFee: 3000,
hasMainUtxo: true,
expectedFee: 2704,
expectedError: nil,
},
"estimated Taproot fee too high": {
movedFundsOutputScript: taprootWalletScript,
walletOutputScript: taprootWalletScript,
sweepTxMaxTotalFee: 2700,
hasMainUtxo: true,
expectedFee: 0,
expectedError: tbtcpg.ErrSweepTxFeeTooHigh,
},
}

Expand All @@ -453,8 +519,10 @@ func TestEstimateMovedFundsSweepFee(t *testing.T) {
btcChain := tbtcpg.NewLocalBitcoinChain()
btcChain.SetEstimateSatPerVByteFee(1, 16)

actualFee, err := tbtcpg.EstimateMovedFundsSweepFee(
actualFee, err := tbtcpg.EstimateMovedFundsSweepFeeForScripts(
btcChain,
test.movedFundsOutputScript,
test.walletOutputScript,
test.hasMainUtxo,
test.sweepTxMaxTotalFee,
)
Expand All @@ -475,6 +543,18 @@ func TestEstimateMovedFundsSweepFee(t *testing.T) {
}
}

func TestEstimateMovedFundsSweepFee(t *testing.T) {
btcChain := tbtcpg.NewLocalBitcoinChain()
btcChain.SetEstimateSatPerVByteFee(1, 16)

actualFee, err := tbtcpg.EstimateMovedFundsSweepFee(btcChain, true, 3000)
if err != nil {
t.Fatal(err)
}

testutils.AssertUintsEqual(t, "fee", 2848, uint64(actualFee))
}

func hashFromString(str string) bitcoin.Hash {
hash, err := bitcoin.NewHashFromString(
str,
Expand Down
Loading
Loading