Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Buffer issues in Lightclient browser #2896

Merged
merged 2 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/beacon-state-transition/src/util/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import {Epoch, Version, Root, DomainType, allForks} from "@chainsafe/lodestar-ty

import {computeForkDataRoot} from "./fork";

// Only used by processDeposit
// Only used by processDeposit + lightclient
/**
* Return the domain for the [[domainType]] and [[forkVersion]].
*/
export function computeDomain(domainType: DomainType, forkVersion: Version, genesisValidatorRoot: Root): Buffer {
export function computeDomain(domainType: DomainType, forkVersion: Version, genesisValidatorRoot: Root): Uint8Array {
const forkDataRoot = computeForkDataRoot(forkVersion, genesisValidatorRoot);
return Buffer.concat([domainType as Buffer, forkDataRoot.slice(0, 28)]);
const domain = new Uint8Array(32);
domain.set(domainType, 0);
domain.set(forkDataRoot.slice(0, 28), 4);
return domain;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/light-client/src/utils/clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type OnSlotFn = (slot: Slot, signal: AbortSignal) => Promise<void>;

export interface IClock {
readonly currentSlot: Slot;
readonly genesisTime: number;
start(signal: AbortSignal): void;
runEverySlot(fn: OnSlotFn): void;
}
Expand All @@ -17,7 +18,7 @@ export class Clock implements IClock {

constructor(
private readonly config: IChainForkConfig,
private readonly genesisTime: number,
readonly genesisTime: number,
private readonly onError?: (e: Error) => void
) {}

Expand Down
7 changes: 4 additions & 3 deletions packages/light-client/src/utils/verifyMerkleBranch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {hash, byteArrayEquals} from "@chainsafe/ssz";
import {byteArrayEquals} from "@chainsafe/ssz";
import {hash} from "@chainsafe/persistent-merkle-tree";

/**
* Verify that the given ``leaf`` is on the merkle branch ``proof``
Expand All @@ -16,9 +17,9 @@ export function isValidMerkleBranch(
let value = leaf;
for (let i = 0; i < depth; i++) {
if (Math.floor(index / 2 ** i) % 2) {
value = hash(Buffer.concat([proof[i], value]));
value = hash(proof[i], value);
} else {
value = hash(Buffer.concat([value, proof[i]]));
value = hash(value, proof[i]);
}
}
return byteArrayEquals(value, root);
Expand Down
1 change: 1 addition & 0 deletions packages/light-client/test/unit/sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ describe("Lightclient flow with LightClientUpdater", () => {
});

class MockClock implements IClock {
readonly genesisTime = 0;
constructor(readonly currentSlot: Slot) {}
start(): void {
//
Expand Down