Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use interfaces for NewTransaction(Body)FromCbor functions #356

Merged
merged 1 commit into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
45 changes: 32 additions & 13 deletions ledger/byron.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions ledger/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down