Skip to content

Commit

Permalink
feat: 🎸 New debug mode to determine contract info
Browse files Browse the repository at this point in the history
Introduced a new debug mode that can help to troubleshoot the matching
of contracts to ABI definition. When passing `--debug-contract-info
<address>`, ethlogger will perform the contract info lookup against the
given contract address and output verbose log information of the
process. This can help spot differences between ABI definitions and
deployed contracts.
  • Loading branch information
ziegfried committed May 22, 2020
1 parent 3b14bbb commit 465efdd
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/abi/files.ts
Expand Up @@ -92,7 +92,9 @@ export function parseAbiFileContents(
let abis: AbiItem[];
let contractName: string;
let contractAddress: string | undefined;
debug('Parsing contents of ABI file %s', fileName);
if (isTruffleBuildFile(abiData)) {
debug('ABI file contains truffle build output');
abis = abiData.abi;
contractName =
abiData.contractName ||
Expand Down
6 changes: 3 additions & 3 deletions src/abi/repo.ts
Expand Up @@ -147,12 +147,12 @@ export class AbiRepository implements ManagedResource {
return candidates != null ? { anonymous: true, candidates } : undefined;
}

public getMatchingSignatureName(signatureHash: string): string | undefined {
public getMatchingSignature(signatureHash: string): string | undefined {
const candidates = this.signatures.get(signatureHash);
trace(
'getMatchingSignatureName(%o) --> ',
'getMatchingSignature(%o) --> ',
signatureHash,
candidates?.map(c => computeSignature(c))
candidates?.filter(c => c.contractFingerprint != null)?.map(c => computeSignature(c))
);
if (candidates != null) {
// We only want to consider signatures from contracts in our repo
Expand Down
2 changes: 1 addition & 1 deletion src/blockwatcher.ts
Expand Up @@ -353,7 +353,7 @@ export class BlockWatcher implements ManagedResource {
getContractInfo(
addr,
this.ethClient,
(sig: string) => abiRepo.getMatchingSignatureName(sig),
(sig: string) => abiRepo.getMatchingSignature(sig),
(address: string, fingerprint: string) =>
abiRepo.getContractByAddress(address)?.contractName ??
abiRepo.getContractByFingerprint(fingerprint)?.contractName
Expand Down
6 changes: 6 additions & 0 deletions src/cliflags.ts
Expand Up @@ -129,6 +129,12 @@ export const CLI_FLAGS = {
description: 'Directory containing ABI definitions (JSON files). This directory will be searched recursively',
}),

'debug-contract-info': flags.string({
hidden: true,
helpValue: '<contract-address>',
description: 'Tool for troubleshooting the machting of ABIs to contracts',
}),

'start-at-block': flags.option<StartBlock>({
env: 'START_AT_BLOCK',
multiple: false,
Expand Down
25 changes: 24 additions & 1 deletion src/index.ts
@@ -1,7 +1,7 @@
import { Command } from '@oclif/command';
import debugModule from 'debug';
import { inspect } from 'util';
import { ContractInfo } from './abi/contract';
import { ContractInfo, getContractInfo } from './abi/contract';
import { AbiRepository } from './abi/repo';
import { BlockWatcher } from './blockwatcher';
import { Checkpoint } from './checkpoint';
Expand Down Expand Up @@ -61,6 +61,29 @@ class Ethlogger extends Command {
return;
}
const config = await loadEthloggerConfig(flags);

if (flags['debug-contract-info'] != null) {
const addr = flags['debug-contract-info'];
info(`Determining info for contract at address=%s`, addr);
enableTraceLogging('ethlogger:abi:*');
const abiRepo = new AbiRepository(config.abi);
await abiRepo.initialize();
const transport = new HttpTransport(config.eth.url, config.eth.http);
const client = new EthereumClient(transport);

const contractInfo = await getContractInfo(
addr,
client,
(sig: string) => abiRepo.getMatchingSignature(sig),
(address: string, fingerprint: string) =>
abiRepo.getContractByAddress(address)?.contractName ??
abiRepo.getContractByFingerprint(fingerprint)?.contractName
);

info('Contract info: %O', contractInfo);
return;
}

const health = new HealthStateMonitor();
health.start();
this.resources.push(health);
Expand Down
5 changes: 4 additions & 1 deletion src/utils/debug.ts
Expand Up @@ -21,8 +21,11 @@ export const createModuleDebug = (name: string) => {
return { debug, info, warn, error, trace };
};

export function enableTraceLogging() {
export function enableTraceLogging(modulePattern?: string) {
TRACE_ENABLED = true;
if (modulePattern != null) {
create.enable(`${modulePattern},ethlogger:*:info,ethlogger:*:warn,ethlogger:*:error`);
}
}

// disable debug logging for tests
Expand Down
2 changes: 1 addition & 1 deletion test/abi/contract.test.ts
Expand Up @@ -13,7 +13,7 @@ test('extractFunctionsAndEvents', async () => {
await abis.loadAbiFile(join(__dirname, '../abis/BCB.json'), config);
const fne = extractFunctionsAndEvents(
await readFile(join(__dirname, '../fixtures/contract1.txt'), { encoding: 'utf-8' }),
(sig: string) => abis.getMatchingSignatureName(sig)
(sig: string) => abis.getMatchingSignature(sig)
);
expect(fne).toMatchInlineSnapshot(`
Object {
Expand Down

0 comments on commit 465efdd

Please sign in to comment.