From 67802c2b0db1e36453b72b0670c8a9d6d56511c6 Mon Sep 17 00:00:00 2001 From: Peter Somogyvari Date: Fri, 1 Mar 2024 17:15:51 -0800 Subject: [PATCH] style(docs/examples/supply-chain): move block scoped code to functions This is a purely stylistic change that makes the code read much better and also reduces the chance of future bugs that would get introduced due to block scoping side effects that people not experienced with the language could make. Signed-off-by: Peter Somogyvari --- .../supply-chain-app-dummy-infrastructure.ts | 311 +++++++++--------- 1 file changed, 159 insertions(+), 152 deletions(-) diff --git a/examples/cactus-example-supply-chain-backend/src/main/typescript/infrastructure/supply-chain-app-dummy-infrastructure.ts b/examples/cactus-example-supply-chain-backend/src/main/typescript/infrastructure/supply-chain-app-dummy-infrastructure.ts index 5fcf9735977..53f6ad805b1 100644 --- a/examples/cactus-example-supply-chain-backend/src/main/typescript/infrastructure/supply-chain-app-dummy-infrastructure.ts +++ b/examples/cactus-example-supply-chain-backend/src/main/typescript/infrastructure/supply-chain-app-dummy-infrastructure.ts @@ -150,7 +150,7 @@ export class SupplyChainAppDummyInfrastructure { public async start(): Promise { try { this.log.info(`Starting dummy infrastructure...`); - await this.fabric.start(); + await this.fabric.start({ omitPull: false }); await this.besu.start(); await this.quorum.start(); this.log.info(`Started dummy infrastructure OK`); @@ -162,11 +162,7 @@ export class SupplyChainAppDummyInfrastructure { public async deployContracts(): Promise { try { - this.log.info(`Deploying smart contracts...`); - - let bambooHarvestRepository: IEthContractDeployment; - let bookshelfRepository: IEthContractDeployment; - let shipmentRepository: IFabricContractDeployment; + this.log.info(`Deploying example supply chain app smart contracts...`); await this.keychain.set( BookshelfRepositoryJSON.contractName, @@ -176,151 +172,10 @@ export class SupplyChainAppDummyInfrastructure { BambooHarvestRepositoryJSON.contractName, JSON.stringify(BambooHarvestRepositoryJSON), ); - { - this._quorumAccount = await this.quorum.createEthTestAccount(2000000); - const rpcApiHttpHost = await this.quorum.getRpcApiHttpHost(); - - const pluginRegistry = new PluginRegistry(); - pluginRegistry.add(this.keychain); - const connector = new PluginLedgerConnectorQuorum({ - instanceId: "PluginLedgerConnectorQuorum_Contract_Deployment", - rpcApiHttpHost, - logLevel: this.options.logLevel, - pluginRegistry, - }); - - const res = await connector.deployContract({ - contractName: BambooHarvestRepositoryJSON.contractName, - gas: 1000000, - web3SigningCredential: { - ethAccount: this.quorumAccount.address, - secret: this.quorumAccount.privateKey, - type: Web3SigningCredentialType.PrivateKeyHex, - }, - keychainId: this.keychain.getKeychainId(), - }); - const { - transactionReceipt: { contractAddress }, - } = res; - - bambooHarvestRepository = { - abi: BambooHarvestRepositoryJSON.abi, - address: contractAddress as string, - bytecode: BambooHarvestRepositoryJSON.bytecode, - contractName: BambooHarvestRepositoryJSON.contractName, - keychainId: this.keychain.getKeychainId(), - }; - } - - { - this._besuAccount = await this.besu.createEthTestAccount(2000000); - const rpcApiHttpHost = await this.besu.getRpcApiHttpHost(); - const rpcApiWsHost = await this.besu.getRpcApiWsHost(); - - const pluginRegistry = new PluginRegistry(); - pluginRegistry.add(this.keychain); - const connector = new PluginLedgerConnectorBesu({ - instanceId: "PluginLedgerConnectorBesu_Contract_Deployment", - rpcApiHttpHost, - rpcApiWsHost, - logLevel: this.options.logLevel, - pluginRegistry, - }); - - const res = await connector.deployContract({ - contractName: BookshelfRepositoryJSON.contractName, - bytecode: BookshelfRepositoryJSON.bytecode, - contractAbi: BookshelfRepositoryJSON.abi, - constructorArgs: [], - gas: 1000000, - web3SigningCredential: { - ethAccount: this.besuAccount.address, - secret: this.besuAccount.privateKey, - type: Web3SigningCredentialType.PrivateKeyHex, - }, - keychainId: this.keychain.getKeychainId(), - }); - const { - transactionReceipt: { contractAddress }, - } = res; - - bookshelfRepository = { - abi: BookshelfRepositoryJSON.abi, - address: contractAddress as string, - bytecode: BookshelfRepositoryJSON.bytecode, - contractName: BookshelfRepositoryJSON.contractName, - keychainId: this.keychain.getKeychainId(), - }; - } - - { - const connectionProfile = await this.fabric.getConnectionProfileOrg1(); - const sshConfig = await this.fabric.getSshConfig(); - const discoveryOptions: DiscoveryOptions = { - enabled: true, - asLocalhost: true, - }; - - const pluginRegistry = new PluginRegistry(); - pluginRegistry.add(this.keychain); - const connector = new PluginLedgerConnectorFabric({ - instanceId: "PluginLedgerConnectorFabric_Contract_Deployment", - dockerBinary: "/usr/local/bin/docker", - pluginRegistry, - peerBinary: "peer", - sshConfig: sshConfig, - logLevel: this.options.logLevel || "INFO", - connectionProfile: connectionProfile, - cliContainerEnv: org1Env, - discoveryOptions: discoveryOptions, - eventHandlerOptions: { - strategy: DefaultEventHandlerStrategy.NetworkScopeAllfortx, - }, - }); - - const res = await connector.deployContractGoSourceV1({ - tlsRootCertFiles: org1Env.CORE_PEER_TLS_ROOTCERT_FILE as string, - targetPeerAddresses: [org1Env.CORE_PEER_ADDRESS as string], - policyDslSource: "OR('Org1MSP.member','Org2MSP.member')", - channelId: "mychannel", - chainCodeVersion: "1.0.0", - constructorArgs: { Args: [] }, - goSource: { - body: Buffer.from(SHIPMENT_CONTRACT_GO_SOURCE).toString("base64"), - filename: "shipment.go", - }, - moduleName: "shipment", - targetOrganizations: [org1Env], - pinnedDeps: [ - "github.com/Knetic/govaluate@v3.0.0+incompatible", - "github.com/Shopify/sarama@v1.27.0", - "github.com/fsouza/go-dockerclient@v1.6.5", - "github.com/grpc-ecosystem/go-grpc-middleware@v1.2.1", - "github.com/hashicorp/go-version@v1.2.1", - "github.com/hyperledger/fabric@v1.4.8", - "github.com/hyperledger/fabric-amcl@v0.0.0-20200424173818-327c9e2cf77a", - "github.com/miekg/pkcs11@v1.0.3", - "github.com/mitchellh/mapstructure@v1.3.3", - "github.com/onsi/ginkgo@v1.14.1", - "github.com/onsi/gomega@v1.10.2", - "github.com/op/go-logging@v0.0.0-20160315200505-970db520ece7", - "github.com/pkg/errors@v0.9.1", - "github.com/spf13/viper@v1.7.1", - "github.com/stretchr/testify@v1.6.1", - "github.com/sykesm/zap-logfmt@v0.0.3", - "go.uber.org/zap@v1.16.0", - "golang.org/x/crypto@v0.0.0-20200820211705-5c72a883971a", - "golang.org/x/net@v0.0.0-20210503060351-7fd8e65b6420", - "google.golang.org/grpc@v1.31.1", - ], - }); - this.log.debug(res); - - shipmentRepository = { - chaincodeId: "shipment", - channelName: "mychannel", - }; - } + + const bambooHarvestRepository = await this.deployQuorumContract(); + const bookshelfRepository = await this.deployBesuContract(); + const shipmentRepository = await this.deployFabricContract(); const out: ISupplyChainContractDeploymentInfo = { bambooHarvestRepository, @@ -328,12 +183,164 @@ export class SupplyChainAppDummyInfrastructure { shipmentRepository, }; - this.log.info(`Deployed smart contracts OK`); + this.log.info(`Deployed example supply chain app smart contracts OK`); return out; } catch (ex) { + await new Promise((res) => setTimeout(res, 6000000)); this.log.error(`Deployment of smart contracts crashed: `, ex); throw ex; } } + + public async deployQuorumContract(): Promise { + this._quorumAccount = await this.quorum.createEthTestAccount(2000000); + const rpcApiHttpHost = await this.quorum.getRpcApiHttpHost(); + + const pluginRegistry = new PluginRegistry(); + pluginRegistry.add(this.keychain); + const connector = new PluginLedgerConnectorQuorum({ + instanceId: "PluginLedgerConnectorQuorum_Contract_Deployment", + rpcApiHttpHost, + logLevel: this.options.logLevel, + pluginRegistry, + }); + + const res = await connector.deployContract({ + contractName: BambooHarvestRepositoryJSON.contractName, + gas: 1000000, + web3SigningCredential: { + ethAccount: this.quorumAccount.address, + secret: this.quorumAccount.privateKey, + type: Web3SigningCredentialType.PrivateKeyHex, + }, + keychainId: this.keychain.getKeychainId(), + }); + const { + transactionReceipt: { contractAddress }, + } = res; + + const bambooHarvestRepository: IEthContractDeployment = { + abi: BambooHarvestRepositoryJSON.abi, + address: contractAddress as string, + bytecode: BambooHarvestRepositoryJSON.bytecode, + contractName: BambooHarvestRepositoryJSON.contractName, + keychainId: this.keychain.getKeychainId(), + }; + + return bambooHarvestRepository; + } + + public async deployBesuContract(): Promise { + this._besuAccount = await this.besu.createEthTestAccount(2000000); + const rpcApiHttpHost = await this.besu.getRpcApiHttpHost(); + const rpcApiWsHost = await this.besu.getRpcApiWsHost(); + + const pluginRegistry = new PluginRegistry(); + pluginRegistry.add(this.keychain); + const connector = new PluginLedgerConnectorBesu({ + instanceId: "PluginLedgerConnectorBesu_Contract_Deployment", + rpcApiHttpHost, + rpcApiWsHost, + logLevel: this.options.logLevel, + pluginRegistry, + }); + + const res = await connector.deployContract({ + contractName: BookshelfRepositoryJSON.contractName, + bytecode: BookshelfRepositoryJSON.bytecode, + contractAbi: BookshelfRepositoryJSON.abi, + constructorArgs: [], + gas: 1000000, + web3SigningCredential: { + ethAccount: this.besuAccount.address, + secret: this.besuAccount.privateKey, + type: Web3SigningCredentialType.PrivateKeyHex, + }, + keychainId: this.keychain.getKeychainId(), + }); + const { + transactionReceipt: { contractAddress }, + } = res; + + const bookshelfRepository: IEthContractDeployment = { + abi: BookshelfRepositoryJSON.abi, + address: contractAddress as string, + bytecode: BookshelfRepositoryJSON.bytecode, + contractName: BookshelfRepositoryJSON.contractName, + keychainId: this.keychain.getKeychainId(), + }; + + return bookshelfRepository; + } + + public async deployFabricContract(): Promise { + const connectionProfile = await this.fabric.getConnectionProfileOrg1(); + const sshConfig = await this.fabric.getSshConfig(); + const discoveryOptions: DiscoveryOptions = { + enabled: true, + asLocalhost: true, + }; + + const pluginRegistry = new PluginRegistry(); + pluginRegistry.add(this.keychain); + const connector = new PluginLedgerConnectorFabric({ + instanceId: "PluginLedgerConnectorFabric_Contract_Deployment", + dockerBinary: "/usr/local/bin/docker", + pluginRegistry, + peerBinary: "peer", + sshConfig: sshConfig, + logLevel: this.options.logLevel || "INFO", + connectionProfile: connectionProfile, + cliContainerEnv: org1Env, + discoveryOptions: discoveryOptions, + eventHandlerOptions: { + strategy: DefaultEventHandlerStrategy.NetworkScopeAllfortx, + }, + }); + + const res = await connector.deployContractGoSourceV1({ + tlsRootCertFiles: org1Env.CORE_PEER_TLS_ROOTCERT_FILE as string, + targetPeerAddresses: [org1Env.CORE_PEER_ADDRESS as string], + policyDslSource: "OR('Org1MSP.member','Org2MSP.member')", + channelId: "mychannel", + chainCodeVersion: "1.0.0", + constructorArgs: { Args: [] }, + goSource: { + body: Buffer.from(SHIPMENT_CONTRACT_GO_SOURCE).toString("base64"), + filename: "shipment.go", + }, + moduleName: "shipment", + targetOrganizations: [org1Env], + pinnedDeps: [ + "github.com/Knetic/govaluate@v3.0.0+incompatible", + "github.com/Shopify/sarama@v1.27.0", + "github.com/fsouza/go-dockerclient@v1.6.5", + "github.com/grpc-ecosystem/go-grpc-middleware@v1.2.1", + "github.com/hashicorp/go-version@v1.2.1", + "github.com/hyperledger/fabric@v1.4.8", + "github.com/hyperledger/fabric-amcl@v0.0.0-20200424173818-327c9e2cf77a", + "github.com/miekg/pkcs11@v1.0.3", + "github.com/mitchellh/mapstructure@v1.3.3", + "github.com/onsi/ginkgo@v1.14.1", + "github.com/onsi/gomega@v1.10.2", + "github.com/op/go-logging@v0.0.0-20160315200505-970db520ece7", + "github.com/pkg/errors@v0.9.1", + "github.com/spf13/viper@v1.7.1", + "github.com/stretchr/testify@v1.6.1", + "github.com/sykesm/zap-logfmt@v0.0.3", + "go.uber.org/zap@v1.16.0", + "golang.org/x/crypto@v0.0.0-20200820211705-5c72a883971a", + "golang.org/x/net@v0.0.0-20210503060351-7fd8e65b6420", + "google.golang.org/grpc@v1.31.1", + ], + }); + this.log.debug("Supply chain app Fabric contract deployment result:", res); + + const shipmentRepository = { + chaincodeId: "shipment", + channelName: "mychannel", + }; + return shipmentRepository; + } }