fix: tx broadcast logging error #852

This commit is contained in:
Matthew Little
2021-11-16 16:00:28 +01:00
committed by GitHub
parent 0a485a3db3
commit b0c43d9395
2 changed files with 44 additions and 10 deletions

View File

@@ -148,16 +148,21 @@ export function createCoreNodeRpcProxyRouter(db: DataStore): express.Router {
* Logs a transaction broadcast event alongside the current block height.
*/
async function logTxBroadcast(response: string): Promise<void> {
const blockHeightQuery = await db.getCurrentBlockHeight();
if (!blockHeightQuery.found) {
return;
try {
const blockHeightQuery = await db.getCurrentBlockHeight();
if (!blockHeightQuery.found) {
return;
}
const blockHeight = blockHeightQuery.result;
// Strip wrapping double quotes (if any)
const txId = response.replace(/^"(.*)"$/, '$1');
logger.info('Transaction broadcasted', {
txid: `0x${txId}`,
first_broadcast_at_stacks_height: blockHeight,
});
} catch (error) {
logError(`Error logging tx broadcast: ${error}`, error);
}
const blockHeight = blockHeightQuery.result;
const txId = JSON.parse(response);
logger.info('Transaction broadcasted', {
txid: `0x${txId}`,
first_broadcast_at_stacks_height: blockHeight,
});
}
router.postAsync('/transactions', async (req, res, next) => {
@@ -223,7 +228,7 @@ export function createCoreNodeRpcProxyRouter(db: DataStore): express.Router {
if (proxyResp.status === 200) {
// Log the transaction id broadcast, but clone the `Response` first before parsing its body
// so we don't mess up the original response's `ReadableStream` pointers.
const parsedTxId: string = await proxyResp.clone().json();
const parsedTxId: string = await proxyResp.clone().text();
await logTxBroadcast(parsedTxId);
}
await pipelineAsync(proxyResp.body, res);

View File

@@ -8,6 +8,7 @@ import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import * as nock from 'nock';
import { DbBlock } from 'src/datastore/common';
describe('v2-proxy tests', () => {
let db: PgDataStore;
@@ -50,6 +51,34 @@ describe('v2-proxy tests', () => {
return [apiServer, apiServer.terminate] as const;
},
async (_, __, api) => {
const block1: DbBlock = {
block_hash: '0x11',
index_block_hash: '0xaa',
parent_index_block_hash: '0x00',
parent_block_hash: '0x00',
parent_microblock_hash: '',
block_height: 1,
burn_block_time: 1234,
burn_block_hash: '0x1234',
burn_block_height: 123,
miner_txid: '0x4321',
canonical: true,
parent_microblock_sequence: 0,
execution_cost_read_count: 0,
execution_cost_read_length: 0,
execution_cost_runtime: 0,
execution_cost_write_count: 0,
execution_cost_write_length: 0,
};
// Ensure db has a block so that current block height queries return a found result
await db.update({
block: block1,
microblocks: [],
minerRewards: [],
txs: [],
});
const primaryStubbedResponse =
'"1659fcdc9167576eb1f2a05d0aaba5ca1aa1943892e7e6e5d3ccb3e537f1c870"';
const extraStubbedResponse = 'extra success stubbed response';