15
15
package shelley
16
16
17
17
import (
18
+ "encoding/hex"
18
19
"encoding/json"
19
20
"io"
20
21
"os"
21
22
"time"
22
23
24
+ "github.com/blinklabs-io/gouroboros/cbor"
23
25
"github.com/blinklabs-io/gouroboros/ledger/common"
24
26
)
25
27
26
28
type ShelleyGenesis struct {
29
+ cbor.StructAsArray
27
30
SystemStart time.Time `json:"systemStart"`
28
31
NetworkMagic uint32 `json:"networkMagic"`
29
32
NetworkId string `json:"networkid"`
30
- ActiveSlotsCoeff float32 `json:"activeSlotsCoeff"`
33
+ ActiveSlotsCoeff common. GenesisRat `json:"activeSlotsCoeff"`
31
34
SecurityParam int `json:"securityParam"`
32
35
EpochLength int `json:"epochLength"`
33
36
SlotsPerKESPeriod int `json:"slotsPerKESPeriod"`
@@ -36,12 +39,67 @@ type ShelleyGenesis struct {
36
39
UpdateQuorum int `json:"updateQuorum"`
37
40
MaxLovelaceSupply uint64 `json:"maxLovelaceSupply"`
38
41
ProtocolParameters ShelleyGenesisProtocolParams `json:"protocolParams"`
39
- GenDelegs map [string ]map [string ]any `json:"genDelegs"`
42
+ GenDelegs map [string ]map [string ]string `json:"genDelegs"`
40
43
InitialFunds map [string ]any `json:"initialFunds"`
41
44
Staking any `json:"staking"`
42
45
}
43
46
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
+
44
101
type ShelleyGenesisProtocolParams struct {
102
+ cbor.StructAsArray
45
103
MinFeeA uint
46
104
MinFeeB uint
47
105
MaxBlockBodySize uint
@@ -64,6 +122,40 @@ type ShelleyGenesisProtocolParams struct {
64
122
MinPoolCost uint
65
123
}
66
124
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
+
67
159
func NewShelleyGenesisFromReader (r io.Reader ) (ShelleyGenesis , error ) {
68
160
var ret ShelleyGenesis
69
161
dec := json .NewDecoder (r )
0 commit comments