/
block_builder.go
123 lines (107 loc) · 2.84 KB
/
block_builder.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
package testutils
import (
gethcmn "github.com/ethereum/go-ethereum/common"
gethtypes "github.com/ethereum/go-ethereum/core/types"
modbtypes "github.com/smartbch/moeingdb/types"
"github.com/smartbch/moeingevm/types"
)
type MdbBlockBuilder struct {
block types.Block
txs []types.Transaction
}
func NewMdbBlockBuilder() *MdbBlockBuilder {
return &MdbBlockBuilder{}
}
func (bb *MdbBlockBuilder) Height(h int64) *MdbBlockBuilder {
bb.block.Number = h
return bb
}
func (bb *MdbBlockBuilder) Hash(hash gethcmn.Hash) *MdbBlockBuilder {
bb.block.Hash = hash
return bb
}
func (bb *MdbBlockBuilder) Tx(txHash gethcmn.Hash, logs ...types.Log) *MdbBlockBuilder {
bb.block.Transactions = append(bb.block.Transactions, txHash)
for _, log := range logs {
log.BlockNumber = uint64(bb.block.Number)
log.BlockHash = bb.block.Hash
log.TxHash = txHash
}
tx := types.Transaction{
BlockHash: bb.block.Hash,
BlockNumber: bb.block.Number,
Hash: txHash,
Logs: logs,
Status: 1,
}
bb.txs = append(bb.txs, tx)
return bb
}
func (bb *MdbBlockBuilder) FailedTx(txHash gethcmn.Hash, statusStr string, outData []byte) *MdbBlockBuilder {
bb.block.Transactions = append(bb.block.Transactions, txHash)
tx := types.Transaction{
BlockHash: bb.block.Hash,
BlockNumber: bb.block.Number,
Hash: txHash,
Status: 0,
StatusStr: statusStr,
OutData: outData,
}
bb.txs = append(bb.txs, tx)
return bb
}
func (bb *MdbBlockBuilder) TxWithAddr(txHash gethcmn.Hash, fromAddr, toAddr gethcmn.Address) *MdbBlockBuilder {
bb.block.Transactions = append(bb.block.Transactions, txHash)
tx := types.Transaction{
BlockHash: bb.block.Hash,
BlockNumber: bb.block.Number,
Hash: txHash,
From: fromAddr,
To: toAddr,
}
bb.txs = append(bb.txs, tx)
return bb
}
func (bb *MdbBlockBuilder) Build() *modbtypes.Block {
mdbTxList := make([]modbtypes.Tx, len(bb.txs))
for i, tx := range bb.txs {
txBytes, _ := tx.MarshalMsg(nil)
mdbTxList[i] = modbtypes.Tx{
HashId: tx.Hash,
Content: txBytes,
LogList: toMdbLogs(tx.Logs),
}
copy(mdbTxList[i].SrcAddr[:], tx.From[:])
copy(mdbTxList[i].DstAddr[:], tx.To[:])
}
bb.block.LogsBloom = createBloom(mdbTxList)
blockInfo, _ := bb.block.MarshalMsg(nil)
return &modbtypes.Block{
Height: bb.block.Number,
BlockHash: bb.block.Hash,
BlockInfo: blockInfo,
TxList: mdbTxList,
}
}
func toMdbLogs(logs []types.Log) []modbtypes.Log {
mdbLogs := make([]modbtypes.Log, len(logs))
for i, log := range logs {
mdbLogs[i] = modbtypes.Log{
Address: log.Address,
Topics: log.Topics,
}
}
return mdbLogs
}
func createBloom(txList []modbtypes.Tx) gethtypes.Bloom {
var bin gethtypes.Bloom
for _, tx := range txList {
for _, log := range tx.LogList {
bin.Add(log.Address[:])
for _, b := range log.Topics {
bin.Add(b[:])
}
}
}
return bin
}