watch_test.go raw

   1  package domain
   2  
   3  import (
   4  	"encoding/hex"
   5  	"testing"
   6  
   7  	"git.smesh.lol/tinywallet/pkg/script"
   8  )
   9  
  10  // TestWatchRoundTrip: a watched script survives the store, is idempotent, and can be forgotten.
  11  func TestWatchRoundTrip(t *testing.T) {
  12  	r := newRig(t)
  13  
  14  	if _, err := r.domain.Restore("btc", phrase, "pass"); err != nil {
  15  		t.Fatal(err)
  16  	}
  17  
  18  	witness, err := script.HTLC(mustHex(t, deriveClaimer), mustHex(t, deriveRefunder), hashOf(deriveHash), 500)
  19  
  20  	if err != nil {
  21  		t.Fatal(err)
  22  	}
  23  
  24  	if err := r.domain.WatchHTLC("btc", witness); err != nil {
  25  		t.Fatal(err)
  26  	}
  27  
  28  	// Watching the same contract twice is one entry.
  29  	if err := r.domain.WatchHTLC("btc", witness); err != nil {
  30  		t.Fatal(err)
  31  	}
  32  
  33  	watches, err := r.domain.Watches("btc")
  34  
  35  	if err != nil {
  36  		t.Fatal(err)
  37  	}
  38  
  39  	if len(watches) != 1 || string(watches[0]) != string(script.P2WSH(witness)) {
  40  		t.Fatalf("watches are %d entries", len(watches))
  41  	}
  42  
  43  	if err := r.domain.WatchHTLC("nope", witness); err == nil {
  44  		t.Fatal("a chain the wallet does not drive was watched")
  45  	}
  46  
  47  	if err := r.domain.WatchHTLC("btc", nil); err == nil {
  48  		t.Fatal("an empty script was watched")
  49  	}
  50  
  51  	if err := r.domain.Unwatch("btc", script.P2WSH(witness)); err != nil {
  52  		t.Fatal(err)
  53  	}
  54  
  55  	if watches, _ := r.domain.Watches("btc"); len(watches) != 0 {
  56  		t.Fatalf("%d watched scripts after unwatch", len(watches))
  57  	}
  58  }
  59  
  60  // TestAWatchTurnsAnHTLCOutputIntoACoin: the point of the watch set. The wallet's own lookahead
  61  // never names a P2WSH contract, so a block paying it is invisible until the contract is watched.
  62  func TestAWatchTurnsAnHTLCOutputIntoACoin(t *testing.T) {
  63  	r := newRig(t)
  64  
  65  	if _, err := r.domain.Restore("btc", phrase, "pass"); err != nil {
  66  		t.Fatal(err)
  67  	}
  68  
  69  	witness, err := script.HTLC(mustHex(t, deriveClaimer), mustHex(t, deriveRefunder), hashOf(deriveHash), 500)
  70  
  71  	if err != nil {
  72  		t.Fatal(err)
  73  	}
  74  
  75  	chain := r.chains["btc"]
  76  	chain.syncTo = chaindataPosition(200)
  77  	chain.matched = []uint32{150}
  78  	chain.blocks = map[uint32][]byte{150: buildBlock(t, script.P2WSH(witness), 70_000)}
  79  
  80  	// Without the watch the block pays nobody the wallet scans for.
  81  	if _, err := r.domain.Sync("btc", 0); err != nil {
  82  		t.Fatal(err)
  83  	}
  84  
  85  	coins, err := r.domain.Unspent()
  86  
  87  	if err != nil {
  88  		t.Fatal(err)
  89  	}
  90  
  91  	if len(coins) != 0 {
  92  		t.Fatalf("%d coins before the contract was watched", len(coins))
  93  	}
  94  
  95  	// The real order is derive, watch, then fund: a contract watched before the block is
  96  	// scanned is found by the scan that sees it. (A height already marked scanned is not
  97  	// refetched, which is why the watch has to come first.)
  98  	second := newRig(t)
  99  
 100  	if _, err := second.domain.Restore("btc", phrase, "pass"); err != nil {
 101  		t.Fatal(err)
 102  	}
 103  
 104  	if err := second.domain.WatchHTLC("btc", witness); err != nil {
 105  		t.Fatal(err)
 106  	}
 107  
 108  	secondChain := second.chains["btc"]
 109  	secondChain.syncTo = chaindataPosition(200)
 110  	secondChain.matched = []uint32{150}
 111  	secondChain.blocks = map[uint32][]byte{150: buildBlock(t, script.P2WSH(witness), 70_000)}
 112  
 113  	if _, err := second.domain.Sync("btc", 0); err != nil {
 114  		t.Fatal(err)
 115  	}
 116  
 117  	coins, err = second.domain.Unspent()
 118  
 119  	if err != nil {
 120  		t.Fatal(err)
 121  	}
 122  
 123  	if len(coins) != 1 {
 124  		t.Fatalf("%d coins after the contract was watched", len(coins))
 125  	}
 126  
 127  	if coins[0].Value != 70_000 || coins[0].Height != 150 || coins[0].Chain != "btc" {
 128  		t.Fatalf("coin %+v", coins[0])
 129  	}
 130  
 131  	// The coin remembers the script that paid it, so a spender knows which key signs it.
 132  	if string(coins[0].Script) != string(script.P2WSH(witness)) {
 133  		t.Fatalf("coin script %x", coins[0].Script)
 134  	}
 135  
 136  	// And it belongs to this chain alone.
 137  	if other, _ := second.domain.UnspentOn("blake"); len(other) != 0 {
 138  		t.Fatalf("%d coins on the other chain", len(other))
 139  	}
 140  }
 141  
 142  // mustHex decodes test hex.
 143  func mustHex(t *testing.T, text string) []byte {
 144  	t.Helper()
 145  
 146  	raw, err := hex.DecodeString(text)
 147  
 148  	if err != nil {
 149  		t.Fatal(err)
 150  	}
 151  
 152  	return raw
 153  }
 154  
 155  // hashOf reads a 32-byte hex string.
 156  func hashOf(text string) [32]byte {
 157  	var out [32]byte
 158  
 159  	raw, _ := hex.DecodeString(text)
 160  
 161  	copy(out[:], raw)
 162  
 163  	return out
 164  }
 165