Skip to content

Commit

Permalink
Move encoding helpers to trie package
Browse files Browse the repository at this point in the history
  • Loading branch information
scorbajio committed Jul 14, 2023
1 parent d6ccb26 commit dd627d5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
File renamed without changes.
1 change: 1 addition & 0 deletions packages/client/src/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @module sync
*/
export * from './beaconsync'
export * from './encoding'
export * from './fullsync'
export * from './lightsync'
export * from './skeleton'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { hexToBytes } from '@ethereumjs/util'
import { assert, describe, it } from 'vitest'
import * as tape from 'tape'

import {
bytesToNibbles,
compactBytesToNibbles,
nibblesToCompactBytes,
} from '../src/util/encoding.js'
} from '../../src/sync/encoding'

describe('encoding', () => {
it('nibblesToCompactBytes and compactBytesToNibbles should encode hex data correctly', () => {
tape('encoding', async (t) => {
t.test('should encode hex data correctly', (t) => {
const tests = [
{ hex: new Uint8Array(), compact: hexToBytes('0x00') },
{ hex: new Uint8Array([16]), compact: hexToBytes('0x20') },
Expand All @@ -30,14 +30,18 @@ describe('encoding', () => {
},
]

for (let i = 0; i < tests.length; i++) {
const test = tests[i]
assert.equal(JSON.stringify(nibblesToCompactBytes(test.hex)), JSON.stringify(test.compact))
assert.equal(JSON.stringify(compactBytesToNibbles(test.compact)), JSON.stringify(test.hex))
for (const test of tests) {
const nibbles = nibblesToCompactBytes(test.hex)
const compact = compactBytesToNibbles(test.compact)

t.deepEqual(nibbles, test.compact)
t.deepEqual(compact, test.hex)
}

t.end()
})

it('bytesToNibbles should encode data correctly', () => {
t.test('bytesToNibbles should encode data correctly', (t) => {
const tests = [
{ key: new Uint8Array(), hexOut: new Uint8Array([16]) },
{ key: new Uint8Array(), hexOut: new Uint8Array([16]) },
Expand All @@ -55,9 +59,10 @@ describe('encoding', () => {
},
]

for (let i = 0; i < tests.length; i++) {
const test = tests[i]
assert.equal(JSON.stringify(bytesToNibbles(test.key)), JSON.stringify(test.hexOut))
for (const test of tests) {
const nibbles = bytesToNibbles(test.key)
t.deepEqual(nibbles, test.hexOut)
}
t.end()
})
})

0 comments on commit dd627d5

Please sign in to comment.