Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(plugin-htlc-eth-besu): stop using the deprecated api.encodePacked #2538

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@
"@hyperledger/cactus-test-plugin-htlc-eth-besu-erc20": "2.0.0-alpha.1",
"axios": "0.21.4",
"body-parser": "1.19.0",
"fast-safe-stringify": "2.1.1",
"joi": "14.3.1",
"openapi-types": "7.0.1",
"prom-client": "13.1.0",
"run-time-error": "1.4.0",
"socket.io-client": "4.5.4",
"typescript-optional": "2.0.1"
},
Expand All @@ -77,7 +79,9 @@
"@hyperledger/cactus-test-tooling": "2.0.0-alpha.1",
"@types/express": "4.17.8",
"express": "4.17.3",
"socket.io": "4.5.4"
"socket.io": "4.5.4",
"web3-eth-abi": "4.0.3",
"web3-utils": "4.0.3"
},
"engines": {
"node": ">=10",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { v4 as uuidv4 } from "uuid";
import { Express } from "express";
import { promisify } from "util";
import { Optional } from "typescript-optional";
import { RuntimeError } from "run-time-error";
import fastSafeStringify from "fast-safe-stringify";
import OAS from "../json/openapi.json";
import {
ICactusPlugin,
Expand Down Expand Up @@ -328,8 +330,13 @@ export class PluginHTLCCoordinatorBesu
connectorId: withdrawCounterpartyRequest.connectorInstanceId,
keychainId: withdrawCounterpartyRequest.keychainId,
};
const res = await pluginHtlc.withdraw(withdrawRequest);
return res;
try {
const res = await pluginHtlc.withdraw(withdrawRequest);
return res;
} catch (ex: unknown) {
const cause = ex instanceof Error ? ex : fastSafeStringify(ex);
throw new WithdrawCounterpartyTxReverted(`EVM tx reverted:`, cause);
}
}
case HtlcPackage.Besu: {
const pluginOptions: IPluginHtlcEthBesuOptions = {
Expand Down Expand Up @@ -361,3 +368,5 @@ export class PluginHTLCCoordinatorBesu
}
}
}

export class WithdrawCounterpartyTxReverted extends RuntimeError {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Express, Request, Response } from "express";
import type { Express, Request, Response } from "express";
import fastSafeStringify from "fast-safe-stringify";

import {
IWebServiceEndpoint,
Expand All @@ -19,6 +20,7 @@ import {
} from "@hyperledger/cactus-core";
import { WithdrawCounterpartyRequest } from "../generated/openapi/typescript-axios";
import { PluginHTLCCoordinatorBesu } from "../plugin-htlc-coordinator-besu";
import { WithdrawCounterpartyTxReverted } from "../plugin-htlc-coordinator-besu";
import OAS from "../../json/openapi.json";

export interface IWithdrawCounterpartyOptions {
Expand Down Expand Up @@ -101,11 +103,17 @@ export class WithdrawCounterpartyEndpoint implements IWebServiceEndpoint {
}) as unknown) as PluginHTLCCoordinatorBesu;
const resBody = await connector.withdrawCounterparty(request);
res.json(resBody);
} catch (ex) {
res.status(500).json({
message: "Internal Server Error",
error: ex,
});
} catch (ex: unknown) {
if (ex instanceof WithdrawCounterpartyTxReverted) {
this.log.debug("%o %o", reqTag, ex);
res.status(400).json(ex);
} else {
const error = ex instanceof Error ? ex.message : fastSafeStringify(ex);
res.status(500).json({
message: "Internal Server Error",
error,
});
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import axios from "axios";
import test, { Test } from "tape-promise/tape";
import { v4 as uuidv4 } from "uuid";
import { AddressInfo } from "net";
Expand Down Expand Up @@ -312,11 +313,15 @@ test(testCase, async (t: Test) => {

try {
await htlcCoordinatorBesuApi.withdrawCounterpartyV1(withdrawCounterparty);
} catch (exp) {
const revertReason = exp.response.data.error.receipt.revertReason;
const regExp = new RegExp(/0e494e56414c49445f5345435245540/);
const rejectMsg = "response === throws OK";
t.match(revertReason, regExp, rejectMsg);
} catch (exp: unknown) {
if (axios.isAxiosError(exp) && exp.response) {
const revertReason = exp.response.data.cause.receipt.revertReason;
const regExp = new RegExp(/0e494e56414c49445f5345435245540/);
const rejectMsg = "response === throws OK";
t.match(revertReason, regExp, rejectMsg);
} else {
throw exp;
}
}

t.comment("Get balance of receiver account");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import express from "express";
import bodyParser from "body-parser";
import http from "http";
import { Server as SocketIoServer } from "socket.io";
import { encodeParameter } from "web3-eth-abi";
import { keccak256 } from "web3-utils";
import {
DefaultApi as HtlcCoordinatorBesuApi,
PluginFactoryHTLCCoordinatorBesu,
Expand Down Expand Up @@ -44,14 +46,12 @@ import HashTimeLockJSON from "@hyperledger/cactus-plugin-htlc-eth-besu-erc20/src
import TestTokenJSON from "@hyperledger/cactus-test-plugin-htlc-eth-besu-erc20/src/test/solidity/token-erc20-contract/Test_Token.json";
import DemoHelperJSON from "@hyperledger/cactus-test-plugin-htlc-eth-besu-erc20/src/test/solidity/token-erc20-contract/DemoHelpers.json";

const logLevel: LogLevelDesc = "INFO";
const logLevel: LogLevelDesc = "DEBUG";
const estimatedGas = 6721975;
const expiration = 2147483648;
const secret =
"0x3853485acd2bfc3c632026ee365279743af107a30492e3ceaa7aefc30c2a048a";
const receiver = "0x627306090abaB3A6e1400e9345bC60c78a8BEf57";
const hashLock =
"0x3c335ba7f06a8b01d0596589f73c19069e21c81e5013b91f408165d1bf623d32";
const firstHighNetWorthAccount = "0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1";
const privateKey =
"0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d";
Expand All @@ -72,6 +72,10 @@ test("BEFORE " + testCase, async (t: Test) => {
});

test(testCase, async (t: Test) => {

const secretEthAbiEncoded = encodeParameter("uint256", secret);
const hashLock = keccak256(secretEthAbiEncoded);

t.comment("Starting Besu Test Ledger");
const besuTestLedger = new BesuTestLedger();
await besuTestLedger.start();
Expand Down
Loading