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

feat(driver): added Monitor to fabric driver for missed events and other fixes #2401

Merged
merged 3 commits into from
Apr 25, 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
2 changes: 2 additions & 0 deletions weaver/core/drivers/fabric-driver/.env.docker.template
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ WALLET_PATH=
TLS_CREDENTIALS_DIR=<dir-with-tls-cert-and-key>
LEVELDB_LOCKED_MAX_RETRIES=<max-attempts-in-retry>
LEVELDB_LOCKED_RETRY_BACKOFF_MSEC=<retry-back-off-time-in-ms>
ENABLE_MONITOR=<true|false>
MONITOR_SYNC_PERIOD=<monitor-sync-period-in-seconds>
DOCKER_IMAGE_NAME=ghcr.io/hyperledger-labs/weaver-fabric-driver
DOCKER_TAG=1.4.0
EXTERNAL_NETWORK=<docker-bridge-network>
Expand Down
4 changes: 3 additions & 1 deletion weaver/core/drivers/fabric-driver/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ DB_PATH=driverdbs
WALLET_PATH=
DEBUG=true
LEVELDB_LOCKED_MAX_RETRIES=
LEVELDB_LOCKED_RETRY_BACKOFF_MSEC=
LEVELDB_LOCKED_RETRY_BACKOFF_MSEC=
ENABLE_MONITOR=false
MONITOR_SYNC_PERIOD=
2 changes: 2 additions & 0 deletions weaver/core/drivers/fabric-driver/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ services:
- DEBUG=false
- LEVELDB_LOCKED_MAX_RETRIES=${LEVELDB_LOCKED_MAX_RETRIES}
- LEVELDB_LOCKED_RETRY_BACKOFF_MSEC=${LEVELDB_LOCKED_RETRY_BACKOFF_MSEC}
- ENABLE_MONITOR=${ENABLE_MONITOR}
- MONITOR_SYNC_PERIOD=${MONITOR_SYNC_PERIOD}
volumes:
- ${CONNECTION_PROFILE}:/fabric-driver/ccp.json
- ${DRIVER_CONFIG}:/fabric-driver/config.json
Expand Down
6 changes: 5 additions & 1 deletion weaver/core/drivers/fabric-driver/server/dbConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class LevelDBConnector implements DBConnector {
dbRetryBackoffTime: number;

constructor(
dbName: string
dbName: string,
retryTimeout: number = 0
) {
if (!dbName || dbName.length == 0) {
dbName = "driverdb";
Expand All @@ -59,6 +60,9 @@ class LevelDBConnector implements DBConnector {
this.dbOpenMaxRetries = process.env.LEVELDB_LOCKED_MAX_RETRIES ? parseInt(process.env.LEVELDB_LOCKED_MAX_RETRIES) : 250;
// Retry back off time in ms, default 20ms
this.dbRetryBackoffTime = process.env.LEVELDB_LOCKED_RETRY_BACKOFF_MSEC ? parseInt(process.env.LEVELDB_LOCKED_RETRY_BACKOFF_MSEC) : 20;
if (retryTimeout > 0) {
this.dbOpenMaxRetries = Math.floor(retryTimeout / this.dbRetryBackoffTime);
}
}

async open(
Expand Down
446 changes: 258 additions & 188 deletions weaver/core/drivers/fabric-driver/server/listener.ts

Large diffs are not rendered by default.

22 changes: 20 additions & 2 deletions weaver/core/drivers/fabric-driver/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import events_grpc_pb from '@hyperledger-labs/weaver-protos-js/relay/events_grpc
import state_pb from '@hyperledger-labs/weaver-protos-js/common/state_pb';
import { invoke, packageFabricView } from './fabric-code';
import 'dotenv/config';
import { loadEventSubscriptionsFromStorage } from './listener'
import { loadEventSubscriptionsFromStorage, monitorBlockForMissedEvents } from './listener'
import { walletSetup } from './walletSetup';
import { subscribeEventHelper, unsubscribeEventHelper, signEventSubscriptionQuery, writeExternalStateHelper } from "./events"
import * as path from 'path';
import { handlePromise, relayCallback, getRelayClientForQueryResponse, getRelayClientForEventSubscription } from './utils';
import { handlePromise, relayCallback, getRelayClientForQueryResponse, getRelayClientForEventSubscription, delay } from './utils';
import { dbConnectionTest, eventSubscriptionTest } from "./tests"
import driverPb from '@hyperledger-labs/weaver-protos-js/driver/driver_pb';
import logger from './logger';
Expand Down Expand Up @@ -253,6 +253,22 @@ const configSetup = async () => {
logger.info(`Load Event Subscriptions Status: ${status}`);
};

const monitorService = async () => {
const delayTime: number = parseInt(process.env.MONITOR_SYNC_PERIOD ? process.env.MONITOR_SYNC_PERIOD : '30');
const networkName = process.env.NETWORK_NAME ? process.env.NETWORK_NAME : 'network1';
const flagEnable = process.env.ENABLE_MONITOR === 'false' ? false : true;
if (flagEnable) {
logger.info("Starting monitor...");
logger.info(`Monitor sync period: ${delayTime}`);
} else {
logger.info("Monitor disabled.");
}
while (flagEnable) {
await monitorBlockForMissedEvents(networkName);
await delay(delayTime * 1000);
}
}

// SERVER: Start the server with the provided url.
// TODO: We should have credentials locally to ensure that the driver can only communicate with the local relay.
if (process.env.DRIVER_TLS === 'true') {
Expand All @@ -268,13 +284,15 @@ if (process.env.DRIVER_TLS === 'true') {
configSetup().then(() => {
logger.info('Starting server with TLS');
server.start();
monitorService();
});
});
} else {
server.bindAsync(`${process.env.DRIVER_ENDPOINT}`, ServerCredentials.createInsecure(), (cb) => {
configSetup().then(() => {
logger.info('Starting server without TLS');
server.start();
monitorService();
});
});
}
2 changes: 1 addition & 1 deletion weaver/core/drivers/fabric-driver/server/walletSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const getDriverKeyCert = async (): Promise<any> => {
const walletPath = process.env.WALLET_PATH ? process.env.WALLET_PATH : path.join(process.cwd(), `wallet-${process.env.NETWORK_NAME ? process.env.NETWORK_NAME : 'network1'}`);
const config = getConfig();
const wallet = await Wallets.newFileSystemWallet(walletPath);
logger.debug(`Wallet path: ${walletPath}`);
logger.info(`Wallet path: ${walletPath}, relay id: ${config.relay.name}`);

const [keyCert, keyCertError] = await handlePromise(
InteroperableHelper.getKeyAndCertForRemoteRequestbyUserName(wallet, config.relay.name)
Expand Down
2 changes: 1 addition & 1 deletion weaver/core/drivers/fabric-driver/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"target": "es2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
Expand Down
17 changes: 12 additions & 5 deletions weaver/sdks/fabric/interoperation-node-sdk/src/eciesCrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,25 @@ function eciesDecryptMessage(recipientPrivateKey, cipherText, options) {
const privKey = ecdsa.keyFromPrivate(recipientPrivateKey.prvKeyHex, "hex");

const Z = privKey.derive(ephPubKey.pub); // 'z'
const kdfOutput = hkdf(Z.toArray(), ECIESKDFOutput, null, null, options); // The 'null's correspond to 's1' and 's2', which are both
// empty in our SNAMCC and ESCC plugin implementations
// Append missing leading zeros to Z
let ZArray = Z.toArray();
const zerosToAdd = 32 - ZArray.length;
for (let ii=0; ii<zerosToAdd; ii++) {
ZArray = new Uint8Array([0, ...ZArray]);
}
// The 'null's below correspond to 's1' and 's2',
// which are both set to nil in golang implementation of the encryption function
const kdfOutput = hkdf(ZArray, ECIESKDFOutput, null, null, options);

const kbuf = Buffer.from(kdfOutput);
const aesKey = kdfOutput.slice(0, AESKeyLength); // 'Ke'
const hmacKey = kdfOutput.slice(AESKeyLength, AESKeyLength + HMACKeyLength); // 'Km'
const hmacKey = kdfOutput.slice(AESKeyLength, AESKeyLength + HMACKeyLength);

const hmacKeyHash = new options.hashFunctionKeyDerivation();
hmacKeyHash.update(bytesToBits(hmacKey));
const hKm = bitsToBytes(hmacKeyHash.finalize());
const hKm = bitsToBytes(hmacKeyHash.finalize()); // 'Km'

const recoveredD = hmac(hKm, EM, options);

if (D.compare(Buffer.from(recoveredD)) !== 0) {
throw new Error("HMAC verify failed");
}
Expand Down
2 changes: 2 additions & 0 deletions weaver/tests/network-setups/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
fabric/dev/config
fabric/dev/bin
fabric/dev/.fabric-setup
fabric/dev/install-fabric.sh
fabric/dev/builders
.lock
*.lock
fabric/dev/scripts/config.json
Expand Down
15 changes: 9 additions & 6 deletions weaver/tests/network-setups/fabric/dev/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
FABRIC_VERSION=2.4.7
FABRIC_CA_VERSION=1.5.5
FABRIC_COUCH_VERSION=0.4.20
FABRIC_VERSION=2.5.0
FABRIC_CA_VERSION=1.5.6
CHAINCODE_NAME?=simplestate
NW?=network1
E2E_CONFIDENTIALITY?=false
Expand Down Expand Up @@ -100,7 +99,8 @@ deploy-cc-local: setup-cc-local


.fabric-setup:
curl -sSL https://bit.ly/2ysbOFE | bash -s -- $(FABRIC_VERSION) $(FABRIC_CA_VERSION) $(FABRIC_COUCH_VERSION) -s
curl -sSLO https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh && chmod +x install-fabric.sh
./install-fabric.sh --fabric-version $(FABRIC_VERSION) --ca-version $(FABRIC_CA_VERSION) docker binary
touch .fabric-setup

.PHONY: stop
Expand Down Expand Up @@ -130,12 +130,15 @@ remove-network2:
.PHONY: clean
clean: remove
+docker system prune -f || true
+rm -rf bin || true
+rm -rf config || true
+rm .fabric-setup || true
+chmod -R 755 ../shared/chaincode/interop
+rm -rf ../shared/chaincode/*

.PHONY: clean-all
clean-all: clean
+rm -rf bin || true
+rm .fabric-setup || true

chmod-artifacts:
sudo chmod 644 ../shared/network1/fabric-ca/ordererOrg/msp/keystore/*
sudo chmod 644 ../shared/network1/fabric-ca/org1/msp/keystore/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ services:
- ORDERER_GENERAL_CLUSTER_CLIENTCERTIFICATE=/var/hyperledger/orderer/tls/server.crt
- ORDERER_GENERAL_CLUSTER_CLIENTPRIVATEKEY=/var/hyperledger/orderer/tls/server.key
- ORDERER_GENERAL_CLUSTER_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
- FABRIC_CFG_PATH=/etc/hyperledger/fabric
working_dir: /opt/gopath/src/github.com/hyperledger/fabric
command: orderer
volumes:
Expand Down Expand Up @@ -63,7 +62,7 @@ services:
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_PROFILE_ENABLED=true
- CORE_PEER_PROFILE_ENABLED=false
- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
Expand All @@ -76,9 +75,9 @@ services:
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org1.${COMPOSE_PROJECT_NAME}.com:$PEER_ORG1_PORT
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.${COMPOSE_PROJECT_NAME}.com:$PEER_ORG1_PORT
- CORE_PEER_LOCALMSPID=Org1MSP
- FABRIC_CFG_PATH=/etc/hyperledger/fabric
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/msp
volumes:
- /var/run/:/host/var/run/
- /var/run/docker.sock:/host/var/run/docker.sock
- $NW_CFG_PATH/peerOrganizations/org1.${COMPOSE_PROJECT_NAME}.com/peers/peer0.org1.${COMPOSE_PROJECT_NAME}.com/msp:/etc/hyperledger/fabric/msp
- $NW_CFG_PATH/peerOrganizations/org1.${COMPOSE_PROJECT_NAME}.com/peers/peer0.org1.${COMPOSE_PROJECT_NAME}.com/tls:/etc/hyperledger/fabric/tls
- peer0.org1.${COMPOSE_PROJECT_NAME}.com:/var/hyperledger/production
Expand All @@ -105,7 +104,7 @@ services:
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_GOSSIP_USELEADERELECTION=true
- CORE_PEER_GOSSIP_ORGLEADER=false
- CORE_PEER_PROFILE_ENABLED=true
- CORE_PEER_PROFILE_ENABLED=false
- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
Expand All @@ -118,9 +117,9 @@ services:
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org2.${COMPOSE_PROJECT_NAME}.com:$PEER_ORG2_PORT
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.${COMPOSE_PROJECT_NAME}.com:$PEER_ORG2_PORT
- CORE_PEER_LOCALMSPID=Org2MSP
- FABRIC_CFG_PATH=/etc/hyperledger/fabric
- CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/msp
volumes:
- /var/run/:/host/var/run/
- /var/run/docker.sock:/host/var/run/docker.sock
- $NW_CFG_PATH/peerOrganizations/org2.${COMPOSE_PROJECT_NAME}.com/peers/peer0.org2.${COMPOSE_PROJECT_NAME}.com/msp:/etc/hyperledger/fabric/msp
- $NW_CFG_PATH/peerOrganizations/org2.${COMPOSE_PROJECT_NAME}.com/peers/peer0.org2.${COMPOSE_PROJECT_NAME}.com/tls:/etc/hyperledger/fabric/tls
- peer0.org2.${COMPOSE_PROJECT_NAME}.com:/var/hyperledger/production
Expand Down