Skip to content

Commit

Permalink
client: snap protocol tests (#2119)
Browse files Browse the repository at this point in the history
* Add snapprotocol tests

* Add snapsync tests
  • Loading branch information
scorbajio authored Aug 10, 2022
1 parent c1ee333 commit 77ead1b
Show file tree
Hide file tree
Showing 2 changed files with 241 additions and 0 deletions.
175 changes: 175 additions & 0 deletions packages/client/test/net/protocol/snapprotocol.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import * as tape from 'tape'

import { Account, bigIntToBuffer } from '@ethereumjs/util'
import { Chain } from '../../../lib/blockchain'
import { Config } from '../../../lib/config'
import { SnapProtocol } from '../../../lib/net/protocol'

(BigInt.prototype as any).toJSON = function () {return this.toString();};

tape('[SnapProtocol]', (t) => {
t.test('should get properties', (t) => {
const config = new Config({ transports: [] })
const chain = new Chain({ config })
const p = new SnapProtocol({ config, chain })
t.ok(typeof p.name === 'string', 'get name')
t.ok(Array.isArray(p.versions), 'get versions')
t.ok(Array.isArray(p.messages), 'get messages')
t.end()
})

t.test('should open correctly', async (t) => {
const config = new Config({ transports: [] })
const chain = new Chain({ config })
const p = new SnapProtocol({ config, chain })
await p.open()
t.ok(p.opened, 'opened is true')
t.notOk(await p.open(), 'repeat open')
t.end()
})

t.test('verify that GetAccountRange handler encodes/decodes correctly', (t) => {
const config = new Config({ transports: [] })
const chain = new Chain({ config })
const p = new SnapProtocol({ config, chain })
const root = {
number: BigInt(4),
stateRoot: Buffer.from([]),
}
const reqId = BigInt(1)
const origin = Buffer.from(
'0000000000000000000000000000000000000000000000000000000000000000',
'hex'
)
const limit = Buffer.from(
'0000000000000000000000000f00000000000000000000000000000000000010',
'hex'
)
const bytes = BigInt(5000000)

const res = p.decode(p.messages.filter((message) => message.name === 'GetAccountRange')[0], [
reqId,
root,
origin,
limit,
bytes,
])
const res2 = p.encode(p.messages.filter((message) => message.name === 'GetAccountRange')[0], {
reqId: reqId,
root: root,
origin: origin,
limit: limit,
bytes: bytes,
})

t.ok(JSON.stringify(res.reqId) === JSON.stringify(reqId), 'correctly decoded reqId')
t.ok(JSON.stringify(res.root) === JSON.stringify(root), 'correctly decoded root')
t.ok(JSON.stringify(res.origin) === JSON.stringify(origin), 'correctly decoded origin')
t.ok(JSON.stringify(res.limit) === JSON.stringify(limit), 'correctly decoded limit')
t.ok(JSON.stringify(res.bytes) === JSON.stringify(bytes), 'correctly decoded bytes')
t.ok(res)

t.ok(JSON.stringify(res2[0]) === JSON.stringify(bigIntToBuffer(BigInt(1))), 'correctly encoded reqId')
t.ok(JSON.stringify(res2[1]) === JSON.stringify(root), 'correctly encoded root')
t.ok(JSON.stringify(res2[2]) === JSON.stringify(origin), 'correctly encoded origin')
t.ok(JSON.stringify(res2[3]) === JSON.stringify(limit), 'correctly encoded limit')
t.ok(JSON.stringify(res2[4]) === JSON.stringify(bytes), 'correctly encoded bytes')
t.ok(res2)
t.end()
})

t.test('verify that GetStorageRanges handler encodes/decodes correctly', (t) => {
const config = new Config({ transports: [] })
const chain = new Chain({ config })
const p = new SnapProtocol({ config, chain })
const root = {
number: BigInt(4),
stateRoot: Buffer.from([]),
hash: () => {
return Buffer.from([])
},
}
const reqId = BigInt(1)
const origin = Buffer.from(
'0000000000000000000000000000000000000000000000000000000000000000',
'hex'
)
const limit = Buffer.from(
'0000000000000000000000000f00000000000000000000000000000000000010',
'hex'
)
const bytes = BigInt(5000000)
const accounts = [
new Account(BigInt(0), BigInt('40000000000100000')),
new Account(BigInt(2), BigInt('40000000000200000')),
]

const res = p.decode(p.messages.filter((message) => message.name === 'GetStorageRanges')[0], [
reqId,
root,
accounts,
origin,
limit,
bytes,
])
const res2 = p.encode(p.messages.filter((message) => message.name === 'GetStorageRanges')[0], {
reqId: reqId,
root: root,
accounts: accounts,
origin: origin,
limit: limit,
bytes: bytes,
})

t.ok(JSON.stringify(res.reqId) === JSON.stringify(reqId), 'correctly decoded reqId')
t.ok(JSON.stringify(res.root) === JSON.stringify(root), 'correctly decoded root')
t.ok(JSON.stringify(res.accounts) === JSON.stringify(accounts), 'correctly decoded accounts')
t.ok(JSON.stringify(res.origin) === JSON.stringify(origin), 'correctly decoded origin')
t.ok(JSON.stringify(res.limit) === JSON.stringify(limit), 'correctly decoded limit')
t.ok(JSON.stringify(res.bytes) === JSON.stringify(bytes), 'correctly decoded bytes')
t.ok(res)

t.ok(JSON.stringify(res2[0]) === JSON.stringify(bigIntToBuffer(BigInt(1))), 'correctly encoded reqId')
t.ok(JSON.stringify(res2[1]) === JSON.stringify(root), 'correctly encoded root')
t.ok(JSON.stringify(res2[2]) === JSON.stringify(accounts), 'correctly encoded accounts')
t.ok(JSON.stringify(res2[3]) === JSON.stringify(origin), 'correctly encoded origin')
t.ok(JSON.stringify(res2[4]) === JSON.stringify(limit), 'correctly encoded limit')
t.ok(JSON.stringify(res2[5]) === JSON.stringify(bigIntToBuffer(bytes)), 'correctly encoded bytes')
t.ok(res2)
t.end()
})

t.test('verify that GetByteCodes handler encodes/decodes correctly', (t) => {
const config = new Config({ transports: [] })
const chain = new Chain({ config })
const p = new SnapProtocol({ config, chain })
const reqId = BigInt(1)
const hashes = Buffer.from(
'0000000000000000000000000f00000000000000000000000000000000000010',
'hex'
)
const bytes = BigInt(5000000)

const res = p.decode(p.messages.filter((message) => message.name === 'GetByteCodes')[0], [
reqId,
hashes,
bytes,
])
const res2 = p.encode(p.messages.filter((message) => message.name === 'GetByteCodes')[0], {
reqId: reqId,
hashes: hashes,
bytes: bytes,
})

t.ok(JSON.stringify(res.reqId) === JSON.stringify(reqId), 'correctly decoded reqId')
t.ok(JSON.stringify(res.hashes) === JSON.stringify(hashes), 'correctly decoded hashes')
t.ok(JSON.stringify(res.bytes) === JSON.stringify(bytes), 'correctly decoded bytes')
t.ok(res)

t.ok(JSON.stringify(res2[0]) === JSON.stringify(bigIntToBuffer(BigInt(1))), 'correctly encoded reqId')
t.ok(JSON.stringify(res2[1]) === JSON.stringify(hashes), 'correctly encoded hashes')
t.ok(JSON.stringify(res2[2]) === JSON.stringify(bigIntToBuffer(bytes)), 'correctly encoded bytes')
t.ok(res2)
t.end()
})
})
66 changes: 66 additions & 0 deletions packages/client/test/sync/snapsync.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import * as tape from 'tape'

import * as td from 'testdouble'
import { BlockHeader } from '@ethereumjs/block'
import { Config } from '../../lib/config'
import { Chain } from '../../lib/blockchain'

tape('[SnapSynchronizer]', async (t) => {
class PeerPool {
open() {}
close() {}
}
PeerPool.prototype.open = td.func<any>()
PeerPool.prototype.close = td.func<any>()

const { SnapSynchronizer } = await import('../../lib/sync/snapsync')

t.test('should initialize correctly', async (t) => {
const config = new Config({ transports: [] })
const pool = new PeerPool() as any
const chain = new Chain({ config })
const sync = new SnapSynchronizer({ config, pool, chain })
t.equals(sync.type, 'snap', 'snap type')
t.end()
})

t.test('should find best', async (t) => {
const config = new Config({ transports: [] })
const pool = new PeerPool() as any
const chain = new Chain({ config })
const sync = new SnapSynchronizer({
config,
interval: 1,
pool,
chain,
})
;(sync as any).running = true
;(sync as any).chain = { blocks: { height: 1 } }
const getBlockHeaders1 = td.func<any>()
td.when(getBlockHeaders1(td.matchers.anything())).thenReturn([
BigInt(1),
[BlockHeader.fromHeaderData({ number: 1 })],
])
const getBlockHeaders2 = td.func<any>()
td.when(getBlockHeaders2(td.matchers.anything())).thenReturn([
BigInt(2),
[BlockHeader.fromHeaderData({ number: 2 })],
])
const peers = [
{
snap: {},
eth: { status: { bestHash: '0xaa' }, getBlockHeaders: getBlockHeaders1 },
inbound: false,
},
{
snap: {},
eth: { status: { bestHash: '0xbb' }, getBlockHeaders: getBlockHeaders2 },
inbound: false,
},
]
;(sync as any).pool = { peers }
;(sync as any).forceSync = true
t.equal(await sync.best(), peers[1], 'found best')
t.end()
})
})

0 comments on commit 77ead1b

Please sign in to comment.