journal_test.go raw

   1  package domain
   2  
   3  import (
   4  	"encoding/hex"
   5  	"errors"
   6  	"testing"
   7  )
   8  
   9  // TestSendIsRemembered: the wallet is the only record of what it sent, so the bytes, the
  10  // destination and the amount are kept and come back by txid.
  11  func TestSendIsRemembered(t *testing.T) {
  12  	r := fundedRestored(t, 100_000)
  13  
  14  	destination, err := r.domain.Addresses("btc", false, 2, 1)
  15  
  16  	if err != nil {
  17  		t.Fatal(err)
  18  	}
  19  
  20  	txid, err := r.domain.Send("btc", destination[0].Text, 50_000, 1_000, SendPolicy{})
  21  
  22  	if err != nil {
  23  		t.Fatalf("Send: %v", err)
  24  	}
  25  
  26  	display := hex.EncodeToString(reversed(txid[:]))
  27  
  28  	sent, err := r.domain.SentTx("btc", display)
  29  
  30  	if err != nil {
  31  		t.Fatalf("SentTx: %v", err)
  32  	}
  33  
  34  	if sent.Hex != hex.EncodeToString(r.chains["btc"].broadcast[0]) {
  35  		t.Fatal("the journal does not hold the bytes that were broadcast")
  36  	}
  37  
  38  	if sent.Address != destination[0].Text || sent.Amount != 50_000 || sent.Vout != 0 || sent.Chain != "btc" {
  39  		t.Fatalf("sent %+v", sent)
  40  	}
  41  
  42  	all, err := r.domain.Sends("btc")
  43  
  44  	if err != nil || len(all) != 1 || all[0].TxID != display {
  45  		t.Fatalf("sends %+v, %v", all, err)
  46  	}
  47  
  48  	if _, err := r.domain.SentTx("btc", "ff"); !errors.Is(err, ErrNoSentTx) {
  49  		t.Fatalf("an unknown txid: %v", err)
  50  	}
  51  
  52  	// The journal is in the store, so it survives a lock and a reopen of the same cache.
  53  	fresh, err := New(r.cache)
  54  
  55  	if err != nil {
  56  		t.Fatal(err)
  57  	}
  58  
  59  	if _, err := fresh.SentTx("btc", display); err != nil {
  60  		t.Fatalf("after reopening the domain: %v", err)
  61  	}
  62  }
  63