-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* RequestManagerMiddleware * adding in context * request manager support * tests * lint fix * lint fix * sample middleware * middleware in sample plugin * middleware test with plugin * lint fix * updated Web3Middleware * extensible middleware * test update * lint update * update * lint fix
- Loading branch information
Showing
8 changed files
with
356 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
packages/web3-core/test/unit/web3_middleware_request_manager.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
This file is part of web3.js. | ||
web3.js is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
web3.js is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with web3.js. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { EthExecutionAPI, JsonRpcResponse, Web3APIMethod, Web3APIRequest, Web3APIReturnType } from 'web3-types'; | ||
import { jsonRpc } from 'web3-utils'; | ||
import { RequestManagerMiddleware } from '../../src/types'; | ||
import { Web3RequestManager } from '../../src/web3_request_manager'; | ||
|
||
class Web3Middleware<API> implements RequestManagerMiddleware<API> { | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
public async processRequest<Method extends Web3APIMethod<API>>( | ||
request: Web3APIRequest<API, Method> | ||
): Promise<Web3APIRequest<API, Method>> { | ||
// Implement the processRequest logic here | ||
|
||
let requestObj = {...request}; | ||
if (request.method === 'eth_call' && Array.isArray(request.params)) { | ||
requestObj = { | ||
...requestObj, | ||
params: [...request.params, '0x0', '0x1'], | ||
}; | ||
} | ||
|
||
return Promise.resolve(requestObj); | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
public async processResponse< | ||
Method extends Web3APIMethod<API>, | ||
ResponseType = Web3APIReturnType<API, Method> | ||
>( | ||
response: JsonRpcResponse<ResponseType> | ||
): Promise<JsonRpcResponse<ResponseType>> { | ||
|
||
let responseObj = {...response}; | ||
if (!jsonRpc.isBatchResponse(responseObj) && responseObj.id === 1) { | ||
responseObj = { | ||
...responseObj, | ||
result: '0x6a756e616964' as any, | ||
}; | ||
} | ||
|
||
return Promise.resolve(responseObj); | ||
} | ||
} | ||
|
||
describe('Request Manager Middleware', () => { | ||
let requestManagerMiddleware: RequestManagerMiddleware<EthExecutionAPI>; | ||
|
||
beforeAll(() => { | ||
requestManagerMiddleware = { | ||
processRequest: jest.fn(async <Method extends Web3APIMethod<EthExecutionAPI>>(request: Web3APIRequest<EthExecutionAPI, Method>) => request), | ||
processResponse: jest.fn(async <Method extends Web3APIMethod<EthExecutionAPI>, ResponseType = Web3APIReturnType<EthExecutionAPI, Method>>(response: JsonRpcResponse<ResponseType>) => response), | ||
}; | ||
|
||
}); | ||
|
||
it('should set requestManagerMiddleware via constructor', () => { | ||
const web3RequestManager1: Web3RequestManager = new Web3RequestManager<EthExecutionAPI>(undefined, true, requestManagerMiddleware); | ||
|
||
expect(web3RequestManager1.middleware).toBeDefined(); | ||
expect(web3RequestManager1.middleware).toEqual(requestManagerMiddleware); | ||
}); | ||
|
||
it('should set requestManagerMiddleware via set method', () => { | ||
|
||
const middleware2: RequestManagerMiddleware<EthExecutionAPI> = new Web3Middleware<EthExecutionAPI>(); | ||
const web3RequestManager2: Web3RequestManager = new Web3RequestManager<EthExecutionAPI>('http://localhost:8181'); | ||
web3RequestManager2.setMiddleware(middleware2); | ||
|
||
expect(web3RequestManager2.middleware).toBeDefined(); | ||
expect(web3RequestManager2.middleware).toEqual(middleware2); | ||
}); | ||
|
||
it('should call processRequest and processResponse functions of requestManagerMiddleware', async () => { | ||
|
||
const web3RequestManager3 = new Web3RequestManager<EthExecutionAPI>('http://localhost:8080', true, requestManagerMiddleware ); | ||
|
||
const expectedResponse: JsonRpcResponse<string> = { | ||
jsonrpc: '2.0', | ||
id: 1, | ||
result: '0x0', | ||
}; | ||
|
||
jest.spyOn(web3RequestManager3 as any, '_sendRequest').mockResolvedValue(expectedResponse); | ||
|
||
const request = { | ||
id: 1, | ||
method: 'eth_call', | ||
params: [], | ||
}; | ||
|
||
await web3RequestManager3.send(request); | ||
|
||
expect(requestManagerMiddleware.processRequest).toHaveBeenCalledWith(request); | ||
expect(requestManagerMiddleware.processResponse).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should allow modification of request and response', async () => { | ||
|
||
const middleware3: RequestManagerMiddleware<EthExecutionAPI> = new Web3Middleware<EthExecutionAPI>(); | ||
|
||
const web3RequestManager3 = new Web3RequestManager<EthExecutionAPI>('http://localhost:8080', true, middleware3); | ||
|
||
const expectedResponse: JsonRpcResponse<string> = { | ||
jsonrpc: '2.0', | ||
id: 1, | ||
result: '0x0', | ||
}; | ||
|
||
const mockSendRequest = jest.spyOn(web3RequestManager3 as any, '_sendRequest'); | ||
mockSendRequest.mockResolvedValue(expectedResponse); | ||
|
||
const request = { | ||
id: 1, | ||
method: 'eth_call', | ||
params: ['0x3'], | ||
}; | ||
|
||
const response = await web3RequestManager3.send(request); | ||
expect(response).toBe('0x6a756e616964'); | ||
|
||
expect(mockSendRequest).toHaveBeenCalledWith({ | ||
...request, | ||
params: [...request.params, '0x0', '0x1'], | ||
}); | ||
|
||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
1ab7a6b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark
processingTx
9407
ops/sec (±3.37%
)9301
ops/sec (±4.81%
)0.99
processingContractDeploy
39019
ops/sec (±6.25%
)39129
ops/sec (±7.62%
)1.00
processingContractMethodSend
20315
ops/sec (±6.71%
)19443
ops/sec (±5.19%
)0.96
processingContractMethodCall
40764
ops/sec (±6.10%
)38971
ops/sec (±6.34%
)0.96
abiEncode
45656
ops/sec (±6.92%
)44252
ops/sec (±6.92%
)0.97
abiDecode
30969
ops/sec (±7.99%
)30419
ops/sec (±8.89%
)0.98
sign
1605
ops/sec (±3.74%
)1656
ops/sec (±4.08%
)1.03
verify
378
ops/sec (±0.47%
)373
ops/sec (±0.78%
)0.99
This comment was automatically generated by workflow using github-action-benchmark.