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
9 changes: 9 additions & 0 deletions ledger/chainsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,22 @@ func (ls *LedgerState) handleEventBlockfetch(evt event.Event) {
}

func (ls *LedgerState) handleEventChainsyncRollback(e ChainsyncEvent) error {
if ls.chainsyncState == SyncingChainsyncState {
ls.config.Logger.Warn(fmt.Sprintf("ledger: rolling back to %d.%s", e.Point.Slot, hex.EncodeToString(e.Point.Hash)))
ls.chainsyncState = RollbackChainsyncState
}
if err := ls.chain.Rollback(e.Point); err != nil {
return fmt.Errorf("chain rollback failed: %w", err)
}
return nil
}

func (ls *LedgerState) handleEventChainsyncBlockHeader(e ChainsyncEvent) error {
if ls.chainsyncState == RollbackChainsyncState {
ls.config.Logger.Info(fmt.Sprintf("ledger: switched to fork at %d.%s", e.Point.Slot, hex.EncodeToString(e.Point.Hash)))
ls.metrics.forks.Add(1)
}
ls.chainsyncState = SyncingChainsyncState
// Allow us to build up a few blockfetch batches worth of headers
allowedHeaderCount := blockfetchBatchSize * 4
headerCount := ls.chain.HeaderCount()
Expand Down
5 changes: 5 additions & 0 deletions ledger/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type stateMetrics struct {
epochNum prometheus.Gauge
slotInEpoch prometheus.Gauge
slotNum prometheus.Gauge
forks prometheus.Gauge
}

func (m *stateMetrics) init(promRegistry prometheus.Registerer) {
Expand All @@ -50,4 +51,8 @@ func (m *stateMetrics) init(promRegistry prometheus.Registerer) {
Name: "cardano_node_metrics_slotNum_int",
Help: "current slot number",
})
m.forks = promautoFactory.NewGauge(prometheus.GaugeOpts{
Name: "cardano_node_metrics_forks_int",
Help: "number of forks seen",
})
}
16 changes: 13 additions & 3 deletions ledger/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ const (
validateHistoricalThreshold = 14 * (24 * time.Hour) // 2 weeks
)

type ChainsyncState string

const (
InitChainsyncState ChainsyncState = "init"
RollbackChainsyncState ChainsyncState = "rollback"
SyncingChainsyncState ChainsyncState = "syncing"
)

type LedgerStateConfig struct {
Logger *slog.Logger
Database *database.Database
Expand All @@ -63,6 +71,7 @@ type BlockfetchRequestRangeFunc func(ouroboros.ConnectionId, ocommon.Point, ocom
type LedgerState struct {
sync.RWMutex
chainsyncMutex sync.Mutex
chainsyncState ChainsyncState
config LedgerStateConfig
db *database.Database
timerCleanupConsumedUtxos *time.Timer
Expand All @@ -84,9 +93,10 @@ type LedgerState struct {

func NewLedgerState(cfg LedgerStateConfig) (*LedgerState, error) {
ls := &LedgerState{
config: cfg,
db: cfg.Database,
chain: cfg.ChainManager.PrimaryChain(),
config: cfg,
chainsyncState: InitChainsyncState,
db: cfg.Database,
chain: cfg.ChainManager.PrimaryChain(),
}
if cfg.Logger == nil {
// Create logger to throw away logs
Expand Down