code.vegaprotocol.io/vega@v0.79.0/core/staking/staking.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 staking
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"math/big"
    22  
    23  	"code.vegaprotocol.io/vega/core/netparams"
    24  	"code.vegaprotocol.io/vega/core/types"
    25  	"code.vegaprotocol.io/vega/logging"
    26  
    27  	ethtypes "github.com/ethereum/go-ethereum/core/types"
    28  )
    29  
    30  //go:generate go run github.com/golang/mock/mockgen -destination mocks/mocks.go -package mocks code.vegaprotocol.io/vega/core/staking EvtForwarder,EthereumClientConfirmations,EthereumEventSource,TimeService,EthConfirmations,EthOnChainVerifier,Witness
    31  
    32  type AllEthereumClient interface {
    33  	EthereumClient
    34  	EthereumClientConfirmations
    35  	EthereumClientCaller
    36  }
    37  
    38  type EthereumClientConfirmations interface {
    39  	HeaderByNumber(context.Context, *big.Int) (*ethtypes.Header, error)
    40  }
    41  
    42  type EthereumEventSource interface {
    43  	UpdateContractBlock(string, string, uint64)
    44  }
    45  
    46  func New(
    47  	log *logging.Logger,
    48  	cfg Config,
    49  	ts TimeService,
    50  	broker Broker,
    51  	witness Witness,
    52  	ethClient AllEthereumClient,
    53  	netp *netparams.Store,
    54  	evtFwd EvtForwarder,
    55  	isValidator bool,
    56  	ethCfns EthConfirmations,
    57  	ethEventSource EthereumEventSource,
    58  ) (*Accounting, *StakeVerifier, *Checkpoint) {
    59  	log = log.Named(namedLogger)
    60  	log.SetLevel(cfg.Level.Get())
    61  	accs := NewAccounting(log, cfg, ts, broker, ethClient, evtFwd, witness, isValidator, ethEventSource)
    62  	ocv := NewOnChainVerifier(cfg, log, ethClient, ethCfns)
    63  	stakeV := NewStakeVerifier(log, cfg, accs, witness, ts, broker, ocv, ethEventSource)
    64  
    65  	_ = netp.Watch(netparams.WatchParam{
    66  		Param: netparams.BlockchainsPrimaryEthereumConfig,
    67  		Watcher: func(_ context.Context, cfg interface{}) error {
    68  			ethCfg, err := types.EthereumConfigFromUntypedProto(cfg)
    69  			if err != nil {
    70  				return fmt.Errorf("staking didn't receive a valid Ethereum configuration: %w", err)
    71  			}
    72  
    73  			ocv.UpdateStakingBridgeAddresses(ethCfg.StakingBridgeAddresses())
    74  
    75  			// We just need one of the staking bridges.
    76  			if err := accs.UpdateStakingBridgeAddress(ethCfg); err != nil {
    77  				return fmt.Errorf("couldn't update Ethereum configuration in accounting: %w", err)
    78  			}
    79  
    80  			return nil
    81  		},
    82  	})
    83  
    84  	return accs, stakeV, NewCheckpoint(log, accs, stakeV, ethEventSource)
    85  }