Skip to content

Commit

Permalink
feat: Conway protocol parameter updates
Browse files Browse the repository at this point in the history
* update protocol parameter set from protocol param update spec
* update protocol parameter set from genesis
* move GenesisRat type to common

Fixes #746
  • Loading branch information
agaffney committed Oct 9, 2024
1 parent 8fb05a9 commit ba0d25d
Show file tree
Hide file tree
Showing 5 changed files with 391 additions and 114 deletions.
79 changes: 0 additions & 79 deletions ledger/conway/conway.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,85 +227,6 @@ func (b *ConwayTransactionBody) Donation() uint64 {
return b.TxDonation
}

type ConwayProtocolParameters struct {
cbor.StructAsArray
MinFeeA uint
MinFeeB uint
MaxBlockBodySize uint
MaxTxSize uint
MaxBlockHeaderSize uint
KeyDeposit uint
PoolDeposit uint
MaxEpoch uint
NOpt uint
A0 *cbor.Rat
Rho *cbor.Rat
Tau *cbor.Rat
Protocol struct {
cbor.StructAsArray
Major uint
Minor uint
}
MinPoolCost uint
AdaPerUtxoByte uint
CostModels map[uint][]int
ExecutionUnitPrices []*cbor.Rat // [priceMemory priceSteps]
MaxTxExecutionUnits []uint
MaxBlockExecutionUnits []uint
MaxValueSize uint
CollateralPercentage uint
MaxCollateralInputs uint
PoolVotingThresholds PoolVotingThresholds
DRepVotingThresholds DRepVotingThresholds
MinCommitteeSize uint
CommitteeTermLimit uint64
GovActionValidityPeriod uint64
GovActionDeposit uint64
DRepDeposit uint64
DRepInactivityPeriod uint64
MinFeeRefScriptCostPerByte *cbor.Rat
}

type ConwayProtocolParameterUpdate struct {
babbage.BabbageProtocolParameterUpdate
PoolVotingThresholds PoolVotingThresholds `cbor:"25,keyasint"`
DRepVotingThresholds DRepVotingThresholds `cbor:"26,keyasint"`
MinCommitteeSize uint `cbor:"27,keyasint"`
CommitteeTermLimit uint64 `cbor:"28,keyasint"`
GovActionValidityPeriod uint64 `cbor:"29,keyasint"`
GovActionDeposit uint64 `cbor:"30,keyasint"`
DRepDeposit uint64 `cbor:"31,keyasint"`
DRepInactivityPeriod uint64 `cbor:"32,keyasint"`
MinFeeRefScriptCostPerByte *cbor.Rat `cbor:"33,keyasint"`
}

func (u *ConwayProtocolParameterUpdate) UnmarshalCBOR(data []byte) error {
return u.UnmarshalCbor(data, u)
}

type PoolVotingThresholds struct {
cbor.StructAsArray
MotionNoConfidence cbor.Rat
CommitteeNormal cbor.Rat
CommitteeNoConfidence cbor.Rat
HardForkInitiation cbor.Rat
SecurityRelevantParameterVotingThreshold cbor.Rat
}

type DRepVotingThresholds struct {
cbor.StructAsArray
MotionNoConfidence cbor.Rat
CommitteeNormal cbor.Rat
CommitteeNoConfidence cbor.Rat
UpdateConstitution cbor.Rat
HardForkInitiation cbor.Rat
PPNetworkGroup cbor.Rat
PPEconomicGroup cbor.Rat
PPTechnicalGroup cbor.Rat
PPGovGroup cbor.Rat
TreasureWithdrawal cbor.Rat
}

type ConwayTransaction struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
Expand Down
34 changes: 18 additions & 16 deletions ledger/conway/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"encoding/json"
"io"
"os"

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

type ConwayGenesis struct {
Expand All @@ -29,31 +31,31 @@ type ConwayGenesis struct {
GovActionDeposit uint64
DRepDeposit uint64 `json:"dRepDeposit"`
DRepInactivityPeriod uint64 `json:"dRepActivity"`
MinFeeRefScriptCostPerByte float32
MinFeeRefScriptCostPerByte *common.GenesisRat
PlutusV3CostModel []int `json:"plutusV3CostModel"`
Constitution ConwayGenesisConstitution
Committee ConwayGenesisCommittee
}

type ConwayGenesisPoolVotingThresholds struct {
CommitteeNormal float32
CommitteeNoConfidence float32
HardForkInitiation float32
MotionNoConfidence float32
PpSecurityGroup float32
CommitteeNormal *common.GenesisRat
CommitteeNoConfidence *common.GenesisRat
HardForkInitiation *common.GenesisRat
MotionNoConfidence *common.GenesisRat
PpSecurityGroup *common.GenesisRat
}

type ConwayGenesisDRepVotingThresholds struct {
MotionNoConfidence float32
CommitteeNormal float32
CommitteeNoConfidence float32
UpdateToConstitution float32
HardForkInitiation float32
PpNetworkGroup float32
PpEconomicGroup float32
PpTechnicalGroup float32
PpGovGroup float32
TreasuryWithdrawal float32
MotionNoConfidence *common.GenesisRat
CommitteeNormal *common.GenesisRat
CommitteeNoConfidence *common.GenesisRat
UpdateToConstitution *common.GenesisRat
HardForkInitiation *common.GenesisRat
PpNetworkGroup *common.GenesisRat
PpEconomicGroup *common.GenesisRat
PpTechnicalGroup *common.GenesisRat
PpGovGroup *common.GenesisRat
TreasuryWithdrawal *common.GenesisRat
}

type ConwayGenesisConstitution struct {
Expand Down
232 changes: 232 additions & 0 deletions ledger/conway/pparams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
// 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 conway

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

type ConwayProtocolParameters struct {
cbor.StructAsArray
MinFeeA uint
MinFeeB uint
MaxBlockBodySize uint
MaxTxSize uint
MaxBlockHeaderSize uint
KeyDeposit uint
PoolDeposit uint
MaxEpoch uint
NOpt uint
A0 *cbor.Rat
Rho *cbor.Rat
Tau *cbor.Rat
ProtocolVersion common.ProtocolParametersProtocolVersion
MinPoolCost uint64
AdaPerUtxoByte uint64
CostModels map[uint][]uint64
ExecutionCosts common.ExUnitPrice
MaxTxExUnits common.ExUnit
MaxBlockExUnits common.ExUnit
MaxValueSize uint
CollateralPercentage uint
MaxCollateralInputs uint
PoolVotingThresholds PoolVotingThresholds
DRepVotingThresholds DRepVotingThresholds
MinCommitteeSize uint
CommitteeTermLimit uint64
GovActionValidityPeriod uint64
GovActionDeposit uint64
DRepDeposit uint64
DRepInactivityPeriod uint64
MinFeeRefScriptCostPerByte *cbor.Rat
}

func (p *ConwayProtocolParameters) Update(paramUpdate *ConwayProtocolParameterUpdate) {
if paramUpdate.MinFeeA != nil {
p.MinFeeA = *paramUpdate.MinFeeA
}
if paramUpdate.MinFeeB != nil {
p.MinFeeB = *paramUpdate.MinFeeB
}
if paramUpdate.MaxBlockBodySize != nil {
p.MaxBlockBodySize = *paramUpdate.MaxBlockBodySize
}
if paramUpdate.MaxTxSize != nil {
p.MaxTxSize = *paramUpdate.MaxTxSize
}
if paramUpdate.MaxBlockHeaderSize != nil {
p.MaxBlockHeaderSize = *paramUpdate.MaxBlockHeaderSize
}
if paramUpdate.KeyDeposit != nil {
p.KeyDeposit = *paramUpdate.KeyDeposit
}
if paramUpdate.PoolDeposit != nil {
p.PoolDeposit = *paramUpdate.PoolDeposit
}
if paramUpdate.MaxEpoch != nil {
p.MaxEpoch = *paramUpdate.MaxEpoch
}
if paramUpdate.NOpt != nil {
p.NOpt = *paramUpdate.NOpt
}
if paramUpdate.A0 != nil {
p.A0 = paramUpdate.A0
}
if paramUpdate.Rho != nil {
p.Rho = paramUpdate.Rho
}
if paramUpdate.Tau != nil {
p.Tau = paramUpdate.Tau
}
if paramUpdate.ProtocolVersion != nil {
p.ProtocolVersion = *paramUpdate.ProtocolVersion
}
if paramUpdate.MinPoolCost != nil {
p.MinPoolCost = *paramUpdate.MinPoolCost
}
if paramUpdate.AdaPerUtxoByte != nil {
p.AdaPerUtxoByte = *paramUpdate.AdaPerUtxoByte
}
if paramUpdate.CostModels != nil {
p.CostModels = paramUpdate.CostModels
}
if paramUpdate.ExecutionCosts != nil {
p.ExecutionCosts = *paramUpdate.ExecutionCosts
}
if paramUpdate.MaxTxExUnits != nil {
p.MaxTxExUnits = *paramUpdate.MaxTxExUnits
}
if paramUpdate.MaxBlockExUnits != nil {
p.MaxBlockExUnits = *paramUpdate.MaxBlockExUnits
}
if paramUpdate.MaxValueSize != nil {
p.MaxValueSize = *paramUpdate.MaxValueSize
}
if paramUpdate.CollateralPercentage != nil {
p.CollateralPercentage = *paramUpdate.CollateralPercentage
}
if paramUpdate.MaxCollateralInputs != nil {
p.MaxCollateralInputs = *paramUpdate.MaxCollateralInputs
}
if paramUpdate.PoolVotingThresholds != nil {
p.PoolVotingThresholds = *paramUpdate.PoolVotingThresholds
}
if paramUpdate.DRepVotingThresholds != nil {
p.DRepVotingThresholds = *paramUpdate.DRepVotingThresholds
}
if paramUpdate.MinCommitteeSize != nil {
p.MinCommitteeSize = *paramUpdate.MinCommitteeSize
}
if paramUpdate.CommitteeTermLimit != nil {
p.CommitteeTermLimit = *paramUpdate.CommitteeTermLimit
}
if paramUpdate.GovActionValidityPeriod != nil {
p.GovActionValidityPeriod = *paramUpdate.GovActionValidityPeriod
}
if paramUpdate.GovActionDeposit != nil {
p.GovActionDeposit = *paramUpdate.GovActionDeposit
}
if paramUpdate.DRepDeposit != nil {
p.DRepDeposit = *paramUpdate.DRepDeposit
}
if paramUpdate.DRepInactivityPeriod != nil {
p.DRepInactivityPeriod = *paramUpdate.DRepInactivityPeriod
}
if paramUpdate.MinFeeRefScriptCostPerByte != nil {
p.MinFeeRefScriptCostPerByte = paramUpdate.MinFeeRefScriptCostPerByte
}
}

func (p *ConwayProtocolParameters) UpdateFromGenesis(genesis *ConwayGenesis) {
// TODO
/*
PoolVotingThresholds ConwayGenesisPoolVotingThresholds
DRepVotingThresholds ConwayGenesisDRepVotingThresholds
MinCommitteeSize uint `json:"committeeMinSize"`
CommitteeTermLimit uint64 `json:"committeeMaxTermLength"`
GovActionValidityPeriod uint64 `json:"govActionLifetime"`
GovActionDeposit uint64
DRepDeposit uint64 `json:"dRepDeposit"`
DRepInactivityPeriod uint64 `json:"dRepActivity"`
MinFeeRefScriptCostPerByte float32
PlutusV3CostModel []int `json:"plutusV3CostModel"`
*/
/*
// XXX: do we need to convert this?
p.AdaPerUtxoByte = genesis.LovelacePerUtxoWord
p.MaxValueSize = genesis.MaxValueSize
p.CollateralPercentage = genesis.CollateralPercentage
p.MaxCollateralInputs = genesis.MaxCollateralInputs
p.MaxTxExUnits = common.ExUnit{
Mem: genesis.MaxTxExUnits.Mem,
Steps: genesis.MaxTxExUnits.Steps,
}
p.MaxBlockExUnits = common.ExUnit{
Mem: genesis.MaxBlockExUnits.Mem,
Steps: genesis.MaxBlockExUnits.Steps,
}
if genesis.ExecutionPrices.Mem != nil && genesis.ExecutionPrices.Steps != nil {
p.ExecutionCosts = common.ExUnitPrice{
MemPrice: &cbor.Rat{Rat: genesis.ExecutionPrices.Mem.Rat},
StepPrice: &cbor.Rat{Rat: genesis.ExecutionPrices.Steps.Rat},
}
}
// TODO: cost models
// We have 150+ string values to map to array indexes
// CostModels map[string]map[string]int
*/
}

type ConwayProtocolParameterUpdate struct {
babbage.BabbageProtocolParameterUpdate
PoolVotingThresholds *PoolVotingThresholds `cbor:"25,keyasint"`
DRepVotingThresholds *DRepVotingThresholds `cbor:"26,keyasint"`
MinCommitteeSize *uint `cbor:"27,keyasint"`
CommitteeTermLimit *uint64 `cbor:"28,keyasint"`
GovActionValidityPeriod *uint64 `cbor:"29,keyasint"`
GovActionDeposit *uint64 `cbor:"30,keyasint"`
DRepDeposit *uint64 `cbor:"31,keyasint"`
DRepInactivityPeriod *uint64 `cbor:"32,keyasint"`
MinFeeRefScriptCostPerByte *cbor.Rat `cbor:"33,keyasint"`
}

func (u *ConwayProtocolParameterUpdate) UnmarshalCBOR(data []byte) error {
return u.UnmarshalCbor(data, u)
}

type PoolVotingThresholds struct {
cbor.StructAsArray
MotionNoConfidence cbor.Rat
CommitteeNormal cbor.Rat
CommitteeNoConfidence cbor.Rat
HardForkInitiation cbor.Rat
SecurityRelevantParameterVotingThreshold cbor.Rat
}

type DRepVotingThresholds struct {
cbor.StructAsArray
MotionNoConfidence cbor.Rat
CommitteeNormal cbor.Rat
CommitteeNoConfidence cbor.Rat
UpdateConstitution cbor.Rat
HardForkInitiation cbor.Rat
PPNetworkGroup cbor.Rat
PPEconomicGroup cbor.Rat
PPTechnicalGroup cbor.Rat
PPGovGroup cbor.Rat
TreasureWithdrawal cbor.Rat
}
Loading

0 comments on commit ba0d25d

Please sign in to comment.