From 7f754fa23175275bc356db6dd1b6bb7be7271cde Mon Sep 17 00:00:00 2001 From: Chris Gianelloni Date: Thu, 27 Mar 2025 11:06:47 -0400 Subject: [PATCH] refactor(database): pparams getter/setter Signed-off-by: Chris Gianelloni --- database/epoch.go | 10 ++++++- database/pparams.go | 72 +++++++++++++++++++++++++++++++++++++++++++++ database/utxo.go | 9 +++++- state/chainsync.go | 6 +++- state/state.go | 27 +++++++---------- 5 files changed, 104 insertions(+), 20 deletions(-) create mode 100644 database/pparams.go diff --git a/database/epoch.go b/database/epoch.go index e259d67a..8ce2e1f3 100644 --- a/database/epoch.go +++ b/database/epoch.go @@ -77,7 +77,15 @@ func (d *Database) SetEpoch( txn *Txn, ) error { if txn == nil { - err := d.metadata.SetEpoch(slot, epoch, nonce, era, slotLength, lengthInSlots, nil) + err := d.metadata.SetEpoch( + slot, + epoch, + nonce, + era, + slotLength, + lengthInSlots, + nil, + ) if err != nil { return err } diff --git a/database/pparams.go b/database/pparams.go new file mode 100644 index 00000000..323c5d65 --- /dev/null +++ b/database/pparams.go @@ -0,0 +1,72 @@ +// Copyright 2025 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 database + +import ( + lcommon "github.com/blinklabs-io/gouroboros/ledger/common" +) + +func (d *Database) GetPParams( + epoch uint64, + decodeFunc func([]byte) (lcommon.ProtocolParameters, error), + txn *Txn, +) (lcommon.ProtocolParameters, error) { + var ret lcommon.ProtocolParameters + var err error + if txn == nil { + pparams, ppErr := d.metadata.GetPParams(epoch, nil) + if err != nil { + return ret, ppErr + } + if len(pparams) == 0 { + return ret, nil + } + // pparams is ordered, so grab the first + tmpPParams := pparams[0] + ret, err = decodeFunc(tmpPParams.Cbor) + } else { + pparams, ppErr := d.metadata.GetPParams(epoch, txn.Metadata()) + if err != nil { + return ret, ppErr + } + if len(pparams) == 0 { + return ret, nil + } + // pparams is ordered, so grab the first + tmpPParams := pparams[0] + ret, err = decodeFunc(tmpPParams.Cbor) + } + return ret, err +} + +func (d *Database) SetPParams( + params []byte, + slot, epoch uint64, + era uint, + txn *Txn, +) error { + if txn == nil { + err := d.metadata.SetPParams(params, slot, epoch, era, nil) + if err != nil { + return err + } + } else { + err := d.metadata.SetPParams(params, slot, epoch, era, txn.Metadata()) + if err != nil { + return err + } + } + return nil +} diff --git a/database/utxo.go b/database/utxo.go index 80bb19ce..f26b358d 100644 --- a/database/utxo.go +++ b/database/utxo.go @@ -75,7 +75,14 @@ func (d *Database) NewUtxo( if err != nil { return err } - return d.metadata.SetUtxo(txId, outputIdx, slot, paymentKey, stakeKey, txn.Metadata()) + return d.metadata.SetUtxo( + txId, + outputIdx, + slot, + paymentKey, + stakeKey, + txn.Metadata(), + ) } func (d *Database) UtxoByRef( diff --git a/state/chainsync.go b/state/chainsync.go index 84aa949c..ab97b437 100644 --- a/state/chainsync.go +++ b/state/chainsync.go @@ -144,7 +144,11 @@ func (ls *LedgerState) handleEventChainsyncBlockHeader(e ChainsyncEvent) error { ls.chainsyncBlockfetchBusy = false ls.chainsyncBlockfetchWaiting = false // Reset buffer - ls.chainsyncBlockEvents = slices.Delete(ls.chainsyncBlockEvents, 0, len(ls.chainsyncBlockEvents)) + ls.chainsyncBlockEvents = slices.Delete( + ls.chainsyncBlockEvents, + 0, + len(ls.chainsyncBlockEvents), + ) ls.config.Logger.Warn( fmt.Sprintf( "blockfetch operation timed out after %s", diff --git a/state/state.go b/state/state.go index eef2f76f..fed0da63 100644 --- a/state/state.go +++ b/state/state.go @@ -327,12 +327,12 @@ func (ls *LedgerState) transitionToEra( if err != nil { return err } - err = txn.DB().Metadata().SetPParams( + err = txn.DB().SetPParams( pparamsCbor, addedSlot, startEpoch, nextEraId, - txn.Metadata(), + txn, ) if err != nil { return err @@ -379,17 +379,17 @@ func (ls *LedgerState) applyPParamUpdates( "pparams", fmt.Sprintf("%#v", ls.currentPParams), ) + // Write pparams update to DB pparamsCbor, err := cbor.Encode(&ls.currentPParams) if err != nil { return err } - // Write pparams update to DB - err = txn.DB().Metadata().SetPParams( + err = txn.DB().SetPParams( pparamsCbor, addedSlot, uint64(currentEpoch+1), ls.currentEra.Id, - txn.Metadata(), + txn, ) if err != nil { return err @@ -447,22 +447,15 @@ func (ls *LedgerState) removeBlock( } func (ls *LedgerState) loadPParams() error { - pparams, err := ls.db.Metadata().GetPParams(ls.currentEpoch.EpochId, nil) - if err != nil { - return err - } - if len(pparams) == 0 { - return nil - } - // pparams is ordered, so grab the first - tmpPParams := pparams[0] - currentPParams, err := ls.currentEra.DecodePParamsFunc( - tmpPParams.Cbor, + pparams, err := ls.db.GetPParams( + ls.currentEpoch.EpochId, + ls.currentEra.DecodePParamsFunc, + nil, ) if err != nil { return err } - ls.currentPParams = currentPParams + ls.currentPParams = pparams return nil }