Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions ledger/conway/conway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
package conway

import (
"bytes"
"encoding/hex"
"reflect"
"strings"
"testing"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/common"
)

Expand Down Expand Up @@ -114,3 +118,88 @@ func TestConwayRedeemersIter(t *testing.T) {
iterIdx++
}
}

// Transaction taken from https://cexplorer.io/tx/6e6e15e39da0b8283b6c6d10b88b29adcac12e67edcf502c84cd2adb38a68880
// HASH: 6e6e15e39da0b8283b6c6d10b88b29adcac12e67edcf502c84cd2adb38a68880

const conwayTxHex = `84a40081825820b267f34cbf10a995565482742f328537ba4ccc767ea3ec99d4a443b5c71ae70101018182584c82d818584283581c4473be071285b7c1ac840cfaef878d95e078bf67fb859d587d488128a101581e581c5981b261ab5ccd11065b4b22cb3d6ba5938430480014d42742714dfb001a103e5cad1a0afcf06d021a00028ab9031a0b532b80a10081825820ca3554458c006d1ef04f33c13688b06c032d7c80adaf2967a01f1159c8058c65584016ce288ac2aca9ec373f3f84fd3a37c762e0ab244f68b2fd1c59525f24bbd5afc82ccbc67fa3eeeaaa7df2e212eae795b48e7803e6c7ec49b6b14bde6b84550af5f6`

func TestConwayTx_CborRoundTrip_UsingCborEncode(t *testing.T) {
txHex := strings.TrimSpace(conwayTxHex)
orig, err := hex.DecodeString(txHex)
if err != nil {
t.Fatalf("failed to decode Conway tx hex into CBOR bytes: %v", err)
}

var tx ConwayTransaction
if err := tx.UnmarshalCBOR(orig); err != nil {
t.Fatalf("failed to unmarshal CBOR into ConwayTransaction: %v", err)
}

enc, err := cbor.Encode(tx)
if err != nil {
t.Fatalf("failed to encode ConwayTransaction via cbor.Encode: %v", err)
}
if len(enc) == 0 {
t.Fatal("encoded tx CBOR is empty")
}

if !bytes.Equal(orig, enc) {
t.Errorf("CBOR round-trip mismatch\noriginal: %x\nencoded : %x", orig, enc)

i := -1
for j := 0; j < len(orig) && j < len(enc); j++ {
if orig[j] != enc[j] {
i = j
break
}
}
if i != -1 {
t.Logf("first diff at byte %d: orig=0x%02x enc=0x%02x", i, orig[i], enc[i])
} else {
t.Logf("length mismatch: orig=%d enc=%d", len(orig), len(enc))
}
}
}

func TestConwayTx_Utxorpc(t *testing.T) {
txHex := strings.TrimSpace(conwayTxHex)
txBytes, err := hex.DecodeString(txHex)
if err != nil {
t.Fatalf("failed to decode Conway tx hex: %v", err)
}

tx, err := NewConwayTransactionFromCbor(txBytes)
if err != nil {
t.Fatalf("failed to parse Conway tx: %v", err)
}

utxoTx, err := tx.Utxorpc()
if err != nil {
t.Fatalf("failed to convert Conway tx to utxorpc: %v", err)
}

expHash := tx.Hash().Bytes()
if !equalBytes(utxoTx.Hash, expHash) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why bytes.Equal() above and a custom function here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function I have copied from block_test.go and I thought of use it one function. Later, I realized why to repeat by writing again. I forgot to remove the custom function & I have made changes, please review it again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of removing it and that is the reason I have kept it in draft PR.

t.Errorf("tx hash mismatch\nexpected: %x\nactual : %x", expHash, utxoTx.Hash)
}

if got, want := len(utxoTx.Inputs), len(tx.Inputs()); got != want {
t.Errorf("inputs count mismatch: got %d want %d", got, want)
}
if got, want := len(utxoTx.Outputs), len(tx.Outputs()); got != want {
t.Errorf("outputs count mismatch: got %d want %d", got, want)
}
}

func equalBytes(a, b []byte) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}