github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/consensus/istanbul/backend/test_util.go (about)

     1  package backend
     2  
     3  import (
     4  	"bytes"
     5  	blscrypto "github.com/ethereum/go-ethereum/crypto/bls"
     6  
     7  	"github.com/ethereum/go-ethereum/common"
     8  	"github.com/ethereum/go-ethereum/consensus/istanbul"
     9  	"github.com/ethereum/go-ethereum/core"
    10  	"github.com/ethereum/go-ethereum/core/types"
    11  	"github.com/ethereum/go-ethereum/rlp"
    12  )
    13  
    14  func AppendValidatorsToGenesisBlock(genesis *core.Genesis, validators []istanbul.ValidatorData) {
    15  	if len(genesis.ExtraData) < types.IstanbulExtraVanity {
    16  		genesis.ExtraData = append(genesis.ExtraData, bytes.Repeat([]byte{0x00}, types.IstanbulExtraVanity)...)
    17  	}
    18  	genesis.ExtraData = genesis.ExtraData[:types.IstanbulExtraVanity]
    19  
    20  	addrs := []common.Address{}
    21  	publicKeys := []blscrypto.SerializedPublicKey{}
    22  
    23  	for i := range validators {
    24  		if (validators[i].BLSPublicKey == blscrypto.SerializedPublicKey{}) {
    25  			panic("BLSPublicKey is nil")
    26  		}
    27  		addrs = append(addrs, validators[i].Address)
    28  		publicKeys = append(publicKeys, validators[i].BLSPublicKey)
    29  	}
    30  
    31  	ist := &types.IstanbulExtra{
    32  		AddedValidators:           addrs,
    33  		AddedValidatorsPublicKeys: publicKeys,
    34  		Seal:                      []byte{},
    35  		AggregatedSeal:            types.IstanbulAggregatedSeal{},
    36  		ParentAggregatedSeal:      types.IstanbulAggregatedSeal{},
    37  	}
    38  
    39  	istPayload, err := rlp.EncodeToBytes(&ist)
    40  	if err != nil {
    41  		panic("failed to encode istanbul extra")
    42  	}
    43  	genesis.ExtraData = append(genesis.ExtraData, istPayload...)
    44  }