github.com/klaytn/klaytn@v1.12.1/node/sc/main_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 22 "github.com/klaytn/klaytn/blockchain/types" 23 "github.com/klaytn/klaytn/common" 24 ) 25 26 var ErrGetServiceChainPHInMCEH = errors.New("ServiceChainPH isn't set in MainChainEventHandler") 27 28 type MainChainEventHandler struct { 29 mainbridge *MainBridge 30 31 handler *MainBridgeHandler 32 } 33 34 func NewMainChainEventHandler(bridge *MainBridge, handler *MainBridgeHandler) (*MainChainEventHandler, error) { 35 return &MainChainEventHandler{mainbridge: bridge, handler: handler}, nil 36 } 37 38 func (mce *MainChainEventHandler) HandleChainHeadEvent(block *types.Block) error { 39 logger.Trace("bridgeNode block number", "number", block.Number()) 40 mce.writeChildChainTxHashFromBlock(block) 41 return nil 42 } 43 44 func (mce *MainChainEventHandler) HandleTxEvent(tx *types.Transaction) error { 45 //@TODO-Klaytn event handle 46 return nil 47 } 48 49 func (mce *MainChainEventHandler) HandleTxsEvent(txs []*types.Transaction) error { 50 //@TODO-Klaytn event handle 51 return nil 52 } 53 54 func (mce *MainChainEventHandler) HandleLogsEvent(logs []*types.Log) error { 55 //@TODO-Klaytn event handle 56 return nil 57 } 58 59 // GetChildChainIndexingEnabled returns the current child chain indexing configuration. 60 func (mce *MainChainEventHandler) GetChildChainIndexingEnabled() bool { 61 return mce.mainbridge.config.ChildChainIndexing 62 } 63 64 // GetLastIndexedBlockNumber returns the last child block number indexed to chain DB. 65 func (mce *MainChainEventHandler) GetLastIndexedBlockNumber() uint64 { 66 return mce.mainbridge.chainDB.GetLastIndexedBlockNumber() 67 } 68 69 // WriteLastIndexedBlockNumber writes the last child block number indexed to chain DB. 70 func (mce *MainChainEventHandler) WriteLastIndexedBlockNumber(blockNum uint64) { 71 mce.mainbridge.chainDB.WriteLastIndexedBlockNumber(blockNum) 72 } 73 74 // ConvertChildChainBlockHashToParentChainTxHash returns a transaction hash of a transaction which contains 75 // AnchoringData, with the key made with given child chain block hash. 76 // Index is built when service chain indexing is enabled. 77 func (mce *MainChainEventHandler) ConvertChildChainBlockHashToParentChainTxHash(scBlockHash common.Hash) common.Hash { 78 return mce.mainbridge.chainDB.ConvertChildChainBlockHashToParentChainTxHash(scBlockHash) 79 } 80 81 // decodeAndWriteAnchoringTx decodes and stores a transaction hash of a transaction which contains 82 // AnchoringData, with the key made with given child chain block hash. 83 // Index is built when child chain indexing is enabled. 84 func (mce *MainChainEventHandler) decodeAndWriteAnchoringTx(tx *types.Transaction) { 85 data, err := tx.AnchoredData() 86 if err != nil { 87 logger.Error("failed to get anchoring data", "txHash", tx.Hash().String(), "err", err) 88 return 89 } 90 decodedData, err := types.DecodeAnchoringData(data) 91 if err != nil { 92 logger.Warn("failed to decode anchoring tx", "txHash", tx.Hash().String(), "err", err) 93 return 94 } 95 mce.mainbridge.chainDB.WriteChildChainTxHash(decodedData.GetBlockHash(), tx.Hash()) 96 logger.Trace("Write anchoring data on chainDB", "blockHash", decodedData.GetBlockNumber().String(), "anchoring txHash", tx.Hash().String()) 97 } 98 99 // TODO-Klaytn-ServiceChain: remove this method and a related option. 100 // writeChildChainTxHashFromBlock writes transaction hashes of transactions which contain 101 // AnchoringData. 102 func (mce *MainChainEventHandler) writeChildChainTxHashFromBlock(block *types.Block) { 103 if !mce.GetChildChainIndexingEnabled() { 104 logger.Trace("ChildChainIndexing is disabled. Skipped to write anchoring data on chainDB", "Head block", block.NumberU64()) 105 return 106 } 107 108 lastIndexedBlkNum := mce.GetLastIndexedBlockNumber() 109 chainHeadBlkNum := block.NumberU64() 110 111 for i := lastIndexedBlkNum + 1; i <= chainHeadBlkNum; i++ { 112 blk := mce.mainbridge.blockchain.GetBlockByNumber(i) 113 114 txs := blk.Transactions() 115 for _, tx := range txs { 116 if tx.Type().IsChainDataAnchoring() { 117 mce.decodeAndWriteAnchoringTx(tx) 118 } 119 } 120 } 121 logger.Trace("Done indexing Blocks", "begin", lastIndexedBlkNum+1, "end", chainHeadBlkNum) 122 mce.WriteLastIndexedBlockNumber(chainHeadBlkNum) 123 }