-
Notifications
You must be signed in to change notification settings - Fork 32
/
backend_test.go
200 lines (152 loc) · 5.24 KB
/
backend_test.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package backend_test
import (
"github.com/synapsecns/sanguine/services/scribe/backend"
"github.com/synapsecns/sanguine/services/scribe/testutil"
"math/big"
"sync"
"testing"
. "github.com/stretchr/testify/assert"
"github.com/synapsecns/sanguine/ethergo/backends/geth"
"github.com/synapsecns/sanguine/ethergo/util"
"k8s.io/apimachinery/pkg/util/sets"
)
func (b *BackendSuite) TestLogsInRange() {
const desiredBlockHeight = 10
var testChainHandler *testutil.TestChainHandler
var err error
var wg sync.WaitGroup
wg.Add(2)
testBackend := geth.NewEmbeddedBackend(b.GetTestContext(), b.T())
go func() {
defer wg.Done()
testChainHandler, err = testutil.PopulateWithLogs(b.GetTestContext(), b.T(), testBackend, desiredBlockHeight, []*testutil.DeployManager{b.manager})
Nil(b.T(), err)
}()
var host string
go func() {
defer wg.Done()
host = testutil.StartOmnirpcServer(b.GetTestContext(), b.T(), testBackend)
}()
wg.Wait()
scribeBackend, err := backend.DialBackend(b.GetTestContext(), host, b.metrics)
Nil(b.T(), err)
chainID, err := scribeBackend.ChainID(b.GetTestContext())
Nil(b.T(), err)
lastBlock, err := testBackend.BlockNumber(b.GetTestContext())
Nil(b.T(), err)
iterator := util.NewChunkIterator(big.NewInt(int64(1)), big.NewInt(int64(lastBlock)), 1, true)
var blockRanges []*util.Chunk
blockRange := iterator.NextChunk()
for blockRange != nil {
blockRanges = append(blockRanges, blockRange)
blockRange = iterator.NextChunk()
}
res, err := backend.GetLogsInRange(b.GetTestContext(), scribeBackend, testChainHandler.Addresses, chainID.Uint64(), blockRanges, nil)
Nil(b.T(), err)
// use to make sure we don't double use values
intSet := sets.NewInt64()
itr := res.Iterator()
numLogs := 0
for !itr.Done() {
index, chunk := itr.Next()
Falsef(b.T(), intSet.Has(int64(index)), "%d appears at least twice", index)
intSet.Insert(int64(index))
NotNil(b.T(), chunk)
for range *chunk {
numLogs++
}
}
Equal(b.T(), int(testChainHandler.EventsEmitted[testChainHandler.Addresses[0]]), numLogs)
}
func (b *BackendSuite) TestLogsInRangeWithMultipleContracts() {
const desiredBlockHeight = 10
var testChainHandler *testutil.TestChainHandler
var err error
var wg sync.WaitGroup
wg.Add(2)
testBackend := geth.NewEmbeddedBackend(b.GetTestContext(), b.T())
managerB := testutil.NewDeployManager(b.T())
managerC := testutil.NewDeployManager(b.T())
managers := []*testutil.DeployManager{b.manager, managerB, managerC}
go func() {
defer wg.Done()
testChainHandler, err = testutil.PopulateWithLogs(b.GetTestContext(), b.T(), testBackend, desiredBlockHeight, managers)
Nil(b.T(), err)
}()
var host string
go func() {
defer wg.Done()
host = testutil.StartOmnirpcServer(b.GetTestContext(), b.T(), testBackend)
}()
wg.Wait()
scribeBackend, err := backend.DialBackend(b.GetTestContext(), host, b.metrics)
Nil(b.T(), err)
chainID, err := scribeBackend.ChainID(b.GetTestContext())
Nil(b.T(), err)
iterator := util.NewChunkIterator(big.NewInt(int64(1)), big.NewInt(int64(desiredBlockHeight)), 1, true)
var blockRanges []*util.Chunk
blockRange := iterator.NextChunk()
for blockRange != nil {
blockRanges = append(blockRanges, blockRange)
blockRange = iterator.NextChunk()
}
res, err := backend.GetLogsInRange(b.GetTestContext(), scribeBackend, testChainHandler.Addresses, chainID.Uint64(), blockRanges, nil)
Nil(b.T(), err)
// use to make sure we don't double use values
intSet := sets.NewInt64()
itr := res.Iterator()
numLogs := 0
logs := make(map[string]int)
for !itr.Done() {
index, chunk := itr.Next()
Falsef(b.T(), intSet.Has(int64(index)), "%d appears at least twice", index)
intSet.Insert(int64(index))
NotNil(b.T(), chunk)
for i := range *chunk {
logAddr := (*chunk)[i].Address.String()
logs[logAddr]++
numLogs++
}
}
Equal(b.T(), len(managers), numLogs)
// Check if there's a log for each of the contracts
for i := range testChainHandler.Addresses {
Equal(b.T(), int(testChainHandler.EventsEmitted[testChainHandler.Addresses[i]]), logs[testChainHandler.Addresses[i].String()])
}
}
func TestMakeRange(t *testing.T) {
testIntRange := backend.MakeRange(0, 4)
Equal(t, []int{0, 1, 2, 3, 4}, testIntRange)
testUintRange := backend.MakeRange(uint16(10), uint16(12))
Equal(t, testUintRange, []uint16{10, 11, 12})
}
func (b *BackendSuite) TestBlockHashesInRange() {
testBackend := geth.NewEmbeddedBackend(b.GetTestContext(), b.T())
// start an omnirpc proxy and run 10 test tranactions so we can batch call blocks
// 1-10
var wg sync.WaitGroup
wg.Add(2)
const desiredBlockHeight = 10
go func() {
defer wg.Done()
err := testutil.ReachBlockHeight(b.GetTestContext(), b.T(), testBackend, desiredBlockHeight)
Nil(b.T(), err)
}()
var host string
go func() {
defer wg.Done()
host = testutil.StartOmnirpcServer(b.GetTestContext(), b.T(), testBackend)
}()
wg.Wait()
scribeBackend, err := backend.DialBackend(b.GetTestContext(), host, b.metrics)
Nil(b.T(), err)
res, err := backend.BlockHashesInRange(b.GetTestContext(), scribeBackend, 1, 10)
Nil(b.T(), err)
// use to make sure we don't double use values
intSet := sets.NewInt64()
itr := res.Iterator()
for !itr.Done() {
index, _, _ := itr.Next()
Falsef(b.T(), intSet.Has(int64(index)), "%d appears at least twice", index)
}
}