|
| 1 | +import { Identities, Managers, Utils } from "@arkecosystem/crypto"; |
| 2 | +import delay from "delay"; |
| 3 | +import { TransactionFactory } from "../../helpers/transaction-factory"; |
| 4 | +import { secrets } from "../../utils/config/testnet/delegates.json"; |
| 5 | +import * as support from "./__support__"; |
| 6 | + |
| 7 | +beforeAll(async () => { |
| 8 | + await support.setUp(); |
| 9 | + Managers.configManager.setFromPreset("testnet"); |
| 10 | +}); |
| 11 | +afterAll(support.tearDown); |
| 12 | + |
| 13 | +describe("applyToRecipient - Multipayment scenario", () => { |
| 14 | + /* |
| 15 | + * Scenario : |
| 16 | + * - init bob and alice wallet |
| 17 | + * - send an initial tx from bob to index his wallet in tx pool |
| 18 | + * - send a multipayment from alice including bob in payment recipients |
| 19 | + * - send bob funds received from multipayment to a random address |
| 20 | + * - this last transaction from bob fails if pool wallet is not updated correctly by multipayment tx |
| 21 | + */ |
| 22 | + const bobPassphrase = "bob pass phrase1"; |
| 23 | + const bobAddress = Identities.Address.fromPassphrase(bobPassphrase, 23); |
| 24 | + const bobInitialFund = 50 * 1e8; // 50 ARK |
| 25 | + |
| 26 | + const alicePassphrase = "alice pass phrase1"; |
| 27 | + const aliceAddress = Identities.Address.fromPassphrase(alicePassphrase, 23); |
| 28 | + const aliceInitialFund = 2500 * 1e8; // 2500 ARK |
| 29 | + |
| 30 | + const randomAddress = Identities.Address.fromPassphrase("ran dom addr1", 23); |
| 31 | + |
| 32 | + it("should correctly update recipients pool wallet balance after a multipayment", async () => { |
| 33 | + const initialTxToBob = TransactionFactory.transfer(bobAddress, bobInitialFund) |
| 34 | + .withPassphrase(secrets[1]) |
| 35 | + .createOne(); |
| 36 | + const initialTxToAlice = TransactionFactory.transfer(aliceAddress, aliceInitialFund) |
| 37 | + .withPassphrase(secrets[2]) |
| 38 | + .createOne(); |
| 39 | + await expect(initialTxToBob).toBeAccepted(); |
| 40 | + await support.forge([initialTxToBob, initialTxToAlice]); |
| 41 | + await delay(1000); |
| 42 | + |
| 43 | + const initialTxFromBob = TransactionFactory.transfer(bobAddress, 1) |
| 44 | + .withPassphrase(bobPassphrase) |
| 45 | + .createOne(); |
| 46 | + await expect(initialTxFromBob).toBeAccepted(); |
| 47 | + await support.forge([initialTxFromBob]); |
| 48 | + await delay(1000); |
| 49 | + |
| 50 | + const multipaymentToBobAndAlice = TransactionFactory.multiPayment([ |
| 51 | + { |
| 52 | + recipientId: bobAddress, |
| 53 | + amount: (2000 * 1e8).toFixed(), // 2000 ARK |
| 54 | + }, |
| 55 | + { |
| 56 | + recipientId: aliceAddress, |
| 57 | + amount: (10 * 1e8).toFixed(), // 10 ARK |
| 58 | + }, |
| 59 | + ]) |
| 60 | + .withPassphrase(alicePassphrase) |
| 61 | + .createOne(); |
| 62 | + await support.forge([multipaymentToBobAndAlice]); |
| 63 | + await delay(1000); |
| 64 | + await expect(multipaymentToBobAndAlice.id).toBeForged(); |
| 65 | + |
| 66 | + const bobTransfer = TransactionFactory.transfer(randomAddress, 2000 * 1e8) |
| 67 | + .withPassphrase(bobPassphrase) |
| 68 | + .createOne(); |
| 69 | + await expect(bobTransfer).toBeAccepted(); |
| 70 | + await support.forge([bobTransfer]); |
| 71 | + await delay(1000); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe("applyToRecipient - transfer and multipayment classic scenarios", () => { |
| 76 | + it("should not accept a transfer in the pool with more amount than sender balance", async () => { |
| 77 | + // just send funds to a new wallet, and try to send more than the funds from this new wallet |
| 78 | + const bobPassphrase = "bob pass phrase2"; |
| 79 | + const bobAddress = Identities.Address.fromPassphrase(bobPassphrase, 23); |
| 80 | + const bobInitialFund = 100 * 1e8; // 100 ARK |
| 81 | + |
| 82 | + const randomAddress = Identities.Address.fromPassphrase(secrets[1], 23); |
| 83 | + const initialTxToBob = TransactionFactory.transfer(bobAddress, bobInitialFund) |
| 84 | + .withPassphrase(secrets[1]) |
| 85 | + .createOne(); |
| 86 | + |
| 87 | + await support.forge([initialTxToBob]); |
| 88 | + await delay(1000); |
| 89 | + |
| 90 | + // the fees for this are making the transfer worth more than bob balance |
| 91 | + const bobTransferMoreThanBalance = TransactionFactory.transfer(randomAddress, bobInitialFund) |
| 92 | + .withPassphrase(bobPassphrase) |
| 93 | + .createOne(); |
| 94 | + await expect(bobTransferMoreThanBalance).not.toBeAccepted(); |
| 95 | + |
| 96 | + // now a transaction with fees + amount === balance should pass |
| 97 | + const fee = 1e7; |
| 98 | + const bobTransferValid = TransactionFactory.transfer(randomAddress, bobInitialFund - fee) |
| 99 | + .withPassphrase(bobPassphrase) |
| 100 | + .withFee(fee) |
| 101 | + .createOne(); |
| 102 | + await expect(bobTransferValid).toBeAccepted(); |
| 103 | + await delay(1000); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should not accept a transfer in the pool with more amount than sender balance", async () => { |
| 107 | + // just send funds to a new wallet with multipayment, and try to send more than the funds from this new wallet |
| 108 | + const bobPassphrase = "bob pass phrase3"; |
| 109 | + const bobAddress = Identities.Address.fromPassphrase(bobPassphrase, 23); |
| 110 | + const bobInitialFund = 100 * 1e8; // 100 ARK |
| 111 | + |
| 112 | + const randomAddress = Identities.Address.fromPassphrase("a b c", 23); |
| 113 | + |
| 114 | + const initialTxToBob = TransactionFactory.multiPayment([ |
| 115 | + { |
| 116 | + recipientId: bobAddress, |
| 117 | + amount: bobInitialFund.toFixed(), |
| 118 | + }, |
| 119 | + { |
| 120 | + recipientId: randomAddress, |
| 121 | + amount: bobInitialFund.toFixed(), |
| 122 | + }, |
| 123 | + ]) |
| 124 | + .withPassphrase(secrets[1]) |
| 125 | + .createOne(); |
| 126 | + |
| 127 | + await support.forge([initialTxToBob]); |
| 128 | + await delay(1000); |
| 129 | + |
| 130 | + // the fees for this are making the transfer worth more than bob balance |
| 131 | + const bobTransferMoreThanBalance = TransactionFactory.transfer(randomAddress, bobInitialFund) |
| 132 | + .withPassphrase(bobPassphrase) |
| 133 | + .createOne(); |
| 134 | + await expect(bobTransferMoreThanBalance).not.toBeAccepted(); |
| 135 | + |
| 136 | + // now a transaction with fees + amount === balance should pass |
| 137 | + const fee = 1e7; |
| 138 | + const bobTransferValid = TransactionFactory.transfer(randomAddress, bobInitialFund - fee) |
| 139 | + .withPassphrase(bobPassphrase) |
| 140 | + .withFee(fee) |
| 141 | + .createOne(); |
| 142 | + await expect(bobTransferValid).toBeAccepted(); |
| 143 | + await delay(1000); |
| 144 | + }); |
| 145 | +}); |
| 146 | + |
| 147 | +describe("Pool transactions when AcceptBlockHandler fails", () => { |
| 148 | + // just send funds to a new wallet, and try to send more than the funds from this new wallet |
| 149 | + const bobPassphrase = "bob pass phrase4"; |
| 150 | + const bobAddress = Identities.Address.fromPassphrase(bobPassphrase, 23); |
| 151 | + const bobInitialFund = 100 * 1e8; // 100 ARK |
| 152 | + |
| 153 | + const randomAddress = Identities.Address.fromPassphrase(secrets[1], 23); |
| 154 | + |
| 155 | + it("should keep transactions in the pool after AcceptBlockHandler fails to accept a block", async () => { |
| 156 | + const initialTxToBob = TransactionFactory.transfer(bobAddress, bobInitialFund) |
| 157 | + .withPassphrase(secrets[1]) |
| 158 | + .createOne(); |
| 159 | + |
| 160 | + await support.forge([initialTxToBob]); |
| 161 | + await delay(1000); |
| 162 | + |
| 163 | + // a valid tx to accept in the pool |
| 164 | + const bobTransfer = TransactionFactory.transfer(randomAddress, 100) |
| 165 | + .withPassphrase(bobPassphrase) |
| 166 | + .createOne(); |
| 167 | + await expect(bobTransfer).toBeAccepted(); |
| 168 | + await expect(bobTransfer).toBeUnconfirmed(); |
| 169 | + |
| 170 | + // this one will make AcceptBlockHandler fail to accept the block |
| 171 | + const bobBusinessResignation = TransactionFactory.businessResignation() |
| 172 | + .withPassphrase(bobPassphrase) |
| 173 | + .withNonce(Utils.BigNumber.ZERO) |
| 174 | + .createOne(); |
| 175 | + await support.forge([bobBusinessResignation]); |
| 176 | + await delay(1000); |
| 177 | + |
| 178 | + await expect(bobTransfer).toBeUnconfirmed(); |
| 179 | + }); |
| 180 | +}); |
0 commit comments