From 73f9492c41b87635f13761d810875a15c423131b Mon Sep 17 00:00:00 2001 From: Alessandro Rezzi Date: Mon, 25 Nov 2024 14:13:32 +0100 Subject: [PATCH] test: Increase the legacy mainnet wallet balance --- scripts/network/__mocks__/network_manager.js | 5 +++- tests/unit/wallet/transactions.spec.js | 30 +++++++++++--------- tests/utils/test_utils.js | 6 +++- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/scripts/network/__mocks__/network_manager.js b/scripts/network/__mocks__/network_manager.js index f5c7f6d9..293d5f43 100644 --- a/scripts/network/__mocks__/network_manager.js +++ b/scripts/network/__mocks__/network_manager.js @@ -45,7 +45,10 @@ class TestNetwork { // tx_1 provides a spendable balance of 0.1 * 10^8 satoshi const tx_1 = '010000000138f9c8ac1b4c6ad54e487206daad1fd12bae510dd70fbd2dc928f617ef6b4a47010000006a47304402200d01891ac5dc6d25452cadbe7ba7edd98143631cc2922da45dde94919593b222022066ef14c01c165a1530e2acf74bcd8648d7151b555fa0bfd0222c33e983091678012102033a004cb71693e809c45e1e491bc797654fa0c012be9dd46401ce8368beb705ffffffff02d02c5d05000000001976a91463fa54dad2e215ec21c54ff45a195d49b570b97988ac80969800000000001976a914f49b25384b79685227be5418f779b98a6be4c73888ac00000000'; - return [Transaction.fromHex(tx_1)]; + // tx_2 provides a spendable balance of 10000 PIVs + const tx_2 = + '010000000198bb641ffb74cf14e4f3e3c329fa7f97d605ca1b89d2c61a1e6d728da49342e2020000006a4730440220437d04f65dbc1e23cab38c1a6d2d2b4905ccfdea9d6e33429af8d58d3a689f41022039830fcae7545d8b5964b7c078b3d158e72e01642d274f9c3b38639d3ed292b9012103109bab9e66c51cb5da0dcc200fa7a336b8a3890489026ba7bbdf8ad379cf0251ffffffff020010a5d4e80000001976a914f49b25384b79685227be5418f779b98a6be4c73888ac228ff73e5d0100001976a914f8e77779f1787490e1459f4bde3afde5c057f6f888ac00000000'; + return [Transaction.fromHex(tx_2), Transaction.fromHex(tx_1)]; } else if ( addr === 'xpub6DVaPT3irDwUth7Y6Ff137FgA9jjvsmA2CHD7g2vjvvuMiNSUJRs9F8jSoPpXpc1s7ohR93dNAuzR5T2oPZFDTn7G2mHKYtpwtk7krNZmnV' diff --git a/tests/unit/wallet/transactions.spec.js b/tests/unit/wallet/transactions.spec.js index 338926cf..2b2d0673 100644 --- a/tests/unit/wallet/transactions.spec.js +++ b/tests/unit/wallet/transactions.spec.js @@ -1,6 +1,9 @@ import { Wallet } from '../../../scripts/wallet.js'; import { Mempool } from '../../../scripts/mempool.js'; -import { setUpLegacyMainnetWallet } from '../../utils/test_utils'; +import { + legacyMainnetInitialBalance, + setUpLegacyMainnetWallet, +} from '../../utils/test_utils'; import { describe, it, vi, afterAll, expect } from 'vitest'; import { COutpoint, @@ -34,6 +37,7 @@ async function checkFees(wallet, tx, feesPerBytes) { const nBytes = (await wallet.sign(tx)).serialize().length / 2; expect(fees).toBeGreaterThanOrEqual(feesPerBytes * nBytes); expect(fees).toBeLessThanOrEqual((feesPerBytes + 1) * nBytes); + return fees; } describe('Wallet transaction tests', () => { let wallet; @@ -198,11 +202,11 @@ describe('Wallet transaction tests', () => { it('creates a tx with max balance', async () => { const tx = wallet.createTransaction( 'SR3L4TFUKKGNsnv2Q4hWTuET2a4vHpm1b9', - 0.1 * 10 ** 8, + legacyMainnetInitialBalance(), { isDelegation: true } ); expect(tx.version).toBe(1); - expect(tx.vin).toHaveLength(1); + expect(tx.vin).toHaveLength(2); expect(tx.vin[0]).toStrictEqual( new CTxIn({ outpoint: new COutpoint({ @@ -213,13 +217,13 @@ describe('Wallet transaction tests', () => { }) ); expect(tx.vout).toHaveLength(1); + const fees = await checkFees(wallet, tx, MIN_FEE_PER_BYTE); expect(tx.vout[0]).toStrictEqual( new CTxOut({ script: '76a97b63d114291a25b5b4d1802e0611e9bf724a1e57d9210e826714f49b25384b79685227be5418f779b98a6be4c7386888ac', - value: 9997810, // 0.1 PIV - fee + value: legacyMainnetInitialBalance() - fees, }) ); - await checkFees(wallet, tx, MIN_FEE_PER_BYTE); }); it('creates a t->s tx correctly', () => { @@ -251,16 +255,15 @@ describe('Wallet transaction tests', () => { it('it does not insert dust change', async () => { // The tipical output has 34 bytes, so a 200 satoshi change is surely going to be dust - // a P2PKH with 1 input and 1 output will have more or less 190 bytes in size and 1900 satoshi of fees - // Finally 0.1*10**8 is the value of the UTXO we are spending (0.1 PIVs) - const value = 0.1 * 10 ** 8 - 1900 - 200; + // a P2PKH with 2 inputs and 1 output will have more or less 346 bytes in size and 3460 satoshi of fees + const value = legacyMainnetInitialBalance() - 3460 - 200; const tx = wallet.createTransaction( 'DLabsktzGMnsK5K9uRTMCF6NoYNY6ET4Bb', value, { subtractFeeFromAmt: false } ); expect(tx.version).toBe(1); - expect(tx.vin).toHaveLength(1); + expect(tx.vin).toHaveLength(2); expect(tx.vin[0]).toStrictEqual( new CTxIn({ outpoint: new COutpoint({ @@ -319,23 +322,24 @@ describe('Wallet transaction tests', () => { }); it('throws when balance is insufficient', () => { + const value = legacyMainnetInitialBalance() + 1; expect(() => wallet.createTransaction( 'SR3L4TFUKKGNsnv2Q4hWTuET2a4vHpm1b9', - 20 * 10 ** 8, + value, { isDelegation: true } ) ).toThrow(/not enough balance/i); expect(() => wallet.createTransaction( 'DLabsktzGMnsK5K9uRTMCF6NoYNY6ET4Bb', - 20 * 10 ** 8 + value ) ).toThrow(/not enough balance/i); expect(() => wallet.createTransaction( 'DLabsktzGMnsK5K9uRTMCF6NoYNY6ET4Bb', - 50 * 10 ** 8, + value, { useShieldInputs: true } ) ).toThrow(/not enough balance/i); @@ -352,7 +356,7 @@ describe('Wallet transaction tests', () => { expect(() => wallet.createTransaction( 'DLabsktzGMnsK5K9uRTMCF6NoYNY6ET4Bb', - 0.1 * 10 ** 8, + legacyMainnetInitialBalance(), { subtractFeeFromAmt: false } ) ).toThrow(/not enough balance/i); diff --git a/tests/utils/test_utils.js b/tests/utils/test_utils.js index 7667fb53..f3be196c 100644 --- a/tests/utils/test_utils.js +++ b/tests/utils/test_utils.js @@ -50,6 +50,10 @@ async function setUpWallet(masterKey, includeShield) { expect(wallet.isSyncing).toBeFalsy(); return wallet; } + +export function legacyMainnetInitialBalance() { + return 10 ** 7 + 10 ** 12; +} /** * Creates a mainnet wallet with a legacy master key and a spendable UTXO and a dummy PIVXShield * @returns {Promise} @@ -59,7 +63,7 @@ export async function setUpLegacyMainnetWallet() { const wallet = await setUpWallet(getLegacyMainnet(), true); // sanity check on the balance - expect(wallet.balance).toBe(0.1 * 10 ** 8); + expect(wallet.balance).toBe(legacyMainnetInitialBalance()); expect(wallet.coldBalance).toBe(0); expect(wallet.immatureBalance).toBe(0);