Skip to content

Commit

Permalink
chore(connector-go-eth): upgrade web3.js to recent version
Browse files Browse the repository at this point in the history
- Upgrade web3 package used by go-ethereum-socketio connector from "0.20.7" to "1.8.1"
- Major cleanup and refactor of go-ethereum-socketio logic,
  without affecting the current functionalities.
- Add safety checking of method called in web3Eth and contract functions.
  Add tests to check if it works.
- Add websocket eth RPC support and some helper functions in openethereum-test-ledger.
- Expose WS RPC in geth-testnet, use this port in sample apps
  (required to monitor new blocks in updated web3.js)
- Change raw use of sendRawTransaction to sendSignedTransaction in sample apps
  in order to conform to new web3.js version.
- Add safeStringifyException to cactus-common that can be used by validators
  to sanitize response errors.
- Refactor socketio-based validators to use safeStringifyException.
- Adjust connector README files.
- Improve cleanup after running sample apps
- Fix discounted-cartrade sample ethereum transfer and checking

Closes hyperledger-cacti#2088

Depends on hyperledger-cacti#2053

Signed-off-by: Michal Bajer <michal.bajer@fujitsu.com>
  • Loading branch information
outSH authored and petermetz committed Nov 26, 2022
1 parent 43c7b40 commit 9dd13cb
Show file tree
Hide file tree
Showing 29 changed files with 1,025 additions and 659 deletions.
14 changes: 11 additions & 3 deletions examples/cactus-example-discounted-asset-trade/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,15 @@ For development purposes, it might be useful to run the sample application outsi
```
# Ethereum fromAccount:
{ status: 200, amount: 100000 }
{ status: 200, amount: 1e+26 }

# Ethereum escrowAccount:
{ status: 200, amount: 0 }

# Ethereum toAccount:
{ status: 200, amount: 0 }


# Fabric:
[
{
Expand Down Expand Up @@ -240,10 +244,14 @@ For development purposes, it might be useful to run the sample application outsi

```
# Ethereum fromAccount:
{ status: 200, amount: 99950 }
{ status: 200, amount: 1.00000045e+26 }
# Ethereum escrowAccount:
{ status: 200, amount: 0 }
# Ethereum toAccount:
{ status: 200, amount: 50 }
{ status: 200, amount: 25 }
# Fabric:
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ export class BusinessLogicAssetTrade extends BusinessLogicBase {
// Register transaction information in transaction information management
const transactionInfo: TransactionInfo = new TransactionInfo();
transactionInfo.setRequestInfo(0, requestInfo);
this.transactionInfoManagement.addTransactionInfo(transactionInfo);

// pricing of trade by user's status which is employee or not employee
this.Pricing(requestInfo)
.then((result) => {
Expand All @@ -121,6 +119,10 @@ export class BusinessLogicAssetTrade extends BusinessLogicBase {
requestInfo.tradeID,
);

// Save transaction value
transactionInfo.setRequestInfo(1, requestInfo);
this.transactionInfoManagement.addTransactionInfo(transactionInfo);

// trade status update
this.transactionInfoManagement.setStatus(
tradeInfo,
Expand Down Expand Up @@ -352,7 +354,7 @@ export class BusinessLogicAssetTrade extends BusinessLogicBase {
// TODO: Get address of escrow and set parameter
const escrowAddress = config.assetTradeInfo.ethereum.escrowAddress;

// Generate parameters for// sendRawTransaction
// Generate parameters for// sendSignedTransaction
const txParam: {
fromAddress: string;
fromAddressPkey: string;
Expand Down Expand Up @@ -385,7 +387,7 @@ export class BusinessLogicAssetTrade extends BusinessLogicBase {
// Set Parameter
logger.debug("firstTransaction data : " + JSON.stringify(result.data));
const contract = {}; // NOTE: Since contract does not need to be specified, specify an empty object.
const method = { type: "web3Eth", command: "sendRawTransaction" };
const method = { type: "web3Eth", command: "sendSignedTransaction" };

const args = { args: [result.data["serializedTx"]] };
// Run Verifier (Ethereum)
Expand Down Expand Up @@ -500,7 +502,7 @@ export class BusinessLogicAssetTrade extends BusinessLogicBase {
// TODO: get escrow secret key
const escrowAddressPkey = config.assetTradeInfo.ethereum.escrowAddressPkey;

// Generate parameters for sendRawTransaction
// Generate parameters for sendSignedTransaction
const txParam: {
fromAddress: string;
fromAddressPkey: string;
Expand Down Expand Up @@ -538,7 +540,7 @@ export class BusinessLogicAssetTrade extends BusinessLogicBase {
// TODO: Neo!!
// Set Parameter
const contract = {}; // NOTE: Since contract does not need to be specified, specify an empty object.
const method = { type: "web3Eth", command: "sendRawTransaction" };
const method = { type: "web3Eth", command: "sendSignedTransaction" };
const args = { args: [result.data["serializedTx"]] };

// Run Verifier (Ethereum)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"shelljs": "0.8.5",
"socket.io": "4.4.1",
"ts-node": "8.9.1",
"web3": "1.7.0",
"web3": "1.8.1",
"xmlhttprequest": "1.8.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ const axios = require("axios");
async function main() {
// Read ethereum balances
const fromAccountResponse = await axios.get(
"http://localhost:5034/api/v1/bl/balance/0x06fc56347d91c6ad2dae0c3ba38eb12ab0d72e97",
"http://localhost:5034/api/v1/bl/balance/0xec709e1774f0ce4aba47b52a499f9abaaa159f71",
);
console.log("\n# Ethereum fromAccount:");
console.log(fromAccountResponse.data);

const escrowAccountResponse = await axios.get(
"http://localhost:5034/api/v1/bl/balance/0x36e146d5afab61ab125ee671708eeb380aea05b6",
);
console.log("\n# Ethereum escrowAccount:");
console.log(escrowAccountResponse.data);

const toAccountResponse = await axios.get(
"http://localhost:5034/api/v1/bl/balance/0x9d624f7995e8bd70251f8265f2f9f2b49f169c55",
);
Expand Down
14 changes: 13 additions & 1 deletion examples/cactus-example-discounted-asset-trade/script-cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,21 @@ rm -rf ./etc/cactus/

echo ">> Stop the docker containers of Ethereum, Fabric and Indy"
docker stop geth1 asset_trade_faio2x_testnet indy-testnet-pool
docker rm geth1 asset_trade_faio2x_testnet indy-testnet-pool
docker rm geth1 asset_trade_faio2x_testnet indy-testnet-pool $(docker container ls -q --all --filter name=geth-testnet_init-chain)
docker compose rm -f

echo ">> Clear docker networks"
docker network rm geth1net geth-testnet_default fabric-all-in-one_testnet-2x geth-testnet_default indy-testnet_indy_net
docker network rm $(docker network ls -q --filter name=cactus-example-discounted-asset-trade)

echo ">> Clear indy testnet sandbox"
pushd ../../tools/docker/indy-testnet/
./script-cleanup.sh
popd

echo ">> Remove geth files"
pushd ../../tools/docker/geth-testnet/
rm -fr ./data-geth1/geth/
popd

echo "Cleanup done."
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export class BusinessLogicElectricityTrade extends BusinessLogicBase {
// Set Parameter
logger.debug("remittanceTransaction data : " + json2str(result.data));
const contract = {}; // NOTE: Since contract does not need to be specified, specify an empty object.
const method = { type: "web3Eth", command: "sendRawTransaction" };
const method = { type: "web3Eth", command: "sendSignedTransaction" };
const args: RawTransationResultData = {
args: [result.data["serializedTx"]],
};
Expand Down
2 changes: 1 addition & 1 deletion examples/cactus-example-electricity-trade/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"shelljs": "0.8.5",
"socket.io": "4.4.1",
"ts-node": "8.9.1",
"web3": "1.7.0",
"web3": "1.8.1",
"xmlhttprequest": "1.8.0"
},
"devDependencies": {
Expand Down
8 changes: 7 additions & 1 deletion examples/cactus-example-electricity-trade/script-cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ docker rm -f sawtooth_all_in_one_ledger_1x \
cactus-example-electricity-trade-blp \
cactus-example-electricity-trade-ethereum-validator \
cactus-example-electricity-trade-sawtooth-validator \
cmd-socketio-base-dummy
cmd-socketio-base-dummy \
$(docker container ls -q --all --filter name=geth-testnet_init-chain)

echo ">> Remove docker networks"
docker network rm sawtooth_aio_testnet_1x \
Expand All @@ -20,4 +21,9 @@ docker network rm sawtooth_aio_testnet_1x \
geth1net \
geth-testnet_default

echo ">> Remove geth files"
pushd ../../tools/docker/geth-testnet/
rm -fr ./data-geth1/geth/
popd

echo "Cleanup done."
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"ethereumjs-common": "1.5.2",
"ethereumjs-tx": "2.1.2",
"ts-node": "9.1.1",
"web3": "1.7.0",
"web3": "1.8.1",
"socket.io": "4.4.1"
},
"devDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions packages/cactus-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,18 @@
"webpack:dev:web": "webpack --env=dev --target=web --config ../../webpack.config.js"
},
"dependencies": {
"fast-safe-stringify": "2.1.1",
"json-stable-stringify": "1.0.1",
"key-encoder": "2.0.3",
"loglevel": "1.7.1",
"loglevel-plugin-prefix": "0.8.4",
"secp256k1": "4.0.2",
"sanitize-html": "2.7.0",
"sha3": "2.1.4"
},
"devDependencies": {
"@types/json-stable-stringify": "1.0.33",
"@types/sanitize-html": "2.6.2",
"@types/secp256k1": "4.0.3",
"@types/uuid": "8.3.1",
"uuid": "8.3.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/cactus-common/src/main/typescript/checks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CodedError } from "./coded-error";
import { CodedError } from "./error-utils";

export class Checks {
/**
Expand Down
14 changes: 0 additions & 14 deletions packages/cactus-common/src/main/typescript/coded-error.ts

This file was deleted.

32 changes: 32 additions & 0 deletions packages/cactus-common/src/main/typescript/error-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import safeStringify from "fast-safe-stringify";
import sanitizeHtml from "sanitize-html";

/**
* Utility class for better exception handling by way of having a code property
* which is designated to uniquely identify the type of exception that was
* thrown, essentially acting as a discriminator property.
*/
export class CodedError extends Error {
constructor(public readonly message: string, public readonly code: string) {
super(message);
}

sameCode(codedError: CodedError): boolean {
return this.code === codedError?.code;
}
}

/**
* Return secure string representation of error from the input.
* Handles circular structures and removes HTML.
*
* @param error Any object to return as an error, preferable `Error`
* @returns Safe string representation of an error.
*/
export function safeStringifyException(error: unknown): string {
if (error instanceof Error) {
return sanitizeHtml(error.stack || error.message);
}

return sanitizeHtml(safeStringify(error));
}
2 changes: 1 addition & 1 deletion packages/cactus-common/src/main/typescript/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export { Objects } from "./objects";
export { Strings } from "./strings";
export { Bools } from "./bools";
export { Checks } from "./checks";
export { CodedError } from "./coded-error";
export { CodedError, safeStringifyException } from "./error-utils";

export {
JsObjectSigner,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CodedError } from "../../../main/typescript/coded-error";
import { CodedError } from "../../../main/typescript/error-utils";
import "jest-extended";

test("Error test", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
"dependencies": {
"@types/node": "14.17.32",
"@hyperledger/cactus-cmd-socketio-server": "1.1.2",
"@hyperledger/cactus-common": "1.1.2",
"body-parser": "1.17.2",
"cookie-parser": "1.4.6",
"debug": "4.1.1",
"express": "4.17.3",
"fabric-ca-client": "1.4.19",
"fabric-client": "1.4.19",
"fabric-network": "1.4.19",
"fast-safe-stringify": "2.1.1",
"fs-extra": "10.0.0",
"protobufjs": "5.0.3",
"grpc": "1.24.11",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ import FabricClient from "fabric-client";
// IF declaration for fabric
import { getClientAndChannel, getSubmitterAndEnroll } from "./fabricaccess";
// config file
import { configRead, signMessageJwt } from "@hyperledger/cactus-cmd-socketio-server";
import {
configRead,
signMessageJwt,
} from "@hyperledger/cactus-cmd-socketio-server";
// Log settings
import { getLogger } from "log4js";
const logger = getLogger("ServerMonitorPlugin[" + process.pid + "]");
logger.level = configRead<string>("logLevel", "info");
// utility
import safeStringify from "fast-safe-stringify";
import { safeStringifyException } from "@hyperledger/cactus-common";

export type MonitorCallback = (callback: {
status: number;
Expand Down Expand Up @@ -152,7 +155,7 @@ export class ServerMonitorPlugin {
logger.error(err);
const errObj = {
status: 504,
errorDetail: safeStringify(err),
errorDetail: safeStringifyException(err),
};
cb(errObj);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/

import path from "path";
import safeStringify from "fast-safe-stringify";
import Client, {
Proposal,
ProposalRequest,
Expand All @@ -37,6 +36,7 @@ const connUserName = configRead<string>("fabric.connUserName");

// Log settings
import { getLogger } from "log4js";
import { safeStringifyException } from "@hyperledger/cactus-common";
const logger = getLogger("ServerPlugin[" + process.pid + "]");
logger.level = configRead<string>("logLevel", "info");

Expand Down Expand Up @@ -231,7 +231,7 @@ export class ServerPlugin {
retObj = {
resObj: {
status: 504,
errorDetail: safeStringify(err),
errorDetail: safeStringifyException(err),
},
};
logger.error(err);
Expand Down Expand Up @@ -304,7 +304,7 @@ export class ServerPlugin {
.catch((err) => {
retObj = {
status: 504,
errorDetail: safeStringify(err),
errorDetail: safeStringifyException(err),
};
logger.error(err);
return reject(retObj);
Expand Down Expand Up @@ -373,7 +373,7 @@ export class ServerPlugin {
throw {
resObj: {
status: 504,
errorDetail: safeStringify(error),
errorDetail: safeStringifyException(error),
},
};
}
Expand Down Expand Up @@ -471,7 +471,7 @@ export class ServerPlugin {
throw {
resObj: {
status: 504,
errorDetail: safeStringify(error),
errorDetail: safeStringifyException(error),
},
};
}
Expand Down Expand Up @@ -630,7 +630,7 @@ export class ServerPlugin {
throw {
resObj: {
status: 504,
errorDetail: safeStringify(error),
errorDetail: safeStringifyException(error),
},
};
}
Expand Down Expand Up @@ -693,7 +693,7 @@ export class ServerPlugin {
throw {
resObj: {
status: 504,
errorDetail: safeStringify(error),
errorDetail: safeStringifyException(error),
},
};
}
Expand Down Expand Up @@ -752,7 +752,7 @@ export class ServerPlugin {
throw {
resObj: {
status: 504,
errorDetail: safeStringify(error),
errorDetail: safeStringifyException(error),
},
};
}
Expand Down
Loading

0 comments on commit 9dd13cb

Please sign in to comment.