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
44 changes: 44 additions & 0 deletions config/cardano/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,61 @@ func (c *CardanoNodeConfig) ByronGenesis() *byron.ByronGenesis {
return c.byronGenesis
}

// LoadByronGenesisFromReader loads a Byron genesis config from an io.Reader
// This is useful mostly for tests
func (c *CardanoNodeConfig) LoadByronGenesisFromReader(r io.Reader) error {
byronGenesis, err := byron.NewByronGenesisFromReader(r)
if err != nil {
return err
}
c.byronGenesis = &byronGenesis
return nil
}

// ShelleyGenesis returns the Shelley genesis config specified in the cardano-node config
func (c *CardanoNodeConfig) ShelleyGenesis() *shelley.ShelleyGenesis {
return c.shelleyGenesis
}

// LoadShelleyGenesisFromReader loads a Shelley genesis config from an io.Reader
// This is useful mostly for tests
func (c *CardanoNodeConfig) LoadShelleyGenesisFromReader(r io.Reader) error {
shelleyGenesis, err := shelley.NewShelleyGenesisFromReader(r)
if err != nil {
return err
}
c.shelleyGenesis = &shelleyGenesis
return nil
}

// AlonzoGenesis returns the Alonzo genesis config specified in the cardano-node config
func (c *CardanoNodeConfig) AlonzoGenesis() *alonzo.AlonzoGenesis {
return c.alonzoGenesis
}

// LoadAlonzoGenesisFromReader loads a Alonzo genesis config from an io.Reader
// This is useful mostly for tests
func (c *CardanoNodeConfig) LoadAlonzoGenesisFromReader(r io.Reader) error {
alonzoGenesis, err := alonzo.NewAlonzoGenesisFromReader(r)
if err != nil {
return err
}
c.alonzoGenesis = &alonzoGenesis
return nil
}

// ConwayGenesis returns the Conway genesis config specified in the cardano-node config
func (c *CardanoNodeConfig) ConwayGenesis() *conway.ConwayGenesis {
return c.conwayGenesis
}

// LoadConwayGenesisFromReader loads a Conway genesis config from an io.Reader
// This is useful mostly for tests
func (c *CardanoNodeConfig) LoadConwayGenesisFromReader(r io.Reader) error {
conwayGenesis, err := conway.NewConwayGenesisFromReader(r)
if err != nil {
return err
}
c.conwayGenesis = &conwayGenesis
return nil
}
22 changes: 22 additions & 0 deletions database/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ func (d *Database) GetEpochsByEra(eraId uint, txn *Txn) ([]Epoch, error) {
return tmpEpochs, nil
}

func (d *Database) GetEpochs(txn *Txn) ([]Epoch, error) {
tmpEpochs := []Epoch{}
if txn == nil {
epochs, err := d.metadata.GetEpochs(nil)
if err != nil {
return tmpEpochs, err
}
for _, epoch := range epochs {
tmpEpochs = append(tmpEpochs, Epoch(epoch))
}
} else {
epochs, err := txn.db.metadata.GetEpochs(txn.Metadata())
if err != nil {
return tmpEpochs, err
}
for _, epoch := range epochs {
tmpEpochs = append(tmpEpochs, Epoch(epoch))
}
}
return tmpEpochs, nil
}

func (d *Database) SetEpoch(
slot, epoch uint64,
nonce []byte,
Expand Down
19 changes: 19 additions & 0 deletions database/plugin/metadata/sqlite/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ func (d *MetadataStoreSqlite) GetEpochsByEra(
return ret, nil
}

// GetEpochs returns the list of epochs
func (d *MetadataStoreSqlite) GetEpochs(
txn *gorm.DB,
) ([]models.Epoch, error) {
ret := []models.Epoch{}
if txn != nil {
result := txn.Order("epoch_id").Find(&ret)
if result.Error != nil {
return ret, result.Error
}
} else {
result := d.DB().Order("epoch_id").Find(&ret)
if result.Error != nil {
return ret, result.Error
}
}
return ret, nil
}

// SetEpoch saves an epoch
func (d *MetadataStoreSqlite) SetEpoch(
slot, epoch uint64,
Expand Down
1 change: 1 addition & 0 deletions database/plugin/metadata/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ type MetadataStore interface {
DeleteUtxosBeforeSlot(uint64, *gorm.DB) error
GetEpochLatest(*gorm.DB) (models.Epoch, error)
GetEpochsByEra(uint, *gorm.DB) ([]models.Epoch, error)
GetEpochs(*gorm.DB) ([]models.Epoch, error)
GetUtxosAddedAfterSlot(uint64, *gorm.DB) ([]models.Utxo, error)
GetUtxosByAddress(ledger.Address, *gorm.DB) ([]models.Utxo, error)
GetUtxosDeletedBeforeSlot(uint64, *gorm.DB) ([]models.Utxo, error)
Expand Down
16 changes: 6 additions & 10 deletions ledger/chainsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,15 +337,13 @@ func (ls *LedgerState) processEpochRollover(
if err != nil {
return err
}
newEpoch, err := ls.db.GetEpochLatest(txn)
if err != nil {
// Reload epoch info
if err := ls.loadEpochs(txn); err != nil {
return err
}
ls.currentEpoch = newEpoch
ls.metrics.epochNum.Set(float64(newEpoch.EpochId))
ls.config.Logger.Debug(
"added initial epoch to DB",
"epoch", fmt.Sprintf("%+v", newEpoch),
"epoch", fmt.Sprintf("%+v", ls.currentEpoch),
"component", "ledger",
)
}
Expand Down Expand Up @@ -392,15 +390,13 @@ func (ls *LedgerState) processEpochRollover(
if err != nil {
return err
}
newEpoch, err := ls.db.GetEpochLatest(txn)
if err != nil {
// Reload epoch info
if err := ls.loadEpochs(txn); err != nil {
return err
}
ls.currentEpoch = newEpoch
ls.metrics.epochNum.Set(float64(newEpoch.EpochId))
ls.config.Logger.Debug(
"added next epoch to DB",
"epoch", fmt.Sprintf("%+v", newEpoch),
"epoch", fmt.Sprintf("%+v", ls.currentEpoch),
"component", "ledger",
)
}
Expand Down
118 changes: 118 additions & 0 deletions ledger/slot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// 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 ledger

import (
"errors"
"math"
"time"

"github.com/blinklabs-io/dingo/database"
)

// SlotToTime returns the current time for a given slot based on known epochs
func (ls *LedgerState) SlotToTime(slot uint64) (time.Time, error) {
if slot > math.MaxInt64 {
return time.Time{}, errors.New("slot is larger than time.Duration")
}
shelleyGenesis := ls.config.CardanoNodeConfig.ShelleyGenesis()
if shelleyGenesis == nil {
return time.Time{}, errors.New("could not get genesis config")
}
slotTime := shelleyGenesis.SystemStart
// Special case for chain genesis
if slot == 0 {
return slotTime, nil
}
foundSlot := false
for _, epoch := range ls.epochCache {
if epoch.StartSlot > math.MaxInt64 ||
epoch.LengthInSlots > math.MaxInt64 ||
epoch.SlotLength > math.MaxInt64 {
return time.Time{}, errors.New("epoch slot values are larger than time.Duration")
}
if slot < epoch.StartSlot+uint64(epoch.LengthInSlots) {
slotTime = slotTime.Add(
time.Duration(int64(slot)-int64(epoch.StartSlot)) * (time.Duration(epoch.SlotLength) * time.Millisecond),
)
foundSlot = true
break
}
slotTime = slotTime.Add(
time.Duration(epoch.LengthInSlots) * (time.Duration(epoch.SlotLength) * time.Millisecond),
)
}
if !foundSlot {
return slotTime, errors.New("slot not found in known epochs")
}
return slotTime, nil
}

// TimeToSlot returns the slot number for a given time based on known epochs
func (ls *LedgerState) TimeToSlot(t time.Time) (uint64, error) {
shelleyGenesis := ls.config.CardanoNodeConfig.ShelleyGenesis()
if shelleyGenesis == nil {
return 0, errors.New("could not get genesis config")
}
epochStartTime := shelleyGenesis.SystemStart
// Special case for chain genesis
if t.Equal(epochStartTime) {
return 0, nil
}
var timeSlot uint64
foundTime := false
for _, epoch := range ls.epochCache {
if epoch.LengthInSlots > math.MaxInt64 ||
epoch.SlotLength > math.MaxInt64 {
return 0, errors.New("epoch slot values are larger than time.Duration")
}
slotDuration := time.Duration(epoch.SlotLength) * time.Millisecond
if slotDuration < 0 {
return 0, errors.New("slot duration is negative")
}
epochEndTime := epochStartTime.Add(
time.Duration(epoch.LengthInSlots) * slotDuration,
)
if (t.Equal(epochStartTime) || t.After(epochStartTime)) && t.Before(epochEndTime) {
// Figure out how far into the epoch the specified time is
timeDiff := t.Sub(epochStartTime)
// nolint:gosec
// This will never overflow using 2 positive int64 values, but gosec seems determined
// to complain about it
timeSlot += uint64(timeDiff / slotDuration)
foundTime = true
break
}
epochStartTime = epochEndTime
timeSlot += uint64(epoch.LengthInSlots)
}
if !foundTime {
return timeSlot, errors.New("time not found in known epochs")
}
return timeSlot, nil
}

// SlotToEpoch returns a known epoch by slot number
func (ls *LedgerState) SlotToEpoch(slot uint64) (database.Epoch, error) {
for _, epoch := range ls.epochCache {
if slot < epoch.StartSlot {
continue
}
if slot < epoch.StartSlot+uint64(epoch.LengthInSlots) {
return epoch, nil
}
}
return database.Epoch{}, errors.New("slot not found in known epochs")
}
Loading
Loading