Skip to content

Commit 194a33b

Browse files
authored
feat(ledger): Add Byron transaction output support to NewTransactionOutputFromCbor (#993)
Signed-off-by: Akhil Repala <[email protected]>
1 parent 582b32e commit 194a33b

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

ledger/byron.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ var (
4747
NewByronMainBlockHeaderFromCbor = byron.NewByronMainBlockHeaderFromCbor
4848
NewByronTransactionInput = byron.NewByronTransactionInput
4949
NewByronTransactionFromCbor = byron.NewByronTransactionFromCbor
50+
NewByronTransactionOutputFromCbor = byron.NewByronTransactionOutputFromCbor
5051
)

ledger/byron/byron.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,3 +771,11 @@ func NewByronTransactionFromCbor(data []byte) (*ByronTransaction, error) {
771771
}
772772
return &byronTx, nil
773773
}
774+
775+
func NewByronTransactionOutputFromCbor(data []byte) (*ByronTransactionOutput, error) {
776+
var byronTxOutput ByronTransactionOutput
777+
if _, err := cbor.Decode(data, &byronTxOutput); err != nil {
778+
return nil, fmt.Errorf("decode Byron transaction output error: %w", err)
779+
}
780+
return &byronTxOutput, nil
781+
}

ledger/tx.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ func NewTransactionBodyFromCbor(
7676
// NewTransactionOutputFromCbor attempts to parse the provided arbitrary CBOR data as a transaction output from
7777
// each of the eras, returning the first one that we can successfully decode
7878
func NewTransactionOutputFromCbor(data []byte) (TransactionOutput, error) {
79-
// TODO: add Byron transaction output support (#849)
79+
if txOut, err := NewByronTransactionOutputFromCbor(data); err == nil {
80+
return txOut, nil
81+
}
8082
if txOut, err := NewShelleyTransactionOutputFromCbor(data); err == nil {
8183
return txOut, nil
8284
}
@@ -93,13 +95,9 @@ func NewTransactionOutputFromCbor(data []byte) (TransactionOutput, error) {
9395
}
9496

9597
func DetermineTransactionType(data []byte) (uint, error) {
96-
// TODO: uncomment this once the following issue is resolved: (#849)
97-
// https://github.com/blinklabs-io/gouroboros/issues/206
98-
/*
99-
if _, err := NewByronTransactionFromCbor(data); err == nil {
100-
return TxTypeByron, nil
101-
}
102-
*/
98+
if _, err := NewByronTransactionFromCbor(data); err == nil {
99+
return TxTypeByron, nil
100+
}
103101
if _, err := NewShelleyTransactionFromCbor(data); err == nil {
104102
return TxTypeShelley, nil
105103
}

ledger/tx_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,20 @@ import (
2323

2424
func TestDetermineTransactionType(t *testing.T) {
2525
testDefs := []struct {
26+
name string
2627
txCborHex string
2728
expectedTxType uint
2829
}{
2930
{
31+
name: "ConwayTx",
3032
txCborHex: "84a500d9010281825820279184037d249e397d97293738370756da559718fcdefae9924834840046b37b01018282583900923d4b64e1d730a4baf3e6dc433a9686983940f458363f37aad7a1a9568b72f85522e4a17d44a45cd021b9741b55d7cbc635c911625b015e1a00a9867082583900923d4b64e1d730a4baf3e6dc433a9686983940f458363f37aad7a1a9568b72f85522e4a17d44a45cd021b9741b55d7cbc635c911625b015e1b00000001267d7b04021a0002938d031a04e304e70800a100d9010281825820b829480e5d5827d2e1bd7c89176a5ca125c30812e54be7dbdf5c47c835a17f3d5840b13a76e7f2b19cde216fcad55ceeeb489ebab3dcf63ef1539ac4f535dece00411ee55c9b8188ef04b4aa3c72586e4a0ec9b89949367d7270fdddad3b18731403f5f6",
3133
expectedTxType: 6,
3234
},
35+
{
36+
name: "ByronTx",
37+
txCborHex: "839f8200d8185824825820a12a839c25a01fa5d118167db5acdbd9e38172ae8f00e5ac0a4997ef792a200700ff9f8282d818584283581c6c9982e7f2b6dcc5eaa880e8014568913c8868d9f0f86eb687b2633ca101581e581c010d876783fb2b4d0d17c86df29af8d35356ed3d1827bf4744f06700001a8dc672c11a000f4240ffa0",
38+
expectedTxType: 0,
39+
},
3340
}
3441
for _, testDef := range testDefs {
3542
txCbor, err := hex.DecodeString(testDef.txCborHex)
@@ -38,7 +45,7 @@ func TestDetermineTransactionType(t *testing.T) {
3845
}
3946
tmpTxType, err := ledger.DetermineTransactionType(txCbor)
4047
if err != nil {
41-
t.Fatalf("unexpected error: %s", err)
48+
t.Fatalf("DetermineTransactionType failed with an unexpected error: %s", err)
4249
}
4350
if tmpTxType != testDef.expectedTxType {
4451
t.Fatalf(

0 commit comments

Comments
 (0)