|
| 1 | +import getProvider from "../../helpers/getProvider"; |
| 2 | +import assert from "assert"; |
| 3 | +import { Logger } from "@ganache/ethereum-options/typings/src/logging-options"; |
| 4 | + |
| 5 | +describe("api", () => { |
| 6 | + describe("eth", () => { |
| 7 | + describe("instamine modes (eager/strict)", () => { |
| 8 | + describe("strict", () => { |
| 9 | + it("when in strict instamine mode, does not mine before returning the tx hash", async () => { |
| 10 | + const provider = await getProvider({ |
| 11 | + miner: { instamine: "strict" } |
| 12 | + }); |
| 13 | + const accounts = await provider.send("eth_accounts"); |
| 14 | + |
| 15 | + const hash = await provider.send("eth_sendTransaction", [ |
| 16 | + { |
| 17 | + from: accounts[0], |
| 18 | + to: accounts[1], |
| 19 | + value: "0x1" |
| 20 | + } |
| 21 | + ]); |
| 22 | + const receipt = await provider.send("eth_getTransactionReceipt", [ |
| 23 | + hash |
| 24 | + ]); |
| 25 | + assert.strictEqual(receipt, null); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + describe("eager", () => { |
| 30 | + it("mines before returning the tx hash", async () => { |
| 31 | + const provider = await getProvider({ |
| 32 | + miner: { instamine: "eager" } |
| 33 | + }); |
| 34 | + const accounts = await provider.send("eth_accounts"); |
| 35 | + |
| 36 | + const hash = await provider.send("eth_sendTransaction", [ |
| 37 | + { |
| 38 | + from: accounts[0], |
| 39 | + to: accounts[1], |
| 40 | + value: "0x1" |
| 41 | + } |
| 42 | + ]); |
| 43 | + const receipt = await provider.send("eth_getTransactionReceipt", [ |
| 44 | + hash |
| 45 | + ]); |
| 46 | + assert.notStrictEqual(receipt, null); |
| 47 | + }); |
| 48 | + |
| 49 | + it("log a message about future-nonce transactions in eager mode", async () => { |
| 50 | + let logger: Logger; |
| 51 | + const logPromise = new Promise<boolean>(resolve => { |
| 52 | + logger = { |
| 53 | + log: (msg: string) => { |
| 54 | + const regex = |
| 55 | + /Transaction "0x[a-zA-z0-9]{64}" has a too-high nonce; this transaction has been added to the pool, and will be processed when its nonce is reached\. See https:\/\/github.com\/trufflesuite\/ganache\/issues\/4165 for more information\./; |
| 56 | + if (regex.test(msg)) resolve(true); |
| 57 | + } |
| 58 | + }; |
| 59 | + }); |
| 60 | + |
| 61 | + const provider = await getProvider({ |
| 62 | + logging: { logger }, |
| 63 | + miner: { instamine: "eager" }, |
| 64 | + chain: { vmErrorsOnRPCResponse: true } |
| 65 | + }); |
| 66 | + const [from, to] = await provider.send("eth_accounts"); |
| 67 | + const futureNonceTx = { from, to, nonce: "0x1" }; |
| 68 | + const futureNonceProm = provider.send("eth_sendTransaction", [ |
| 69 | + futureNonceTx |
| 70 | + ]); |
| 71 | + |
| 72 | + // send a transaction to fill the nonce gap |
| 73 | + provider.send("eth_sendTransaction", [{ from, to }]); // we don't await this on purpose. |
| 74 | + |
| 75 | + const result = await Promise.race([futureNonceProm, logPromise]); |
| 76 | + // `logPromise` should resolve before the the hash gets returned |
| 77 | + // (logPromise returns true) |
| 78 | + assert.strictEqual(result, true); |
| 79 | + |
| 80 | + // now our nonce gap is filled so the original tx is mined |
| 81 | + const receipt = await provider.send("eth_getTransactionReceipt", [ |
| 82 | + await futureNonceProm |
| 83 | + ]); |
| 84 | + assert.notStrictEqual(receipt, null); |
| 85 | + }); |
| 86 | + |
| 87 | + it("handles transaction balance errors, callback style", done => { |
| 88 | + getProvider({ |
| 89 | + miner: { instamine: "eager" }, |
| 90 | + chain: { vmErrorsOnRPCResponse: true } |
| 91 | + }).then(async provider => { |
| 92 | + const [from, to] = await provider.send("eth_accounts"); |
| 93 | + const balance = parseInt( |
| 94 | + await provider.send("eth_getBalance", [from]), |
| 95 | + 16 |
| 96 | + ); |
| 97 | + const gasCost = 99967968750001; |
| 98 | + // send a transaction that will spend some of the balance |
| 99 | + provider.request({ |
| 100 | + method: "eth_sendTransaction", |
| 101 | + params: [ |
| 102 | + { |
| 103 | + from, |
| 104 | + to |
| 105 | + } |
| 106 | + ] |
| 107 | + }); |
| 108 | + |
| 109 | + // send another transaction while the previous transaction is still |
| 110 | + // pending. this transaction appears to have enough balance to run, |
| 111 | + // so the transaction pool will accept it, but when it runs in the VM |
| 112 | + // it won't have enough balance to run. |
| 113 | + provider.send( |
| 114 | + { |
| 115 | + jsonrpc: "2.0", |
| 116 | + id: "1", |
| 117 | + method: "eth_sendTransaction", |
| 118 | + params: [ |
| 119 | + { |
| 120 | + from, |
| 121 | + to, |
| 122 | + value: `0x${(balance - gasCost).toString(16)}` |
| 123 | + } |
| 124 | + ] |
| 125 | + }, |
| 126 | + (e, r) => { |
| 127 | + assert( |
| 128 | + e.message.includes( |
| 129 | + "sender doesn't have enough funds to send tx" |
| 130 | + ) |
| 131 | + ); |
| 132 | + assert.strictEqual(e.message, (r as any).error.message); |
| 133 | + assert.strictEqual((r as any).error.code, -32000); |
| 134 | + assert.strictEqual( |
| 135 | + typeof (r as any).error.data.result, |
| 136 | + "string" |
| 137 | + ); |
| 138 | + done(); |
| 139 | + } |
| 140 | + ); |
| 141 | + }); |
| 142 | + }); |
| 143 | + }); |
| 144 | + }); |
| 145 | + }); |
| 146 | +}); |
0 commit comments