Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add request limits for JSON-RPC batch calls #277

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ type httpServer struct {
}

const (
shutdownTimeout = 5 * time.Second
shutdownTimeout = 5 * time.Second
batchRequestLimit = 5
batchResponseMaxSize = 5 * 1000 * 1000 // 5 MB
)

func NewHTTPServer(logger zerolog.Logger, cfg *config.Config) *httpServer {
Expand Down Expand Up @@ -107,6 +109,7 @@ func (h *httpServer) EnableRPC(apis []rpc.API) error {

// Create RPC server and handler.
srv := rpc.NewServer()
srv.SetBatchLimits(batchRequestLimit, batchResponseMaxSize)

// Register all the APIs exposed by the services
for _, api := range apis {
Expand Down
100 changes: 92 additions & 8 deletions tests/web3js/eth_non_interactive_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { assert } = require('chai')
const conf = require('./config')
const web3 = conf.web3

it('get chain ID', async() => {
it('get chain ID', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using const instead of let for chainID as it is not reassigned.

- let chainID = await web3.eth.getChainId()
+ const chainID = await web3.eth.getChainId()
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
it('get chain ID', async () => {
it('get chain ID', async () => {
const chainID = await web3.eth.getChainId()

let chainID = await web3.eth.getChainId()
assert.isDefined(chainID)
assert.equal(chainID, 646n)
Expand Down Expand Up @@ -43,7 +43,7 @@ it('get block', async () => {
assert.isNull(no)
})

it('get balance', async() => {
it('get balance', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using const instead of let for wei as it is not reassigned.

- let wei = await web3.eth.getBalance(conf.eoa.address)
+ const wei = await web3.eth.getBalance(conf.eoa.address)

Committable suggestion was skipped due low confidence.

let wei = await web3.eth.getBalance(conf.eoa.address)
assert.isNotNull(wei)

Expand All @@ -54,22 +54,22 @@ it('get balance', async() => {
assert.equal(wei, weiAtBlock)
})

it('get code', async() => {
it('get code', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using const instead of let for code as it is not reassigned.

- let code = await web3.eth.getCode(conf.eoa.address)
+ const code = await web3.eth.getCode(conf.eoa.address)
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
it('get code', async () => {
it('get code', async () => {
const code = await web3.eth.getCode(conf.eoa.address)

let code = await web3.eth.getCode(conf.eoa.address)
assert.equal(code, "0x") // empty
})

it('get coinbase', async() => {
it('get coinbase', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using const instead of let for coinbase as it is not reassigned.

- let coinbase = await web3.eth.getCoinbase()
+ const coinbase = await web3.eth.getCoinbase()
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
it('get coinbase', async () => {
it('get coinbase', async () => {
const coinbase = await web3.eth.getCoinbase()

let coinbase = await web3.eth.getCoinbase()
assert.equal(coinbase, conf.serviceEOA) // e2e configured account
})

it('get gas price', async() => {
it('get gas price', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using const instead of let for gasPrice as it is not reassigned.

- let gasPrice = await web3.eth.getGasPrice()
+ const gasPrice = await web3.eth.getGasPrice()

Committable suggestion was skipped due low confidence.

let gasPrice = await web3.eth.getGasPrice()
assert.equal(gasPrice, 0n) // 0 by default in tests
})

it('get transaction', async() => {
it('get transaction', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using const instead of let for blockTx as it is not reassigned.

- let blockTx = await web3.eth.getTransactionFromBlock(conf.startBlockHeight, 0)
+ const blockTx = await web3.eth.getTransactionFromBlock(conf.startBlockHeight, 0)

Committable suggestion was skipped due low confidence.

let blockTx = await web3.eth.getTransactionFromBlock(conf.startBlockHeight, 0)
assert.isNotNull(blockTx)

Expand All @@ -95,12 +95,12 @@ it('get transaction', async() => {
assert.equal(rcp.gasUsed, tx.gas)
})

it('get mining status', async() => {
it('get mining status', async () => {
let mining = await web3.eth.isMining()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using const instead of let for mining as it is not reassigned.

- let mining = await web3.eth.isMining()
+ const mining = await web3.eth.isMining()
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
let mining = await web3.eth.isMining()
const mining = await web3.eth.isMining()
Tools
Biome

[error] 99-99: This let declares a variable that is only assigned once.

assert.isFalse(mining)
})

it('get syncing status', async() => {
it('get syncing status', async () => {
let height = await web3.eth.getBlockNumber()
assert.equal(height, conf.startBlockHeight)

Expand All @@ -110,3 +110,87 @@ it('get syncing status', async() => {
assert.equal(syncInfo.currentBlock, height)
assert.equal(syncInfo.highestBlock, height)
})

it('can make batch requests', async () => {
let batch = new web3.BatchRequest()
let getBlockNumber = {
jsonrpc: '2.0',
id: 1,
method: 'eth_blockNumber',
params: [],
}
let getChainId = {
jsonrpc: '2.0',
id: 2,
method: 'eth_chainId',
params: [],
}
let getSyncing = {
jsonrpc: '2.0',
id: 3,
method: 'eth_syncing',
params: [],
}
let getNetVersion = {
jsonrpc: '2.0',
id: 4,
method: 'net_version',
params: [],
}
let getBlockTransactionCount = {
jsonrpc: '2.0',
id: 5,
method: 'eth_getBlockTransactionCountByNumber',
params: ['0x2'],
}

batch.add(getBlockNumber)
batch.add(getChainId)
batch.add(getSyncing)
batch.add(getNetVersion)
batch.add(getBlockTransactionCount)

let results = await batch.execute()

assert.deepEqual(
results[0],
{ jsonrpc: '2.0', id: 1, result: '0x3' }
)
assert.deepEqual(
results[1],
{ jsonrpc: '2.0', id: 2, result: '0x286' }
)
assert.deepEqual(
results[2],
{
jsonrpc: '2.0',
id: 3,
result: { startingBlock: '0x3', currentBlock: '0x3', highestBlock: '0x3' }
}
)
assert.deepEqual(
results[3],
{ jsonrpc: '2.0', id: 4, result: '545' }
)
assert.deepEqual(
results[4],
{ jsonrpc: '2.0', id: 5, result: '0x1' }
)

// The maximum number of batch requests is 5,
// so this next batch should fail.
let getTransactionCount = {
jsonrpc: '2.0',
id: 6,
method: 'eth_getTransactionCount',
params: ['0x658Bdf435d810C91414eC09147DAA6DB62406379', 'latest'],
}

batch.add(getTransactionCount)

try {
results = await batch.execute()
} catch (error) {
assert.equal(error.innerError[0].message, 'batch too large')
}
})
Loading