Skip to content

Commit

Permalink
consensus: get Validators from genesis instead of triedb in v1 consor…
Browse files Browse the repository at this point in the history
…tium. In snap sync, we will disable accessing/mark stale to triedb when enabling path scheme for protecting the persistent storing, so the data of validators only used for checking in some first blocks which we can return hardcore list from genesis data for following the flow of snap-sync from go-eth team.
  • Loading branch information
huyngopt1994 committed Nov 1, 2024
1 parent eaa459d commit dea1363
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions consensus/consortium/v1/consortium.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,10 @@ func (c *Consortium) snapshot(chain consensus.ChainHeaderReader, number uint64,
if cpHeader != nil {
hash := cpHeader.Hash()

validators, err := c.getValidatorsFromContract(chain, number)
validators, err := c.getValidatorsFromGenesis()
if err != nil {
return nil, err
}

snap = newSnapshot(c.config, c.signatures, number, hash, validators)
if err := snap.store(c.db); err != nil {
return nil, err
Expand Down Expand Up @@ -485,7 +484,8 @@ func (c *Consortium) Prepare(chain consensus.ChainHeaderReader, header *types.He
header.Extra = header.Extra[:extraVanity]

if number%c.config.Epoch == 0 {
validators, err := c.getValidatorsFromContract(chain, number)
var validators []common.Address
validators, err := c.getValidatorsFromGenesis()
if err != nil {
return err
}
Expand Down Expand Up @@ -756,6 +756,31 @@ func (c *Consortium) doCalcDifficulty(signer common.Address, number uint64, vali
return new(big.Int).Set(diffNoTurn)
}

// getValidatorsFromGenesis gets the list of validators from the genesis block support backward compatibility in v1, only used with Snap Sync.
func (c *Consortium) getValidatorsFromGenesis() ([]common.Address, error) {
var validatorSet []string
switch {
case c.chainConfig.ChainID.Cmp(big.NewInt(2020)) == 0:
validatorSet = []string{
"0x000000000000000000000000f224beff587362a88d859e899d0d80c080e1e812",
"0x00000000000000000000000011360eacdedd59bc433afad4fc8f0417d1fbebab",
"0x00000000000000000000000070bb1fb41c8c42f6ddd53a708e2b82209495e455",
}
case c.chainConfig.ChainID.Cmp(big.NewInt(2021)) == 0:
validatorSet = []string{
"0x0000000000000000000000004a4bc674a97737376cfe990ae2fe0d2b6e738393",
"0x000000000000000000000000b6bc5bc0410773a3f86b1537ce7495c52e38f88b",
}
default:
return nil, errors.New("no validator set for this chain only support Mainnet & Testnet")
}
var addresses []common.Address
for _, str := range validatorSet {
addresses = append(addresses, common.HexToAddress(str))
}
return addresses, nil
}

// Read the validator list from contract
func (c *Consortium) getValidatorsFromContract(chain consensus.ChainHeaderReader, number uint64) ([]common.Address, error) {
if chain.Config().IsFenix(big.NewInt(int64(number))) {
Expand All @@ -779,7 +804,7 @@ func (c *Consortium) getValidatorsFromLastCheckpoint(chain consensus.ChainHeader

if lastCheckpoint == 0 {
// TODO(andy): Review if we should put validators in genesis block's extra data
return c.getValidatorsFromContract(chain, number)
return c.getValidatorsFromGenesis()
}

var header *types.Header
Expand Down

0 comments on commit dea1363

Please sign in to comment.