/
block_time_calculator.go
130 lines (104 loc) · 3.44 KB
/
block_time_calculator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (C) 2017, 2018, 2019 EGAAS S.A.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
package utils
import (
"time"
"github.com/pkg/errors"
)
// BlockTimeCalculator calculating block generation time
type BlockTimeCalculator struct {
clock Clock
blocksCounter intervalBlocksCounter
firstBlockTime time.Time
blockGenerationTime time.Duration
blocksGap time.Duration
nodesCount int64
}
type blockGenerationState struct {
start time.Time
duration time.Duration
nodePosition int64
}
var TimeError = errors.New("current time before first block")
var DuplicateBlockError = errors.New("block with that time interval already exists in db")
func NewBlockTimeCalculator(firstBlockTime time.Time, generationTime, blocksGap time.Duration, nodesCount int64) BlockTimeCalculator {
return BlockTimeCalculator{
clock: &ClockWrapper{},
blocksCounter: &blocksCounter{},
firstBlockTime: firstBlockTime,
blockGenerationTime: generationTime,
blocksGap: blocksGap,
nodesCount: nodesCount,
}
}
func (btc *BlockTimeCalculator) TimeToGenerate(nodePosition int64) (bool, error) {
bgs, err := btc.countBlockTime(btc.clock.Now())
if err != nil {
return false, err
}
blocks, err := btc.blocksCounter.count(bgs)
if err != nil {
return false, err
}
if blocks != 0 {
return false, DuplicateBlockError
}
return bgs.nodePosition == nodePosition, nil
}
func (btc *BlockTimeCalculator) ValidateBlock(nodePosition int64, at time.Time) (bool, error) {
bgs, err := btc.countBlockTime(at)
if err != nil {
return false, err
}
blocks, err := btc.blocksCounter.count(bgs)
if err != nil {
return false, err
}
if blocks != 0 {
return false, DuplicateBlockError
}
return bgs.nodePosition == nodePosition, nil
}
func (btc *BlockTimeCalculator) SetClock(clock Clock) *BlockTimeCalculator {
btc.clock = clock
return btc
}
func (btc *BlockTimeCalculator) setBlockCounter(counter intervalBlocksCounter) *BlockTimeCalculator {
btc.blocksCounter = counter
return btc
}
func (btc *BlockTimeCalculator) countBlockTime(blockTime time.Time) (blockGenerationState, error) {
bgs := blockGenerationState{}
nextBlockStart := btc.firstBlockTime
var curNodeIndex int64
if blockTime.Before(nextBlockStart) {
return blockGenerationState{}, TimeError
}
for {
curBlockStart := nextBlockStart
curBlockEnd := curBlockStart.Add(btc.blocksGap + btc.blockGenerationTime)
nextBlockStart = curBlockEnd.Add(time.Second)
if blockTime.Equal(curBlockStart) || blockTime.After(curBlockStart) && blockTime.Before(nextBlockStart) {
bgs.start = curBlockStart
bgs.duration = btc.blocksGap + btc.blockGenerationTime
bgs.nodePosition = curNodeIndex
return bgs, nil
}
if btc.nodesCount > 0 {
curNodeIndex = (curNodeIndex + 1) % btc.nodesCount
}
}
}