migrate to use chain reader instead of BlockVerifier

pull/510/head
Rongjian Lan 6 years ago
parent 562ae5078d
commit 4f578bd4ed
  1. 1
      cmd/harmony.go
  2. 18
      consensus/consensus.go
  3. 7
      consensus/consensus_validator.go
  4. 42
      consensus/consensus_validator_test.go

@ -290,7 +290,6 @@ func main() {
}
// Assign closure functions to the consensus object
consensus.BlockVerifier = currentNode.VerifyNewBlock
consensus.OnConsensusDone = currentNode.PostConsensusProcessing
currentNode.State = node.NodeWaitToJoin

@ -42,6 +42,9 @@ type Consensus struct {
prepareBitmap *bls_cosi.Mask
commitBitmap *bls_cosi.Mask
// The chain reader for the blockchain this consensus is working on
ChainReader consensus_engine.ChainReader
// map of nodeID to validator Peer object
// FIXME: should use PubKey of p2p.Peer as the hashkey
validators sync.Map // key is uint16, value is p2p.Peer
@ -85,8 +88,6 @@ type Consensus struct {
// Signal channel for starting a new consensus process
ReadySignal chan struct{}
// The verifier func passed from Node object
BlockVerifier func(*types.Block) bool
// The post-consensus processing func passed from Node object
// Called when consensus on a new block is done
OnConsensusDone func(*types.Block)
@ -521,10 +522,17 @@ func NewFaker() *Consensus {
return &Consensus{}
}
// VerifyHeader checks whether a header conforms to the consensus rules of the
// stock bft engine.
// VerifyHeader checks whether a header conforms to the consensus rules of the bft engine.
func (consensus *Consensus) VerifyHeader(chain consensus_engine.ChainReader, header *types.Header, seal bool) error {
// TODO: implement this
parentHeader := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1)
if parentHeader == nil {
return consensus_engine.ErrUnknownAncestor
}
if seal {
if err := consensus.VerifySeal(chain, header); err != nil {
return err
}
}
return nil
}

@ -115,8 +115,8 @@ func (consensus *Consensus) processAnnounceMessage(message *msg_pb.Message) {
}
// check block data transactions
if !consensus.BlockVerifier(&blockObj) {
utils.GetLogInstance().Warn("Block content is not verified successfully", "consensus", consensus)
if err := consensus.VerifyHeader(consensus.ChainReader, blockObj.Header(), false); err != nil {
utils.GetLogInstance().Warn("Block content is not verified successfully", "error", err)
return
}
@ -270,7 +270,8 @@ func (consensus *Consensus) processCommittedMessage(message *msg_pb.Message) {
utils.GetLogInstance().Debug("failed to construct the new block after consensus")
}
// check block data (transactions
if !consensus.BlockVerifier(&blockObj) {
// TODO: change to verify the block body
if err := consensus.VerifyHeader(consensus.ChainReader, blockObj.Header(), true); err != nil {
utils.GetLogInstance().Debug("[WARNING] Block content is not verified successfully", "consensusID", consensus.consensusID)
return
}

@ -5,6 +5,9 @@ import (
"testing"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/params"
"github.com/golang/mock/gomock"
protobuf "github.com/golang/protobuf/proto"
"github.com/harmony-one/harmony/api/proto"
@ -18,6 +21,33 @@ import (
"github.com/stretchr/testify/assert"
)
type MockChainReader struct{}
func (MockChainReader) Config() *params.ChainConfig {
return nil
}
func (MockChainReader) CurrentHeader() *types.Header {
return &types.Header{}
}
func (MockChainReader) GetHeader(hash common.Hash, number uint64) *types.Header {
return &types.Header{}
}
func (MockChainReader) GetHeaderByNumber(number uint64) *types.Header {
return &types.Header{}
}
func (MockChainReader) GetHeaderByHash(hash common.Hash) *types.Header {
return &types.Header{}
}
// GetBlock retrieves a block from the database by hash and number.
func (MockChainReader) GetBlock(hash common.Hash, number uint64) *types.Block {
return &types.Block{}
}
func TestProcessMessageValidatorAnnounce(test *testing.T) {
ctrl := gomock.NewController(test)
defer ctrl.Finish()
@ -62,9 +92,7 @@ func TestProcessMessageValidatorAnnounce(test *testing.T) {
}
consensusValidator1 := New(m, "0", []p2p.Peer{validator1, validator2, validator3}, leader)
consensusValidator1.BlockVerifier = func(block *types.Block) bool {
return true
}
consensusValidator1.ChainReader = MockChainReader{}
copy(consensusValidator1.blockHash[:], hashBytes[:])
consensusValidator1.processAnnounceMessage(message)
@ -112,9 +140,7 @@ func TestProcessMessageValidatorPrepared(test *testing.T) {
preparedMsg, _ := consensusLeader.constructPreparedMessage()
consensusValidator1 := New(m, "0", []p2p.Peer{validator1, validator2, validator3}, leader)
consensusValidator1.BlockVerifier = func(block *types.Block) bool {
return true
}
consensusValidator1.ChainReader = MockChainReader{}
// Get actual consensus messages.
announceMsg, err = proto.GetConsensusMessagePayload(announceMsg)
@ -202,9 +228,7 @@ func TestProcessMessageValidatorCommitted(test *testing.T) {
}
consensusValidator1 := New(m, "0", []p2p.Peer{validator1, validator2, validator3}, leader)
consensusValidator1.BlockVerifier = func(block *types.Block) bool {
return true
}
consensusValidator1.ChainReader = MockChainReader{}
consensusValidator1.OnConsensusDone = func(newBlock *types.Block) {}
if err = protobuf.Unmarshal(announceMsg, message); err != nil {

Loading…
Cancel
Save