forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpos.go
259 lines (213 loc) · 6.14 KB
/
dpos.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package dpos
import (
"fmt"
"time"
"github.com/aergoio/aergo-lib/log"
"github.com/aergoio/aergo/config"
"github.com/aergoio/aergo/consensus"
"github.com/aergoio/aergo/consensus/impl/dpos/bp"
"github.com/aergoio/aergo/consensus/impl/dpos/slot"
"github.com/aergoio/aergo/internal/enc"
"github.com/aergoio/aergo/p2p"
"github.com/aergoio/aergo/pkg/component"
"github.com/aergoio/aergo/state"
"github.com/aergoio/aergo/types"
peer "github.com/libp2p/go-libp2p-peer"
)
// DefaultDposBpNumber is the default number of block producers.
const DefaultDposBpNumber = 23
var (
logger = log.NewLogger("dpos")
// blockProducers is the number of block producers
blockProducers uint16
majorityCount uint16
initialBpElectionPeriod types.BlockNo
lastJob = &lastSlot{}
)
type lastSlot struct {
// sync.Mutex
s *slot.Slot
}
func (l *lastSlot) get() *slot.Slot {
// l.Lock()
// defer l.Unlock()
return l.s
}
func (l *lastSlot) set(s *slot.Slot) {
// l.Lock()
// defer l.Unlock()
l.s = s
}
// DPoS is the main data structure of DPoS consensus
type DPoS struct {
*Status
*component.ComponentHub
bpc *bp.Cluster
bf *BlockFactory
quit chan interface{}
ca types.ChainAccessor
}
// Status shows DPoS consensus's current status
type bpInfo struct {
bestBlock *types.Block
slot *slot.Slot
ca types.ChainAccessor
}
func (bi *bpInfo) updateBestBLock() *types.Block {
block, _ := bi.ca.GetBestBlock()
if block != nil {
bi.bestBlock = block
}
return block
}
// GetConstructor build and returns consensus.Constructor from New function.
func GetConstructor(cfg *config.ConsensusConfig, hub *component.ComponentHub, cdb consensus.ChainDB) consensus.Constructor {
return func() (consensus.Consensus, error) {
return New(cfg, hub, cdb)
}
}
// New returns a new DPos object
func New(cfg *config.ConsensusConfig, hub *component.ComponentHub, cdb consensus.ChainDB) (consensus.Consensus, error) {
bpc, err := bp.NewCluster(cfg, cdb)
if err != nil {
return nil, err
}
Init(bpc.Size(), cfg.BlockInterval)
quitC := make(chan interface{})
return &DPoS{
Status: NewStatus(bpc, cdb),
ComponentHub: hub,
bpc: bpc,
bf: NewBlockFactory(hub, quitC),
quit: quitC,
}, nil
}
// Init initilizes the DPoS parameters.
func Init(bpCount uint16, blockInterval int64) {
blockProducers = bpCount
majorityCount = blockProducers*2/3 + 1
// Collect voting for BPs during 10 rounds.
initialBpElectionPeriod = types.BlockNo(blockProducers) * 10
slot.Init(blockInterval, blockProducers)
}
func consensusBlockCount(bpCount uint16) uint16 {
return bpCount*2/3 + 1
}
// Ticker returns a time.Ticker for the main consensus loop.
func (dpos *DPoS) Ticker() *time.Ticker {
return time.NewTicker(tickDuration())
}
func tickDuration() time.Duration {
return consensus.BlockInterval / 100
}
// QueueJob send a block triggering information to jq.
func (dpos *DPoS) QueueJob(now time.Time, jq chan<- interface{}) {
bpi := dpos.getBpInfo(now)
if bpi != nil {
jq <- bpi
lastJob.set(bpi.slot)
}
}
// BlockFactory returns the BlockFactory interface in dpos.
func (dpos *DPoS) BlockFactory() consensus.BlockFactory {
return dpos.bf
}
// SetChainAccessor sets dpost.ca to chainAccessor
func (dpos *DPoS) SetChainAccessor(chainAccessor types.ChainAccessor) {
dpos.ca = chainAccessor
}
// SetStateDB sets sdb to the corresponding field of DPoS. This method is
// called only once during the boot sequence.
func (dpos *DPoS) SetStateDB(sdb *state.ChainStateDB) {
dpos.bf.sdb = sdb
dpos.Status.setStateDB(sdb)
}
// IsTransactionValid checks the DPoS consensus level validity of a transaction
func (dpos *DPoS) IsTransactionValid(tx *types.Tx) bool {
// TODO: put a transaction validity check code here.
return true
}
// QuitChan returns the channel from which consensus-related goroutines check when
// shutdown is initiated.
func (dpos *DPoS) QuitChan() chan interface{} {
return dpos.quit
}
func (dpos *DPoS) bpid() peer.ID {
return p2p.NodeID()
}
// IsBlockValid checks the DPoS consensus level validity of a block
func (dpos *DPoS) IsBlockValid(block *types.Block, bestBlock *types.Block) error {
id, err := block.BPID()
if err != nil {
return &consensus.ErrorConsensus{Msg: "bad public key in block", Err: err}
}
ns := block.GetHeader().GetTimestamp()
idx, ok := dpos.bpc.BpID2Index(id)
s := slot.NewFromUnixNano(ns)
// Check whether the BP ID is one of the current BP members and its
// corresponding BP index is consistent with the block timestamp.
if !ok || !s.IsFor(idx) {
return &consensus.ErrorConsensus{
Msg: fmt.Sprintf("BP %v (idx: %v) is not permitted for the time slot %v (%v)",
block.BPID2Str(), idx, time.Unix(0, ns), s.NextBpIndex()),
}
}
valid, err := block.VerifySign()
if !valid {
return &consensus.ErrorConsensus{Msg: "bad block signature", Err: err}
}
return nil
}
func (dpos *DPoS) bpIdx() uint16 {
idx, exist := dpos.bpc.BpID2Index(dpos.bpid())
if !exist {
logger.Fatal().Str("id", enc.ToString([]byte(dpos.bpid()))).Msg("BP has no correct BP membership")
}
return idx
}
func (dpos *DPoS) getBpInfo(now time.Time) *bpInfo {
s := slot.Time(now)
if !s.IsFor(dpos.bpIdx()) {
return nil
}
// already queued slot.
if slot.Equal(s, lastJob.get()) {
return nil
}
block, _ := dpos.ca.GetBestBlock()
logger.Debug().Str("best", block.ID()).Uint64("no", block.GetHeader().GetBlockNo()).
Msg("GetBestBlock from BP")
if block == nil {
return nil
}
if !isBpTiming(block, s) {
return nil
}
return &bpInfo{
bestBlock: block,
slot: s,
ca: dpos.ca,
}
}
func isBpTiming(block *types.Block, s *slot.Slot) bool {
blockSlot := slot.NewFromUnixNano(block.Header.Timestamp)
// The block corresponding to the current slot has already been generated.
if slot.LessEqual(s, blockSlot) {
return false
}
// Check whether the remaining time is enough until the next block
// generation time.
if !slot.IsNextTo(s, blockSlot) && !s.TimesUp() {
return false
}
timeLeft := s.RemainingTimeMS()
if timeLeft < 0 {
logger.Debug().Int64("remaining time", timeLeft).Msg("no time left to produce block")
return false
}
return true
}