Skip to content

Commit

Permalink
chore: move era registry to sub-package (#691)
Browse files Browse the repository at this point in the history
This also moves to a registration model for eras to avoid import cycles
  • Loading branch information
agaffney committed Aug 25, 2024
1 parent faa045f commit 9eff0ee
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 82 deletions.
22 changes: 17 additions & 5 deletions ledger/allegra.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import (
"encoding/hex"
"fmt"

utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/common"

utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
)

const (
EraIdAllegra = 2
EraIdAllegra = 2
EraNameAllegra = "Allegra"

BlockTypeAllegra = 3

Expand All @@ -34,6 +35,17 @@ const (
TxTypeAllegra = 2
)

var (
EraAllegra = common.Era{
Id: EraIdAllegra,
Name: EraNameAllegra,
}
)

func init() {
common.RegisterEra(EraAllegra)
}

type AllegraBlock struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
Expand Down Expand Up @@ -68,7 +80,7 @@ func (b *AllegraBlock) BlockBodySize() uint64 {
}

func (b *AllegraBlock) Era() Era {
return eras[EraIdAllegra]
return EraAllegra
}

func (b *AllegraBlock) Transactions() []Transaction {
Expand Down Expand Up @@ -110,7 +122,7 @@ type AllegraBlockHeader struct {
}

func (h *AllegraBlockHeader) Era() Era {
return eras[EraIdAllegra]
return EraAllegra
}

type AllegraTransactionBody struct {
Expand Down
22 changes: 17 additions & 5 deletions ledger/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ import (
"encoding/json"
"fmt"

utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/common"

utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
)

const (
EraIdAlonzo = 4
EraIdAlonzo = 4
EraNameAlonzo = "Alonzo"

BlockTypeAlonzo = 5

Expand All @@ -35,6 +36,17 @@ const (
TxTypeAlonzo = 4
)

var (
EraAlonzo = common.Era{
Id: EraIdAlonzo,
Name: EraNameAlonzo,
}
)

func init() {
common.RegisterEra(EraAlonzo)
}

type AlonzoBlock struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
Expand Down Expand Up @@ -70,7 +82,7 @@ func (b *AlonzoBlock) BlockBodySize() uint64 {
}

func (b *AlonzoBlock) Era() Era {
return eras[EraIdAlonzo]
return EraAlonzo
}

func (b *AlonzoBlock) Transactions() []Transaction {
Expand Down Expand Up @@ -118,7 +130,7 @@ type AlonzoBlockHeader struct {
}

func (h *AlonzoBlockHeader) Era() Era {
return eras[EraIdAlonzo]
return EraAlonzo
}

type AlonzoTransactionBody struct {
Expand Down
22 changes: 17 additions & 5 deletions ledger/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ import (
"encoding/json"
"fmt"

utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/common"

utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
)

const (
EraIdBabbage = 5
EraIdBabbage = 5
EraNameBabbage = "Babbage"

BlockTypeBabbage = 6

Expand All @@ -35,6 +36,17 @@ const (
TxTypeBabbage = 5
)

var (
EraBabbage = common.Era{
Id: EraIdBabbage,
Name: EraNameBabbage,
}
)

func init() {
common.RegisterEra(EraBabbage)
}

type BabbageBlock struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
Expand Down Expand Up @@ -70,7 +82,7 @@ func (b *BabbageBlock) BlockBodySize() uint64 {
}

func (b *BabbageBlock) Era() Era {
return eras[EraIdBabbage]
return EraBabbage
}

func (b *BabbageBlock) Transactions() []Transaction {
Expand Down Expand Up @@ -171,7 +183,7 @@ func (h *BabbageBlockHeader) BlockBodySize() uint64 {
}

func (h *BabbageBlockHeader) Era() Era {
return eras[EraIdBabbage]
return EraBabbage
}

type BabbageTransactionBody struct {
Expand Down
22 changes: 17 additions & 5 deletions ledger/byron.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import (
"encoding/hex"
"fmt"

utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/common"

utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
)

const (
EraIdByron = 0
EraIdByron = 0
EraNameByron = "Byron"

BlockTypeByronEbb = 0
BlockTypeByronMain = 1
Expand All @@ -37,6 +38,17 @@ const (
ByronSlotsPerEpoch = 21600
)

var (
EraByron = common.Era{
Id: EraIdByron,
Name: EraNameByron,
}
)

func init() {
common.RegisterEra(EraByron)
}

type ByronMainBlockHeader struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
Expand Down Expand Up @@ -119,7 +131,7 @@ func (h *ByronMainBlockHeader) BlockBodySize() uint64 {
}

func (h *ByronMainBlockHeader) Era() Era {
return eras[EraIdByron]
return EraByron
}

type ByronTransaction struct {
Expand Down Expand Up @@ -481,7 +493,7 @@ func (h *ByronEpochBoundaryBlockHeader) BlockBodySize() uint64 {
}

func (h *ByronEpochBoundaryBlockHeader) Era() Era {
return eras[EraIdByron]
return EraByron
}

type ByronMainBlock struct {
Expand Down
42 changes: 42 additions & 0 deletions ledger/common/era.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package common

type Era struct {
Id uint8
Name string
}

var EraInvalid = Era{
Id: 0,
Name: "invalid",
}

var eras map[uint8]Era

func RegisterEra(era Era) {
if eras == nil {
eras = make(map[uint8]Era)
}
eras[era.Id] = era
}

func EraById(eraId uint8) Era {
era, ok := eras[eraId]
if !ok {
return EraInvalid
}
return era
}
16 changes: 11 additions & 5 deletions ledger/era_test.go → ledger/common/era_test.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 All @@ -12,11 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package ledger_test
package common_test

import (
"github.com/blinklabs-io/gouroboros/ledger"
"testing"

_ "github.com/blinklabs-io/gouroboros/ledger" // This is needed to get the eras registered
"github.com/blinklabs-io/gouroboros/ledger/common"
)

type getEraByIdTestDefinition struct {
Expand Down Expand Up @@ -49,6 +51,10 @@ var getEraByIdTests = []getEraByIdTestDefinition{
Id: 5,
Name: "Babbage",
},
{
Id: 6,
Name: "Conway",
},
{
Id: 99,
Name: "invalid",
Expand All @@ -57,8 +63,8 @@ var getEraByIdTests = []getEraByIdTestDefinition{

func TestGetEraById(t *testing.T) {
for _, test := range getEraByIdTests {
era := ledger.GetEraById(test.Id)
if era == ledger.EraInvalid {
era := common.EraById(test.Id)
if era == common.EraInvalid {
if test.Name != "invalid" {
t.Fatalf("got unexpected EraInvalid, wanted %s", test.Name)
}
Expand Down
22 changes: 17 additions & 5 deletions ledger/conway.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import (
"encoding/hex"
"fmt"

utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/common"

utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
)

const (
EraIdConway = 6
EraIdConway = 6
EraNameConway = "Conway"

BlockTypeConway = 7

Expand All @@ -34,6 +35,17 @@ const (
TxTypeConway = 6
)

var (
EraConway = common.Era{
Id: EraIdConway,
Name: EraNameConway,
}
)

func init() {
common.RegisterEra(EraConway)
}

type ConwayBlock struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
Expand Down Expand Up @@ -69,7 +81,7 @@ func (b *ConwayBlock) BlockBodySize() uint64 {
}

func (b *ConwayBlock) Era() Era {
return eras[EraIdConway]
return EraConway
}

func (b *ConwayBlock) Transactions() []Transaction {
Expand Down Expand Up @@ -117,7 +129,7 @@ type ConwayBlockHeader struct {
}

func (h *ConwayBlockHeader) Era() Era {
return eras[EraIdConway]
return EraConway
}

type ConwayRedeemerKey struct {
Expand Down
Loading

0 comments on commit 9eff0ee

Please sign in to comment.