code.vegaprotocol.io/vega@v0.79.0/datanode/entities/snapshot_data.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 entities
    17  
    18  import (
    19  	"encoding/json"
    20  	"fmt"
    21  	"time"
    22  
    23  	v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
    24  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    25  )
    26  
    27  type CoreSnapshotData struct {
    28  	BlockHeight     uint64
    29  	BlockHash       string
    30  	VegaCoreVersion string
    31  	TxHash          TxHash
    32  	VegaTime        time.Time
    33  }
    34  
    35  func CoreSnapshotDataFromProto(s *eventspb.CoreSnapshotData, txHash TxHash, vegaTime time.Time) CoreSnapshotData {
    36  	return CoreSnapshotData{
    37  		BlockHeight:     s.BlockHeight,
    38  		BlockHash:       s.BlockHash,
    39  		VegaCoreVersion: s.CoreVersion,
    40  		TxHash:          txHash,
    41  		VegaTime:        vegaTime,
    42  	}
    43  }
    44  
    45  func (s *CoreSnapshotData) ToProto() *eventspb.CoreSnapshotData {
    46  	return &eventspb.CoreSnapshotData{
    47  		BlockHeight: s.BlockHeight,
    48  		BlockHash:   s.BlockHash,
    49  		CoreVersion: s.VegaCoreVersion,
    50  	}
    51  }
    52  
    53  func (s CoreSnapshotData) Cursor() *Cursor {
    54  	pc := CoreSnapshotDataCursor{
    55  		VegaTime:        s.VegaTime,
    56  		BlockHeight:     s.BlockHeight,
    57  		BlockHash:       s.BlockHash,
    58  		VegaCoreVersion: s.VegaCoreVersion,
    59  	}
    60  	return NewCursor(pc.String())
    61  }
    62  
    63  func (s CoreSnapshotData) ToProtoEdge(_ ...any) (*v2.CoreSnapshotEdge, error) {
    64  	return &v2.CoreSnapshotEdge{
    65  		Node:   s.ToProto(),
    66  		Cursor: s.Cursor().Encode(),
    67  	}, nil
    68  }
    69  
    70  type CoreSnapshotDataCursor struct {
    71  	VegaTime        time.Time
    72  	BlockHeight     uint64
    73  	BlockHash       string
    74  	VegaCoreVersion string
    75  }
    76  
    77  func (sc CoreSnapshotDataCursor) String() string {
    78  	bs, err := json.Marshal(sc)
    79  	if err != nil {
    80  		panic(fmt.Errorf("failed to marshal core snapshot data cursor: %w", err))
    81  	}
    82  	return string(bs)
    83  }
    84  
    85  func (sc *CoreSnapshotDataCursor) Parse(cursorString string) error {
    86  	if cursorString == "" {
    87  		return nil
    88  	}
    89  	return json.Unmarshal([]byte(cursorString), sc)
    90  }