From cd81d2f52fd0a08683bab1e9ea25e6d8448abf5e Mon Sep 17 00:00:00 2001 From: flicker-harmony Date: Wed, 28 Aug 2019 20:18:42 +0300 Subject: [PATCH 1/3] Fix block signers --- api/service/explorer/service.go | 20 ++++++++++++++++++++ api/service/explorer/structs.go | 6 ++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/api/service/explorer/service.go b/api/service/explorer/service.go index a4ee5bf28..538f6e8d7 100644 --- a/api/service/explorer/service.go +++ b/api/service/explorer/service.go @@ -198,11 +198,31 @@ func (s *Service) GetExplorerBlocks(w http.ResponseWriter, r *http.Request) { } accountBlocks := s.ReadBlocksFromDB(fromInt, toInt) + curEpoch := int64(-1) + committee := &types.Committee{} for id, accountBlock := range accountBlocks { if id == 0 || id == len(accountBlocks)-1 || accountBlock == nil { continue } block := NewBlock(accountBlock, id+fromInt-1) + if len(block.Signers) == 0 { + if int64(block.Epoch) > curEpoch { + if bytes, err := db.Get([]byte(GetCommitteeKey(uint32(s.ShardID), block.Epoch))); err == nil { + committee = &types.Committee{} + if err = rlp.DecodeBytes(bytes, committee); err != nil { + utils.Logger().Warn().Err(err).Msg("cannot read committee for new epoch") + } + } + curEpoch = int64(block.Epoch) + } + for i, validator := range committee.NodeList { + oneAddress, err := common2.AddressToBech32(validator.EcdsaAddress) + if err != nil && accountBlock.Header().LastCommitBitmap[i] != 0x0 { + continue + } + block.Signers = append(block.Signers, oneAddress) + } + } // Populate transactions for _, tx := range accountBlock.Transactions() { transaction := GetTransaction(tx, accountBlock) diff --git a/api/service/explorer/structs.go b/api/service/explorer/structs.go index 650d589a2..65621d814 100644 --- a/api/service/explorer/structs.go +++ b/api/service/explorer/structs.go @@ -92,17 +92,15 @@ func NewBlock(block *types.Block, height int) *Block { if err == nil { for _, committee := range state { if committee.ShardID == block.ShardID() { - for _, validator := range committee.NodeList { + for i, validator := range committee.NodeList { oneAddress, err := common.AddressToBech32(validator.EcdsaAddress) - if err != nil { + if err != nil && block.Header().LastCommitBitmap[i] != 0x0 { continue } signers = append(signers, oneAddress) } } } - } else { - utils.Logger().Warn().Err(err).Msgf("bad state block %d", block.NumberU64()) } return &Block{ Height: strconv.Itoa(height), From f293f69c4d68a9339bca5ab60bcec5f98bf31d1c Mon Sep 17 00:00:00 2001 From: flicker-harmony Date: Fri, 30 Aug 2019 21:04:46 +0300 Subject: [PATCH 2/3] Signers from LastCommitBitmap correctly --- api/service/explorer/service.go | 51 +++++++++++++++++++++++++-------- api/service/explorer/structs.go | 5 ++-- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/api/service/explorer/service.go b/api/service/explorer/service.go index 538f6e8d7..1dfe8af73 100644 --- a/api/service/explorer/service.go +++ b/api/service/explorer/service.go @@ -16,8 +16,10 @@ import ( "github.com/gorilla/mux" libp2p_peer "github.com/libp2p/go-libp2p-peer" + "github.com/harmony-one/bls/ffi/go/bls" msg_pb "github.com/harmony-one/harmony/api/proto/message" "github.com/harmony-one/harmony/core/types" + bls2 "github.com/harmony-one/harmony/crypto/bls" "github.com/harmony-one/harmony/internal/bech32" common2 "github.com/harmony-one/harmony/internal/common" "github.com/harmony-one/harmony/internal/ctxerror" @@ -204,23 +206,48 @@ func (s *Service) GetExplorerBlocks(w http.ResponseWriter, r *http.Request) { if id == 0 || id == len(accountBlocks)-1 || accountBlock == nil { continue } + utils.Logger().Info().Msgf("Block %d", id+fromInt-1) block := NewBlock(accountBlock, id+fromInt-1) - if len(block.Signers) == 0 { - if int64(block.Epoch) > curEpoch { - if bytes, err := db.Get([]byte(GetCommitteeKey(uint32(s.ShardID), block.Epoch))); err == nil { - committee = &types.Committee{} - if err = rlp.DecodeBytes(bytes, committee); err != nil { - utils.Logger().Warn().Err(err).Msg("cannot read committee for new epoch") + if int64(block.Epoch) > curEpoch { + if bytes, err := db.Get([]byte(GetCommitteeKey(uint32(s.ShardID), block.Epoch))); err == nil { + committee = &types.Committee{} + if err = rlp.DecodeBytes(bytes, committee); err != nil { + utils.Logger().Warn().Err(err).Msg("cannot read committee for new epoch") + } + } else { + state, err := accountBlock.Header().GetShardState() + if err == nil { + for _, shardCommittee := range state { + if shardCommittee.ShardID == accountBlock.ShardID() { + committee = &shardCommittee + break + } } } - curEpoch = int64(block.Epoch) } - for i, validator := range committee.NodeList { - oneAddress, err := common2.AddressToBech32(validator.EcdsaAddress) - if err != nil && accountBlock.Header().LastCommitBitmap[i] != 0x0 { - continue + curEpoch = int64(block.Epoch) + } + pubkeys := make([]*bls.PublicKey, len(committee.NodeList)) + for i, validator := range committee.NodeList { + pubkeys[i] = new(bls.PublicKey) + validator.BlsPublicKey.ToLibBLSPublicKey(pubkeys[i]) + } + mask, err := bls2.NewMask(pubkeys, nil) + if err == nil && accountBlocks[id+1] != nil { + err = mask.SetMask(accountBlocks[id+1].Header().LastCommitBitmap) + if err == nil { + for _, validator := range committee.NodeList { + oneAddress, err := common2.AddressToBech32(validator.EcdsaAddress) + utils.Logger().Info().Msgf("Validator address %s", oneAddress) + if err != nil { + continue + } + blsPublicKey := new(bls.PublicKey) + validator.BlsPublicKey.ToLibBLSPublicKey(blsPublicKey) + if ok, err := mask.KeyEnabled(blsPublicKey); err == nil && ok { + block.Signers = append(block.Signers, oneAddress) + } } - block.Signers = append(block.Signers, oneAddress) } } // Populate transactions diff --git a/api/service/explorer/structs.go b/api/service/explorer/structs.go index 65621d814..8d3f79c0f 100644 --- a/api/service/explorer/structs.go +++ b/api/service/explorer/structs.go @@ -6,7 +6,6 @@ import ( "strconv" "github.com/harmony-one/harmony/core/types" - "github.com/harmony-one/harmony/internal/common" "github.com/harmony-one/harmony/internal/utils" ) @@ -88,7 +87,7 @@ type Shard struct { func NewBlock(block *types.Block, height int) *Block { // TODO(ricl): use block.Header().CommitBitmap and GetPubKeyFromMask signers := []string{} - state, err := block.Header().GetShardState() + /*state, err := block.Header().GetShardState() if err == nil { for _, committee := range state { if committee.ShardID == block.ShardID() { @@ -101,7 +100,7 @@ func NewBlock(block *types.Block, height int) *Block { } } } - } + }*/ return &Block{ Height: strconv.Itoa(height), ID: block.Hash().Hex(), From 39e22877da86607aba838e8c7dd1e8b170e358c0 Mon Sep 17 00:00:00 2001 From: flicker-harmony Date: Fri, 30 Aug 2019 21:07:44 +0300 Subject: [PATCH 3/3] Remove logging --- api/service/explorer/service.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/api/service/explorer/service.go b/api/service/explorer/service.go index 1dfe8af73..603eecfde 100644 --- a/api/service/explorer/service.go +++ b/api/service/explorer/service.go @@ -206,7 +206,6 @@ func (s *Service) GetExplorerBlocks(w http.ResponseWriter, r *http.Request) { if id == 0 || id == len(accountBlocks)-1 || accountBlock == nil { continue } - utils.Logger().Info().Msgf("Block %d", id+fromInt-1) block := NewBlock(accountBlock, id+fromInt-1) if int64(block.Epoch) > curEpoch { if bytes, err := db.Get([]byte(GetCommitteeKey(uint32(s.ShardID), block.Epoch))); err == nil { @@ -238,7 +237,6 @@ func (s *Service) GetExplorerBlocks(w http.ResponseWriter, r *http.Request) { if err == nil { for _, validator := range committee.NodeList { oneAddress, err := common2.AddressToBech32(validator.EcdsaAddress) - utils.Logger().Info().Msgf("Validator address %s", oneAddress) if err != nil { continue }