package domain import ( "errors" "testing" "git.smesh.lol/tinywallet/pkg/tx" ) // forkReady restores the blake wallet, gives it one pre-fork coin at height 50 // and schedules the fork at 100. That is the exact shared-history situation a // split exists for. func forkReady(t *testing.T, value uint64) (*rig, []byte) { t.Helper() r := newRig(t, "regtest") if _, err := r.domain.Restore("blake", phrase, "pass"); err != nil { t.Fatal(err) } address, err := r.domain.Addresses("blake", false, 0, 1) if err != nil { t.Fatal(err) } chain := r.chains["blake"] chain.syncTo = chaindataPosition(60) chain.matched = []uint32{50} chain.blocks = map[uint32][]byte{50: buildBlock(t, address[0].Script(), value)} if _, err := r.domain.Sync("blake", 0); err != nil { t.Fatal(err) } link, err := r.domain.link("blake") if err != nil { t.Fatal(err) } link.Profile.Name = "blake" link.Profile.ForkHeight = 100 r.domain.mu.Lock() r.domain.links["blake"] = link r.domain.mu.Unlock() return r, address[0].Script() } // TestForkSplitsAPreForkCoin: one shared coin becomes this chain's own money, // signed SIGHASH_UNIFIED, and the source coin is spent. func TestForkSplitsAPreForkCoin(t *testing.T) { r, _ := forkReady(t, 100_000) shared, err := r.domain.PreForkCoins("blake") if err != nil { t.Fatal(err) } if len(shared) != 1 { t.Fatalf("%d pre-fork coins, want 1", len(shared)) } destination, err := r.domain.Addresses("blake", false, 9, 1) if err != nil { t.Fatal(err) } txid, err := r.domain.Fork("blake", shared[0].Outpoint(), destination[0].Text, 1_000, SendPolicy{}) if err != nil { t.Fatalf("Fork: %v", err) } chain := r.chains["blake"] if chain.broadcastCalls != 1 { t.Fatalf("broadcast %d times", chain.broadcastCalls) } sent, err := tx.Decode(chain.broadcast[0]) if err != nil { t.Fatal(err) } if got, _ := sent.TxID(); got != txid { t.Fatalf("returned txid %x is not the transaction's %x", txid, got) } if len(sent.Inputs) != 1 || len(sent.Outputs) != 1 { t.Fatalf("%d inputs, %d outputs; want one of each (the whole coin, no change)", len(sent.Inputs), len(sent.Outputs)) } if sent.Outputs[0].Value != 99_000 || string(sent.Outputs[0].Script) != string(destination[0].Script()) { t.Fatalf("the split pays %+v", sent.Outputs[0]) } item := sent.Inputs[0].Witness[0] if len(item) == 0 || item[len(item)-1] != byte(tx.SighashAllUnified) { t.Fatalf("the split was not signed SIGHASH_UNIFIED: %x", item) } if coins, _ := r.domain.UnspentOn("blake"); len(coins) != 0 { t.Fatalf("%d coins remain after the split", len(coins)) } } // TestForkRefusesWhatIsNotShared: a post-fork coin, a wrong outpoint, the wrong // direction and a chain with no fork are each refused, and nothing broadcasts. func TestForkRefusesWhatIsNotShared(t *testing.T) { r, script := forkReady(t, 100_000) shared, err := r.domain.PreForkCoins("blake") if err != nil { t.Fatal(err) } chain := r.chains["blake"] // A post-fork coin (height 150) is not shared history. address, _ := r.domain.Addresses("blake", false, 3, 1) chain.syncTo = chaindataPosition(200) chain.matched = []uint32{150} chain.blocks = map[uint32][]byte{150: buildBlock(t, address[0].Script(), 50_000)} if _, err := r.domain.Sync("blake", 0); err != nil { t.Fatal(err) } post, err := r.domain.UnspentOn("blake") if err != nil { t.Fatal(err) } var postOutpoint string for _, coin := range post { if coin.Height == 150 { postOutpoint = coin.Outpoint() break } } destination, _ := r.domain.Addresses("blake", false, 12, 1) if _, err := r.domain.Fork("blake", postOutpoint, destination[0].Text, 1_000, SendPolicy{}); !errors.Is(err, ErrNotPreFork) { t.Fatalf("a post-fork coin: %v", err) } if _, err := r.domain.Fork("blake", "deadbeef:0", destination[0].Text, 1_000, SendPolicy{}); !errors.Is(err, ErrNoSuchCoin) { t.Fatalf("an unknown outpoint: %v", err) } if _, err := r.domain.Fork("btc", shared[0].Outpoint(), destination[0].Text, 1_000, SendPolicy{}); err == nil { t.Fatal("a split onto Bitcoin was accepted") } if chain.broadcastCalls != 0 { t.Fatalf("a refused split broadcast %d times", chain.broadcastCalls) } // No fork scheduled: the split does not exist. link, _ := r.domain.link("blake") link.Profile.ForkHeight = 0 r.domain.mu.Lock() r.domain.links["blake"] = link r.domain.mu.Unlock() if _, err := r.domain.PreForkCoins("blake"); !errors.Is(err, ErrNoFork) { t.Fatalf("a no-fork chain lists pre-fork coins: %v", err) } if _, err := r.domain.Fork("blake", shared[0].Outpoint(), destination[0].Text, 1_000, SendPolicy{}); !errors.Is(err, ErrNoFork) { t.Fatalf("a no-fork chain accepted a split: %v", err) } _ = script }