code.vegaprotocol.io/vega@v0.79.0/core/events/snapshot.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 events
    17  
    18  import (
    19  	"context"
    20  
    21  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    22  	"code.vegaprotocol.io/vega/version"
    23  )
    24  
    25  type SnapshotTakenEvent struct {
    26  	*Base
    27  	SnapshotBlockHeight  uint64
    28  	SnapshotBlockHash    string
    29  	VegaCoreVersion      string
    30  	ProtocolUpgradeBlock bool
    31  }
    32  
    33  func NewSnapshotEventEvent(ctx context.Context, blockHeight uint64, blockHash string, protocolUpgradeBlock bool) *SnapshotTakenEvent {
    34  	return &SnapshotTakenEvent{
    35  		Base:                 newBase(ctx, CoreSnapshotEvent),
    36  		SnapshotBlockHeight:  blockHeight,
    37  		SnapshotBlockHash:    blockHash,
    38  		VegaCoreVersion:      version.Get(),
    39  		ProtocolUpgradeBlock: protocolUpgradeBlock,
    40  	}
    41  }
    42  
    43  func (ste SnapshotTakenEvent) Proto() eventspb.CoreSnapshotData {
    44  	return eventspb.CoreSnapshotData{
    45  		BlockHeight:          ste.SnapshotBlockHeight,
    46  		BlockHash:            ste.SnapshotBlockHash,
    47  		CoreVersion:          ste.VegaCoreVersion,
    48  		ProtocolUpgradeBlock: ste.ProtocolUpgradeBlock,
    49  	}
    50  }
    51  
    52  func (ste SnapshotTakenEvent) SnapshotTakenEvent() eventspb.CoreSnapshotData {
    53  	return ste.Proto()
    54  }
    55  
    56  func (ste SnapshotTakenEvent) StreamMessage() *eventspb.BusEvent {
    57  	p := ste.Proto()
    58  	busEvent := newBusEventFromBase(ste.Base)
    59  	busEvent.Event = &eventspb.BusEvent_CoreSnapshotEvent{
    60  		CoreSnapshotEvent: &p,
    61  	}
    62  
    63  	return busEvent
    64  }
    65  
    66  func SnapthostTakenEventFromStream(ctx context.Context, be *eventspb.BusEvent) *SnapshotTakenEvent {
    67  	event := be.GetCoreSnapshotEvent()
    68  	if event == nil {
    69  		return nil
    70  	}
    71  
    72  	return &SnapshotTakenEvent{
    73  		Base:                 newBaseFromBusEvent(ctx, CoreSnapshotEvent, be),
    74  		SnapshotBlockHeight:  event.BlockHeight,
    75  		SnapshotBlockHash:    event.BlockHash,
    76  		VegaCoreVersion:      event.CoreVersion,
    77  		ProtocolUpgradeBlock: event.ProtocolUpgradeBlock,
    78  	}
    79  }