Skip to content

Commit

Permalink
Separate client and runtime connections (contributes to IBM-Blockchai…
Browse files Browse the repository at this point in the history
…n#776)

Signed-off-by: Simon Stone <sstone1@uk.ibm.com>
  • Loading branch information
Simon Stone committed Mar 29, 2019
1 parent 0e09503 commit 2ad5f76
Show file tree
Hide file tree
Showing 24 changed files with 149 additions and 116 deletions.
4 changes: 2 additions & 2 deletions client/integrationTest/integrationTestUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { PackageRegistry } from '../src/packages/PackageRegistry';
import { ExtensionCommands } from '../ExtensionCommands';
import { FabricWalletRegistryEntry } from '../src/fabric/FabricWalletRegistryEntry';
import { FabricConnectionManager } from '../src/fabric/FabricConnectionManager';
import { IFabricConnection } from '../src/fabric/IFabricConnection';
import { IFabricClientConnection } from '../src/fabric/IFabricClientConnection';

// tslint:disable no-unused-expression
const should: Chai.Should = chai.should();
Expand Down Expand Up @@ -170,7 +170,7 @@ export class IntegrationTestUtil {

await vscode.commands.executeCommand(ExtensionCommands.CONNECT, gatewayEntry);

const fabricConnection: IFabricConnection = FabricConnectionManager.instance().getConnection();
const fabricConnection: IFabricClientConnection = FabricConnectionManager.instance().getConnection();
should.exist(fabricConnection);
}

Expand Down
6 changes: 3 additions & 3 deletions client/integrationTest/nodeTests/integrationNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { TestUtil } from '../../test/TestUtil';
import { FabricConnectionManager } from '../../src/fabric/FabricConnectionManager';
import { FabricRuntimeManager } from '../../src/fabric/FabricRuntimeManager';
import { FabricRuntime } from '../../src/fabric/FabricRuntime';
import { IFabricConnection } from '../../src/fabric/IFabricConnection';
import { MetadataUtil } from '../../src/util/MetadataUtil';
import { IntegrationTestUtil } from '../integrationTestUtil';
import { RuntimeTreeItem } from '../../src/explorer/runtimeOps/RuntimeTreeItem';
Expand All @@ -42,6 +41,7 @@ import { CommandUtil } from '../../src/util/CommandUtil';
import { PackageRegistryEntry } from '../../src/packages/PackageRegistryEntry';
import { PackageRegistry } from '../../src/packages/PackageRegistry';
import { GatewayTreeItem } from '../../src/explorer/model/GatewayTreeItem';
import { IFabricClientConnection } from '../../src/fabric/IFabricClientConnection';

chai.use(sinonChai);
chai.use(chaiAsPromised);
Expand Down Expand Up @@ -361,7 +361,7 @@ describe('Integration Tests for Node Smart Contracts', () => {

await integrationTestUtil.packageSmartContract();

const fabricConnection: IFabricConnection = FabricConnectionManager.instance().getConnection();
const fabricConnection: IFabricClientConnection = FabricConnectionManager.instance().getConnection();
should.exist(fabricConnection);

const allPackages: Array<PackageRegistryEntry> = await PackageRegistry.instance().getAll();
Expand Down Expand Up @@ -470,7 +470,7 @@ describe('Integration Tests for Node Smart Contracts', () => {
}
openFileNameArray.includes(pathToTestFile).should.be.true;
// Get the smart contract metadata
const connection: IFabricConnection = FabricConnectionManager.instance().getConnection();
const connection: IFabricClientConnection = FabricConnectionManager.instance().getConnection();
const smartContractTransactionsMap: Map<string, string[]> = await MetadataUtil.getTransactionNames(connection, smartContractName, 'mychannel');
let smartContractTransactionsArray: string[];
let contractName: string = '';
Expand Down
19 changes: 10 additions & 9 deletions client/src/commands/UserInputUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { FabricConnectionManager } from '../fabric/FabricConnectionManager';
import { PackageRegistry } from '../packages/PackageRegistry';
import { PackageRegistryEntry } from '../packages/PackageRegistryEntry';
import { FabricGatewayRegistryEntry } from '../fabric/FabricGatewayRegistryEntry';
import { IFabricConnection } from '../fabric/IFabricConnection';
import { MetadataUtil } from '../util/MetadataUtil';
import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter';
import { LogType } from '../logging/OutputAdapter';
Expand All @@ -31,6 +30,8 @@ import { FabricWalletRegistryEntry } from '../fabric/FabricWalletRegistryEntry';
import { FabricWalletRegistry } from '../fabric/FabricWalletRegistry';
import { IFabricWallet } from '../fabric/IFabricWallet';
import { FabricWalletGeneratorFactory } from '../fabric/FabricWalletGeneratorFactory';
import { IFabricRuntimeConnection } from '../fabric/IFabricRuntimeConnection';
import { IFabricClientConnection } from '../fabric/IFabricClientConnection';

export interface IBlockchainQuickPickItem<T = undefined> extends vscode.QuickPickItem {
data: T;
Expand Down Expand Up @@ -176,7 +177,7 @@ export class UserInputUtil {
}

public static async showPeerQuickPickBox(prompt: string): Promise<string | undefined> {
const connection: IFabricConnection = await FabricRuntimeManager.instance().getConnection();
const connection: IFabricRuntimeConnection = await FabricRuntimeManager.instance().getConnection();
const peerNames: Array<string> = connection.getAllPeerNames();

const quickPickOptions: vscode.QuickPickOptions = {
Expand All @@ -189,7 +190,7 @@ export class UserInputUtil {
}

public static async showChaincodeAndVersionQuickPick(prompt: string, peers: Set<string>, contractName?: string, contractVersion?: string): Promise<IBlockchainQuickPickItem<{ packageEntry: PackageRegistryEntry, workspace: vscode.WorkspaceFolder }> | undefined> {
const connection: IFabricConnection = await FabricRuntimeManager.instance().getConnection();
const connection: IFabricRuntimeConnection = await FabricRuntimeManager.instance().getConnection();
const tempQuickPickItems: IBlockchainQuickPickItem<{ packageEntry: PackageRegistryEntry, workspace: vscode.WorkspaceFolder }>[] = [];

// Get all installed smart contracts
Expand Down Expand Up @@ -429,7 +430,7 @@ export class UserInputUtil {

public static async showInstantiatedSmartContractsQuickPick(prompt: string, channelName?: string): Promise<IBlockchainQuickPickItem<{ name: string, channel: string, version: string }> | undefined> {
const outputAdapter: VSCodeBlockchainOutputAdapter = VSCodeBlockchainOutputAdapter.instance();
const connection: IFabricConnection = await FabricRuntimeManager.instance().getConnection();
const connection: IFabricRuntimeConnection = await FabricRuntimeManager.instance().getConnection();

let channels: Array<string> = [];
if (!channelName) {
Expand Down Expand Up @@ -485,7 +486,7 @@ export class UserInputUtil {

public static async showTransactionQuickPick(prompt: string, chaincodeName: string, channelName: string): Promise<IBlockchainQuickPickItem<{ name: string, contract: string }> | undefined> {
const fabricConnectionManager: FabricConnectionManager = FabricConnectionManager.instance();
const connection: IFabricConnection = fabricConnectionManager.getConnection();
const connection: IFabricClientConnection = fabricConnectionManager.getConnection();

if (!connection) {
VSCodeBlockchainOutputAdapter.instance().log(LogType.ERROR, 'No connection to a blockchain found');
Expand Down Expand Up @@ -524,7 +525,7 @@ export class UserInputUtil {

public static async showInstallableSmartContractsQuickPick(prompt: string, peers: Set<string>): Promise<IBlockchainQuickPickItem<{ packageEntry: PackageRegistryEntry, workspace: vscode.WorkspaceFolder }> | undefined> {
// Get connection
const connection: IFabricConnection = await FabricRuntimeManager.instance().getConnection();
const connection: IFabricRuntimeConnection = await FabricRuntimeManager.instance().getConnection();

const quickPickItems: IBlockchainQuickPickItem<{ packageEntry: PackageRegistryEntry, workspace: vscode.WorkspaceFolder }>[] = [];
// Get packaged contracts
Expand Down Expand Up @@ -626,7 +627,7 @@ export class UserInputUtil {
}

public static async showCertificateAuthorityQuickPickBox(prompt: string): Promise<string | undefined> {
const connection: IFabricConnection = await FabricRuntimeManager.instance().getConnection();
const connection: IFabricRuntimeConnection = await FabricRuntimeManager.instance().getConnection();
const caNameOption: string = connection.getCertificateAuthorityName();
const caNames: string[] = [caNameOption];

Expand Down Expand Up @@ -758,7 +759,7 @@ export class UserInputUtil {
}

private static async getChannels(): Promise<Array<IBlockchainQuickPickItem<Set<string>>>> {
const connection: IFabricConnection = await FabricRuntimeManager.instance().getConnection();
const connection: IFabricRuntimeConnection = await FabricRuntimeManager.instance().getConnection();

const quickPickItems: Array<IBlockchainQuickPickItem<Set<string>>> = [];
const peerNames: Array<string> = connection.getAllPeerNames();
Expand All @@ -780,7 +781,7 @@ export class UserInputUtil {
return quickPickItems;
}

private static async getInstalledContracts(connection: IFabricConnection, peers: Set<string>): Promise<IBlockchainQuickPickItem<{ packageEntry: PackageRegistryEntry, workspace: vscode.WorkspaceFolder }>[]> {
private static async getInstalledContracts(connection: IFabricRuntimeConnection, peers: Set<string>): Promise<IBlockchainQuickPickItem<{ packageEntry: PackageRegistryEntry, workspace: vscode.WorkspaceFolder }>[]> {
const tempQuickPickItems: IBlockchainQuickPickItem<{ packageEntry: PackageRegistryEntry, workspace: vscode.WorkspaceFolder }>[] = [];
for (const peer of peers) {
const chaincodes: Map<string, Array<string>> = await connection.getInstalledChaincode(peer);
Expand Down
15 changes: 7 additions & 8 deletions client/src/commands/connectCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
'use strict';
import * as vscode from 'vscode';
import { UserInputUtil, IBlockchainQuickPickItem } from './UserInputUtil';
import { IFabricConnection } from '../fabric/IFabricConnection';
import { FabricConnectionFactory } from '../fabric/FabricConnectionFactory';
import { FabricConnectionManager } from '../fabric/FabricConnectionManager';
import { FabricGatewayRegistryEntry } from '../fabric/FabricGatewayRegistryEntry';
Expand All @@ -28,6 +27,7 @@ import { IFabricWalletGenerator } from '../fabric/IFabricWalletGenerator';
import { FabricWalletGeneratorFactory } from '../fabric/FabricWalletGeneratorFactory';
import { ExtensionCommands } from '../../ExtensionCommands';
import { FabricWalletRegistryEntry } from '../fabric/FabricWalletRegistryEntry';
import { IFabricClientConnection } from '../fabric/IFabricClientConnection';

export async function connect(gatewayRegistryEntry: FabricGatewayRegistryEntry, identityName?: string): Promise<void> {
const outputAdapter: VSCodeBlockchainOutputAdapter = VSCodeBlockchainOutputAdapter.instance();
Expand Down Expand Up @@ -70,7 +70,7 @@ export async function connect(gatewayRegistryEntry: FabricGatewayRegistryEntry,
identityName = identityNames[0];
}

let connection: IFabricConnection;
let connection: IFabricClientConnection;
if (gatewayRegistryEntry.managedRuntime) {

const runtimeManager: FabricRuntimeManager = FabricRuntimeManager.instance();
Expand All @@ -85,15 +85,14 @@ export async function connect(gatewayRegistryEntry: FabricGatewayRegistryEntry,
}

gatewayRegistryEntry.connectionProfilePath = await runtime.getConnectionProfilePath();
connection = FabricConnectionFactory.createFabricRuntimeConnection(runtime);
runtimeData = 'managed runtime';
} else {
const connectionData: { connectionProfilePath: string } = {
connectionProfilePath: gatewayRegistryEntry.connectionProfilePath
};
connection = FabricConnectionFactory.createFabricClientConnection(connectionData);
}

const connectionData: { connectionProfilePath: string } = {
connectionProfilePath: gatewayRegistryEntry.connectionProfilePath
};
connection = FabricConnectionFactory.createFabricClientConnection(connectionData);

try {
await connection.connect(wallet, identityName);
connection.wallet = chosenWallet.data;
Expand Down
14 changes: 3 additions & 11 deletions client/src/commands/createNewIdentityCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import { CertificateAuthorityTreeItem } from '../explorer/runtimeOps/Certificate
import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter';
import { LogType } from '../logging/OutputAdapter';
import { UserInputUtil } from './UserInputUtil';
import { IFabricConnection } from '../fabric/IFabricConnection';
import { FabricRuntimeManager } from '../fabric/FabricRuntimeManager';
import { IFabricWallet } from '../fabric/IFabricWallet';
import { FabricRuntime } from '../fabric/FabricRuntime';
import { FabricConnectionFactory } from '../fabric/FabricConnectionFactory';
import { IFabricRuntimeConnection } from '../fabric/IFabricRuntimeConnection';

export async function createNewIdentity(certificateAuthorityTreeItem?: CertificateAuthorityTreeItem): Promise<void> {
const outputAdapter: VSCodeBlockchainOutputAdapter = VSCodeBlockchainOutputAdapter.instance();
Expand Down Expand Up @@ -53,11 +51,8 @@ export async function createNewIdentity(certificateAuthorityTreeItem?: Certifica
return;
}

let connection: IFabricConnection;

try {
const mspid: string = 'Org1MSP';
const adminName: string = 'Admin@org1.example.com';
const affiliation: string = 'org1.department1'; // Currently works for org1.department1, org1.department2
// check to see if identity of same name exists
const wallet: IFabricWallet = FabricRuntimeManager.instance().gatewayWallet;
Expand All @@ -67,10 +62,8 @@ export async function createNewIdentity(certificateAuthorityTreeItem?: Certifica
return;
}

const runtime: FabricRuntime = await FabricRuntimeManager.instance().getRuntime();
connection = FabricConnectionFactory.createFabricRuntimeConnection(runtime);
// Connect and then register the user
await connection.connect(wallet, adminName);
const connection: IFabricRuntimeConnection = await FabricRuntimeManager.instance().getConnection();
// Register the user
const secret: string = await connection.register(identityName, affiliation);

// Enroll the user
Expand All @@ -87,7 +80,6 @@ export async function createNewIdentity(certificateAuthorityTreeItem?: Certifica
} catch (error) {
outputAdapter.log(LogType.ERROR, `Issue creating new identity: ${error.message}`, `Issue creating new identity: ${error.toString()}`);

await connection.disconnect();
return;
}
}
4 changes: 2 additions & 2 deletions client/src/commands/installCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import { IBlockchainQuickPickItem, UserInputUtil } from './UserInputUtil';
import { PeerTreeItem } from '../explorer/runtimeOps/PeerTreeItem';
import { BlockchainTreeItem } from '../explorer/model/BlockchainTreeItem';
import { PackageRegistryEntry } from '../packages/PackageRegistryEntry';
import { IFabricConnection } from '../fabric/IFabricConnection';
import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter';
import { LogType } from '../logging/OutputAdapter';
import { FabricRuntimeManager } from '../fabric/FabricRuntimeManager';
import { ExtensionCommands } from '../../ExtensionCommands';
import { VSCodeBlockchainDockerOutputAdapter } from '../logging/VSCodeBlockchainDockerOutputAdapter';
import { IFabricRuntimeConnection } from '../fabric/IFabricRuntimeConnection';

export async function installSmartContract(treeItem?: BlockchainTreeItem, peerNames?: Set<string>, chosenPackage?: PackageRegistryEntry): Promise<PackageRegistryEntry | boolean> {
const outputAdapter: VSCodeBlockchainOutputAdapter = VSCodeBlockchainOutputAdapter.instance();
Expand Down Expand Up @@ -70,7 +70,7 @@ export async function installSmartContract(treeItem?: BlockchainTreeItem, peerNa

}

const connection: IFabricConnection = await FabricRuntimeManager.instance().getConnection();
const connection: IFabricRuntimeConnection = await FabricRuntimeManager.instance().getConnection();

const promises: Promise<string | void>[] = [];
for (const peer of peerNames) {
Expand Down
4 changes: 2 additions & 2 deletions client/src/commands/instantiateCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import * as vscode from 'vscode';
import { IBlockchainQuickPickItem, UserInputUtil } from './UserInputUtil';
import { ChannelTreeItem } from '../explorer/model/ChannelTreeItem';
import { BlockchainTreeItem } from '../explorer/model/BlockchainTreeItem';
import { IFabricConnection } from '../fabric/IFabricConnection';
import { Reporter } from '../util/Reporter';
import { PackageRegistryEntry } from '../packages/PackageRegistryEntry';
import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter';
import { LogType } from '../logging/OutputAdapter';
import { FabricRuntimeManager } from '../fabric/FabricRuntimeManager';
import { ExtensionCommands } from '../../ExtensionCommands';
import { VSCodeBlockchainDockerOutputAdapter } from '../logging/VSCodeBlockchainDockerOutputAdapter';
import { IFabricRuntimeConnection } from '../fabric/IFabricRuntimeConnection';

export async function instantiateSmartContract(treeItem?: BlockchainTreeItem): Promise<void> {

Expand Down Expand Up @@ -110,7 +110,7 @@ export async function instantiateSmartContract(treeItem?: BlockchainTreeItem): P
}, async (progress: vscode.Progress<{ message: string }>) => {

progress.report({ message: 'Instantiating Smart Contract' });
const connection: IFabricConnection = await FabricRuntimeManager.instance().getConnection();
const connection: IFabricRuntimeConnection = await FabricRuntimeManager.instance().getConnection();

VSCodeBlockchainDockerOutputAdapter.instance().show();
if (packageEntry) {
Expand Down
4 changes: 2 additions & 2 deletions client/src/commands/testSmartContractCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import * as path from 'path';
import * as os from 'os';
import { UserInputUtil, IBlockchainQuickPickItem, LanguageQuickPickItem } from './UserInputUtil';
import { FabricConnectionManager } from '../fabric/FabricConnectionManager';
import { IFabricConnection } from '../fabric/IFabricConnection';
import { VSCodeBlockchainOutputAdapter } from '../logging/VSCodeBlockchainOutputAdapter';
import { Reporter } from '../util/Reporter';
import { CommandUtil } from '../util/CommandUtil';
Expand All @@ -29,6 +28,7 @@ import { MetadataUtil } from '../util/MetadataUtil';
import { LogType } from '../logging/OutputAdapter';
import { ExtensionCommands } from '../../ExtensionCommands';
import { FabricWalletRegistryEntry } from '../fabric/FabricWalletRegistryEntry';
import { IFabricClientConnection } from '../fabric/IFabricClientConnection';

export async function testSmartContract(chaincode?: InstantiatedContractTreeItem): Promise<void> {

Expand Down Expand Up @@ -71,7 +71,7 @@ export async function testSmartContract(chaincode?: InstantiatedContractTreeItem
console.log('testSmartContractCommand: chaincode to generate tests for is: ' + chaincodeLabel);

// Get metadata
const connection: IFabricConnection = FabricConnectionManager.instance().getConnection();
const connection: IFabricClientConnection = FabricConnectionManager.instance().getConnection();

const transactions: Map<string, any[]> = await MetadataUtil.getTransactions(connection, chaincodeName, channelName, true);
if (transactions.size === 0) {
Expand Down
Loading

0 comments on commit 2ad5f76

Please sign in to comment.