|
1 | 1 | package tbtc
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
| 5 | + "math/big" |
4 | 6 | "testing"
|
| 7 | + "time" |
5 | 8 |
|
6 | 9 | "github.com/keep-network/keep-core/internal/testutils"
|
7 | 10 | "github.com/keep-network/keep-core/pkg/bitcoin"
|
8 | 11 | "github.com/keep-network/keep-core/pkg/tbtc/internal/test"
|
| 12 | + "github.com/keep-network/keep-core/pkg/tecdsa" |
9 | 13 | )
|
10 | 14 |
|
| 15 | +func TestMovingFundsAction_Execute(t *testing.T) { |
| 16 | + scenarios, err := test.LoadMovingFundsTestScenarios() |
| 17 | + if err != nil { |
| 18 | + t.Fatal(err) |
| 19 | + } |
| 20 | + |
| 21 | + for _, scenario := range scenarios { |
| 22 | + t.Run(scenario.Title, func(t *testing.T) { |
| 23 | + hostChain := Connect() |
| 24 | + bitcoinChain := newLocalBitcoinChain() |
| 25 | + |
| 26 | + wallet := wallet{ |
| 27 | + // Set only relevant fields. |
| 28 | + publicKey: scenario.WalletPublicKey, |
| 29 | + } |
| 30 | + walletPublicKeyHash := bitcoin.PublicKeyHash(wallet.publicKey) |
| 31 | + |
| 32 | + // Record the transaction that will serve as moving funds transaction's |
| 33 | + // input in the Bitcoin local chain. |
| 34 | + err := bitcoinChain.BroadcastTransaction(scenario.InputTransaction) |
| 35 | + if err != nil { |
| 36 | + t.Fatal(err) |
| 37 | + } |
| 38 | + |
| 39 | + // Build the moving funds proposal based on the scenario data. |
| 40 | + proposal := &MovingFundsProposal{ |
| 41 | + TargetWallets: scenario.TargetWallets, |
| 42 | + MovingFundsTxFee: big.NewInt(scenario.Fee), |
| 43 | + } |
| 44 | + |
| 45 | + // Choose an arbitrary start block and expiration time. |
| 46 | + proposalProcessingStartBlock := uint64(100) |
| 47 | + proposalExpiryBlock := proposalProcessingStartBlock + |
| 48 | + movingFundsProposalValidityBlocks |
| 49 | + |
| 50 | + // Simulate the on-chain proposal validation passes with success. |
| 51 | + err = hostChain.setMovingFundsProposalValidationResult( |
| 52 | + walletPublicKeyHash, |
| 53 | + scenario.WalletMainUtxo, |
| 54 | + proposal, |
| 55 | + true, |
| 56 | + ) |
| 57 | + if err != nil { |
| 58 | + t.Fatal(err) |
| 59 | + } |
| 60 | + |
| 61 | + // Record the wallet main UTXO hash and moving funds commitment |
| 62 | + // hash in the local host chain so the moving funds action can detect it. |
| 63 | + walletMainUtxoHash := hostChain.ComputeMainUtxoHash( |
| 64 | + scenario.WalletMainUtxo, |
| 65 | + ) |
| 66 | + movingFundsCommitmentHash := hostChain.ComputeMovingFundsCommitmentHash( |
| 67 | + scenario.TargetWallets, |
| 68 | + ) |
| 69 | + hostChain.setWallet(walletPublicKeyHash, &WalletChainData{ |
| 70 | + MainUtxoHash: walletMainUtxoHash, |
| 71 | + MovingFundsTargetWalletsCommitmentHash: movingFundsCommitmentHash, |
| 72 | + }) |
| 73 | + |
| 74 | + // Create a signing executor mock instance. |
| 75 | + signingExecutor := newMockWalletSigningExecutor() |
| 76 | + |
| 77 | + // The signature within the scenario fixture is in the format |
| 78 | + // suitable for applying them directly to a Bitcoin transaction. |
| 79 | + // However, the signing executor operates on raw tECDSA signatures |
| 80 | + // so, we need to unpack it first. |
| 81 | + rawSignature := &tecdsa.Signature{ |
| 82 | + R: scenario.Signature.R, |
| 83 | + S: scenario.Signature.S, |
| 84 | + } |
| 85 | + |
| 86 | + // Set up the signing executor mock to return the signature from |
| 87 | + // the test fixture when called with the expected parameters. |
| 88 | + // Note that the start block is set based on the proposal |
| 89 | + // processing start block as done within the action. |
| 90 | + signingExecutor.setSignatures( |
| 91 | + []*big.Int{scenario.ExpectedSigHash}, |
| 92 | + proposalProcessingStartBlock, |
| 93 | + []*tecdsa.Signature{rawSignature}, |
| 94 | + ) |
| 95 | + |
| 96 | + action := newMovingFundsAction( |
| 97 | + logger.With(), |
| 98 | + hostChain, |
| 99 | + bitcoinChain, |
| 100 | + wallet, |
| 101 | + signingExecutor, |
| 102 | + proposal, |
| 103 | + proposalProcessingStartBlock, |
| 104 | + proposalExpiryBlock, |
| 105 | + func(ctx context.Context, blockHeight uint64) error { |
| 106 | + return nil |
| 107 | + }, |
| 108 | + ) |
| 109 | + |
| 110 | + // Modify the default parameters of the action to make |
| 111 | + // it possible to execute in the current test environment. |
| 112 | + action.broadcastCheckDelay = 1 * time.Second |
| 113 | + |
| 114 | + err = action.execute() |
| 115 | + if err != nil { |
| 116 | + t.Fatal(err) |
| 117 | + } |
| 118 | + |
| 119 | + // Action execution that completes without an error is a sign of |
| 120 | + // success. However, just in case, make an additional check that |
| 121 | + // the expected moving funds transaction was actually broadcasted |
| 122 | + // on the local Bitcoin chain. |
| 123 | + broadcastedMovingFundsTransaction, err := bitcoinChain.GetTransaction( |
| 124 | + scenario.ExpectedMovingFundsTransactionHash, |
| 125 | + ) |
| 126 | + if err != nil { |
| 127 | + t.Fatal(err) |
| 128 | + } |
| 129 | + |
| 130 | + testutils.AssertBytesEqual( |
| 131 | + t, |
| 132 | + scenario.ExpectedMovingFundsTransaction.Serialize(), |
| 133 | + broadcastedMovingFundsTransaction.Serialize(), |
| 134 | + ) |
| 135 | + }) |
| 136 | + } |
| 137 | +} |
| 138 | + |
11 | 139 | func TestAssembleMovingFundsTransaction(t *testing.T) {
|
12 | 140 | scenarios, err := test.LoadMovingFundsTestScenarios()
|
13 | 141 | if err != nil {
|
|
0 commit comments