code.vegaprotocol.io/vega@v0.79.0/datanode/entities/epoch.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 "time" 20 21 "code.vegaprotocol.io/vega/protos/vega" 22 eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" 23 ) 24 25 type Epoch struct { 26 ID int64 27 StartTime time.Time 28 ExpireTime time.Time 29 EndTime *time.Time 30 TxHash TxHash 31 VegaTime time.Time 32 FirstBlock *int64 33 LastBlock *int64 34 } 35 36 func (e *Epoch) ToProto() *vega.Epoch { 37 protoEpoch := vega.Epoch{ 38 Seq: uint64(e.ID), 39 Timestamps: &vega.EpochTimestamps{ 40 StartTime: e.StartTime.UnixNano(), 41 ExpiryTime: e.ExpireTime.UnixNano(), 42 }, 43 } 44 if e.EndTime != nil { 45 protoEpoch.Timestamps.EndTime = e.EndTime.UnixNano() 46 } 47 if e.FirstBlock != nil { 48 protoEpoch.Timestamps.FirstBlock = uint64(*e.FirstBlock) 49 } 50 if e.LastBlock != nil { 51 protoEpoch.Timestamps.LastBlock = uint64(*e.LastBlock) 52 } 53 return &protoEpoch 54 } 55 56 func EpochFromProto(ee eventspb.EpochEvent, txHash TxHash) Epoch { 57 var endTime *time.Time 58 if ee.Action == vega.EpochAction_EPOCH_ACTION_END { 59 t := NanosToPostgresTimestamp(ee.EndTime) 60 endTime = &t 61 } 62 epoch := Epoch{ 63 ID: int64(ee.Seq), 64 StartTime: NanosToPostgresTimestamp(ee.StartTime), 65 ExpireTime: NanosToPostgresTimestamp(ee.ExpireTime), 66 EndTime: endTime, 67 TxHash: txHash, 68 } 69 return epoch 70 }