-
-
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.
Merge pull request #843 from ChainSafe/tuyen/enr-eth2
Add ENRForkID to ENR eth2
- Loading branch information
Showing
19 changed files
with
321 additions
and
22 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
packages/lodestar-beacon-state-transition/src/epoch/fork.ts
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,19 @@ | ||
import {IBeaconConfig} from "@chainsafe/lodestar-config"; | ||
import {BeaconState} from "@chainsafe/lodestar-types"; | ||
import {getCurrentEpoch} from ".."; | ||
import {intToBytes} from "@chainsafe/lodestar-utils"; | ||
|
||
export function processForkChanged(config: IBeaconConfig, state: BeaconState): void { | ||
const currentEpoch = getCurrentEpoch(config, state); | ||
const nextEpoch = currentEpoch + 1; | ||
const currentForkVersion = state.fork.currentVersion; | ||
const nextFork = config.params.ALL_FORKS && config.params.ALL_FORKS.find( | ||
(fork) => config.types.Version.equals(currentForkVersion, intToBytes(fork.previousVersion, 4))); | ||
if (nextFork && nextFork.epoch === nextEpoch) { | ||
state.fork = { | ||
previousVersion: state.fork.currentVersion, | ||
currentVersion: intToBytes(nextFork.currentVersion, 4), | ||
epoch: nextFork.epoch, | ||
}; | ||
} | ||
} |
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
59 changes: 59 additions & 0 deletions
59
packages/lodestar-beacon-state-transition/test/unit/stateTransition/epoch/fork.test.ts
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,59 @@ | ||
import {config} from "@chainsafe/lodestar-config/lib/presets/mainnet"; | ||
import {BeaconState} from "@chainsafe/lodestar-types"; | ||
import {generateState} from "../../../utils/state"; | ||
import {processForkChanged} from "../../../../src/epoch/fork"; | ||
import {expect} from "chai"; | ||
import {bytesToInt} from "@chainsafe/lodestar-utils"; | ||
|
||
describe("processForkChanged", () => { | ||
let state: BeaconState; | ||
|
||
beforeEach(() => { | ||
state = generateState(); | ||
state.fork = { | ||
currentVersion: Buffer.from([1, 0, 0, 0]), | ||
epoch: 100, | ||
previousVersion: Buffer.alloc(4), | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
config.params.ALL_FORKS = undefined; | ||
}); | ||
|
||
it("should not update fork if no matched next fork", () => { | ||
config.params.ALL_FORKS = undefined; | ||
const preFork = state.fork; | ||
processForkChanged(config, state); | ||
expect(config.types.Fork.equals(preFork, state.fork)).to.be.true; | ||
}); | ||
|
||
it("should not update fork if found matched next fork but epoch not matched", () => { | ||
config.params.ALL_FORKS = [ | ||
{ | ||
previousVersion: bytesToInt(Buffer.from([1, 0, 0, 0])), | ||
currentVersion: bytesToInt(Buffer.from([2, 0, 0, 0])), | ||
epoch: 200, | ||
}, | ||
]; | ||
const preFork = state.fork; | ||
processForkChanged(config, state); | ||
expect(config.types.Fork.equals(preFork, state.fork)).to.be.true; | ||
}); | ||
|
||
it("should update fork if found matched next fork and matched epoch", () => { | ||
config.params.ALL_FORKS = [ | ||
{ | ||
previousVersion: bytesToInt(Buffer.from([1, 0, 0, 0])), | ||
currentVersion: bytesToInt(Buffer.from([2, 0, 0, 0])), | ||
epoch: 200, | ||
}, | ||
]; | ||
const preFork = state.fork; | ||
state.slot = 200 * config.params.SLOTS_PER_EPOCH - 1; | ||
processForkChanged(config, state); | ||
expect(config.types.Fork.equals(preFork, state.fork)).to.be.false; | ||
expect(config.types.Version.equals(preFork.currentVersion, state.fork.previousVersion)); | ||
expect(state.fork.epoch).to.be.equal(200); | ||
}); | ||
}); |
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
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.