Skip to content

Commit

Permalink
refactor(plugin-htlc-eth-besu): stop using the deprecated api.encodeP…
Browse files Browse the repository at this point in the history
…acked

1. Add tests to vanilla HashTimeLock
2. Replace abi.encodePacked by abi.encode
3. Compile and replace generated ABIs (for HashTimeLock and PrivateHashTimeLock)

Peter's additional changes:
1. Replace all the function calls that were missed.
2. Also update the hash function from sha256 to keccak256 where it was missed
3. Recompile the contracts again but with the VScode extension that gives them
the additional properties with their metadata like the contract name that we
use during deployment.
4. Modify the test case so that it computes the hash for the contract ID
purely in Javascript instead of doing a call to the EVM to do the same.

Co-authored-by: Peter Somogyvari <peter.somogyvari@accenture.com>

Signed-off-by: Rafael Belchior <rafael.belchior@tecnico.ulisboa.pt>
Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
  • Loading branch information
RafaelAPB authored and petermetz committed Jul 24, 2023
1 parent 292d287 commit f4f7516
Show file tree
Hide file tree
Showing 22 changed files with 23,630 additions and 49,341 deletions.
6 changes: 5 additions & 1 deletion extensions/cactus-plugin-htlc-coordinator-besu/package.json
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

0 comments on commit f4f7516

Please sign in to comment.