Resolved issue mapping tx hash

This commit is contained in:
julian merlo 2024-03-25 18:41:08 -03:00
parent 83198e8565
commit 617a886b25
4 changed files with 78 additions and 67 deletions

View File

@ -5,26 +5,28 @@ import { WormchainLog } from "../../entities/wormchain";
export class HandleWormchainLogs { export class HandleWormchainLogs {
constructor( constructor(
private readonly cfg: HandleWormchainLogsOptions, private readonly cfg: HandleWormchainLogsOptions,
private readonly mapper: (tx: WormchainLog) => TransactionFoundEvent, private readonly mapper: (tx: WormchainLog) => TransactionFoundEvent[],
private readonly target: (parsed: TransactionFoundEvent[]) => Promise<void>, private readonly target: (parsed: TransactionFoundEvent[]) => Promise<void>,
private readonly statsRepo: StatRepository private readonly statsRepo: StatRepository
) {} ) {}
public async handle(logs: WormchainLog[]): Promise<TransactionFoundEvent[]> { public async handle(logs: WormchainLog[]): Promise<TransactionFoundEvent[]> {
const mappedItems = logs.map((log) => { const filterLogs: TransactionFoundEvent[] = [];
const logMap = this.mapper(log);
return logMap;
});
const filterItems = mappedItems.filter((item) => { logs.map((log) => {
if (item) { const logMapped = this.mapper(log);
this.report();
return item; if (logMapped && logMapped.length > 0) {
logMapped.forEach((log) => {
this.report();
filterLogs.push(log);
});
} }
return logMapped;
}); });
await this.target(filterItems); await this.target(filterLogs);
return filterItems; return filterLogs;
} }
private report() { private report() {

View File

@ -9,44 +9,52 @@ let logger: winston.Logger = winston.child({ module: "wormchainLogMessagePublish
export const wormchainLogMessagePublishedMapper = ( export const wormchainLogMessagePublishedMapper = (
log: WormchainLog log: WormchainLog
): LogFoundEvent<LogMessagePublished> | undefined => { ): LogFoundEvent<LogMessagePublished[]> | [] => {
const { coreContract, sequence, payload, emitter, nonce, hash } = transactionAttibutes(log); 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( 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", name: "log-message-published",
address: CORE_ADDRESS, address: CORE_ADDRESS,
chainId: 3104, chainId: 3104,
txHash: hash, txHash: tx.hash,
blockHeight: log.blockHeight, blockHeight: log.blockHeight,
blockTime: log.timestamp, blockTime: log.timestamp,
attributes: { attributes: {
sender: emitter, sender: tx.emitter,
sequence: sequence, sequence: tx.sequence,
payload: payload, payload: tx.payload,
nonce: nonce, nonce: tx.nonce,
consistencyLevel: 0, consistencyLevel: 0,
}, },
}; });
} });
return logMessages;
}; };
function transactionAttibutes(log: WormchainLog): TransactionAttributes { function transactionAttributes(log: WormchainLog): TransactionAttributes[] {
let coreContract; const transactionAttributes: TransactionAttributes[] = [];
let sequence;
let payload;
let emitter;
let nonce;
let hash;
log.transactions?.forEach((tx) => { 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 key = Buffer.from(attr.key, "base64").toString().toLowerCase();
const value = Buffer.from(attr.value, "base64").toString().toLowerCase(); const value = Buffer.from(attr.value, "base64").toString().toLowerCase();
@ -65,22 +73,20 @@ function transactionAttibutes(log: WormchainLog): TransactionAttributes {
break; break;
case "_contract_address": case "_contract_address":
case "contract_address": case "contract_address":
if (value.toLocaleLowerCase() === CORE_ADDRESS.toLowerCase()) { if (value.toLowerCase() === CORE_ADDRESS.toLowerCase()) {
coreContract = true; coreContract = true;
} }
break; break;
} }
}); }
if (coreContract && sequence && payload && emitter && nonce) {
hash = tx.hash;
transactionAttributes.push({ coreContract, sequence, payload, emitter, nonce, hash });
}
}); });
return { return transactionAttributes;
coreContract,
sequence,
payload,
emitter,
nonce,
hash,
};
} }
type TransactionAttributes = { type TransactionAttributes = {

View File

@ -40,22 +40,24 @@ describe("HandleWormchainLogs", () => {
}); });
const mapper = (tx: WormchainLog) => { const mapper = (tx: WormchainLog) => {
return { return [
name: "log-message-published", {
address: "wormhole1ufs3tlq4umljk0qfe8k5ya0x6hpavn897u2cnf9k0en9jr7qarqqaqfk2j", name: "log-message-published",
chainId: 3104, address: "wormhole1ufs3tlq4umljk0qfe8k5ya0x6hpavn897u2cnf9k0en9jr7qarqqaqfk2j",
txHash: "0x7f61bf387fdb700d32d2b40ccecfb70ae46a2f82775242d04202bb7a538667c6", chainId: 3104,
blockHeight: 153549311n, txHash: "0x7f61bf387fdb700d32d2b40ccecfb70ae46a2f82775242d04202bb7a538667c6",
blockTime: 1709645685704036, blockHeight: 153549311n,
attributes: { blockTime: 1709645685704036,
sender: "wormhole1ufs3tlq4umljk0qfe8k5ya0x6hpavn897u2cnf9k0en9jr7qarqqaqfk2j", attributes: {
sequence: 203, sender: "wormhole1ufs3tlq4umljk0qfe8k5ya0x6hpavn897u2cnf9k0en9jr7qarqqaqfk2j",
payload: "", sequence: 203,
nonce: 75952, payload: "",
consistencyLevel: 0, nonce: 75952,
protocol: "Token Bridge", consistencyLevel: 0,
protocol: "Token Bridge",
},
}, },
}; ];
}; };
const targetRepo = { const targetRepo = {

View File

@ -1,31 +1,32 @@
import { wormchainLogMessagePublishedMapper } from "../../../../src/infrastructure/mappers/wormchain/wormchainLogMessagePublishedMapper"; import { wormchainLogMessagePublishedMapper } from "../../../../src/infrastructure/mappers/wormchain/wormchainLogMessagePublishedMapper";
import { describe, it, expect } from "@jest/globals"; import { describe, it, expect } from "@jest/globals";
import { WormchainLog } from "../../../../src/domain/entities/wormchain"; import { WormchainLog } from "../../../../src/domain/entities/wormchain";
import { LogFoundEvent, LogMessagePublished } from "../../../../src/domain/entities";
describe("wormchainLogMessagePublishedMapper", () => { describe("wormchainLogMessagePublishedMapper", () => {
it("should be able to map log to aptosLogMessagePublishedMapper", async () => { it("should be able to map log to aptosLogMessagePublishedMapper", async () => {
// When // When
const result = wormchainLogMessagePublishedMapper(log); const result = wormchainLogMessagePublishedMapper(log) as any;
if (result) { if (result) {
// Then // Then
expect(result.name).toBe("log-message-published"); expect(result[0].name).toBe("log-message-published");
expect(result.chainId).toBe(3104); expect(result[0].chainId).toBe(3104);
expect(result.txHash).toBe( expect(result[0].txHash).toBe(
"0xa08b0ac6ee67e21d3dd89f48f60cc907fc867288f4439bcf72731b0884d8aff2" "0xa08b0ac6ee67e21d3dd89f48f60cc907fc867288f4439bcf72731b0884d8aff2"
); );
expect(result.address).toBe( expect(result[0].address).toBe(
"wormhole1ufs3tlq4umljk0qfe8k5ya0x6hpavn897u2cnf9k0en9jr7qarqqaqfk2j" "wormhole1ufs3tlq4umljk0qfe8k5ya0x6hpavn897u2cnf9k0en9jr7qarqqaqfk2j"
); );
expect(result.attributes.consistencyLevel).toBe(0); expect(result[0].attributes.consistencyLevel).toBe(0);
expect(result.attributes.nonce).toBe(7671); expect(result[0].attributes.nonce).toBe(7671);
expect(result.attributes.payload).toBe( expect(result[0].attributes.payload).toBe(
"0100000000000000000000000000000000000000000000000000000000555643a3f5edec8471c75624ebc4079a634326d96a689e6157d79abe8f5a6f94472853bc00018622b98735cb870ae0cb22bd4ea58cfb512bd4002247ccd0b250eb6d0c5032fc00010000000000000000000000000000000000000000000000000000000000000000" "0100000000000000000000000000000000000000000000000000000000555643a3f5edec8471c75624ebc4079a634326d96a689e6157d79abe8f5a6f94472853bc00018622b98735cb870ae0cb22bd4ea58cfb512bd4002247ccd0b250eb6d0c5032fc00010000000000000000000000000000000000000000000000000000000000000000"
); );
expect(result.attributes.sender).toBe( expect(result[0].attributes.sender).toBe(
"aeb534c45c3049d380b9d9b966f9895f53abd4301bfaff407fa09dea8ae7a924" "aeb534c45c3049d380b9d9b966f9895f53abd4301bfaff407fa09dea8ae7a924"
); );
expect(result.attributes.sequence).toBe(28603); expect(result[0].attributes.sequence).toBe(28603);
} }
}); });
}); });