forked from klaytn/klaytn
/
main_event_handler.go
127 lines (106 loc) · 4.74 KB
/
main_event_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2019 The klaytn Authors
// This file is part of the klaytn library.
//
// The klaytn library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The klaytn library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
package sc
import (
"errors"
"github.com/klaytn/klaytn/blockchain/types"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/ser/rlp"
)
var (
ErrGetServiceChainPHInMCEH = errors.New("ServiceChainPH isn't set in MainChainEventHandler")
)
type MainChainEventHandler struct {
mainbridge *MainBridge
handler *MainBridgeHandler
}
func NewMainChainEventHandler(bridge *MainBridge, handler *MainBridgeHandler) (*MainChainEventHandler, error) {
return &MainChainEventHandler{mainbridge: bridge, handler: handler}, nil
}
func (mce *MainChainEventHandler) HandleChainHeadEvent(block *types.Block) error {
logger.Debug("bridgeNode block number", "number", block.Number())
mce.writeChildChainTxHashFromBlock(block)
return nil
}
func (mce *MainChainEventHandler) HandleTxEvent(tx *types.Transaction) error {
//@TODO-Klaytn event handle
return nil
}
func (mce *MainChainEventHandler) HandleTxsEvent(txs []*types.Transaction) error {
//@TODO-Klaytn event handle
return nil
}
func (mce *MainChainEventHandler) HandleLogsEvent(logs []*types.Log) error {
//@TODO-Klaytn event handle
return nil
}
// GetChildChainIndexingEnabled returns the current child chain indexing configuration.
func (mce *MainChainEventHandler) GetChildChainIndexingEnabled() bool {
return mce.mainbridge.config.ChildChainIndexing
}
// GetLastIndexedBlockNumber returns the last child block number indexed to chain DB.
func (mce *MainChainEventHandler) GetLastIndexedBlockNumber() uint64 {
return mce.mainbridge.chainDB.GetLastIndexedBlockNumber()
}
// WriteLastIndexedBlockNumber writes the last child block number indexed to chain DB.
func (mce *MainChainEventHandler) WriteLastIndexedBlockNumber(blockNum uint64) {
mce.mainbridge.chainDB.WriteLastIndexedBlockNumber(blockNum)
}
// ConvertServiceChainBlockHashToMainChainTxHash returns a transaction hash of a transaction which contains
// ChainHashes, with the key made with given service chain block hash.
// Index is built when service chain indexing is enabled.
func (mce *MainChainEventHandler) ConvertServiceChainBlockHashToMainChainTxHash(scBlockHash common.Hash) common.Hash {
return mce.mainbridge.chainDB.ConvertServiceChainBlockHashToMainChainTxHash(scBlockHash)
}
// WriteChildChainTxHash stores a transaction hash of a transaction which contains
// ChainHashes, with the key made with given child chain block hash.
// Index is built when child chain indexing is enabled.
func (mce *MainChainEventHandler) WriteChildChainTxHash(ccBlockHash common.Hash, ccTxHash common.Hash) {
mce.mainbridge.chainDB.WriteChildChainTxHash(ccBlockHash, ccTxHash)
}
// writeChildChainTxHashFromBlock writes transaction hashes of transactions which contain
// ChainHashes.
func (mce *MainChainEventHandler) writeChildChainTxHashFromBlock(block *types.Block) {
if !mce.GetChildChainIndexingEnabled() {
logger.Trace("ChildChainIndexing is disabled. Skipped to write anchoring data on chainDB", "Head block", block.NumberU64())
return
}
lastIndexedBlkNum := mce.GetLastIndexedBlockNumber()
chainHeadBlkNum := block.NumberU64()
for i := lastIndexedBlkNum + 1; i <= chainHeadBlkNum; i++ {
blk := mce.mainbridge.blockchain.GetBlockByNumber(i)
txs := blk.Transactions()
for _, tx := range txs {
if tx.Type() != types.TxTypeChainDataAnchoring {
continue
}
chainHashes := new(types.ChainHashes)
data, err := tx.AnchoredData()
if err != nil {
logger.Error("writeChildChainTxHashFromBlock : failed to get anchoring data from the tx", "txHash", tx.Hash().String())
continue
}
if err := rlp.DecodeBytes(data, chainHashes); err != nil {
logger.Error("writeChildChainTxHashFromBlock : failed to decode anchoring data")
continue
}
mce.mainbridge.chainDB.WriteChildChainTxHash(chainHashes.BlockHash, tx.Hash())
logger.Trace("Write anchoring data on chainDB", "blockHash", chainHashes.BlockHash.String(), "txHash", tx.Hash().String())
}
}
logger.Trace("Done indexing Blocks", "begin", lastIndexedBlkNum+1, "end", chainHeadBlkNum)
mce.WriteLastIndexedBlockNumber(chainHeadBlkNum)
}