code.vegaprotocol.io/vega@v0.79.0/core/events/checkpoint.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  	"encoding/hex"
    21  
    22  	"code.vegaprotocol.io/vega/core/types"
    23  	vgcontext "code.vegaprotocol.io/vega/libs/context"
    24  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    25  )
    26  
    27  type Checkpoint struct {
    28  	*Base
    29  	data eventspb.CheckpointEvent
    30  }
    31  
    32  func NewCheckpointEvent(ctx context.Context, snap *types.CheckpointState) *Checkpoint {
    33  	height, _ := vgcontext.BlockHeightFromContext(ctx)
    34  	_, block := vgcontext.TraceIDFromContext(ctx)
    35  	return &Checkpoint{
    36  		Base: newBase(ctx, CheckpointEvent),
    37  		data: eventspb.CheckpointEvent{
    38  			Hash:        hex.EncodeToString(snap.Hash),
    39  			BlockHash:   block,
    40  			BlockHeight: height,
    41  		},
    42  	}
    43  }
    44  
    45  func (e Checkpoint) Proto() eventspb.CheckpointEvent {
    46  	return e.data
    47  }
    48  
    49  func (e Checkpoint) StreamMessage() *eventspb.BusEvent {
    50  	busEvent := newBusEventFromBase(e.Base)
    51  	busEvent.Event = &eventspb.BusEvent_Checkpoint{
    52  		Checkpoint: &e.data,
    53  	}
    54  	return busEvent
    55  }
    56  
    57  func CheckpointEventFromStream(ctx context.Context, be *eventspb.BusEvent) *Checkpoint {
    58  	if event := be.GetCheckpoint(); event != nil {
    59  		return &Checkpoint{
    60  			Base: newBaseFromBusEvent(ctx, CheckpointEvent, be),
    61  			data: *event,
    62  		}
    63  	}
    64  	return nil
    65  }