|
| 1 | +package syncer |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "math/big" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/ethereum/go-ethereum/common" |
| 11 | + "github.com/ethereum/go-ethereum/core/types" |
| 12 | + "github.com/ethereum/go-ethereum/log" |
| 13 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/medley/chainsync/client" |
| 14 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/medley/chainsync/event" |
| 15 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/medley/encodeable/number" |
| 16 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/medley/service" |
| 17 | + "gotest.tools/v3/assert" |
| 18 | +) |
| 19 | + |
| 20 | +func MakeChain(start int64, startParent common.Hash, numHeader uint, seed int64) []*types.Header { |
| 21 | + n := numHeader |
| 22 | + parent := startParent |
| 23 | + num := big.NewInt(start) |
| 24 | + h := []*types.Header{} |
| 25 | + |
| 26 | + // change the hashes for different seeds |
| 27 | + mixinh := common.BigToHash(big.NewInt(seed)) |
| 28 | + for n > 0 { |
| 29 | + head := &types.Header{ |
| 30 | + ParentHash: parent, |
| 31 | + Number: num, |
| 32 | + MixDigest: mixinh, |
| 33 | + } |
| 34 | + h = append(h, head) |
| 35 | + num = new(big.Int).Add(num, big.NewInt(1)) |
| 36 | + parent = head.Hash() |
| 37 | + n-- |
| 38 | + } |
| 39 | + return h |
| 40 | +} |
| 41 | + |
| 42 | +func TestReorg(t *testing.T) { |
| 43 | + headersBeforeReorg := MakeChain(1, common.BigToHash(big.NewInt(0)), 10, 42) |
| 44 | + branchOff := headersBeforeReorg[5] |
| 45 | + // block number 5 will be reorged |
| 46 | + headersReorgBranch := MakeChain(branchOff.Number.Int64()+1, branchOff.Hash(), 10, 43) |
| 47 | + clnt, ctl := client.NewTestClient() |
| 48 | + ctl.AppendNextHeaders(headersBeforeReorg...) |
| 49 | + ctl.AppendNextHeaders(headersReorgBranch...) |
| 50 | + |
| 51 | + handlerBlock := make(chan *event.LatestBlock, 1) |
| 52 | + |
| 53 | + h := &UnsafeHeadSyncer{ |
| 54 | + Client: clnt, |
| 55 | + Log: log.New(), |
| 56 | + Handler: func(_ context.Context, ev *event.LatestBlock) error { |
| 57 | + handlerBlock <- ev |
| 58 | + return nil |
| 59 | + }, |
| 60 | + SyncedHandler: []ManualFilterHandler{}, |
| 61 | + SyncStartBlock: number.NewBlockNumber(nil), |
| 62 | + FetchActiveAtStart: false, |
| 63 | + } |
| 64 | + |
| 65 | + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 66 | + defer cancel() |
| 67 | + service.RunBackground(ctx, h) |
| 68 | + |
| 69 | + // intitial sync is independent of the subscription, |
| 70 | + // this will get polled from the eth client |
| 71 | + b := <-handlerBlock |
| 72 | + assert.Assert(t, b.Number.Cmp(headersBeforeReorg[0].Number) == 0) |
| 73 | + idx := 1 |
| 74 | + for { |
| 75 | + ok := ctl.ProgressHead() |
| 76 | + assert.Assert(t, ok) |
| 77 | + err := ctl.EmitEvents(ctx) |
| 78 | + assert.NilError(t, err) |
| 79 | + |
| 80 | + b = <-handlerBlock |
| 81 | + assert.Equal(t, b.Number.Uint64(), headersBeforeReorg[idx].Number.Uint64(), fmt.Sprintf("block number equal for idx %d", idx)) |
| 82 | + assert.Equal(t, b.BlockHash, headersBeforeReorg[idx].Hash()) |
| 83 | + idx++ |
| 84 | + if idx == len(headersBeforeReorg) { |
| 85 | + break |
| 86 | + } |
| 87 | + } |
| 88 | + ok := ctl.ProgressHead() |
| 89 | + assert.Assert(t, ok) |
| 90 | + err := ctl.EmitEvents(ctx) |
| 91 | + assert.NilError(t, err) |
| 92 | + b = <-handlerBlock |
| 93 | + // now the reorg should have happened. |
| 94 | + // the handler should have emitted an "artificial" latest head |
| 95 | + // event for the block BEFORE the re-orged block |
| 96 | + assert.Equal(t, b.Number.Uint64(), headersReorgBranch[0].Number.Uint64()-1, "block number equal for reorg") |
| 97 | + assert.Equal(t, b.BlockHash, headersReorgBranch[0].ParentHash) |
| 98 | + idx = 0 |
| 99 | + for ctl.ProgressHead() { |
| 100 | + assert.Assert(t, ok) |
| 101 | + err := ctl.EmitEvents(ctx) |
| 102 | + assert.NilError(t, err) |
| 103 | + |
| 104 | + b := <-handlerBlock |
| 105 | + assert.Equal(t, b.Number.Uint64(), headersReorgBranch[idx].Number.Uint64(), fmt.Sprintf("block number equal for idx %d", idx)) |
| 106 | + assert.Equal(t, b.BlockHash, headersReorgBranch[idx].Hash()) |
| 107 | + idx++ |
| 108 | + if idx == len(headersReorgBranch) { |
| 109 | + break |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments