@@ -27,13 +27,12 @@ import (
27
27
"github.com/ethereum/go-ethereum/core"
28
28
"github.com/ethereum/go-ethereum/core/types"
29
29
"github.com/ethereum/go-ethereum/crypto"
30
- "github.com/ethereum/go-ethereum/crypto/sha3"
31
30
"github.com/ethereum/go-ethereum/ethdb"
32
31
"github.com/ethereum/go-ethereum/event"
33
32
"github.com/ethereum/go-ethereum/log"
34
- "github.com/ethereum/go-ethereum/rlp"
35
33
)
36
34
35
+ // New creates an Ethereum backend for PBFT core engine.
37
36
func New (config * pbft.Config , eventMux * event.TypeMux , privateKey * ecdsa.PrivateKey , db ethdb.Database ) consensus.PBFT {
38
37
backend := & simpleBackend {
39
38
config : config ,
@@ -50,6 +49,7 @@ func New(config *pbft.Config, eventMux *event.TypeMux, privateKey *ecdsa.Private
50
49
}
51
50
52
51
// ----------------------------------------------------------------------------
52
+
53
53
type simpleBackend struct {
54
54
config * pbft.Config
55
55
peerSet * peerSet
@@ -116,15 +116,15 @@ func (sb *simpleBackend) Broadcast(payload []byte) error {
116
116
117
117
// Commit implements pbft.Backend.Commit
118
118
func (sb * simpleBackend ) Commit (proposal pbft.Proposal ) error {
119
- sb .logger .Info ("Committed" , "address" , sb .Address ().Hex (), "hash" , proposal .Hash (), "number" , proposal .Number ().Uint64 ())
120
- // step1: update validator set from extra data of block
121
- // step2: insert chain
119
+ // Check if the proposal is a valid block
122
120
block := & types.Block {}
123
121
block , ok := proposal .(* types.Block )
124
122
if ! ok {
125
- sb .logger .Error ("Failed to commit proposal since RequestContext cannot cast to *types.Block" )
126
- return errCastingRequest
123
+ sb .logger .Error ("Invalid proposal, %v" , proposal )
124
+ return errInvalidProposal
127
125
}
126
+
127
+ sb .logger .Info ("Committed" , "address" , sb .Address (), "hash" , proposal .Hash (), "number" , proposal .Number ().Uint64 ())
128
128
// - if the proposed and committed blocks are the same, send the proposed hash
129
129
// to commit channel, which is being watched inside the engine.Seal() function.
130
130
// - otherwise, we try to insert the block.
@@ -143,33 +143,24 @@ func (sb *simpleBackend) Commit(proposal pbft.Proposal) error {
143
143
// NextRound will broadcast ChainHeadEvent to trigger next seal()
144
144
func (sb * simpleBackend ) NextRound () error {
145
145
header := sb .chain .CurrentHeader ()
146
- sb .logger .Debug ("NextRound" , "address" , sb .Address (). Hex () , "current_hash" , header .Hash (), "current_number" , header .Number )
146
+ sb .logger .Debug ("NextRound" , "address" , sb .Address (), "current_hash" , header .Hash (), "current_number" , header .Number )
147
147
go sb .eventMux .Post (core.ChainHeadEvent {})
148
148
return nil
149
149
}
150
150
151
- // Hash implements pbft.Backend.Hash
152
- func (sb * simpleBackend ) Hash (x interface {}) (h common.Hash ) {
153
- hw := sha3 .NewKeccak256 ()
154
- rlp .Encode (hw , x )
155
- hw .Sum (h [:0 ])
156
- return h
157
- }
158
-
159
151
// EventMux implements pbft.Backend.EventMux
160
152
func (sb * simpleBackend ) EventMux () * event.TypeMux {
161
- // not implemented
162
153
return sb .pbftEventMux
163
154
}
164
155
165
156
// Verify implements pbft.Backend.Verify
166
157
func (sb * simpleBackend ) Verify (proposal pbft.Proposal ) error {
167
- // decode the proposal to block
158
+ // Check if the proposal is a valid block
168
159
block := & types.Block {}
169
160
block , ok := proposal .(* types.Block )
170
161
if ! ok {
171
- sb .logger .Error ("Failed to commit proposal since RequestContext cannot cast to *types.Block" )
172
- return errCastingRequest
162
+ sb .logger .Error ("Invalid proposal, %v" , proposal )
163
+ return errInvalidProposal
173
164
}
174
165
// verify the header of proposed block
175
166
return sb .VerifyHeader (sb .chain , block .Header (), false )
@@ -185,12 +176,12 @@ func (sb *simpleBackend) Sign(data []byte) ([]byte, error) {
185
176
func (sb * simpleBackend ) CheckSignature (data []byte , address common.Address , sig []byte ) error {
186
177
signer , err := sb .getSignatureAddress (data , sig )
187
178
if err != nil {
188
- log .Error ("CheckSignature " , "error " , err )
179
+ log .Error ("Failed to get signer address " , "err " , err )
189
180
return err
190
181
}
191
- //Compare derived addresses
182
+ // Compare derived addresses
192
183
if signer != address {
193
- return pbft . ErrInvalidSignature
184
+ return errInvalidSignature
194
185
}
195
186
return nil
196
187
}
@@ -200,7 +191,7 @@ func (sb *simpleBackend) CheckValidatorSignature(data []byte, sig []byte) (commo
200
191
// 1. Get signature address
201
192
signer , err := sb .getSignatureAddress (data , sig )
202
193
if err != nil {
203
- log .Error ("CheckValidatorSignature " , "error " , err )
194
+ log .Error ("Failed to get signer address " , "err " , err )
204
195
return common.Address {}, err
205
196
}
206
197
@@ -209,14 +200,14 @@ func (sb *simpleBackend) CheckValidatorSignature(data []byte, sig []byte) (commo
209
200
return val .Address (), nil
210
201
}
211
202
212
- return common.Address {}, pbft .ErrNoMatchingValidator
203
+ return common.Address {}, pbft .ErrUnauthorizedAddress
213
204
}
214
205
215
206
// get the signer address from the signature
216
207
func (sb * simpleBackend ) getSignatureAddress (data []byte , sig []byte ) (common.Address , error ) {
217
- //1. Keccak data
208
+ // 1. Keccak data
218
209
hashData := crypto .Keccak256 ([]byte (data ))
219
- //2. Recover public key
210
+ // 2. Recover public key
220
211
pubkey , err := crypto .SigToPub (hashData , sig )
221
212
if err != nil {
222
213
return common.Address {}, err
0 commit comments