|
5 | 5 | BlockLevelAccessList, |
6 | 6 | createBlockLevelAccessList, |
7 | 7 | createBlockLevelAccessListFromJSON, |
| 8 | + createBlockLevelAccessListFromRLP, |
8 | 9 | } from '../src/bal.ts' |
9 | | -import { bytesToHex } from '../src/bytes.ts' |
| 10 | +import { bytesToHex, hexToBytes } from '../src/bytes.ts' |
10 | 11 | import { KECCAK256_RLP_ARRAY_S } from '../src/constants.ts' |
11 | 12 | import type { PrefixedHexString } from '../src/types.ts' |
12 | 13 | import bal_all_transaction_types from './testdata/bal/bal_all_transaction_types.json' with { |
@@ -124,4 +125,60 @@ describe('JSON', () => { |
124 | 125 | assert.deepEqual(bytesToHex(bal.serialize()), balAllTransactionTypesRLP) |
125 | 126 | assert.deepEqual(bytesToHex(bal.hash()), balAllTransactionTypesHash) |
126 | 127 | }) |
| 128 | + |
| 129 | + it('toJSON() should produce correct JSON from Accesses data', () => { |
| 130 | + // bal_simple and bal_all_transaction_types use even-padded hex, |
| 131 | + // so toJSON() output should match the JSON test data directly. |
| 132 | + let bal = new BlockLevelAccessList(balSimple) |
| 133 | + assert.deepEqual(bal.toJSON(), bal_simple as BALJSONBlockAccessList) |
| 134 | + |
| 135 | + bal = new BlockLevelAccessList(balAllTransactionTypes) |
| 136 | + assert.deepEqual(bal.toJSON(), bal_all_transaction_types as BALJSONBlockAccessList) |
| 137 | + }) |
| 138 | + |
| 139 | + it('toJSON() roundtrip: JSON -> internal -> toJSON()', () => { |
| 140 | + // For already-normalized JSON (even-padded hex), toJSON() output matches input. |
| 141 | + let bal = createBlockLevelAccessListFromJSON(bal_simple as BALJSONBlockAccessList) |
| 142 | + assert.deepEqual(bal.toJSON(), bal_simple as BALJSONBlockAccessList) |
| 143 | + |
| 144 | + bal = createBlockLevelAccessListFromJSON(bal_all_transaction_types as BALJSONBlockAccessList) |
| 145 | + assert.deepEqual(bal.toJSON(), bal_all_transaction_types as BALJSONBlockAccessList) |
| 146 | + |
| 147 | + // bal_empty_block_no_coinbase uses un-normalized hex (e.g. "0x0" vs "0x00"), |
| 148 | + // so direct JSON comparison won't match. Verify semantic roundtrip instead: |
| 149 | + // JSON -> internal -> toJSON() -> internal -> RLP/hash must still match. |
| 150 | + bal = createBlockLevelAccessListFromJSON(bal_empty_block_no_coinbase as BALJSONBlockAccessList) |
| 151 | + const roundtripJSON = bal.toJSON() |
| 152 | + const bal2 = createBlockLevelAccessListFromJSON(roundtripJSON) |
| 153 | + assert.deepEqual(bal2.accesses, balEmptyBlockNoCoinbase) |
| 154 | + assert.deepEqual(bytesToHex(bal2.serialize()), balEmptyBlockNoCoinbaseRLP) |
| 155 | + assert.deepEqual(bytesToHex(bal2.hash()), balEmptyBlockNoCoinbaseHash) |
| 156 | + }) |
| 157 | +}) |
| 158 | + |
| 159 | +describe('RLP', () => { |
| 160 | + it('serialize() should produce correct RLP output', () => { |
| 161 | + let bal = new BlockLevelAccessList(balSimple) |
| 162 | + assert.deepEqual(bytesToHex(bal.serialize()), balSimpleRLP) |
| 163 | + |
| 164 | + bal = new BlockLevelAccessList(balEmptyBlockNoCoinbase) |
| 165 | + assert.deepEqual(bytesToHex(bal.serialize()), balEmptyBlockNoCoinbaseRLP) |
| 166 | + |
| 167 | + bal = new BlockLevelAccessList(balAllTransactionTypes) |
| 168 | + assert.deepEqual(bytesToHex(bal.serialize()), balAllTransactionTypesRLP) |
| 169 | + }) |
| 170 | + |
| 171 | + it('serialize() roundtrip: RLP -> internal -> serialize()', () => { |
| 172 | + let bal = createBlockLevelAccessListFromRLP(hexToBytes(balSimpleRLP)) |
| 173 | + assert.deepEqual(bytesToHex(bal.serialize()), balSimpleRLP) |
| 174 | + assert.deepEqual(bytesToHex(bal.hash()), balSimpleHash) |
| 175 | + |
| 176 | + bal = createBlockLevelAccessListFromRLP(hexToBytes(balEmptyBlockNoCoinbaseRLP)) |
| 177 | + assert.deepEqual(bytesToHex(bal.serialize()), balEmptyBlockNoCoinbaseRLP) |
| 178 | + assert.deepEqual(bytesToHex(bal.hash()), balEmptyBlockNoCoinbaseHash) |
| 179 | + |
| 180 | + bal = createBlockLevelAccessListFromRLP(hexToBytes(balAllTransactionTypesRLP)) |
| 181 | + assert.deepEqual(bytesToHex(bal.serialize()), balAllTransactionTypesRLP) |
| 182 | + assert.deepEqual(bytesToHex(bal.hash()), balAllTransactionTypesHash) |
| 183 | + }) |
127 | 184 | }) |
0 commit comments