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 Oct 31, 2024
1 parent eaa459d commit 249be04
Showing 1 changed file with 54 additions and 7 deletions.
61 changes: 54 additions & 7 deletions consensus/consortium/v1/consortium.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,19 @@ func (c *Consortium) snapshot(chain consensus.ChainHeaderReader, number uint64,
if cpHeader != nil {
hash := cpHeader.Hash()

validators, err := c.getValidatorsFromContract(chain, number)
if err != nil {
return nil, err
var validators []common.Address
var err error
// Snap Sync mode.
if c.skipCheckpointHeaderCheck {
validators, err = c.getValidatorsFromGenesis()
if err != nil {
return nil, err
}
} else {
validators, err = c.getValidatorsFromContract(chain, number-1)
if err != nil {
return nil, err
}
}

snap = newSnapshot(c.config, c.signatures, number, hash, validators)
Expand Down Expand Up @@ -485,9 +495,17 @@ 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)
if err != nil {
return err
var validators []common.Address
if c.skipCheckpointHeaderCheck {
validators, err = c.getValidatorsFromGenesis()
if err != nil {
return err
}
} else {
validators, err = c.getValidatorsFromContract(chain, number-1)
if err != nil {
return err
}
}

for _, signer := range validators {
Expand Down Expand Up @@ -756,6 +774,35 @@ 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) {
if c.chainConfig.ConsortiumV2Block == nil {
return nil, errors.New("No genesis block supplied")
}
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:
log.Info("chainID", c.chainConfig.ChainID)
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 +826,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 249be04

Please sign in to comment.