Skip to content

Commit

Permalink
rpc patch entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Kang committed Jan 7, 2025
1 parent da3e266 commit 9ec9428
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 14 deletions.
11 changes: 4 additions & 7 deletions src/rpc/cfx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -605,8 +603,7 @@ class CFX extends RPCMethodFactory {
...extra,
],
});
// return cfxFormat.epochReceipts(result);
return fastFormatEpochReceipts(result);
return cfxFormat.epochReceipts(result);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const format = require('../util/format');
const { rpcPatch } = require('./rpcPatch');

class RPCMethodFactory {
constructor(conflux, methods = []) {
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions src/rpc/rpcPatch.js
Original file line number Diff line number Diff line change
@@ -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 };
10 changes: 4 additions & 6 deletions src/rpc/trace.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const RPCMethodFactory = require('./index');
const format = require('../util/format');
const { fastFormatBlockTraces } = require('./types/fastFormatter');

/**
* @typedef {Object} ActionCall
Expand Down Expand Up @@ -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',
Expand Down
25 changes: 24 additions & 1 deletion src/rpc/types/fastFormatter.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const cfxFormat = require('./formatter');

function maybeBigInt(v, prop) {
if (v[prop] === undefined) {
return;
Expand Down Expand Up @@ -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 };

0 comments on commit 9ec9428

Please sign in to comment.