Merge pull request #3492 from ethcore/jg-trace-apis

Add trace_{call, rawTransaction, replayTransaction}
This commit is contained in:
Gav Wood 2016-11-17 21:56:19 +08:00 committed by GitHub
commit 9c8b7b25c9
6 changed files with 161 additions and 33 deletions

View File

@ -166,3 +166,11 @@ export function inTraceFilter (filterObject) {
return filterObject; return filterObject;
} }
export function inTraceType (whatTrace) {
if (isString(whatTrace)) {
return [whatTrace];
}
return whatTrace;
}

View File

@ -16,7 +16,7 @@
import BigNumber from 'bignumber.js'; import BigNumber from 'bignumber.js';
import { inAddress, inBlockNumber, inData, inFilter, inHex, inNumber10, inNumber16, inOptions } from './input'; import { inAddress, inBlockNumber, inData, inFilter, inHex, inNumber10, inNumber16, inOptions, inTraceType } from './input';
import { isAddress } from '../../../test/types'; import { isAddress } from '../../../test/types';
describe('api/format/input', () => { describe('api/format/input', () => {
@ -242,4 +242,16 @@ describe('api/format/input', () => {
}); });
}); });
}); });
describe('inTraceType', () => {
it('returns array of types as is', () => {
const types = ['vmTrace', 'trace', 'stateDiff'];
expect(inTraceType(types)).to.deep.equal(types);
});
it('formats single string type into array', () => {
const type = 'vmTrace';
expect(inTraceType(type)).to.deep.equal([type]);
});
});
}); });

View File

@ -254,3 +254,25 @@ export function outTrace (trace) {
return trace; return trace;
} }
export function outTraces (traces) {
if (traces) {
return traces.map(outTrace);
}
return traces;
}
export function outTraceReplay (trace) {
if (trace) {
Object.keys(trace).forEach((key) => {
switch (key) {
case 'trace':
trace[key] = outTraces(trace[key]);
break;
}
});
}
return trace;
}

View File

@ -20,15 +20,25 @@ describe('ethapi.trace', () => {
const ethapi = createHttpApi(); const ethapi = createHttpApi();
describe('block', () => { describe('block', () => {
it('returns the latest block', () => { it('returns the latest block traces', () => {
return ethapi.trace.block().then((block) => { return ethapi.trace.block().then((traces) => {
expect(block).to.be.ok; expect(traces).to.be.ok;
}); });
}); });
it('returns a specified block', () => { it('returns traces for a specified block', () => {
return ethapi.trace.block('0x65432').then((block) => { return ethapi.trace.block('0x65432').then((traces) => {
expect(block).to.be.ok; expect(traces).to.be.ok;
});
});
});
describe('replayTransaction', () => {
it('returns traces for a specific transaction', () => {
return ethapi.eth.getBlockByNumber().then((latestBlock) => {
return ethapi.trace.replayTransaction(latestBlock.transactions[0]).then((traces) => {
expect(traces).to.be.ok;
});
}); });
}); });
}); });

View File

@ -14,35 +14,53 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { inBlockNumber, inHex, inNumber16, inTraceFilter } from '../../format/input'; import { inBlockNumber, inData, inHex, inNumber16, inOptions, inTraceFilter, inTraceType } from '../../format/input';
import { outTrace } from '../../format/output'; import { outTraces, outTraceReplay } from '../../format/output';
export default class Trace { export default class Trace {
constructor (transport) { constructor (transport) {
this._transport = transport; this._transport = transport;
} }
block (blockNumber = 'latest') {
return this._transport
.execute('trace_block', inBlockNumber(blockNumber))
.then(outTraces);
}
call (options, blockNumber = 'latest', whatTrace = ['trace']) {
return this._transport
.execute('trace_call', inOptions(options), inBlockNumber(blockNumber), inTraceType(whatTrace))
.then(outTraceReplay);
}
filter (filterObj) { filter (filterObj) {
return this._transport return this._transport
.execute('trace_filter', inTraceFilter(filterObj)) .execute('trace_filter', inTraceFilter(filterObj))
.then(traces => traces.map(trace => outTrace(trace))); .then(outTraces);
} }
get (txHash, position) { get (txHash, position) {
return this._transport return this._transport
.execute('trace_get', inHex(txHash), inNumber16(position)) .execute('trace_get', inHex(txHash), inNumber16(position))
.then(trace => outTrace(trace)); .then(outTraces);
}
rawTransaction (data, whatTrace = ['trace']) {
return this._transport
.execute('trace_rawTransaction', inData(data), inTraceType(whatTrace))
.then(outTraceReplay);
}
replayTransaction (txHash, whatTrace = ['trace']) {
return this._transport
.execute('trace_replayTransaction', txHash, inTraceType(whatTrace))
.then(outTraceReplay);
} }
transaction (txHash) { transaction (txHash) {
return this._transport return this._transport
.execute('trace_transaction', inHex(txHash)) .execute('trace_transaction', inHex(txHash))
.then(traces => traces.map(trace => outTrace(trace))); .then(outTraces);
}
block (blockNumber = 'latest') {
return this._transport
.execute('trace_block', inBlockNumber(blockNumber))
.then(traces => traces.map(trace => outTrace(trace)));
} }
} }

View File

@ -14,9 +14,45 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { BlockNumber, Hash, Integer } from '../types'; import { BlockNumber, Data, Hash, Integer } from '../types';
export default { export default {
block: {
desc: 'Returns traces created at given block',
params: [
{
type: BlockNumber,
desc: 'Integer block number, or \'latest\' for the last mined block or \'pending\', \'earliest\' for not yet mined transactions'
}
],
returns: {
type: Array,
desc: 'Block traces'
}
},
call: {
desc: 'Returns traces for a specific call',
params: [
{
type: Object,
desc: 'Call options'
},
{
type: BlockNumber,
desc: 'The blockNumber'
},
{
type: Array,
desc: 'Type of trace, one or more of \'vmTrace\', \'trace\' and/or \'stateDiff\''
}
],
returns: {
type: Array,
desc: 'Block traces'
}
},
filter: { filter: {
desc: 'Returns traces matching given filter', desc: 'Returns traces matching given filter',
params: [ params: [
@ -49,6 +85,42 @@ export default {
} }
}, },
rawTransaction: {
desc: 'Traces a call to eth_sendRawTransaction without making the call, returning the traces',
params: [
{
type: Data,
desc: 'Transaction data'
},
{
type: Array,
desc: 'Type of trace, one or more of \'vmTrace\', \'trace\' and/or \'stateDiff\''
}
],
returns: {
type: Array,
desc: 'Block traces'
}
},
replayTransaction: {
desc: 'Replays a transaction, returning the traces',
params: [
{
type: Hash,
desc: 'Transaction hash'
},
{
type: Array,
desc: 'Type of trace, one or more of \'vmTrace\', \'trace\' and/or \'stateDiff\''
}
],
returns: {
type: Array,
desc: 'Block traces'
}
},
transaction: { transaction: {
desc: 'Returns all traces of given transaction', desc: 'Returns all traces of given transaction',
params: [ params: [
@ -61,19 +133,5 @@ export default {
type: Array, type: Array,
desc: 'Traces of given transaction' desc: 'Traces of given transaction'
} }
},
block: {
desc: 'Returns traces created at given block',
params: [
{
type: BlockNumber,
desc: 'Integer block number, or \'latest\' for the last mined block or \'pending\', \'earliest\' for not yet mined transactions'
}
],
returns: {
type: Array,
desc: 'Block traces'
}
} }
}; };