Fixed import of ASTs

This commit is contained in:
Sebastian Mueller 2019-04-23 20:43:13 +02:00
parent 7a7fa49b90
commit 9f7a38053a
4 changed files with 70 additions and 70 deletions

View File

@ -25,7 +25,7 @@ module.exports = function(embark) {
cmdName[1] != 'status' &&
cmdName.length >= 1)
},
usage: "verify [options]",
usage: "verify [options] [contracts]",
process: async (cmd, callback) => {
//embark.logger.info("cmd", cmd)
//embark.logger.info("verifying...")

View File

@ -70,7 +70,7 @@ class MythXIssues {
this.issues = [];
this.logs = [];
//console.log("mythx", JSON.stringify(mythx))
this.buildObj = mythx.truffle2MythXJSON(buildObj);
this.buildObj = mythx.embark2MythXJSON(buildObj);
this.debug = config.debug;
this.logger = config.logger;
this.sourceMap = this.buildObj.sourceMap;
@ -267,30 +267,18 @@ function doReport(config, objects, errors, notAnalyzedContracts) {
})
})
if (config.yaml) {
config.logger.info("config.yaml", config.yaml)
const yamlDumpObjects = objects;
for(let i = 0; i < yamlDumpObjects.length; i++) {
delete yamlDumpObjects[i].logger;
}
config.logger.info(yaml.safeDump(yamlDumpObjects, {'skipInvalid': true}));
} else if (config.json) {
config.logger.info("config.json", config.json)
config.logger.info(JSON.stringify(objects, null, 4));
} else {
const spaceLimited = ['tap', 'markdown', 'json'].indexOf(config.style) === -1;
const eslintIssues = objects
.map(obj => obj.getEslintIssues(config, spaceLimited))
.reduce((acc, curr) => acc.concat(curr), []);
const spaceLimited = ['tap', 'markdown', 'json'].indexOf(config.style) === -1;
const eslintIssues = objects
.map(obj => obj.getEslintIssues(config, spaceLimited))
.reduce((acc, curr) => acc.concat(curr), []);
// FIXME: temporary solution until backend will return correct filepath and output.
const eslintIssuesByBaseName = groupEslintIssuesByBasename(eslintIssues);
// FIXME: temporary solution until backend will return correct filepath and output.
const eslintIssuesByBaseName = groupEslintIssuesByBasename(eslintIssues);
const uniqueIssues = getUniqueIssues(eslintIssuesByBaseName);
const formatter = getFormatter(config.style);
const report = formatter(uniqueIssues);
config.logger.info(report);
}
const uniqueIssues = getUniqueIssues(eslintIssuesByBaseName);
const formatter = getFormatter(config.style);
const report = formatter(uniqueIssues);
config.logger.info(report);
const logGroups = objects.map(obj => { return {'sourcePath': obj.sourcePath, 'logs': obj.logs, 'uuid': obj.uuid};})
.reduce((acc, curr) => acc.concat(curr), []);

View File

@ -35,15 +35,20 @@ const buildRequestData = contractObjects => {
//console.log("contractObjects", JSON.stringify(contractObjects))
const { sources, compiler } = contractObjects;
let allContracts = [];
const allSources = Object.entries(sources).reduce((accum, [sourcePath, data]) => {
const source = fs.readFileSync(sourcePath, 'utf8')
const { ast, legacyAST } = data;
const key = path.basename(sourcePath);
accum[key] = { ast, legacyAST, source };
return accum;
}, {});
Object.keys(contractObjects.contracts).forEach(function(fileKey, index) {
const contractFile = contractObjects.contracts[fileKey];
const sources = contractObjects.sources[fileKey];
// Read source code from file
const source = fs.readFileSync(fileKey, 'utf8')
//console.log("source", source)
//const sources = contractObjects.sources[fileKey];
Object.keys(contractFile).forEach(function(contractKey, index) {
@ -55,10 +60,8 @@ const buildRequestData = contractObjects => {
deployedBytecode: contractJSON.evm.deployedBytecode.object,
sourceMap: contractJSON.evm.bytecode.sourceMap,
deployedSourceMap: contractJSON.evm.deployedBytecode.sourceMap,
ast: sources.ast,
legacyAST: sources.legacyAST,
sourcePath: fileKey,
source: source
sources: allSources,
sourcePath: fileKey
};
//console.log("comes out", contract)
@ -69,7 +72,7 @@ const buildRequestData = contractObjects => {
return allContracts;
};
const truffle2MythXJSON = function(truffleJSON, toolId = 'embark-mythx') {
const embark2MythXJSON = function(embarkJSON, toolId = 'embark-mythx') {
let {
contractName,
bytecode,
@ -77,15 +80,15 @@ const truffle2MythXJSON = function(truffleJSON, toolId = 'embark-mythx') {
sourceMap,
deployedSourceMap,
sourcePath,
source,
legacyAST,
ast
} = truffleJSON;
sources
} = embarkJSON;
const sourcesKey = path.basename(sourcePath);
sourceMap = srcmap.zeroedSourceMap(sourceMap);
deployedSourceMap = srcmap.zeroedSourceMap(deployedSourceMap);
let sourceList = [];
for(let key in sources) {
sourceList.push(sources[key].ast.absolutePath);
}
const mythXJSON = {
contractName,
@ -93,15 +96,9 @@ const truffle2MythXJSON = function(truffleJSON, toolId = 'embark-mythx') {
deployedBytecode,
sourceMap,
deployedSourceMap,
sourceList: [ sourcesKey ],
sources: {
[sourcesKey]: {
source,
ast,
legacyAST,
},
},
mainSource: sourcesKey,
sourceList: sourceList,
sources,
toolId
}
@ -181,7 +178,7 @@ const cleanAnalyzeDataEmptyProps = (data, debug, logger) => {
module.exports = {
remapMythXOutput,
truffle2MythXJSON,
embark2MythXJSON,
buildRequestData,
getNotFoundContracts,
getFoundContractNames,

View File

@ -42,37 +42,56 @@ async function analyse(contracts, cfg, embark) {
//Check contract names provided in options are respected
//embark.logger.info("contracts", contracts)
//embark.logger.info("cfg.contracts", cfg.contracts)
embark.logger.info("cfg.contracts", cfg.contracts)
//console.log("embark.pluginConfig.ignore", embark.pluginConfig.ignore)
// Filter contracts based on parameter choice
let toSubmit = {};
if(cfg.contracts) {
toSubmit.sources = contracts.sources
toSubmit.contracts = {}
for (let [filename, contractObjects] of Object.entries(contracts.contracts)) {
for (let [contractName, contract] of Object.entries(contractObjects)) {
let toSubmit = { "contracts": {}, "sources": contracts.sources };
if(!("ignore" in embark.pluginConfig)) {
embark.pluginConfig.ignore = []
}
console.log("embark.pluginConfig.ignore", JSON.stringify(embark.pluginConfig.ignore))
//console.log("cfg.contracts", cfg.contracts)
for (let [filename, contractObjects] of Object.entries(contracts.contracts)) {
for (let [contractName, contract] of Object.entries(contractObjects)) {
if(!("contracts" in cfg)) {
if (embark.pluginConfig.ignore.indexOf(contractName) == -1) {
//console.log("Adding to submit", contractName, contractObjects)
if(!toSubmit.contracts[filename]) {
toSubmit.contracts[filename] = {}
//toSubmit.sources[filename] = contracts.sources[filename]
}
toSubmit.contracts[filename][contractName] = contract;
}
} else {
if (cfg.contracts.indexOf(contractName) >= 0 && embark.pluginConfig.ignore.indexOf(contractName) == -1) {
//console.log("Adding to submit", contractName, contractObjects)
if(!toSubmit.contracts[filename]) {
toSubmit.contracts[filename] = {}
//toSubmit.sources[filename] = contracts.sources[filename]
}
toSubmit.contracts[filename][contractName] = contract;
}
}
}
} else {
toSubmit = contracts
}
//console.log("toSubmit", JSON.stringify(toSubmit))
//console.log("contracts", JSON.stringify(contracts))
// Stop here if no contracts are left
if(Object.keys(toSubmit.contracts).length === 0) {
embark.logger.info("No contracts to submit");
embark.logger.info("No contracts to submit.");
return 0;
}
//embark.logger.info("toSubmit", toSubmit)
const submitObjects = mythXUtil.buildRequestData(toSubmit)
console.log("submitObjects", JSON.stringify(submitObjects))
//return 0
const { objects, errors } = await doAnalysis(armletClient, cfg, submitObjects, null, limit)
@ -128,6 +147,8 @@ const doAnalysis = async (armletClient, config, contracts, contractNames = null,
initialDelay
};
console.log("obj", JSON.stringify(obj))
analyzeOpts.data = mythXUtil.cleanAnalyzeDataEmptyProps(obj.buildObj, config.debug, config.logger.debug);
analyzeOpts.data.analysisMode = config.full ? "full" : "quick";
if (config.debug > 1) {
@ -136,36 +157,30 @@ const doAnalysis = async (armletClient, config, contracts, contractNames = null,
// request analysis to armlet.
try {
//TODO: Call analyze/analyzeWithStatus asynchronously
config.logger.info("Submitting '" + obj.contractName + "' for " + analyzeOpts.data.analysisMode + " analysis...")
const {issues, status} = await armletClient.analyzeWithStatus(analyzeOpts);
console.log("after analyze call")
//config.logger.info("armletResult", JSON.stringify(armletResult))
//config.logger.info("issues", issues)
//config.logger.info("status", status)
obj.uuid = status.uuid;
config.logger.info(`${analyzeOpts.data.contractName}: UUID is ${status.uuid}`);
console.log("uuid", obj.uuid)
if (status.status === 'Error') {
return [status, null];
} else {
obj.setIssues(issues);
}
return [null, obj];
} catch (err) {
let errStr;
if (typeof err === 'string') {
// It is assumed that err should be string here.
errStr = `${err}`;
} else if (typeof err.message === 'string') {
// If err is Error, get message property.
errStr = err.message;
} else {
// If err is unexpected type, coerce err to inspectable format.
// This situation itself is not assumed, but this is for robustness and investigation.
errStr = `${util.inspect(err)}`;
}
// Check error message from armlet to determine if a timeout occurred.
if (errStr.includes('User or default timeout reached after')
|| errStr.includes('Timeout reached after')) {
return [(buildObj.contractName + ": ").yellow + errStr, null];