scanner_test.go raw

   1  package scanner
   2  
   3  import (
   4  	"context"
   5  	"errors"
   6  	"testing"
   7  )
   8  
   9  // fakeChain records the windows it was asked for, which is the behaviour under test: the walk
  10  // must be a descending series of ascending windows, never one huge range.
  11  type fakeChain struct {
  12  	tip     uint32
  13  	windows [][2]uint32
  14  	matches map[uint32]bool
  15  	failAt  uint32
  16  }
  17  
  18  func (f *fakeChain) Tip(context.Context) (uint32, error) { return f.tip, nil }
  19  
  20  func (f *fakeChain) Fetch(_ context.Context, from, to uint32) error {
  21  	f.windows = append(f.windows, [2]uint32{from, to})
  22  
  23  	if f.failAt != 0 && to == f.failAt {
  24  		return errors.New("peer disconnected")
  25  	}
  26  
  27  	return nil
  28  }
  29  
  30  func (f *fakeChain) Match(_ context.Context, from, to uint32, _ [][]byte) ([]uint32, error) {
  31  	var out []uint32
  32  
  33  	for height := from; height <= to; height++ {
  34  		if f.matches[height] {
  35  			out = append(out, height)
  36  		}
  37  	}
  38  
  39  	return out, nil
  40  }
  41  
  42  // TestTheWalkGoesBackwardsInAscendingWindows: a node serves ascending ranges, so a backwards walk
  43  // is a series of them, newest first, and every window is within the protocol limit.
  44  func TestTheWalkGoesBackwardsInAscendingWindows(t *testing.T) {
  45  	chain := &fakeChain{tip: 2000, matches: map[uint32]bool{1700: true}}
  46  
  47  	result, err := Backwards(context.Background(), chain, [][]byte{{1}}, Options{Window: 500, Gap: 600})
  48  
  49  	if err != nil {
  50  		t.Fatal(err)
  51  	}
  52  
  53  	if len(result.Matched) != 1 || result.Matched[0] != 1700 {
  54  		t.Fatalf("matched %v", result.Matched)
  55  	}
  56  
  57  	want := [][2]uint32{{1501, 2000}, {1001, 1500}, {501, 1000}}
  58  
  59  	if len(chain.windows) != len(want) {
  60  		t.Fatalf("windows %v, want %v", chain.windows, want)
  61  	}
  62  
  63  	for i, window := range want {
  64  		if chain.windows[i] != window {
  65  			t.Fatalf("window %d is %v, want %v", i, chain.windows[i], window)
  66  		}
  67  
  68  		if chain.windows[i][1]-chain.windows[i][0] >= 1000 {
  69  			t.Fatalf("window %v spans more than the node allows", chain.windows[i])
  70  		}
  71  	}
  72  
  73  	if result.Stopped != "gap" {
  74  		t.Fatalf("stopped by %q, want gap", result.Stopped)
  75  	}
  76  }
  77  
  78  // TestTheWalkStopsAtItsFloor: a wallet with a known birthday never scans below it.
  79  func TestTheWalkStopsAtItsFloor(t *testing.T) {
  80  	chain := &fakeChain{tip: 1200, matches: map[uint32]bool{}}
  81  
  82  	result, err := Backwards(context.Background(), chain, [][]byte{{1}}, Options{Window: 500, Floor: 800, Gap: 1 << 20})
  83  
  84  	if err != nil {
  85  		t.Fatal(err)
  86  	}
  87  
  88  	if result.Stopped != "floor" || result.Lowest != 800 {
  89  		t.Fatalf("stopped %q at %d", result.Stopped, result.Lowest)
  90  	}
  91  }
  92  
  93  // TestAFailedWindowIsAnError: the walk reports the window it could not fetch rather than silently
  94  // skipping history.
  95  func TestAFailedWindowIsAnError(t *testing.T) {
  96  	chain := &fakeChain{tip: 2000, matches: map[uint32]bool{}, failAt: 1000}
  97  
  98  	if _, err := Backwards(context.Background(), chain, [][]byte{{1}}, Options{Window: 500, Gap: 1 << 20}); err == nil {
  99  		t.Fatal("a failed fetch was not reported")
 100  	}
 101  }
 102