package verify import ( "context" "errors" "strings" "testing" "git.smesh.lol/tinywallet/pkg/script" "git.smesh.lol/tinywallet/pkg/tx" ) // --- fake sources --------------------------------------------------------------- type fake struct { name string raw []byte found bool height int64 tip Tip fee int64 err error } func (f fake) Name() string { return f.name } func (f fake) Tx(context.Context, [32]byte) (TxFact, error) { if f.err != nil { return TxFact{}, f.err } return TxFact{Found: f.found, Raw: f.raw, Height: f.height, Confirmations: 10}, nil } func (f fake) Tip(context.Context) (Tip, error) { return f.tip, nil } func (f fake) FeeEstimate(context.Context) (int64, error) { return f.fee, nil } func buildTx(t *testing.T, lockTime uint32, sequence uint32, outputs ...tx.Output) *tx.Tx { t.Helper() built := tx.New() built.LockTime = lockTime built.AddInput(tx.Outpoint{Hash: [32]byte{0x11}, Index: 0}, sequence) for _, out := range outputs { built.AddOutput(out.Value, out.Script) } return built } func rawOf(t *testing.T, built *tx.Tx) []byte { t.Helper() raw, err := built.Serialize() if err != nil { t.Fatal(err) } return raw } // --- the external engine -------------------------------------------------------- func TestQuorumNeedsTwoIndependentSources(t *testing.T) { if _, err := NewFacts([]Source{fake{name: "a"}}, 1); err == nil { t.Fatal("a single source was accepted as a quorum") } if _, err := NewFacts([]Source{fake{name: "a"}, fake{name: "a"}}, 2); err == nil { t.Fatal("the same source name twice was accepted") } } func TestTxAcceptedWhenQuorumAgreesAndBytesHashToOneID(t *testing.T) { built := buildTx(t, 0, tx.SequenceFinal, tx.Output{Value: 1000, Script: []byte{0x51}}) raw := rawOf(t, built) txid, err := TxidOf(raw) if err != nil { t.Fatal(err) } engine, err := NewFacts([]Source{ fake{name: "one", raw: raw, found: true, height: 962000, tip: Tip{Height: 962000, Hash: [32]byte{1}}, fee: 5}, fake{name: "two", raw: raw, found: true, height: 962000, tip: Tip{Height: 962000, Hash: [32]byte{2}}, fee: 6}, }, 2) if err != nil { t.Fatal(err) } answer, err := engine.Tx(context.Background(), txid) if err != nil { t.Fatal(err) } if !answer.Verified || !answer.Found || answer.Height != 962000 { t.Fatalf("answer %+v", answer) } } func TestTxRejectedWhenBytesDoNotHashToTheRequestedID(t *testing.T) { built := buildTx(t, 0, tx.SequenceFinal, tx.Output{Value: 1, Script: []byte{0x51}}) other := buildTx(t, 0, tx.SequenceFinal, tx.Output{Value: 2, Script: []byte{0x51}}) txid, err := TxidOf(rawOf(t, built)) if err != nil { t.Fatal(err) } engine, err := NewFacts([]Source{ fake{name: "one", raw: rawOf(t, other), found: true, height: 1, tip: Tip{Hash: [32]byte{1}}}, fake{name: "two", raw: rawOf(t, other), found: true, height: 1, tip: Tip{Hash: [32]byte{2}}}, }, 2) if err != nil { t.Fatal(err) } if _, err := engine.Tx(context.Background(), txid); !errors.Is(err, ErrDisagree) { t.Fatalf("err %v, want ErrDisagree", err) } } func TestTxAbsentFromOneSourceIsDisagreement(t *testing.T) { built := buildTx(t, 0, tx.SequenceFinal, tx.Output{Value: 1, Script: []byte{0x51}}) raw := rawOf(t, built) txid, err := TxidOf(raw) if err != nil { t.Fatal(err) } engine, err := NewFacts([]Source{ fake{name: "one", raw: raw, found: true, height: 5, tip: Tip{Hash: [32]byte{1}}}, fake{name: "two", found: false, tip: Tip{Hash: [32]byte{2}}}, }, 2) if err != nil { t.Fatal(err) } if _, err := engine.Tx(context.Background(), txid); !errors.Is(err, ErrDisagree) { t.Fatalf("err %v, want ErrDisagree", err) } } func TestIndependenceWarnsWhenEverySourceSeesOneTip(t *testing.T) { tip := Tip{Height: 900, Hash: [32]byte{7}} engine, err := NewFacts([]Source{ fake{name: "one", tip: tip}, fake{name: "two", tip: tip}, }, 2) if err != nil { t.Fatal(err) } report, err := engine.Independence(context.Background()) if err != nil { t.Fatal(err) } if report.DistinctTips != 1 { t.Fatalf("independence %+v", report) } // The warning must not claim a shared backend, because one chain has one tip. if len(report.Warnings) != 1 || strings.Contains(report.Warnings[0], "may be one backend") { t.Fatalf("warnings %v", report.Warnings) } } // --- the terms checks ----------------------------------------------------------- func testTerms(t *testing.T) (Terms, []byte) { t.Helper() key := func(b byte) []byte { return append([]byte{0x02}, make([]byte, 32)...) } claimer := append([]byte{0x02}, make([]byte, 32)...) claimer[1] = 1 refunder := append([]byte{0x02}, make([]byte, 32)...) refunder[1] = 2 var hash [32]byte hash[0] = 9 terms := Terms{ HashLock: hash, TimeoutBTC: 900, TimeoutXBT: 800, AmountBTC: 100_000, AmountXBT: 100_000, FeeBTC: 500, FeeXBT: 500, ClaimerBTC: claimer, RefunderBTC: refunder, ClaimerXBT: claimer, RefunderXBT: refunder, MinTimeoutGap: 24, } htlc, err := script.HTLC(claimer, refunder, hash, terms.TimeoutBTC) if err != nil { t.Fatal(err) } _ = key return terms, htlc } func TestTermsFailOnWrongTimeout(t *testing.T) { terms, htlc := testTerms(t) leg := Leg{Chain: "btc", Timeout: 901, Amount: terms.AmountBTC, Fee: terms.FeeBTC, HashLock: terms.HashLock, Claimer: terms.ClaimerBTC, Refunder: terms.RefunderBTC} var report Report terms.Verify(&report, []Leg{leg}) if report.Verdict != VerdictFail { t.Fatalf("verdict %s, want fail", report.Verdict) } found := false for _, check := range report.Checks { if check.Name == "btc_timeout_matches_terms" && check.Status == Fail { found = true } } if !found { t.Fatal("the timeout mismatch was not reported") } _ = htlc } func TestTermsFailOnAHiddenOutput(t *testing.T) { terms, htlc := testTerms(t) funding := buildTx(t, 0, tx.SequenceFinal, tx.Output{Value: terms.AmountBTC, Script: htlc}, tx.Output{Value: 2_000, Script: []byte{0x76, 0xa9, 0x14}}, ) leg := Leg{Chain: "btc", Timeout: terms.TimeoutBTC, Amount: terms.AmountBTC, Fee: terms.FeeBTC, HashLock: terms.HashLock, Claimer: terms.ClaimerBTC, Refunder: terms.RefunderBTC, Funding: rawOf(t, funding)} var report Report terms.Verify(&report, []Leg{leg}) if report.Verdict != VerdictFail { t.Fatalf("verdict %s, want fail", report.Verdict) } } func TestTermsFailOnReusedHashlock(t *testing.T) { terms, htlc := testTerms(t) other := terms other.HashLock[0] = 10 funding := buildTx(t, 0, tx.SequenceFinal, tx.Output{Value: terms.AmountBTC, Script: htlc}) leg := Leg{Chain: "btc", Timeout: terms.TimeoutBTC, Amount: terms.AmountBTC, Fee: terms.FeeBTC, HashLock: other.HashLock, Claimer: terms.ClaimerBTC, Refunder: terms.RefunderBTC, Funding: rawOf(t, funding)} var report Report terms.Verify(&report, []Leg{leg}) if report.Verdict != VerdictFail { t.Fatalf("verdict %s, want fail", report.Verdict) } } // --- fork constants ------------------------------------------------------------- func TestClockUsesObservedIntervals(t *testing.T) { clock := NewClock([]int64{420, 600, 900}) if clock.Median != 600 || clock.Fastest != 420 || clock.Slowest != 900 { t.Fatalf("clock %+v", clock) } expected, fast, slow := clock.Expected(6) if expected != 3600 || fast != 2520 || slow != 5400 { t.Fatalf("expected %d %d %d", expected, fast, slow) } } func TestCoinbaseMaturityIsTheForksNotBitcoins(t *testing.T) { if !ImmatureCoinbase(99) { t.Fatal("an immature coinbase was accepted") } if ImmatureCoinbase(XBTCoinbaseMaturity) { t.Fatal("a mature coinbase was refused") } }