order_test.go raw

   1  package utxostore
   2  
   3  import (
   4  	"sort"
   5  	"testing"
   6  
   7  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
   8  
   9  	"git.smesh.lol/btcd-lite/internal/utxo"
  10  	"git.smesh.lol/btcd-lite/internal/wire"
  11  )
  12  
  13  // twoOps are the two outpoints whose order the memory sort and the key file
  14  // disagreed on: a and b sort one way by their stored bytes and the other way
  15  // when the hash is read from its end.
  16  func twoOps(t *testing.T) (wire.OutPoint, wire.OutPoint) {
  17  	t.Helper()
  18  	ha, err := chainhash.NewHashFromStr("2d5f4dd2d318e94886272c591c58d3386b4396d83b68e669b673cbe9a9eb1a0a")
  19  	if err != nil {
  20  		t.Fatal(err)
  21  	}
  22  	hb, err := chainhash.NewHashFromStr("439733ee21a83b5e6e8b2743846345b2d690624868141df4879d046fe1d67e20")
  23  	if err != nil {
  24  		t.Fatal(err)
  25  	}
  26  	return wire.OutPoint{Hash: *ha, Index: 1}, wire.OutPoint{Hash: *hb, Index: 0}
  27  }
  28  
  29  // TestOneOrderForOutpoints: the merge's comparison and the set's comparison are
  30  // one order, and the key lists are sorted by that order and not by the other.
  31  //
  32  // This is the defect that took real chain keys to find: writeKeys sorted keys
  33  // with a comparison that differed from the set's on a pair of outpoints, the
  34  // merge walked the base with one order and read keys in the other, and the set
  35  // it wrote came out with the right entries in an order nothing else produced.
  36  func TestOneOrderForOutpoints(t *testing.T) {
  37  	t.Run("key lists use the set's order", func(t *testing.T) {
  38  		var ops []wire.OutPoint
  39  		for i := uint32(0); i < 500; i++ {
  40  			var h chainhash.Hash
  41  			h[0] = byte(i >> 8)
  42  			h[1] = byte(i)
  43  			h[31] = byte(255 - i%7)
  44  			ops = append(ops, wire.OutPoint{Hash: h, Index: i % 3})
  45  		}
  46  		sortBySet(ops)
  47  		for i := 1; i < len(ops); i++ {
  48  			if !utxo.LessEntry(utxo.Entry{OutPoint: ops[i-1]}, utxo.Entry{OutPoint: ops[i]}) {
  49  				t.Fatalf("the key list is out of the set's order at %d", i)
  50  			}
  51  		}
  52  	})
  53  	t.Run("the two comparisons agree on the pair", testPairOrder)
  54  }
  55  
  56  func testPairOrder(t *testing.T) {
  57  	a, b := twoOps(t)
  58  	byMerge := cmpOp(a, b)
  59  	bySet := 0
  60  	if utxo.LessEntry(utxo.Entry{OutPoint: a}, utxo.Entry{OutPoint: b}) {
  61  		bySet = -1
  62  	} else if utxo.LessEntry(utxo.Entry{OutPoint: b}, utxo.Entry{OutPoint: a}) {
  63  		bySet = 1
  64  	}
  65  	if byMerge != bySet {
  66  		// Report both readings so the fix is a decision and not a guess.
  67  		ra, rb := reverse(a.Hash), reverse(b.Hash)
  68  		t.Fatalf("the merge compares a to b as %d, the set as %d; bytes order %x vs %x, read from the end %x vs %x",
  69  			byMerge, bySet, a.Hash[:4], b.Hash[:4], ra[:4], rb[:4])
  70  	}
  71  	// And the order is the one the set uses everywhere: a sorts first here.
  72  	if bySet >= 0 {
  73  		t.Fatalf("a does not sort before b")
  74  	}
  75  	// A sort of the pair agrees with the comparison.
  76  	pair := []wire.OutPoint{b, a}
  77  	sort.Slice(pair, func(i, j int) bool { return cmpOp(pair[i], pair[j]) < 0 })
  78  	if pair[0] != a {
  79  		t.Fatal("sorting the pair does not put a first")
  80  	}
  81  }
  82  
  83  func reverse(h chainhash.Hash) chainhash.Hash {
  84  	var out chainhash.Hash
  85  	for i := 0; i < 32; i++ {
  86  		out[i] = h[31-i]
  87  	}
  88  	return out
  89  }
  90