github.com/codingfuture/orig-energi3@v0.8.4/swarm/storage/feed/lookup/epoch.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package lookup 18 19 import ( 20 "encoding/binary" 21 "errors" 22 "fmt" 23 ) 24 25 // Epoch represents a time slot at a particular frequency level 26 type Epoch struct { 27 Time uint64 `json:"time"` // Time stores the time at which the update or lookup takes place 28 Level uint8 `json:"level"` // Level indicates the frequency level as the exponent of a power of 2 29 } 30 31 // EpochID is a unique identifier for an Epoch, based on its level and base time. 32 type EpochID [8]byte 33 34 // EpochLength stores the serialized binary length of an Epoch 35 const EpochLength = 8 36 37 // MaxTime contains the highest possible time value an Epoch can handle 38 const MaxTime uint64 = (1 << 56) - 1 39 40 // Base returns the base time of the Epoch 41 func (e *Epoch) Base() uint64 { 42 return getBaseTime(e.Time, e.Level) 43 } 44 45 // ID Returns the unique identifier of this epoch 46 func (e *Epoch) ID() EpochID { 47 base := e.Base() 48 var id EpochID 49 binary.LittleEndian.PutUint64(id[:], base) 50 id[7] = e.Level 51 return id 52 } 53 54 // MarshalBinary implements the encoding.BinaryMarshaller interface 55 func (e *Epoch) MarshalBinary() (data []byte, err error) { 56 b := make([]byte, 8) 57 binary.LittleEndian.PutUint64(b[:], e.Time) 58 b[7] = e.Level 59 return b, nil 60 } 61 62 // UnmarshalBinary implements the encoding.BinaryUnmarshaller interface 63 func (e *Epoch) UnmarshalBinary(data []byte) error { 64 if len(data) != EpochLength { 65 return errors.New("Invalid data unmarshalling Epoch") 66 } 67 b := make([]byte, 8) 68 copy(b, data) 69 e.Level = b[7] 70 b[7] = 0 71 e.Time = binary.LittleEndian.Uint64(b) 72 return nil 73 } 74 75 // After returns true if this epoch occurs later or exactly at the other epoch. 76 func (e *Epoch) After(epoch Epoch) bool { 77 if e.Time == epoch.Time { 78 return e.Level < epoch.Level 79 } 80 return e.Time >= epoch.Time 81 } 82 83 // Equals compares two epochs and returns true if they refer to the same time period. 84 func (e *Epoch) Equals(epoch Epoch) bool { 85 return e.Level == epoch.Level && e.Base() == epoch.Base() 86 } 87 88 // String implements the Stringer interface. 89 func (e *Epoch) String() string { 90 return fmt.Sprintf("Epoch{Time:%d, Level:%d}", e.Time, e.Level) 91 }