-
Notifications
You must be signed in to change notification settings - Fork 476
/
test_backend.go
177 lines (140 loc) · 4.46 KB
/
test_backend.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package filters
import (
context "context"
"crypto/rand"
"math/big"
common "github.com/ethereum/go-ethereum/common"
core "github.com/ethereum/go-ethereum/core"
bloombits "github.com/ethereum/go-ethereum/core/bloombits"
"github.com/ethereum/go-ethereum/core/rawdb"
types "github.com/ethereum/go-ethereum/core/types"
ethdb "github.com/ethereum/go-ethereum/ethdb"
event "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/params"
rpc "github.com/ethereum/go-ethereum/rpc"
)
type TestBackend struct {
DB ethdb.Database
sections uint64
txFeed event.Feed
logsFeed event.Feed
rmLogsFeed event.Feed
pendingLogsFeed event.Feed
chainFeed event.Feed
stateSyncFeed event.Feed
}
func (b *TestBackend) BloomStatus() (uint64, uint64) {
return params.BloomBitsBlocks, b.sections
}
func (b *TestBackend) GetBorBlockReceipt(ctx context.Context, hash common.Hash) (*types.Receipt, error) {
number := rawdb.ReadHeaderNumber(b.DB, hash)
if number == nil {
return &types.Receipt{}, nil
}
receipt := rawdb.ReadBorReceipt(b.DB, hash, *number)
if receipt == nil {
return &types.Receipt{}, nil
}
return receipt, nil
}
func (b *TestBackend) GetBorBlockLogs(ctx context.Context, hash common.Hash) ([]*types.Log, error) {
receipt, err := b.GetBorBlockReceipt(ctx, hash)
if err != nil {
return []*types.Log{}, err
}
if receipt == nil {
return []*types.Log{}, nil
}
return receipt.Logs, nil
}
func (b *TestBackend) ChainDb() ethdb.Database {
return b.DB
}
func (b *TestBackend) HeaderByNumber(ctx context.Context, blockNr rpc.BlockNumber) (*types.Header, error) {
var (
hash common.Hash
num uint64
)
if blockNr == rpc.LatestBlockNumber {
hash = rawdb.ReadHeadBlockHash(b.DB)
number := rawdb.ReadHeaderNumber(b.DB, hash)
if number == nil {
return &types.Header{}, nil
}
num = *number
} else {
num = uint64(blockNr)
hash = rawdb.ReadCanonicalHash(b.DB, num)
}
return rawdb.ReadHeader(b.DB, hash, num), nil
}
func (b *TestBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
number := rawdb.ReadHeaderNumber(b.DB, hash)
if number == nil {
return &types.Header{}, nil
}
return rawdb.ReadHeader(b.DB, hash, *number), nil
}
func (b *TestBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) {
if number := rawdb.ReadHeaderNumber(b.DB, hash); number != nil {
return rawdb.ReadReceipts(b.DB, hash, *number, params.TestChainConfig), nil
}
return nil, nil
}
func (b *TestBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
number := rawdb.ReadHeaderNumber(b.DB, hash)
if number == nil {
return nil, nil
}
receipts := rawdb.ReadReceipts(b.DB, hash, *number, params.TestChainConfig)
logs := make([][]*types.Log, len(receipts))
for i, receipt := range receipts {
logs[i] = receipt.Logs
}
return logs, nil
}
func (b *TestBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
return b.txFeed.Subscribe(ch)
}
func (b *TestBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
return b.rmLogsFeed.Subscribe(ch)
}
func (b *TestBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription {
return b.logsFeed.Subscribe(ch)
}
func (b *TestBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription {
return b.pendingLogsFeed.Subscribe(ch)
}
func (b *TestBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription {
return b.chainFeed.Subscribe(ch)
}
func (b *TestBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
requests := make(chan chan *bloombits.Retrieval)
go session.Multiplex(16, 0, requests)
go func() {
for {
// Wait for a service request or a shutdown
select {
case <-ctx.Done():
return
case request := <-requests:
task := <-request
task.Bitsets = make([][]byte, len(task.Sections))
for i, section := range task.Sections {
nBig, err := rand.Int(rand.Reader, big.NewInt(100))
if err != nil {
panic(err)
}
if nBig.Int64()%4 != 0 { // Handle occasional missing deliveries
head := rawdb.ReadCanonicalHash(b.DB, (section+1)*params.BloomBitsBlocks-1)
task.Bitsets[i], _ = rawdb.ReadBloomBits(b.DB, task.Bit, section, head)
}
}
request <- task
}
}
}()
}
func (b *TestBackend) SubscribeStateSyncEvent(ch chan<- core.StateSyncEvent) event.Subscription {
return b.stateSyncFeed.Subscribe(ch)
}