|
| 1 | +import { assert, describe, it } from 'vitest' |
| 2 | + |
| 3 | +import { createFeeMarket1559Tx, createLegacyTx } from '@ethereumjs/tx' |
| 4 | +import type { TypedTransaction } from '@ethereumjs/tx' |
| 5 | +import { hexToBigInt } from '@ethereumjs/util' |
| 6 | +import { createClient, createManager, getRPCClient, startRPC } from '../helpers.ts' |
| 7 | + |
| 8 | +const method = 'eth_getMaxPriorityFeePerGas' |
| 9 | + |
| 10 | +type BlocksMPF = bigint[][] |
| 11 | + |
| 12 | +interface Block { |
| 13 | + transactions: TypedTransaction[] |
| 14 | + hash: () => Uint8Array |
| 15 | +} |
| 16 | +/** |
| 17 | + * @param blockMPF - Representing txs with the respective maxPriorityFeePerGas values |
| 18 | + * @param non1559Txs - Number of non-1559 txs to add to the block |
| 19 | + * @returns A block representation with the transactions and a hash |
| 20 | + */ |
| 21 | +function createBlock(blockMPF: bigint[], non1559Txs = 0): Block { |
| 22 | + const transactions: TypedTransaction[] = [] |
| 23 | + for (const mpf of blockMPF) { |
| 24 | + const tx = createFeeMarket1559Tx({ |
| 25 | + maxFeePerGas: mpf, // Side addition to satisfy tx creation |
| 26 | + maxPriorityFeePerGas: mpf, |
| 27 | + }) |
| 28 | + transactions.push(tx) |
| 29 | + } |
| 30 | + for (let i = 0; i < non1559Txs; i++) { |
| 31 | + const tx = createLegacyTx({}) |
| 32 | + transactions.push(tx) |
| 33 | + } |
| 34 | + |
| 35 | + return { |
| 36 | + transactions, |
| 37 | + hash: () => new Uint8Array([1]), |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function createChain(blocksMPF: BlocksMPF = [[]], non1559Txs = 0) { |
| 42 | + if (blocksMPF.length === 0) { |
| 43 | + throw new Error('call with minimum 1 block') |
| 44 | + } |
| 45 | + |
| 46 | + const blocks: Block[] = [] |
| 47 | + |
| 48 | + for (const blockMPF of blocksMPF) { |
| 49 | + const block = createBlock(blockMPF, non1559Txs) |
| 50 | + blocks.push(block) |
| 51 | + } |
| 52 | + const latest = blocks[blocks.length - 1] |
| 53 | + return { |
| 54 | + getCanonicalHeadBlock: () => latest, |
| 55 | + getBlocks: () => blocks.reverse(), // needs to be returned in reverse order to simulate reverse flag |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +async function getSetup(blocksMPF: BlocksMPF, non1559Txs = 0) { |
| 60 | + const client = await createClient({ chain: createChain(blocksMPF, non1559Txs) }) |
| 61 | + client.config.synchronized = true |
| 62 | + const manager = createManager(client) |
| 63 | + const rpcServer = startRPC(manager.getMethods()) |
| 64 | + const rpc = getRPCClient(rpcServer) |
| 65 | + return { client, manager, rpcServer, rpc } |
| 66 | +} |
| 67 | + |
| 68 | +describe(method, () => { |
| 69 | + it('should return 0 for a simple block with no transactions', async () => { |
| 70 | + const blocksMPF = [[]] |
| 71 | + const { rpc } = await getSetup(blocksMPF) |
| 72 | + const res = await rpc.request(method, []) |
| 73 | + assert.strictEqual(hexToBigInt(res.result), 0n) |
| 74 | + }) |
| 75 | + |
| 76 | + it('should return "100" for a simple block with one 1559 tx', async () => { |
| 77 | + const blocksMPF = [[100n]] |
| 78 | + const { rpc } = await getSetup(blocksMPF) |
| 79 | + const res = await rpc.request(method, []) |
| 80 | + assert.strictEqual(hexToBigInt(res.result), 100n) |
| 81 | + }) |
| 82 | + |
| 83 | + it('should return 0 for a simple block with one non-1559 tx', async () => { |
| 84 | + const blocksMPF = [[]] |
| 85 | + const { rpc } = await getSetup(blocksMPF, 1) |
| 86 | + const res = await rpc.request(method, []) |
| 87 | + assert.strictEqual(hexToBigInt(res.result), 0n) |
| 88 | + }) |
| 89 | + |
| 90 | + it('should return "100" for two simple blocks with one 1559 tx', async () => { |
| 91 | + const blocksMPF = [[100n], [100n]] |
| 92 | + const { rpc } = await getSetup(blocksMPF) |
| 93 | + const res = await rpc.request(method, []) |
| 94 | + assert.strictEqual(hexToBigInt(res.result), 100n) |
| 95 | + }) |
| 96 | + |
| 97 | + it('should return median for two transactions', async () => { |
| 98 | + const blocksMPF = [[100n, 200n]] |
| 99 | + const { rpc } = await getSetup(blocksMPF) |
| 100 | + const res = await rpc.request(method, []) |
| 101 | + assert.strictEqual(hexToBigInt(res.result), 150n) |
| 102 | + }) |
| 103 | + |
| 104 | + it('should return median for three transactions', async () => { |
| 105 | + const blocksMPF = [[100n, 200n, 300n]] |
| 106 | + const { rpc } = await getSetup(blocksMPF) |
| 107 | + const res = await rpc.request(method, []) |
| 108 | + assert.strictEqual(hexToBigInt(res.result), 200n) |
| 109 | + }) |
| 110 | + |
| 111 | + it('should apply linear regression - clear trend', async () => { |
| 112 | + const blocksMPF = [[100n], [200n]] |
| 113 | + const { rpc } = await getSetup(blocksMPF) |
| 114 | + const res = await rpc.request(method, []) |
| 115 | + assert.strictEqual(hexToBigInt(res.result), 300n) |
| 116 | + }) |
| 117 | + |
| 118 | + it('should apply linear regression - mixed upwards trend (simple)', async () => { |
| 119 | + const blocksMPF = [[100n], [200n], [150n]] |
| 120 | + const { rpc } = await getSetup(blocksMPF) |
| 121 | + const res = await rpc.request(method, []) |
| 122 | + assert.strictEqual(hexToBigInt(res.result), 200n) |
| 123 | + }) |
| 124 | + |
| 125 | + it('should apply linear regression - mixed upwards trend', async () => { |
| 126 | + const blocksMPF = [[300n], [200n], [250n], [400n], [700n], [200n]] |
| 127 | + const { rpc } = await getSetup(blocksMPF) |
| 128 | + const res = await rpc.request(method, []) |
| 129 | + assert.strictEqual(hexToBigInt(res.result), 457n) |
| 130 | + }) |
| 131 | + |
| 132 | + it('should apply median + linear regression in more complex scenarios', async () => { |
| 133 | + const blocksMPF = [ |
| 134 | + [300n, 10n], |
| 135 | + [200n, 20n], |
| 136 | + [250n, 30n], |
| 137 | + [400n, 40n, 100n], |
| 138 | + [700n], |
| 139 | + [200n, 10n], |
| 140 | + ] |
| 141 | + const { rpc } = await getSetup(blocksMPF) |
| 142 | + const res = await rpc.request(method, []) |
| 143 | + assert.strictEqual(hexToBigInt(res.result), 366n) |
| 144 | + }) |
| 145 | +}) |
0 commit comments