Skip to content

Commit

Permalink
Allow to disable sync process as chain segment
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed Jun 2, 2021
1 parent e6205fe commit 8e08d89
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 8 deletions.
11 changes: 11 additions & 0 deletions packages/cli/src/options/beaconNodeOptions/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import {ICliCommandOptions} from "../../util";

export interface ISyncArgs {
"sync.isSingleNode": boolean;
"sync.disableProcessAsChainSegment": boolean;
}

export function parseArgs(args: ISyncArgs): IBeaconNodeOptions["sync"] {
return {
isSingleNode: args["sync.isSingleNode"],
disableProcessAsChainSegment: args["sync.disableProcessAsChainSegment"],
};
}

Expand All @@ -21,4 +23,13 @@ Use only for local networks with a single node, can be dangerous in regular netw
defaultDescription: String(defaultOptions.sync.isSingleNode),
group: "sync",
},

"sync.disableProcessAsChainSegment": {
hidden: true,
type: "boolean",
description:
"For RangeSync disable processing batches of blocks at once. Should only be used for debugging or testing.",
defaultDescription: String(defaultOptions.sync.disableProcessAsChainSegment),
group: "sync",
},
};
2 changes: 2 additions & 0 deletions packages/cli/test/unit/options/beaconNodeOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe("options / beaconNodeOptions", () => {
"network.localMultiaddrs": [],

"sync.isSingleNode": true,
"sync.disableProcessAsChainSegment": true,
} as IBeaconNodeArgs;

const expectedOptions: RecursivePartial<IBeaconNodeOptions> = {
Expand Down Expand Up @@ -82,6 +83,7 @@ describe("options / beaconNodeOptions", () => {
},
sync: {
isSingleNode: true,
disableProcessAsChainSegment: true,
},
};

Expand Down
13 changes: 13 additions & 0 deletions packages/lodestar/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,19 @@ export class BeaconChain implements IBeaconChain {
.catch(() => /* unreachable */ ({}));
}

async processBlock(
signedBlock: allForks.SignedBeaconBlock,
{prefinalized, trusted = false}: {prefinalized: boolean; trusted: boolean}
): Promise<void> {
return await this.blockProcessor.processBlockJob({
signedBlock,
reprocess: false,
prefinalized,
validSignatures: trusted,
validProposerSignature: trusted,
});
}

async processChainSegment(
signedBlocks: allForks.SignedBeaconBlock[],
{prefinalized, trusted = false}: {prefinalized: boolean; trusted: boolean}
Expand Down
5 changes: 5 additions & 0 deletions packages/lodestar/src/chain/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ export interface IBeaconChain {
receiveAttestation(attestation: phase0.Attestation): void;
/** Pre-process and run the per slot state transition function */
receiveBlock(signedBlock: allForks.SignedBeaconBlock, trusted?: boolean): void;
/** Process a block until complete */
processBlock(
signedBlock: allForks.SignedBeaconBlock,
flags: {prefinalized: boolean; trusted: boolean}
): Promise<void>;
/** Process a chain of blocks until complete */
processChainSegment(
signedBlocks: allForks.SignedBeaconBlock[],
Expand Down
6 changes: 6 additions & 0 deletions packages/lodestar/src/sync/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ export interface ISyncOptions {
* Use only for local networks with a single node, can be dangerous in regular networks.
*/
isSingleNode?: boolean;
/**
* For RangeSync disable processing batches of blocks at once.
* Should only be used for debugging or testing.
*/
disableProcessAsChainSegment?: boolean;
}

export const defaultSyncOptions: Required<ISyncOptions> = {
isSingleNode: false,
disableProcessAsChainSegment: false,
};
1 change: 0 additions & 1 deletion packages/lodestar/src/sync/range/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {hashBlocks} from "./utils";

export type BatchOpts = {
epochsPerBatch: Epoch;
logAfterAttempts?: number;
};

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/lodestar/src/sync/range/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
computeMostCommonTarget,
} from "./utils";

export type SyncChainOpts = BatchOpts;
export type SyncChainOpts = Partial<BatchOpts>;

export type SyncChainModules = {
config: IBeaconConfig;
Expand Down Expand Up @@ -107,7 +107,7 @@ export class SyncChain {

private readonly logger: ILogger;
private readonly config: IBeaconConfig;
private readonly opts: SyncChainOpts;
private readonly opts: BatchOpts;

constructor(
startEpoch: Epoch,
Expand Down
18 changes: 14 additions & 4 deletions packages/lodestar/src/sync/range/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export type RangeSyncModules = {
logger: ILogger;
};

export type RangeSyncOpts = SyncChainOpts;
export type RangeSyncOpts = SyncChainOpts & {
disableProcessAsChainSegment?: boolean;
};

/**
* RangeSync groups peers by their `status` into static target `SyncChain` instances
Expand Down Expand Up @@ -79,9 +81,9 @@ export class RangeSync extends (EventEmitter as {new (): RangeSyncEmitter}) {
/** There is a single chain per type, 1 finalized sync, 1 head sync */
private readonly chains = new Map<RangeSyncType, SyncChain>();

private opts?: SyncChainOpts;
private opts?: RangeSyncOpts;

constructor(modules: RangeSyncModules, opts?: SyncChainOpts) {
constructor(modules: RangeSyncModules, opts?: RangeSyncOpts) {
super();
this.chain = modules.chain;
this.network = modules.network;
Expand Down Expand Up @@ -181,7 +183,15 @@ export class RangeSync extends (EventEmitter as {new (): RangeSyncEmitter}) {

/** Convenience method for `SyncChain` */
private processChainSegment: SyncChainFns["processChainSegment"] = async (blocks) => {
await this.chain.processChainSegment(blocks, {prefinalized: true, trusted: false}); // Not trusted, verify signatures
// Not trusted, verify signatures
const flags = {prefinalized: true, trusted: false};

if (this.opts?.disableProcessAsChainSegment) {
// Should only be used for debugging or testing
for (const block of blocks) await this.chain.processBlock(block, flags);
} else {
await this.chain.processChainSegment(blocks, flags);
}
};

/** Convenience method for `SyncChain` */
Expand Down
2 changes: 1 addition & 1 deletion packages/lodestar/src/sync/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class BeaconSync implements IBeaconSync {
this.network = network;
this.chain = chain;
this.logger = logger;
this.rangeSync = new RangeSync(modules);
this.rangeSync = new RangeSync(modules, this.opts);
this.slotImportTolerance = modules.config.params.SLOTS_PER_EPOCH;

// Subscribe to RangeSync completing a SyncChain and recompute sync state
Expand Down
4 changes: 4 additions & 0 deletions packages/lodestar/test/utils/mocks/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ export class MockBeaconChain implements IBeaconChain {
return;
}

async processBlock(): Promise<void> {
return;
}

async processChainSegment(): Promise<void> {
return;
}
Expand Down

0 comments on commit 8e08d89

Please sign in to comment.