code.vegaprotocol.io/vega@v0.79.0/core/types/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  //lint:file-ignore ST1003 Ignore underscores in names, this is straigh copied from the proto package to ease introducing the domain types
    17  
    18  package types
    19  
    20  import (
    21  	"fmt"
    22  	"time"
    23  
    24  	proto "code.vegaprotocol.io/vega/protos/vega"
    25  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    26  )
    27  
    28  type Epoch struct {
    29  	// Unique identifier that increases by one each epoch
    30  	Seq uint64
    31  	// What time did this epoch start
    32  	StartTime time.Time
    33  	// What time should this epoch end
    34  	ExpireTime time.Time
    35  	// What time did it actually end
    36  	EndTime time.Time
    37  	// What action took place
    38  	Action proto.EpochAction
    39  }
    40  
    41  func (e Epoch) String() string {
    42  	str := fmt.Sprintf(
    43  		"seq(%d) action(%s) startTime(%s) expireTime(%s)",
    44  		e.Seq,
    45  		e.Action.String(),
    46  		e.StartTime,
    47  		e.ExpireTime,
    48  	)
    49  
    50  	// End time is only defined when the epoch event is an end "action".
    51  	if e.Action == proto.EpochAction_EPOCH_ACTION_END {
    52  		str = fmt.Sprintf("%s endTime(%s)", str, e.EndTime)
    53  	}
    54  
    55  	return str
    56  }
    57  
    58  func NewEpochFromProto(p *eventspb.EpochEvent) *Epoch {
    59  	e := &Epoch{
    60  		Seq:        p.Seq,
    61  		StartTime:  time.Unix(0, p.StartTime),
    62  		ExpireTime: time.Unix(0, p.ExpireTime),
    63  		EndTime:    time.Unix(0, p.EndTime),
    64  		Action:     p.Action,
    65  	}
    66  	return e
    67  }
    68  
    69  func (e Epoch) IntoProto() *eventspb.EpochEvent {
    70  	eProto := &eventspb.EpochEvent{
    71  		Seq:        e.Seq,
    72  		StartTime:  e.StartTime.UnixNano(),
    73  		ExpireTime: e.ExpireTime.UnixNano(),
    74  		Action:     e.Action,
    75  	}
    76  
    77  	// End time is only defined when the epoch event is an end "action".
    78  	if e.Action == proto.EpochAction_EPOCH_ACTION_END {
    79  		eProto.EndTime = e.EndTime.UnixNano()
    80  	}
    81  
    82  	return eProto
    83  }