github.com/klaytn/klaytn@v1.10.2/datasync/chaindatafetcher/kafka/repository.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  	"fmt"
    21  	"math/big"
    22  
    23  	"github.com/klaytn/klaytn/blockchain"
    24  	"github.com/klaytn/klaytn/blockchain/vm"
    25  	"github.com/klaytn/klaytn/consensus"
    26  	"github.com/klaytn/klaytn/datasync/chaindatafetcher/types"
    27  )
    28  
    29  type traceGroupResult struct {
    30  	BlockNumber      *big.Int              `json:"blockNumber"`
    31  	InternalTxTraces []*vm.InternalTxTrace `json:"result"`
    32  }
    33  
    34  func (r *traceGroupResult) Key() string {
    35  	return r.BlockNumber.String()
    36  }
    37  
    38  type blockGroupResult struct {
    39  	BlockNumber *big.Int               `json:"blockNumber"`
    40  	Result      map[string]interface{} `json:"result"`
    41  }
    42  
    43  func (r *blockGroupResult) Key() string {
    44  	return r.BlockNumber.String()
    45  }
    46  
    47  type repository struct {
    48  	blockchain *blockchain.BlockChain
    49  	engine     consensus.Engine
    50  	kafka      *Kafka
    51  }
    52  
    53  func NewRepository(config *KafkaConfig) (*repository, error) {
    54  	kafka, err := NewKafka(config)
    55  	if err != nil {
    56  		logger.Error("Failed to create a new Kafka structure", "err", err, "config", config)
    57  		return nil, err
    58  	}
    59  	return &repository{
    60  		kafka: kafka,
    61  	}, nil
    62  }
    63  
    64  func (r *repository) SetComponent(component interface{}) {
    65  	switch c := component.(type) {
    66  	case *blockchain.BlockChain:
    67  		r.blockchain = c
    68  	case consensus.Engine:
    69  		r.engine = c
    70  	}
    71  }
    72  
    73  func (r *repository) HandleChainEvent(event blockchain.ChainEvent, dataType types.RequestType) error {
    74  	switch dataType {
    75  	case types.RequestTypeBlockGroup:
    76  		cInfo, err := r.engine.GetConsensusInfo(event.Block)
    77  		if err != nil {
    78  			return fmt.Errorf("failed to retrieve consensusinfo with the given block number: %v", event.Block.Number())
    79  		}
    80  		result := &blockGroupResult{
    81  			BlockNumber: event.Block.Number(),
    82  			Result:      makeBlockGroupOutput(r.blockchain, event.Block, cInfo, event.Receipts),
    83  		}
    84  		return r.kafka.Publish(r.kafka.getTopicName(EventBlockGroup), result)
    85  	case types.RequestTypeTraceGroup:
    86  		if len(event.InternalTxTraces) > 0 {
    87  			result := &traceGroupResult{
    88  				BlockNumber:      event.Block.Number(),
    89  				InternalTxTraces: event.InternalTxTraces,
    90  			}
    91  			return r.kafka.Publish(r.kafka.getTopicName(EventTraceGroup), result)
    92  		}
    93  		return nil
    94  	default:
    95  		return fmt.Errorf("not supported type. [blockNumber: %v, reqType: %v]", event.Block.NumberU64(), dataType)
    96  	}
    97  }