-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathtransaction_test.go
116 lines (109 loc) · 3.34 KB
/
transaction_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) 2019-2020 The Zcash developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
package parser
import (
"bytes"
"encoding/hex"
"encoding/json"
"os"
"testing"
)
// Some of these values may be "null" (which translates to nil in Go) in
// the test data, so we have *_set variables to indicate if the corresponding
// variable is non-null. (There is an "optional" package we could use for
// these but it doesn't seem worth pulling it in.)
type TxTestData struct {
Tx string
Txid string
Version int
NVersionGroupId int
NConsensusBranchId int
Tx_in_count int
Tx_out_count int
NSpendsSapling int
NoutputsSapling int
NActionsOrchard int
}
// https://jhall.io/posts/go-json-tricks-array-as-structs/
func (r *TxTestData) UnmarshalJSON(p []byte) error {
var t []interface{}
if err := json.Unmarshal(p, &t); err != nil {
return err
}
r.Tx = t[0].(string)
r.Txid = t[1].(string)
r.Version = int(t[2].(float64))
r.NVersionGroupId = int(t[3].(float64))
r.NConsensusBranchId = int(t[4].(float64))
r.Tx_in_count = int(t[7].(float64))
r.Tx_out_count = int(t[8].(float64))
r.NSpendsSapling = int(t[9].(float64))
r.NoutputsSapling = int(t[10].(float64))
r.NActionsOrchard = int(t[14].(float64))
return nil
}
func TestV5TransactionParser(t *testing.T) {
// The raw data are stored in a separate file because they're large enough
// to make the test table difficult to scroll through. They are in the same
// order as the test table above. If you update the test table without
// adding a line to the raw file, this test will panic due to index
// misalignment.
s, err := os.ReadFile("../testdata/tx_v5.json")
if err != nil {
t.Fatal(err)
}
var testdata []json.RawMessage
err = json.Unmarshal(s, &testdata)
if err != nil {
t.Fatal(err)
}
if len(testdata) < 3 {
t.Fatal("tx_vt.json has too few lines")
}
testdata = testdata[2:]
for _, onetx := range testdata {
var txtestdata TxTestData
err = json.Unmarshal(onetx, &txtestdata)
if err != nil {
t.Fatal(err)
}
t.Logf("txid %s", txtestdata.Txid)
rawTxData, _ := hex.DecodeString(txtestdata.Tx)
tx := NewTransaction()
rest, err := tx.ParseFromSlice(rawTxData)
if err != nil {
t.Fatalf("%v", err)
}
if len(rest) != 0 {
t.Fatalf("Test did not consume entire buffer, %d remaining", len(rest))
}
if bytes.Equal(tx.cachedTxID, []byte(txtestdata.Txid)) {
t.Fatal("txid")
}
if tx.version != uint32(txtestdata.Version) {
t.Fatal("version miscompare")
}
if tx.nVersionGroupID != uint32(txtestdata.NVersionGroupId) {
t.Fatal("nVersionGroupId miscompare")
}
if tx.consensusBranchID != uint32(txtestdata.NConsensusBranchId) {
t.Fatal("consensusBranchID miscompare")
}
if len(tx.transparentInputs) != int(txtestdata.Tx_in_count) {
t.Fatal("tx_in_count miscompare")
}
if len(tx.transparentOutputs) != int(txtestdata.Tx_out_count) {
t.Fatal("tx_out_count miscompare")
}
if len(tx.shieldedSpends) != int(txtestdata.NSpendsSapling) {
t.Fatal("NSpendsSapling miscompare")
}
if len(tx.shieldedOutputs) != int(txtestdata.NoutputsSapling) {
t.Fatal("NOutputsSapling miscompare")
}
if len(tx.orchardActions) != int(txtestdata.NActionsOrchard) {
t.Fatal("NActionsOrchard miscompare")
}
}
}