|
| 1 | +import { Common, Hardfork, Mainnet } from '@ethereumjs/common' |
| 2 | +import { CLRequestType, createCLRequest, equalsBytes, hexToBytes } from '@ethereumjs/util' |
| 3 | +import { sha256 } from 'ethereum-cryptography/sha256.js' |
| 4 | +import { assert, describe, it } from 'vitest' |
| 5 | + |
| 6 | +import { createBlock, genRequestsRoot } from '../src/index.ts' |
| 7 | + |
| 8 | +import type { CLRequest } from '@ethereumjs/util' |
| 9 | + |
| 10 | +describe('[Block]: CLRequests tests', () => { |
| 11 | + // Common with EIP-7685 enabled (CLRequests) |
| 12 | + const common = new Common({ chain: Mainnet, hardfork: Hardfork.Cancun, eips: [7685] }) |
| 13 | + |
| 14 | + function createDepositRequest(): CLRequest<CLRequestType> { |
| 15 | + // Example deposit data |
| 16 | + const sampleDepositRequest = hexToBytes( |
| 17 | + '0x00ac842878bb70009552a4cfcad801d6e659c50bd50d7d03306790cb455ce7363c5b6972f0159d170f625a99b2064dbefc010000000000000000000000818ccb1c4eda80270b04d6df822b1e72dd83c3030040597307000000a747f75c72d0cf0d2b52504c7385b516f0523e2f0842416399f42b4aee5c6384a5674f6426b1cc3d0827886fa9b909e616f5c9f61f986013ed2b9bf37071cbae951136265b549f44e3c8e26233c0433e9124b7fd0dc86e82f9fedfc0a179d7690000000000000000', |
| 18 | + ) |
| 19 | + return createCLRequest(sampleDepositRequest) |
| 20 | + } |
| 21 | + |
| 22 | + function createWithdrawalRequest(): CLRequest<CLRequestType> { |
| 23 | + // Type 1 (Withdrawal) + example data |
| 24 | + const withdrawalData = hexToBytes( |
| 25 | + '0x01000000000000000000000000000000000000000001000000000000000000000de0b6b3a7640000', |
| 26 | + ) |
| 27 | + return createCLRequest(withdrawalData) |
| 28 | + } |
| 29 | + |
| 30 | + function createConsolidationRequest(): CLRequest<CLRequestType> { |
| 31 | + // Type 2 (Consolidation) + example data |
| 32 | + const consolidationData = hexToBytes('0x020000000100000000000000000000000000000000000001') |
| 33 | + return createCLRequest(consolidationData) |
| 34 | + } |
| 35 | + |
| 36 | + it('should create a block with deposit request', () => { |
| 37 | + const depositRequest = createDepositRequest() |
| 38 | + assert.equal(depositRequest.type, CLRequestType.Deposit, 'should be a deposit request') |
| 39 | + |
| 40 | + const requestsHash = genRequestsRoot([depositRequest], sha256) |
| 41 | + const block = createBlock( |
| 42 | + { |
| 43 | + header: { requestsHash }, |
| 44 | + }, |
| 45 | + { common }, |
| 46 | + ) |
| 47 | + |
| 48 | + assert.isDefined(block.header.requestsHash, 'block should have requestsHash') |
| 49 | + assert.isTrue( |
| 50 | + equalsBytes(block.header.requestsHash!, requestsHash), |
| 51 | + 'requestsHash should match the expected value', |
| 52 | + ) |
| 53 | + }) |
| 54 | + |
| 55 | + it('should create a block with withdrawal request', () => { |
| 56 | + const withdrawalRequest = createWithdrawalRequest() |
| 57 | + assert.equal(withdrawalRequest.type, CLRequestType.Withdrawal, 'should be a withdrawal request') |
| 58 | + |
| 59 | + const requestsHash = genRequestsRoot([withdrawalRequest], sha256) |
| 60 | + const block = createBlock( |
| 61 | + { |
| 62 | + header: { requestsHash }, |
| 63 | + }, |
| 64 | + { common }, |
| 65 | + ) |
| 66 | + |
| 67 | + assert.isDefined(block.header.requestsHash, 'block should have requestsHash') |
| 68 | + assert.isTrue( |
| 69 | + equalsBytes(block.header.requestsHash!, requestsHash), |
| 70 | + 'requestsHash should match the expected value', |
| 71 | + ) |
| 72 | + }) |
| 73 | + |
| 74 | + it('should create a block with consolidation request', () => { |
| 75 | + const consolidationRequest = createConsolidationRequest() |
| 76 | + assert.equal( |
| 77 | + consolidationRequest.type, |
| 78 | + CLRequestType.Consolidation, |
| 79 | + 'should be a consolidation request', |
| 80 | + ) |
| 81 | + |
| 82 | + const requestsHash = genRequestsRoot([consolidationRequest], sha256) |
| 83 | + const block = createBlock( |
| 84 | + { |
| 85 | + header: { requestsHash }, |
| 86 | + }, |
| 87 | + { common }, |
| 88 | + ) |
| 89 | + |
| 90 | + assert.isDefined(block.header.requestsHash, 'block should have requestsHash') |
| 91 | + assert.isTrue( |
| 92 | + equalsBytes(block.header.requestsHash!, requestsHash), |
| 93 | + 'requestsHash should match the expected value', |
| 94 | + ) |
| 95 | + }) |
| 96 | + |
| 97 | + it('should create a block with multiple CLRequests', () => { |
| 98 | + const depositRequest = createDepositRequest() |
| 99 | + const withdrawalRequest = createWithdrawalRequest() |
| 100 | + const consolidationRequest = createConsolidationRequest() |
| 101 | + |
| 102 | + // Requests should be sorted by type |
| 103 | + const requests = [depositRequest, withdrawalRequest, consolidationRequest] |
| 104 | + const requestsHash = genRequestsRoot(requests, sha256) |
| 105 | + |
| 106 | + const block = createBlock( |
| 107 | + { |
| 108 | + header: { requestsHash }, |
| 109 | + }, |
| 110 | + { common }, |
| 111 | + ) |
| 112 | + |
| 113 | + assert.isDefined(block.header.requestsHash, 'block should have requestsHash') |
| 114 | + assert.isTrue( |
| 115 | + equalsBytes(block.header.requestsHash!, requestsHash), |
| 116 | + 'requestsHash should match the expected value', |
| 117 | + ) |
| 118 | + }) |
| 119 | + |
| 120 | + it('should validate the requests are sorted by type', () => { |
| 121 | + const depositRequest = createDepositRequest() |
| 122 | + const withdrawalRequest = createWithdrawalRequest() |
| 123 | + |
| 124 | + // Requests in wrong order should throw |
| 125 | + const requests = [withdrawalRequest, depositRequest] |
| 126 | + |
| 127 | + assert.throws( |
| 128 | + () => genRequestsRoot(requests, sha256), |
| 129 | + 'requests are not sorted in ascending order', |
| 130 | + 'should throw when requests are not sorted by type', |
| 131 | + ) |
| 132 | + }) |
| 133 | +}) |
0 commit comments