From 602f36a4b731c4da3cc7a43f1419e7c42efc30fd Mon Sep 17 00:00:00 2001 From: Dennis Won Date: Mon, 9 Sep 2019 19:21:19 -0700 Subject: [PATCH] fix incoming receipt tx block header verification by correct shard state reading --- cmd/harmony/main.go | 4 ++-- internal/chain/engine.go | 15 +++------------ node/node.go | 12 ++++++------ 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index 88018e169..226d060a5 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -364,8 +364,8 @@ func setupConsensusAndNode(nodeConfig *nodeconfig.ConfigType) *node.Node { // currentNode.DRand = dRand // This needs to be executed after consensus and drand are setup - if err := currentNode.InitShardState(); err != nil { - ctxerror.Crit(utils.GetLogger(), err, "InitShardState failed", + if err := currentNode.GetInitShardState(); err != nil { + ctxerror.Crit(utils.GetLogger(), err, "GetInitShardState failed", "shardID", *shardID) } diff --git a/internal/chain/engine.go b/internal/chain/engine.go index e082f3fd8..9d14710a4 100644 --- a/internal/chain/engine.go +++ b/internal/chain/engine.go @@ -11,6 +11,7 @@ import ( "github.com/harmony-one/harmony/block" "github.com/harmony-one/harmony/consensus/engine" + "github.com/harmony-one/harmony/core" "github.com/harmony-one/harmony/core/state" "github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/internal/ctxerror" @@ -139,12 +140,7 @@ func (e *engineImpl) VerifyHeaderWithSignature(chain engine.ChainReader, header // retrievePublicKeys finds the public keys of current block's committee func retrievePublicKeys(bc engine.ChainReader, header *block.Header) ([]*bls.PublicKey, error) { - shardState, err := bc.ReadShardState(header.Epoch()) - if err != nil { - return nil, ctxerror.New("cannot read shard state", - "epoch", header.Epoch(), - ).WithCause(err) - } + shardState := core.GetShardState(header.Epoch()) committee := shardState.FindCommitteeByID(header.ShardID()) if committee == nil { return nil, ctxerror.New("cannot find shard in the shard state", @@ -172,12 +168,7 @@ func retrievePublicKeysFromLastBlock(bc engine.ChainReader, header *block.Header return nil, ctxerror.New("cannot find parent block header in DB", "parentHash", header.ParentHash()) } - parentShardState, err := bc.ReadShardState(parentHeader.Epoch()) - if err != nil { - return nil, ctxerror.New("cannot read shard state", - "epoch", parentHeader.Epoch(), - ).WithCause(err) - } + parentShardState := core.GetShardState(parentHeader.Epoch()) parentCommittee := parentShardState.FindCommitteeByID(parentHeader.ShardID()) if parentCommittee == nil { return nil, ctxerror.New("cannot find shard in the shard state", diff --git a/node/node.go b/node/node.go index 1ea38b7ed..2350e2798 100644 --- a/node/node.go +++ b/node/node.go @@ -457,10 +457,10 @@ func New(host p2p.Host, consensusObj *consensus.Consensus, chainDBFactory shardc return &node } -// InitShardState initialize shard state from latest epoch and update committee pub keys for consensus and drand -func (node *Node) InitShardState() (err error) { +// GetInitShardState initialize shard state from latest epoch and update committee pub keys for consensus and drand +func (node *Node) GetInitShardState() (err error) { if node.Consensus == nil { - return ctxerror.New("[InitShardState] consenus is nil; Cannot figure out shardID") + return ctxerror.New("[GetInitShardState] consenus is nil; Cannot figure out shardID") } shardID := node.Consensus.ShardID @@ -472,11 +472,11 @@ func (node *Node) InitShardState() (err error) { Uint64("blockNum", blockNum). Uint32("shardID", shardID). Uint64("epoch", epoch.Uint64()). - Msg("[InitShardState] Try To Get PublicKeys from database") + Msg("[GetInitShardState] Try To Get PublicKeys from database") pubKeys := core.GetPublicKeys(epoch, shardID) if len(pubKeys) == 0 { return ctxerror.New( - "[InitShardState] PublicKeys is Empty, Cannot update public keys", + "[GetInitShardState] PublicKeys is Empty, Cannot update public keys", "shardID", shardID, "blockNum", blockNum) } @@ -486,7 +486,7 @@ func (node *Node) InitShardState() (err error) { utils.Logger().Info(). Uint64("blockNum", blockNum). Int("numPubKeys", len(pubKeys)). - Msg("[InitShardState] Successfully updated public keys") + Msg("[GetInitShardState] Successfully updated public keys") node.Consensus.UpdatePublicKeys(pubKeys) node.Consensus.SetMode(consensus.Normal) return nil