code.vegaprotocol.io/vega@v0.79.0/core/genesis/handler.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package genesis
    17  
    18  import (
    19  	"context"
    20  	"time"
    21  
    22  	"code.vegaprotocol.io/vega/logging"
    23  )
    24  
    25  type Handler struct {
    26  	log *logging.Logger
    27  	cfg Config
    28  
    29  	onGenesisTimeLoadedCB     []func(context.Context, time.Time)
    30  	onGenesisAppStateLoadedCB []func(context.Context, []byte) error
    31  }
    32  
    33  func New(log *logging.Logger, cfg Config) *Handler {
    34  	log = log.Named(namedLogger)
    35  	log.SetLevel(cfg.Level.Level)
    36  	return &Handler{
    37  		log:                       log,
    38  		cfg:                       cfg,
    39  		onGenesisTimeLoadedCB:     []func(context.Context, time.Time){},
    40  		onGenesisAppStateLoadedCB: []func(context.Context, []byte) error{},
    41  	}
    42  }
    43  
    44  // ReloadConf update the internal configuration of the positions engine.
    45  func (h *Handler) ReloadConf(cfg Config) {
    46  	h.log.Info("reloading configuration")
    47  	if h.log.GetLevel() != cfg.Level.Get() {
    48  		h.log.Info("updating log level",
    49  			logging.String("old", h.log.GetLevel().String()),
    50  			logging.String("new", cfg.Level.String()),
    51  		)
    52  		h.log.SetLevel(cfg.Level.Get())
    53  	}
    54  	h.cfg = cfg
    55  }
    56  
    57  func (h *Handler) OnGenesis(ctx context.Context, t time.Time, state []byte) error {
    58  	h.log.Debug("vega time at genesis",
    59  		logging.String("time", t.String()))
    60  	for _, f := range h.onGenesisTimeLoadedCB {
    61  		f(ctx, t)
    62  	}
    63  
    64  	h.log.Debug("vega initial state at genesis",
    65  		logging.String("state", string(state)))
    66  	for _, f := range h.onGenesisAppStateLoadedCB {
    67  		if err := f(ctx, state); err != nil {
    68  			return err
    69  		}
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  func (h *Handler) OnGenesisTimeLoaded(f ...func(context.Context, time.Time)) {
    76  	h.onGenesisTimeLoadedCB = append(h.onGenesisTimeLoadedCB, f...)
    77  }
    78  
    79  func (h *Handler) OnGenesisAppStateLoaded(f ...func(context.Context, []byte) error) {
    80  	h.onGenesisAppStateLoadedCB = append(h.onGenesisAppStateLoadedCB, f...)
    81  }