Skip to content

Commit

Permalink
style(docs/examples/supply-chain): move block scoped code to functions
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
petermetz committed Mar 2, 2024
1 parent 4ca792d commit 67802c2
Showing 1 changed file with 159 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export class SupplyChainAppDummyInfrastructure {
public async start(): Promise<void> {
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`);
Expand All @@ -162,11 +162,7 @@ export class SupplyChainAppDummyInfrastructure {

public async deployContracts(): Promise<ISupplyChainContractDeploymentInfo> {
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,
Expand All @@ -176,164 +172,175 @@ 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/[email protected]+incompatible",
"github.com/Shopify/[email protected]",
"github.com/fsouza/[email protected]",
"github.com/grpc-ecosystem/[email protected]",
"github.com/hashicorp/[email protected]",
"github.com/hyperledger/[email protected]",
"github.com/hyperledger/[email protected]",
"github.com/miekg/[email protected]",
"github.com/mitchellh/[email protected]",
"github.com/onsi/[email protected]",
"github.com/onsi/[email protected]",
"github.com/op/[email protected]",
"github.com/pkg/[email protected]",
"github.com/spf13/[email protected]",
"github.com/stretchr/[email protected]",
"github.com/sykesm/[email protected]",
"go.uber.org/[email protected]",
"golang.org/x/[email protected]",
"golang.org/x/[email protected]",
"google.golang.org/[email protected]",
],
});
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,
bookshelfRepository,
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<IEthContractDeployment> {
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<IEthContractDeployment> {
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<IFabricContractDeployment> {
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/[email protected]+incompatible",
"github.com/Shopify/[email protected]",
"github.com/fsouza/[email protected]",
"github.com/grpc-ecosystem/[email protected]",
"github.com/hashicorp/[email protected]",
"github.com/hyperledger/[email protected]",
"github.com/hyperledger/[email protected]",
"github.com/miekg/[email protected]",
"github.com/mitchellh/[email protected]",
"github.com/onsi/[email protected]",
"github.com/onsi/[email protected]",
"github.com/op/[email protected]",
"github.com/pkg/[email protected]",
"github.com/spf13/[email protected]",
"github.com/stretchr/[email protected]",
"github.com/sykesm/[email protected]",
"go.uber.org/[email protected]",
"golang.org/x/[email protected]",
"golang.org/x/[email protected]",
"google.golang.org/[email protected]",
],
});
this.log.debug("Supply chain app Fabric contract deployment result:", res);

const shipmentRepository = {
chaincodeId: "shipment",
channelName: "mychannel",
};
return shipmentRepository;
}
}

0 comments on commit 67802c2

Please sign in to comment.