code.vegaprotocol.io/vega@v0.79.0/core/banking/bridge_stop_resume.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 banking
    17  
    18  import (
    19  	"context"
    20  
    21  	"code.vegaprotocol.io/vega/core/types"
    22  )
    23  
    24  func (e *Engine) BridgeStopped(_ context.Context, stopped bool, id string, block uint64, logIndex uint64, ethTxHash string, chainID string) error {
    25  	bridgeView, err := e.bridgeViewForChainID(chainID)
    26  	if err != nil {
    27  		return err
    28  	}
    29  
    30  	aa := &assetAction{
    31  		id:                 id,
    32  		state:              newPendingState(),
    33  		blockHeight:        block,
    34  		logIndex:           logIndex,
    35  		txHash:             ethTxHash,
    36  		chainID:            chainID,
    37  		erc20BridgeStopped: &types.ERC20EventBridgeStopped{BridgeStopped: stopped},
    38  		bridgeView:         bridgeView,
    39  	}
    40  
    41  	e.assetActions[aa.id] = aa
    42  	return e.witness.StartCheck(aa, e.onCheckDone, e.timeService.GetTimeNow().Add(defaultValidationDuration))
    43  }
    44  
    45  func (e *Engine) BridgeResumed(_ context.Context, resumed bool, id string, block uint64, logIndex uint64, ethTxHash string, chainID string) error {
    46  	bridgeView, err := e.bridgeViewForChainID(chainID)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	aa := &assetAction{
    52  		id:                 id,
    53  		state:              newPendingState(),
    54  		erc20BridgeResumed: &types.ERC20EventBridgeResumed{BridgeResumed: resumed},
    55  		blockHeight:        block,
    56  		logIndex:           logIndex,
    57  		txHash:             ethTxHash,
    58  		chainID:            chainID,
    59  		bridgeView:         bridgeView,
    60  	}
    61  	e.assetActions[aa.id] = aa
    62  	return e.witness.StartCheck(aa, e.onCheckDone, e.timeService.GetTimeNow().Add(defaultValidationDuration))
    63  }
    64  
    65  type bridgeState struct {
    66  	// is the operation suspended, or as usual
    67  	active bool
    68  	// last block + log index we received an update from the bridge
    69  	// this will be used later to verify no new state of the bridge is processed
    70  	// in a wrong order.
    71  	block, logIndex uint64
    72  }
    73  
    74  func (b *bridgeState) IsStopped() bool {
    75  	return !b.active
    76  }
    77  
    78  func (b *bridgeState) NewBridgeStopped(
    79  	block, logIndex uint64,
    80  ) {
    81  	if b.isNewerEvent(block, logIndex) {
    82  		b.active, b.block, b.logIndex = false, block, logIndex
    83  	}
    84  }
    85  
    86  func (b *bridgeState) NewBridgeResumed(
    87  	block, logIndex uint64,
    88  ) {
    89  	if b.isNewerEvent(block, logIndex) {
    90  		b.active, b.block, b.logIndex = true, block, logIndex
    91  	}
    92  }
    93  
    94  func (b *bridgeState) isNewerEvent(
    95  	block, logIndex uint64,
    96  ) bool {
    97  	if block == b.block {
    98  		return logIndex > b.logIndex
    99  	}
   100  	return block > b.block
   101  }