github.com/klaytn/klaytn@v1.10.2/datasync/chaindatafetcher/kafka/utils.go (about)

     1  // Copyright 2020 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 kafka
    18  
    19  import (
    20  	klaytnApi "github.com/klaytn/klaytn/api"
    21  	"github.com/klaytn/klaytn/blockchain"
    22  	"github.com/klaytn/klaytn/blockchain/types"
    23  	"github.com/klaytn/klaytn/common"
    24  	"github.com/klaytn/klaytn/consensus"
    25  	"github.com/klaytn/klaytn/consensus/istanbul"
    26  	"github.com/klaytn/klaytn/crypto/sha3"
    27  	"github.com/klaytn/klaytn/rlp"
    28  )
    29  
    30  func getProposerAndValidatorsFromBlock(block *types.Block) (common.Address, []common.Address, error) {
    31  	blockNumber := block.NumberU64()
    32  	if blockNumber == 0 {
    33  		return common.Address{}, []common.Address{}, nil
    34  	}
    35  	// Retrieve the signature from the header extra-data
    36  	istanbulExtra, err := types.ExtractIstanbulExtra(block.Header())
    37  	if err != nil {
    38  		return common.Address{}, []common.Address{}, err
    39  	}
    40  
    41  	sigHash, err := sigHash(block.Header())
    42  	if err != nil {
    43  		return common.Address{}, []common.Address{}, err
    44  	}
    45  	proposerAddr, err := istanbul.GetSignatureAddress(sigHash.Bytes(), istanbulExtra.Seal)
    46  	if err != nil {
    47  		return common.Address{}, []common.Address{}, err
    48  	}
    49  
    50  	return proposerAddr, istanbulExtra.Validators, nil
    51  }
    52  
    53  func sigHash(header *types.Header) (hash common.Hash, err error) {
    54  	hasher := sha3.NewKeccak256()
    55  
    56  	// Clean seal is required for calculating proposer seal.
    57  	if err := rlp.Encode(hasher, types.IstanbulFilteredHeader(header, false)); err != nil {
    58  		logger.Error("fail to encode", "err", err)
    59  		return common.Hash{}, err
    60  	}
    61  	hasher.Sum(hash[:0])
    62  	return hash, nil
    63  }
    64  
    65  func makeBlockGroupOutput(blockchain *blockchain.BlockChain, block *types.Block, cInfo consensus.ConsensusInfo, receipts types.Receipts) map[string]interface{} {
    66  	head := block.Header() // copies the header once
    67  	hash := head.Hash()
    68  
    69  	td := blockchain.GetTd(hash, block.NumberU64())
    70  	r, _ := klaytnApi.RpcOutputBlock(block, td, false, false, blockchain.Config().IsEthTxTypeForkEnabled(block.Header().Number))
    71  
    72  	// make transactions
    73  	transactions := block.Transactions()
    74  	numTxs := len(transactions)
    75  	rpcTransactions := make([]map[string]interface{}, numTxs)
    76  	for i, tx := range transactions {
    77  		rpcTransactions[i] = klaytnApi.RpcOutputReceipt(head, tx, hash, head.Number.Uint64(), uint64(i), receipts[i])
    78  	}
    79  
    80  	r["committee"] = cInfo.Committee
    81  	r["proposer"] = cInfo.Proposer
    82  	r["round"] = cInfo.Round
    83  	r["originProposer"] = cInfo.OriginProposer
    84  	r["transactions"] = rpcTransactions
    85  	return r
    86  }