Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 94 additions & 2 deletions ledger/shelley/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@
package shelley

import (
"encoding/hex"
"encoding/json"
"io"
"os"
"time"

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

type ShelleyGenesis struct {
cbor.StructAsArray
SystemStart time.Time `json:"systemStart"`
NetworkMagic uint32 `json:"networkMagic"`
NetworkId string `json:"networkid"`
ActiveSlotsCoeff float32 `json:"activeSlotsCoeff"`
ActiveSlotsCoeff common.GenesisRat `json:"activeSlotsCoeff"`
SecurityParam int `json:"securityParam"`
EpochLength int `json:"epochLength"`
SlotsPerKESPeriod int `json:"slotsPerKESPeriod"`
Expand All @@ -36,12 +39,67 @@ type ShelleyGenesis struct {
UpdateQuorum int `json:"updateQuorum"`
MaxLovelaceSupply uint64 `json:"maxLovelaceSupply"`
ProtocolParameters ShelleyGenesisProtocolParams `json:"protocolParams"`
GenDelegs map[string]map[string]any `json:"genDelegs"`
GenDelegs map[string]map[string]string `json:"genDelegs"`
InitialFunds map[string]any `json:"initialFunds"`
Staking any `json:"staking"`
}

func (g ShelleyGenesis) MarshalCBOR() ([]byte, error) {
genDelegs := map[cbor.ByteString][]cbor.ByteString{}
for k, v := range g.GenDelegs {
keyBytes, err := hex.DecodeString(k)
if err != nil {
return nil, err
}
vrfBytes, err := hex.DecodeString(v["vrf"])
if err != nil {
return nil, err
}
delegateBytes, err := hex.DecodeString(v["delegate"])
if err != nil {
return nil, err
}
genDelegs[cbor.NewByteString(keyBytes)] = []cbor.ByteString{
cbor.NewByteString(delegateBytes),
cbor.NewByteString(vrfBytes),
}
}
staking := []any{}
if g.Staking == nil {
staking = []any{
map[any]any{},
map[any]any{},
}
}
tmpData := []any{
[]any{
g.SystemStart.Year(),
g.SystemStart.YearDay(),
g.SystemStart.Nanosecond() * 1000,
},
g.NetworkMagic,
map[string]int{"Testnet": 0, "Mainnet": 1}[g.NetworkId],
[]any{
g.ActiveSlotsCoeff.Num().Int64(),
g.ActiveSlotsCoeff.Denom().Int64(),
},
g.SecurityParam,
g.EpochLength,
g.SlotsPerKESPeriod,
g.MaxKESEvolutions,
g.SlotLength * 1_000_000,
g.UpdateQuorum,
g.MaxLovelaceSupply,
g.ProtocolParameters,
genDelegs,
g.InitialFunds,
staking,
}
return cbor.Encode(tmpData)
}

type ShelleyGenesisProtocolParams struct {
cbor.StructAsArray
MinFeeA uint
MinFeeB uint
MaxBlockBodySize uint
Expand All @@ -64,6 +122,40 @@ type ShelleyGenesisProtocolParams struct {
MinPoolCost uint
}

func (p ShelleyGenesisProtocolParams) MarshalCBOR() ([]byte, error) {
tmpData := []any{
p.MinFeeA,
p.MinFeeB,
p.MaxBlockBodySize,
p.MaxTxSize,
p.MaxBlockHeaderSize,
p.KeyDeposit,
p.PoolDeposit,
p.MaxEpoch,
p.NOpt,
cbor.Rat{
Rat: p.A0.Rat,
},
cbor.Rat{
Rat: p.Rho.Rat,
},
cbor.Rat{
Rat: p.Tau.Rat,
},
cbor.Rat{
Rat: p.Decentralization.Rat,
},
[]any{
map[string]int{"NeutralNonce": 0}[p.ExtraEntropy["Tag"]],
},
p.ProtocolVersion.Major,
p.ProtocolVersion.Minor,
p.MinUtxoValue,
p.MinPoolCost,
}
return cbor.Encode(tmpData)
}

func NewShelleyGenesisFromReader(r io.Reader) (ShelleyGenesis, error) {
var ret ShelleyGenesis
dec := json.NewDecoder(r)
Expand Down
10 changes: 6 additions & 4 deletions ledger/shelley/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ var expectedGenesisObj = shelley.ShelleyGenesis{
0,
time.UTC,
),
NetworkMagic: 764824073,
NetworkId: "Mainnet",
ActiveSlotsCoeff: 0.05,
NetworkMagic: 764824073,
NetworkId: "Mainnet",
ActiveSlotsCoeff: common.GenesisRat{
Rat: big.NewRat(5, 100),
},
SecurityParam: 2160,
EpochLength: 432000,
SlotsPerKESPeriod: 129600,
Expand Down Expand Up @@ -144,7 +146,7 @@ var expectedGenesisObj = shelley.ShelleyGenesis{
MinUtxoValue: 1000000,
MinPoolCost: 340000000,
},
GenDelegs: map[string]map[string]any{
GenDelegs: map[string]map[string]string{
"162f94554ac8c225383a2248c245659eda870eaa82d0ef25fc7dcd82": {
"delegate": "4485708022839a7b9b8b639a939c85ec0ed6999b5b6dc651b03c43f6",
"vrf": "aba81e764b71006c515986bf7b37a72fbb5554f78e6775f08e384dbd572a4b32",
Expand Down
Loading