github.com/klaytn/klaytn@v1.12.1/datasync/dbsyncer/utils.go (about)

     1  // Copyright 2019 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn 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 klaytn 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 klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package dbsyncer
    18  
    19  import (
    20  	"strings"
    21  
    22  	"github.com/klaytn/klaytn/blockchain/types"
    23  	"github.com/klaytn/klaytn/common"
    24  	"github.com/klaytn/klaytn/consensus/istanbul"
    25  	"github.com/klaytn/klaytn/crypto/sha3"
    26  	"github.com/klaytn/klaytn/rlp"
    27  )
    28  
    29  const (
    30  	// mysql driver (go) has query parameter max size (65535)
    31  	// transaction record has 15 parameters
    32  	BULK_INSERT_SIZE = 3000
    33  	TX_KEY_FACTOR    = 100000
    34  )
    35  
    36  func getProposerAndValidatorsFromBlock(block *types.Block) (proposer string, validators string, err error) {
    37  	blockNumber := block.NumberU64()
    38  	if blockNumber == 0 {
    39  		return "", "", nil
    40  	}
    41  	// Retrieve the signature from the header extra-data
    42  	istanbulExtra, err := types.ExtractIstanbulExtra(block.Header())
    43  	if err != nil {
    44  		return "", "", err
    45  	}
    46  
    47  	sigHash, err := sigHash(block.Header())
    48  	if err != nil {
    49  		return "", "", err
    50  	}
    51  	proposerAddr, err := istanbul.GetSignatureAddress(sigHash.Bytes(), istanbulExtra.Seal)
    52  	if err != nil {
    53  		return "", "", err
    54  	}
    55  	commiteeAddrs := make([]common.Address, len(istanbulExtra.Validators))
    56  	for i, addr := range istanbulExtra.Validators {
    57  		commiteeAddrs[i] = addr
    58  	}
    59  	var strValidators []string
    60  	for _, validator := range istanbulExtra.Validators {
    61  		strValidators = append(strValidators, validator.Hex())
    62  	}
    63  
    64  	return proposerAddr.Hex(), strings.Join(strValidators, ","), nil
    65  }
    66  
    67  // ecrecover extracts the Klaytn account address from a signed header.
    68  func ecrecover(header *types.Header) (common.Address, error) {
    69  	// Retrieve the signature from the header extra-data
    70  	istanbulExtra, err := types.ExtractIstanbulExtra(header)
    71  	if err != nil {
    72  		return common.Address{}, err
    73  	}
    74  
    75  	sigHash, err := sigHash(header)
    76  	if err != nil {
    77  		return common.Address{}, err
    78  	}
    79  	addr, err := istanbul.GetSignatureAddress(sigHash.Bytes(), istanbulExtra.Seal)
    80  	if err != nil {
    81  		return addr, err
    82  	}
    83  	return addr, nil
    84  }
    85  
    86  func sigHash(header *types.Header) (hash common.Hash, err error) {
    87  	hasher := sha3.NewKeccak256()
    88  
    89  	// Clean seal is required for calculating proposer seal.
    90  	if err := rlp.Encode(hasher, types.IstanbulFilteredHeader(header, false)); err != nil {
    91  		logger.Error("fail to encode", "err", err)
    92  		return common.Hash{}, err
    93  	}
    94  	hasher.Sum(hash[:0])
    95  	return hash, nil
    96  }