Skip to content

Commit 23aebae

Browse files
authored
fix: custom CBOR marshaling for ShelleyGenesisConfig (#818)
1 parent eedb336 commit 23aebae

File tree

2 files changed

+100
-6
lines changed

2 files changed

+100
-6
lines changed

ledger/shelley/genesis.go

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,22 @@
1515
package shelley
1616

1717
import (
18+
"encoding/hex"
1819
"encoding/json"
1920
"io"
2021
"os"
2122
"time"
2223

24+
"github.com/blinklabs-io/gouroboros/cbor"
2325
"github.com/blinklabs-io/gouroboros/ledger/common"
2426
)
2527

2628
type ShelleyGenesis struct {
29+
cbor.StructAsArray
2730
SystemStart time.Time `json:"systemStart"`
2831
NetworkMagic uint32 `json:"networkMagic"`
2932
NetworkId string `json:"networkid"`
30-
ActiveSlotsCoeff float32 `json:"activeSlotsCoeff"`
33+
ActiveSlotsCoeff common.GenesisRat `json:"activeSlotsCoeff"`
3134
SecurityParam int `json:"securityParam"`
3235
EpochLength int `json:"epochLength"`
3336
SlotsPerKESPeriod int `json:"slotsPerKESPeriod"`
@@ -36,12 +39,67 @@ type ShelleyGenesis struct {
3639
UpdateQuorum int `json:"updateQuorum"`
3740
MaxLovelaceSupply uint64 `json:"maxLovelaceSupply"`
3841
ProtocolParameters ShelleyGenesisProtocolParams `json:"protocolParams"`
39-
GenDelegs map[string]map[string]any `json:"genDelegs"`
42+
GenDelegs map[string]map[string]string `json:"genDelegs"`
4043
InitialFunds map[string]any `json:"initialFunds"`
4144
Staking any `json:"staking"`
4245
}
4346

47+
func (g ShelleyGenesis) MarshalCBOR() ([]byte, error) {
48+
genDelegs := map[cbor.ByteString][]cbor.ByteString{}
49+
for k, v := range g.GenDelegs {
50+
keyBytes, err := hex.DecodeString(k)
51+
if err != nil {
52+
return nil, err
53+
}
54+
vrfBytes, err := hex.DecodeString(v["vrf"])
55+
if err != nil {
56+
return nil, err
57+
}
58+
delegateBytes, err := hex.DecodeString(v["delegate"])
59+
if err != nil {
60+
return nil, err
61+
}
62+
genDelegs[cbor.NewByteString(keyBytes)] = []cbor.ByteString{
63+
cbor.NewByteString(delegateBytes),
64+
cbor.NewByteString(vrfBytes),
65+
}
66+
}
67+
staking := []any{}
68+
if g.Staking == nil {
69+
staking = []any{
70+
map[any]any{},
71+
map[any]any{},
72+
}
73+
}
74+
tmpData := []any{
75+
[]any{
76+
g.SystemStart.Year(),
77+
g.SystemStart.YearDay(),
78+
g.SystemStart.Nanosecond() * 1000,
79+
},
80+
g.NetworkMagic,
81+
map[string]int{"Testnet": 0, "Mainnet": 1}[g.NetworkId],
82+
[]any{
83+
g.ActiveSlotsCoeff.Num().Int64(),
84+
g.ActiveSlotsCoeff.Denom().Int64(),
85+
},
86+
g.SecurityParam,
87+
g.EpochLength,
88+
g.SlotsPerKESPeriod,
89+
g.MaxKESEvolutions,
90+
g.SlotLength * 1_000_000,
91+
g.UpdateQuorum,
92+
g.MaxLovelaceSupply,
93+
g.ProtocolParameters,
94+
genDelegs,
95+
g.InitialFunds,
96+
staking,
97+
}
98+
return cbor.Encode(tmpData)
99+
}
100+
44101
type ShelleyGenesisProtocolParams struct {
102+
cbor.StructAsArray
45103
MinFeeA uint
46104
MinFeeB uint
47105
MaxBlockBodySize uint
@@ -64,6 +122,40 @@ type ShelleyGenesisProtocolParams struct {
64122
MinPoolCost uint
65123
}
66124

125+
func (p ShelleyGenesisProtocolParams) MarshalCBOR() ([]byte, error) {
126+
tmpData := []any{
127+
p.MinFeeA,
128+
p.MinFeeB,
129+
p.MaxBlockBodySize,
130+
p.MaxTxSize,
131+
p.MaxBlockHeaderSize,
132+
p.KeyDeposit,
133+
p.PoolDeposit,
134+
p.MaxEpoch,
135+
p.NOpt,
136+
cbor.Rat{
137+
Rat: p.A0.Rat,
138+
},
139+
cbor.Rat{
140+
Rat: p.Rho.Rat,
141+
},
142+
cbor.Rat{
143+
Rat: p.Tau.Rat,
144+
},
145+
cbor.Rat{
146+
Rat: p.Decentralization.Rat,
147+
},
148+
[]any{
149+
map[string]int{"NeutralNonce": 0}[p.ExtraEntropy["Tag"]],
150+
},
151+
p.ProtocolVersion.Major,
152+
p.ProtocolVersion.Minor,
153+
p.MinUtxoValue,
154+
p.MinPoolCost,
155+
}
156+
return cbor.Encode(tmpData)
157+
}
158+
67159
func NewShelleyGenesisFromReader(r io.Reader) (ShelleyGenesis, error) {
68160
var ret ShelleyGenesis
69161
dec := json.NewDecoder(r)

ledger/shelley/genesis_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,11 @@ var expectedGenesisObj = shelley.ShelleyGenesis{
107107
0,
108108
time.UTC,
109109
),
110-
NetworkMagic: 764824073,
111-
NetworkId: "Mainnet",
112-
ActiveSlotsCoeff: 0.05,
110+
NetworkMagic: 764824073,
111+
NetworkId: "Mainnet",
112+
ActiveSlotsCoeff: common.GenesisRat{
113+
Rat: big.NewRat(5, 100),
114+
},
113115
SecurityParam: 2160,
114116
EpochLength: 432000,
115117
SlotsPerKESPeriod: 129600,
@@ -144,7 +146,7 @@ var expectedGenesisObj = shelley.ShelleyGenesis{
144146
MinUtxoValue: 1000000,
145147
MinPoolCost: 340000000,
146148
},
147-
GenDelegs: map[string]map[string]any{
149+
GenDelegs: map[string]map[string]string{
148150
"162f94554ac8c225383a2248c245659eda870eaa82d0ef25fc7dcd82": {
149151
"delegate": "4485708022839a7b9b8b639a939c85ec0ed6999b5b6dc651b03c43f6",
150152
"vrf": "aba81e764b71006c515986bf7b37a72fbb5554f78e6775f08e384dbd572a4b32",

0 commit comments

Comments
 (0)