refactor(@embark/debugger): move debugger index.js to typescript

This commit is contained in:
Iuri Matias 2018-11-15 16:14:13 -05:00
parent 227decde0a
commit b86634886e
2 changed files with 314 additions and 294 deletions

View File

@ -1,294 +0,0 @@
var DebuggerManager = require('./debugger_manager.js').default;
class TransactionDebugger {
constructor(embark, _options) {
this.embark = embark;
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));
this.tx_tracker = {};
this.last_tx = "";
this.isDebugging = false;
this.listenToEvents();
this.listenToCommands();
this.listentoAPI();
}
listenToEvents() {
const self = this;
this.embark.events.on('blockchain:tx', (tx) => {
this.embark.events.request("contracts:contract", tx.name, (contract) => {
self.tx_tracker[tx.transactionHash] = {tx: tx, contract: contract};
self.last_tx = tx.transactionHash;
if (tx.status !== '0x0') return;
self.embark.logger.info("Transaction failed");
self.debugger_manager.getLastLine(tx.transactionHash, contract.filename, (lines, line, known_vars) => {
lines.forEach((line) => {
self.embark.logger.error(line);
});
self.find_vars_in_line(tx.transactionHash, line, known_vars, (found_vars) => {
if (!found_vars) return;
self.embark.logger.info("vars:");
found_vars.forEach((variable) => {
self.embark.logger.info(`${variable.name}: ${variable.value}`);
});
});
});
});
});
}
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) {
let value = globals[variable];
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];
let variable_name = variable.split(' ')[0];
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];
let variable_name = variable.split(' ')[0];
if (line.indexOf(variable_name) >= 0) {
found_vars.push({name: variable, value: value});
}
}
cb(found_vars);
});
}
listentoAPI() {
this.debuggerData = {};
this.apiDebugger = false;
this.embark.registerAPICall('post', '/embark-api/debugger/start', (req, res) => {
let txHash = req.body.params.txHash;
this.embark.events.request("contracts:contract:byTxHash", txHash, (err, contract) => {
if (err) {
this.embark.logger.error(err);
return res.send({error: err});
}
let filename = contract.filename;
this.apiDebugger = this.debugger_manager.createDebuggerSession(txHash, filename, () => {
this.getGlobals(txHash, (err, globals) => {
if (err) return res.send({ok: false});
this.debuggerData.globals = globals;
res.send({ok :true});
});
});
});
});
this.embark.registerAPICall('post', '/embark-api/debugger/JumpBack', (req, res) => {
this.apiDebugger.stepJumpNextBreakpoint();
res.send({ok :true});
});
this.embark.registerAPICall('post', '/embark-api/debugger/JumpForward', (req, res) => {
this.apiDebugger.stepJumpPreviousBreakpoint();
res.send({ok :true});
});
this.embark.registerAPICall('post', '/embark-api/debugger/StepOverForward', (req, res) => {
this.apiDebugger.stepOverForward(true);
res.send({ok :true});
});
this.embark.registerAPICall('post', '/embark-api/debugger/StepOverBackward', (req, res) => {
this.apiDebugger.stepOverBack(true);
res.send({ok :true});
});
this.embark.registerAPICall('post', '/embark-api/debugger/StepIntoForward', (req, res) => {
this.apiDebugger.stepIntoForward(true);
res.send({ok :true});
});
this.embark.registerAPICall('post', '/embark-api/debugger/StepIntoBackward', (req, res) => {
this.apiDebugger.stepIntoBack(true);
res.send({ok :true});
});
this.embark.registerAPICall('post', '/embark-api/debugger/breakpoint', (req, res) => {
console.dir("new breakpoint");
res.send({ok :true});
});
this.embark.registerAPICall('ws', '/embark-api/debugger', (ws, _req) => {
if (!this.apiDebugger) return;
this.apiDebugger.events.on("source", (lineColumnPos, rawLocation) => {
this.debuggerData.sources = {lineColumnPos, rawLocation};
ws.send(JSON.stringify(this.debuggerData), () => {});
});
this.apiDebugger.events.on("locals", (data) => {
this.debuggerData.locals = this.simplifyDebuggerVars(data);
ws.send(JSON.stringify(this.debuggerData), () => {});
});
this.apiDebugger.events.on("globals", (data) => {
this.debuggerData.contract = this.simplifyDebuggerVars(data);
ws.send(JSON.stringify(this.debuggerData), () => {});
});
});
}
simplifyDebuggerVars(data) {
let new_data = {};
for (let key in data) {
let field = data[key];
new_data[`${key} (${field.type})`] = field.value;
}
return new_data;
}
listenToCommands() {
const self = this;
this.cmdDebugger = false;
this.currentCmdTxHash = "";
this.embark.registerConsoleCommand((cmd, _options) => {
let cmdName = cmd.split(" ")[0];
let txHash = cmd.split(" ")[1];
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;
}
let filename = contract.filename;
self.currentCmdTxHash = txHash;
self.embark.logger.info("debugging tx " + txHash);
self.cmdDebugger = self.debugger_manager.createDebuggerSession(txHash, filename, () => {
self.cmdDebugger.getSource().forEach((line) => {
console.dir(line);
});
});
});
return;
}
self.currentCmdTxHash = self.last_tx;
let filename = self.tx_tracker[self.last_tx].contract.filename;
self.embark.logger.info("debugging tx " + self.last_tx);
self.cmdDebugger = self.debugger_manager.createDebuggerSession(self.last_tx, filename, () => {
self.cmdDebugger.getSource().forEach((line) => {
console.dir(line);
});
});
}
};
});
this.embark.registerConsoleCommand((cmd, _options) => {
return {
match: () => (cmd === 'next' || cmd === 'n'),
process: (_cb) => {
if (!self.cmdDebugger.currentStep()) {
console.dir("end of execution reached");
return self.cmdDebugger.unload();
}
self.cmdDebugger.stepOverForward(true);
self.cmdDebugger.getSource().forEach((line) => {
console.dir(line);
});
}
};
});
this.embark.registerConsoleCommand((cmd, _options) => {
return {
match: () => (cmd === 'previous' || cmd === 'p'),
process: (_cb) => {
if (!self.cmdDebugger.currentStep()) {
console.dir("end of execution reached");
return self.cmdDebugger.unload();
}
self.cmdDebugger.stepOverBack(true);
self.cmdDebugger.getSource().forEach((line) => {
console.dir(line);
});
}
};
});
this.embark.registerConsoleCommand((cmd, _options) => {
return {
match: () => (cmd === 'var local' || cmd === 'v l' || cmd === 'vl'),
process: (_cb) => {
self.cmdDebugger.displayLocals();
}
};
});
this.embark.registerConsoleCommand((cmd, _options) => {
return {
match: () => (cmd === 'var global' || cmd === 'v g' || cmd === 'vg'),
process: (_cb) => {
self.cmdDebugger.displayGlobals();
}
};
});
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);
});
}
};
});
}
getGlobals(txHash, cb) {
let globals = {};
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);
});
});
}
}
module.exports = TransactionDebugger;

View File

@ -0,0 +1,314 @@
import DebuggerManager from "./debugger_manager";
interface Events {
on: any;
request: any;
}
interface EmbarkApi {
events: Events;
registerAPICall: any;
registerConsoleCommand: any;
logger: any;
}
class TransactionDebugger {
private embark: EmbarkApi;
private lastTx: string;
private debuggerManager: any;
private txTracker: any;
private isDebugging: boolean;
private currentCmdTxHash: string;
private apiDebugger: any;
private cmdDebugger: any;
private debuggerData: any;
constructor(embark: EmbarkApi, options?: any) {
this.embark = embark;
this.debuggerManager = new DebuggerManager("http://localhost:8545");
embark.events.on("contracts:compile:solc", this.debuggerManager.setInputJson.bind(this.debuggerManager));
embark.events.on("contracts:compiled:solc", this.debuggerManager.setOutputJson.bind(this.debuggerManager));
this.txTracker = {};
this.lastTx = "";
this.isDebugging = false;
this.currentCmdTxHash = "";
this.listenToEvents();
this.listenToCommands();
this.listentoAPI();
}
private listenToEvents() {
this.embark.events.on("blockchain:tx", (tx: any) => {
this.embark.events.request("contracts:contract", tx.name, (contract: any) => {
this.txTracker[tx.transactionHash] = {tx, contract};
this.lastTx = tx.transactionHash;
if (tx.status !== "0x0") { return; }
this.embark.logger.info("Transaction failed");
this.debuggerManager.getLastLine(tx.transactionHash, contract.filename, (lines: string[], line: string, knownVars: any) => {
lines.forEach((errorLine: string) => {
this.embark.logger.error(errorLine);
});
this.find_vars_in_line(tx.transactionHash, line, knownVars, (foundVars: any) => {
if (!foundVars) { return; }
this.embark.logger.info("vars:");
foundVars.forEach((variable: any) => {
this.embark.logger.info(`${variable.name}: ${variable.value}`);
});
});
});
});
});
}
private find_vars_in_line(txHash: string, line: string, knownVars: any, cb: any) {
const foundVars: any = [];
this.getGlobals(txHash, (err: any, globals: any) => {
if (err) { return cb([]); }
for (const variable of Object.keys(globals)) {
const value: any = globals[variable];
if (line.indexOf(variable) >= 0) {
foundVars.push({name: variable, value});
}
}
for (const variable of Object.keys(knownVars.locals)) {
const value: any = knownVars.locals[variable];
const variableName: string = variable.split(" ")[0];
if (line.indexOf(variableName) >= 0) {
foundVars.push({name: variable, value});
}
}
for (const variable of Object.keys(knownVars.contract)) {
const value: any = knownVars.contract[variable];
const variableName: string = variable.split(" ")[0];
if (line.indexOf(variableName) >= 0) {
foundVars.push({name: variable, value});
}
}
cb(foundVars);
});
}
private listentoAPI() {
this.debuggerData = {};
this.apiDebugger = false;
this.embark.registerAPICall("post", "/embark-api/debugger/start", (req: any, res: any) => {
const txHash: string = req.body.params.txHash;
this.embark.events.request("contracts:contract:byTxHash", txHash, (err: any, contract: any) => {
if (err) {
this.embark.logger.error(err);
return res.send({error: err});
}
const filename: string = contract.filename;
this.apiDebugger = this.debuggerManager.createDebuggerSession(txHash, filename, () => {
this.getGlobals(txHash, (errGlobals: any, globals: any) => {
if (errGlobals) { return res.send({ok: false}); }
this.debuggerData.globals = globals;
res.send({ok: true});
});
});
});
});
this.embark.registerAPICall("post", "/embark-api/debugger/JumpBack", (req: any, res: any) => {
this.apiDebugger.stepJumpNextBreakpoint();
res.send({ok: true});
});
this.embark.registerAPICall("post", "/embark-api/debugger/JumpForward", (req: any, res: any) => {
this.apiDebugger.stepJumpPreviousBreakpoint();
res.send({ok: true});
});
this.embark.registerAPICall("post", "/embark-api/debugger/StepOverForward", (req: any, res: any) => {
this.apiDebugger.stepOverForward(true);
res.send({ok: true});
});
this.embark.registerAPICall("post", "/embark-api/debugger/StepOverBackward", (req: any, res: any) => {
this.apiDebugger.stepOverBack(true);
res.send({ok: true});
});
this.embark.registerAPICall("post", "/embark-api/debugger/StepIntoForward", (req: any, res: any) => {
this.apiDebugger.stepIntoForward(true);
res.send({ok: true});
});
this.embark.registerAPICall("post", "/embark-api/debugger/StepIntoBackward", (req: any, res: any) => {
this.apiDebugger.stepIntoBack(true);
res.send({ok: true});
});
this.embark.registerAPICall("post", "/embark-api/debugger/breakpoint", (req: any, res: any) => {
res.send({ok: true});
});
this.embark.registerAPICall("ws", "/embark-api/debugger", (ws: any, req: any) => {
if (!this.apiDebugger) { return; }
this.apiDebugger.events.on("source", (lineColumnPos: any, rawLocation: any) => {
this.debuggerData.sources = {lineColumnPos, rawLocation};
ws.send(JSON.stringify(this.debuggerData), () => {});
});
this.apiDebugger.events.on("locals", (data: any) => {
this.debuggerData.locals = this.simplifyDebuggerVars(data);
ws.send(JSON.stringify(this.debuggerData), () => {});
});
this.apiDebugger.events.on("globals", (data: any) => {
this.debuggerData.contract = this.simplifyDebuggerVars(data);
ws.send(JSON.stringify(this.debuggerData), () => {});
});
});
}
private simplifyDebuggerVars(data: any) {
const newData: any = {};
for (const key of Object.keys(data)) {
const field = data[key];
newData[`${key} (${field.type})`] = field.value;
}
return newData;
}
private listenToCommands() {
this.cmdDebugger = false;
this.currentCmdTxHash = "";
this.embark.registerConsoleCommand((cmd: string, options: any) => {
const cmdName = cmd.split(" ")[0];
const txHash = cmd.split(" ")[1];
return {
match: () => cmdName === "debug",
process: (cb: any) => {
if (txHash) {
this.embark.events.request("contracts:contract:byTxHash", txHash, (err: any, contract: any) => {
if (err) {
this.embark.logger.error(err);
return;
}
this.currentCmdTxHash = txHash;
this.embark.logger.info("debugging tx " + txHash);
this.cmdDebugger = this.debuggerManager.createDebuggerSession(txHash, contract.filename, () => {
this.cmdDebugger.getSource().forEach((line: string) => {
this.embark.logger.info(line);
});
});
});
return;
}
this.currentCmdTxHash = this.lastTx;
const filename: string = this.txTracker[this.lastTx].contract.filename;
this.embark.logger.info("debugging tx " + this.lastTx);
this.cmdDebugger = this.debuggerManager.createDebuggerSession(this.lastTx, filename, () => {
this.cmdDebugger.getSource().forEach((line: string) => {
this.embark.logger.info(line);
});
});
},
};
});
this.embark.registerConsoleCommand((cmd: string, options: any) => {
return {
match: () => (cmd === "next" || cmd === "n"),
process: (cb: any) => {
if (!this.cmdDebugger.currentStep()) {
this.embark.logger.info("end of execution reached");
return this.cmdDebugger.unload();
}
this.cmdDebugger.stepOverForward(true);
this.cmdDebugger.getSource().forEach((line: string) => {
this.embark.logger.info(line);
});
},
};
});
this.embark.registerConsoleCommand((cmd: string, options: any) => {
return {
match: () => (cmd === "previous" || cmd === "p"),
process: (cb: any) => {
if (!this.cmdDebugger.currentStep()) {
this.embark.logger.info("end of execution reached");
return this.cmdDebugger.unload();
}
this.cmdDebugger.stepOverBack(true);
this.cmdDebugger.getSource().forEach((line: string) => {
this.embark.logger.info(line);
});
},
};
});
this.embark.registerConsoleCommand((cmd: string, options: any) => {
return {
match: () => (cmd === "var local" || cmd === "v l" || cmd === "vl"),
process: (cb: any) => {
this.cmdDebugger.displayLocals();
},
};
});
this.embark.registerConsoleCommand((cmd: string, options: any) => {
return {
match: () => (cmd === "var global" || cmd === "v g" || cmd === "vg"),
process: (cb: any) => {
this.cmdDebugger.displayGlobals();
},
};
});
this.embark.registerConsoleCommand((cmd: string, options: any) => {
return {
match: () => (cmd === "var all" || cmd === "v a" || cmd === "va"),
process: (cb: any) => {
this.getGlobals(this.currentCmdTxHash, (err: any, globals: any) => {
if (err) { return this.embark.logger.error(err); }
this.embark.logger.info(globals);
});
},
};
});
}
private getGlobals(txHash: string, cb: any) {
const globals: any = {};
this.embark.events.request("blockchain:getTransaction", txHash, (err: any, tx: any) => {
if (err) { return cb(err); }
this.embark.events.request("blockchain:block:byHash", tx.blockHash, (errHash: any, block: any) => {
if (errHash) { return cb(errHash); }
/* tslint:disable:no-string-literal */
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;
/* tslint:enable:no-string-literal */
cb(null, globals);
});
});
}
}
module.exports = TransactionDebugger;