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

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package validator
    18  
    19  import (
    20  	"github.com/ethereum/go-ethereum/common"
    21  	"github.com/ethereum/go-ethereum/consensus/istanbul"
    22  	blscrypto "github.com/ethereum/go-ethereum/crypto/bls"
    23  	"github.com/ethereum/go-ethereum/rlp"
    24  )
    25  
    26  func New(addr common.Address, blsPublicKey blscrypto.SerializedPublicKey) istanbul.Validator {
    27  	return &defaultValidator{
    28  		address:      addr,
    29  		blsPublicKey: blsPublicKey,
    30  	}
    31  }
    32  
    33  func DeserializeValidator(binaryData []byte) (istanbul.Validator, error) {
    34  	var value defaultValidator
    35  
    36  	err := rlp.DecodeBytes(binaryData, &value)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	return &value, nil
    41  }
    42  
    43  func NewSet(validators []istanbul.ValidatorData) istanbul.ValidatorSet {
    44  	return newDefaultSet(validators)
    45  }
    46  
    47  func DeserializeValidatorSet(binaryData []byte) (istanbul.ValidatorSet, error) {
    48  	var value defaultSet
    49  
    50  	err := rlp.DecodeBytes(binaryData, &value)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	return &value, nil
    55  }
    56  
    57  func ExtractValidators(extraData []byte) []istanbul.ValidatorData {
    58  	// get the validator addresses
    59  	validatorLength := common.AddressLength + blscrypto.PUBLICKEYBYTES
    60  	validators := make([]istanbul.ValidatorData, (len(extraData) / validatorLength))
    61  	for i := 0; i < len(validators); i++ {
    62  		copy(validators[i].Address[:], extraData[i*validatorLength:i*validatorLength+common.AddressLength])
    63  		copy(validators[i].BLSPublicKey[:], extraData[i*validatorLength+common.AddressLength:])
    64  	}
    65  
    66  	return validators
    67  }
    68  
    69  // Check whether the extraData is presented in prescribed form
    70  func ValidExtraData(extraData []byte) bool {
    71  	return len(extraData)%common.AddressLength == 0
    72  }