From b742b8a5d5e82283ac7f5468e19c8dfa7cca7dc9 Mon Sep 17 00:00:00 2001 From: George Tankersley Date: Tue, 25 Sep 2018 00:38:34 +0000 Subject: [PATCH] parser: add ZIP143 (pre-Sapling) test vectors --- parser/transaction_test.go | 79 ++++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 7 deletions(-) diff --git a/parser/transaction_test.go b/parser/transaction_test.go index e371759..27d6d52 100644 --- a/parser/transaction_test.go +++ b/parser/transaction_test.go @@ -1,9 +1,12 @@ package parser import ( + "bytes" "encoding/binary" "encoding/hex" "testing" + + "github.com/gtank/ctxd/parser/internal/bytestring" ) // https://github.com/zcash/zips/blob/master/zip-0143.rst @@ -91,15 +94,77 @@ func TestSproutTransactionParser(t *testing.T) { // 4201cfb1cd8dbf69b8250c18ef41294ca97993db546c1fe01f7e9c8e36d6a5e2 9d4e30a7 03ac6a00 98421c69 for idx, ti := range tt.vin { - prevTxHash, _ := hex.DecodeString(ti[0]) - prevTxIndexBytes, _ := hex.DecodeString(ti[1]) - prevTxIndex := le.Uint32(prevTxIndexBytes) - scriptSig, _ := hex.DecodeString(ti[2]) - seqNumBytes, _ := hex.DecodeString(ti[3]) - seqNum := le.Uint32(seqNumBytes) + txInput := tx.transparentInputs[idx] + + testPrevTxHash, _ := hex.DecodeString(ti[0]) + if eq := bytes.Equal(testPrevTxHash, txInput.PrevTxHash); !eq { + t.Errorf("Test %d tin %d: prevhash mismatch %x %x", i, idx, testPrevTxHash, txInput.PrevTxHash) + continue + } + + testPrevTxOutIndexBytes, _ := hex.DecodeString(ti[1]) + testPrevTxOutIndex := le.Uint32(testPrevTxOutIndexBytes) + if testPrevTxOutIndex != txInput.PrevTxOutIndex { + t.Errorf("Test %d tin %d: prevout index mismatch %d %d", i, idx, testPrevTxOutIndex, txInput.PrevTxOutIndex) + continue + } + + // Decode scriptSig and correctly consume own CompactSize field + testScriptSig, _ := hex.DecodeString(ti[2]) + ok := (*bytestring.String)(&testScriptSig).ReadCompactLengthPrefixed((*bytestring.String)(&testScriptSig)) + if !ok { + t.Errorf("Test %d, tin %d: couldn't strip size from script", i, idx) + continue + } + + if eq := bytes.Equal(testScriptSig, txInput.ScriptSig); !eq { + t.Errorf("Test %d tin %d: scriptsig mismatch %x %x", i, idx, testScriptSig, txInput.ScriptSig) + continue + } + + testSeqNumBytes, _ := hex.DecodeString(ti[3]) + testSeqNum := le.Uint32(testSeqNumBytes) + if testSeqNum != txInput.SequenceNumber { + t.Errorf("Test %d tin %d: seq mismatch %d %d", i, idx, testSeqNum, txInput.SequenceNumber) + continue + } + + } + + if tt.vout == nil && tx.transparentOutputs != nil { + t.Errorf("Test %d: non-zero vout when expected zero", i) + continue + } + + if len(tt.vout) != len(tx.transparentOutputs) { + t.Errorf("Test %d: vout have mismatched lengths", i) + continue + } + + for idx, testOutput := range tt.vout { + txOutput := tx.transparentOutputs[idx] + + // Parse tx out value from test + testValueBytes, _ := hex.DecodeString(testOutput[0]) + testValue := le.Uint64(testValueBytes) - if !bytes.Equal(prevTxHash, tx.transparentInputs[idx][0]) { + if testValue != txOutput.Value { + t.Errorf("Test %d, tout %d: value mismatch %d %d", i, idx, testValue, txOutput.Value) + continue + } + + // Parse script from test + testScript, _ := hex.DecodeString(testOutput[1]) + // Correctly consume own CompactSize field + ok := (*bytestring.String)(&testScript).ReadCompactLengthPrefixed((*bytestring.String)(&testScript)) + if !ok { + t.Errorf("Test %d, tout %d: couldn't strip size from script", i, idx) + continue + } + if !bytes.Equal(testScript, txOutput.Script) { + t.Errorf("Test %d, tout %d: script mismatch %x %x", i, idx, testScript, txOutput.Script) + continue } } }