-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(evm-api): implement
eth_getBlockTransactionCountByNumber
(#797)
* Add action * Remove todo * Register action * Add tests
- Loading branch information
1 parent
ec14683
commit eac22b3
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
packages/api-evm/source/actions/eth-get-block-transaction-count-by-number.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,65 @@ | ||
import { Identifiers } from "@mainsail/contracts"; | ||
import { Validator } from "@mainsail/validation"; | ||
|
||
import { describe, Sandbox } from "../../../test-framework/source"; | ||
import { EthGetBlockTransactionCountByNumber } from "./index.js"; | ||
|
||
describe<{ | ||
sandbox: Sandbox; | ||
action: EthGetBlockTransactionCountByNumber; | ||
validator: Validator; | ||
database: any; | ||
}>("EthGetBlockTransactionCountByHash", ({ beforeEach, it, assert, stub }) => { | ||
beforeEach(async (context) => { | ||
context.database = { | ||
getBlockHeader: async () => undefined, | ||
}; | ||
|
||
context.sandbox = new Sandbox(); | ||
|
||
context.sandbox.app.bind(Identifiers.Database.Service).toConstantValue(context.database); | ||
|
||
context.action = context.sandbox.app.resolve(EthGetBlockTransactionCountByNumber); | ||
context.validator = context.sandbox.app.resolve(Validator); | ||
}); | ||
|
||
it("should have a name", ({ action }) => { | ||
assert.equal(action.name, "eth_getBlockTransactionCountByNumber"); | ||
}); | ||
|
||
it("schema should be array with 0 parameters", ({ action, validator }) => { | ||
validator.addSchema({ | ||
$id: "prefixedHex", | ||
pattern: "^0x[0-9a-f]+$", | ||
type: "string", | ||
}); | ||
validator.addSchema(action.schema); | ||
|
||
assert.undefined(validator.validate("jsonRpc_eth_getBlockTransactionCountByNumber", ["0x0"]).errors); | ||
assert.defined(validator.validate("jsonRpc_eth_getBlockTransactionCountByNumber", ["0x0", ""]).errors); | ||
assert.defined(validator.validate("jsonRpc_eth_getBlockTransactionCountByNumber", [1]).errors); | ||
assert.defined(validator.validate("jsonRpc_eth_getBlockTransactionCountByNumber", {}).errors); | ||
}); | ||
|
||
it("should return null if block not found", async ({ action }) => { | ||
assert.null(await action.handle(["0x10"])); | ||
}); | ||
|
||
it("should return 0x0", async ({ action, database }) => { | ||
const spyGetBlockHeader = stub(database, "getBlockHeader").returnValue({ numberOfTransactions: 0 }); | ||
|
||
assert.equal(await action.handle(["0x0"]), "0x0"); | ||
|
||
spyGetBlockHeader.calledOnce(); | ||
spyGetBlockHeader.calledWith(0); | ||
}); | ||
|
||
it("should return 0x14", async ({ action, database }) => { | ||
const spyGetBlockHeader = stub(database, "getBlockHeader").returnValue({ numberOfTransactions: 20 }); | ||
|
||
assert.equal(await action.handle(["0x14"]), "0x14"); | ||
|
||
spyGetBlockHeader.calledOnce(); | ||
spyGetBlockHeader.calledWith(20); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
packages/api-evm/source/actions/eth-get-block-transaction-count-by-number.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,31 @@ | ||
import { inject, injectable } from "@mainsail/container"; | ||
import { Contracts, Identifiers } from "@mainsail/contracts"; | ||
|
||
@injectable() | ||
export class EthGetBlockTransactionCountByNumber implements Contracts.Api.RPC.Action { | ||
public readonly name: string = "eth_getBlockTransactionCountByNumber"; | ||
|
||
@inject(Identifiers.Database.Service) | ||
private readonly databaseService!: Contracts.Database.DatabaseService; | ||
|
||
public readonly schema = { | ||
$id: `jsonRpc_${this.name}`, | ||
|
||
maxItems: 1, | ||
minItems: 1, | ||
|
||
prefixItems: [{ $ref: "prefixedHex" }], | ||
type: "array", | ||
}; | ||
|
||
public async handle(parameters: [string]): Promise<string | null> { | ||
const block = await this.databaseService.getBlockHeader(Number(parameters[0])); | ||
|
||
if (!block) { | ||
// eslint-disable-next-line unicorn/no-null | ||
return null; | ||
} | ||
|
||
return `0x${block.numberOfTransactions.toString(16)}`; | ||
} | ||
} |
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