-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetBlockchainLogs.js
88 lines (62 loc) · 2.63 KB
/
getBlockchainLogs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//TODO: Automate these steps.
/*
Execute the following steps before running this script:
1. Create/Copy/Edit the connectionprofile.yaml to match the Fabric network configuration. For example:
cp /home/ubuntu/HyperLedgerLab-2.0_Extended/caliper/caliper-config/templates/networkConfig.yaml /home/ubuntu/BlockOptR/log_extraction/connectionprofile.yaml
2. Edit connectionprofile.yaml such that there is only one client as shown below:
client:
organization: Org1
credentialStore:
path: /tmp/1615470564-cred/org1
cryptoStore:
path: /tmp/1615470564-crypto/org1
clientPrivateKey:
path: /home/ubuntu/HyperLedgerLab-2.0_Extended/inventory/blockchain/fabric-config/crypto-config/peerOrganizations/org1/users/User1@org1/msp/keystore/priv_sk
clientSignedCert:
path: /home/ubuntu/HyperLedgerLab-2.0_Extended/inventory/blockchain/fabric-config/crypto-config/peerOrganizations/org1/users/User1@org1/msp/signcerts/[email protected]
3. mkdir data if it does not exist. rm data/* if it exists.
*/
'use strict';
const fs = require('fs');
const FabricClient = require('fabric-client');
async function setClient() {
let client = FabricClient.loadFromConfig('./log_extraction/connectionprofile.yaml')
await client.initCredentialStores()
.then(async (nothing) => {
await client.setUserContext({username:'admin', password:'adminpw'})
.then(async (admin) => {
const channel = client.getChannel();
let blockchaininfo = await channel.queryInfo();
let blockchainheight = blockchaininfo.height;
blockchainheight = blockchainheight|0;
//The complete blockchain is parsed
for (let index = 0; index < blockchainheight; index++) {
var fileName = "./log_extraction/data/" + index + ".json";
var jsonstr = "";
try {
//Blocks are queried from the blockchain
jsonstr = JSON.stringify((await channel.queryBlock(index)), null, 4)
}
catch(e) {
console.log("CAUGHT JSON LENGTH EXCEPTION")
}
//Blocks are written to the filesystem
fs.writeFile(
fileName,
jsonstr,
function (err) {
if (err) {
console.error('Saving BLOCK failed');
}
}
);
}
return channel;
})
})
}
setClient()
.then((channel) => {
console.log('Client setup successful')
})
.then(() => { console.log('Client setup complete')});