Skip to content

Commit

Permalink
Add not linear error to check against
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopeereboom committed Jul 2, 2024
1 parent 7c8d403 commit 9dcfb76
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
35 changes: 23 additions & 12 deletions service/tbc/crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
var (
UtxoIndexHashKey = []byte("utxoindexhash") // last indexed utxo hash
TxIndexHashKey = []byte("txindexhash") // last indexed tx hash

ErrNotLinear = errors.New("not linear") // not a valid chain
)

type HashHeight struct {
Expand Down Expand Up @@ -392,12 +394,13 @@ func (s *Server) UtxoIndexer(ctx context.Context, endHash *chainhash.Hash) error
if err != nil {
return fmt.Errorf("blockheader hash: %w", err)
}
if endBH.Difficulty.Cmp(&startBH.Difficulty) >= 1 {
return s.UtxoIndexerWind(ctx, startBH, endBH)
switch endBH.Difficulty.Cmp(&startBH.Difficulty) {
case 1:
return s.TxIndexerWind(ctx, startBH, endBH)
case -1:
return s.TxIndexerUnwind(ctx, endBH, startBH)
}

// start > end thus we must unwind
return s.UtxoIndexerUnwind(ctx, endBH, startBH)
return ErrNotLinear
}

func processTxs(cp *chaincfg.Params, blockHash *chainhash.Hash, txs []*btcutil.Tx, txsCache map[tbcd.TxKey]*tbcd.TxValue) error {
Expand Down Expand Up @@ -602,17 +605,19 @@ func (s *Server) TxIndexer(ctx context.Context, endHash *chainhash.Hash) error {
Height: 0,
}
}

// XXX make sure there is no gap between start and end or vice versa.
startBH, err := s.db.BlockHeaderByHash(ctx, txHH.Hash[:])
if err != nil {
return fmt.Errorf("blockheader hash: %w", err)
}
if endBH.Difficulty.Cmp(&startBH.Difficulty) >= 1 {
switch endBH.Difficulty.Cmp(&startBH.Difficulty) {
case 1:
return s.TxIndexerWind(ctx, startBH, endBH)
case -1:
return s.TxIndexerUnwind(ctx, endBH, startBH)
}

// start > end thus we must unwind
return s.TxIndexerUnwind(ctx, endBH, startBH)
return ErrNotLinear
}

func (s *Server) TxIndexIsLinear(ctx context.Context, endHash *chainhash.Hash) (int, error) {
Expand Down Expand Up @@ -656,12 +661,18 @@ func (s *Server) TxIndexIsLinear(ctx context.Context, endHash *chainhash.Hash) (
// Always walk backwards because it's only a single lookup.
// XXX is direction = 0 even meaningful?
var h, e *chainhash.Hash
if direction > 0 {
switch direction {
case 1:
h = endBH.BlockHash()
e = startBH.BlockHash()
} else {
case -1:
h = startBH.BlockHash()
e = endBH.BlockHash()
default:
return 0, ErrNotLinear
}
if direction > 0 {
} else {
}
for {
bh, err := s.db.BlockHeaderByHash(ctx, h[:])
Expand All @@ -673,7 +684,7 @@ func (s *Server) TxIndexIsLinear(ctx context.Context, endHash *chainhash.Hash) (
return direction, nil
}
if h.IsEqual(s.chainParams.GenesisHash) {
return direction, fmt.Errorf("not linear")
return direction, ErrNotLinear
}
}
}
Expand Down
18 changes: 12 additions & 6 deletions service/tbc/tbcfork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ func TestIndexFork(t *testing.T) {
if err != nil {
t.Fatalf("expected success b3 -> genesis, got %v", err)
}
if direction >= 1 {
if direction != -1 {
t.Fatalf("expected -1 going from b3 to genesis, got %v", direction)
}

Expand All @@ -987,19 +987,25 @@ func TestIndexFork(t *testing.T) {
if err != nil {
t.Fatalf("expected success b3 -> b1, got %v", err)
}
if direction >= 1 {
if direction != -1 {
t.Fatalf("expected -1 going from b3 to genesis, got %v", direction)
}
// b3 -> b2a should fail
direction, err = s.TxIndexIsLinear(ctx, b2a.Hash())
if err == nil {
t.Fatal("b2a is not linear to b3")
if !errors.Is(err, ErrNotLinear) {
t.Fatalf("b2a is not linear to b3: %v", err)
}

// b3 -> b2b should fail
direction, err = s.TxIndexIsLinear(ctx, b2b.Hash())
if err == nil {
t.Fatal("b2b is not linear to b3")
if !errors.Is(err, ErrNotLinear) {
t.Fatalf("b2b is not linear to b3: %v", err)
}

// unwind back to genesis
err = s.SyncIndexersToHash(ctx, b3.Hash())
if !errors.Is(err, ErrNotLinear) {
t.Fatalf("at b3, should have returned not linear, got %v", err)
}

//// Should fail
Expand Down

0 comments on commit 9dcfb76

Please sign in to comment.