-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
83 lines (75 loc) · 2.03 KB
/
events.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
package types
import (
gethcmn "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
gethtypes "github.com/ethereum/go-ethereum/core/types"
dbtypes "github.com/zeniqsmart/db-zeniq-smart-chain/types"
)
/*
type ChainEvent struct {
Block *types.Block
Hash common.Hash
Logs []*types.Log
}
*/
type ChainEvent struct {
BlockHeader *Header
Block *dbtypes.Block
Hash gethcmn.Hash
Logs []*gethtypes.Log
// TODO: define more fields
}
func BlockToChainEvent(mdbBlock *dbtypes.Block) ChainEvent {
return ChainEvent{
Hash: mdbBlock.BlockHash,
BlockHeader: toBlockHeader(mdbBlock),
Block: mdbBlock,
Logs: collectAllGethLogs(mdbBlock),
}
}
func collectAllGethLogs(mdbBlock *dbtypes.Block) []*gethtypes.Log {
logs := make([]*gethtypes.Log, 0, 8)
for _, mdbTx := range mdbBlock.TxList {
tx := &Transaction{}
_, err := tx.UnmarshalMsg(mdbTx.Content)
if err != nil { // ignore error
//panic(err)
println("failed to unmarshal tx:", err.Error())
}
for i, mdbLog := range mdbTx.LogList {
gethLog := &gethtypes.Log{
Address: mdbLog.Address,
Topics: ToGethHashes(mdbLog.Topics),
BlockNumber: uint64(mdbBlock.Height),
BlockHash: tx.BlockHash,
TxHash: tx.Hash,
TxIndex: uint(tx.TransactionIndex),
Index: uint(i),
}
if i < len(tx.Logs) {
gethLog.Data = tx.Logs[i].Data
}
logs = append(logs, gethLog)
}
}
return logs
}
func toBlockHeader(mdbBlock *dbtypes.Block) *Header {
block := &Block{}
_, err := block.UnmarshalMsg(mdbBlock.BlockInfo)
if err != nil { // ignore error
//panic(err)
println("failed to unmarshal block:", err.Error())
}
return &Header{
Number: hexutil.Uint64(mdbBlock.Height),
BlockHash: mdbBlock.BlockHash,
ParentHash: block.ParentHash,
Bloom: block.LogsBloom,
TxRoot: block.TransactionsRoot,
StateRoot: block.StateRoot,
Miner: block.Miner,
GasUsed: hexutil.Uint64(block.GasUsed),
Timestamp: hexutil.Uint64(block.Timestamp),
}
}