From 174b27fe1d6c59dbfc5f1b7b03ffd334f80cee65 Mon Sep 17 00:00:00 2001 From: Aurora Gaffney Date: Mon, 3 Jul 2023 15:14:57 -0500 Subject: [PATCH] feat: use interfaces for NewTransaction(Body)FromCbor functions --- ledger/byron.go | 45 ++++++++++++++++++++++++++++++++------------- ledger/tx.go | 6 +++--- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/ledger/byron.go b/ledger/byron.go index 62edabb2..f5515cf4 100644 --- a/ledger/byron.go +++ b/ledger/byron.go @@ -100,15 +100,42 @@ func (h *ByronMainBlockHeader) Era() Era { return eras[ERA_ID_BYRON] } -// TODO: flesh this out -type ByronTransactionBody interface{} +type ByronTransaction struct { + cbor.DecodeStoreCbor + // TODO: flesh these out + TxInputs []any + TxOutputs []any + Attributes cbor.Value +} -// TODO: flesh this out -type ByronTransaction interface{} +func (t *ByronTransaction) Hash() string { + // TODO + return "" +} + +func (t *ByronTransaction) Inputs() []TransactionInput { + // TODO + return nil +} + +func (t *ByronTransaction) Outputs() []TransactionOutput { + // TODO + return nil +} + +func (t *ByronTransaction) Metadata() cbor.Value { + return t.Attributes +} type ByronMainBlockBody struct { cbor.StructAsArray - TxPayload []ByronTransactionBody + // TODO: split this to its own type + TxPayload []struct { + cbor.StructAsArray + Transaction ByronTransaction + // TODO: figure out what this field actually is + Twit []cbor.Value + } SscPayload cbor.Value DlgPayload []interface{} UpdPayload []interface{} @@ -257,14 +284,6 @@ func NewByronMainBlockHeaderFromCbor(data []byte) (*ByronMainBlockHeader, error) return &byronMainBlockHeader, nil } -func NewByronTransactionBodyFromCbor(data []byte) (*ByronTransactionBody, error) { - var byronTx ByronTransactionBody - if _, err := cbor.Decode(data, &byronTx); err != nil { - return nil, fmt.Errorf("Byron transaction body decode error: %s", err) - } - return &byronTx, nil -} - func NewByronTransactionFromCbor(data []byte) (*ByronTransaction, error) { var byronTx ByronTransaction if _, err := cbor.Decode(data, &byronTx); err != nil { diff --git a/ledger/tx.go b/ledger/tx.go index c7044a83..f83adace 100644 --- a/ledger/tx.go +++ b/ledger/tx.go @@ -47,7 +47,7 @@ type TransactionOutput interface { DatumHash() *Blake2b256 } -func NewTransactionFromCbor(txType uint, data []byte) (interface{}, error) { +func NewTransactionFromCbor(txType uint, data []byte) (Transaction, error) { switch txType { case TX_TYPE_BYRON: return NewByronTransactionFromCbor(data) @@ -65,10 +65,10 @@ func NewTransactionFromCbor(txType uint, data []byte) (interface{}, error) { return nil, fmt.Errorf("unknown transaction type: %d", txType) } -func NewTransactionBodyFromCbor(txType uint, data []byte) (interface{}, error) { +func NewTransactionBodyFromCbor(txType uint, data []byte) (TransactionBody, error) { switch txType { case TX_TYPE_BYRON: - return NewByronTransactionBodyFromCbor(data) + return nil, fmt.Errorf("Byron transactions do not contain a body") case TX_TYPE_SHELLEY: return NewShelleyTransactionBodyFromCbor(data) case TX_TYPE_ALLEGRA: