Skip to content

Commit

Permalink
add V5 transaction (orchard) parsing test
Browse files Browse the repository at this point in the history
  • Loading branch information
Larry Ruane authored and LarryRuane committed Apr 12, 2022
1 parent 631bb16 commit 26df6f0
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 3 deletions.
3 changes: 0 additions & 3 deletions parser/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,6 @@ func (tx *Transaction) parseV5(data []byte) ([]byte, error) {
if tx.nVersionGroupID != 0x26A7270A {
return nil, errors.New(fmt.Sprintf("version group ID %d must be 0x26A7270A", tx.nVersionGroupID))
}
if tx.consensusBranchID != 0x37519621 {
return nil, errors.New("unknown consensusBranchID")
}
if !s.Skip(4) {
return nil, errors.New("could not skip nLockTime")
}
Expand Down
112 changes: 112 additions & 0 deletions parser/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,115 @@
// 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")
}
}
}
Loading

0 comments on commit 26df6f0

Please sign in to comment.