From 0aafc9e6c8870432629c653088591fa6f3331900 Mon Sep 17 00:00:00 2001 From: Akhil Repala Date: Wed, 1 Oct 2025 23:11:53 -0500 Subject: [PATCH 1/2] feat(protocol): Added CBOR round trip test for conway transaction Signed-off-by: Akhil Repala --- ledger/conway/conway_test.go | 89 ++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/ledger/conway/conway_test.go b/ledger/conway/conway_test.go index eb9aa6ae..49ae47e6 100644 --- a/ledger/conway/conway_test.go +++ b/ledger/conway/conway_test.go @@ -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" ) @@ -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) { + 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 +} From 3b515f56e3d4b3b9a4596c7fbeac785307a93bd1 Mon Sep 17 00:00:00 2001 From: Akhil Repala Date: Fri, 3 Oct 2025 01:54:35 -0500 Subject: [PATCH 2/2] feat(conway): Removed custom equal bytes function Signed-off-by: Akhil Repala --- ledger/conway/conway_test.go | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/ledger/conway/conway_test.go b/ledger/conway/conway_test.go index 49ae47e6..7cf191ea 100644 --- a/ledger/conway/conway_test.go +++ b/ledger/conway/conway_test.go @@ -180,7 +180,7 @@ func TestConwayTx_Utxorpc(t *testing.T) { } expHash := tx.Hash().Bytes() - if !equalBytes(utxoTx.Hash, expHash) { + if !bytes.Equal(utxoTx.Hash, expHash) { t.Errorf("tx hash mismatch\nexpected: %x\nactual : %x", expHash, utxoTx.Hash) } @@ -191,15 +191,3 @@ func TestConwayTx_Utxorpc(t *testing.T) { 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 -}