Skip to content

Commit

Permalink
feat(satp-hermes): bungee & ethereum bridge
Browse files Browse the repository at this point in the history
* added logic for satp-bridge supporting the ethereum connector
* finished the integration with bungee
* changed common stringify for safeStableStringify across the package
* added test for the new bridge & end-to-end tests for the new features
* fabric-connector: fix bug handling empty read-write sets
* bungee: strategy fabric fix bug handling empty read-write sets
* bungee: hole package introduce safeStableStringify
* ethereum-connector: bump web3 packages versions to
    fix (web3/web3.js#6798)

Signed-off-by: Eduardo Vasques <eduardovasques10@tecnico.ulisboa.pt>
  • Loading branch information
eduv09 authored and rafaelbelchiorbd committed Nov 13, 2024
1 parent edbcfab commit 97916a4
Show file tree
Hide file tree
Showing 66 changed files with 4,010 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import {
AdminApi,
TransactionApi,
} from "@hyperledger/cactus-plugin-satp-hermes/src/main/typescript/generated/gateway-client/typescript-axios/api";
import { ClaimFormat } from "@hyperledger/cactus-plugin-satp-hermes/src/main/typescript/generated/proto/cacti/satp/v02/common/message_pb";

export interface ICbdcBridgingAppDummyInfrastructureOptions {
logLevel?: LogLevelDesc;
Expand Down Expand Up @@ -439,6 +440,7 @@ export class CbdcBridgingAppDummyInfrastructure {
contractName: CbdcBridgingAppDummyInfrastructure.SATP_WRAPPER,
options: this.fabricConnectorBridgeOptions,
bungeeOptions: pluginBungeeFabricOptions,
claimFormat: ClaimFormat.DEFAULT,
} as FabricConfig;

const besuConfig = {
Expand All @@ -454,6 +456,7 @@ export class CbdcBridgingAppDummyInfrastructure {
options: this.besuOptions,
bungeeOptions: pluginBungeeBesuOptions,
gas: 999999999999999,
claimFormat: ClaimFormat.DEFAULT,
};

const besuGatewayOptions: SATPGatewayConfig = {
Expand Down
1 change: 1 addition & 0 deletions packages/cactus-plugin-bungee-hermes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"http-errors-enhanced-cjs": "2.0.1",
"key-encoder": "2.0.3",
"merkletreejs": "0.3.11",
"safe-stable-stringify": "2.5.0",
"typescript-optional": "2.0.1",
"uuid": "10.0.0",
"web3": "1.6.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
LoggerProvider,
Secp256k1Keys,
} from "@hyperledger/cactus-common";
import { stringify as safeStableStringify } from "safe-stable-stringify";

import { v4 as uuidV4 } from "uuid";
import {
ICactusPlugin,
Expand Down Expand Up @@ -231,8 +233,8 @@ export class PluginBungeeHermes implements ICactusPlugin, IPluginWebService {
view.setCreator(this.pubKeyBungee);
view.setKey(uuidV4());
return {
view: JSON.stringify(view),
signature: this.sign(JSON.stringify(view)),
view: safeStableStringify(view),
signature: this.sign(safeStableStringify(view)),
};
}
onMergeViews(request: MergeViewsRequest): MergeViewsResponse {
Expand Down Expand Up @@ -267,7 +269,7 @@ export class PluginBungeeHermes implements ICactusPlugin, IPluginWebService {
request.policyArguments ? request.policyArguments : [],
);
return {
integratedView: JSON.stringify(integratedView),
integratedView: safeStableStringify(integratedView),
signature: integratedView.signature,
};
}
Expand All @@ -287,7 +289,7 @@ export class PluginBungeeHermes implements ICactusPlugin, IPluginWebService {
this.logger.info("Generating view for request: ", request);
const response = this.generateView(snapshot, ti, tf, request.viewID);
return {
view: JSON.stringify(response.view),
view: safeStableStringify(response.view),
signature: response.signature,
};
}
Expand All @@ -308,7 +310,7 @@ export class PluginBungeeHermes implements ICactusPlugin, IPluginWebService {
const view = new View(this.pubKeyBungee, tI, tF, snapshot, id);
snapshot.pruneStates(tI, tF);

const signature = this.sign(JSON.stringify(view));
const signature = this.sign(safeStableStringify(view));

return { view: view, signature: signature };
}
Expand Down Expand Up @@ -414,7 +416,7 @@ export class PluginBungeeHermes implements ICactusPlugin, IPluginWebService {
integratedView: integratedView,
//The paper specs suggest the integratedView should be jointly signed by all participants.
//That process is left to be addressed in the future
signature: this.sign(JSON.stringify(integratedView)),
signature: this.sign(safeStableStringify(integratedView)),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
Logger,
LoggerProvider,
} from "@hyperledger/cactus-common";
import { stringify as safeStableStringify } from "safe-stable-stringify";

import {
DefaultApi as BesuApi,
EthContractInvocationType,
Expand Down Expand Up @@ -170,7 +172,7 @@ export class StrategyBesu implements ObtainLedgerStrategy {
"Transaction: " +
log.transactionHash +
"\nData: " +
JSON.stringify(log.data) +
safeStableStringify(log.data) +
"\n =========== \n",
);
const proof = new Proof({
Expand All @@ -185,7 +187,7 @@ export class StrategyBesu implements ObtainLedgerStrategy {
transaction.setTarget(networkDetails.contractAddress as string);
transaction.setPayload(txTx.input ? txTx.input : ""); //FIXME: payload = transaction input ?
transactions.push(transaction);
values.push(JSON.stringify(log.data));
values.push(safeStableStringify(log.data));

blocks.set(transaction.getId(), txBlock);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
LoggerProvider,
Checks,
} from "@hyperledger/cactus-common";
import { stringify as safeStableStringify } from "safe-stable-stringify";

import {
Web3SigningCredential,
DefaultApi as EthereumApi,
Expand Down Expand Up @@ -214,7 +216,7 @@ export class StrategyEthereum implements ObtainLedgerStrategy {
"Transaction: " +
log.transactionHash +
"\nData: " +
JSON.stringify(log.data) +
safeStableStringify(log.data) +
"\n =========== \n",
);
const proof = new Proof({
Expand All @@ -229,7 +231,7 @@ export class StrategyEthereum implements ObtainLedgerStrategy {
transaction.setTarget(networkDetails.contractAddress as string);
transaction.setPayload(txTx.data.input ? txTx.data.input : ""); //FIXME: payload = transaction input ?
transactions.push(transaction);
values.push(JSON.stringify(log.data));
values.push(safeStableStringify(log.data));

blocks.set(transaction.getId(), txBlock.data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Logger,
LoggerProvider,
} from "@hyperledger/cactus-common";
import { stringify as safeStableStringify } from "safe-stable-stringify";
import { Transaction } from "../view-creation/transaction";
import { State } from "../view-creation/state";
import { StateProof } from "../view-creation/state-proof";
Expand Down Expand Up @@ -101,7 +102,11 @@ export class StrategyFabric implements ObtainLedgerStrategy {
mspid: receipt.transactionCreator.mspid,
}),
);
assetValues.push(JSON.parse(receipt.rwsetWriteData).Value.toString());
if (!receipt.rwsetWriteData) {
assetValues.push("");
} else {
assetValues.push(JSON.parse(receipt.rwsetWriteData).Value.toString());
}
tx.setStateId(assetKey);
tx.setTarget(receipt.channelID + ": " + receipt.chainCodeName);

Expand Down Expand Up @@ -135,10 +140,11 @@ export class StrategyFabric implements ObtainLedgerStrategy {
//only adding last block for each state, in the state proof
stateProof.addBlock({
blockHash: block.hash,
blockCreator: JSON.stringify({
mspid: last_receipt.blockMetaData.mspid,
id: last_receipt.blockMetaData.blockCreatorID,
}),
blockCreator:
safeStableStringify({
mspid: last_receipt.blockMetaData.mspid,
id: last_receipt.blockMetaData.blockCreatorID,
}) ?? "",
blockSigners: block.signers,
});

Expand Down Expand Up @@ -170,7 +176,7 @@ export class StrategyFabric implements ObtainLedgerStrategy {
if (!response) {
throw new InternalServerError(`${fn} response is falsy`);
}
const receiptLockRes = JSON.stringify(response);
const receiptLockRes = safeStableStringify(response);
if (!receiptLockRes) {
throw new InternalServerError(`${fn} receiptLockRes is falsy`);
}
Expand Down Expand Up @@ -199,7 +205,7 @@ export class StrategyFabric implements ObtainLedgerStrategy {
throw new InternalServerError(`${fn} response.data is falsy`);
}

const receiptLockRes = JSON.stringify(data);
const receiptLockRes = safeStableStringify(data);
if (!receiptLockRes) {
throw new InternalServerError(`${fn} receiptLockRes is falsy`);
}
Expand Down Expand Up @@ -275,7 +281,7 @@ export class StrategyFabric implements ObtainLedgerStrategy {
);
}

const block = JSON.parse(JSON.stringify(block_data)).decodedBlock;
const block = JSON.parse(safeStableStringify(block_data)).decodedBlock;

const blockSig = block.metadata.metadata[0].signatures;
const sigs = [];
Expand All @@ -289,7 +295,7 @@ export class StrategyFabric implements ObtainLedgerStrategy {
},
signature: Buffer.from(sig.signature.data).toString("hex"),
};
sigs.push(JSON.stringify(decoded));
sigs.push(safeStableStringify(decoded));
}
return {
hash: Buffer.from(block.header.data_hash.data).toString("hex"),
Expand Down Expand Up @@ -440,7 +446,7 @@ export class StrategyFabric implements ObtainLedgerStrategy {
ts,
new TransactionProof(new Proof({ creator: "" }), txId), //transaction proof details are set in function 'generateLedgerStates'
);
transaction.setPayload(JSON.stringify(tx.value));
transaction.setPayload(safeStableStringify(tx.value) ?? "");
transactions.push(transaction);
}
return transactions.reverse();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Proof is a general purpose type, used to represent signatures of diverse elements.
// Proof may be used, for example in Fabric, to represent an transaction endorsement
// Or simply the signature of a transaction upon is creation (in Besu)
import { stringify as safeStableStringify } from "safe-stable-stringify";

export class Proof {
// The term creator refers to the ID of the entity who created the signature
// For example endorserID in Fabric (when Proof represents an endorsement)
Expand Down Expand Up @@ -36,7 +38,7 @@ export class Proof {
mspid: this.mspid,
signature: this.signature,
};
return JSON.stringify(proof);
return safeStableStringify(proof);
}

public getCreator(): string {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { State } from "./state";
import { stringify as safeStableStringify } from "safe-stable-stringify";

export class Snapshot {
private id: string;
Expand Down Expand Up @@ -99,7 +100,7 @@ export class Snapshot {
stateBins: this.stateBins,
};

return JSON.stringify(snapshotJson);
return safeStableStringify(snapshotJson);
}

public removeState(stateId: string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { StateProof } from "./state-proof";
import { Transaction } from "./transaction";
import { stringify as safeStableStringify } from "safe-stable-stringify";

export class State {
private id: string;
Expand Down Expand Up @@ -36,7 +37,7 @@ export class State {

for (const tx of this.transactions) {
txs.push(tx.getTxJson());
txEndorsements.push(JSON.stringify(tx.getProof()));
txEndorsements.push(safeStableStringify(tx.getProof()));
}

const jsonSnap = {
Expand All @@ -47,7 +48,7 @@ export class State {
proofs: txEndorsements,
};

return JSON.stringify(jsonSnap);
return safeStableStringify(jsonSnap);
}

public getTransactions() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Proof } from "./proof";
import { stringify as safeStableStringify } from "safe-stable-stringify";

export class TransactionProof {
private transactionCreator: Proof;
Expand Down Expand Up @@ -27,7 +28,7 @@ export class TransactionProof {
transactionCreator: this.transactionCreator,
endorsements: this.endorsements,
};
return JSON.stringify(proof);
return safeStableStringify(proof);
}

public getCreator(): Proof {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Proof } from "./proof";
import { TransactionProof } from "./transaction-proof";
import { stringify as safeStableStringify } from "safe-stable-stringify";

export class Transaction {
private id: string;
Expand Down Expand Up @@ -52,7 +53,7 @@ export class Transaction {

public getTxJson(): string {
const tx = { Id: this.id, TimeStamp: this.timeStamp };
return JSON.stringify(tx);
return safeStableStringify(tx);
}

public getProof(): TransactionProof {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Transaction } from "./transaction";
import { IPrivacyPolicy, IPrivacyPolicyValue } from "./privacy-policies";
import { PrivacyPolicyOpts } from "../generated/openapi/typescript-axios";
import { JsObjectSigner } from "@hyperledger/cactus-common";
import { stringify as safeStableStringify } from "safe-stable-stringify";

export interface IViewMetadata {
viewId: string;
Expand Down Expand Up @@ -87,9 +88,9 @@ export class View {
const transactions: string[] = [];

for (const state of this.snapshot.getStateBins()) {
states.push(JSON.stringify(state.getStateProof()));
states.push(safeStableStringify(state.getStateProof()));
for (const transaction of state.getTransactions()) {
transactions.push(JSON.stringify(transaction.getProof()));
transactions.push(safeStableStringify(transaction.getProof()));
}
}

Expand All @@ -112,7 +113,7 @@ export class View {
tF: this.tF,
snapshot: this.snapshot,
};
return JSON.stringify(viewStr);
return safeStableStringify(viewStr);
// return this.snapshot.getSnapshotJson();
}
public getViewProof(): {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Transaction } from "../view-creation/transaction";
import { IViewMetadata } from "../view-creation/view";
import { MergePolicyOpts } from "../generated/openapi/typescript-axios";
import { JsObjectSigner } from "@hyperledger/cactus-common";
import { stringify as safeStableStringify } from "safe-stable-stringify";

export class IntegratedView {
private id: string;
Expand Down Expand Up @@ -114,11 +115,11 @@ export class IntegratedView {
const states: string[] = [];
const transactions: string[] = [];
this.getAllTransactions().forEach((transaction) => {
transactions.push(JSON.stringify(transaction.getProof()));
transactions.push(safeStableStringify(transaction.getProof()));
});

this.getAllStates().forEach((state) => {
states.push(JSON.stringify(state.getStateProof()));
states.push(safeStableStringify(state.getStateProof()));
});
const statesTree = new MerkleTree(states, undefined, {
sort: true,
Expand All @@ -129,7 +130,7 @@ export class IntegratedView {
hashLeaves: true,
});
const viewsTree = new MerkleTree(
this.viewsMetadata.map((x) => JSON.stringify(x)),
this.viewsMetadata.map((x) => safeStableStringify(x)),
undefined,
{
sort: true,
Expand Down
6 changes: 3 additions & 3 deletions packages/cactus-plugin-ledger-connector-ethereum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@
"sanitize-html": "2.12.1",
"socket.io-client-fixed-types": "4.5.4",
"typescript-optional": "2.0.1",
"web3": "4.4.0",
"web3-eth": "4.4.0",
"web3-eth-contract": "4.2.0"
"web3": "4.14.0",
"web3-eth": "4.10.0",
"web3-eth-contract": "4.7.0"
},
"devDependencies": {
"@hyperledger/cactus-plugin-keychain-memory": "2.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export async function getTransactionReceiptByTxID(
if (!extensionNsRwset.rwset) continue;

const rwset = extensionNsRwset.rwset;
if (!rwset.writes) continue;
if (!rwset.writes || rwset.writes.length === 0) continue;
const rwsetWrite = rwset.writes;
if (!rwsetWrite[0].key) continue;
const rwsetKey = rwsetWrite[0].key;
Expand Down
Loading

0 comments on commit 97916a4

Please sign in to comment.