Skip to content

Commit

Permalink
Correct the signature of getUnfinalizedBlocksAtSlots
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Jun 25, 2020
1 parent b0982b0 commit 5939740
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 24 deletions.
9 changes: 5 additions & 4 deletions packages/lodestar/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,17 @@ export class BeaconChain extends (EventEmitter as { new(): ChainEventEmitter })
return this.epochCtx.copy();
}

public async getUnfinalizedBlocksAtSlots(slots: Slot[]): Promise<(SignedBeaconBlock|null)[]> {
public async getUnfinalizedBlocksAtSlots(slots: Slot[]): Promise<SignedBeaconBlock[]|null> {
if (!slots) {
return [];
return null;
}
const blockRoots = slots.map((slot) => {
const summary = this.forkChoice.getBlockSummaryAtSlot(slot);
return summary? summary.blockRoot : null;
});
}).filter((blockRoot) => !!blockRoot);
// these blocks are on the same chain to head
return await Promise.all(blockRoots.map(
(blockRoot) => blockRoot? this.db.block.get(blockRoot) : Promise.resolve(null)));
(blockRoot) => this.db.block.get(blockRoot)));
}

public async getFinalizedCheckpoint(): Promise<Checkpoint> {
Expand Down
2 changes: 1 addition & 1 deletion packages/lodestar/src/chain/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface IBeaconChain extends ChainEventEmitter {

getBlockAtSlot(slot: Slot): Promise<SignedBeaconBlock|null>;

getUnfinalizedBlocksAtSlots(slots: Slot[]): Promise<(SignedBeaconBlock|null)[]>;
getUnfinalizedBlocksAtSlots(slots: Slot[]): Promise<SignedBeaconBlock[]|null>;

/**
* Add attestation to the fork-choice rule
Expand Down
10 changes: 2 additions & 8 deletions packages/lodestar/src/sync/reqResp/reqResp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,21 +265,15 @@ export class BeaconReqRespHandler implements IReqRespHandler {
}
slot = (slot === -1)? request.startSlot : slot + request.step;
const upperSlot = request.startSlot + request.count * request.step;
const headRoot = await chain.forkChoice.head().blockRoot;
const slots = [];
while (slot < upperSlot) {
slots.push(slot);
slot += request.step;
}
const blocks = await chain.getUnfinalizedBlocksAtSlots(slots);
const blocks = await chain.getUnfinalizedBlocksAtSlots(slots) || [];
for (const block of blocks) {
if(block) {
// make sure block is on the same chain to head
const blockRoot = config.types.BeaconBlock.hashTreeRoot(block.message);
const ancestorRoot = chain.forkChoice.getAncestor(headRoot, block.message.slot);
if (ancestorRoot && config.types.Root.equals(blockRoot, ancestorRoot)) {
yield block;
}
yield block;
}
}
};
Expand Down
14 changes: 4 additions & 10 deletions packages/lodestar/test/unit/sync/reqRes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ describe("sync req resp", function () {
const peerInfo: PeerInfo = new PeerInfo(new PeerId(Buffer.from("lodestar")));
const body: BeaconBlocksByRangeRequest = {
startSlot: 2,
count: 5,
count: 4,
step: 2,
};
dbStub.blockArchive.valuesStream.returns(async function* () {
Expand All @@ -201,14 +201,8 @@ describe("sync req resp", function () {
}());
const block8 = generateEmptySignedBlock();
block8.message.slot = 8;
const block10 = generateEmptySignedBlock();
block10.message.slot = 10;
// block 6 does not exist
chainStub.getUnfinalizedBlocksAtSlots.resolves([null, block8, block10]);
// block8 exists but not on same chain to head
forkChoiceStub.getAncestor.onFirstCall().returns(null);
// block10 is good
forkChoiceStub.getAncestor.onSecondCall().returns(config.types.BeaconBlock.hashTreeRoot(block10.message));
chainStub.getUnfinalizedBlocksAtSlots.resolves([null, block8]);
let blockStream: AsyncIterable<ResponseBody>;
reqRespStub.sendResponseStream.callsFake((id: RequestId, err: RpcError, chunkIter: AsyncIterable<ResponseBody>) => {
blockStream = chunkIter;
Expand All @@ -218,7 +212,7 @@ describe("sync req resp", function () {
for await(const body of blockStream) {
slots.push((body as SignedBeaconBlock).message.slot);
}
// count is 5 but it returns only 3 blocks because block 6 does not exist and block 8 is not on same chain to head
expect(slots).to.be.deep.equal([2,4,10]);
// count is 4 but it returns only 3 blocks because block 6 does not exist
expect(slots).to.be.deep.equal([2,4,8]);
});
});
2 changes: 1 addition & 1 deletion packages/lodestar/test/utils/mocks/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class MockBeaconChain extends EventEmitter implements IBeaconChain {
return this.epochCtx.copy();
}

public async getUnfinalizedBlocksAtSlots(slots: Slot[]): Promise<(SignedBeaconBlock|null)[]> {
public async getUnfinalizedBlocksAtSlots(slots: Slot[]): Promise<SignedBeaconBlock[]|null> {
if (!slots) {
return [];
}
Expand Down

0 comments on commit 5939740

Please sign in to comment.