/
consensus.go
413 lines (341 loc) · 14.3 KB
/
consensus.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright 2018 The cpchain authors
// This file is part of the cpchain library.
//
// The cpchain 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 cpchain 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 cpchain library. If not, see <http://www.gnu.org/licenses/>.
package dpor
import (
"bytes"
"errors"
"math/big"
"time"
"bitbucket.org/cpchain/chain/api/rpc"
"bitbucket.org/cpchain/chain/commons/log"
"bitbucket.org/cpchain/chain/configs"
"bitbucket.org/cpchain/chain/consensus"
"bitbucket.org/cpchain/chain/consensus/dpor/backend"
"bitbucket.org/cpchain/chain/core/state"
"bitbucket.org/cpchain/chain/types"
"github.com/ethereum/go-ethereum/common"
)
// Dpor proof-of-reputation protocol constants.
const (
defaultCampaignTerms = uint64(3) // Default number of terms to campaign for proposer committee.
extraVanity = 32 // Fixed number of extra-data prefix bytes reserved for signer vanity
extraSeal = 65 // Fixed number of extra-data suffix bytes reserved for signer seal
)
// Various error messages to mark blocks invalid. These should be private to
// prevent engine specific errors from being referenced in the remainder of the
// codebase, inherently breaking if the engine is swapped out. Please put common
// error types into the consensus package.
var (
// errUnknownBlock is returned when the list of signers is requested for a block
// that is not part of the local blockchain.
errUnknownBlock = errors.New("unknown block")
// errInvalidCheckpointBeneficiary is returned if a checkpoint/epoch transition
// block has a beneficiary set to non-zeroes.
errInvalidCheckpointBeneficiary = errors.New("beneficiary in checkpoint block non-zero")
// errMissingVanity is returned if a block's extra-data section is shorter than
// 32 bytes, which is required to store the signer vanity.
errMissingVanity = errors.New("extra-data 32 byte vanity prefix missing")
// errMissingSignature is returned if a block's extra-data section doesn't seem
// to contain a 65 byte secp256k1 signature.
errMissingSignature = errors.New("extra-data 65 byte suffix signature missing")
// ErrInvalidTimestamp is returned if the timestamp of a block is lower than
// the previous block's timestamp + the minimum block period.
ErrInvalidTimestamp = errors.New("invalid timestamp")
// ErrInvalidGasLimit is returned if the gasLimit of a block is invalid
ErrInvalidGasLimit = errors.New("invalid gas limit for the block")
// errInvalidChain is returned if an authorization list is attempted to
// be modified via out-of-range or non-contiguous headers.
errInvalidChain = errors.New("invalid voting chain")
// --- new error types ---
// errMultiBlocksInOneHeight is returned if there is multi blocks in one height in the chain.
errMultiBlocksInOneHeight = errors.New("multi blocks in one height")
// errInvalidValidatorSigs is returned if the dpor sigs are not sigend by correct validator committtee.
errInvalidValidatorSigs = errors.New("invalid validator signatures")
// errNoSigsInCache is returned if the cache is unable to store and return sigs.
errNoSigsInCache = errors.New("signatures not found in cache")
errFakerFail = errors.New("error fake fail")
// --- our new error types ---
// errNilParent is returned when the parent is nil.
errNilParent = errors.New("parent is nil")
// errVerifyUncleNotAllowed is returned when verify uncle block.
errVerifyUncleNotAllowed = errors.New("uncles not allowed")
// errWaitTransactions is returned if an empty block is attempted to be sealed
// on an instant chain (0 second period). It's important to refuse these as the
// block reward is zero, so an empty block just bloats the chain... fast.
errWaitTransactions = errors.New("waiting for transactions")
errInvalidStateForSign = errors.New("the state is unexpected for signing header")
)
// Author implements consensus.Engine, returning the cpchain address recovered
// from the signature in the header's extra-data section.
func (d *Dpor) Author(header *types.Header) (common.Address, error) {
proposer, _, err := d.dh.ecrecover(header, d.finalSigs)
return proposer, err
}
// VerifyHeader checks whether a header conforms to the consensus rules.
func (d *Dpor) VerifyHeader(chain consensus.ChainReader, header *types.Header, verifySigs bool, refHeader *types.Header) error {
return d.dh.verifyHeader(d, chain, header, nil, refHeader, verifySigs, false)
}
// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers. The
// method returns a quit channel to abort the operations and a results channel to
// retrieve the async verifications (the order is that of the input slice).
func (d *Dpor) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, verifySigs []bool, refHeaders []*types.Header) (chan<- struct{}, <-chan error) {
abort := make(chan struct{})
results := make(chan error, len(headers))
go func() {
for i, header := range headers {
err := d.dh.verifyHeader(d, chain, header, headers[:i], refHeaders[i], verifySigs[i], false)
select {
case <-abort:
return
case results <- err:
}
}
}()
return abort, results
}
// VerifySeal implements consensus.Engine, checking whether the signature contained
// in the header satisfies the consensus protocol requirements.
func (d *Dpor) VerifySeal(chain consensus.ChainReader, header *types.Header, refHeader *types.Header) error {
return d.dh.verifySeal(d, chain, header, nil, refHeader)
}
// VerifySigs checks if header has enough signatures of validators.
func (d *Dpor) VerifySigs(chain consensus.ChainReader, header *types.Header, refHeader *types.Header) error {
return d.dh.verifySignatures(d, chain, header, nil, refHeader)
}
// PrepareBlock implements consensus.Engine, preparing all the consensus fields of the
// header for running the transactions on top.
func (d *Dpor) PrepareBlock(chain consensus.ChainReader, header *types.Header) error {
number := header.Number.Uint64()
// Create a snapshot
snap, err := d.dh.snapshot(d, chain, number-1, header.ParentHash, nil)
if err != nil {
return err
}
// Ensure the extra data has all its components
if len(header.Extra) < extraVanity {
header.Extra = append(header.Extra, bytes.Repeat([]byte{0x00}, extraVanity-len(header.Extra))...)
}
header.Extra = header.Extra[:extraVanity]
for _, proposer := range snap.ProposersOf(number) {
header.Dpor.Proposers = append(header.Dpor.Proposers, proposer)
}
log.Debug("prepare a block", "number", header.Number.Uint64(), "proposers", header.Dpor.ProposersFormatText(),
"validators", header.Dpor.ValidatorsFormatText())
// Set correct signatures size
header.Dpor.Sigs = make([]types.DporSignature, d.config.ValidatorsLen())
// Ensure the timestamp has the correct delay
parent := chain.GetHeader(header.ParentHash, number-1)
if parent == nil {
log.Warn("consensus.ErrUnknownAncestor 4", "number", number, "parentHash", header.ParentHash.Hex())
return consensus.ErrUnknownAncestor
}
header.SetTimestamp(parent.Timestamp().Add(d.config.PeriodDuration()))
if header.Timestamp().Before(time.Now()) {
header.SetTimestamp(time.Now())
}
return nil
}
// TryCampaign tries to start campaign
func (d *Dpor) TryCampaign() {
if d.ac == nil {
// it is not able to campaign in the situation
log.Debug("it is not able to campaign in the situation")
return
}
snap := d.CurrentSnap()
if snap != nil {
isV := snap.IsValidatorOf(d.coinbase, snap.Number)
log.Debug("check if participate campaign", "isToCampaign", d.IsToCampaign(), "isStartCampaign", snap.isStartCampaign(), "number", snap.number(), "isValidator", isV)
if d.IsToCampaign() && snap.isAboutToCampaign() && !isV {
// make sure it is a RNode, because only RNode has permission to participate campaign
isRNode, err := d.ac.IsRNode()
if err != nil {
log.Debug("encounter error when invoke IsRNode()", "error", err)
return
}
if !isRNode {
log.Info("it is not RNode, cannot participate campaign")
if err := d.ac.FundForRNode(); err != nil {
log.Debug("failed to FundForRNode", "detail", err)
return
}
log.Info("already send money to become RNode")
}
}
if d.IsToCampaign() && snap.isStartCampaign() && !isV {
newTerm := d.CurrentSnap().TermOf(snap.Number)
if newTerm > d.lastCampaignTerm+defaultCampaignTerms-1 {
d.lastCampaignTerm = newTerm
log.Info("campaign for proposer committee", "eleTerm", newTerm)
if d.ac != nil {
d.ac.Campaign(defaultCampaignTerms)
}
}
}
}
}
// GetBlockReward returns block reward according to block number
func (d *Dpor) GetBlockReward(blockNum uint64) *big.Int {
reward := getBlockReward(new(big.Int).SetUint64(blockNum))
return reward
}
func getBlockReward(number *big.Int) *big.Int {
var amount *big.Int
if number.Cmp(configs.Cep1LastBlockY1()) <= 0 {
amount = configs.Cep1BlockRewardY1()
} else if number.Cmp(configs.Cep1LastBlockY2()) <= 0 {
amount = configs.Cep1BlockRewardY2()
} else if number.Cmp(configs.Cep1LastBlockY3()) <= 0 {
amount = configs.Cep1BlockRewardY3()
} else if number.Cmp(configs.Cep1LastBlockY4()) <= 0 {
amount = configs.Cep1BlockRewardY4()
} else if number.Cmp(configs.Cep1LastBlockY5()) <= 0 {
amount = configs.Cep1BlockRewardY5()
} else {
amount = big.NewInt(0)
}
return amount
}
func addCoinbaseReward(coinbase common.Address, state *state.StateDB, number *big.Int) {
amount := getBlockReward(number)
state.AddBalance(coinbase, amount)
}
// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
// rewards given, and returns the final block.
func (d *Dpor) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
if (header.Coinbase != common.Address{}) {
addCoinbaseReward(header.Coinbase, state, header.Number)
}
// last step
header.StateRoot = state.IntermediateRoot(true)
// Assemble and return the final block for sealing
return types.NewBlock(header, txs, receipts), nil
}
// Authorize injects a private key into the consensus engine to mint new blocks
// with.
func (d *Dpor) Authorize(signer common.Address, signFn backend.SignFn) {
d.coinbaseLock.Lock()
d.coinbase = signer
d.signFn = signFn
d.coinbaseLock.Unlock()
if d.handler == nil {
d.handler = backend.NewHandler(d.config, d.Coinbase(), d.db)
}
if d.handler.Coinbase() != signer {
d.handler.SetCoinbase(signer)
}
}
// Seal implements consensus.Engine, attempting to create a sealed block using
// the local signing credentials.
// NB please populate the correct field values. we are now removing some fields such as nonce.
func (d *Dpor) Seal(chain consensus.ChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error) {
var (
header = block.Header()
number = header.Number.Uint64()
coinbase = d.Coinbase()
signFn = d.SignHash
)
// Sealing the genesis block is not supported
if number == 0 {
return nil, errUnknownBlock
}
// For 0-period chains, refuse to seal empty blocks (no reward but would spin sealing)
if d.config.Period == 0 && len(block.Transactions()) == 0 {
return nil, errWaitTransactions
}
// Bail out if we're unauthorized to sign a block
snap, err := d.dh.snapshot(d, chain, number-1, header.ParentHash, nil)
if err != nil {
return nil, err
}
ok, err := snap.IsProposerOf(coinbase, number)
if err != nil {
if err == errProposerNotInCommittee {
return nil, consensus.ErrNotInProposerCommittee
}
log.Debug("Error occurs when seal block", "error", err)
return nil, err
}
if !ok {
return nil, consensus.ErrUnauthorized
}
// Sweet, the protocol permits us to sign the block, wait for our time
delay := header.Timestamp().Sub(time.Now())
log.Debug("Waiting for slot to sign and propagate", "delay", delay)
select {
case <-stop:
log.Warn("Quit block sealing", "number", block.NumberU64(), "hash", block.Hash().Hex())
return nil, nil
case <-time.After(delay):
log.Debug("wait for seal", "delay", delay)
}
// Proposer seals the block with signature
sighash, err := signFn(d.dh.sigHash(header).Bytes())
if err != nil {
return nil, err
}
copy(header.Dpor.Seal[:], sighash)
// Create a signature space for validators
header.Dpor.Sigs = make([]types.DporSignature, len(header.Dpor.Validators))
log.Debug("sealed the block", "hash", header.Hash().Hex(), "number", header.Number)
// Update dpor current snapshot
d.SetCurrentSnap(snap)
return block.WithSeal(header), nil
}
// CanMakeBlock checks if the given coinbase is ready to propose a block
func (d *Dpor) CanMakeBlock(chain consensus.ChainReader, coinbase common.Address, parent *types.Header) bool {
number := parent.Number.Uint64()
// Bail out if we're unauthorized to sign a block
snap, err := d.dh.snapshot(d, chain, number, parent.Hash(), nil)
if err != nil {
log.Debug("Error occurs when create a snapshot", "error", err)
return false
}
log.Debug("created an snapshot")
// check if it is the in-charge proposer for next block
ok, err := snap.IsProposerOf(coinbase, number+1)
if err != nil {
log.Debug("it is not proposer", "msg", err)
return false
}
log.Debug("now can finished CanMakeBlock call", "ok", ok)
return ok
}
// APIs implements consensus.Engine, returning the user facing RPC API to allow
// controlling the signer voting.
func (d *Dpor) APIs(chain consensus.ChainReader) []rpc.API {
return []rpc.API{{
Namespace: "dpor",
Version: "1.0",
Service: &API{chain: chain, dpor: d},
Public: false,
}}
}
// State returns current pbft phrase, one of (PrePrepare, Prepare, Commit).
func (d *Dpor) State() consensus.State {
d.stateLock.Lock()
defer d.stateLock.Unlock()
return d.pbftState
}
// GetCalcRptInfo get the rpt value of an address at specific block number
func (d *Dpor) GetCalcRptInfo(address common.Address, addresses []common.Address, blockNum uint64) int64 {
rptService := d.GetRptBackend()
if rptService == nil {
log.Fatal("dpor rpt service is nil")
}
rp := rptService.CalcRptInfo(address, addresses, blockNum)
return rp.Rpt
}