gitlab.com/yannislg/go-pulse@v0.0.0-20210722055913-a3e24e95638d/consensus/parlia/primordialpulse_fork.go (about)

     1  package parlia
     2  
     3  import (
     4  	"errors"
     5  	"sort"
     6  
     7  	"github.com/ethereum/go-ethereum/common"
     8  	"github.com/ethereum/go-ethereum/core/state"
     9  	"github.com/ethereum/go-ethereum/core/types"
    10  	"github.com/ethereum/go-ethereum/log"
    11  )
    12  
    13  // Fetches the initial validators from the parlia config for bootstrapping the
    14  // authorization snapshot on a new PulseChain fork.
    15  func (p *Parlia) initPulsors() ([]common.Address, error) {
    16  	// get validators from parlia config
    17  	if p.config.InitValidators == nil || len(*p.config.InitValidators) == 0 {
    18  		return nil, errors.New("missing initValidators in parlia config")
    19  	}
    20  
    21  	validators := make([]common.Address, len(*p.config.InitValidators))
    22  	for i, addr := range *p.config.InitValidators {
    23  		validators[i] = common.HexToAddress(addr)
    24  	}
    25  	return validators, nil
    26  }
    27  
    28  // Returns the byte array of sorted validators for validator rotation on epoch.
    29  // If PrimordialPulseBlock happens to fall on an epoch, validators will be taken
    30  // from the snapshot instead of the system contracts, which won't yet be deployed & initialized.
    31  func (p *Parlia) getEpochValidatorBytes(header *types.Header, snap *Snapshot) ([]byte, error) {
    32  	var (
    33  		validators []common.Address
    34  		err        error
    35  	)
    36  
    37  	if p.chainConfig.IsPrimordialPulseBlock(header.Number.Uint64()) {
    38  		// already sorted ascending by address
    39  		validators = snap.validators()
    40  	} else {
    41  		validators, err = p.getCurrentValidators(header.ParentHash)
    42  		if err != nil {
    43  			return nil, err
    44  		}
    45  
    46  		// sort contract validator by address
    47  		sort.Sort(validatorsAscending(validators))
    48  	}
    49  
    50  	validatorsBytes := make([]byte, len(validators)*validatorBytesLength)
    51  	for i, validator := range validators {
    52  		copy(validatorsBytes[i*validatorBytesLength:], validator.Bytes())
    53  	}
    54  	return validatorsBytes, nil
    55  }
    56  
    57  // Performs the initial allocations and balance adjustments for the PrimordialPulse fork.
    58  func (p *Parlia) primordialPulseAlloctions(state *state.StateDB) error {
    59  	log.Info("Applying PrimordialPulse fork allocations 💸")
    60  
    61  	// state.SetBalance()
    62  
    63  	return nil
    64  }