Skip to content

Commit 1389c51

Browse files
authored
feat(conway): Added CBOR round trip test for conway transaction (#1198)
Signed-off-by: Akhil Repala <[email protected]>
1 parent 134ec30 commit 1389c51

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

ledger/conway/conway_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
package conway
1616

1717
import (
18+
"bytes"
19+
"encoding/hex"
1820
"reflect"
21+
"strings"
1922
"testing"
2023

24+
"github.com/blinklabs-io/gouroboros/cbor"
2125
"github.com/blinklabs-io/gouroboros/ledger/common"
2226
)
2327

@@ -114,3 +118,76 @@ func TestConwayRedeemersIter(t *testing.T) {
114118
iterIdx++
115119
}
116120
}
121+
122+
// Transaction taken from https://cexplorer.io/tx/6e6e15e39da0b8283b6c6d10b88b29adcac12e67edcf502c84cd2adb38a68880
123+
// HASH: 6e6e15e39da0b8283b6c6d10b88b29adcac12e67edcf502c84cd2adb38a68880
124+
125+
const conwayTxHex = `84a40081825820b267f34cbf10a995565482742f328537ba4ccc767ea3ec99d4a443b5c71ae70101018182584c82d818584283581c4473be071285b7c1ac840cfaef878d95e078bf67fb859d587d488128a101581e581c5981b261ab5ccd11065b4b22cb3d6ba5938430480014d42742714dfb001a103e5cad1a0afcf06d021a00028ab9031a0b532b80a10081825820ca3554458c006d1ef04f33c13688b06c032d7c80adaf2967a01f1159c8058c65584016ce288ac2aca9ec373f3f84fd3a37c762e0ab244f68b2fd1c59525f24bbd5afc82ccbc67fa3eeeaaa7df2e212eae795b48e7803e6c7ec49b6b14bde6b84550af5f6`
126+
127+
func TestConwayTx_CborRoundTrip_UsingCborEncode(t *testing.T) {
128+
txHex := strings.TrimSpace(conwayTxHex)
129+
orig, err := hex.DecodeString(txHex)
130+
if err != nil {
131+
t.Fatalf("failed to decode Conway tx hex into CBOR bytes: %v", err)
132+
}
133+
134+
var tx ConwayTransaction
135+
if err := tx.UnmarshalCBOR(orig); err != nil {
136+
t.Fatalf("failed to unmarshal CBOR into ConwayTransaction: %v", err)
137+
}
138+
139+
enc, err := cbor.Encode(tx)
140+
if err != nil {
141+
t.Fatalf("failed to encode ConwayTransaction via cbor.Encode: %v", err)
142+
}
143+
if len(enc) == 0 {
144+
t.Fatal("encoded tx CBOR is empty")
145+
}
146+
147+
if !bytes.Equal(orig, enc) {
148+
t.Errorf("CBOR round-trip mismatch\noriginal: %x\nencoded : %x", orig, enc)
149+
150+
i := -1
151+
for j := 0; j < len(orig) && j < len(enc); j++ {
152+
if orig[j] != enc[j] {
153+
i = j
154+
break
155+
}
156+
}
157+
if i != -1 {
158+
t.Logf("first diff at byte %d: orig=0x%02x enc=0x%02x", i, orig[i], enc[i])
159+
} else {
160+
t.Logf("length mismatch: orig=%d enc=%d", len(orig), len(enc))
161+
}
162+
}
163+
}
164+
165+
func TestConwayTx_Utxorpc(t *testing.T) {
166+
txHex := strings.TrimSpace(conwayTxHex)
167+
txBytes, err := hex.DecodeString(txHex)
168+
if err != nil {
169+
t.Fatalf("failed to decode Conway tx hex: %v", err)
170+
}
171+
172+
tx, err := NewConwayTransactionFromCbor(txBytes)
173+
if err != nil {
174+
t.Fatalf("failed to parse Conway tx: %v", err)
175+
}
176+
177+
utxoTx, err := tx.Utxorpc()
178+
if err != nil {
179+
t.Fatalf("failed to convert Conway tx to utxorpc: %v", err)
180+
}
181+
182+
expHash := tx.Hash().Bytes()
183+
if !bytes.Equal(utxoTx.Hash, expHash) {
184+
t.Errorf("tx hash mismatch\nexpected: %x\nactual : %x", expHash, utxoTx.Hash)
185+
}
186+
187+
if got, want := len(utxoTx.Inputs), len(tx.Inputs()); got != want {
188+
t.Errorf("inputs count mismatch: got %d want %d", got, want)
189+
}
190+
if got, want := len(utxoTx.Outputs), len(tx.Outputs()); got != want {
191+
t.Errorf("outputs count mismatch: got %d want %d", got, want)
192+
}
193+
}

0 commit comments

Comments
 (0)