forked from Consensys/quorum
/
engine.go
566 lines (476 loc) · 18.1 KB
/
engine.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package qbftengine
import (
"bytes"
"fmt"
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/istanbul"
istanbulcommon "github.com/ethereum/go-ethereum/consensus/istanbul/common"
"github.com/ethereum/go-ethereum/consensus/istanbul/validator"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
"golang.org/x/crypto/sha3"
)
var (
nilUncleHash = types.CalcUncleHash(nil) // Always Keccak256(RLP([])) as uncles are meaningless outside of PoW.
)
type SignerFn func(data []byte) ([]byte, error)
type Engine struct {
cfg *istanbul.Config
signer common.Address // Ethereum address of the signing key
sign SignerFn // Signer function to authorize hashes with
}
func NewEngine(cfg *istanbul.Config, signer common.Address, sign SignerFn) *Engine {
return &Engine{
cfg: cfg,
signer: signer,
sign: sign,
}
}
func (e *Engine) Author(header *types.Header) (common.Address, error) {
return header.Coinbase, nil
}
func (e *Engine) CommitHeader(header *types.Header, seals [][]byte, round *big.Int) error {
return ApplyHeaderQBFTExtra(
header,
writeCommittedSeals(seals),
writeRoundNumber(round),
)
}
// writeCommittedSeals writes the extra-data field of a block header with given committed seals.
func writeCommittedSeals(committedSeals [][]byte) ApplyQBFTExtra {
return func(qbftExtra *types.QBFTExtra) error {
if len(committedSeals) == 0 {
return istanbulcommon.ErrInvalidCommittedSeals
}
for _, seal := range committedSeals {
if len(seal) != types.IstanbulExtraSeal {
return istanbulcommon.ErrInvalidCommittedSeals
}
}
qbftExtra.CommittedSeal = make([][]byte, len(committedSeals))
copy(qbftExtra.CommittedSeal, committedSeals)
return nil
}
}
// writeRoundNumber writes the extra-data field of a block header with given round.
func writeRoundNumber(round *big.Int) ApplyQBFTExtra {
return func(qbftExtra *types.QBFTExtra) error {
qbftExtra.Round = uint32(round.Uint64())
return nil
}
}
func (e *Engine) VerifyBlockProposal(chain consensus.ChainHeaderReader, block *types.Block, validators istanbul.ValidatorSet) (time.Duration, error) {
// check block body
txnHash := types.DeriveSha(block.Transactions(), new(trie.Trie))
if txnHash != block.Header().TxHash {
return 0, istanbulcommon.ErrMismatchTxhashes
}
uncleHash := types.CalcUncleHash(block.Uncles())
if uncleHash != nilUncleHash {
return 0, istanbulcommon.ErrInvalidUncleHash
}
// verify the header of proposed block
err := e.VerifyHeader(chain, block.Header(), nil, validators)
if err == nil || err == istanbulcommon.ErrEmptyCommittedSeals {
// ignore errEmptyCommittedSeals error because we don't have the committed seals yet
return 0, nil
} else if err == consensus.ErrFutureBlock {
return time.Until(time.Unix(int64(block.Header().Time), 0)), consensus.ErrFutureBlock
}
parentHeader := chain.GetHeaderByHash(block.ParentHash())
config := e.cfg.GetConfig(parentHeader.Number)
if config.EmptyBlockPeriod > config.BlockPeriod && len(block.Transactions()) == 0 {
if block.Header().Time < parentHeader.Time+config.EmptyBlockPeriod {
return 0, fmt.Errorf("empty block verification fail")
}
}
return 0, err
}
func (e *Engine) VerifyHeader(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header, validators istanbul.ValidatorSet) error {
return e.verifyHeader(chain, header, parents, validators)
}
// verifyHeader checks whether a header conforms to the consensus rules.The
// caller may optionally pass in a batch of parents (ascending order) to avoid
// looking those up from the database. This is useful for concurrently verifying
// a batch of new headers.
func (e *Engine) verifyHeader(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header, validators istanbul.ValidatorSet) error {
if header.Number == nil {
return istanbulcommon.ErrUnknownBlock
}
// Don't waste time checking blocks from the future (adjusting for allowed threshold)
adjustedTimeNow := time.Now().Add(time.Duration(e.cfg.AllowedFutureBlockTime) * time.Second).Unix()
if header.Time > uint64(adjustedTimeNow) {
return consensus.ErrFutureBlock
}
if _, err := types.ExtractQBFTExtra(header); err != nil {
return istanbulcommon.ErrInvalidExtraDataFormat
}
// Ensure that the mix digest is zero as we don't have fork protection currently
if header.MixDigest != types.IstanbulDigest {
return istanbulcommon.ErrInvalidMixDigest
}
// Ensure that the block doesn't contain any uncles which are meaningless in Istanbul
if header.UncleHash != nilUncleHash {
return istanbulcommon.ErrInvalidUncleHash
}
// Ensure that the block's difficulty is meaningful (may not be correct at this point)
if header.Difficulty == nil || header.Difficulty.Cmp(istanbulcommon.DefaultDifficulty) != 0 {
return istanbulcommon.ErrInvalidDifficulty
}
return e.verifyCascadingFields(chain, header, validators, parents)
}
func (e *Engine) VerifyHeaders(chain consensus.ChainHeaderReader, headers []*types.Header, seals []bool, validators istanbul.ValidatorSet) (chan<- struct{}, <-chan error) {
abort := make(chan struct{})
results := make(chan error, len(headers))
go func() {
errored := false
for i, header := range headers {
var err error
if errored {
err = consensus.ErrUnknownAncestor
} else {
err = e.verifyHeader(chain, header, headers[:i], validators)
}
if err != nil {
errored = true
}
select {
case <-abort:
return
case results <- err:
}
}
}()
return abort, results
}
// verifyCascadingFields verifies all the header fields that are not standalone,
// rather depend on a batch of previous headers. The caller may optionally pass
// in a batch of parents (ascending order) to avoid looking those up from the
// database. This is useful for concurrently verifying a batch of new headers.
func (e *Engine) verifyCascadingFields(chain consensus.ChainHeaderReader, header *types.Header, validators istanbul.ValidatorSet, parents []*types.Header) error {
// The genesis block is the always valid dead-end
number := header.Number.Uint64()
if number == 0 {
return nil
}
// Check parent
var parent *types.Header
if len(parents) > 0 {
parent = parents[len(parents)-1]
} else {
parent = chain.GetHeader(header.ParentHash, number-1)
}
// Ensure that the block's parent has right number and hash
if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash {
return consensus.ErrUnknownAncestor
}
// Ensure that the block's timestamp isn't too close to it's parent
// When the BlockPeriod is reduced it is reduced for the proposal.
// e.g when blockperiod is 1 from block 10 the block period between 9 and 10 is 1
if parent.Time+e.cfg.GetConfig(header.Number).BlockPeriod > header.Time {
return istanbulcommon.ErrInvalidTimestamp
}
// Verify signer
if err := e.verifySigner(chain, header, parents, validators); err != nil {
return err
}
return e.verifyCommittedSeals(chain, header, parents, validators)
}
func (e *Engine) verifySigner(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header, validators istanbul.ValidatorSet) error {
// Verifying the genesis block is not supported
number := header.Number.Uint64()
if number == 0 {
return istanbulcommon.ErrUnknownBlock
}
// Resolve the authorization key and check against signers
signer, err := e.Author(header)
if err != nil {
return err
}
// Signer should be in the validator set of previous block's extraData.
if _, v := validators.GetByAddress(signer); v == nil {
return istanbulcommon.ErrUnauthorized
}
return nil
}
// verifyCommittedSeals checks whether every committed seal is signed by one of the parent's validators
func (e *Engine) verifyCommittedSeals(chain consensus.ChainHeaderReader, header *types.Header, parents []*types.Header, validators istanbul.ValidatorSet) error {
number := header.Number.Uint64()
if number == 0 {
// We don't need to verify committed seals in the genesis block
return nil
}
extra, err := types.ExtractQBFTExtra(header)
if err != nil {
return err
}
committedSeal := extra.CommittedSeal
// The length of Committed seals should be larger than 0
if len(committedSeal) == 0 {
return istanbulcommon.ErrEmptyCommittedSeals
}
validatorsCpy := validators.Copy()
// Check whether the committed seals are generated by validators
validSeal := 0
committers, err := e.Signers(header)
if err != nil {
return err
}
for _, addr := range committers {
if validatorsCpy.RemoveValidator(addr) {
validSeal++
continue
}
return istanbulcommon.ErrInvalidCommittedSeals
}
// The length of validSeal should be larger than number of faulty node + 1
if validSeal <= validators.F() {
return istanbulcommon.ErrInvalidCommittedSeals
}
return nil
}
// VerifyUncles verifies that the given block's uncles conform to the consensus
// rules of a given engine.
func (e *Engine) VerifyUncles(chain consensus.ChainReader, block *types.Block) error {
if len(block.Uncles()) > 0 {
return istanbulcommon.ErrInvalidUncleHash
}
return nil
}
// VerifySeal checks whether the crypto seal on a header is valid according to
// the consensus rules of the given engine.
func (e *Engine) VerifySeal(chain consensus.ChainHeaderReader, header *types.Header, validators istanbul.ValidatorSet) error {
// get parent header and ensure the signer is in parent's validator set
number := header.Number.Uint64()
if number == 0 {
return istanbulcommon.ErrUnknownBlock
}
// ensure that the difficulty equals to istanbulcommon.DefaultDifficulty
if header.Difficulty.Cmp(istanbulcommon.DefaultDifficulty) != 0 {
return istanbulcommon.ErrInvalidDifficulty
}
return e.verifySigner(chain, header, nil, validators)
}
func (e *Engine) Prepare(chain consensus.ChainHeaderReader, header *types.Header, validators istanbul.ValidatorSet) error {
header.Coinbase = common.Address{}
header.Nonce = istanbulcommon.EmptyBlockNonce
header.MixDigest = types.IstanbulDigest
// copy the parent extra data as the header extra data
number := header.Number.Uint64()
parent := chain.GetHeader(header.ParentHash, number-1)
if parent == nil {
return consensus.ErrUnknownAncestor
}
// use the same difficulty for all blocks
header.Difficulty = istanbulcommon.DefaultDifficulty
// set header's timestamp
header.Time = parent.Time + e.cfg.GetConfig(header.Number).BlockPeriod
if header.Time < uint64(time.Now().Unix()) {
header.Time = uint64(time.Now().Unix())
}
currentBlockNumber := big.NewInt(0).SetUint64(number - 1)
validatorContract := e.cfg.GetValidatorContractAddress(currentBlockNumber)
if validatorContract != (common.Address{}) && e.cfg.GetValidatorSelectionMode(currentBlockNumber) == params.ContractMode {
return ApplyHeaderQBFTExtra(
header,
WriteValidators([]common.Address{}),
)
} else {
for _, transition := range e.cfg.Transitions {
if transition.Block.Cmp(currentBlockNumber) == 0 && len(transition.Validators) > 0 {
toRemove := make([]istanbul.Validator, 0, validators.Size())
l := validators.List()
toRemove = append(toRemove, l...)
for i := range toRemove {
validators.RemoveValidator(toRemove[i].Address())
}
for i := range transition.Validators {
validators.AddValidator(transition.Validators[i])
}
break
}
}
validatorsList := validator.SortedAddresses(validators.List())
// add validators in snapshot to extraData's validators section
return ApplyHeaderQBFTExtra(
header,
WriteValidators(validatorsList),
)
}
}
func WriteValidators(validators []common.Address) ApplyQBFTExtra {
return func(qbftExtra *types.QBFTExtra) error {
qbftExtra.Validators = validators
return nil
}
}
// Finalize runs any post-transaction state modifications (e.g. block rewards)
// and assembles the final block.
//
// Note, the block header and state database might be updated to reflect any
// consensus rules that happen at finalization (e.g. block rewards).
func (e *Engine) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) {
// Accumulate any block and uncle rewards and commit the final state root
e.accumulateRewards(chain, state, header)
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
header.UncleHash = nilUncleHash
}
// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,
// nor block rewards given, and returns the final block.
func (e *Engine) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
e.Finalize(chain, header, state, txs, uncles)
// Assemble and return the final block for sealing
return types.NewBlock(header, txs, nil, receipts, new(trie.Trie)), nil
}
// Seal generates a new block for the given input block with the local miner's
// seal place on top.
func (e *Engine) Seal(chain consensus.ChainHeaderReader, block *types.Block, validators istanbul.ValidatorSet) (*types.Block, error) {
if _, v := validators.GetByAddress(e.signer); v == nil {
return block, istanbulcommon.ErrUnauthorized
}
header := block.Header()
parent := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1)
if parent == nil {
return block, consensus.ErrUnknownAncestor
}
// Set Coinbase
header.Coinbase = e.signer
return block.WithSeal(header), nil
}
func (e *Engine) SealHash(header *types.Header) common.Hash {
header.Coinbase = e.signer
return sigHash(header)
}
func (e *Engine) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int {
return new(big.Int)
}
func (e *Engine) ExtractGenesisValidators(header *types.Header) ([]common.Address, error) {
extra, err := types.ExtractQBFTExtra(header)
if err != nil {
return nil, err
}
return extra.Validators, nil
}
func (e *Engine) Signers(header *types.Header) ([]common.Address, error) {
extra, err := types.ExtractQBFTExtra(header)
if err != nil {
return []common.Address{}, err
}
committedSeal := extra.CommittedSeal
proposalSeal := PrepareCommittedSeal(header, extra.Round)
var addrs []common.Address
// 1. Get committed seals from current header
for _, seal := range committedSeal {
// 2. Get the original address by seal and parent block hash
addr, err := istanbul.GetSignatureAddressNoHashing(proposalSeal, seal)
if err != nil {
return nil, istanbulcommon.ErrInvalidSignature
}
addrs = append(addrs, addr)
}
return addrs, nil
}
func (e *Engine) Address() common.Address {
return e.signer
}
// FIXME: Need to update this for Istanbul
// sigHash returns the hash which is used as input for the Istanbul
// signing. It is the hash of the entire header apart from the 65 byte signature
// contained at the end of the extra data.
//
// Note, the method requires the extra data to be at least 65 bytes, otherwise it
// panics. This is done to avoid accidentally using both forms (signature present
// or not), which could be abused to produce different hashes for the same header.
func sigHash(header *types.Header) (hash common.Hash) {
hasher := sha3.NewLegacyKeccak256()
rlp.Encode(hasher, types.QBFTFilteredHeader(header))
hasher.Sum(hash[:0])
return hash
}
// PrepareCommittedSeal returns a committed seal for the given hash
func PrepareCommittedSeal(header *types.Header, round uint32) []byte {
h := types.CopyHeader(header)
return h.QBFTHashWithRoundNumber(round).Bytes()
}
func (e *Engine) WriteVote(header *types.Header, candidate common.Address, authorize bool) error {
return ApplyHeaderQBFTExtra(
header,
WriteVote(candidate, authorize),
)
}
func WriteVote(candidate common.Address, authorize bool) ApplyQBFTExtra {
return func(qbftExtra *types.QBFTExtra) error {
voteType := types.QBFTDropVote
if authorize {
voteType = types.QBFTAuthVote
}
vote := &types.ValidatorVote{RecipientAddress: candidate, VoteType: voteType}
qbftExtra.Vote = vote
return nil
}
}
func (e *Engine) ReadVote(header *types.Header) (candidate common.Address, authorize bool, err error) {
qbftExtra, err := getExtra(header)
if err != nil {
return common.Address{}, false, err
}
var vote *types.ValidatorVote
if qbftExtra.Vote == nil {
vote = &types.ValidatorVote{RecipientAddress: common.Address{}, VoteType: types.QBFTDropVote}
} else {
vote = qbftExtra.Vote
}
// Tally up the new vote from the validator
switch {
case vote.VoteType == types.QBFTAuthVote:
authorize = true
case vote.VoteType == types.QBFTDropVote:
authorize = false
default:
return common.Address{}, false, istanbulcommon.ErrInvalidVote
}
return vote.RecipientAddress, authorize, nil
}
func getExtra(header *types.Header) (*types.QBFTExtra, error) {
if len(header.Extra) < types.IstanbulExtraVanity {
// In this scenario, the header extradata only contains client specific information, hence create a new qbftExtra and set vanity
vanity := append(header.Extra, bytes.Repeat([]byte{0x00}, types.IstanbulExtraVanity-len(header.Extra))...)
return &types.QBFTExtra{
VanityData: vanity,
Validators: []common.Address{},
CommittedSeal: [][]byte{},
Round: 0,
Vote: nil,
}, nil
}
// This is the case when Extra has already been set
return types.ExtractQBFTExtra(header)
}
func setExtra(h *types.Header, qbftExtra *types.QBFTExtra) error {
payload, err := rlp.EncodeToBytes(qbftExtra)
if err != nil {
return err
}
h.Extra = payload
return nil
}
// AccumulateRewards credits the beneficiary of the given block with a reward.
func (e *Engine) accumulateRewards(chain consensus.ChainHeaderReader, state *state.StateDB, header *types.Header) {
blockReward := chain.Config().GetBlockReward(header.Number)
if blockReward.Cmp(big.NewInt(0)) > 0 {
coinbase := header.Coinbase
if (coinbase == common.Address{}) {
coinbase = e.signer
}
rewardAccount, _ := chain.Config().GetRewardAccount(header.Number, coinbase)
log.Trace("QBFT: accumulate rewards to", "rewardAccount", rewardAccount, "blockReward", blockReward)
state.AddBalance(rewardAccount, &blockReward)
}
}