Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #72 from trufflesuite/storage-on-tracetransaction
Browse files Browse the repository at this point in the history
Return storage changes in structLogs on debug_traceTransaction, fixes #46
  • Loading branch information
benjamincburns authored Feb 27, 2018
2 parents e70aab9 + 12a0035 commit 6976b6a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
66 changes: 63 additions & 3 deletions lib/blockchain_double.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,13 +677,18 @@ BlockchainDouble.prototype.processTransactionTrace = function(hash, params, call
var tx_hash_currently_processing = "";
var tx_currently_processing = null;

var storageStack = {
currentDepth: -1,
stack: []
}

var returnVal = {
gas: 0,
returnValue: "",
structLogs: []
};

function step_listener(event) {
function step_listener(event, next) {
// See these docs:
// https://github.com/ethereum/go-ethereum/wiki/Management-APIs

Expand Down Expand Up @@ -715,10 +720,14 @@ BlockchainDouble.prototype.processTransactionTrace = function(hash, params, call
stack: event.stack.map(function(item) {
return item.toString("hex"); // non-0x prefixed.
}),
storage: {} // TODO: Figure out storage
storage: {}
}

returnVal.structLogs.push(structLog);
structLog = self.processStorageTrace(structLog, storageStack, event, function(err, structLog) {
if (err) return next(err)
returnVal.structLogs.push(structLog);
next()
});
}

function beforeTx_listener(tx) {
Expand Down Expand Up @@ -800,6 +809,57 @@ BlockchainDouble.prototype.processTransactionTrace = function(hash, params, call
});
};

BlockchainDouble.prototype.processStorageTrace = function(structLog, storageStack, event, callback) {
var self = this;
var name = event.opcode.name;

var argsNum = event.opcode.in;
var args = event.stack.slice(-argsNum)
.map((arg) => to.hex(arg));

if (storageStack.currentDepth > event.depth) {
storageStack.pop()
} if (storageStack.currentDepth < event.depth) {
storageStack.stack.push({})
}

storageStack.currentDepth = event.depth;

var key;
var value;
switch(name) {
case 'SSTORE':
key = to.rpcDataHexString(args[1], 64).replace('0x','')
value = to.rpcDataHexString(args[0], 64).replace('0x','')
// use Object.assign to prevent future steps from overwriting this step's storage values
structLog.storage = Object.assign({}, storageStack.stack[storageStack.currentDepth])

callback(null, structLog)
// assign after callback because this storage change actually takes
// effect _after_ this opcode executes
storageStack.stack[storageStack.currentDepth][key] = value;
break;
case 'SLOAD':
// this one's more fun, we need to get the value the contract is loading from current storage
key = to.rpcDataHexString(args[0], 64).replace('0x','')

self.vm.stateManager.getContractStorage(event.address, args[0], function(err, result) {
if (err) return callback(err);

value = to.rpcDataHexString(result, 64).replace('0x','')
storageStack.stack[storageStack.currentDepth][key] = value;
// use Object.assign to prevent future steps from overwriting this step's storage values
structLog.storage = Object.assign({}, storageStack.stack[storageStack.currentDepth])
callback(null, structLog)
})
break;
default:
// use Object.assign to prevent future steps from overwriting this step's storage values
structLog.storage = Object.assign({}, storageStack.stack[storageStack.currentDepth])
callback(null, structLog)
}
};

BlockchainDouble.prototype.getAccount = function(address, number, callback) {
var self = this;

Expand Down
2 changes: 2 additions & 0 deletions test/DebugContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ pragma solidity ^0.4.2;
// Changes to this file will make tests fail.
contract DebugContract {
uint public value = 5;
uint public otherValue = 5;

function setValue(uint _val) {
value = _val;
otherValue += _val;
}
}
4 changes: 3 additions & 1 deletion test/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ describe("Debug", function() {

assert.equal(lastop.op, "STOP");
assert.equal(lastop.gasCost, 1);
assert.equal(lastop.pc, 131);
assert.equal(lastop.pc, 141);
assert.equal(lastop.storage['0000000000000000000000000000000000000000000000000000000000000000'], '000000000000000000000000000000000000000000000000000000000000001a')
assert.equal(lastop.storage['0000000000000000000000000000000000000000000000000000000000000001'], '000000000000000000000000000000000000000000000000000000000000001f')

accept();
});
Expand Down

0 comments on commit 6976b6a

Please sign in to comment.