github.com/klaytn/klaytn@v1.12.1/node/sc/sub_event_handler.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 sc
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  
    23  	"github.com/klaytn/klaytn/blockchain/types"
    24  	"github.com/klaytn/klaytn/common"
    25  )
    26  
    27  var ErrGetServiceChainPHInCCEH = errors.New("ServiceChainPH isn't set in ChildChainEventHandler")
    28  
    29  type ChildChainEventHandler struct {
    30  	subbridge *SubBridge
    31  
    32  	handler *SubBridgeHandler
    33  }
    34  
    35  const (
    36  	// TODO-Klaytn need to define proper value.
    37  	errorDiffRequestHandleNonce = 10000
    38  )
    39  
    40  func NewChildChainEventHandler(bridge *SubBridge, handler *SubBridgeHandler) (*ChildChainEventHandler, error) {
    41  	return &ChildChainEventHandler{subbridge: bridge, handler: handler}, nil
    42  }
    43  
    44  func (cce *ChildChainEventHandler) HandleChainHeadEvent(block *types.Block) error {
    45  	logger.Trace("bridgeNode block number", "number", block.Number())
    46  	cce.handler.LocalChainHeadEvent(block)
    47  
    48  	// Logging information of value transfer
    49  	cce.subbridge.bridgeManager.LogBridgeStatus()
    50  
    51  	return nil
    52  }
    53  
    54  func (cce *ChildChainEventHandler) HandleTxEvent(tx *types.Transaction) error {
    55  	// TODO-Klaytn event handle
    56  	return nil
    57  }
    58  
    59  func (cce *ChildChainEventHandler) HandleTxsEvent(txs []*types.Transaction) error {
    60  	// TODO-Klaytn event handle
    61  	return nil
    62  }
    63  
    64  func (cce *ChildChainEventHandler) HandleLogsEvent(logs []*types.Log) error {
    65  	// TODO-Klaytn event handle
    66  	return nil
    67  }
    68  
    69  func (cce *ChildChainEventHandler) ProcessRequestEvent(ev IRequestValueTransferEvent) error {
    70  	addr := ev.GetRaw().Address
    71  
    72  	handleBridgeAddr := cce.subbridge.bridgeManager.GetCounterPartBridgeAddr(addr)
    73  	if handleBridgeAddr == (common.Address{}) {
    74  		return fmt.Errorf("there is no counter part bridge of the bridge(%v)", addr.String())
    75  	}
    76  
    77  	handleBridgeInfo, ok := cce.subbridge.bridgeManager.GetBridgeInfo(handleBridgeAddr)
    78  	if !ok {
    79  		return fmt.Errorf("there is no counter part bridge info(%v) of the bridge(%v)", handleBridgeAddr.String(), addr.String())
    80  	}
    81  
    82  	// TODO-Klaytn need to manage the size limitation of pending event list.
    83  	handleBridgeInfo.AddRequestValueTransferEvents([]IRequestValueTransferEvent{ev})
    84  	return nil
    85  }
    86  
    87  func (cce *ChildChainEventHandler) ProcessHandleEvent(ev *HandleValueTransferEvent) error {
    88  	handleBridgeInfo, ok := cce.subbridge.bridgeManager.GetBridgeInfo(ev.Raw.Address)
    89  	if !ok {
    90  		return errors.New("there is no bridge")
    91  	}
    92  
    93  	handleBridgeInfo.MarkHandledNonce(ev.HandleNonce)
    94  	handleBridgeInfo.UpdateLowerHandleNonce(ev.LowerHandleNonce)
    95  
    96  	logger.Trace("RequestValueTransfer Event",
    97  		"bridgeAddr", ev.Raw.Address.String(),
    98  		"handleNonce", ev.HandleNonce,
    99  		"to", ev.To.String(),
   100  		"valueType", ev.TokenType,
   101  		"token/NFT contract", ev.TokenAddress,
   102  		"value", ev.ValueOrTokenId.String())
   103  	return nil
   104  }
   105  
   106  // ConvertChildChainBlockHashToParentChainTxHash returns a transaction hash of a transaction which contains
   107  // AnchoringData, with the key made with given child chain block hash.
   108  // Index is built when child chain indexing is enabled.
   109  func (cce *ChildChainEventHandler) ConvertChildChainBlockHashToParentChainTxHash(scBlockHash common.Hash) common.Hash {
   110  	return cce.subbridge.chainDB.ConvertChildChainBlockHashToParentChainTxHash(scBlockHash)
   111  }