Skip to content

Commit

Permalink
Use Uint8Array in config getDomain (#3475)
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion authored Dec 2, 2021
1 parent be50e4e commit 000aec9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 8 additions & 5 deletions packages/config/src/genesisConfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {ForkDigestHex, ICachedGenesis} from "./types";
export {IForkDigestContext} from "./types";

export function createICachedGenesis(chainForkConfig: IChainForkConfig, genesisValidatorsRoot: Root): ICachedGenesis {
const domainCache = new Map<ForkName, Map<DomainType, Buffer>>();
const domainCache = new Map<ForkName, Map<DomainType, Uint8Array>>();

const forkDigestByForkName = new Map<ForkName, ForkDigest>();
const forkDigestHexByForkName = new Map<ForkName, ForkDigestHex>();
Expand All @@ -22,11 +22,11 @@ export function createICachedGenesis(chainForkConfig: IChainForkConfig, genesisV
}

return {
getDomain(domainType: DomainType, slot: Slot): Buffer {
getDomain(domainType: DomainType, slot: Slot): Uint8Array {
const forkInfo = chainForkConfig.getForkInfo(slot);
let domainByType = domainCache.get(forkInfo.name);
if (!domainByType) {
domainByType = new Map<DomainType, Buffer>();
domainByType = new Map<DomainType, Uint8Array>();
domainCache.set(forkInfo.name, domainByType);
}
let domain = domainByType.get(domainType);
Expand Down Expand Up @@ -73,9 +73,12 @@ export function createICachedGenesis(chainForkConfig: IChainForkConfig, genesisV
};
}

function computeDomain(domainType: DomainType, forkVersion: Version, genesisValidatorRoot: Root): Buffer {
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;
}

function computeForkDataRoot(currentVersion: Version, genesisValidatorsRoot: Root): Uint8Array {
Expand Down
2 changes: 1 addition & 1 deletion packages/config/src/genesisConfig/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export interface IForkDigestContext {
}

export interface ICachedGenesis extends IForkDigestContext {
getDomain(domainType: DomainType, slot: Slot): Buffer;
getDomain(domainType: DomainType, slot: Slot): Uint8Array;
}

0 comments on commit 000aec9

Please sign in to comment.