From 617a886b2540cc9ea82124dff2d49d829dd0062a Mon Sep 17 00:00:00 2001 From: julian merlo Date: Mon, 25 Mar 2024 18:41:08 -0300 Subject: [PATCH] Resolved issue mapping tx hash --- .../actions/wormchain/HandleWormchainLogs.ts | 24 ++++--- .../wormchainLogMessagePublishedMapper.ts | 68 ++++++++++--------- .../wormchain/HandleWormchainLogs.test.ts | 32 +++++---- ...wormchainLogMessagePublishedMapper.test.ts | 21 +++--- 4 files changed, 78 insertions(+), 67 deletions(-) diff --git a/blockchain-watcher/src/domain/actions/wormchain/HandleWormchainLogs.ts b/blockchain-watcher/src/domain/actions/wormchain/HandleWormchainLogs.ts index a5cd0e17..a89c6837 100644 --- a/blockchain-watcher/src/domain/actions/wormchain/HandleWormchainLogs.ts +++ b/blockchain-watcher/src/domain/actions/wormchain/HandleWormchainLogs.ts @@ -5,26 +5,28 @@ import { WormchainLog } from "../../entities/wormchain"; export class HandleWormchainLogs { constructor( private readonly cfg: HandleWormchainLogsOptions, - private readonly mapper: (tx: WormchainLog) => TransactionFoundEvent, + private readonly mapper: (tx: WormchainLog) => TransactionFoundEvent[], private readonly target: (parsed: TransactionFoundEvent[]) => Promise, private readonly statsRepo: StatRepository ) {} public async handle(logs: WormchainLog[]): Promise { - const mappedItems = logs.map((log) => { - const logMap = this.mapper(log); - return logMap; - }); + const filterLogs: TransactionFoundEvent[] = []; - const filterItems = mappedItems.filter((item) => { - if (item) { - this.report(); - return item; + logs.map((log) => { + const logMapped = this.mapper(log); + + if (logMapped && logMapped.length > 0) { + logMapped.forEach((log) => { + this.report(); + filterLogs.push(log); + }); } + return logMapped; }); - await this.target(filterItems); - return filterItems; + await this.target(filterLogs); + return filterLogs; } private report() { diff --git a/blockchain-watcher/src/infrastructure/mappers/wormchain/wormchainLogMessagePublishedMapper.ts b/blockchain-watcher/src/infrastructure/mappers/wormchain/wormchainLogMessagePublishedMapper.ts index 0e0ea84a..f1d06f62 100644 --- a/blockchain-watcher/src/infrastructure/mappers/wormchain/wormchainLogMessagePublishedMapper.ts +++ b/blockchain-watcher/src/infrastructure/mappers/wormchain/wormchainLogMessagePublishedMapper.ts @@ -9,44 +9,52 @@ let logger: winston.Logger = winston.child({ module: "wormchainLogMessagePublish export const wormchainLogMessagePublishedMapper = ( log: WormchainLog -): LogFoundEvent | undefined => { - const { coreContract, sequence, payload, emitter, nonce, hash } = transactionAttibutes(log); +): LogFoundEvent | [] => { + const transactionAttributesMapped = transactionAttributes(log); - if (coreContract && sequence && payload && emitter && payload && nonce && hash) { + if (transactionAttributesMapped.length === 0) { + return []; + } + + const logMessages: any = []; + + transactionAttributesMapped.forEach((tx) => { logger.info( - `[wormchain] Source event info: [tx: ${hash}][emitterChain: ${CHAIN_ID_WORMCHAIN}][sender: ${emitter}][sequence: ${sequence}]` + `[wormchain] Source event info: [tx: ${tx.hash}][emitterChain: ${CHAIN_ID_WORMCHAIN}][sender: ${tx.emitter}][sequence: ${tx.sequence}]` ); - return { + logMessages.push({ name: "log-message-published", address: CORE_ADDRESS, chainId: 3104, - txHash: hash, + txHash: tx.hash, blockHeight: log.blockHeight, blockTime: log.timestamp, attributes: { - sender: emitter, - sequence: sequence, - payload: payload, - nonce: nonce, + sender: tx.emitter, + sequence: tx.sequence, + payload: tx.payload, + nonce: tx.nonce, consistencyLevel: 0, }, - }; - } + }); + }); + + return logMessages; }; -function transactionAttibutes(log: WormchainLog): TransactionAttributes { - let coreContract; - let sequence; - let payload; - let emitter; - let nonce; - let hash; +function transactionAttributes(log: WormchainLog): TransactionAttributes[] { + const transactionAttributes: TransactionAttributes[] = []; log.transactions?.forEach((tx) => { - hash = tx.hash; + let coreContract = false; + let sequence: number | undefined; + let payload: string | undefined; + let emitter: string | undefined; + let nonce: number | undefined; + let hash: string | undefined; - tx.attributes.forEach((attr) => { + for (const attr of tx.attributes) { const key = Buffer.from(attr.key, "base64").toString().toLowerCase(); const value = Buffer.from(attr.value, "base64").toString().toLowerCase(); @@ -65,22 +73,20 @@ function transactionAttibutes(log: WormchainLog): TransactionAttributes { break; case "_contract_address": case "contract_address": - if (value.toLocaleLowerCase() === CORE_ADDRESS.toLowerCase()) { + if (value.toLowerCase() === CORE_ADDRESS.toLowerCase()) { coreContract = true; } break; } - }); + } + + if (coreContract && sequence && payload && emitter && nonce) { + hash = tx.hash; + transactionAttributes.push({ coreContract, sequence, payload, emitter, nonce, hash }); + } }); - return { - coreContract, - sequence, - payload, - emitter, - nonce, - hash, - }; + return transactionAttributes; } type TransactionAttributes = { diff --git a/blockchain-watcher/test/domain/actions/wormchain/HandleWormchainLogs.test.ts b/blockchain-watcher/test/domain/actions/wormchain/HandleWormchainLogs.test.ts index be531f63..e6ff1106 100644 --- a/blockchain-watcher/test/domain/actions/wormchain/HandleWormchainLogs.test.ts +++ b/blockchain-watcher/test/domain/actions/wormchain/HandleWormchainLogs.test.ts @@ -40,22 +40,24 @@ describe("HandleWormchainLogs", () => { }); const mapper = (tx: WormchainLog) => { - return { - name: "log-message-published", - address: "wormhole1ufs3tlq4umljk0qfe8k5ya0x6hpavn897u2cnf9k0en9jr7qarqqaqfk2j", - chainId: 3104, - txHash: "0x7f61bf387fdb700d32d2b40ccecfb70ae46a2f82775242d04202bb7a538667c6", - blockHeight: 153549311n, - blockTime: 1709645685704036, - attributes: { - sender: "wormhole1ufs3tlq4umljk0qfe8k5ya0x6hpavn897u2cnf9k0en9jr7qarqqaqfk2j", - sequence: 203, - payload: "", - nonce: 75952, - consistencyLevel: 0, - protocol: "Token Bridge", + return [ + { + name: "log-message-published", + address: "wormhole1ufs3tlq4umljk0qfe8k5ya0x6hpavn897u2cnf9k0en9jr7qarqqaqfk2j", + chainId: 3104, + txHash: "0x7f61bf387fdb700d32d2b40ccecfb70ae46a2f82775242d04202bb7a538667c6", + blockHeight: 153549311n, + blockTime: 1709645685704036, + attributes: { + sender: "wormhole1ufs3tlq4umljk0qfe8k5ya0x6hpavn897u2cnf9k0en9jr7qarqqaqfk2j", + sequence: 203, + payload: "", + nonce: 75952, + consistencyLevel: 0, + protocol: "Token Bridge", + }, }, - }; + ]; }; const targetRepo = { diff --git a/blockchain-watcher/test/infrastructure/mappers/wormchain/wormchainLogMessagePublishedMapper.test.ts b/blockchain-watcher/test/infrastructure/mappers/wormchain/wormchainLogMessagePublishedMapper.test.ts index 0c8a10d2..82f9f682 100644 --- a/blockchain-watcher/test/infrastructure/mappers/wormchain/wormchainLogMessagePublishedMapper.test.ts +++ b/blockchain-watcher/test/infrastructure/mappers/wormchain/wormchainLogMessagePublishedMapper.test.ts @@ -1,31 +1,32 @@ import { wormchainLogMessagePublishedMapper } from "../../../../src/infrastructure/mappers/wormchain/wormchainLogMessagePublishedMapper"; import { describe, it, expect } from "@jest/globals"; import { WormchainLog } from "../../../../src/domain/entities/wormchain"; +import { LogFoundEvent, LogMessagePublished } from "../../../../src/domain/entities"; describe("wormchainLogMessagePublishedMapper", () => { it("should be able to map log to aptosLogMessagePublishedMapper", async () => { // When - const result = wormchainLogMessagePublishedMapper(log); + const result = wormchainLogMessagePublishedMapper(log) as any; if (result) { // Then - expect(result.name).toBe("log-message-published"); - expect(result.chainId).toBe(3104); - expect(result.txHash).toBe( + expect(result[0].name).toBe("log-message-published"); + expect(result[0].chainId).toBe(3104); + expect(result[0].txHash).toBe( "0xa08b0ac6ee67e21d3dd89f48f60cc907fc867288f4439bcf72731b0884d8aff2" ); - expect(result.address).toBe( + expect(result[0].address).toBe( "wormhole1ufs3tlq4umljk0qfe8k5ya0x6hpavn897u2cnf9k0en9jr7qarqqaqfk2j" ); - expect(result.attributes.consistencyLevel).toBe(0); - expect(result.attributes.nonce).toBe(7671); - expect(result.attributes.payload).toBe( + expect(result[0].attributes.consistencyLevel).toBe(0); + expect(result[0].attributes.nonce).toBe(7671); + expect(result[0].attributes.payload).toBe( "0100000000000000000000000000000000000000000000000000000000555643a3f5edec8471c75624ebc4079a634326d96a689e6157d79abe8f5a6f94472853bc00018622b98735cb870ae0cb22bd4ea58cfb512bd4002247ccd0b250eb6d0c5032fc00010000000000000000000000000000000000000000000000000000000000000000" ); - expect(result.attributes.sender).toBe( + expect(result[0].attributes.sender).toBe( "aeb534c45c3049d380b9d9b966f9895f53abd4301bfaff407fa09dea8ae7a924" ); - expect(result.attributes.sequence).toBe(28603); + expect(result[0].attributes.sequence).toBe(28603); } }); });