|
|
|
@ -12,16 +12,15 @@ var helper = require('../../../../lib/helper.js') |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
class Recorder { |
|
|
|
|
constructor (executionContext, udapp, fileManager, config) { |
|
|
|
|
constructor (blockchain, fileManager, config) { |
|
|
|
|
var self = this |
|
|
|
|
self.event = new EventManager() |
|
|
|
|
self.executionContext = executionContext |
|
|
|
|
self.blockchain = blockchain |
|
|
|
|
self.data = { _listen: true, _replay: false, journal: [], _createdContracts: {}, _createdContractsReverse: {}, _usedAccounts: {}, _abis: {}, _contractABIReferences: {}, _linkReferences: {} } |
|
|
|
|
this.udapp = udapp |
|
|
|
|
this.fileManager = fileManager |
|
|
|
|
this.config = config |
|
|
|
|
|
|
|
|
|
this.udapp.event.register('initiatingTransaction', (timestamp, tx, payLoad) => { |
|
|
|
|
this.blockchain.event.register('initiatingTransaction', (timestamp, tx, payLoad) => { |
|
|
|
|
if (tx.useCall) return |
|
|
|
|
var { from, to, value } = tx |
|
|
|
|
|
|
|
|
@ -59,7 +58,7 @@ class Recorder { |
|
|
|
|
if (thistimestamp) record.parameters[p] = `created{${thistimestamp}}` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.udapp.getAccounts((error, accounts) => { |
|
|
|
|
this.blockchain.getAccounts((error, accounts) => { |
|
|
|
|
if (error) return console.log(error) |
|
|
|
|
record.from = `account{${accounts.indexOf(from)}}` |
|
|
|
|
self.data._usedAccounts[record.from] = from |
|
|
|
@ -68,11 +67,11 @@ class Recorder { |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
this.udapp.event.register('transactionExecuted', (error, from, to, data, call, txResult, timestamp) => { |
|
|
|
|
this.blockchain.event.register('transactionExecuted', (error, from, to, data, call, txResult, timestamp) => { |
|
|
|
|
if (error) return console.log(error) |
|
|
|
|
if (call) return |
|
|
|
|
|
|
|
|
|
const rawAddress = this.executionContext.isVM() ? txResult.result.createdAddress : txResult.result.contractAddress |
|
|
|
|
const rawAddress = this.blockchain.getAddressFromTransactionResult(txResult) |
|
|
|
|
if (!rawAddress) return // not a contract creation
|
|
|
|
|
const stringAddress = this.addressToString(rawAddress) |
|
|
|
|
const address = ethutil.toChecksumAddress(stringAddress) |
|
|
|
@ -80,7 +79,7 @@ class Recorder { |
|
|
|
|
this.data._createdContracts[address] = timestamp |
|
|
|
|
this.data._createdContractsReverse[timestamp] = address |
|
|
|
|
}) |
|
|
|
|
this.executionContext.event.register('contextChanged', this.clearAll.bind(this)) |
|
|
|
|
this.blockchain.event.register('contextChanged', this.clearAll.bind(this)) |
|
|
|
|
this.event.register('newTxRecorded', (count) => { |
|
|
|
|
this.event.trigger('recorderCountChange', [count]) |
|
|
|
|
}) |
|
|
|
@ -183,7 +182,6 @@ class Recorder { |
|
|
|
|
* @param {Object} accounts |
|
|
|
|
* @param {Object} options |
|
|
|
|
* @param {Object} abis |
|
|
|
|
* @param {Object} udapp |
|
|
|
|
* @param {Function} newContractFn |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
@ -195,8 +193,7 @@ class Recorder { |
|
|
|
|
var record = self.resolveAddress(tx.record, accounts, options) |
|
|
|
|
var abi = abis[tx.record.abi] |
|
|
|
|
if (!abi) { |
|
|
|
|
alertCb('cannot find ABI for ' + tx.record.abi + '. Execution stopped at ' + index) |
|
|
|
|
return |
|
|
|
|
return alertCb('cannot find ABI for ' + tx.record.abi + '. Execution stopped at ' + index) |
|
|
|
|
} |
|
|
|
|
/* Resolve Library */ |
|
|
|
|
if (record.linkReferences && Object.keys(record.linkReferences).length) { |
|
|
|
@ -220,8 +217,7 @@ class Recorder { |
|
|
|
|
} |
|
|
|
|
if (!fnABI) { |
|
|
|
|
alertCb('cannot resolve abi of ' + JSON.stringify(record, null, '\t') + '. Execution stopped at ' + index) |
|
|
|
|
cb('cannot resolve abi') |
|
|
|
|
return |
|
|
|
|
return cb('cannot resolve abi') |
|
|
|
|
} |
|
|
|
|
if (tx.record.parameters) { |
|
|
|
|
/* check if we have some params to resolve */ |
|
|
|
@ -239,27 +235,25 @@ class Recorder { |
|
|
|
|
tx.record.parameters[index] = value |
|
|
|
|
}) |
|
|
|
|
} catch (e) { |
|
|
|
|
alertCb('cannot resolve input parameters ' + JSON.stringify(tx.record.parameters) + '. Execution stopped at ' + index) |
|
|
|
|
return |
|
|
|
|
return alertCb('cannot resolve input parameters ' + JSON.stringify(tx.record.parameters) + '. Execution stopped at ' + index) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
var data = format.encodeData(fnABI, tx.record.parameters, tx.record.bytecode) |
|
|
|
|
if (data.error) { |
|
|
|
|
alertCb(data.error + '. Record:' + JSON.stringify(record, null, '\t') + '. Execution stopped at ' + index) |
|
|
|
|
cb(data.error) |
|
|
|
|
return |
|
|
|
|
} else { |
|
|
|
|
return cb(data.error) |
|
|
|
|
} |
|
|
|
|
logCallBack(`(${index}) ${JSON.stringify(record, null, '\t')}`) |
|
|
|
|
logCallBack(`(${index}) data: ${data.data}`) |
|
|
|
|
record.data = { dataHex: data.data, funArgs: tx.record.parameters, funAbi: fnABI, contractBytecode: tx.record.bytecode, contractName: tx.record.contractName, timestamp: tx.timestamp } |
|
|
|
|
} |
|
|
|
|
self.udapp.runTx(record, confirmationCb, continueCb, promptCb, |
|
|
|
|
|
|
|
|
|
self.blockchain.runTransaction (record, continueCb, promptCb, confirmationCb, |
|
|
|
|
function (err, txResult) { |
|
|
|
|
if (err) { |
|
|
|
|
console.error(err) |
|
|
|
|
logCallBack(err + '. Execution failed at ' + index) |
|
|
|
|
} else { |
|
|
|
|
const rawAddress = self.executionContext.isVM() ? txResult.result.createdAddress : txResult.result.contractAddress |
|
|
|
|
return logCallBack(err + '. Execution failed at ' + index) |
|
|
|
|
} |
|
|
|
|
const rawAddress = self.blockchain.getAddressFromTransactionResult(txResult) |
|
|
|
|
if (rawAddress) { |
|
|
|
|
const stringAddress = self.addressToString(rawAddress) |
|
|
|
|
const address = ethutil.toChecksumAddress(stringAddress) |
|
|
|
@ -268,7 +262,6 @@ class Recorder { |
|
|
|
|
self.data._createdContractsReverse[tx.timestamp] = address |
|
|
|
|
newContractFn(abi, address, record.contractName) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
cb(err) |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|