From ca33facd42ddcccf31dd4938b1419cdb1ecd44ff Mon Sep 17 00:00:00 2001 From: tomtomcrypto Date: Wed, 22 May 2024 19:42:33 +0000 Subject: [PATCH 1/6] fixed bad parsing of eosio errors --- src/routes/evm/index.ts | 44 +++++++++++++++++++++++----------------- src/telosevm-js/telos.ts | 21 +++++++++++++------ 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/routes/evm/index.ts b/src/routes/evm/index.ts index 4315927..9de1a9f 100644 --- a/src/routes/evm/index.ts +++ b/src/routes/evm/index.ts @@ -410,13 +410,13 @@ export default async function (fastify: FastifyInstance, opts: TelosEvmConfig) { trxs.push({ blockHash: blockHash, blockNumber: hexBlockNum, - from: finalFrom, + from: finalFrom.toLowerCase(), gas: hexGas, gasPrice: hexGasPrice, hash: receipt['hash'], input: receipt['input_data'], nonce: hexNonce, - to: toChecksumAddress(receipt['to']), + to: receipt['to']?.toLowerCase(), transactionIndex: hexTransactionIndex, value: hexValue, v, r, s @@ -465,7 +465,7 @@ export default async function (fastify: FastifyInstance, opts: TelosEvmConfig) { return results?.hits?.hits; } - async function getCurrentBlockNumber(indexed: boolean = false) { + async function getCurrentBlockNumber(indexed: boolean = false, retry: number = 0) { if (!indexed) { const key = `${CACHE_PREFIX}_last_onchain_block`; const cachedData = await fastify.redis.get(key); @@ -504,14 +504,20 @@ export default async function (fastify: FastifyInstance, opts: TelosEvmConfig) { const docsCount = parseInt(index['docs.count']); const adjustedNum = indexToSuffixNum(index.index); lastBlockNum = (adjustedNum * 1e7) + docsCount - opts.blockNumberDelta - 1; - } else { + } else if (index?.index) { const results = await fastify.elastic.search({ index: `${index.index}`, size: 1, sort: [{"@global.block_num": {order: "desc"}}] }); lastBlockNum = results?.hits?.hits[0]?._source["@global"].block_num - opts.blockNumberDelta - 1; - + } else if (retry < 3) { + await new Promise(resolve => { + setTimeout(resolve, 1000); + }); + return getCurrentBlockNumber(indexed, retry+1); + } else { + throw new Error('Could not find last indexed block. Check connection to ElasticSearch.'); } let currentBlockNumber = addHexPrefix((Number(lastBlockNum)).toString(16)); @@ -1053,7 +1059,6 @@ export default async function (fastify: FastifyInstance, opts: TelosEvmConfig) { let receiptLog = consoleOutput.slice(consoleOutput.indexOf(RECEIPT_LOG_START) + RECEIPT_LOG_START.length, consoleOutput.indexOf(RECEIPT_LOG_END)); let receipt = JSON.parse(receiptLog); - // This error handling looks wrong, we should not be throwing errors like this directly for everything... if (receipt.status === 0) { let err = new TransactionError('Transaction error'); let output = addHexPrefix(receipt.output); @@ -1078,16 +1083,15 @@ export default async function (fastify: FastifyInstance, opts: TelosEvmConfig) { return addHexPrefix(rawResponse.eth.transactionHash); } catch (e) { if (opts.orderNonces) { - // The previous version with eosjs was: - // const assertionMessage = e?.details[0]?.message const assertionMessage = e?.response?.json?.error?.details[0]?.message if (assertionMessage && assertionMessage.includes('incorrect nonce')) { return nonceRetryManager.submitFailedRawTrx(signedTx); } } - if (e instanceof TransactionError) + if (e instanceof TransactionError){ throw e; + } throw e; } @@ -1132,13 +1136,13 @@ export default async function (fastify: FastifyInstance, opts: TelosEvmConfig) { return { blockHash: _blockHash, blockNumber: removeLeftZeros(numToHex(receipt['block'])), - contractAddress: toChecksumAddress(_contractAddr), + contractAddress: _contractAddr?.toLowerCase(), cumulativeGasUsed: removeLeftZeros(_gas), - from: toChecksumAddress(receipt['from']), + from: toChecksumAddress(receipt['from'])?.toLowerCase(), gasUsed: removeLeftZeros(_gas), logsBloom: _logsBloom, status: removeLeftZeros(numToHex(receipt['status'])), - to: toChecksumAddress(receipt['to']), + to: toChecksumAddress(receipt['to'])?.toLowerCase(), transactionHash: receipt['hash'], transactionIndex: removeLeftZeros(numToHex(receipt['trx_index'])), logs: buildLogsObject( @@ -1176,13 +1180,13 @@ export default async function (fastify: FastifyInstance, opts: TelosEvmConfig) { return { blockHash: _blockHash, blockNumber: removeLeftZeros(_blockNum), - from: toChecksumAddress(receipt['from']), + from: toChecksumAddress(receipt['from']).toLowerCase(), gas: removeLeftZeros(numToHex(receipt.gas_limit)), gasPrice: removeLeftZeros(numToHex(receipt.charged_gas_price)), hash: receipt['hash'], input: receipt['input_data'], nonce: removeLeftZeros(numToHex(receipt['nonce'])), - to: toChecksumAddress(receipt['to']), + to: toChecksumAddress(receipt['to'])?.toLowerCase(), transactionIndex: removeLeftZeros(numToHex(receipt['trx_index'])), value: removeLeftZeros(receipt['value']), v: removeLeftZeros(v), @@ -1515,10 +1519,10 @@ export default async function (fastify: FastifyInstance, opts: TelosEvmConfig) { action: { callType: toOpname(itx.callType), //why is 0x not in the receipt table? - from: toChecksumAddress(itx.from), + from: itx.from, gas: addHexPrefix(itx.gas), input: addHexPrefix(itx.input), - to: toChecksumAddress(itx.to), + to: itx.to, value: addHexPrefix(itx.value) }, blockHash: addHexPrefix(doc['@raw']['block_hash']), @@ -1742,18 +1746,20 @@ export default async function (fastify: FastifyInstance, opts: TelosEvmConfig) { return { jsonrpc, id, error }; } + let type: string = 'RPCERROR'; let error: any = { code: 3 }; - if (e?.json?.error?.code === 3050003) { - let message = e?.json?.error?.details[0]?.message; + if (e?.response?.json?.error?.code === 3050003) { + let message = e?.response?.json?.error?.details[0]?.message; if (message.startsWith(EOSIO_ASSERTION_PREFIX)) message = message.substr(EOSIO_ASSERTION_PREFIX.length, message.length + 1); + type = 'RPCREVERT'; error.message = message; } else { error.message = e?.message; } - Logger.error(`RPCERROR: ${new Date().toISOString()} - ${ip} - ${JSON.stringify(error)} | Method: ${method} | REQ: ${JSON.stringify(params)}`); + Logger.error(`${type}: ${new Date().toISOString()} - ${ip} - ${JSON.stringify(error)} | Method: ${method} | REQ: ${JSON.stringify(params)}`); return { jsonrpc, id, error }; } } else { diff --git a/src/telosevm-js/telos.ts b/src/telosevm-js/telos.ts index 5b5f40d..8b7737a 100644 --- a/src/telosevm-js/telos.ts +++ b/src/telosevm-js/telos.ts @@ -87,13 +87,22 @@ export class TelosEvmApi { antelopeChainId: string retryTrxNumBlocks: number }) { - this.readAPI = new APIClient({ - provider: new FetchProvider(nodeosRead) - }) + try { + let provider = new FetchProvider(nodeosRead); + this.readAPI = new APIClient({ + provider: provider + }) + } catch (e) { + throw new Error(`Failed to create read API: ${e.message}`) + } this.signingPermission = signingPermission || 'active' - this.writeAPI = new APIClient({ - provider: new FetchProvider(nodeosWrite) - }) + try { + this.writeAPI = new APIClient({ + provider: new FetchProvider(nodeosWrite) + }) + } catch (e) { + throw new Error(`Failed to create write API: ${e.message}`) + } this.retryTrxNumBlocks = retryTrxNumBlocks this.chainId = Checksum256.from(antelopeChainId) this.signingKey = PrivateKey.from(telosPrivateKey) From b5cf88c4e421f57aa0c66f5d6bb231eeb293cb47 Mon Sep 17 00:00:00 2001 From: tomtomcrypto Date: Wed, 22 May 2024 19:47:51 +0000 Subject: [PATCH 2/6] keep checksum formatting but turn address lowercase as per best practices --- src/routes/evm/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/routes/evm/index.ts b/src/routes/evm/index.ts index 9de1a9f..b96ddc4 100644 --- a/src/routes/evm/index.ts +++ b/src/routes/evm/index.ts @@ -1519,10 +1519,10 @@ export default async function (fastify: FastifyInstance, opts: TelosEvmConfig) { action: { callType: toOpname(itx.callType), //why is 0x not in the receipt table? - from: itx.from, + from: toChecksumAddress(itx.from).toLowerCase(), gas: addHexPrefix(itx.gas), input: addHexPrefix(itx.input), - to: itx.to, + to: toChecksumAddress(itx.to)?.toLowerCase(), value: addHexPrefix(itx.value) }, blockHash: addHexPrefix(doc['@raw']['block_hash']), From 38ed13716e97634b36c37d2fc1da32c5a4cc44f8 Mon Sep 17 00:00:00 2001 From: tomtomcrypto Date: Wed, 22 May 2024 19:49:33 +0000 Subject: [PATCH 3/6] keep checksum formatting but turn address lowercase as per best practices --- src/routes/evm/index.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/routes/evm/index.ts b/src/routes/evm/index.ts index b96ddc4..255fd79 100644 --- a/src/routes/evm/index.ts +++ b/src/routes/evm/index.ts @@ -395,9 +395,7 @@ export default async function (fastify: FastifyInstance, opts: TelosEvmConfig) { if (receipt['logsBloom']){ bloom.or(new Bloom(Buffer.from(receipt['logsBloom'], "hex"))); } - let finalFrom = receipt['from']; - if (receipt['from'] == zeros) - finalFrom = toChecksumAddress(receipt['from']); + let finalFrom = toChecksumAddress(receipt['from']); if (!full) { trxs.push(receipt['hash']); } else { @@ -416,7 +414,7 @@ export default async function (fastify: FastifyInstance, opts: TelosEvmConfig) { hash: receipt['hash'], input: receipt['input_data'], nonce: hexNonce, - to: receipt['to']?.toLowerCase(), + to: toChecksumAddress(receipt['to'])?.toLowerCase(), transactionIndex: hexTransactionIndex, value: hexValue, v, r, s From 4e1b4f632e4f207236581a457dba9c8118cc7229 Mon Sep 17 00:00:00 2001 From: tomtomcrypto Date: Wed, 22 May 2024 19:50:58 +0000 Subject: [PATCH 4/6] removed redundant throw --- src/routes/evm/index.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/routes/evm/index.ts b/src/routes/evm/index.ts index 255fd79..12f13e5 100644 --- a/src/routes/evm/index.ts +++ b/src/routes/evm/index.ts @@ -1086,11 +1086,6 @@ export default async function (fastify: FastifyInstance, opts: TelosEvmConfig) { return nonceRetryManager.submitFailedRawTrx(signedTx); } } - - if (e instanceof TransactionError){ - throw e; - } - throw e; } }); From a51e06bcaa327449d37b57ffa8b0e069e99fdd19 Mon Sep 17 00:00:00 2001 From: tomtomcrypto Date: Wed, 22 May 2024 19:52:02 +0000 Subject: [PATCH 5/6] keep checksum formatting but turn address lowercase as per best practices --- src/routes/evm/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/evm/index.ts b/src/routes/evm/index.ts index 12f13e5..3776236 100644 --- a/src/routes/evm/index.ts +++ b/src/routes/evm/index.ts @@ -1129,7 +1129,7 @@ export default async function (fastify: FastifyInstance, opts: TelosEvmConfig) { return { blockHash: _blockHash, blockNumber: removeLeftZeros(numToHex(receipt['block'])), - contractAddress: _contractAddr?.toLowerCase(), + contractAddress: toChecksumAddress(_contractAddr)?.toLowerCase(), cumulativeGasUsed: removeLeftZeros(_gas), from: toChecksumAddress(receipt['from'])?.toLowerCase(), gasUsed: removeLeftZeros(_gas), From 37d97c26a359db6efc1c6f4f3f77b7527646ec58 Mon Sep 17 00:00:00 2001 From: tomtomcrypto Date: Thu, 23 May 2024 08:31:22 +0000 Subject: [PATCH 6/6] added retry & error logging for block delta --- src/routes/evm/index.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/routes/evm/index.ts b/src/routes/evm/index.ts index 3776236..94f1862 100644 --- a/src/routes/evm/index.ts +++ b/src/routes/evm/index.ts @@ -301,7 +301,7 @@ export default async function (fastify: FastifyInstance, opts: TelosEvmConfig) { return String(adjustedNum).padStart(8, '0'); } - async function getDeltaDocFromNumber(blockNumber: number) { + async function getDeltaDocFromNumber(blockNumber: number, retry: number = 0) { const indexSuffix = indexSuffixForBlock(blockNumber); const results = await fastify.elastic.search({ index: `${opts.elasticIndexPrefix}-delta-${opts.elasticIndexVersion}-${indexSuffix}`, @@ -313,6 +313,15 @@ export default async function (fastify: FastifyInstance, opts: TelosEvmConfig) { } }); const blockDelta = results?.hits?.hits[0]?._source; + if(!blockDelta && retry < 3){ + retry++; + await new Promise(resolve => { + setTimeout(resolve, 500 * retry); + }); + return getDeltaDocFromNumber(blockNumber, retry); + } else if(!blockDelta){ + Logger.error(`Could not find delta doc for block number ${blockNumber}`); + } return blockDelta; }