fix(@embark/transaction-logger): handle non-contract transactions

The transaction logger takes care of intercepting any responses coming from the
blockchain proxy, determines what response it's dealing with and then extracts
information from the response to output useful log information to the user, such as
when a contract methods has been called etc.

It turned out that it didn't properly handle cases where value transactions
from account to account (such as done here in [Status Teller](a5ab4d4b26/embarkConfig/data.js (L100-L110))) have been made.

Since Embark couldn't map any of those accounts to actual contracts, while
still having stored corresponding transactions, it logged them as transactions
from "Unknown Smart Contracts".

This commit ensures via heuristics that, if the address a transactions is sent to is
included in any of the nodes accounts, the transaction logger logs a useful message
that a certain value is sent from one to anover account.
This commit is contained in:
Pascal Precht 2019-12-12 11:34:50 +01:00 committed by Iuri Matias
parent b630a71f7f
commit 1a7fc663b7
1 changed files with 24 additions and 3 deletions

View File

@ -36,11 +36,12 @@ class TransactionLogger {
this.events.on('outputDone', () => { this.events.on('outputDone', () => {
this.outputDone = true; this.outputDone = true;
}); });
this.events.on("contractsDeployed", () => {
this.contractsDeployed = true;
this.embark.registerActionForEvent("deployment:deployContracts:afterAll", { priority: 38 }, (params, cb) => {
this.contractsDeployed = true;
this._getContractsList((contractsList) => { this._getContractsList((contractsList) => {
this.addressToContract = getAddressToContract(contractsList, this.addressToContract); this.addressToContract = getAddressToContract(contractsList, this.addressToContract);
cb(null, params);
}); });
}); });
@ -70,6 +71,16 @@ class TransactionLogger {
})(); })();
} }
get web3Accounts() {
return (async () => {
if (!this._web3Accounts) {
const web3 = await this.web3;
this._web3Accounts = await web3.eth.getAccounts();
}
return this._web3Accounts;
})();
}
_getContractsList(callback) { _getContractsList(callback) {
this.events.request("contracts:list", (err, contractsList) => { this.events.request("contracts:list", (err, contractsList) => {
if (err) { if (err) {
@ -98,6 +109,7 @@ class TransactionLogger {
} }
async _onLogRequest(args) { async _onLogRequest(args) {
/*eslint complexity: ["error", 22]*/
const method = args.request.method; const method = args.request.method;
if (!this.contractsDeployed || !LISTENED_METHODS.includes(method)) { if (!this.contractsDeployed || !LISTENED_METHODS.includes(method)) {
return; return;
@ -108,6 +120,7 @@ class TransactionLogger {
this.transactions[args.response.result] = { this.transactions[args.response.result] = {
address: args.request.params[0].to, address: args.request.params[0].to,
data: args.request.params[0].data, data: args.request.params[0].data,
value: args.request.params[0].value,
txHash: args.response.result txHash: args.response.result
}; };
return; return;
@ -143,8 +156,16 @@ class TransactionLogger {
// It's a deployment // It's a deployment
return; return;
} }
const contract = this.addressToContract[address];
const accounts = await this.web3Accounts;
if (accounts.map(account => account.toLowerCase()).includes(address.toLowerCase())) {
const web3 = await this.web3;
const value = web3.utils.fromWei(web3.utils.hexToNumberString(dataObject.value));
return this.logger.info(`Blockchain>`.underline + ` transferring ${value} ETH from ${dataObject.from} to ${address}`.bold);
}
const contract = this.addressToContract[address];
if (!contract) { if (!contract) {
this.logger.info(`Contract log for unknown contract: ${JSON.stringify(args)}`); this.logger.info(`Contract log for unknown contract: ${JSON.stringify(args)}`);
return this._getContractsList((contractsList) => { return this._getContractsList((contractsList) => {