Skip to content

Commit

Permalink
feat(core-blockchain): increase block download frequency using fastSy…
Browse files Browse the repository at this point in the history
…nc option (#4805)

* Fast sync default

* Use value

* fix tests
  • Loading branch information
sebastijankuzner authored Jan 29, 2024
1 parent 47811d5 commit e2aa715
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
50 changes: 43 additions & 7 deletions __tests__/unit/core-blockchain/blockchain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ EventEmitter.prototype.constructor = Object.prototype.constructor;
describe("Blockchain", () => {
let sandbox: Sandbox;

const configuration: any = {};
const configuration: any = {
getRequired: jest.fn(),
};
const logService: any = {};
const stateStore: any = {};
const databaseService: any = {};
Expand Down Expand Up @@ -59,11 +61,11 @@ describe("Blockchain", () => {
.get<Services.Triggers.Triggers>(Container.Identifiers.TriggerService)
.bind("getActiveDelegates", new GetActiveDelegatesAction(sandbox.app));

sandbox.app
.bind(Identifiers.QueueFactory)
.toFactory((context: interfaces.Context) => async <K, T>(name?: string): Promise<Queue> =>
sandbox.app.resolve<Queue>(MemoryQueue).make(),
);
sandbox.app.bind(Identifiers.QueueFactory).toFactory(
(context: interfaces.Context) =>
async <K, T>(name?: string): Promise<Queue> =>
sandbox.app.resolve<Queue>(MemoryQueue).make(),
);

Managers.configManager.setFromPreset("testnet");
});
Expand Down Expand Up @@ -278,14 +280,18 @@ describe("Blockchain", () => {
it("should set wakeUpTimeout on stateStore", () => {
const blockchain = sandbox.app.resolve<Blockchain>(Blockchain);

configuration.getRequired.mockReturnValueOnce(false);

blockchain.setWakeUp();
expect(stateStore.setWakeUpTimeout).toHaveBeenCalled();
});

it("should dispatch WAKEUP when wake up function is called", () => {
it("should dispatch WAKEUP when wake up function is called after 60 second", () => {
const blockchain = sandbox.app.resolve<Blockchain>(Blockchain);
const spyDispatch = jest.spyOn(blockchain, "dispatch");

configuration.getRequired.mockReturnValueOnce(false);

blockchain.setWakeUp();
expect(spyDispatch).toBeCalledTimes(0);

Expand All @@ -297,6 +303,24 @@ describe("Blockchain", () => {
expect(spyDispatch).toBeCalledTimes(1);
expect(spyDispatch).toBeCalledWith("WAKEUP");
});

it("should dispatch WAKEUP when wake up function is called after 8 second, when fastSync === true", () => {
const blockchain = sandbox.app.resolve<Blockchain>(Blockchain);
const spyDispatch = jest.spyOn(blockchain, "dispatch");

configuration.getRequired.mockReturnValueOnce(true);

blockchain.setWakeUp();
expect(spyDispatch).toBeCalledTimes(0);

expect(stateStore.setWakeUpTimeout).toHaveBeenCalledWith(expect.toBeFunction(), 8000);

// Call given callback function
stateStore.setWakeUpTimeout.mock.calls[0][0]();

expect(spyDispatch).toBeCalledTimes(1);
expect(spyDispatch).toBeCalledWith("WAKEUP");
});
});

describe("resetWakeUp", () => {
Expand Down Expand Up @@ -829,12 +853,24 @@ describe("Blockchain", () => {
it("should return false if last block is more than 3 blocktimes away from current slot time", () => {
const blockchain = sandbox.app.resolve<Blockchain>(Blockchain);

configuration.getRequired.mockReturnValueOnce(false);
peerRepository.hasPeers = jest.fn().mockReturnValue(true);
const mockBlock = { data: { id: "123", height: 444, timestamp: Crypto.Slots.getTime() - 25 } };
stateStore.getLastBlock = jest.fn().mockReturnValue(mockBlock);

expect(blockchain.isSynced()).toBeFalse();
});

it("should return false if last block is more than 1 blocktimes away from current slot time and fastSync === true", () => {
const blockchain = sandbox.app.resolve<Blockchain>(Blockchain);

configuration.getRequired.mockReturnValueOnce(true);
peerRepository.hasPeers = jest.fn().mockReturnValue(true);
const mockBlock = { data: { id: "123", height: 444, timestamp: Crypto.Slots.getTime() - 8 } };
stateStore.getLastBlock = jest.fn().mockReturnValue(mockBlock);

expect(blockchain.isSynced()).toBeFalse();
});
});

describe("getLastBlock", () => {
Expand Down
14 changes: 10 additions & 4 deletions packages/core-blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,12 @@ export class Blockchain implements Contracts.Blockchain.Blockchain {
* Set wakeup timeout to check the network for new blocks.
*/
public setWakeUp(): void {
this.stateStore.setWakeUpTimeout(() => {
this.dispatch("WAKEUP");
}, 60000);
this.stateStore.setWakeUpTimeout(
() => {
this.dispatch("WAKEUP");
},
this.configuration.getRequired<boolean>("fastSync") ? 8000 : 60000,
);
}

/**
Expand Down Expand Up @@ -437,8 +440,11 @@ export class Blockchain implements Contracts.Blockchain.Blockchain {

block = block || this.getLastBlock().data;

const blockCount = this.configuration.getRequired<boolean>("fastSync") ? 1 : 3;

return (
Crypto.Slots.getTime() - block.timestamp < 3 * Managers.configManager.getMilestone(block.height).blocktime
Crypto.Slots.getTime() - block.timestamp <
blockCount * Managers.configManager.getMilestone(block.height).blocktime
);
}

Expand Down
1 change: 1 addition & 0 deletions packages/core-blockchain/src/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const defaults = {
fastSync: !!process.env.CORE_BLOCKCHAIN_FAST_SYNC, // Improves sync rate for readonly nodes, that have a p2p port closed to public. Such node doesn't broadcast data
databaseRollback: {
maxBlockRewind: 10000,
steps: 1000,
Expand Down
1 change: 1 addition & 0 deletions packages/core-blockchain/src/service-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class ServiceProvider extends Providers.ServiceProvider {

public configSchema(): object {
return Joi.object({
fastSync: Joi.bool().required(),
databaseRollback: Joi.object({
maxBlockRewind: Joi.number().integer().min(1).required(),
steps: Joi.number().integer().min(1).required(),
Expand Down

0 comments on commit e2aa715

Please sign in to comment.