-
Notifications
You must be signed in to change notification settings - Fork 1
/
engine.go
758 lines (673 loc) · 24.9 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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package backend
import (
"bytes"
"errors"
"math/big"
"math/rand"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/istanbul"
istanbulCore "github.com/ethereum/go-ethereum/consensus/istanbul/core"
"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/rlp"
"github.com/ethereum/go-ethereum/rpc"
lru "github.com/hashicorp/golang-lru"
"golang.org/x/crypto/sha3"
)
const (
checkpointInterval = 1024 // Number of blocks after which to save the vote snapshot to the database
inmemorySnapshots = 128 // Number of recent vote snapshots to keep in memory
inmemoryPeers = 40
inmemoryMessages = 1024
)
var (
// errInvalidProposal is returned when a prposal is malformed.
errInvalidProposal = errors.New("invalid proposal")
// errInvalidSignature is returned when given signature is not signed by given
// address.
errInvalidSignature = errors.New("invalid signature")
// errUnknownBlock is returned when the list of validators is requested for a block
// that is not part of the local blockchain.
errUnknownBlock = errors.New("unknown block")
// errUnauthorized is returned if a header is signed by a non authorized entity.
errUnauthorized = errors.New("unauthorized")
// errInvalidDifficulty is returned if the difficulty of a block is not 1
errInvalidDifficulty = errors.New("invalid difficulty")
// errInvalidExtraDataFormat is returned when the extra data format is incorrect
errInvalidExtraDataFormat = errors.New("invalid extra data format")
// errInvalidMixDigest is returned if a block's mix digest is not Istanbul digest.
errInvalidMixDigest = errors.New("invalid Istanbul mix digest")
// errInvalidNonce is returned if a block's nonce is invalid
errInvalidNonce = errors.New("invalid nonce")
// errInvalidUncleHash is returned if a block contains an non-empty uncle list.
errInvalidUncleHash = errors.New("non empty uncle hash")
// errInconsistentValidatorSet is returned if the validator set is inconsistent
// errInconsistentValidatorSet = errors.New("non empty uncle hash")
// 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")
// errInvalidVotingChain is returned if an authorization list is attempted to
// be modified via out-of-range or non-contiguous headers.
errInvalidVotingChain = errors.New("invalid voting chain")
// errInvalidVote is returned if a nonce value is something else that the two
// allowed constants of 0x00..0 or 0xff..f.
errInvalidVote = errors.New("vote nonce not 0x00..0 or 0xff..f")
// errInvalidCommittedSeals is returned if the committed seal is not signed by any of parent validators.
errInvalidCommittedSeals = errors.New("invalid committed seals")
// errEmptyCommittedSeals is returned if the field of committed seals is zero.
errEmptyCommittedSeals = errors.New("zero committed seals")
// errMismatchTxhashes is returned if the TxHash in header is mismatch.
errMismatchTxhashes = errors.New("mismatch transactions hashes")
)
var (
defaultDifficulty = big.NewInt(1)
nilUncleHash = types.CalcUncleHash(nil) // Always Keccak256(RLP([])) as uncles are meaningless outside of PoW.
emptyNonce = types.BlockNonce{}
now = time.Now
nonceAuthVote = hexutil.MustDecode("0xffffffffffffffff") // Magic nonce number to vote on adding a new validator
nonceDropVote = hexutil.MustDecode("0x0000000000000000") // Magic nonce number to vote on removing a validator.
inmemoryAddresses = 20 // Number of recent addresses from ecrecover
recentAddresses, _ = lru.NewARC(inmemoryAddresses)
)
// Author retrieves the Ethereum address of the account that minted the given
// block, which may be different from the header's coinbase if a consensus
// engine is based on signatures.
func (sb *backend) Author(header *types.Header) (common.Address, error) {
return ecrecover(header)
}
// Signers extracts all the addresses who have signed the given header
// It will extract for each seal who signed it, regardless of if the seal is
// repeated
func (sb *backend) Signers(header *types.Header) ([]common.Address, error) {
extra, err := types.ExtractIstanbulExtra(header)
if err != nil {
return []common.Address{}, err
}
var addrs []common.Address
proposalSeal := istanbulCore.PrepareCommittedSeal(header.Hash())
// 1. Get committed seals from current header
for _, seal := range extra.CommittedSeal {
// 2. Get the original address by seal and parent block hash
addr, err := istanbul.GetSignatureAddress(proposalSeal, seal)
if err != nil {
sb.logger.Error("not a valid address", "err", err)
return nil, errInvalidSignature
}
addrs = append(addrs, addr)
}
return addrs, nil
}
// VerifyHeader checks whether a header conforms to the consensus rules of a
// given engine. Verifying the seal may be done optionally here, or explicitly
// via the VerifySeal method.
func (sb *backend) VerifyHeader(chain consensus.ChainReader, header *types.Header, seal bool) error {
return sb.verifyHeader(chain, header, nil)
}
// 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 (sb *backend) verifyHeader(chain consensus.ChainReader, header *types.Header, parents []*types.Header) error {
if header.Number == nil {
return errUnknownBlock
}
// Don't waste time checking blocks from the future (adjusting for allowed threshold)
adjustedTimeNow := now().Add(time.Duration(sb.config.AllowedFutureBlockTime) * time.Second).Unix()
if header.Time > uint64(adjustedTimeNow) {
return consensus.ErrFutureBlock
}
// Ensure that the extra data format is satisfied
if _, err := types.ExtractIstanbulExtra(header); err != nil {
return errInvalidExtraDataFormat
}
// Ensure that the coinbase is valid
if header.Nonce != (emptyNonce) && !bytes.Equal(header.Nonce[:], nonceAuthVote) && !bytes.Equal(header.Nonce[:], nonceDropVote) {
return errInvalidNonce
}
// Ensure that the mix digest is zero as we don't have fork protection currently
if header.MixDigest != types.IstanbulDigest {
return errInvalidMixDigest
}
// Ensure that the block doesn't contain any uncles which are meaningless in Istanbul
if header.UncleHash != nilUncleHash {
return errInvalidUncleHash
}
// Ensure that the block's difficulty is meaningful (may not be correct at this point)
if header.Difficulty == nil || header.Difficulty.Cmp(defaultDifficulty) != 0 {
return errInvalidDifficulty
}
return sb.verifyCascadingFields(chain, header, parents)
}
// 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 (sb *backend) verifyCascadingFields(chain consensus.ChainReader, header *types.Header, parents []*types.Header) error {
// The genesis block is the always valid dead-end
number := header.Number.Uint64()
if number == 0 {
return nil
}
// Ensure that the block's timestamp isn't too close to it's parent
var parent *types.Header
if len(parents) > 0 {
parent = parents[len(parents)-1]
} else {
parent = chain.GetHeader(header.ParentHash, number-1)
}
if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash {
return consensus.ErrUnknownAncestor
}
if parent.Time+sb.config.BlockPeriod > header.Time {
return errInvalidTimestamp
}
// Verify validators in extraData. Validators in snapshot and extraData should be the same.
snap, err := sb.snapshot(chain, number-1, header.ParentHash, parents)
if err != nil {
return err
}
validators := make([]byte, len(snap.validators())*common.AddressLength)
for i, validator := range snap.validators() {
copy(validators[i*common.AddressLength:], validator[:])
}
if err := sb.verifySigner(chain, header, parents); err != nil {
return err
}
return sb.verifyCommittedSeals(chain, header, parents)
}
// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
// concurrently. 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 (sb *backend) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, seals []bool) (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 = sb.verifyHeader(chain, header, headers[:i])
}
if err != nil {
errored = true
}
select {
case <-abort:
return
case results <- err:
}
}
}()
return abort, results
}
// VerifyUncles verifies that the given block's uncles conform to the consensus
// rules of a given engine.
func (sb *backend) VerifyUncles(chain consensus.ChainReader, block *types.Block) error {
if len(block.Uncles()) > 0 {
return errInvalidUncleHash
}
return nil
}
// verifySigner checks whether the signer is in parent's validator set
func (sb *backend) verifySigner(chain consensus.ChainReader, header *types.Header, parents []*types.Header) error {
// Verifying the genesis block is not supported
number := header.Number.Uint64()
if number == 0 {
return errUnknownBlock
}
// Retrieve the snapshot needed to verify this header and cache it
snap, err := sb.snapshot(chain, number-1, header.ParentHash, parents)
if err != nil {
return err
}
// resolve the authorization key and check against signers
signer, err := ecrecover(header)
if err != nil {
return err
}
// Signer should be in the validator set of previous block's extraData.
if _, v := snap.ValSet.GetByAddress(signer); v == nil {
return errUnauthorized
}
return nil
}
// verifyCommittedSeals checks whether every committed seal is signed by one of the parent's validators
func (sb *backend) verifyCommittedSeals(chain consensus.ChainReader, header *types.Header, parents []*types.Header) error {
number := header.Number.Uint64()
// We don't need to verify committed seals in the genesis block
if number == 0 {
return nil
}
// Retrieve the snapshot needed to verify this header and cache it
snap, err := sb.snapshot(chain, number-1, header.ParentHash, parents)
if err != nil {
return err
}
extra, err := types.ExtractIstanbulExtra(header)
if err != nil {
return err
}
// The length of Committed seals should be larger than 0
if len(extra.CommittedSeal) == 0 {
return errEmptyCommittedSeals
}
validators := snap.ValSet.Copy()
// Check whether the committed seals are generated by parent's validators
validSeal := 0
committers, err := sb.Signers(header)
if err != nil {
return err
}
for _, addr := range committers {
if validators.RemoveValidator(addr) {
validSeal++
continue
}
return errInvalidCommittedSeals
}
// The length of validSeal should be larger than number of faulty node + 1
if validSeal <= snap.ValSet.F() {
return errInvalidCommittedSeals
}
return nil
}
// VerifySeal checks whether the crypto seal on a header is valid according to
// the consensus rules of the given engine.
func (sb *backend) VerifySeal(chain consensus.ChainReader, header *types.Header) error {
// get parent header and ensure the signer is in parent's validator set
number := header.Number.Uint64()
if number == 0 {
return errUnknownBlock
}
// ensure that the difficulty equals to defaultDifficulty
if header.Difficulty.Cmp(defaultDifficulty) != 0 {
return errInvalidDifficulty
}
return sb.verifySigner(chain, header, nil)
}
// Prepare initializes the consensus fields of a block header according to the
// rules of a particular engine. The changes are executed inline.
func (sb *backend) Prepare(chain consensus.ChainReader, header *types.Header) error {
// unused fields, force to set to empty
header.Coinbase = common.Address{}
header.Nonce = emptyNonce
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 = defaultDifficulty
// Assemble the voting snapshot
snap, err := sb.snapshot(chain, number-1, header.ParentHash, nil)
if err != nil {
return err
}
// get valid candidate list
sb.candidatesLock.RLock()
var addresses []common.Address
var authorizes []bool
for address, authorize := range sb.candidates {
if snap.checkVote(address, authorize) {
addresses = append(addresses, address)
authorizes = append(authorizes, authorize)
}
}
sb.candidatesLock.RUnlock()
// pick one of the candidates randomly
if len(addresses) > 0 {
index := rand.Intn(len(addresses))
// add validator voting in coinbase
header.Coinbase = addresses[index]
if authorizes[index] {
copy(header.Nonce[:], nonceAuthVote)
} else {
copy(header.Nonce[:], nonceDropVote)
}
}
// add validators in snapshot to extraData's validators section
extra, err := prepareExtra(header, snap.validators())
if err != nil {
return err
}
header.Extra = extra
// set header's timestamp
header.Time = parent.Time + sb.config.BlockPeriod
if header.Time < uint64(time.Now().Unix()) {
header.Time = uint64(time.Now().Unix())
}
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 (sb *backend) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
uncles []*types.Header) {
// No block rewards in Istanbul, so the state remains as is and uncles are dropped
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 (sb *backend) FinalizeAndAssemble(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
/// No block rewards in Istanbul, so the state remains as is and uncles are dropped
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
header.UncleHash = nilUncleHash
// Assemble and return the final block for sealing
return types.NewBlock(header, txs, nil, receipts), nil
}
// Seal generates a new block for the given input block with the local miner's
// seal place on top.
func (sb *backend) Seal(chain consensus.ChainReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
// update the block header timestamp and signature and propose the block to core engine
header := block.Header()
number := header.Number.Uint64()
// Bail out if we're unauthorized to sign a block
snap, err := sb.snapshot(chain, number-1, header.ParentHash, nil)
if err != nil {
return err
}
if _, v := snap.ValSet.GetByAddress(sb.address); v == nil {
return errUnauthorized
}
parent := chain.GetHeader(header.ParentHash, number-1)
if parent == nil {
return consensus.ErrUnknownAncestor
}
block, err = sb.updateBlock(parent, block)
if err != nil {
return err
}
delay := time.Unix(int64(block.Header().Time), 0).Sub(now())
go func() {
// wait for the timestamp of header, use this to adjust the block period
select {
case <-time.After(delay):
case <-stop:
results <- nil
return
}
// get the proposed block hash and clear it if the seal() is completed.
sb.sealMu.Lock()
sb.proposedBlockHash = block.Hash()
defer func() {
sb.proposedBlockHash = common.Hash{}
sb.sealMu.Unlock()
}()
// post block into Istanbul engine
go sb.EventMux().Post(istanbul.RequestEvent{
Proposal: block,
})
for {
select {
case result := <-sb.commitCh:
// if the block hash and the hash from channel are the same,
// return the result. Otherwise, keep waiting the next hash.
if result != nil && block.Hash() == result.Hash() {
results <- result
return
}
case <-stop:
results <- nil
return
}
}
}()
return nil
}
// update timestamp and signature of the block based on its number of transactions
func (sb *backend) updateBlock(parent *types.Header, block *types.Block) (*types.Block, error) {
header := block.Header()
// sign the hash
seal, err := sb.Sign(sigHash(header).Bytes())
if err != nil {
return nil, err
}
err = writeSeal(header, seal)
if err != nil {
return nil, err
}
return block.WithSeal(header), nil
}
// APIs returns the RPC APIs this consensus engine provides.
func (sb *backend) APIs(chain consensus.ChainReader) []rpc.API {
return []rpc.API{{
Namespace: "istanbul",
Version: "1.0",
Service: &API{chain: chain, istanbul: sb},
Public: true,
}}
}
// Start implements consensus.Istanbul.Start
func (sb *backend) Start(chain consensus.ChainReader, currentBlock func() *types.Block, hasBadBlock func(hash common.Hash) bool) error {
sb.coreMu.Lock()
defer sb.coreMu.Unlock()
if sb.coreStarted {
return istanbul.ErrStartedEngine
}
// clear previous data
sb.proposedBlockHash = common.Hash{}
if sb.commitCh != nil {
close(sb.commitCh)
}
sb.commitCh = make(chan *types.Block, 1)
sb.chain = chain
sb.currentBlock = currentBlock
sb.hasBadBlock = hasBadBlock
if err := sb.core.Start(); err != nil {
return err
}
sb.coreStarted = true
return nil
}
// Stop implements consensus.Istanbul.Stop
func (sb *backend) Stop() error {
sb.coreMu.Lock()
defer sb.coreMu.Unlock()
if !sb.coreStarted {
return istanbul.ErrStoppedEngine
}
if err := sb.core.Stop(); err != nil {
return err
}
sb.coreStarted = false
return nil
}
// snapshot retrieves the authorization snapshot at a given point in time.
func (sb *backend) snapshot(chain consensus.ChainReader, number uint64, hash common.Hash, parents []*types.Header) (*Snapshot, error) {
// Search for a snapshot in memory or on disk for checkpoints
var (
headers []*types.Header
snap *Snapshot
)
for snap == nil {
// If an in-memory snapshot was found, use that
if s, ok := sb.recents.Get(hash); ok {
snap = s.(*Snapshot)
break
}
// If an on-disk checkpoint snapshot can be found, use that
if number%checkpointInterval == 0 {
if s, err := loadSnapshot(sb.config.Epoch, sb.db, hash); err == nil {
log.Trace("Loaded voting snapshot form disk", "number", number, "hash", hash)
snap = s
break
}
}
// If we're at block zero, make a snapshot
if number == 0 {
genesis := chain.GetHeaderByNumber(0)
if err := sb.VerifyHeader(chain, genesis, false); err != nil {
return nil, err
}
istanbulExtra, err := types.ExtractIstanbulExtra(genesis)
if err != nil {
return nil, err
}
snap = newSnapshot(sb.config.Epoch, 0, genesis.Hash(), validator.NewSet(istanbulExtra.Validators, sb.config.ProposerPolicy))
if err := snap.store(sb.db); err != nil {
return nil, err
}
log.Trace("Stored genesis voting snapshot to disk")
break
}
// No snapshot for this header, gather the header and move backward
var header *types.Header
if len(parents) > 0 {
// If we have explicit parents, pick from there (enforced)
header = parents[len(parents)-1]
if header.Hash() != hash || header.Number.Uint64() != number {
return nil, consensus.ErrUnknownAncestor
}
parents = parents[:len(parents)-1]
} else {
// No explicit parents (or no more left), reach out to the database
header = chain.GetHeader(hash, number)
if header == nil {
return nil, consensus.ErrUnknownAncestor
}
}
headers = append(headers, header)
number, hash = number-1, header.ParentHash
}
// Previous snapshot found, apply any pending headers on top of it
for i := 0; i < len(headers)/2; i++ {
headers[i], headers[len(headers)-1-i] = headers[len(headers)-1-i], headers[i]
}
snap, err := snap.apply(headers)
if err != nil {
return nil, err
}
sb.recents.Add(snap.Hash, snap)
// If we've generated a new checkpoint snapshot, save to disk
if snap.Number%checkpointInterval == 0 && len(headers) > 0 {
if err = snap.store(sb.db); err != nil {
return nil, err
}
log.Trace("Stored voting snapshot to disk", "number", snap.Number, "hash", snap.Hash)
}
return snap, err
}
// 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()
// Clean seal is required for calculating proposer seal.
rlp.Encode(hasher, types.IstanbulFilteredHeader(header, false))
hasher.Sum(hash[:0])
return hash
}
// SealHash returns the hash of a block prior to it being sealed.
func (sb *backend) SealHash(header *types.Header) common.Hash {
return sigHash(header)
}
// ecrecover extracts the Ethereum account address from a signed header.
func ecrecover(header *types.Header) (common.Address, error) {
hash := header.Hash()
if addr, ok := recentAddresses.Get(hash); ok {
return addr.(common.Address), nil
}
// Retrieve the signature from the header extra-data
istanbulExtra, err := types.ExtractIstanbulExtra(header)
if err != nil {
return common.Address{}, err
}
addr, err := istanbul.GetSignatureAddress(sigHash(header).Bytes(), istanbulExtra.Seal)
if err != nil {
return addr, err
}
recentAddresses.Add(hash, addr)
return addr, nil
}
// prepareExtra returns a extra-data of the given header and validators
func prepareExtra(header *types.Header, vals []common.Address) ([]byte, error) {
var buf bytes.Buffer
// compensate the lack bytes if header.Extra is not enough IstanbulExtraVanity bytes.
if len(header.Extra) < types.IstanbulExtraVanity {
header.Extra = append(header.Extra, bytes.Repeat([]byte{0x00}, types.IstanbulExtraVanity-len(header.Extra))...)
}
buf.Write(header.Extra[:types.IstanbulExtraVanity])
ist := &types.IstanbulExtra{
Validators: vals,
Seal: []byte{},
CommittedSeal: [][]byte{},
}
payload, err := rlp.EncodeToBytes(&ist)
if err != nil {
return nil, err
}
return append(buf.Bytes(), payload...), nil
}
// writeSeal writes the extra-data field of the given header with the given seals.
// suggest to rename to writeSeal.
func writeSeal(h *types.Header, seal []byte) error {
if len(seal)%types.IstanbulExtraSeal != 0 {
return errInvalidSignature
}
istanbulExtra, err := types.ExtractIstanbulExtra(h)
if err != nil {
return err
}
istanbulExtra.Seal = seal
payload, err := rlp.EncodeToBytes(&istanbulExtra)
if err != nil {
return err
}
h.Extra = append(h.Extra[:types.IstanbulExtraVanity], payload...)
return nil
}
// writeCommittedSeals writes the extra-data field of a block header with given committed seals.
func writeCommittedSeals(h *types.Header, committedSeals [][]byte) error {
if len(committedSeals) == 0 {
return errInvalidCommittedSeals
}
for _, seal := range committedSeals {
if len(seal) != types.IstanbulExtraSeal {
return errInvalidCommittedSeals
}
}
istanbulExtra, err := types.ExtractIstanbulExtra(h)
if err != nil {
return err
}
istanbulExtra.CommittedSeal = make([][]byte, len(committedSeals))
copy(istanbulExtra.CommittedSeal, committedSeals)
payload, err := rlp.EncodeToBytes(&istanbulExtra)
if err != nil {
return err
}
h.Extra = append(h.Extra[:types.IstanbulExtraVanity], payload...)
return nil
}