Skip to content

Commit

Permalink
Add constant for lookback window and debug
Browse files Browse the repository at this point in the history
  • Loading branch information
scorbajio committed Jul 30, 2024
1 parent edd0866 commit dca3e3f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
4 changes: 4 additions & 0 deletions packages/client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ export interface ConfigOptions {
pruneEngineCache?: boolean
snapAvailabilityDepth?: bigint
snapTransitionSafeDepth?: bigint
snapLookbackWindow?: number

/**
* Save account keys preimages in the meta db (default: false)
Expand Down Expand Up @@ -393,6 +394,7 @@ export class Config {
// distance from head at which we can safely transition from a synced snapstate to vmexecution
// randomly kept it at 5 for fast testing purposes but ideally should be >=32 slots
public static readonly SNAP_TRANSITION_SAFE_DEPTH = BigInt(5)
public static readonly SNAP_LOOKBACK_WINDOW = 2

public readonly logger: Logger
public readonly syncmode: SyncMode
Expand Down Expand Up @@ -441,6 +443,7 @@ export class Config {
public readonly engineNewpayloadMaxTxsExecute: number
public readonly snapAvailabilityDepth: bigint
public readonly snapTransitionSafeDepth: bigint
public readonly snapLookbackWindow: number

public readonly prefixStorageTrieKeys: boolean
// Defaulting to false as experimental as of now
Expand Down Expand Up @@ -535,6 +538,7 @@ export class Config {
this.snapAvailabilityDepth = options.snapAvailabilityDepth ?? Config.SNAP_AVAILABILITY_DEPTH
this.snapTransitionSafeDepth =
options.snapTransitionSafeDepth ?? Config.SNAP_TRANSITION_SAFE_DEPTH
this.snapLookbackWindow = options.snapLookbackWindow ?? Config.SNAP_LOOKBACK_WINDOW

this.prefixStorageTrieKeys = options.prefixStorageTrieKeys ?? true
this.enableSnapSync = options.enableSnapSync ?? false
Expand Down
3 changes: 2 additions & 1 deletion packages/client/src/sync/fetcher/accountfetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,11 @@ export class AccountFetcher extends Fetcher<JobTask, AccountData[], AccountData>
// could be running independently of eachother in some cases
const currentHeight = this.height
const newestHeight = latest.number
if (newestHeight - currentHeight >= 1) {
if (newestHeight - currentHeight >= this.config.snapLookbackWindow) {
this.fetcherDoneFlags.snapTargetHeight = latest.number
this.fetcherDoneFlags.snapTargetRoot = latest.stateRoot
this.fetcherDoneFlags.snapTargetHash = latest.hash()
this.height = newestHeight
}
}

Expand Down
5 changes: 2 additions & 3 deletions packages/client/src/sync/fetcher/storagefetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import type { Job, SnapFetcherDoneFlags } from './types.js'
import type { Debugger } from 'debug'

const TOTAL_RANGE_END = BIGINT_2 ** BIGINT_256 - BIGINT_1
const LOOKBACK_WINDOW = 1

type StorageDataResponse = StorageData[][] & { completed?: boolean }

Expand Down Expand Up @@ -232,7 +231,7 @@ export class StorageFetcher extends Fetcher<JobTask, StorageData[][], StorageDat
// could be running independently of eachother in some cases
const currentHeight = this.height
const newestHeight = latest.number
if (newestHeight - currentHeight >= LOOKBACK_WINDOW) {
if (newestHeight - currentHeight >= this.config.snapLookbackWindow) {
this.fetcherDoneFlags.snapTargetHeight = latest.number
this.fetcherDoneFlags.snapTargetRoot = latest.stateRoot
this.fetcherDoneFlags.snapTargetHash = latest.hash()
Expand Down Expand Up @@ -266,7 +265,7 @@ export class StorageFetcher extends Fetcher<JobTask, StorageData[][], StorageDat

if (
this.fetcherDoneFlags.snapTargetHeight! - (task.height ?? task.storageRequests[0].height) >=
LOOKBACK_WINDOW
this.config.snapLookbackWindow
) {
// skip request if we are close to being outside of lookback range to avoid getting banned
this.debug(`skipping request that is close to being outside of lookback range`)
Expand Down
30 changes: 28 additions & 2 deletions packages/client/src/sync/fetcher/trienodefetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class TrieNodeFetcher extends Fetcher<JobTask, Uint8Array[], Uint8Array>
// will always start with root node as first set of node requests
this.pathToNodeRequestData.setElement('', {
requested: false,
nodeHash: bytesToHex(this.fetcherDoneFlags.snapTargetRoot!),
nodeHash: bytesToHex(this.fetcherDoneFlags.snapTargetRoot!), // TODO this target could change, have to update tfn to clear and recreate root request on targetRoot changing

Check failure on line 123 in packages/client/src/sync/fetcher/trienodefetcher.ts

View workflow job for this annotation

GitHub Actions / test-client (18)

test/sync/fetcher/accountfetcher.spec.ts > [AccountFetcher] > should start/stop

Error: Uint8Array expected ❯ bytes ../../node_modules/@noble/hashes/src/_assert.ts:18:26 ❯ Module.bytesToHex ../../node_modules/@noble/hashes/src/utils.ts:64:3 ❯ new TrieNodeFetcher src/sync/fetcher/trienodefetcher.ts:123:17 ❯ new AccountFetcher src/sync/fetcher/accountfetcher.ts:122:28 ❯ test/sync/fetcher/accountfetcher.spec.ts:46:21

Check failure on line 123 in packages/client/src/sync/fetcher/trienodefetcher.ts

View workflow job for this annotation

GitHub Actions / test-client (18)

test/sync/fetcher/accountfetcher.spec.ts > [AccountFetcher] > should update highest known hash

Error: Uint8Array expected ❯ bytes ../../node_modules/@noble/hashes/src/_assert.ts:18:26 ❯ Module.bytesToHex ../../node_modules/@noble/hashes/src/utils.ts:64:3 ❯ new TrieNodeFetcher src/sync/fetcher/trienodefetcher.ts:123:17 ❯ new AccountFetcher src/sync/fetcher/accountfetcher.ts:122:28 ❯ test/sync/fetcher/accountfetcher.spec.ts:67:21

Check failure on line 123 in packages/client/src/sync/fetcher/trienodefetcher.ts

View workflow job for this annotation

GitHub Actions / test-client (18)

test/sync/fetcher/accountfetcher.spec.ts > [AccountFetcher] > should process

Error: Uint8Array expected ❯ bytes ../../node_modules/@noble/hashes/src/_assert.ts:18:26 ❯ Module.bytesToHex ../../node_modules/@noble/hashes/src/utils.ts:64:3 ❯ new TrieNodeFetcher src/sync/fetcher/trienodefetcher.ts:123:17 ❯ new AccountFetcher src/sync/fetcher/accountfetcher.ts:122:28 ❯ test/sync/fetcher/accountfetcher.spec.ts:99:21

Check failure on line 123 in packages/client/src/sync/fetcher/trienodefetcher.ts

View workflow job for this annotation

GitHub Actions / test-client (18)

test/sync/fetcher/accountfetcher.spec.ts > [AccountFetcher] > should adopt correctly

Error: Uint8Array expected ❯ bytes ../../node_modules/@noble/hashes/src/_assert.ts:18:26 ❯ Module.bytesToHex ../../node_modules/@noble/hashes/src/utils.ts:64:3 ❯ new TrieNodeFetcher src/sync/fetcher/trienodefetcher.ts:123:17 ❯ new AccountFetcher src/sync/fetcher/accountfetcher.ts:122:28 ❯ test/sync/fetcher/accountfetcher.spec.ts:135:21

Check failure on line 123 in packages/client/src/sync/fetcher/trienodefetcher.ts

View workflow job for this annotation

GitHub Actions / test-client (18)

test/sync/fetcher/accountfetcher.spec.ts > [AccountFetcher] > should skip job with limit lower than highest known hash

Error: Uint8Array expected ❯ bytes ../../node_modules/@noble/hashes/src/_assert.ts:18:26 ❯ Module.bytesToHex ../../node_modules/@noble/hashes/src/utils.ts:64:3 ❯ new TrieNodeFetcher src/sync/fetcher/trienodefetcher.ts:123:17 ❯ new AccountFetcher src/sync/fetcher/accountfetcher.ts:122:28 ❯ test/sync/fetcher/accountfetcher.spec.ts:176:21

Check failure on line 123 in packages/client/src/sync/fetcher/trienodefetcher.ts

View workflow job for this annotation

GitHub Actions / test-client (18)

test/sync/fetcher/accountfetcher.spec.ts > [AccountFetcher] > should request correctly

Error: Uint8Array expected ❯ bytes ../../node_modules/@noble/hashes/src/_assert.ts:18:26 ❯ Module.bytesToHex ../../node_modules/@noble/hashes/src/utils.ts:64:3 ❯ new TrieNodeFetcher src/sync/fetcher/trienodefetcher.ts:123:17 ❯ new AccountFetcher src/sync/fetcher/accountfetcher.ts:122:28 ❯ test/sync/fetcher/accountfetcher.spec.ts:214:21

Check failure on line 123 in packages/client/src/sync/fetcher/trienodefetcher.ts

View workflow job for this annotation

GitHub Actions / test-client (18)

test/sync/fetcher/accountfetcher.spec.ts > [AccountFetcher] > should verify zero-element proof correctly

Error: Uint8Array expected ❯ bytes ../../node_modules/@noble/hashes/src/_assert.ts:18:26 ❯ Module.bytesToHex ../../node_modules/@noble/hashes/src/utils.ts:64:3 ❯ new TrieNodeFetcher src/sync/fetcher/trienodefetcher.ts:123:17 ❯ new AccountFetcher src/sync/fetcher/accountfetcher.ts:122:28 ❯ test/sync/fetcher/accountfetcher.spec.ts:258:21

Check failure on line 123 in packages/client/src/sync/fetcher/trienodefetcher.ts

View workflow job for this annotation

GitHub Actions / test-client (18)

test/sync/fetcher/accountfetcher.spec.ts > [AccountFetcher] > should reject zero-element proof if elements still remain to right of requested range

Error: Uint8Array expected ❯ bytes ../../node_modules/@noble/hashes/src/_assert.ts:18:26 ❯ Module.bytesToHex ../../node_modules/@noble/hashes/src/utils.ts:64:3 ❯ new TrieNodeFetcher src/sync/fetcher/trienodefetcher.ts:123:17 ❯ new AccountFetcher src/sync/fetcher/accountfetcher.ts:122:28 ❯ test/sync/fetcher/accountfetcher.spec.ts:296:21

Check failure on line 123 in packages/client/src/sync/fetcher/trienodefetcher.ts

View workflow job for this annotation

GitHub Actions / test-client (18)

test/sync/fetcher/accountfetcher.spec.ts > [AccountFetcher] > should verify proof correctly

Error: Uint8Array expected ❯ bytes ../../node_modules/@noble/hashes/src/_assert.ts:18:26 ❯ Module.bytesToHex ../../node_modules/@noble/hashes/src/utils.ts:64:3 ❯ new TrieNodeFetcher src/sync/fetcher/trienodefetcher.ts:123:17 ❯ new AccountFetcher src/sync/fetcher/accountfetcher.ts:122:28 ❯ test/sync/fetcher/accountfetcher.spec.ts:331:21

Check failure on line 123 in packages/client/src/sync/fetcher/trienodefetcher.ts

View workflow job for this annotation

GitHub Actions / test-client (18)

test/sync/fetcher/accountfetcher.spec.ts > [AccountFetcher] > should find a fetchable peer

Error: Uint8Array expected ❯ bytes ../../node_modules/@noble/hashes/src/_assert.ts:18:26 ❯ Module.bytesToHex ../../node_modules/@noble/hashes/src/utils.ts:64:3 ❯ new TrieNodeFetcher src/sync/fetcher/trienodefetcher.ts:123:17 ❯ new AccountFetcher src/sync/fetcher/accountfetcher.ts:122:28 ❯ test/sync/fetcher/accountfetcher.spec.ts:406:21
nodeParentHash: '', // root node does not have a parent
} as NodeRequestData)

Expand Down Expand Up @@ -154,10 +154,25 @@ export class TrieNodeFetcher extends Fetcher<JobTask, Uint8Array[], Uint8Array>
if (latest !== undefined && latest.stateRoot !== undefined) {
const currentHeight = this.height
const newestHeight = latest.number
if (newestHeight - currentHeight >= 1) {
if (newestHeight - currentHeight >= this.config.snapLookbackWindow) {
this.debug('dbg810: updating roots')
// update latest height and root
this.fetcherDoneFlags.snapTargetHeight = latest.number
this.fetcherDoneFlags.snapTargetRoot = latest.stateRoot
this.fetcherDoneFlags.snapTargetHash = latest.hash()
this.height = newestHeight

// clear any tasks or requests and create new request for newest root set to
this.clear()
this.pathToNodeRequestData = new OrderedMap<string, NodeRequestData>()
this.requestedNodeToPath = new Map<string, string>()
this.fetchedAccountNodes = new Map<string, FetchedNodeData>()
this.nodeCount = 0
this.pathToNodeRequestData.setElement('', {
requested: false,
nodeHash: bytesToHex(this.fetcherDoneFlags.snapTargetRoot!), // TODO this target could change, have to update tfn to clear and recreate root request on targetRoot changing
nodeParentHash: '', // root node does not have a parent
} as NodeRequestData)
}
}

Expand All @@ -169,6 +184,14 @@ export class TrieNodeFetcher extends Fetcher<JobTask, Uint8Array[], Uint8Array>
bytes: BigInt(this.config.maxRangeBytes),
})

this.debug(`dbg800: trienodefetcher.ts`)
this.debug(`root: ${bytesToHex(this.fetcherDoneFlags.snapTargetRoot!)}`)
this.debug(
`paths requested: ${paths.map((valArr, _) => {
return valArr.map((val, _) => bytesToHex(val))
})}`,
)

// Response is valid, but check if peer is signalling that it does not have
// the requested data. For trie node range queries that means the peer is not
// yet synced.
Expand Down Expand Up @@ -237,6 +260,9 @@ export class TrieNodeFetcher extends Fetcher<JobTask, Uint8Array[], Uint8Array>
let unknownChildNodeCount = 0
let hasStorageComponent = false

this.debug(`dbg804: trienodefetcher.ts`)
this.debug(`nodeData: ${node}`)

// get all children of received node
if (node instanceof BranchNode) {
const children = (node as BranchNode).getChildren()
Expand Down

0 comments on commit dca3e3f

Please sign in to comment.