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
73 changes: 73 additions & 0 deletions database/pparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package database

import (
"fmt"

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

Expand Down Expand Up @@ -70,3 +73,73 @@ func (d *Database) SetPParams(
}
return nil
}

func (d *Database) ApplyPParamUpdates(
slot, epoch uint64,
era uint,
currentPParams *lcommon.ProtocolParameters,
decodeFunc func([]byte) (any, error),
updateFunc func(lcommon.ProtocolParameters, any) (lcommon.ProtocolParameters, error),
txn *Txn,
) error {
// Check for pparam updates that apply at the end of the epoch
pparamUpdates, err := d.metadata.GetPParamUpdates(epoch, txn.Metadata())
if err != nil {
return err
}
if len(pparamUpdates) == 0 {
// nothing to do
return nil
}
// We only want the latest for the epoch
pparamUpdate := pparamUpdates[0]
tmpPParamUpdate, err := decodeFunc(pparamUpdate.Cbor)
if err != nil {
return err
}
// Update current pparams
newPParams, err := updateFunc(
*currentPParams,
tmpPParamUpdate,
)
if err != nil {
return err
}
*currentPParams = newPParams
d.logger.Debug(
"updated protocol params",
"pparams",
fmt.Sprintf("%#v", currentPParams),
)
// Write pparams update to DB
pparamsCbor, err := cbor.Encode(&currentPParams)
if err != nil {
return err
}
return d.metadata.SetPParams(
pparamsCbor,
slot,
uint64(epoch+1),
era,
txn.Metadata(),
)
}

func (d *Database) SetPParamUpdate(
genesis, params []byte,
slot, epoch uint64,
txn *Txn,
) error {
if txn == nil {
err := d.metadata.SetPParamUpdate(genesis, params, slot, epoch, nil)
if err != nil {
return err
}
} else {
err := d.metadata.SetPParamUpdate(genesis, params, slot, epoch, txn.Metadata())
if err != nil {
return err
}
}
return nil
}
15 changes: 12 additions & 3 deletions state/chainsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,16 @@ func (ls *LedgerState) processEpochRollover(
ls.currentEpoch.LengthInSlots,
) {
// Apply pending pparam updates
if err := ls.applyPParamUpdates(txn, ls.currentEpoch.EpochId, point.Slot); err != nil {
err := ls.db.ApplyPParamUpdates(
point.Slot,
ls.currentEpoch.EpochId,
ls.currentEra.Id,
&ls.currentPParams,
ls.currentEra.DecodePParamsUpdateFunc,
ls.currentEra.PParamsUpdateFunc,
txn,
)
if err != nil {
return err
}
// Create next epoch record
Expand Down Expand Up @@ -600,12 +609,12 @@ func (ls *LedgerState) processTransaction(
// Protocol parameter updates
if updateEpoch, paramUpdates := tx.ProtocolParameterUpdates(); updateEpoch > 0 {
for genesisHash, update := range paramUpdates {
err := ls.db.Metadata().SetPParamUpdate(
err := ls.db.SetPParamUpdate(
genesisHash.Bytes(),
update.Cbor(),
point.Slot,
updateEpoch,
txn.Metadata(),
txn,
)
if err != nil {
return err
Expand Down
58 changes: 0 additions & 58 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,64 +337,6 @@ func (ls *LedgerState) transitionToEra(
return nil
}

func (ls *LedgerState) applyPParamUpdates(
txn *database.Txn,
currentEpoch uint64,
addedSlot uint64,
) error {
// Check for pparam updates that apply at the end of the epoch
pparamUpdates, err := ls.db.
Metadata().
GetPParamUpdates(currentEpoch, txn.Metadata())
if err != nil {
return err
}
if len(pparamUpdates) > 0 {
// We only want the latest for the epoch
pparamUpdate := pparamUpdates[0]
if ls.currentEra.DecodePParamsUpdateFunc != nil {
tmpPParamUpdate, err := ls.currentEra.DecodePParamsUpdateFunc(
pparamUpdate.Cbor,
)
if err != nil {
return err
}
if ls.currentEra.PParamsUpdateFunc != nil {
// Update current pparams
newPParams, err := ls.currentEra.PParamsUpdateFunc(
ls.currentPParams,
tmpPParamUpdate,
)
if err != nil {
return err
}
ls.currentPParams = newPParams
ls.config.Logger.Debug(
"updated protocol params",
"pparams",
fmt.Sprintf("%#v", ls.currentPParams),
)
// Write pparams update to DB
pparamsCbor, err := cbor.Encode(&ls.currentPParams)
if err != nil {
return err
}
err = ls.db.SetPParams(
pparamsCbor,
addedSlot,
uint64(currentEpoch+1),
ls.currentEra.Id,
txn,
)
if err != nil {
return err
}
}
}
}
return nil
}

// consumeUtxo marks a UTxO as "deleted" without actually deleting it. This allows for a UTxO
// to be easily on rollback
func (ls *LedgerState) consumeUtxo(
Expand Down
Loading