forked from node-real/bsc-erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added hard fork transition support to Erigon-CL. (erigontech#7088)
- Loading branch information
1 parent
2e0be95
commit 150ee47
Showing
11 changed files
with
318 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package consensustests | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/ledgerwatch/erigon/cl/clparams" | ||
"github.com/ledgerwatch/erigon/cl/cltypes" | ||
"github.com/ledgerwatch/erigon/cmd/erigon-cl/core/transition" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type transitionMeta struct { | ||
ForkEpoch uint64 `yaml:"fork_epoch"` | ||
} | ||
|
||
func transitionTestFunction(context testContext) error { | ||
metaBytes, err := os.ReadFile("meta.yaml") | ||
if err != nil { | ||
return err | ||
} | ||
meta := transitionMeta{} | ||
if err := yaml.Unmarshal(metaBytes, &meta); err != nil { | ||
return err | ||
} | ||
contextPrev := context | ||
contextPrev.version-- | ||
testState, err := decodeStateFromFile(contextPrev, "pre.ssz_snappy") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
expectedState, err := decodeStateFromFile(context, "post.ssz_snappy") | ||
if err != nil { | ||
return err | ||
} | ||
switch context.version { | ||
case clparams.AltairVersion: | ||
testState.BeaconConfig().AltairForkEpoch = meta.ForkEpoch | ||
case clparams.BellatrixVersion: | ||
testState.BeaconConfig().BellatrixForkEpoch = meta.ForkEpoch | ||
case clparams.CapellaVersion: | ||
testState.BeaconConfig().CapellaForkEpoch = meta.ForkEpoch | ||
} | ||
startSlot := testState.Slot() | ||
blockIndex := 0 | ||
for { | ||
testSlot, err := testBlockSlot(blockIndex) | ||
if err != nil { | ||
return err | ||
} | ||
var block *cltypes.SignedBeaconBlock | ||
if testSlot/clparams.MainnetBeaconConfig.SlotsPerEpoch >= meta.ForkEpoch { | ||
block, err = testBlock(context, blockIndex) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
block, err = testBlock(contextPrev, blockIndex) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if block == nil { | ||
break | ||
} | ||
|
||
blockIndex++ | ||
|
||
if err := transition.TransitionState(testState, block, true); err != nil { | ||
return fmt.Errorf("cannot transition state: %s. slot=%d. start_slot=%d", err, block.Block.Slot, startSlot) | ||
} | ||
} | ||
expectedRoot, err := expectedState.HashSSZ() | ||
if err != nil { | ||
return err | ||
} | ||
haveRoot, err := testState.HashSSZ() | ||
if err != nil { | ||
return err | ||
} | ||
if haveRoot != expectedRoot { | ||
return fmt.Errorf("mismatching state roots") | ||
} | ||
return nil | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
git clone https://github.com/ethereum/consensus-spec-tests | ||
cd consensus-spec-tests && git lfs pull && cd .. | ||
mv consensus-spec-tests/tests . | ||
rm -rf consensus-spec-tests | ||
rm -rf tests/minimal #these ones are useless |
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,94 @@ | ||
package state | ||
|
||
import ( | ||
libcommon "github.com/ledgerwatch/erigon-lib/common" | ||
"github.com/ledgerwatch/erigon/cl/clparams" | ||
"github.com/ledgerwatch/erigon/cl/cltypes" | ||
"github.com/ledgerwatch/erigon/cl/utils" | ||
) | ||
|
||
func (b *BeaconState) UpgradeToAltair() error { | ||
b.previousStateRoot = libcommon.Hash{} | ||
epoch := b.Epoch() | ||
// update version | ||
b.fork.Epoch = epoch | ||
b.fork.CurrentVersion = utils.Uint32ToBytes4(b.beaconConfig.AltairForkVersion) | ||
// Process new fields | ||
b.previousEpochParticipation = make(cltypes.ParticipationFlagsList, len(b.validators)) | ||
b.currentEpochParticipation = make(cltypes.ParticipationFlagsList, len(b.validators)) | ||
b.inactivityScores = make([]uint64, len(b.validators)) | ||
// Change version | ||
b.version = clparams.AltairVersion | ||
// Fill in previous epoch participation from the pre state's pending attestations | ||
for _, attestation := range b.previousEpochAttestations { | ||
flags, err := b.GetAttestationParticipationFlagIndicies(attestation.Data, attestation.InclusionDelay) | ||
if err != nil { | ||
return err | ||
} | ||
indicies, err := b.GetAttestingIndicies(attestation.Data, attestation.AggregationBits, false) | ||
if err != nil { | ||
return err | ||
} | ||
for _, index := range indicies { | ||
for _, flagIndex := range flags { | ||
b.previousEpochParticipation[index].Add(int(flagIndex)) | ||
} | ||
} | ||
} | ||
b.previousEpochAttestations = nil | ||
// Process sync committees | ||
var err error | ||
if b.currentSyncCommittee, err = b.ComputeNextSyncCommittee(); err != nil { | ||
return err | ||
} | ||
if b.nextSyncCommittee, err = b.ComputeNextSyncCommittee(); err != nil { | ||
return err | ||
} | ||
// Update the state root cache | ||
b.touchedLeaves[ForkLeafIndex] = true | ||
b.touchedLeaves[PreviousEpochParticipationLeafIndex] = true | ||
b.touchedLeaves[CurrentEpochParticipationLeafIndex] = true | ||
b.touchedLeaves[InactivityScoresLeafIndex] = true | ||
b.touchedLeaves[CurrentSyncCommitteeLeafIndex] = true | ||
b.touchedLeaves[NextSyncCommitteeLeafIndex] = true | ||
|
||
return nil | ||
} | ||
|
||
func (b *BeaconState) UpgradeToBellatrix() error { | ||
b.previousStateRoot = libcommon.Hash{} | ||
epoch := b.Epoch() | ||
// update version | ||
b.fork.Epoch = epoch | ||
b.fork.PreviousVersion = b.fork.CurrentVersion | ||
b.fork.CurrentVersion = utils.Uint32ToBytes4(b.beaconConfig.BellatrixForkVersion) | ||
b.latestExecutionPayloadHeader = cltypes.NewEth1Header(clparams.BellatrixVersion) | ||
// Update the state root cache | ||
b.touchedLeaves[ForkLeafIndex] = true | ||
b.touchedLeaves[LatestExecutionPayloadHeaderLeafIndex] = true | ||
b.version = clparams.BellatrixVersion | ||
return nil | ||
} | ||
|
||
func (b *BeaconState) UpgradeToCapella() error { | ||
b.previousStateRoot = libcommon.Hash{} | ||
epoch := b.Epoch() | ||
// update version | ||
b.fork.Epoch = epoch | ||
b.fork.PreviousVersion = b.fork.CurrentVersion | ||
b.fork.CurrentVersion = utils.Uint32ToBytes4(b.beaconConfig.CapellaForkVersion) | ||
// Update the payload header. | ||
b.latestExecutionPayloadHeader.Capella() | ||
// Set new fields | ||
b.nextWithdrawalIndex = 0 | ||
b.nextWithdrawalValidatorIndex = 0 | ||
b.historicalSummaries = nil | ||
// Update the state root cache | ||
b.touchedLeaves[ForkLeafIndex] = true | ||
b.touchedLeaves[LatestExecutionPayloadHeaderLeafIndex] = true | ||
b.touchedLeaves[NextWithdrawalIndexLeafIndex] = true | ||
b.touchedLeaves[NextWithdrawalValidatorIndexLeafIndex] = true | ||
b.touchedLeaves[HistoricalSummariesLeafIndex] = true | ||
b.version = clparams.CapellaVersion | ||
return nil | ||
} |
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.