Skip to content

Commit

Permalink
test(sync/fork): Implement a roundabout fork following test to ensure…
Browse files Browse the repository at this point in the history
… the syncer crashes if it detects that it has been following a fork
  • Loading branch information
renaynay committed Sep 20, 2023
1 parent a7eadaf commit c9352b5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
16 changes: 12 additions & 4 deletions sync/fork_test/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,28 @@ func main() {
panic(err)
}

logging.Logger("sync")
err = logging.SetLogLevel("sync", "debug")
_, err = ee.trustedPeer.GetByHeight(ctx, 100)
if err != nil {
panic(err)
}
_, err = ee.eclipsedExchange.GetByHeight(ctx, 100)
if err != nil {
panic(err)
}

logging.Logger("sync")

err = syncer.Start(ctx)
if err != nil {
panic(err)
}

time.Sleep(100 * time.Millisecond)
// this sleep is necessary to allow the syncer to trigger a job
// as calling SyncWait prematurely may falsely return without error
// as the syncer has not yet registered a sync job.
//time.Sleep(time.Millisecond * 100)
time.Sleep(time.Millisecond * 500)
syncer.SyncWait(ctx) //nolint:errcheck

}

// eclipsedExchange is an exchange that can serve a good Head to the syncer
Expand Down
3 changes: 0 additions & 3 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,9 @@ func (s *Syncer[H]) wantSync() {

// syncLoop controls syncing process.
func (s *Syncer[H]) syncLoop() {
fmt.Println("Spawned")
for {
select {
case <-s.triggerSync:
fmt.Println("triggering sync")
s.sync(s.ctx)
case <-s.ctx.Done():
return
Expand All @@ -192,7 +190,6 @@ func (s *Syncer[H]) sync(ctx context.Context) {
log.Errorw("getting sync target", "err", err)
return
}
fmt.Println("got sync target: ", target.Height())

storeHead, err := s.store.Head(ctx)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions sync/sync_fork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import (
// 4. sync up to subjective head - 1, try to apply subjective head (fails)
// 5. ensure fork is tossed and Fatal is thrown
func TestForkFollowingPrevention(t *testing.T) {

path, err := filepath.Abs("./fork_test/fork.go")
require.NoError(t, err)

cmd := exec.Command("go", "run", path)
cmd.Stdout = os.Stdout
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
err = cmd.Run()
t.Log("err: ", err)
require.Error(t, err)
require.Contains(t, err.Error(), "exit status 1")
}

0 comments on commit c9352b5

Please sign in to comment.