eth/tracers: add three more JavaScript tracers

This commit is contained in:
Martin Holst Swende 2017-11-27 11:10:10 +01:00 committed by Péter Szilágyi
parent 5fb85d4b7d
commit 3947b547c2
4 changed files with 344 additions and 0 deletions

View file

@ -0,0 +1,70 @@
/**
4bytes searches for 4byte-identifiers, and collects them for post-processing.
It collects the methods identifiers along with the size of the supplied data, so that
a reversed signature can be matched against the size of the data.
Example:
> debug.traceTransaction( "0x214e597e35da083692f5386141e69f47e973b2c56e7a8073b1ea08fd7571e9de", {tracer: "4bytes"})
{
0x27dc297e-128: 1,
0x38cc4831-0: 2,
0x524f3889-96: 1,
0xadf59f99-288: 1,
0xc281d19e-0: 1
}
@author MH Swende
**/
{
ids : {},
// callType returns 'false' for non-calls, or the
// peek-index for the first param after 'value', i.e. meminstart
callType : function(opstr){
switch(opstr){
case "CALL":
case "CALLCODE":
//g,a,v, memin, meminsz, memout, memoutsz
return 3 //stack ptr to memin
case "DELEGATECALL":
case "STATICCALL":
//g,a,memin, meminsz, memout, memoutsz
return 2;//stack ptr to memin
}
return false;
},
// store save the given indentifier and datasize
store: function(id, size){
var key = ""+id+"-"+size;
this.ids[key] = this.ids[key]+1 || 1;
},
// step is invoked for every opcode that the VM executes.
step: function(log, db) {
var ct = this.callType(log.op.toString());
if (!ct) return;
// Skip any pre-compile invocations, those are just fancy opcodes
if (isPrecompiled(toAddress(log.stack.peek(1).Bytes()))) {
return
}
// Gather internal call details
var inSz = log.stack.peek(ct + 1).Int64();
if (inSz >= 4) {
var inOff = log.stack.peek(ct).Int64();
this.store(toHex(log.memory.slice(inOff, inOff + 4)), inSz-4);
}
},
// result is invoked when all the opcodes have been iterated over and returns
// the final result of the tracing.
result: function(ctx) {
//Save the outer calldata also
if(ctx.input.length > 4){
this.store(toHex(ctx.input.slice(0,4)),ctx.input.length-4 )
}
return this.ids;
},
}

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,74 @@
/**
This tracer returns sufficent information from a trace to
perform evmdis-style disassembly
@author Nick Johnson, Martin Swende
**/
{
"stack": [{"ops": []}],
"npushes": {0: 0, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1, 10: 1, 11: 1, 16: 1, 17: 1, 18: 1, 19: 1, 20: 1, 21: 1, 22: 1, 23: 1, 24: 1, 25: 1, 26: 1, 32: 1, 48: 1, 49: 1, 50: 1, 51: 1, 52: 1, 53: 1, 54: 1, 55: 0, 56: 1, 57: 0, 58: 1, 59: 1, 60: 0, 64: 1, 65: 1, 66: 1, 67: 1, 68: 1, 69: 1, 80: 0, 81: 1, 82: 0, 83: 0, 84: 1, 85: 0, 86: 0, 87: 0, 88: 1, 89: 1, 90: 1, 91: 0, 96: 1, 97: 1, 98: 1, 99: 1, 100: 1, 101: 1, 102: 1, 103: 1, 104: 1, 105: 1, 106: 1, 107: 1, 108: 1, 109: 1, 110: 1, 111: 1, 112: 1, 113: 1, 114: 1, 115: 1, 116: 1, 117: 1, 118: 1, 119: 1, 120: 1, 121: 1, 122: 1, 123: 1, 124: 1, 125: 1, 126: 1, 127: 1, 128: 2, 129: 3, 130: 4, 131: 5, 132: 6, 133: 7, 134: 8, 135: 9, 136: 10, 137: 11, 138: 12, 139: 13, 140: 14, 141: 15, 142: 16, 143: 17, 144: 2, 145: 3, 146: 4, 147: 5, 148: 6, 149: 7, 150: 8, 151: 9, 152: 10, 153: 11, 154: 12, 155: 13, 156: 14, 157: 15, 158: 16, 159: 17, 160: 0, 161: 0, 162: 0, 163: 0, 164: 0, 240: 1, 241: 1, 242: 1, 243: 0, 244: 0, 255: 0},
"result": function() { return this.stack[0].ops; },
"step": function(log, db) {
var frame = this.stack[this.stack.length - 1];
if(log.err) {
frame["error"] = log.err.toString();
} else if(log.depth == this.stack.length) {
opinfo = {
"op": log.op.toNumber(),
"depth" : log.depth,
"result": [],
};
if(frame.ops.length > 0) {
var prevop = frame.ops[frame.ops.length - 1];
for(var i = 0; i < this.npushes[prevop.op]; i++)
prevop.result.push(log.stack.peek(i).Text(16));
}
switch(log.op.toString()) {
case "CALL":
case "CALLCODE":
var instart = log.stack.peek(3).Int64();
var insize = log.stack.peek(4).Int64();
opinfo["gas"] = log.stack.peek(0).Int64();
opinfo["to"] = log.stack.peek(1).Text(16);
opinfo["value"] = log.stack.peek(2).String();
opinfo["input"] = log.memory.slice(instart, instart + insize);
opinfo["error"] = null;
opinfo["return"] = null;
opinfo["ops"] = [];
this.stack.push(opinfo);
break;
case "DELEGATECALL":
case "STATICCALL":
var instart = log.stack.peek(2).Int64();
var insize = log.stack.peek(3).Int64();
opinfo["op"] = log.op.toString();
opinfo["gas"] = log.stack.peek(0).Int64();
opinfo["to"] = log.stack.peek(1).Text(16);
opinfo["input"] = log.memory.slice(instart, instart + insize);
opinfo["error"] = null;
opinfo["return"] = null;
opinfo["ops"] = [];
this.stack.push(opinfo);
break;
case "RETURN":
var out = log.stack.peek(0).Int64();
var outsize = log.stack.peek(1).Int64();
frame.return = log.memory.slice(out, out + outsize);
break;
case "STOP":
case "SUICIDE":
frame.return = log.memory.slice(0, 0);
break;
case "JUMPDEST":
opinfo["pc"] = log.pc;
}
if(log.op.isPush()) {
opinfo["len"] = log.op.toNumber() - 0x5e;
}
frame.ops.push(opinfo);
} else {
this.stack = this.stack.slice(0, log.depth);
}
}
}

View file

@ -0,0 +1,131 @@
/**
This tracer-script outputs sufficient information to create a local
execution of the transaction.
@author Martin Holst Swende
**/
{
//The genesis that we're building
prestate : null,
lookupAccount: function(acc,db){
var addr = toHex(acc);
if(this.prestate[addr]) return;
this.prestate[addr] = {
'balance': db.getBalance(acc),
'nonce' : db.getNonce(acc),
'code' : toHex(db.getCode(acc)),
'storage' : {}
};
},
lookupStorage: function(addr, index, db){
//No overwrite
if (!this.prestate[addr]['storage'][index]){
this.prestate[addr]['storage'][index] = db.getState(addr,key);
}
},
result: function(ctx,db) {
// At this point, we need to deduct the 'value' from the
// outer transaction, and move it back to the origin
var from = toAddress(ctx.from);
var to = toAddress(ctx.to);
var v = ctx.value;
this.lookupAccount(from, db);
// Add back value to sender (bigints)
var from_bal = this.prestate[toHex(from)]['balance'];
var to_bal = this.prestate[toHex(to)]['balance'];
to_bal.Sub(to_bal, v);
from_bal.Add(from_bal, v);
// Set back nonce (uint64)
this.prestate[toHex(from)]['nonce']--;
//Note, the sender-account still may not be 100% correct,
// since we haven't accounted for the gas.
//Homestead
config = {
"eip150Block": 2000,
"eip158Block": 2000,
"eip155Block": 2000,
"homesteadBlock": 0,
"daoForkBlock": 0,
"byzantiumBlock" : 2000,
}
var blnum = ctx['blocknumber']
if (blnum > 2463000){
// Tangerine: disable
config["eip150Block"] = 0;
}
if (blnum > 2675000){
// Spurious
config["eip155Block"] = 0;
config["eip158Block"] = 0;
}
if (blnum > 4370000){
//Byzantium
config["byzantiumBlock"] = 0;
}
genesis ={
nonce: "0x0000000000000000",
difficulty: "0x020000",
mixhash: "0x0000000000000000000000000000000000000000000000000000000000000000",
coinbase: "0x0000000000000000000000000000000000000000",
timestamp: "0x00",
number: "0x00",
parentHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
extraData: "0x0000000000000000000000000000000000000000000000000000000000000000",
gasLimit: toHex(ctx['gas'].Bytes()),
alloc: this.prestate,
config: config,
};
return genesis;
},
step: function(log, db) {
if (!this.prestate){
// Add the current account
this.prestate = {};
// Balance will potentially be wrong here,
// since this will include the value sent
// along with the message
// We fix that in 'result()'
this.lookupAccount(log.account,db);
}
switch(log.op.toString()){
case "EXTCODECOPY":
case "EXTCODESIZE":
case "BALANCE":
var addr = log.peek(0).Text(16);
this.lookupAccount(addr,db);
break;
case "CALL":
case "CALLCODE":
case "DELEGATECALL":
case "STATICCALL":
var addr = log.stack.peek(1).Text(16);
this.lookupAccount(addr,db);
break;
case 'SSTORE':
case 'SLOAD':
var key = log.stack.peek(0).Text(16);
this.lookupStorage(log.account.Text(16), key,db);
break;
}
}
}
/** TODO
Missing BLOCKNUM
Somehow get 'origin'-account in there.
**/