Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 18x 18x 18x 11x 11x 2x 2x 11x 7x 7x 11x 2x 2x 11x 11x 11x 17x 17x 2x 2x 2x 2x 11x 11x 11x 11x 11x 11x 11x 11x 7x 11x 4x 4x 11x 11x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 1x 10x 10x 3x 3x 3x 3x 1x 1x 1x 1x 2x 2x 3x 1x 1x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 10x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 1x 8x 8x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import assert from "assert"; import { id, isError, makeError, toUtf8Bytes, toUtf8String, FetchRequest, JsonRpcProvider, Transaction, Wallet } from "../index.js"; const StatusMessages: Record<number, string> = { 200: "OK", 400: "BAD REQUEST", 500: "SERVER ERROR", }; type ProcessRequest = (method: string, params: Array<string>, blockNumber: number) => any; const wallet = new Wallet(id("test")); function createProvider(testFunc: ProcessRequest): JsonRpcProvider { let blockNumber = 1; const ticker = setInterval(() => { blockNumber++ }, 100); if (ticker.unref) { ticker.unref(); } const processReq = (req: { method: string, params: Array<string>, id: any }) => { let result = testFunc(req.method, req.params, blockNumber); if (result === undefined) { switch (req.method) { case "eth_blockNumber": result = blockNumber; break; case "eth_chainId": result = "0x1337"; break; case "eth_accounts": result = [ wallet.address ]; break; default: console.log("****", req); return { id, error: "unsupported", jsonrpc: "2.0" }; } } return { id: req.id, result, jsonrpc: "2.0" }; }; const req = new FetchRequest("http:/\/localhost:8082/"); req.getUrlFunc = async (_req, signal) => { const req = JSON.parse(_req.hasBody() ? toUtf8String(_req.body): ""); let statusCode = 200; const headers = { }; let resp: any; try { if (Array.isArray(req)) { resp = req.map((r) => processReq(r)); } else { resp = processReq(req); } } catch(error: any) { statusCode = 500; resp = error.message; } const body = toUtf8Bytes(JSON.stringify(resp)); return { statusCode, statusMessage: StatusMessages[statusCode], headers, body }; }; return new JsonRpcProvider(req, undefined, { cacheTimeout: -1 }); } describe("Ensure Catchable Errors", function() { it("Can catch bad broadcast replies", async function() { this.timeout(15000); const txInfo = { chainId: 1337, gasLimit: 100000, maxFeePerGas: 2000000000, maxPriorityFeePerGas: 1000000000, to: wallet.address, value: 1, }; const txSign = await wallet.signTransaction(txInfo); const txObj = Transaction.from(txSign); let count = 0; const provider = createProvider((method, params, blockNumber) => { switch (method) { case "eth_sendTransaction": return txObj.hash; case "eth_getTransactionByHash": { count++; // First time; fail! if (count === 1) { throw makeError("Faux Error", "SERVER_ERROR", { request: <any>({ }) }); } // Second time; return null if (count === 2) { return null; } // Return a valid tx... const result = Object.assign({ }, txObj.toJSON(), txObj.signature!.toJSON(), { hash: txObj.hash, from: wallet.address }); // ...eventually mined if (count > 4) { result.blockNumber = blockNumber; result.blockHash = id("test"); } return result; } } return undefined; }); const signer = await provider.getSigner(); const tx = await signer.sendTransaction(txInfo); assert.ok(tx); }); it("Missing v is recovered", async function() { this.timeout(15000); const txInfo = { chainId: 1337, gasLimit: 100000, maxFeePerGas: 2000000000, maxPriorityFeePerGas: 1000000000, to: wallet.address, value: 1, }; const txSign = await wallet.signTransaction(txInfo); const txObj = Transaction.from(txSign); let count = 0; // A provider which is mocked to return a "missing v" // in getTransaction const provider = createProvider((method, params, blockNumber) => { switch (method) { case "eth_sendTransaction": return txObj.hash; case "eth_getTransactionByHash": { count++; // The fully valid tx response const result = Object.assign({ }, txObj.toJSON(), txObj.signature!.toJSON(), { hash: txObj.hash, from: wallet.address, sig: null }); // First time; fail with a missing v! if (count < 2) { delete result.v; } // Debug result._count = count; return result; } } return undefined; }); // Track any "missing v" error let missingV: Error | null = null; provider.on("error", (e) => { if (isError(e, "UNKNOWN_ERROR") && isError(e.error, "INVALID_ARGUMENT")) { if (e.error.argument === "signature" && e.error.shortMessage === "missing v") { missingV = e.error; } } }); const signer = await provider.getSigner(); const tx = await signer.sendTransaction(txInfo); assert.ok(!!tx, "we got a transaction"); assert.ok(!!missingV, "missing v error present"); }); }); |