-
Notifications
You must be signed in to change notification settings - Fork 405
/
Copy pathethereum-connector.js
480 lines (422 loc) · 18.3 KB
/
ethereum-connector.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const EthereumHDKey = require('ethereumjs-wallet/hdkey');
const Web3 = require('web3');
const EEAClient = require('web3-eea');
const {ConnectorBase, CaliperUtils, ConfigUtil, TxStatus} = require('@hyperledger/caliper-core');
const logger = CaliperUtils.getLogger('ethereum-connector');
/**
* @typedef {Object} EthereumInvoke
*
* @property {string} contract Required. The name of the smart contract
* @property {string} verb Required. The name of the smart contract function
* @property {string} args Required. Arguments of the smart contract function in the order in which they are defined
* @property {boolean} readOnly Optional. If method to call is a view.
*/
/**
* Extends {BlockchainConnector} for a web3 Ethereum backend.
*/
class EthereumConnector extends ConnectorBase {
/**
* Create a new instance of the {Ethereum} class.
* @param {number} workerIndex The zero-based index of the worker who wants to create an adapter instance. -1 for the manager process.
* @param {string} bcType The target SUT type
*/
constructor(workerIndex, bcType) {
super(workerIndex, bcType);
let configPath = CaliperUtils.resolvePath(ConfigUtil.get(ConfigUtil.keys.NetworkConfig));
let ethereumConfig = require(configPath).ethereum;
// throws on configuration error
this.checkConfig(ethereumConfig);
this.ethereumConfig = ethereumConfig;
this.web3 = new Web3(this.ethereumConfig.url);
if (this.ethereumConfig.privacy) {
this.web3eea = new EEAClient(this.web3, ethereumConfig.chainId);
}
this.web3.transactionConfirmationBlocks = this.ethereumConfig.transactionConfirmationBlocks;
this.workerIndex = workerIndex;
this.context = undefined;
}
/**
* Check the ethereum networkconfig file for errors, throw if invalid
* @param {object} ethereumConfig The ethereum networkconfig to check.
*/
checkConfig(ethereumConfig) {
if (!ethereumConfig.url) {
throw new Error(
'No URL given to access the Ethereum SUT. Please check your network configuration.'
);
}
if (ethereumConfig.url.toLowerCase().indexOf('http') === 0) {
throw new Error(
'Ethereum benchmarks must not use http(s) RPC connections, as there is no way to guarantee the ' +
'order of submitted transactions when using other transports. For more information, please see ' +
'https://github.com/hyperledger-caliper/caliper/issues/776#issuecomment-624771622'
);
}
//TODO: add validation logic for the rest of the configuration object
}
/**
* Initialize the {Ethereum} object.
* @param {boolean} workerInit Indicates whether the initialization happens in the worker process.
* @return {object} Promise<boolean> True if the account got unlocked successful otherwise false.
*/
init(workerInit) {
if (this.ethereumConfig.contractDeployerAddressPrivateKey) {
this.web3.eth.accounts.wallet.add(this.ethereumConfig.contractDeployerAddressPrivateKey);
} else if (this.ethereumConfig.contractDeployerAddressPassword) {
return this.web3.eth.personal.unlockAccount(this.ethereumConfig.contractDeployerAddress, this.ethereumConfig.contractDeployerAddressPassword, 1000);
}
}
/**
* Deploy smart contracts specified in the network configuration file.
* @return {object} Promise execution for all the contract creations.
*/
async installSmartContract() {
let promises = [];
let self = this;
logger.info('Creating contracts...');
for (const key of Object.keys(this.ethereumConfig.contracts)) {
const contract = this.ethereumConfig.contracts[key];
const contractData = require(CaliperUtils.resolvePath(contract.path)); // TODO remove path property
const contractGas = contract.gas;
const estimateGas = contract.estimateGas;
let privacy;
if (this.ethereumConfig.privacy) {
privacy = this.ethereumConfig.privacy[contract.private];
}
this.ethereumConfig.contracts[key].abi = contractData.abi;
promises.push(new Promise(async function(resolve, reject) {
let contractInstance;
try {
if (privacy) {
contractInstance = await self.deployPrivateContract(contractData, privacy);
logger.info(`Deployed private contract ${contractData.name} at ${contractInstance.options.address}`);
} else {
contractInstance = await self.deployContract(contractData);
logger.info(`Deployed contract ${contractData.name} at ${contractInstance.options.address}`);
}
} catch (err) {
reject(err);
}
self.ethereumConfig.contracts[key].address = contractInstance.options.address;
self.ethereumConfig.contracts[key].gas = contractGas;
self.ethereumConfig.contracts[key].estimateGas = estimateGas;
resolve(contractInstance);
}));
}
return Promise.all(promises);
}
/**
* Return the Ethereum context associated with the given callback module name.
* @param {Number} roundIndex The zero-based round index of the test.
* @param {object} args worker arguments.
* @return {object} The assembled Ethereum context.
* @async
*/
async getContext(roundIndex, args) {
let context = {
chainId: 1,
clientIndex: this.workerIndex,
gasPrice: 0,
contracts: {},
nonces: {},
web3: this.web3
};
context.gasPrice = this.ethereumConfig.gasPrice !== undefined
? this.ethereumConfig.gasPrice
: await this.web3.eth.getGasPrice();
context.chainId = this.ethereumConfig.chainId !== undefined
? this.ethereumConfig.chainId
: await this.web3.eth.getChainId();
for (const key of Object.keys(args.contracts)) {
context.contracts[key] = {
contract: new this.web3.eth.Contract(args.contracts[key].abi, args.contracts[key].address),
gas: args.contracts[key].gas,
estimateGas: args.contracts[key].estimateGas
};
}
if (this.ethereumConfig.fromAddress) {
context.fromAddress = this.ethereumConfig.fromAddress;
}
if (this.ethereumConfig.contractDeployerAddress) {
context.contractDeployerAddress = this.ethereumConfig.contractDeployerAddress;
context.contractDeployerAddressPrivateKey = this.ethereumConfig.contractDeployerAddressPrivateKey;
}
if (this.ethereumConfig.fromAddressSeed) {
let hdwallet = EthereumHDKey.fromMasterSeed(this.ethereumConfig.fromAddressSeed);
let wallet = hdwallet.derivePath('m/44\'/60\'/' + this.workerIndex + '\'/0/0').getWallet();
context.fromAddress = wallet.getChecksumAddressString();
context.nonces[context.fromAddress] = await this.web3.eth.getTransactionCount(context.fromAddress);
this.web3.eth.accounts.wallet.add(wallet.getPrivateKeyString());
} else if (this.ethereumConfig.fromAddressPrivateKey) {
context.nonces[this.ethereumConfig.fromAddress] = await this.web3.eth.getTransactionCount(this.ethereumConfig.fromAddress);
this.web3.eth.accounts.wallet.add(this.ethereumConfig.fromAddressPrivateKey);
} else if (this.ethereumConfig.fromAddressPassword) {
await context.web3.eth.personal.unlockAccount(this.ethereumConfig.fromAddress, this.ethereumConfig.fromAddressPassword, 1000);
}
if (this.ethereumConfig.privacy) {
context.web3eea = this.web3eea;
context.privacy = this.ethereumConfig.privacy;
}
this.context = context;
return context;
}
/**
* Release the given Ethereum context.
* @async
*/
async releaseContext() {
// nothing to do
}
/**
* Submit a transaction to the ethereum context.
* @param {EthereumInvoke} request Methods call data.
* @return {Promise<TxStatus>} Result and stats of the transaction invocation.
*/
async _sendSingleRequest(request) {
if (request.privacy) {
return this._sendSinglePrivateRequest(request);
}
const context = this.context;
let status = new TxStatus();
let params = {from: context.fromAddress};
if (request.hasOwnProperty('value') && request.value > 0) {
params.value = request.value;
}
let contractInfo = context.contracts[request.contract];
let receipt = null;
let methodType = 'send';
if (request.readOnly) {
methodType = 'call';
} else if (context.nonces && (typeof context.nonces[context.fromAddress] !== 'undefined')) {
let nonce = context.nonces[context.fromAddress];
context.nonces[context.fromAddress] = nonce + 1;
params.nonce = nonce;
// leaving these values unset causes web3 to fetch gasPrice and
// chainId on the fly. This can cause transactions to be
// reordered, which in turn causes nonce failures
params.gasPrice = context.gasPrice;
params.chainId = context.chainId;
}
const onFailure = (err) => {
status.SetStatusFail();
logger.error(`Failed tx on ${request.contract}; calling method: ${request.verb}; nonce: ${params.nonce}`);
logger.error(err);
};
const onSuccess = (rec) => {
status.SetID(rec.transactionHash);
status.SetResult(rec);
status.SetVerification(true);
status.SetStatusSuccess();
};
if (request.args) {
if (contractInfo.gas && contractInfo.gas[request.verb]) {
params.gas = contractInfo.gas[request.verb];
} else if (contractInfo.estimateGas) {
params.gas = 1000 + await contractInfo.contract.methods[request.verb](...request.args).estimateGas();
}
try {
receipt = await contractInfo.contract.methods[request.verb](...request.args)[methodType](params);
onSuccess(receipt);
} catch (err) {
onFailure(err);
}
} else {
if (contractInfo.gas && contractInfo.gas[request.verb]) {
params.gas = contractInfo.gas[request.verb];
} else if (contractInfo.estimateGas) {
params.gas = 1000 + await contractInfo.contract.methods[request.verb].estimateGas(params);
}
try {
receipt = await contractInfo.contract.methods[request.verb]()[methodType](params);
onSuccess(receipt);
} catch (err) {
onFailure(err);
}
}
return status;
}
/**
* Submit a private transaction to the ethereum context.
* @param {EthereumInvoke} request Methods call data.
* @return {Promise<TxStatus>} Result and stats of the transaction invocation.
*/
async _sendSinglePrivateRequest(request) {
const context = this.context;
const web3eea = context.web3eea;
const contractInfo = context.contracts[request.contract];
const privacy = request.privacy;
const sender = privacy.sender;
const status = new TxStatus();
const onFailure = (err) => {
status.SetStatusFail();
logger.error(`Failed private tx on ${request.contract}; calling method: ${request.verb}; private nonce: ` + 0);
logger.error(err);
};
const onSuccess = (rec) => {
status.SetID(rec.transactionHash);
status.SetResult(rec);
status.SetVerification(true);
status.SetStatusSuccess();
};
let payload;
if (request.args) {
payload = contractInfo.contract.methods[request.verb](...request.args).encodeABI();
} else {
payload = contractInfo.contract.methods[request.verb]().encodeABI();
}
const transaction = {
to: contractInfo.contract._address,
data: payload
};
try {
if (request.readOnly) {
transaction.privacyGroupId = await this.resolvePrivacyGroup(privacy);
const value = await web3eea.priv.call(transaction);
onSuccess(value);
} else {
transaction.nonce = sender.nonce;
transaction.privateKey = sender.privateKey.substring(2);
this.setPrivateTransactionParticipants(transaction, privacy);
const txHash = await web3eea.eea.sendRawTransaction(transaction);
const rcpt = await web3eea.priv.getTransactionReceipt(txHash, transaction.privateFrom);
if (rcpt.status === '0x1') {
onSuccess(rcpt);
} else {
onFailure(rcpt);
}
}
} catch(err) {
onFailure(err);
}
return status;
}
/**
* Deploys a new contract using the given web3 instance
* @param {JSON} contractData Contract data with abi, bytecode and gas properties
* @returns {Promise<web3.eth.Contract>} The deployed contract instance
*/
async deployContract(contractData) {
const web3 = this.web3;
const contractDeployerAddress = this.ethereumConfig.contractDeployerAddress;
const contract = new web3.eth.Contract(contractData.abi);
const contractDeploy = contract.deploy({
data: contractData.bytecode
});
try {
return contractDeploy.send({
from: contractDeployerAddress,
gas: contractData.gas
});
} catch (err) {
throw(err);
}
}
/**
* Deploys a new contract using the given web3 instance
* @param {JSON} contractData Contract data with abi, bytecode and gas properties
* @param {JSON} privacy Privacy options
* @returns {Promise<web3.eth.Contract>} The deployed contract instance
*/
async deployPrivateContract(contractData, privacy) {
const web3 = this.web3;
const web3eea = this.web3eea;
// Using randomly generated account to deploy private contract to avoid public/private nonce issues
const deployerAccount = web3.eth.accounts.create();
const transaction = {
data: contractData.bytecode,
nonce: deployerAccount.nonce,
privateKey: deployerAccount.privateKey.substring(2), // web3js-eea doesn't not accept private keys prefixed by '0x'
};
this.setPrivateTransactionParticipants(transaction, privacy);
try {
const txHash = await web3eea.eea.sendRawTransaction(transaction);
const txRcpt = await web3eea.priv.getTransactionReceipt(txHash, transaction.privateFrom);
if (txRcpt.status === '0x1') {
return new web3.eth.Contract(contractData.abi, txRcpt.contractAddress);
} else {
const msg = `Failed private transaction hash ${txHash}`;
logger.error(msg);
throw new Error(msg);
}
} catch (err) {
logger.error('Error deploying private contract: ', JSON.stringify(err));
throw(err);
}
}
/**
* It passes deployed contracts addresses to all workers (only known after deploy contract)
* @param {Number} number of workers to prepare
* @returns {Array} worker args
* @async
*/
async prepareWorkerArguments(number) {
let result = [];
for (let i = 0 ; i<= number ; i++) {
result[i] = {contracts: this.ethereumConfig.contracts};
}
return result;
}
/**
* Returns the privacy group id depending on the privacy mode being used
* @param {JSON} privacy Privacy options
* @returns {Promise<string>} The privacyGroupId
*/
async resolvePrivacyGroup(privacy) {
const web3eea = this.context.web3eea;
switch(privacy.groupType) {
case 'legacy': {
const privGroups = await web3eea.priv.findPrivacyGroup({addresses: [privacy.privateFrom, ...privacy.privateFor]});
if (privGroups.length > 0) {
return privGroups.filter(function(el) {
return el.type === 'LEGACY';
})[0].privacyGroupId;
} else {
throw new Error('There are multiple legacy privacy groups with same members. Can\'t resolve privacyGroupId.');
}
}
case 'pantheon':
case 'onchain': {
return privacy.privacyGroupId;
} default: {
throw new Error('Invalid privacy type');
}
}
}
/**
* Set the participants of a privacy transaction depending on the privacy mode being used
* @param {JSON} transaction Object representing the transaction fields
* @param {JSON} privacy Privacy options
*/
setPrivateTransactionParticipants(transaction, privacy) {
switch(privacy.groupType) {
case 'legacy': {
transaction.privateFrom = privacy.privateFrom;
transaction.privateFor = privacy.privateFor;
break;
}
case 'pantheon':
case 'onchain': {
transaction.privateFrom = privacy.privateFrom;
transaction.privacyGroupId = privacy.privacyGroupId;
break;
} default: {
throw new Error('Invalid privacy type');
}
}
}
}
module.exports = EthereumConnector;