Skip to content

Commit

Permalink
Merge pull request #5836 from connext/main
Browse files Browse the repository at this point in the history
testnet-prod <- main sync (for pnp fix)
  • Loading branch information
preethamr authored Mar 11, 2024
2 parents 2a64589 + bfbffa3 commit 83d5647
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/adapters/database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@
},
"gitHead": "937a7cde93e6ac1e151c6374f48adf83d3fa4ec6",
"stableVersion": "2.0.0"
}
}
41 changes: 25 additions & 16 deletions packages/agents/lighthouse/src/tasks/prover/operations/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import {
XMessage,
ExecStatus,
DBHelper,
canonizeId,
parseBodyFromMessage,
parseSenderFromMessage,
parseNonceFromMessage,
parseOriginFromMessage,
} from "@connext/nxtp-utils";
import { ReadTransaction } from "@connext/nxtp-txservice";

import {
NoDestinationDomainForProof,
Expand Down Expand Up @@ -154,32 +158,37 @@ export const processMessages = async (brokerMessage: BrokerMessage, _requestCont
}

// Verify handle will work once proven
let tx = {};
try {
const reconciledEncodedData = contracts.connext.encodeFunctionData("handle", [
originDomain,
message.origin.index,
canonizeId(originConnext),
message.origin.message,
]);
const tx = {
const parsedMessage = {
originDomain: parseOriginFromMessage(message.origin.message),
nonce: parseNonceFromMessage(message.origin.message),
sender: parseSenderFromMessage(message.origin.message),
body: parseBodyFromMessage(message.origin.message),
};
tx = {
to: connext,
from: destinationSpokeConnector,
data: reconciledEncodedData,
data: contracts.connext.encodeFunctionData("handle", [
parsedMessage.originDomain,
parsedMessage.nonce,
parsedMessage.sender,
parsedMessage.body,
]),
domain: +destinationDomain,
};
const gas = await chainreader.getGasEstimateWithRevertCode(tx);
logger.debug("Getting gas estimate for reconcile", requestContext, methodContext, { ...tx });
const gas = await chainreader.getGasEstimateWithRevertCode(tx as ReadTransaction);
logger.debug("Gas estimated for reconcile", requestContext, methodContext, {
gas: gas.toString(),
...tx,
});
} catch (err: unknown) {
// ignore message
logger.warn(
"Failed to estimate gas for reconcile",
requestContext,
methodContext,
jsonifyError(err as NxtpError),
);
logger.warn("Failed to estimate gas for reconcile", requestContext, methodContext, {
...jsonifyError(err as NxtpError),
tx,
});
continue;
}

Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from "./parse";
export * from "./signatures";
export * from "./swap";
export * from "./time";
export * from "./messages";
38 changes: 38 additions & 0 deletions packages/utils/src/helpers/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Messages are formatted:
// prefix (76 bytes) + data (variable length)
// where prefix:
// domain (4 bytes) + sender (32 bytes) + nonce (4 bytes) + destination domain (4 bytes) + recipient (32 bytes)

export const MESSAGE_PREFIX_LENGTH = 76;

export const parseBodyFromMessage = (message: string): string => {
// you want to splice out prefix
return parseSubstring(message, MESSAGE_PREFIX_LENGTH);
};

export const parseOriginFromMessage = (message: string): string => {
return parseSubstring(message, 0, 4);
};

export const parseSenderFromMessage = (message: string): string => {
return parseSubstring(message, 4, 32);
};

export const parseNonceFromMessage = (message: string): string => {
return parseSubstring(message, 4 + 32, 4);
};

export const parseDestinationFromMessage = (message: string): string => {
return parseSubstring(message, 4 + 32 + 4, 4);
};

export const parseRecipientFromMessage = (message: string): string => {
return parseSubstring(message, 4 + 32 + 4 + 4, 32);
};

const parseSubstring = (message: string, bytesToStrip: number, bytesLength?: number): string => {
const stripped = message.startsWith("0x")
? message.slice(2).substring(bytesToStrip * 2)
: message.substring(bytesToStrip * 2);
return `0x${bytesLength ? stripped.slice(0, bytesLength * 2) : stripped}`;
};
85 changes: 85 additions & 0 deletions packages/utils/test/helpers/messages.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { expect } from "@connext/nxtp-utils";

import {
parseBodyFromMessage,
parseSenderFromMessage,
parseNonceFromMessage,
parseDestinationFromMessage,
parseRecipientFromMessage,
parseOriginFromMessage,
} from "../../src";
import { BigNumber } from "ethers";

const SAMPLE_MESSAGE =
"0x6172626f000000000000000000000000ee9dec2712cce65174b561151701bf54b99c24c800000d1f00626e62000000000000000000000000cd401c10afa37d641d2f594852da94c700e4f2ce00657468000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480300000000000000000000000000000000000000000000000000000000f7e9029879b9a8d764c36c3cae232f27e7e30d5f0b08e90425bc403ecacacb484c3d1251";
const SAMPLE_ORIGIN = 1634886255;
const SAMPLE_NONCE = 3359;
const SAMPLE_DESTINATION = 6450786;
const SAMPLE_RECIPIENT = "0x000000000000000000000000cd401c10afa37d641d2f594852da94c700e4f2ce";
const SAMPLE_SENDER = "0x000000000000000000000000ee9dec2712cce65174b561151701bf54b99c24c8";
const SAMPLE_BODY =
"0x00657468000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480300000000000000000000000000000000000000000000000000000000f7e9029879b9a8d764c36c3cae232f27e7e30d5f0b08e90425bc403ecacacb484c3d1251";

describe("Helpers:Messages", () => {
describe("#parseBodyFromMessage", () => {
it("handles 0x-prefixed messages", () => {
expect(parseBodyFromMessage(SAMPLE_MESSAGE)).to.be.deep.eq(SAMPLE_BODY);
});

it("handles unprefixed messages", () => {
expect(parseBodyFromMessage(SAMPLE_MESSAGE.slice(2))).to.be.deep.eq(SAMPLE_BODY);
});
});

describe("#parseOriginFromMessage", () => {
it("handles 0x-prefixed messages", () => {
expect(BigNumber.from(parseOriginFromMessage(SAMPLE_MESSAGE)).toNumber()).to.be.deep.eq(SAMPLE_ORIGIN);
});

it("handles unprefixed messages", () => {
expect(BigNumber.from(parseOriginFromMessage(SAMPLE_MESSAGE.slice(2))).toNumber()).to.be.deep.eq(SAMPLE_ORIGIN);
});
});

describe("#parseSenderFromMessage", () => {
it("handles 0x-prefixed messages", () => {
expect(parseSenderFromMessage(SAMPLE_MESSAGE)).to.be.deep.eq(SAMPLE_SENDER);
});

it("handles unprefixed messages", () => {
expect(parseSenderFromMessage(SAMPLE_MESSAGE.slice(2))).to.be.deep.eq(SAMPLE_SENDER);
});
});

describe("#parseNonceFromMessage", () => {
it("handles 0x-prefixed messages", () => {
expect(BigNumber.from(parseNonceFromMessage(SAMPLE_MESSAGE)).toNumber()).to.be.deep.eq(SAMPLE_NONCE);
});

it("handles unprefixed messages", () => {
expect(BigNumber.from(parseNonceFromMessage(SAMPLE_MESSAGE.slice(2))).toNumber()).to.be.deep.eq(SAMPLE_NONCE);
});
});

describe("#parseDestinationFromMessage", () => {
it("handles 0x-prefixed messages", () => {
expect(BigNumber.from(parseDestinationFromMessage(SAMPLE_MESSAGE)).toNumber()).to.be.deep.eq(SAMPLE_DESTINATION);
});

it("handles unprefixed messages", () => {
expect(BigNumber.from(parseDestinationFromMessage(SAMPLE_MESSAGE.slice(2))).toNumber()).to.be.deep.eq(
SAMPLE_DESTINATION,
);
});
});

describe("#parseRecipientFromMessage", () => {
it("handles 0x-prefixed messages", () => {
expect(parseRecipientFromMessage(SAMPLE_MESSAGE)).to.be.deep.eq(SAMPLE_RECIPIENT);
});

it("handles unprefixed messages", () => {
expect(parseRecipientFromMessage(SAMPLE_MESSAGE.slice(2))).to.be.deep.eq(SAMPLE_RECIPIENT);
});
});
});

0 comments on commit 83d5647

Please sign in to comment.