decode_test.go raw

   1  package tx
   2  
   3  import (
   4  	"bytes"
   5  	"errors"
   6  	"testing"
   7  )
   8  
   9  // TestDecodeRoundTrip: what the wallet writes, it reads back byte for byte, with and without
  10  // witnesses.
  11  func TestDecodeRoundTrip(t *testing.T) {
  12  	transaction := New()
  13  	transaction.AddInput(Outpoint{Hash: [32]byte{1, 2, 3}, Index: 7}, 0xfffffffe)
  14  	transaction.AddInput(Outpoint{Hash: [32]byte{4}, Index: 0}, 0xffffffff)
  15  	transaction.AddOutput(50_000, []byte{0x00, 0x14, 1, 2, 3, 4})
  16  	transaction.AddOutput(1_000, []byte{0x6a})
  17  	transaction.LockTime = 42
  18  
  19  	raw, err := transaction.Serialize()
  20  
  21  	if err != nil {
  22  		t.Fatalf("Serialize: %v", err)
  23  	}
  24  
  25  	decoded, err := Decode(raw)
  26  
  27  	if err != nil {
  28  		t.Fatalf("Decode: %v", err)
  29  	}
  30  
  31  	if decoded.Version != transaction.Version || decoded.LockTime != 42 {
  32  		t.Fatalf("version %d locktime %d", decoded.Version, decoded.LockTime)
  33  	}
  34  
  35  	if len(decoded.Inputs) != 2 || len(decoded.Outputs) != 2 {
  36  		t.Fatalf("%d inputs, %d outputs", len(decoded.Inputs), len(decoded.Outputs))
  37  	}
  38  
  39  	if decoded.Inputs[0].Index != 7 || decoded.Inputs[0].Hash != ([32]byte{1, 2, 3}) {
  40  		t.Fatalf("first outpoint %v", decoded.Inputs[0].Outpoint)
  41  	}
  42  
  43  	if decoded.Outputs[0].Value != 50_000 || !bytes.Equal(decoded.Outputs[0].Script, []byte{0x00, 0x14, 1, 2, 3, 4}) {
  44  		t.Fatalf("first output %+v", decoded.Outputs[0])
  45  	}
  46  
  47  	again, err := decoded.Serialize()
  48  
  49  	if err != nil || !bytes.Equal(again, raw) {
  50  		t.Fatalf("re-serialised differs: %v", err)
  51  	}
  52  
  53  	// With a witness.
  54  	transaction.Inputs[0].Witness = [][]byte{{1, 2}, {3}}
  55  	wire, err := transaction.SerializeWitness()
  56  
  57  	if err != nil {
  58  		t.Fatalf("SerializeWitness: %v", err)
  59  	}
  60  
  61  	decoded, err = Decode(wire)
  62  
  63  	if err != nil {
  64  		t.Fatalf("Decode with witness: %v", err)
  65  	}
  66  
  67  	if len(decoded.Inputs[0].Witness) != 2 || decoded.Inputs[0].Witness[1][0] != 3 {
  68  		t.Fatalf("witness %v", decoded.Inputs[0].Witness)
  69  	}
  70  
  71  	if decoded.TxIDOrPanic(t) != transaction.TxIDOrPanic(t) {
  72  		t.Fatal("the decoded transaction has a different txid")
  73  	}
  74  }
  75  
  76  // TxIDOrPanic is a test convenience: a txid error is a test failure, not a value to compare.
  77  func (t *Tx) TxIDOrPanic(tb testing.TB) [32]byte {
  78  	tb.Helper()
  79  
  80  	id, err := t.TxID()
  81  
  82  	if err != nil {
  83  		tb.Fatalf("TxID: %v", err)
  84  	}
  85  
  86  	return id
  87  }
  88  
  89  // TestDecodeRefusesBadInput: truncation and trailing bytes are errors, not shorter
  90  // transactions.
  91  func TestDecodeRefusesBadInput(t *testing.T) {
  92  	transaction := New()
  93  	transaction.AddInput(Outpoint{Index: 1}, 0)
  94  	transaction.AddOutput(1, []byte{0x6a})
  95  
  96  	raw, err := transaction.Serialize()
  97  
  98  	if err != nil {
  99  		t.Fatal(err)
 100  	}
 101  
 102  	for cut := 0; cut < len(raw); cut++ {
 103  		if _, err := Decode(raw[:cut]); err == nil {
 104  			t.Fatalf("%d bytes decoded", cut)
 105  		}
 106  	}
 107  
 108  	if _, err := Decode(append(append([]byte(nil), raw...), 0x00)); !errors.Is(err, ErrTrailing) {
 109  		t.Fatalf("trailing byte: %v", err)
 110  	}
 111  }
 112