Skip to content

Commit

Permalink
consensus/istanbul: refactor backend verify function
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Chen authored and markya0616 committed Jul 17, 2017
1 parent b8654b7 commit d1680d5
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion consensus/istanbul/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Backend interface {

// Verify verifies the proposal. If a consensus.ErrFutureBlock error is returned,
// the time difference of the proposal and current time is also returned.
Verify(Proposal) (error, time.Duration)
Verify(Proposal) (time.Duration, error)

// Sign signs input data with the backend's private key
Sign([]byte) ([]byte, error)
Expand Down
10 changes: 5 additions & 5 deletions consensus/istanbul/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,23 +182,23 @@ func (sb *backend) EventMux() *event.TypeMux {
}

// Verify implements istanbul.Backend.Verify
func (sb *backend) Verify(proposal istanbul.Proposal) (error, time.Duration) {
func (sb *backend) Verify(proposal istanbul.Proposal) (time.Duration, error) {
// Check if the proposal is a valid block
block := &types.Block{}
block, ok := proposal.(*types.Block)
if !ok {
sb.logger.Error("Invalid proposal, %v", proposal)
return errInvalidProposal, 0
return 0, errInvalidProposal
}
// verify the header of proposed block
err := sb.VerifyHeader(sb.chain, block.Header(), false)
// ignore errEmptyCommittedSeals error because we don't have the committed seals yet
if err == nil || err == errEmptyCommittedSeals {
return nil, 0
return 0, nil
} else if err == consensus.ErrFutureBlock {
return consensus.ErrFutureBlock, time.Unix(block.Header().Time.Int64(), 0).Sub(now())
return time.Unix(block.Header().Time.Int64(), 0).Sub(now()), consensus.ErrFutureBlock
}
return err, 0
return 0, err
}

// Sign implements istanbul.Backend.Sign
Expand Down
2 changes: 1 addition & 1 deletion consensus/istanbul/core/preprepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (c *core) handlePreprepare(msg *message, src istanbul.Validator) error {
}

// Verify the proposal we received
if err, duration := c.backend.Verify(preprepare.Proposal); err != nil {
if duration, err := c.backend.Verify(preprepare.Proposal); err != nil {
logger.Warn("Failed to verify proposal", "err", err, "duration", duration)
// if it's a future block, we will handle it again after the duration
if err == consensus.ErrFutureBlock {
Expand Down
4 changes: 2 additions & 2 deletions consensus/istanbul/core/testbackend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func (self *testSystemBackend) Commit(proposal istanbul.Proposal, seals []byte)
return nil
}

func (self *testSystemBackend) Verify(proposal istanbul.Proposal) (error, time.Duration) {
return nil, 0
func (self *testSystemBackend) Verify(proposal istanbul.Proposal) (time.Duration, error) {
return 0, nil
}

func (self *testSystemBackend) Sign(data []byte) ([]byte, error) {
Expand Down

0 comments on commit d1680d5

Please sign in to comment.