-
-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
383 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export function parseFeeRecipient(feeRecipientHex: string): string { | ||
if (!/^0x[a-fA-F0-9]{40}$/i.test(feeRecipientHex)) { | ||
throw Error(`Invalid feeRecipient= ${feeRecipientHex}, expected format: ^0x[a-fA-F0-9]{40}$`); | ||
} | ||
return feeRecipientHex.toLowerCase(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {expect} from "chai"; | ||
import {parseFeeRecipient} from "../../../src/util"; | ||
|
||
const feeRecipient = Buffer.from(Array.from({length: 20}, () => Math.round(Math.random() * 255))); | ||
const feeRecipientString = feeRecipient.toString("hex"); | ||
|
||
describe("validator / parseFeeRecipient", () => { | ||
const testCases: string[] = [`0x${feeRecipientString}`, `0X${feeRecipientString}`]; | ||
for (const testCase of testCases) { | ||
it(`parse ${testCase}`, () => { | ||
expect(`0x${feeRecipientString}`).to.be.deep.equal(parseFeeRecipient(testCase)); | ||
}); | ||
} | ||
}); | ||
|
||
describe("validator / invalid feeRecipient", () => { | ||
const testCases: string[] = [ | ||
feeRecipientString, | ||
`X0${feeRecipientString}`, | ||
`0x${feeRecipientString}13`, | ||
`0x${feeRecipientString.substr(0, 38)}`, | ||
]; | ||
for (const testCase of testCases) { | ||
it(`should error on ${testCase}`, () => { | ||
expect(() => parseFeeRecipient(testCase)).to.throw(); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {MapDef} from "../util/map"; | ||
import {Epoch} from "@chainsafe/lodestar-types"; | ||
import {IMetrics} from "../metrics"; | ||
import {routes} from "@chainsafe/lodestar-api"; | ||
|
||
const PROPOSER_PRESERVE_EPOCHS = 2; | ||
|
||
export type ProposerPreparationData = routes.validator.ProposerPreparationData; | ||
|
||
export class BeaconProposerCache { | ||
private readonly feeRecipientByValidatorIndex: MapDef<string, {epoch: Epoch; feeRecipient: string}>; | ||
constructor(opts: {defaultFeeRecipient: string}, private readonly metrics?: IMetrics | null) { | ||
this.feeRecipientByValidatorIndex = new MapDef<string, {epoch: Epoch; feeRecipient: string}>(() => ({ | ||
epoch: 0, | ||
feeRecipient: opts.defaultFeeRecipient, | ||
})); | ||
} | ||
|
||
add(epoch: Epoch, {validatorIndex, feeRecipient}: ProposerPreparationData): void { | ||
this.feeRecipientByValidatorIndex.set(validatorIndex, {epoch, feeRecipient}); | ||
} | ||
|
||
prune(epoch: Epoch): void { | ||
// This is not so optimized function, but could maintain a 2d array may be? | ||
for (const [validatorIndex, feeRecipientEntry] of this.feeRecipientByValidatorIndex.entries()) { | ||
// We only retain an entry for PROPOSER_PRESERVE_EPOCHS epochs | ||
if (feeRecipientEntry.epoch + PROPOSER_PRESERVE_EPOCHS < epoch) { | ||
this.feeRecipientByValidatorIndex.delete(validatorIndex); | ||
} | ||
} | ||
} | ||
|
||
get(proposerIndex: number | string): string { | ||
return this.feeRecipientByValidatorIndex.getOrDefault(`${proposerIndex}`).feeRecipient; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.