github.com/braveheart12/just@v0.8.7/core/pulse.go (about) 1 /* 2 * Copyright 2019 Insolar Technologies 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package core 18 19 import ( 20 "encoding/binary" 21 "fmt" 22 "time" 23 24 "github.com/insolar/insolar/core/utils" 25 ) 26 27 const ( 28 // PulseNumberSize declares the number of bytes in the pulse number 29 PulseNumberSize = 4 30 // EntropySize declares the number of bytes in the pulse entropy 31 EntropySize = 64 32 // OriginIDSize declares the number of bytes in the origin id 33 OriginIDSize = 16 34 ) 35 36 // Entropy is 64 random bytes used in every pseudo-random calculations. 37 type Entropy [EntropySize]byte 38 39 // PulseNumber is a sequential number of Pulse. 40 // Upper 2 bits are reserved for use in references (scope), must be zero otherwise. 41 // Valid Absolute PulseNumber must be >65536. 42 // If PulseNumber <65536 it is a relative PulseNumber 43 type PulseNumber uint32 44 45 // NewPulseNumber creates pulse number from bytes. 46 func NewPulseNumber(buf []byte) PulseNumber { 47 return PulseNumber(binary.BigEndian.Uint32(buf)) 48 } 49 50 // Bytes serializes pulse number. 51 func (pn PulseNumber) Bytes() []byte { 52 return utils.UInt32ToBytes(uint32(pn)) 53 } 54 55 // PulseRange represents range of pulses. 56 type PulseRange struct { 57 Begin PulseNumber 58 End PulseNumber 59 } 60 61 func (pr *PulseRange) String() string { 62 return fmt.Sprintf("[%v:%v]", pr.Begin, pr.End) 63 } 64 65 // Pulse is base data structure for a pulse. 66 type Pulse struct { 67 PulseNumber PulseNumber 68 PrevPulseNumber PulseNumber 69 NextPulseNumber PulseNumber 70 71 PulseTimestamp int64 72 EpochPulseNumber int 73 OriginID [OriginIDSize]byte 74 75 Entropy Entropy 76 Signs map[string]PulseSenderConfirmation 77 } 78 79 // PulseSenderConfirmation contains confirmations of the pulse from other pulsars 80 // Because the system is using BFT for consensus between pulsars, because of it 81 // All pulsar send to the chosen pulsar their confirmations 82 // Every node in the network can verify the signatures 83 type PulseSenderConfirmation struct { 84 PulseNumber PulseNumber 85 ChosenPublicKey string 86 Entropy Entropy 87 Signature []byte 88 } 89 90 // FirstPulseDate is the hardcoded date of the first pulse 91 const firstPulseDate = 1535760000 //09/01/2018 @ 12:00am (UTC) 92 93 const ( 94 // FirstPulseNumber is the hardcoded first pulse number. Because first 65536 numbers are saved for the system's needs 95 FirstPulseNumber = 65537 96 // PulseNumberJet is a special pulse number value that signifies jet ID. 97 PulseNumberJet = PulseNumber(1) 98 // PulseNumberCurrent is a special pulse number value that signifies current pulse number. 99 PulseNumberCurrent = PulseNumber(2) 100 ) 101 102 // GenesisPulse is a first pulse for the system 103 // because first 2 bits of pulse number and first 65536 pulses a are used by system needs and pulse numbers are related to the seconds of Unix time 104 // for calculation pulse numbers we use the formula = unix.Now() - firstPulseDate + 65536 105 var GenesisPulse = &Pulse{ 106 PulseNumber: FirstPulseNumber, 107 Entropy: [EntropySize]byte{}, 108 EpochPulseNumber: 1, 109 PulseTimestamp: firstPulseDate, 110 } 111 112 // CalculatePulseNumber is helper for calculating next pulse number, when a network is being started 113 func CalculatePulseNumber(now time.Time) PulseNumber { 114 return PulseNumber(now.Unix() - firstPulseDate + FirstPulseNumber) 115 }