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
|
// Copyright 2018 The dexon-consensus Authors
// This file is part of the dexon-consensus library.
//
// The dexon-consensus library is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The dexon-consensus library 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 Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the dexon-consensus library. If not, see
// <http://www.gnu.org/licenses/>.
package utils
import (
"fmt"
"gitlab.com/byzantine-lab/tangerine-consensus/core/types"
)
// RoundBasedConfig is based config for rounds and provide boundary checking
// for rounds.
type RoundBasedConfig struct {
roundID uint64
roundBeginHeight uint64
roundEndHeight uint64
roundLength uint64
}
// SetupRoundBasedFields setup round based fields, including round ID, the
// length of rounds.
func (c *RoundBasedConfig) SetupRoundBasedFields(
roundID uint64, cfg *types.Config) {
if c.roundLength > 0 {
panic(fmt.Errorf("duplicated set round based fields: %d",
c.roundLength))
}
c.roundID = roundID
c.roundLength = cfg.RoundLength
}
// SetRoundBeginHeight gives the beginning height for the initial round provided
// when constructed.
func (c *RoundBasedConfig) SetRoundBeginHeight(begin uint64) {
if c.roundBeginHeight != 0 {
panic(fmt.Errorf("duplicated set round begin height: %d",
c.roundBeginHeight))
}
c.roundBeginHeight = begin
c.roundEndHeight = begin + c.roundLength
}
// IsLastBlock checks if a block is the last block of this round.
func (c *RoundBasedConfig) IsLastBlock(b *types.Block) bool {
if b.Position.Round != c.roundID {
panic(fmt.Errorf("attempt to compare by different round: %s, %d",
b, c.roundID))
}
return b.Position.Height+1 == c.roundEndHeight
}
// ExtendLength extends round ending height by the length of current round.
func (c *RoundBasedConfig) ExtendLength() {
c.roundEndHeight += c.roundLength
}
// Contains checks if a block height is in this round.
func (c *RoundBasedConfig) Contains(h uint64) bool {
return c.roundBeginHeight <= h && c.roundEndHeight > h
}
// RoundID returns the round ID of this config.
func (c *RoundBasedConfig) RoundID() uint64 {
if c.roundLength == 0 {
panic(fmt.Errorf("config is not initialized: %d", c.roundID))
}
return c.roundID
}
// RoundEndHeight returns next checkpoint to varify if this round is ended.
func (c *RoundBasedConfig) RoundEndHeight() uint64 {
if c.roundLength == 0 {
panic(fmt.Errorf("config is not initialized: %d", c.roundID))
}
return c.roundEndHeight
}
// AppendTo a config from previous round.
func (c *RoundBasedConfig) AppendTo(other RoundBasedConfig) {
if c.roundID != other.roundID+1 {
panic(fmt.Errorf("round IDs of configs not continuous: %d %d",
c.roundID, other.roundID))
}
c.SetRoundBeginHeight(other.roundEndHeight)
}
// LastPeriodBeginHeight returns the begin height of last period. For example,
// if a round is extended twice, then the return from this method is:
//
// begin + 2 * roundLength - roundLength
//
func (c *RoundBasedConfig) LastPeriodBeginHeight() uint64 {
if c.roundLength == 0 {
panic(fmt.Errorf("config is not initialized: %d", c.roundID))
}
return c.roundEndHeight - c.roundLength
}
|