|
5 | 5 | "fmt" |
6 | 6 | "math/big" |
7 | 7 | "net/http" |
8 | | - "os" |
9 | | - "os/exec" |
10 | | - "strings" |
11 | 8 | "testing" |
12 | 9 | "time" |
13 | 10 |
|
@@ -145,153 +142,6 @@ func TestMonitorBasic(t *testing.T) { |
145 | 142 | assert.Equal(t, 1, monitor.NumSubscribers()) |
146 | 143 | } |
147 | 144 |
|
148 | | -func GetIp(index uint) string { |
149 | | - output, err := exec.Command("yarn", "--silent", "--cwd", "../tools/reorgme", "chain:ip", "0").CombinedOutput() |
150 | | - |
151 | | - if err != nil { |
152 | | - os.Stderr.WriteString(err.Error()) |
153 | | - } |
154 | | - |
155 | | - return strings.Replace(string(output), "\n", "", 1) |
156 | | -} |
157 | | - |
158 | | -func WaitBlock(ctx context.Context, provider *ethrpc.Provider) error { |
159 | | - var lastBlock = uint64(0) |
160 | | - |
161 | | - fmt.Println("Waiting a block") |
162 | | - |
163 | | - for { |
164 | | - block, err := provider.BlockNumber(ctx) |
165 | | - if err != nil { |
166 | | - return err |
167 | | - } |
168 | | - |
169 | | - if lastBlock == 0 { |
170 | | - lastBlock = block |
171 | | - } |
172 | | - |
173 | | - if block != lastBlock { |
174 | | - return nil |
175 | | - } |
176 | | - } |
177 | | -} |
178 | | - |
179 | | -func Fork(index uint) string { |
180 | | - fmt.Println("Forking...") |
181 | | - output, err := exec.Command("yarn", "--silent", "--cwd", "../tools/reorgme", "chain:fork").CombinedOutput() |
182 | | - |
183 | | - if err != nil { |
184 | | - os.Stderr.WriteString(err.Error()) |
185 | | - } |
186 | | - |
187 | | - fmt.Println("Forked!") |
188 | | - |
189 | | - return string(output) |
190 | | -} |
191 | | - |
192 | | -func Join(index uint) string { |
193 | | - fmt.Println("Joining...") |
194 | | - output, err := exec.Command("yarn", "--silent", "--cwd", "../tools/reorgme", "chain:join").CombinedOutput() |
195 | | - |
196 | | - if err != nil { |
197 | | - os.Stderr.WriteString(err.Error()) |
198 | | - } |
199 | | - |
200 | | - fmt.Println("Joined!") |
201 | | - |
202 | | - return string(output) |
203 | | -} |
204 | | - |
205 | | -func TestMonitorWithReorgme(t *testing.T) { |
206 | | - if strings.ToLower(os.Getenv("REORGME")) != "true" { |
207 | | - t.Logf("REORGME is not enabled, skipping this test case.") |
208 | | - return |
209 | | - } |
210 | | - |
211 | | - ip := GetIp(0) |
212 | | - |
213 | | - provider, err := ethrpc.NewProvider("http://" + ip + ":8545/") |
214 | | - assert.NoError(t, err) |
215 | | - |
216 | | - monitorOptions := ethmonitor.DefaultOptions |
217 | | - monitorOptions.PollingInterval = 5 * time.Millisecond |
218 | | - |
219 | | - monitor, err := ethmonitor.NewMonitor(provider, monitorOptions) |
220 | | - assert.NoError(t, err) |
221 | | - |
222 | | - go func(t *testing.T) { |
223 | | - err := monitor.Run(context.Background()) |
224 | | - if err != nil { |
225 | | - panic(err) |
226 | | - } |
227 | | - }(t) |
228 | | - defer monitor.Stop() |
229 | | - |
230 | | - sub := monitor.Subscribe("TestMonitorWithReorgme") |
231 | | - defer sub.Unsubscribe() |
232 | | - |
233 | | - events := make([]*ethmonitor.Block, 0) |
234 | | - |
235 | | - go func() { |
236 | | - for { |
237 | | - select { |
238 | | - case blocks := <-sub.Blocks(): |
239 | | - _ = blocks |
240 | | - for _, b := range blocks { |
241 | | - events = append(events, b) |
242 | | - fmt.Println("event:", b.Event, "block:", b.NumberU64(), b.Hash().Hex(), "parent:", b.ParentHash().Hex(), "# logs:", len(b.Logs)) |
243 | | - } |
244 | | - case <-sub.Done(): |
245 | | - return |
246 | | - } |
247 | | - } |
248 | | - }() |
249 | | - |
250 | | - Fork(0) |
251 | | - events = make([]*ethmonitor.Block, 0) |
252 | | - |
253 | | - WaitBlock(context.Background(), provider) |
254 | | - WaitBlock(context.Background(), provider) |
255 | | - |
256 | | - time.Sleep(2 * time.Second) |
257 | | - |
258 | | - for _, b := range events { |
259 | | - assert.Equal(t, b.Event, ethmonitor.Added) |
260 | | - } |
261 | | - |
262 | | - revertedEvents := events |
263 | | - events = make([]*ethmonitor.Block, 0) |
264 | | - |
265 | | - Join(0) |
266 | | - |
267 | | - // Wait for reorg |
268 | | - WaitBlock(context.Background(), provider) |
269 | | - WaitBlock(context.Background(), provider) |
270 | | - |
271 | | - time.Sleep(2 * time.Second) |
272 | | - |
273 | | - offset := 0 |
274 | | - for _, e := range events { |
275 | | - if e.Block.Hash() == revertedEvents[len(revertedEvents)-1].Hash() { |
276 | | - break |
277 | | - } |
278 | | - |
279 | | - offset++ |
280 | | - } |
281 | | - |
282 | | - for i, b := range revertedEvents { |
283 | | - ri := len(revertedEvents) - 1 - i + offset |
284 | | - rb := events[ri] |
285 | | - |
286 | | - // Should revert last blocks |
287 | | - assert.Equal(t, rb.Block.Number(), b.Block.Number()) |
288 | | - assert.Equal(t, rb.Block.Hash(), b.Block.Hash()) |
289 | | - assert.Equal(t, b.Event, ethmonitor.Removed) |
290 | | - } |
291 | | - |
292 | | - monitor.Stop() |
293 | | -} |
294 | | - |
295 | 145 | func TestMonitorFeeHistory(t *testing.T) { |
296 | 146 | const N = 1 |
297 | 147 |
|
|
0 commit comments