Skip to content

Commit 9eff0ee

Browse files
authored
chore: move era registry to sub-package (#691)
This also moves to a registration model for eras to avoid import cycles
1 parent faa045f commit 9eff0ee

File tree

10 files changed

+177
-82
lines changed

10 files changed

+177
-82
lines changed

ledger/allegra.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ import (
1818
"encoding/hex"
1919
"fmt"
2020

21-
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
22-
2321
"github.com/blinklabs-io/gouroboros/cbor"
2422
"github.com/blinklabs-io/gouroboros/ledger/common"
23+
24+
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
2525
)
2626

2727
const (
28-
EraIdAllegra = 2
28+
EraIdAllegra = 2
29+
EraNameAllegra = "Allegra"
2930

3031
BlockTypeAllegra = 3
3132

@@ -34,6 +35,17 @@ const (
3435
TxTypeAllegra = 2
3536
)
3637

38+
var (
39+
EraAllegra = common.Era{
40+
Id: EraIdAllegra,
41+
Name: EraNameAllegra,
42+
}
43+
)
44+
45+
func init() {
46+
common.RegisterEra(EraAllegra)
47+
}
48+
3749
type AllegraBlock struct {
3850
cbor.StructAsArray
3951
cbor.DecodeStoreCbor
@@ -68,7 +80,7 @@ func (b *AllegraBlock) BlockBodySize() uint64 {
6880
}
6981

7082
func (b *AllegraBlock) Era() Era {
71-
return eras[EraIdAllegra]
83+
return EraAllegra
7284
}
7385

7486
func (b *AllegraBlock) Transactions() []Transaction {
@@ -110,7 +122,7 @@ type AllegraBlockHeader struct {
110122
}
111123

112124
func (h *AllegraBlockHeader) Era() Era {
113-
return eras[EraIdAllegra]
125+
return EraAllegra
114126
}
115127

116128
type AllegraTransactionBody struct {

ledger/alonzo.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ import (
1919
"encoding/json"
2020
"fmt"
2121

22-
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
23-
2422
"github.com/blinklabs-io/gouroboros/cbor"
2523
"github.com/blinklabs-io/gouroboros/ledger/common"
24+
25+
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
2626
)
2727

2828
const (
29-
EraIdAlonzo = 4
29+
EraIdAlonzo = 4
30+
EraNameAlonzo = "Alonzo"
3031

3132
BlockTypeAlonzo = 5
3233

@@ -35,6 +36,17 @@ const (
3536
TxTypeAlonzo = 4
3637
)
3738

39+
var (
40+
EraAlonzo = common.Era{
41+
Id: EraIdAlonzo,
42+
Name: EraNameAlonzo,
43+
}
44+
)
45+
46+
func init() {
47+
common.RegisterEra(EraAlonzo)
48+
}
49+
3850
type AlonzoBlock struct {
3951
cbor.StructAsArray
4052
cbor.DecodeStoreCbor
@@ -70,7 +82,7 @@ func (b *AlonzoBlock) BlockBodySize() uint64 {
7082
}
7183

7284
func (b *AlonzoBlock) Era() Era {
73-
return eras[EraIdAlonzo]
85+
return EraAlonzo
7486
}
7587

7688
func (b *AlonzoBlock) Transactions() []Transaction {
@@ -118,7 +130,7 @@ type AlonzoBlockHeader struct {
118130
}
119131

120132
func (h *AlonzoBlockHeader) Era() Era {
121-
return eras[EraIdAlonzo]
133+
return EraAlonzo
122134
}
123135

124136
type AlonzoTransactionBody struct {

ledger/babbage.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ import (
1919
"encoding/json"
2020
"fmt"
2121

22-
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
23-
2422
"github.com/blinklabs-io/gouroboros/cbor"
2523
"github.com/blinklabs-io/gouroboros/ledger/common"
24+
25+
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
2626
)
2727

2828
const (
29-
EraIdBabbage = 5
29+
EraIdBabbage = 5
30+
EraNameBabbage = "Babbage"
3031

3132
BlockTypeBabbage = 6
3233

@@ -35,6 +36,17 @@ const (
3536
TxTypeBabbage = 5
3637
)
3738

39+
var (
40+
EraBabbage = common.Era{
41+
Id: EraIdBabbage,
42+
Name: EraNameBabbage,
43+
}
44+
)
45+
46+
func init() {
47+
common.RegisterEra(EraBabbage)
48+
}
49+
3850
type BabbageBlock struct {
3951
cbor.StructAsArray
4052
cbor.DecodeStoreCbor
@@ -70,7 +82,7 @@ func (b *BabbageBlock) BlockBodySize() uint64 {
7082
}
7183

7284
func (b *BabbageBlock) Era() Era {
73-
return eras[EraIdBabbage]
85+
return EraBabbage
7486
}
7587

7688
func (b *BabbageBlock) Transactions() []Transaction {
@@ -171,7 +183,7 @@ func (h *BabbageBlockHeader) BlockBodySize() uint64 {
171183
}
172184

173185
func (h *BabbageBlockHeader) Era() Era {
174-
return eras[EraIdBabbage]
186+
return EraBabbage
175187
}
176188

177189
type BabbageTransactionBody struct {

ledger/byron.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ import (
1818
"encoding/hex"
1919
"fmt"
2020

21-
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
22-
2321
"github.com/blinklabs-io/gouroboros/cbor"
2422
"github.com/blinklabs-io/gouroboros/ledger/common"
23+
24+
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
2525
)
2626

2727
const (
28-
EraIdByron = 0
28+
EraIdByron = 0
29+
EraNameByron = "Byron"
2930

3031
BlockTypeByronEbb = 0
3132
BlockTypeByronMain = 1
@@ -37,6 +38,17 @@ const (
3738
ByronSlotsPerEpoch = 21600
3839
)
3940

41+
var (
42+
EraByron = common.Era{
43+
Id: EraIdByron,
44+
Name: EraNameByron,
45+
}
46+
)
47+
48+
func init() {
49+
common.RegisterEra(EraByron)
50+
}
51+
4052
type ByronMainBlockHeader struct {
4153
cbor.StructAsArray
4254
cbor.DecodeStoreCbor
@@ -119,7 +131,7 @@ func (h *ByronMainBlockHeader) BlockBodySize() uint64 {
119131
}
120132

121133
func (h *ByronMainBlockHeader) Era() Era {
122-
return eras[EraIdByron]
134+
return EraByron
123135
}
124136

125137
type ByronTransaction struct {
@@ -481,7 +493,7 @@ func (h *ByronEpochBoundaryBlockHeader) BlockBodySize() uint64 {
481493
}
482494

483495
func (h *ByronEpochBoundaryBlockHeader) Era() Era {
484-
return eras[EraIdByron]
496+
return EraByron
485497
}
486498

487499
type ByronMainBlock struct {

ledger/common/era.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2024 Blink Labs Software
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package common
16+
17+
type Era struct {
18+
Id uint8
19+
Name string
20+
}
21+
22+
var EraInvalid = Era{
23+
Id: 0,
24+
Name: "invalid",
25+
}
26+
27+
var eras map[uint8]Era
28+
29+
func RegisterEra(era Era) {
30+
if eras == nil {
31+
eras = make(map[uint8]Era)
32+
}
33+
eras[era.Id] = era
34+
}
35+
36+
func EraById(eraId uint8) Era {
37+
era, ok := eras[eraId]
38+
if !ok {
39+
return EraInvalid
40+
}
41+
return era
42+
}

ledger/era_test.go renamed to ledger/common/era_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Blink Labs Software
1+
// Copyright 2024 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,11 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package ledger_test
15+
package common_test
1616

1717
import (
18-
"github.com/blinklabs-io/gouroboros/ledger"
1918
"testing"
19+
20+
_ "github.com/blinklabs-io/gouroboros/ledger" // This is needed to get the eras registered
21+
"github.com/blinklabs-io/gouroboros/ledger/common"
2022
)
2123

2224
type getEraByIdTestDefinition struct {
@@ -49,6 +51,10 @@ var getEraByIdTests = []getEraByIdTestDefinition{
4951
Id: 5,
5052
Name: "Babbage",
5153
},
54+
{
55+
Id: 6,
56+
Name: "Conway",
57+
},
5258
{
5359
Id: 99,
5460
Name: "invalid",
@@ -57,8 +63,8 @@ var getEraByIdTests = []getEraByIdTestDefinition{
5763

5864
func TestGetEraById(t *testing.T) {
5965
for _, test := range getEraByIdTests {
60-
era := ledger.GetEraById(test.Id)
61-
if era == ledger.EraInvalid {
66+
era := common.EraById(test.Id)
67+
if era == common.EraInvalid {
6268
if test.Name != "invalid" {
6369
t.Fatalf("got unexpected EraInvalid, wanted %s", test.Name)
6470
}

ledger/conway.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ import (
1818
"encoding/hex"
1919
"fmt"
2020

21-
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
22-
2321
"github.com/blinklabs-io/gouroboros/cbor"
2422
"github.com/blinklabs-io/gouroboros/ledger/common"
23+
24+
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
2525
)
2626

2727
const (
28-
EraIdConway = 6
28+
EraIdConway = 6
29+
EraNameConway = "Conway"
2930

3031
BlockTypeConway = 7
3132

@@ -34,6 +35,17 @@ const (
3435
TxTypeConway = 6
3536
)
3637

38+
var (
39+
EraConway = common.Era{
40+
Id: EraIdConway,
41+
Name: EraNameConway,
42+
}
43+
)
44+
45+
func init() {
46+
common.RegisterEra(EraConway)
47+
}
48+
3749
type ConwayBlock struct {
3850
cbor.StructAsArray
3951
cbor.DecodeStoreCbor
@@ -69,7 +81,7 @@ func (b *ConwayBlock) BlockBodySize() uint64 {
6981
}
7082

7183
func (b *ConwayBlock) Era() Era {
72-
return eras[EraIdConway]
84+
return EraConway
7385
}
7486

7587
func (b *ConwayBlock) Transactions() []Transaction {
@@ -117,7 +129,7 @@ type ConwayBlockHeader struct {
117129
}
118130

119131
func (h *ConwayBlockHeader) Era() Era {
120-
return eras[EraIdConway]
132+
return EraConway
121133
}
122134

123135
type ConwayRedeemerKey struct {

0 commit comments

Comments
 (0)