fork_test.go raw

   1  package domain
   2  
   3  import (
   4  	"errors"
   5  	"testing"
   6  
   7  	"git.smesh.lol/tinywallet/pkg/tx"
   8  )
   9  
  10  // forkReady restores the blake wallet, gives it one pre-fork coin at height 50
  11  // and schedules the fork at 100. That is the exact shared-history situation a
  12  // split exists for.
  13  func forkReady(t *testing.T, value uint64) (*rig, []byte) {
  14  	t.Helper()
  15  
  16  	r := newRig(t, "regtest")
  17  
  18  	if _, err := r.domain.Restore("blake", phrase, "pass"); err != nil {
  19  		t.Fatal(err)
  20  	}
  21  
  22  	address, err := r.domain.Addresses("blake", false, 0, 1)
  23  
  24  	if err != nil {
  25  		t.Fatal(err)
  26  	}
  27  
  28  	chain := r.chains["blake"]
  29  	chain.syncTo = chaindataPosition(60)
  30  	chain.matched = []uint32{50}
  31  	chain.blocks = map[uint32][]byte{50: buildBlock(t, address[0].Script(), value)}
  32  
  33  	if _, err := r.domain.Sync("blake", 0); err != nil {
  34  		t.Fatal(err)
  35  	}
  36  
  37  	link, err := r.domain.link("blake")
  38  
  39  	if err != nil {
  40  		t.Fatal(err)
  41  	}
  42  
  43  	link.Profile.Name = "blake"
  44  	link.Profile.ForkHeight = 100
  45  
  46  	r.domain.mu.Lock()
  47  	r.domain.links["blake"] = link
  48  	r.domain.mu.Unlock()
  49  
  50  	return r, address[0].Script()
  51  }
  52  
  53  // TestForkSplitsAPreForkCoin: one shared coin becomes this chain's own money,
  54  // signed SIGHASH_UNIFIED, and the source coin is spent.
  55  func TestForkSplitsAPreForkCoin(t *testing.T) {
  56  	r, _ := forkReady(t, 100_000)
  57  
  58  	shared, err := r.domain.PreForkCoins("blake")
  59  
  60  	if err != nil {
  61  		t.Fatal(err)
  62  	}
  63  
  64  	if len(shared) != 1 {
  65  		t.Fatalf("%d pre-fork coins, want 1", len(shared))
  66  	}
  67  
  68  	destination, err := r.domain.Addresses("blake", false, 9, 1)
  69  
  70  	if err != nil {
  71  		t.Fatal(err)
  72  	}
  73  
  74  	txid, err := r.domain.Fork("blake", shared[0].Outpoint(), destination[0].Text, 1_000, SendPolicy{})
  75  
  76  	if err != nil {
  77  		t.Fatalf("Fork: %v", err)
  78  	}
  79  
  80  	chain := r.chains["blake"]
  81  
  82  	if chain.broadcastCalls != 1 {
  83  		t.Fatalf("broadcast %d times", chain.broadcastCalls)
  84  	}
  85  
  86  	sent, err := tx.Decode(chain.broadcast[0])
  87  
  88  	if err != nil {
  89  		t.Fatal(err)
  90  	}
  91  
  92  	if got, _ := sent.TxID(); got != txid {
  93  		t.Fatalf("returned txid %x is not the transaction's %x", txid, got)
  94  	}
  95  
  96  	if len(sent.Inputs) != 1 || len(sent.Outputs) != 1 {
  97  		t.Fatalf("%d inputs, %d outputs; want one of each (the whole coin, no change)",
  98  			len(sent.Inputs), len(sent.Outputs))
  99  	}
 100  
 101  	if sent.Outputs[0].Value != 99_000 || string(sent.Outputs[0].Script) != string(destination[0].Script()) {
 102  		t.Fatalf("the split pays %+v", sent.Outputs[0])
 103  	}
 104  
 105  	item := sent.Inputs[0].Witness[0]
 106  
 107  	if len(item) == 0 || item[len(item)-1] != byte(tx.SighashAllUnified) {
 108  		t.Fatalf("the split was not signed SIGHASH_UNIFIED: %x", item)
 109  	}
 110  
 111  	if coins, _ := r.domain.UnspentOn("blake"); len(coins) != 0 {
 112  		t.Fatalf("%d coins remain after the split", len(coins))
 113  	}
 114  }
 115  
 116  // TestForkRefusesWhatIsNotShared: a post-fork coin, a wrong outpoint, the wrong
 117  // direction and a chain with no fork are each refused, and nothing broadcasts.
 118  func TestForkRefusesWhatIsNotShared(t *testing.T) {
 119  	r, script := forkReady(t, 100_000)
 120  
 121  	shared, err := r.domain.PreForkCoins("blake")
 122  
 123  	if err != nil {
 124  		t.Fatal(err)
 125  	}
 126  
 127  	chain := r.chains["blake"]
 128  
 129  	// A post-fork coin (height 150) is not shared history.
 130  	address, _ := r.domain.Addresses("blake", false, 3, 1)
 131  	chain.syncTo = chaindataPosition(200)
 132  	chain.matched = []uint32{150}
 133  	chain.blocks = map[uint32][]byte{150: buildBlock(t, address[0].Script(), 50_000)}
 134  
 135  	if _, err := r.domain.Sync("blake", 0); err != nil {
 136  		t.Fatal(err)
 137  	}
 138  
 139  	post, err := r.domain.UnspentOn("blake")
 140  
 141  	if err != nil {
 142  		t.Fatal(err)
 143  	}
 144  
 145  	var postOutpoint string
 146  
 147  	for _, coin := range post {
 148  		if coin.Height == 150 {
 149  			postOutpoint = coin.Outpoint()
 150  
 151  			break
 152  		}
 153  	}
 154  
 155  	destination, _ := r.domain.Addresses("blake", false, 12, 1)
 156  
 157  	if _, err := r.domain.Fork("blake", postOutpoint, destination[0].Text, 1_000, SendPolicy{}); !errors.Is(err, ErrNotPreFork) {
 158  		t.Fatalf("a post-fork coin: %v", err)
 159  	}
 160  
 161  	if _, err := r.domain.Fork("blake", "deadbeef:0", destination[0].Text, 1_000, SendPolicy{}); !errors.Is(err, ErrNoSuchCoin) {
 162  		t.Fatalf("an unknown outpoint: %v", err)
 163  	}
 164  
 165  	if _, err := r.domain.Fork("btc", shared[0].Outpoint(), destination[0].Text, 1_000, SendPolicy{}); err == nil {
 166  		t.Fatal("a split onto Bitcoin was accepted")
 167  	}
 168  
 169  	if chain.broadcastCalls != 0 {
 170  		t.Fatalf("a refused split broadcast %d times", chain.broadcastCalls)
 171  	}
 172  
 173  	// No fork scheduled: the split does not exist.
 174  	link, _ := r.domain.link("blake")
 175  	link.Profile.ForkHeight = 0
 176  	r.domain.mu.Lock()
 177  	r.domain.links["blake"] = link
 178  	r.domain.mu.Unlock()
 179  
 180  	if _, err := r.domain.PreForkCoins("blake"); !errors.Is(err, ErrNoFork) {
 181  		t.Fatalf("a no-fork chain lists pre-fork coins: %v", err)
 182  	}
 183  
 184  	if _, err := r.domain.Fork("blake", shared[0].Outpoint(), destination[0].Text, 1_000, SendPolicy{}); !errors.Is(err, ErrNoFork) {
 185  		t.Fatalf("a no-fork chain accepted a split: %v", err)
 186  	}
 187  
 188  	_ = script
 189  }
 190