-
Notifications
You must be signed in to change notification settings - Fork 199
/
consensus.go
228 lines (173 loc) · 8.91 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
// 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 consensus implements different Ethereum consensus engines.
package consensus
import (
"math/big"
"github.com/celo-org/celo-blockchain/common"
"github.com/celo-org/celo-blockchain/consensus/istanbul"
"github.com/celo-org/celo-blockchain/core/state"
"github.com/celo-org/celo-blockchain/core/types"
"github.com/celo-org/celo-blockchain/core/vm"
"github.com/celo-org/celo-blockchain/ethdb"
"github.com/celo-org/celo-blockchain/p2p"
"github.com/celo-org/celo-blockchain/params"
"github.com/celo-org/celo-blockchain/rpc"
)
// ChainHeaderReader defines a small collection of methods needed to access the local
// blockchain during header verification.
type ChainHeaderReader interface {
// Config retrieves the blockchain's chain configuration.
Config() *params.ChainConfig
// CurrentHeader retrieves the current header from the local chain.
CurrentHeader() *types.Header
// GetHeader retrieves a block header from the database by hash and number.
GetHeader(hash common.Hash, number uint64) *types.Header
// GetHeaderByNumber retrieves a block header from the database by number.
GetHeaderByNumber(number uint64) *types.Header
// GetHeaderByHash retrieves a block header from the database by its hash.
GetHeaderByHash(hash common.Hash) *types.Header
}
// ChainContext defines a small collection of methods needed to access the local
// blockchain
type ChainContext interface {
ChainHeaderReader
// NewEVMRunnerForCurrentBlock creates the System's EVMRunner for current block & state
NewEVMRunnerForCurrentBlock() (vm.EVMRunner, error)
// NewEVMRunner creates the System's EVMRunner for given header & sttate
NewEVMRunner(header *types.Header, state vm.StateDB) vm.EVMRunner
}
// Engine is an algorithm agnostic consensus engine.
type Engine interface {
// 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.
Author(header *types.Header) (common.Address, error)
// 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.
VerifyHeader(chain ChainHeaderReader, header *types.Header, seal bool) error
// 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).
VerifyHeaders(chain ChainHeaderReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error)
// VerifySeal checks whether the crypto seal on a header is valid according to
// the consensus rules of the given engine.
VerifySeal(header *types.Header) error
// Prepare initializes the consensus fields of a block header according to the
// rules of a particular engine. The changes are executed inline.
Prepare(chain ChainHeaderReader, header *types.Header) error
// Finalize runs any post-transaction state modifications (e.g. block rewards)
// but does not assemble the block.
//
// Note: The block header and state database might be updated to reflect any
// consensus rules that happen at finalization (e.g. block rewards).
Finalize(chain ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction)
// FinalizeAndAssemble 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).
FinalizeAndAssemble(chain ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, receipts []*types.Receipt, randomness *types.Randomness) (*types.Block, error)
// Seal generates a new sealing request for the given input block.
//
// Note: The engine will insert the resulting block.
Seal(chain ChainHeaderReader, block *types.Block) error
// GetValidators returns the list of current validators.
GetValidators(blockNumber *big.Int, headerHash common.Hash) []istanbul.Validator
EpochSize() uint64
// APIs returns the RPC APIs this consensus engine provides.
APIs(chain ChainHeaderReader) []rpc.API
// Close terminates any background threads maintained by the consensus engine.
Close() error
}
type Genesis interface {
GetAlloc() GenesisAlloc
UnmarshalFromDB(db ethdb.Database) error
}
type GenesisAlloc map[common.Address]GenesisAccount
type GenesisAccount interface {
GetPublicKey() []byte
}
// Handler should be implemented if the consensus needs to handle and send peer messages
type Handler interface {
// NewWork handles a new work event from the miner
NewWork() error
// HandleMsg handles a message from peer
HandleMsg(address common.Address, data p2p.Msg, peer Peer) (bool, error)
// SetBroadcaster sets the broadcaster to send message to peers
SetBroadcaster(Broadcaster)
// SetP2PServer sets the p2p server to connect/disconnect to/from peers
SetP2PServer(P2PServer)
// RegisterPeer will notify the consensus engine that a new peer has been added
RegisterPeer(peer Peer, fromProxiedNode bool) error
// UnregisterPeer will notify the consensus engine that a new peer has been removed
UnregisterPeer(peer Peer, fromProxiedNode bool)
// Handshake will begin a handshake with a new peer. It returns if the peer
// has identified itself as a validator and should bypass any max peer checks.
Handshake(peer Peer) (bool, error)
}
// PoW is a consensus engine based on proof-of-work.
type PoW interface {
Engine
// Hashrate returns the current mining hashrate of a PoW consensus engine.
Hashrate() float64
}
// Istanbul is a consensus engine to avoid byzantine failure
type Istanbul interface {
Engine
// IsProxiedValidator returns true if this node is a proxied validator
IsProxiedValidator() bool
// IsProxy returns true if this node is a proxy
IsProxy() bool
// IsPrimary returns true if this node is the primary validator
IsPrimary() bool
// IsPrimaryForSeq returns true if this node is the primary validator for the sequence
IsPrimaryForSeq(seq *big.Int) bool
// SetChain injects the blockchain and related functions to the istanbul consensus engine
SetChain(chain ChainContext, currentBlock func() *types.Block, stateAt func(common.Hash) (*state.StateDB, error))
// SetCallBacks sets call back functions
SetCallBacks(hasBadBlock func(common.Hash) bool,
processBlock func(*types.Block, *state.StateDB) (types.Receipts, []*types.Log, uint64, error),
validateState func(*types.Block, *state.StateDB, types.Receipts, uint64) error,
onNewConsensusBlock func(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB)) error
// StartValidating starts the validating engine
StartValidating() error
// StopValidating stops the validating engine
StopValidating() error
// StartAnnouncing starts the announcing
StartAnnouncing() error
// StopAnnouncing stops the announcing
StopAnnouncing() error
// StartProxiedValidatorEngine starts the proxied validator engine
StartProxiedValidatorEngine() error
// StopProxiedValidatorEngine stops the proxied validator engine
StopProxiedValidatorEngine() error
// UpdateValSetDiff will update the validator set diff in the header, if the mined header is the last block of the epoch.
// The changes are executed inline.
UpdateValSetDiff(chain ChainHeaderReader, header *types.Header, state *state.StateDB) error
// OnBlockInsertion is a hook method called when system is inserting a block to the chain
OnBlockInsertion(header *types.Header, state *state.StateDB) error
// IsLastBlockOfEpoch will check to see if the header is from the last block of an epoch
IsLastBlockOfEpoch(header *types.Header) bool
// LookbackWindow returns the size of the lookback window for calculating uptime (in blocks)
LookbackWindow(header *types.Header, state *state.StateDB) uint64
// ValidatorAddress will return the istanbul engine's validator address
ValidatorAddress() common.Address
// GenerateRandomness will generate the random beacon randomness
GenerateRandomness(parentHash common.Hash) (common.Hash, common.Hash, error)
}