Skip to content

Commit

Permalink
rebase fixes for new master
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Jul 17, 2022
1 parent c6c65ea commit 95f4bc2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 74 deletions.
4 changes: 2 additions & 2 deletions packages/client/lib/net/peer/rlpxpeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ export class RlpxPeer extends Peer {
.getProtocols()
.filter((p) => p.constructor.name.toLowerCase() === 'snap')[0]
const snapProtocol =
snapRlpxProtocol &&
isTruthy(snapRlpxProtocol) &&
this.protocols.find(
(p) => p.name === snapRlpxProtocol?.constructor.name.toLowerCase()
)
if (snapProtocol) {
if (isTruthy(snapProtocol)) {
const snapSender = new RlpxSender(snapRlpxProtocol)
return this.bindProtocol(snapProtocol, snapSender)
}
Expand Down
101 changes: 49 additions & 52 deletions packages/client/lib/net/protocol/snapprotocol.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BN, AccountData as AccountDataBody } from 'ethereumjs-util'
import { AccountData as AccountDataBody, bigIntToBuffer, bufferToBigInt } from '@ethereumjs/util'
import { Chain } from './../../blockchain'
import { Message, Protocol, ProtocolOptions } from './protocol'

Expand All @@ -14,20 +14,20 @@ type AccountData = {

type GetAccountRangeOpts = {
/* Request id (default: next internal id) */
reqId?: BN
reqId?: bigint
root: Buffer
origin: Buffer
limit: Buffer
bytes: BN
bytes: bigint
}

type GetStorageRangesOpts = {
reqId?: BN
reqId?: bigint
root: Buffer
accounts: Buffer[]
origin: Buffer
limit: Buffer
bytes: BN
bytes: bigint
}

type StorageData = {
Expand All @@ -36,16 +36,16 @@ type StorageData = {
}

type GetByteCodesOpts = {
reqId?: BN
reqId?: bigint
hashes: Buffer[]
bytes: BN
bytes: bigint
}

type GetTrieNodesOpts = {
reqId?: BN
reqId?: bigint
root: Buffer
paths: Buffer[][]
bytes: BN
bytes: bigint
}
/*
* Messages with responses that are added as
Expand All @@ -54,39 +54,33 @@ type GetTrieNodesOpts = {
export interface SnapProtocolMethods {
getAccountRange: (
opts: GetAccountRangeOpts
) => Promise<{ reqId: BN; accounts: AccountData[]; proof: Buffer[][] }>
) => Promise<{ reqId: bigint; accounts: AccountData[]; proof: Buffer[][] }>
}

const id = new BN(0)

/**
* Implements snap/1 protocol
* @memberof module:net/protocol
*/
export class SnapProtocol extends Protocol {
private chain: Chain
private nextReqId = BigInt(0)

/* eslint-disable no-invalid-this */
private protocolMessages: Message[] = [
{
name: 'GetAccountRange',
code: 0x00,
response: 0x01,
encode: ({ reqId, root, origin, limit, bytes }: GetAccountRangeOpts) => {
return [
(reqId === undefined ? id.iaddn(1) : new BN(reqId)).toArrayLike(Buffer),
root,
origin,
limit,
bytes,
]
return [bigIntToBuffer(reqId ?? ++this.nextReqId), root, origin, limit, bytes]
},
decode: ([reqId, root, origin, limit, bytes]: any) => {
return {
reqId: new BN(reqId),
reqId: bufferToBigInt(reqId),
root,
origin,
limit,
bytes: new BN(bytes),
bytes: bufferToBigInt(bytes),
}
},
},
Expand All @@ -98,19 +92,19 @@ export class SnapProtocol extends Protocol {
accounts,
proof,
}: {
reqId: BN
reqId: bigint
accounts: AccountData[]
proof: Buffer[]
}) => {
return [
reqId.toArrayLike(Buffer),
bigIntToBuffer(reqId ?? ++this.nextReqId),
accounts.map((account) => [account.hash, account.body]),
proof,
]
},
decode: ([reqId, accounts, proof]: any) => {
return {
reqId: new BN(reqId),
reqId: bufferToBigInt(reqId),
accounts: accounts.map(([hash, body]: any) => ({ hash, body } as AccountData)),
proof,
}
Expand All @@ -122,34 +116,46 @@ export class SnapProtocol extends Protocol {
response: 0x03,
encode: ({ reqId, root, accounts, origin, limit, bytes }: GetStorageRangesOpts) => {
return [
(reqId === undefined ? id.iaddn(1) : new BN(reqId)).toArrayLike(Buffer),
bigIntToBuffer(reqId ?? ++this.nextReqId),
root,
accounts,
origin,
limit,
bytes,
bigIntToBuffer(bytes),
]
},
decode: ([reqId, root, accounts, origin, limit, bytes]: any) => {
return {
reqId: new BN(reqId),
reqId: bufferToBigInt(reqId),
root,
accounts,
origin,
limit,
bytes: new BN(bytes),
bytes: bufferToBigInt(bytes),
}
},
},
{
name: 'StorageRanges',
code: 0x03,
encode: ({ reqId, slots, proof }: { reqId: BN; slots: StorageData[]; proof: Buffer[] }) => {
return [reqId.toArrayLike(Buffer), slots.map((slot) => [slot.hash, slot.body]), proof]
encode: ({
reqId,
slots,
proof,
}: {
reqId: bigint
slots: StorageData[]
proof: Buffer[]
}) => {
return [
bigIntToBuffer(reqId ?? ++this.nextReqId),
slots.map((slot) => [slot.hash, slot.body]),
proof,
]
},
decode: ([reqId, slots, proof]: any) => {
return {
reqId: new BN(reqId),
reqId: bufferToBigInt(reqId),
slots: slots.map(([hash, body]: any) => ({ hash, body } as StorageData)),
proof,
}
Expand All @@ -160,29 +166,25 @@ export class SnapProtocol extends Protocol {
code: 0x04,
response: 0x05,
encode: ({ reqId, hashes, bytes }: GetByteCodesOpts) => {
return [
(reqId === undefined ? id.iaddn(1) : new BN(reqId)).toArrayLike(Buffer),
hashes,
bytes,
]
return [bigIntToBuffer(reqId ?? ++this.nextReqId), hashes, bigIntToBuffer(bytes)]
},
decode: ([reqId, hashes, bytes]: any) => {
return {
reqId: new BN(reqId),
reqId: bufferToBigInt(reqId),
hashes,
bytes: new BN(bytes),
bytes: bufferToBigInt(bytes),
}
},
},
{
name: 'ByteCodes',
code: 0x05,
encode: ({ reqId, codes }: { reqId: BN; codes: Buffer[] }) => {
return [reqId.toArrayLike(Buffer), codes]
encode: ({ reqId, codes }: { reqId: bigint; codes: Buffer[] }) => {
return [bigIntToBuffer(reqId ?? ++this.nextReqId), codes]
},
decode: ([reqId, codes]: any) => {
return {
reqId: new BN(reqId),
reqId: bufferToBigInt(reqId),
codes,
}
},
Expand All @@ -192,31 +194,26 @@ export class SnapProtocol extends Protocol {
code: 0x06,
response: 0x07,
encode: ({ reqId, root, paths, bytes }: GetTrieNodesOpts) => {
return [
(reqId === undefined ? id.iaddn(1) : new BN(reqId)).toArrayLike(Buffer),
root,
paths,
bytes,
]
return [bigIntToBuffer(reqId ?? ++this.nextReqId), root, paths, bigIntToBuffer(bytes)]
},
decode: ([reqId, root, paths, bytes]: any) => {
return {
reqId: new BN(reqId),
reqId: bufferToBigInt(reqId),
root,
paths,
bytes: new BN(bytes),
bytes: bufferToBigInt(bytes),
}
},
},
{
name: 'TrieNodes',
code: 0x07,
encode: ({ reqId, nodes }: { reqId: BN; nodes: Buffer[] }) => {
return [reqId.toArrayLike(Buffer), nodes]
encode: ({ reqId, nodes }: { reqId: bigint; nodes: Buffer[] }) => {
return [bigIntToBuffer(reqId ?? ++this.nextReqId), nodes]
},
decode: ([reqId, nodes]: any) => {
return {
reqId: new BN(reqId),
reqId: bufferToBigInt(reqId),
nodes,
}
},
Expand Down
43 changes: 23 additions & 20 deletions packages/client/lib/sync/snapsync.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { short } from '../util'
import { Synchronizer, SynchronizerOptions } from './sync'
import type { Peer } from '../net/peer/peer'
import { BN } from 'ethereumjs-util'

interface SnapSynchronizerOptions extends SynchronizerOptions {}

Expand Down Expand Up @@ -37,14 +35,14 @@ export class SnapSynchronizer extends Synchronizer {
* blockchain. Returns null if no valid peer is found
*/
async best(): Promise<Peer | undefined> {
let best: [Peer, BN] | undefined
let best: [Peer, bigint] | undefined
const peers = this.pool.peers.filter(this.syncable.bind(this))
if (peers.length < this.config.minPeers && !this.forceSync) return
for (const peer of peers) {
const latest = await this.latest(peer)
if (latest) {
const { number } = latest
if ((!best && number.gte(this.chain.blocks.height)) || (best && best[1].lt(number))) {
if ((!best && number >= this.chain.blocks.height) || (best && best[1] < number)) {
best = [peer, number]
}
}
Expand Down Expand Up @@ -72,27 +70,32 @@ export class SnapSynchronizer extends Synchronizer {
const latest = peer ? await this.latest(peer) : undefined
if (!latest) return false

const stateRoot = latest.stateRoot
const rangeResult = await peer!.snap!.getAccountRange({
root: stateRoot,
origin: Buffer.from(
'0000000000000000000000000000000000000000000000000000000000000000',
'hex'
),
limit: Buffer.from('0000000000000000000000000f00000000000000000000000000000000000010', 'hex'),
bytes: new BN(5000000),
})
// Just a small snipped to test out the methods manually
// From/for g11tech:
// Clean it up, once we have a full fetcher implemented, else let them
// stay commented for easy reference and manual testing
//
// const stateRoot = latest.stateRoot
// const rangeResult = await peer!.snap!.getAccountRange({
// root: stateRoot,
// origin: Buffer.from(
// '0000000000000000000000000000000000000000000000000000000000000000',
// 'hex'
// ),
// limit: Buffer.from('0000000000000000000000000f00000000000000000000000000000000000010', 'hex'),
// bytes: BigInt(5000000),
// })

console.log({ rangeResult: rangeResult?.accounts[0] })
// console.log({ rangeResult: rangeResult?.accounts[0] })
// if (rangeResult) {
// process.exit()
// }

const height = latest.number
if (!this.config.syncTargetHeight || this.config.syncTargetHeight.lt(latest.number)) {
this.config.syncTargetHeight = height
this.config.logger.info(`New sync target height=${height} hash=${short(latest.hash())}`)
}
// const height = latest.number
// if (!this.config.syncTargetHeight || this.config.syncTargetHeight < latest.number) {
// this.config.syncTargetHeight = height
// this.config.logger.info(`New sync target height=${height} hash=${short(latest.hash())}`)
// }

return false
}
Expand Down

0 comments on commit 95f4bc2

Please sign in to comment.