package tx import ( "encoding/hex" "strings" "testing" ) func fixture() Transaction { return Transaction{Version: 2, Inputs: []Input{{Previous: OutPoint{Hash: [32]byte{1}, Index: 3}, Sequence: 0xfffffffd}}, Outputs: []Output{{Value: 9000, Script: []byte{0x51}}}} } func TestSerialization(t *testing.T) { x := fixture() want := "020000000101" + strings.Repeat("00", 31) + "0300000000fdffffff012823000000000000015100000000" if got := hex.EncodeToString(x.Serialize(false)); got != want { t.Fatal(got, want) } if x.TXID() != x.WTXID() { t.Fatal("legacy hash mismatch") } } func TestWitness(t *testing.T) { x := fixture() id := x.TXID() x.Inputs[0].Witness = [][]byte{{1, 2}} if x.TXID() != id || x.WTXID() == id { t.Fatal("hash separation") } if x.Weight() != 250 || x.VSize() != 63 { t.Fatal(x.Weight(), x.VSize()) } } func TestFee(t *testing.T) { x := fixture() coins := map[OutPoint]Output{x.Inputs[0].Previous: {Value: 10000}} f, e := x.Fee(coins) if e != nil || f != 1000 { t.Fatal(f, e) } if _, e = x.Fee(nil); e == nil { t.Fatal("missing coin") } x.Inputs = append(x.Inputs, x.Inputs[0]) if _, e = x.Fee(coins); e == nil { t.Fatal("duplicate") } } func TestAmounts(t *testing.T) { x := fixture() coins := map[OutPoint]Output{x.Inputs[0].Previous: {Value: 10000}} for _, v := range []int64{-1, 10001, MaxMoney + 1} { x.Outputs[0].Value = v if _, e := x.Fee(coins); e == nil { t.Fatal(v) } } }