Skip to content

Commit

Permalink
fast format
Browse files Browse the repository at this point in the history
  • Loading branch information
Kang committed Jan 6, 2025
1 parent 4b2be25 commit f0de639
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 8 deletions.
11 changes: 7 additions & 4 deletions src/rpc/cfx.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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 @@ -188,11 +189,12 @@ class CFX extends RPCMethodFactory {
{
method: 'cfx_getBlockByHashWithPivotAssumption',
requestFormatters: [
format.blockHash,
format.blockHash,
v => v, // format.blockHash,
v => v, // blockHash,
format.epochNumber,
],
responseFormatter: cfxFormat.block,
// responseFormatter: cfxFormat.block,
responseFormatter: fastFormatBlock,
},
{
method: 'cfx_getConfirmationRiskByHash',
Expand Down Expand Up @@ -603,7 +605,8 @@ class CFX extends RPCMethodFactory {
...extra,
],
});
return cfxFormat.epochReceipts(result);
// return cfxFormat.epochReceipts(result);
return fastFormatEpochReceipts(result);
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/rpc/trace.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const RPCMethodFactory = require('./index');
const format = require('../util/format');
const { fastFormatBlockTraces } = require('./types/fastFormatter');

/**
* @typedef {Object} ActionCall
Expand Down Expand Up @@ -174,10 +175,11 @@ class Trace extends RPCMethodFactory {
{
method: 'trace_block',
alias: 'traceBlock',
requestFormatters: [
format.blockHash,
],
responseFormatter: format.blockTraces,
// requestFormatters: [
// format.blockHash,
// ],
// responseFormatter: format.blockTraces,
responseFormatter: fastFormatBlockTraces,
},
{
method: 'trace_transaction',
Expand Down
91 changes: 91 additions & 0 deletions src/rpc/types/fastFormatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
function maybeBigInt(v, prop) {
if (v[prop] === undefined) {
return;
}
v[prop] = BigInt(v[prop]);
}

function fastFormatAction(v) {
maybeBigInt(v.action, 'value');
maybeBigInt(v.action, 'gas');
maybeBigInt(v.action, 'gasLeft');
maybeBigInt(v, 'epochNumber');
maybeBigInt(v, 'transactionPosition');
return v;
}

function fastFormatTxTraces(v) {
v.traces.forEach(fastFormatAction);
v.transactionPosition = BigInt(v.transactionPosition);
return v;
}

function fastFormatBlockTraces(v) {
v.transactionTraces.forEach(fastFormatTxTraces);
v.epochNumber = parseInt10(v.epochNumber);

return v;
}
function parseInt10(v) {
return parseInt(v, 10);
}
// receipt
function fastFormatReceipt(v) {
v.type = v.type ? parseInt10(v.type) : null;
v.index = v.index ? parseInt10(v.index) : null;
v.epochNumber = BigInt(v.epochNumber);
v.outcomeStatus = v.outcomeStatus ? parseInt10(v.outcomeStatus) : null;
v.gasUsed = BigInt(v.gasUsed);
v.effectiveGasPrice = v.effectiveGasPrice ? BigInt(v.effectiveGasPrice) : null;
v.burntGasFee = v.burntGasFee ? BigInt(v.burntGasFee) : null;
v.gasFee = BigInt(v.gasFee);
v.storageCollateralized = BigInt(v.storageCollateralized);
if (v.storageReleased) {
v.storageReleased.forEach(r => {
r.collaterals = BigInt(r.collaterals);
});
}
}

function fastFormatEpochReceipts(v2d) {
v2d.forEach(v => {
v.forEach(fastFormatReceipt);
});

return v2d;
}

function fastFormatTx(v) {
v.type = v.type ? parseInt10(v.type) : null;
v.nonce = BigInt(v.nonce);
v.gasPrice = v.gasPrice ? BigInt(v.gasPrice) : null;
v.maxPriorityFeePerGas = v.maxPriorityFeePerGas ? BigInt(v.maxPriorityFeePerGas) : null;
v.maxFeePerGas = v.maxFeePerGas ? BigInt(v.maxFeePerGas) : null;
v.gas = BigInt(v.gas);
v.value = BigInt(v.value);
v.storageLimit = BigInt(v.storageLimit);
v.epochHeight = BigInt(v.epochHeight);
v.chainId = parseInt10(v.chainId);
v.v = parseInt10(v.v);
v.yParity = v.yParity ? parseInt10(v.yParity) : null;
v.status = v.status ? parseInt10(v.status) : null;
v.transactionIndex = v.transactionIndex ? parseInt10(v.transactionIndex) : null;
}

function fastFormatBlock(v) {
v.baseFeePerGas = v.baseFeePerGas ? BigInt(v.baseFeePerGas) : null;
v.epochNumber = v.epochNumber ? parseInt10(v.epochNumber) : null;
v.blockNumber = v.blockNumber ? parseInt10(v.blockNumber) : null;
v.blame = parseInt10(v.blame);
v.height = parseInt10(v.height);
v.size = parseInt10(v.size);
v.timestamp = parseInt10(v.timestamp);
maybeBigInt(v, 'gasLimit');
maybeBigInt(v, 'gasUsed');
maybeBigInt(v, 'difficulty');
v.transactions.forEach(fastFormatTx);

return v;
}

module.exports = { fastFormatBlockTraces, fastFormatEpochReceipts, fastFormatBlock };

0 comments on commit f0de639

Please sign in to comment.