Skip to content

Commit

Permalink
feat: additional Byron ledger support (#575)
Browse files Browse the repository at this point in the history
This rounds out the API interfaces for Byron blocks and transactions and
implements Byron transaction hash generation

Fixes #206
  • Loading branch information
agaffney authored Apr 2, 2024
1 parent 78484a1 commit aca581f
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions ledger/byron.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Blink Labs Software
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -76,6 +76,7 @@ type ByronMainBlockHeader struct {
}

func (h *ByronMainBlockHeader) UnmarshalCBOR(cborData []byte) error {
// Decode generically and store original CBOR
return h.UnmarshalCbor(cborData, h)
}

Expand Down Expand Up @@ -111,7 +112,7 @@ func (h *ByronMainBlockHeader) IssuerVkey() IssuerVkey {
}

func (h *ByronMainBlockHeader) BlockBodySize() uint64 {
// TODO: calculate this
// Byron doesn't include the block body size in the header
return 0
}

Expand All @@ -122,15 +123,22 @@ func (h *ByronMainBlockHeader) Era() Era {
type ByronTransaction struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
// TODO: flesh these out
hash string
TxInputs []ByronTransactionInput
TxOutputs []ByronTransactionOutput
Attributes *cbor.Value
}

func (t *ByronTransaction) UnmarshalCBOR(data []byte) error {
// Decode generically and store original CBOR
return t.UnmarshalCbor(data, t)
}

func (t *ByronTransaction) Hash() string {
// TODO
return ""
if t.hash == "" {
t.hash = generateTransactionHash(t.Cbor(), nil)
}
return t.hash
}

func (t *ByronTransaction) Inputs() []TransactionInput {
Expand All @@ -151,17 +159,19 @@ func (t *ByronTransaction) Outputs() []TransactionOutput {
}

func (t *ByronTransaction) Fee() uint64 {
// TODO
// The fee is implicit in Byron, and we don't have enough information here to calculate it.
// You need to know the Lovelace in the inputs to determine the fee, and that information is
// not provided directly in the TX
return 0
}

func (t *ByronTransaction) TTL() uint64 {
// TODO
// No TTL in Byron
return 0
}

func (t *ByronTransaction) ReferenceInputs() []TransactionInput {
// TODO
// No reference inputs in Byron
return nil
}

Expand Down Expand Up @@ -287,6 +297,7 @@ func (o ByronTransactionOutput) Utxorpc() *utxorpc.TxOutput {

type ByronMainBlockBody struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
// TODO: split this to its own type
TxPayload []struct {
cbor.StructAsArray
Expand All @@ -299,6 +310,11 @@ type ByronMainBlockBody struct {
UpdPayload []interface{}
}

func (b *ByronMainBlockBody) UnmarshalCBOR(data []byte) error {
// Decode generically and store original CBOR
return b.UnmarshalCbor(data, b)
}

type ByronEpochBoundaryBlockHeader struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
Expand All @@ -318,6 +334,7 @@ type ByronEpochBoundaryBlockHeader struct {
}

func (h *ByronEpochBoundaryBlockHeader) UnmarshalCBOR(cborData []byte) error {
// Decode generically and store original CBOR
return h.UnmarshalCbor(cborData, h)
}

Expand Down Expand Up @@ -349,7 +366,7 @@ func (h *ByronEpochBoundaryBlockHeader) IssuerVkey() IssuerVkey {
}

func (h *ByronEpochBoundaryBlockHeader) BlockBodySize() uint64 {
// TODO: calculate this
// Byron doesn't include the block body size in the header
return 0
}

Expand All @@ -366,6 +383,7 @@ type ByronMainBlock struct {
}

func (b *ByronMainBlock) UnmarshalCBOR(cborData []byte) error {
// Decode generically and store original CBOR
return b.UnmarshalCbor(cborData, b)
}

Expand All @@ -386,7 +404,7 @@ func (b *ByronMainBlock) IssuerVkey() IssuerVkey {
}

func (b *ByronMainBlock) BlockBodySize() uint64 {
return b.Header.BlockBodySize()
return uint64(len(b.Body.Cbor()))
}

func (b *ByronMainBlock) Era() Era {
Expand Down Expand Up @@ -415,6 +433,7 @@ type ByronEpochBoundaryBlock struct {
}

func (b *ByronEpochBoundaryBlock) UnmarshalCBOR(cborData []byte) error {
// Decode generically and store original CBOR
return b.UnmarshalCbor(cborData, b)
}

Expand All @@ -435,7 +454,8 @@ func (b *ByronEpochBoundaryBlock) IssuerVkey() IssuerVkey {
}

func (b *ByronEpochBoundaryBlock) BlockBodySize() uint64 {
return b.Header.BlockBodySize()
// There's not really a body for an epoch boundary block
return 0
}

func (b *ByronEpochBoundaryBlock) Era() Era {
Expand Down

0 comments on commit aca581f

Please sign in to comment.