-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom chain support to Caplin (#11508)
Things added: * Keep genesis file stored in datadir * Fixed race conditions in Sentinel * Fixed scheduling of hard fork for same epoch-hardforks * Small refactorings --------- Co-authored-by: Kewei <kewei.train@gmail.com>
- Loading branch information
1 parent
41465d5
commit f6a39a5
Showing
37 changed files
with
446 additions
and
452 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 was deleted.
Oops, something went wrong.
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,71 @@ | ||
package genesisdb | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/erigontech/erigon/cl/clparams" | ||
"github.com/erigontech/erigon/cl/phase1/core/state" | ||
"github.com/erigontech/erigon/cl/utils" | ||
"github.com/spf13/afero" | ||
) | ||
|
||
const genesisStateFileName = "genesis_state.ssz_snappy" | ||
|
||
/* | ||
* GenesisDB only keeps track of one file | ||
* genesis_state.ssz_snappy | ||
* This DB is static and only used to store the genesis state, it is write-once and read-always. | ||
*/ | ||
type genesisDB struct { | ||
fs afero.Fs // Use afero to make it easier to test. | ||
beaconConfig *clparams.BeaconChainConfig | ||
} | ||
|
||
func NewGenesisDB(beaconConfig *clparams.BeaconChainConfig, genesisDBPath string) GenesisDB { | ||
return &genesisDB{ | ||
fs: afero.NewBasePathFs(afero.NewOsFs(), genesisDBPath), | ||
beaconConfig: beaconConfig, | ||
} | ||
} | ||
|
||
func (g *genesisDB) IsInitialized() (bool, error) { | ||
return afero.Exists(g.fs, genesisStateFileName) | ||
} | ||
|
||
func (g *genesisDB) Initialize(state *state.CachingBeaconState) error { | ||
initialized, err := g.IsInitialized() | ||
if err != nil { | ||
return err | ||
} | ||
// No need to initialize. | ||
if initialized || state == nil { | ||
return nil | ||
} | ||
enc, err := state.EncodeSSZ(nil) | ||
if err != nil { | ||
return err | ||
} | ||
return afero.WriteFile(g.fs, genesisStateFileName, utils.CompressSnappy(enc), 0644) | ||
} | ||
|
||
func (g *genesisDB) ReadGenesisState() (*state.CachingBeaconState, error) { | ||
enc, err := afero.ReadFile(g.fs, genesisStateFileName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
decompressedEnc, err := utils.DecompressSnappy(enc) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
st := state.New(g.beaconConfig) | ||
slot, err := utils.ExtractSlotFromSerializedBeaconState(decompressedEnc) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not deserialize state slot: %s", err) | ||
} | ||
if err := st.DecodeSSZ(decompressedEnc, int(g.beaconConfig.GetCurrentStateVersion(slot/g.beaconConfig.SlotsPerEpoch))); err != nil { | ||
return nil, fmt.Errorf("could not deserialize state: %s", err) | ||
} | ||
return st, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package genesisdb | ||
|
||
import "github.com/erigontech/erigon/cl/phase1/core/state" | ||
|
||
type GenesisDB interface { | ||
// Initialize initializes the genesis database, with either a given genesis state or the hardcoded databases. | ||
Initialize(state *state.CachingBeaconState) error | ||
|
||
IsInitialized() (bool, error) | ||
|
||
// ReadGenesisState returns the genesis state. | ||
ReadGenesisState() (*state.CachingBeaconState, error) | ||
} |
Oops, something went wrong.