2018-10-12 22:22:17 +00:00
|
|
|
var RemixDebug = require('remix-debug-debugtest');
|
|
|
|
var CmdLine = RemixDebug.CmdLine;
|
|
|
|
var DebuggerManager = require('./debugger_manager.js');
|
|
|
|
|
|
|
|
class TransactionDebugger {
|
|
|
|
constructor(embark, _options) {
|
2018-10-29 13:15:48 +00:00
|
|
|
const self = this;
|
|
|
|
this.embark = embark;
|
2018-10-12 22:22:17 +00:00
|
|
|
|
2018-10-29 13:15:48 +00:00
|
|
|
this.debugger_manager = new DebuggerManager("http://localhost:8545");
|
|
|
|
embark.events.on('contracts:compile:solc', this.debugger_manager.setInputJson.bind(this.debugger_manager));
|
|
|
|
embark.events.on('contracts:compiled:solc', this.debugger_manager.setOutputJson.bind(this.debugger_manager));
|
2018-10-12 22:22:17 +00:00
|
|
|
|
2018-10-29 13:15:48 +00:00
|
|
|
this.tx_tracker = {};
|
|
|
|
this.last_tx = "";
|
2018-10-12 22:22:17 +00:00
|
|
|
|
2018-10-29 13:15:48 +00:00
|
|
|
this.isDebugging = false;
|
|
|
|
this.listenToEvents();
|
|
|
|
this.listenToCommands();
|
|
|
|
this.listentoAPI();
|
2018-10-12 22:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
listenToEvents() {
|
2018-10-29 13:15:48 +00:00
|
|
|
const self = this;
|
2018-10-12 22:22:17 +00:00
|
|
|
this.embark.events.on('blockchain:tx', (tx) => {
|
|
|
|
this.embark.events.request("contracts:contract", tx.name, (contract) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
self.tx_tracker[tx.transactionHash] = {tx: tx, contract: contract};
|
|
|
|
self.last_tx = tx.transactionHash;
|
|
|
|
if (tx.status !== '0x0') return;
|
2018-10-12 22:22:17 +00:00
|
|
|
|
|
|
|
self.embark.logger.info("Transaction failed");
|
|
|
|
|
2018-10-27 19:31:13 +00:00
|
|
|
self.debugger_manager.getLastLine(tx.transactionHash, contract.filename, (lines, line, known_vars) => {
|
2018-10-12 22:22:17 +00:00
|
|
|
lines.forEach((line) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
self.embark.logger.error(line);
|
|
|
|
});
|
2018-10-27 19:31:13 +00:00
|
|
|
|
|
|
|
self.find_vars_in_line(tx.transactionHash, line, known_vars, (found_vars) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
if (!found_vars) return;
|
|
|
|
self.embark.logger.info("vars:");
|
2018-10-27 19:31:13 +00:00
|
|
|
found_vars.forEach((variable) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
self.embark.logger.info(`${variable.name}: ${variable.value}`);
|
|
|
|
});
|
2018-10-27 19:31:13 +00:00
|
|
|
});
|
2018-10-29 13:15:48 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-10-12 22:22:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:31:13 +00:00
|
|
|
find_vars_in_line(txHash, line, known_vars, cb) {
|
|
|
|
let found_vars = [];
|
|
|
|
this.getGlobals(txHash, (err, globals) => {
|
|
|
|
if (err) return cb([]);
|
|
|
|
for (let variable in globals) {
|
2018-10-29 13:15:48 +00:00
|
|
|
let value = globals[variable];
|
2018-10-27 19:31:13 +00:00
|
|
|
if (line.indexOf(variable) >= 0) {
|
|
|
|
found_vars.push({name: variable, value: value});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let variable in known_vars.locals) {
|
|
|
|
let value = known_vars.locals[variable];
|
2018-10-29 13:15:48 +00:00
|
|
|
let variable_name = variable.split(' ')[0];
|
2018-10-27 19:31:13 +00:00
|
|
|
if (line.indexOf(variable_name) >= 0) {
|
|
|
|
found_vars.push({name: variable, value: value});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let variable in known_vars.contract) {
|
|
|
|
let value = known_vars.contract[variable];
|
2018-10-29 13:15:48 +00:00
|
|
|
let variable_name = variable.split(' ')[0];
|
2018-10-27 19:31:13 +00:00
|
|
|
if (line.indexOf(variable_name) >= 0) {
|
|
|
|
found_vars.push({name: variable, value: value});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(found_vars);
|
2018-10-29 13:15:48 +00:00
|
|
|
});
|
2018-10-27 19:31:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 22:22:17 +00:00
|
|
|
listentoAPI() {
|
2018-10-29 13:15:48 +00:00
|
|
|
this.debuggerData = {};
|
|
|
|
this.apiDebugger = false;
|
2018-10-12 22:22:17 +00:00
|
|
|
|
|
|
|
this.embark.registerAPICall('post', '/embark-api/debugger/start', (req, res) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
let txHash = req.body.params.txHash;
|
2018-10-12 22:22:17 +00:00
|
|
|
|
|
|
|
this.embark.events.request("contracts:contract:byTxHash", txHash, (err, contract) => {
|
|
|
|
if (err) {
|
|
|
|
this.embark.logger.error(err);
|
2018-10-29 13:15:48 +00:00
|
|
|
return res.send({error: err});
|
2018-10-12 22:22:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 13:15:48 +00:00
|
|
|
let filename = contract.filename;
|
2018-10-12 22:22:17 +00:00
|
|
|
|
|
|
|
this.apiDebugger = this.debugger_manager.createDebuggerSession(txHash, filename, () => {
|
2018-10-27 19:31:13 +00:00
|
|
|
this.getGlobals(txHash, (err, globals) => {
|
|
|
|
if (err) return res.send({ok: false});
|
|
|
|
this.debuggerData.globals = globals;
|
2018-10-29 13:15:48 +00:00
|
|
|
res.send({ok :true});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-10-12 22:22:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.embark.registerAPICall('post', '/embark-api/debugger/JumpBack', (req, res) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
this.apiDebugger.stepJumpNextBreakpoint();
|
|
|
|
res.send({ok :true});
|
|
|
|
});
|
2018-10-12 22:22:17 +00:00
|
|
|
this.embark.registerAPICall('post', '/embark-api/debugger/JumpForward', (req, res) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
this.apiDebugger.stepJumpPreviousBreakpoint();
|
|
|
|
res.send({ok :true});
|
|
|
|
});
|
2018-10-12 22:22:17 +00:00
|
|
|
this.embark.registerAPICall('post', '/embark-api/debugger/StepOverForward', (req, res) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
this.apiDebugger.stepOverForward(true);
|
|
|
|
res.send({ok :true});
|
|
|
|
});
|
2018-10-12 22:22:17 +00:00
|
|
|
this.embark.registerAPICall('post', '/embark-api/debugger/StepOverBackward', (req, res) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
this.apiDebugger.stepOverBack(true);
|
|
|
|
res.send({ok :true});
|
|
|
|
});
|
2018-10-12 22:22:17 +00:00
|
|
|
this.embark.registerAPICall('post', '/embark-api/debugger/StepIntoForward', (req, res) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
this.apiDebugger.stepIntoForward(true);
|
|
|
|
res.send({ok :true});
|
|
|
|
});
|
2018-10-12 22:22:17 +00:00
|
|
|
this.embark.registerAPICall('post', '/embark-api/debugger/StepIntoBackward', (req, res) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
this.apiDebugger.stepIntoBack(true);
|
|
|
|
res.send({ok :true});
|
2018-10-12 22:22:17 +00:00
|
|
|
});
|
|
|
|
this.embark.registerAPICall('post', '/embark-api/debugger/breakpoint', (req, res) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
console.dir("new breakpoint");
|
|
|
|
res.send({ok :true});
|
2018-10-12 22:22:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.embark.registerAPICall('ws', '/embark-api/debugger', (ws, _req) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
if (!this.apiDebugger) return;
|
2018-10-12 22:22:17 +00:00
|
|
|
|
|
|
|
this.apiDebugger.events.on("source", (lineColumnPos, rawLocation) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
this.debuggerData.sources = {lineColumnPos, rawLocation};
|
|
|
|
ws.send(JSON.stringify(this.debuggerData), () => {});
|
|
|
|
});
|
2018-10-12 22:22:17 +00:00
|
|
|
|
2018-10-27 19:31:13 +00:00
|
|
|
this.apiDebugger.events.on("locals", (data) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
this.debuggerData.locals = this.simplifyDebuggerVars(data);
|
|
|
|
ws.send(JSON.stringify(this.debuggerData), () => {});
|
2018-10-27 19:31:13 +00:00
|
|
|
});
|
2018-10-12 22:22:17 +00:00
|
|
|
|
|
|
|
this.apiDebugger.events.on("globals", (data) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
this.debuggerData.contract = this.simplifyDebuggerVars(data);
|
|
|
|
ws.send(JSON.stringify(this.debuggerData), () => {});
|
|
|
|
});
|
2018-10-12 22:22:17 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
simplifyDebuggerVars(data) {
|
|
|
|
let new_data = {};
|
|
|
|
|
|
|
|
for (let key in data) {
|
|
|
|
let field = data[key];
|
2018-10-29 13:15:48 +00:00
|
|
|
new_data[`${key} (${field.type})`] = field.value;
|
2018-10-12 22:22:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 13:15:48 +00:00
|
|
|
return new_data;
|
2018-10-12 22:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
listenToCommands() {
|
2018-10-29 13:15:48 +00:00
|
|
|
const self = this;
|
|
|
|
this.cmdDebugger = false;
|
|
|
|
this.currentCmdTxHash = "";
|
2018-10-12 22:22:17 +00:00
|
|
|
|
|
|
|
this.embark.registerConsoleCommand((cmd, _options) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
let cmdName = cmd.split(" ")[0];
|
|
|
|
let txHash = cmd.split(" ")[1];
|
2018-10-12 22:22:17 +00:00
|
|
|
return {
|
|
|
|
match: () => cmdName === 'debug',
|
|
|
|
process: (cb) => {
|
|
|
|
if (txHash) {
|
|
|
|
this.embark.events.request("contracts:contract:byTxHash", txHash, (err, contract) => {
|
|
|
|
if (err) {
|
|
|
|
this.embark.logger.error(err);
|
|
|
|
return;
|
|
|
|
}
|
2018-10-29 13:15:48 +00:00
|
|
|
let filename = contract.filename;
|
|
|
|
self.currentCmdTxHash = txHash;
|
2018-10-12 22:22:17 +00:00
|
|
|
self.cmdDebugger = self.debugger_manager.createDebuggerSession(txHash, filename, () => {
|
|
|
|
self.cmdDebugger.getSource().forEach((line) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
console.dir(line);
|
|
|
|
});
|
|
|
|
});
|
2018-10-12 22:22:17 +00:00
|
|
|
});
|
2018-10-29 13:15:48 +00:00
|
|
|
return;
|
2018-10-12 22:22:17 +00:00
|
|
|
}
|
2018-10-29 13:15:48 +00:00
|
|
|
self.currentCmdTxHash = self.last_tx;
|
|
|
|
let filename = self.tx_tracker[self.last_tx].contract.filename;
|
2018-10-12 22:22:17 +00:00
|
|
|
self.cmdDebugger = self.debugger_manager.createDebuggerSession(self.last_tx, filename, () => {
|
|
|
|
self.cmdDebugger.getSource().forEach((line) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
console.dir(line);
|
|
|
|
});
|
|
|
|
});
|
2018-10-12 22:22:17 +00:00
|
|
|
}
|
|
|
|
};
|
2018-10-29 13:15:48 +00:00
|
|
|
});
|
2018-10-12 22:22:17 +00:00
|
|
|
|
|
|
|
this.embark.registerConsoleCommand((cmd, _options) => {
|
|
|
|
return {
|
|
|
|
match: () => (cmd === 'next' || cmd === 'n'),
|
|
|
|
process: (cb) => {
|
|
|
|
if (!self.cmdDebugger.currentStep()) {
|
2018-10-29 13:15:48 +00:00
|
|
|
console.dir("end of execution reached");
|
|
|
|
return self.cmdDebugger.unload();
|
2018-10-12 22:22:17 +00:00
|
|
|
}
|
2018-10-29 13:15:48 +00:00
|
|
|
self.cmdDebugger.stepOverForward(true);
|
2018-10-12 22:22:17 +00:00
|
|
|
self.cmdDebugger.getSource().forEach((line) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
console.dir(line);
|
|
|
|
});
|
2018-10-12 22:22:17 +00:00
|
|
|
}
|
|
|
|
};
|
2018-10-29 13:15:48 +00:00
|
|
|
});
|
2018-10-12 22:22:17 +00:00
|
|
|
|
|
|
|
this.embark.registerConsoleCommand((cmd, _options) => {
|
|
|
|
return {
|
|
|
|
match: () => (cmd === 'previous' || cmd === 'p'),
|
|
|
|
process: (cb) => {
|
|
|
|
if (!self.cmdDebugger.currentStep()) {
|
2018-10-29 13:15:48 +00:00
|
|
|
console.dir("end of execution reached");
|
|
|
|
return self.cmdDebugger.unload();
|
2018-10-12 22:22:17 +00:00
|
|
|
}
|
2018-10-29 13:15:48 +00:00
|
|
|
self.cmdDebugger.stepOverBack(true);
|
2018-10-12 22:22:17 +00:00
|
|
|
self.cmdDebugger.getSource().forEach((line) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
console.dir(line);
|
|
|
|
});
|
2018-10-12 22:22:17 +00:00
|
|
|
}
|
|
|
|
};
|
2018-10-29 13:15:48 +00:00
|
|
|
});
|
2018-10-12 22:22:17 +00:00
|
|
|
|
|
|
|
this.embark.registerConsoleCommand((cmd, _options) => {
|
|
|
|
return {
|
|
|
|
match: () => (cmd === 'var local' || cmd === 'v l' || cmd === 'vl'),
|
|
|
|
process: (cb) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
self.cmdDebugger.displayLocals();
|
2018-10-12 22:22:17 +00:00
|
|
|
}
|
|
|
|
};
|
2018-10-29 13:15:48 +00:00
|
|
|
});
|
2018-10-12 22:22:17 +00:00
|
|
|
|
|
|
|
this.embark.registerConsoleCommand((cmd, _options) => {
|
|
|
|
return {
|
|
|
|
match: () => (cmd === 'var global' || cmd === 'v g' || cmd === 'vg'),
|
|
|
|
process: (cb) => {
|
2018-10-29 13:15:48 +00:00
|
|
|
self.cmdDebugger.displayGlobals();
|
2018-10-12 22:22:17 +00:00
|
|
|
}
|
|
|
|
};
|
2018-10-29 13:15:48 +00:00
|
|
|
});
|
2018-10-27 19:31:13 +00:00
|
|
|
|
|
|
|
this.embark.registerConsoleCommand((cmd, _options) => {
|
|
|
|
return {
|
|
|
|
match: () => (cmd === 'var all' || cmd === 'v a' || cmd === 'va'),
|
|
|
|
process: (cb) => {
|
|
|
|
|
|
|
|
self.getGlobals((err, globals) => {
|
|
|
|
if (err) return self.embark.logger.error(err);
|
|
|
|
console.dir(globals);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2018-10-29 13:15:48 +00:00
|
|
|
});
|
2018-10-12 22:22:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:31:13 +00:00
|
|
|
getGlobals(txHash, cb) {
|
2018-10-29 13:15:48 +00:00
|
|
|
let globals = {};
|
2018-10-27 19:31:13 +00:00
|
|
|
this.embark.events.request("blockchain:getTransaction", txHash, (err, tx) => {
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
|
|
|
this.embark.events.request("blockchain:block:byHash", tx.blockHash, (err, block) => {
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
|
|
|
globals["block.blockHash"] = tx.blockHash;
|
|
|
|
globals["block.number"] = tx.blockNumber;
|
|
|
|
globals["block.coinbase"] = block.miner;
|
|
|
|
globals["block.difficulty"] = block.difficulty;
|
|
|
|
globals["block.gaslimit"] = block.gasLimit;
|
|
|
|
globals["block.timestamp"] = block.timestamp;
|
|
|
|
globals["msg.sender"] = tx.from;
|
|
|
|
globals["msg.gas"] = tx.gas;
|
|
|
|
globals["msg.gasPrice"] = tx.gasPrice;
|
|
|
|
globals["msg.value"] = tx.value;
|
|
|
|
globals["now"] = block.timestamp;
|
|
|
|
|
|
|
|
cb(null, globals);
|
2018-10-29 13:15:48 +00:00
|
|
|
});
|
|
|
|
});
|
2018-10-27 19:31:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 22:22:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 13:15:48 +00:00
|
|
|
module.exports = TransactionDebugger;
|