Skip to content

Commit

Permalink
[WIP] diagnose.js
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkhala committed May 5, 2024
1 parent b0fc0e7 commit 537b0be
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 345 deletions.
18 changes: 11 additions & 7 deletions app/chaincodeOperator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'assert';
import {consoleLogger} from '@davidkhala/logger/log4.js';
import {install, getEndorsePolicy, getCollectionConfig} from './chaincodeHelper.js';
import ChaincodeAction from '../common/nodejs/chaincodeOperation.js';
import ChaincodeLifecycleOperation from '../common/nodejs/chaincodeOperation.js';
import {parsePackageID} from '../common/nodejs/formatter/chaincode.js';
import * as helper from './helper.js';
import QueryHub from '../common/nodejs/query.js';
Expand All @@ -22,9 +22,13 @@ export class ChaincodeDefinitionOperator {
constructor(channelName, admin, peers, init_required) {
const channel = helper.prepareChannel(channelName);
this.waitForConsensus = 1000;
const chaincodeAction = new ChaincodeAction(peers, admin, channel, logger);
this.init_required = init_required;
this.chaincodeAction = new ChaincodeLifecycleOperation(peers, admin, channel, logger);
this.forceUpgrade = true;
Object.assign(this, {chaincodeAction, peers, admin, channel, channelName});
this.peers = peers;
this.admin = admin;
this.channel = channel;
this.channelName = channelName;

}

Expand All @@ -44,7 +48,7 @@ export class ChaincodeDefinitionOperator {
}

async approves({sequence, package_id}, orderer, gate) {
const {waitForConsensus, chaincodeAction} = this;
const {waitForConsensus, chaincodeAction, init_required} = this;

await orderer.connect();

Expand All @@ -58,7 +62,7 @@ export class ChaincodeDefinitionOperator {
chaincodeAction.setEndorsementPolicy(endorsementPolicy);
chaincodeAction.setCollectionsConfig(getCollectionConfig(name));
try {
await chaincodeAction.approve({name, package_id, sequence}, orderer, waitForConsensus);
await chaincodeAction.approve({name, package_id, sequence, init_required}, orderer, waitForConsensus);
} finally {
orderer.disconnect();
}
Expand All @@ -72,14 +76,14 @@ export class ChaincodeDefinitionOperator {
* @param [gate]
*/
async commitChaincodeDefinition({sequence, name}, orderer, gate) {
const {chaincodeAction} = this;
const {chaincodeAction, init_required} = this;
await orderer.connect();
const endorsementPolicy = {gate};
Object.assign(endorsementPolicy, getEndorsePolicy(name));
chaincodeAction.setEndorsementPolicy(endorsementPolicy);
chaincodeAction.setCollectionsConfig(getCollectionConfig(name));
try {
await chaincodeAction.commitChaincodeDefinition({name, sequence}, orderer);
await chaincodeAction.commitChaincodeDefinition({name, sequence, init_required}, orderer);
} finally {
orderer.disconnect();
}
Expand Down
17 changes: 10 additions & 7 deletions app/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {randomKeyOf} from '@davidkhala/light/random.js';
import {getClientKeyPairPath} from '../config/caCryptoGen.js';
import {importFrom, filedirname} from '@davidkhala/light/es6.mjs';

const globalConfig = importFrom(import.meta, '../config/orgs.json', );
const globalConfig = importFrom(import.meta, '../config/orgs.json',);
const logger = consoleLogger('Helper');
const orgsConfig = globalConfig.organizations;
const channelsConfig = globalConfig.channels;
Expand All @@ -22,20 +22,22 @@ export const projectResolve = (...args) => path.resolve(projectRoot, ...args);

const CRYPTO_CONFIG_DIR = homeResolve(globalConfig.docker.volumes.MSPROOT);

export function orgNamesOfChannel(channelName){
return Object.keys(globalConfig.channels[channelName].organizations)
export function orgNamesOfChannel(channelName) {
return Object.keys(globalConfig.channels[channelName].organizations);
}

const preparePeer = (orgName, peerIndex, peerConfig) => {
const {port: peerPort} = peerConfig;

let peer;
const logger = consoleLogger('peer');
const cryptoPath = new CryptoPath(CRYPTO_CONFIG_DIR,
{peer: {name: `peer${peerIndex}`, org: orgName}});
if (globalConfig.TLS) {
const {caCert} = cryptoPath.TLSFile('peer');
peer = new Peer({host: 'localhost', peerPort, cert: caCert});
peer = new Peer({host: 'localhost', peerPort, cert: caCert}, logger);
} else {
peer = new Peer({peerPort});
peer = new Peer({peerPort}, logger);
}

return peer;
Expand All @@ -51,6 +53,7 @@ export const newOrderer = (name, org, ordererSingleConfig) => {
}
});
let ordererWrapper;
const logger = consoleLogger('orderer');
if (globalConfig.TLS) {
const {caCert} = cryptoPath.TLSFile(nodeType);
const {clientKey, clientCert} = getClientKeyPairPath(cryptoPath, nodeType);
Expand All @@ -60,9 +63,9 @@ export const newOrderer = (name, org, ordererSingleConfig) => {
tlsCaCert: caCert,
clientKey,
clientCert,
});
}, undefined, logger);
} else {
ordererWrapper = new Orderer({ordererPort});
ordererWrapper = new Orderer({ordererPort}, undefined, logger);
}
ordererWrapper.adminAddress = `localhost:${portAdmin}`;
return ordererWrapper;
Expand Down
9 changes: 2 additions & 7 deletions cc/golang/contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as helper from '../../app/helper.js';
import FabricGateway from '../../common/nodejs/fabric-gateway/index.js';
import {consoleLogger} from '@davidkhala/logger/log4.js';
import UserBuilder from '../../common/nodejs/admin/user.js';
import {dev, installAndApprove, commit} from '../testutil.js';
import {dev, installAndApprove, commit, getContract} from '../testutil.js';

const chaincodeID = 'contracts';
const logger = consoleLogger(`chaincode:${chaincodeID}`);
Expand Down Expand Up @@ -34,12 +34,7 @@ describe('deploy', function () {
});
describe('invoke', function () {
this.timeout(0);
const peer = helper.newPeer(0, 'astri.org');
const org = 'icdd';
const user = new UserBuilder(undefined, helper.getOrgAdmin(org));

const gateway = new FabricGateway(peer, user);
const contract = gateway.getContract(channel, chaincodeID);
const contract = getContract(chaincodeID);

it('touch', async () => {
contract.subContract = 'StupidContract';
Expand Down
83 changes: 59 additions & 24 deletions cc/golang/diagnose/invoke.js → cc/golang/diagnose.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,85 @@
import {commit, dev, getContract, installAndApprove} from '../testutil.js';
import {consoleLogger} from '@davidkhala/logger/log4.js';
import * as helper from '../../app/helper.js';
import {ChaincodeDefinitionOperator} from '../../app/chaincodeOperator.js';
import assert from 'assert';
import {base64} from '@davidkhala/light/format.js';
import {consoleLogger} from '@davidkhala/logger/log4.js';
import {
DiagnoseInvoke
} from './diagnoseInvoke.js';
import {TxValidationCode} from '../../common/nodejs/formatter/constants.js';
import {getResponses} from '../../common/nodejs/formatter/proposalResponse.js';

const channelName = 'allchannel';
const clientOrg = 'icdd';
const peer = helper.newPeer(0, 'icdd');
const diagnose = new DiagnoseInvoke(channelName, clientOrg, peer);
import {getResponses} from '../../../common/nodejs/formatter/proposalResponse.js';
import {TxValidationCode} from '../../../common/nodejs/formatter/constants.js';
import * as helper from '../../../app/helper.js';
const logger = consoleLogger('chaincode:diagnose');
const chaincodeID = 'diagnose';
describe(`${chaincodeID} : green path`, function () {
this.timeout(0);
const orderers = helper.newOrderers();
const orderer = orderers[0];
it('validate', async () => {
const orgs = ['icdd', 'astri.org'];
for (const org of orgs) {
await dev(org, chaincodeID);
}
});
it('install & approve', async () => {
const orgs = ['icdd', 'astri.org'];
for (const org of orgs) {
await installAndApprove(org, chaincodeID, orderer, true);
}

});
it('commit', async () => {
const org = 'icdd';
await commit(org, chaincodeID, orderer, true);
});
});

describe('variant', async function () {
this.timeout(0);

// TODO const gate = `AND('icddMSP.member')`;
it('init', async () => {
const org = 'icdd';
const channel = 'allchannel';
const peers = helper.allPeers();
const admin = helper.getOrgAdmin(org);
const operator = new ChaincodeDefinitionOperator(channel, admin, peers);
await operator.connect();
await operator.init(chaincodeID);

operator.disconnect();
});
});

const logger = consoleLogger('chaincode:diagnose');
const org1 = 'icdd';
const org2 = 'astri.org';

describe('chaincode query', () => {
const contract = getContract(chaincodeID);
it('whom am i ', async () => {
const queryResult = await diagnose.whoami();
const queryResult = await contract.evaluate(['whoami']);
logger.info(queryResult);
const {MspID, Attrs, CertificatePem} = JSON.parse(queryResult);
assert.equal(MspID, 'icddMSP');
});

it('getCertID', async () => {
const result = await diagnose.getCertID();
logger.info('certID', result);
const queryResult = await contract.evaluate(['getCertID']);
assert.equal(queryResult, 'x509::CN=Admin,OU=client::CN=fabric-ca-server,OU=Fabric,O=Hyperledger,ST=North Carolina,C=US');
});
it('peerMSPID', async () => {
const result = await diagnose.peerMSPID();
logger.info(result);
const result = await contract.evaluate(['peerMSPID']);
assert.equal(result, 'icddMSP');
});
it('chaincode ping (google.com)', async () => {
const result = await diagnose.chaincodePing();
logger.info(result);
const result = await contract.evaluate(['external']);
assert.equal(result, '200 OK');
});
});
describe('chaincode invoke', function () {
this.timeout(0);
const contract = getContract(chaincodeID);
it('put & get raw value', async () => {
const value = Date.now().toString();
const key = 'a';
await diagnose.putRaw(key, value);
const queryResult = await diagnose.getRaw(key);
logger.info(queryResult);
await contract.submit(['putRaw', key, value]);
const queryResult = await contract.evaluate(['getRaw', key]);
assert.strictEqual(queryResult, value);
});

Expand Down
Loading

0 comments on commit 537b0be

Please sign in to comment.