diff --git a/src/rpc/cfx.js b/src/rpc/cfx.js index 2af3dbf..e27930a 100644 --- a/src/rpc/cfx.js +++ b/src/rpc/cfx.js @@ -8,7 +8,6 @@ const { decodeCfxAddress, ADDRESS_TYPES } = require('../util/address'); const PendingTransaction = require('../subscribe/PendingTransaction'); const Contract = require('../contract'); const RPCTypes = require('./types/index'); -const { fastFormatEpochReceipts, fastFormatBlock } = require('./types/fastFormatter'); /** * @typedef { import('../Transaction').TransactionMeta } TransactionMeta @@ -189,12 +188,11 @@ class CFX extends RPCMethodFactory { { method: 'cfx_getBlockByHashWithPivotAssumption', requestFormatters: [ - v => v, // format.blockHash, - v => v, // blockHash, + format.blockHash, + format.blockHash, format.epochNumber, ], - // responseFormatter: cfxFormat.block, - responseFormatter: fastFormatBlock, + responseFormatter: cfxFormat.block, }, { method: 'cfx_getConfirmationRiskByHash', @@ -605,8 +603,7 @@ class CFX extends RPCMethodFactory { ...extra, ], }); - // return cfxFormat.epochReceipts(result); - return fastFormatEpochReceipts(result); + return cfxFormat.epochReceipts(result); } /** diff --git a/src/rpc/index.js b/src/rpc/index.js index 00e9319..ccb356c 100644 --- a/src/rpc/index.js +++ b/src/rpc/index.js @@ -1,4 +1,5 @@ const format = require('../util/format'); +const { rpcPatch } = require('./rpcPatch'); class RPCMethodFactory { constructor(conflux, methods = []) { @@ -8,6 +9,7 @@ class RPCMethodFactory { addMethods(methods) { for (const methodMeta of methods) { + rpcPatch(methodMeta); const method = methodMeta.method.split('_')[1]; this[method] = this.createRPCMethod(methodMeta); // create method alias diff --git a/src/rpc/rpcPatch.js b/src/rpc/rpcPatch.js new file mode 100644 index 0000000..98a8596 --- /dev/null +++ b/src/rpc/rpcPatch.js @@ -0,0 +1,19 @@ +/** + * Give a chance to change the RPC behavior. + * For example, you may want to use a different responseFormatter for traceBlock. + * @param rpcDef + */ + +// eslint-disable-next-line no-unused-vars +function emptyPatchRPCMethod(rpcDef) { + // const { method, requestFormatters, responseFormatter } = rpcDef; +} + +let rpcPatch = emptyPatchRPCMethod; + +// set it before initializing a new Conflux instance. +function setPRCMethodPatch(fn) { + rpcPatch = fn; +} + +module.exports = { rpcPatch, setPRCMethodPatch }; diff --git a/src/rpc/trace.js b/src/rpc/trace.js index 1fce2e7..b3d8021 100644 --- a/src/rpc/trace.js +++ b/src/rpc/trace.js @@ -1,6 +1,5 @@ const RPCMethodFactory = require('./index'); const format = require('../util/format'); -const { fastFormatBlockTraces } = require('./types/fastFormatter'); /** * @typedef {Object} ActionCall @@ -175,11 +174,10 @@ class Trace extends RPCMethodFactory { { method: 'trace_block', alias: 'traceBlock', - // requestFormatters: [ - // format.blockHash, - // ], - // responseFormatter: format.blockTraces, - responseFormatter: fastFormatBlockTraces, + requestFormatters: [ + format.blockHash, + ], + responseFormatter: format.blockTraces, }, { method: 'trace_transaction', diff --git a/src/rpc/types/fastFormatter.js b/src/rpc/types/fastFormatter.js index 86a7e6d..ebf8e93 100644 --- a/src/rpc/types/fastFormatter.js +++ b/src/rpc/types/fastFormatter.js @@ -1,3 +1,5 @@ +const cfxFormat = require('./formatter'); + function maybeBigInt(v, prop) { if (v[prop] === undefined) { return; @@ -88,4 +90,25 @@ function fastFormatBlock(v) { return v; } -module.exports = { fastFormatBlockTraces, fastFormatEpochReceipts, fastFormatBlock }; +const nothing = v => v; +// use setPRCMethodPatch(useFastFormat) to enable it. +function useFastFormat(rpcDef) { + cfxFormat.epochReceipts = fastFormatEpochReceipts; + cfxFormat.hex64 = nothing; + const { method } = rpcDef; + switch (method) { + case 'cfx_getBlockByHashWithPivotAssumption': + rpcDef.requestFormatters[0] = undefined; + rpcDef.requestFormatters[1] = undefined; + rpcDef.responseFormatter = fastFormatBlock; + break; + case 'trace_block': + rpcDef.requestFormatters = undefined; + rpcDef.responseFormatter = fastFormatBlockTraces; + break; + default: + break; + } +} + +module.exports = { fastFormatBlockTraces, fastFormatEpochReceipts, fastFormatBlock, useFastFormat };