From bc6eb8d1fa888f1991390b2150304ae2779af756 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Tue, 30 Apr 2019 15:41:47 -0700 Subject: [PATCH] Fix 0 balance issue on explorer; increase consensus delay --- api/service/explorer/service.go | 37 +++++++++++++++++++++++---------- node/node_newblock.go | 2 +- node/service_setup.go | 4 ++-- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/api/service/explorer/service.go b/api/service/explorer/service.go index ef66e8906..ae615c9f1 100644 --- a/api/service/explorer/service.go +++ b/api/service/explorer/service.go @@ -4,11 +4,14 @@ import ( "context" "encoding/json" "fmt" + "math/big" "net" "net/http" "os" "strconv" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/rlp" "github.com/gorilla/mux" msg_pb "github.com/harmony-one/harmony/api/proto/message" @@ -25,21 +28,23 @@ const ( // Service is the struct for explorer service. type Service struct { - router *mux.Router - IP string - Port string - GetNodeIDs func() []libp2p_peer.ID - storage *Storage - server *http.Server - messageChan chan *msg_pb.Message + router *mux.Router + IP string + Port string + GetNodeIDs func() []libp2p_peer.ID + storage *Storage + server *http.Server + messageChan chan *msg_pb.Message + GetAccountBalance func(common.Address) (*big.Int, error) } // New returns explorer service. -func New(selfPeer *p2p.Peer, GetNodeIDs func() []libp2p_peer.ID) *Service { +func New(selfPeer *p2p.Peer, GetNodeIDs func() []libp2p_peer.ID, GetAccountBalance func(common.Address) (*big.Int, error)) *Service { return &Service{ - IP: selfPeer.IP, - Port: selfPeer.Port, - GetNodeIDs: GetNodeIDs, + IP: selfPeer.IP, + Port: selfPeer.Port, + GetNodeIDs: GetNodeIDs, + GetAccountBalance: GetAccountBalance, } } @@ -255,6 +260,16 @@ func (s *Service) GetExplorerAddress(w http.ResponseWriter, r *http.Request) { return } data.Address = address + + // Check the balance from blockchain rather than local DB dump + if s.GetAccountBalance != nil { + address := common.HexToAddress(id) + balance, err := s.GetAccountBalance(address) + if err == nil { + data.Address.Balance = balance + } + } + json.NewEncoder(w).Encode(data.Address) } diff --git a/node/node_newblock.go b/node/node_newblock.go index 91e4c4855..b3d305165 100644 --- a/node/node_newblock.go +++ b/node/node_newblock.go @@ -31,7 +31,7 @@ func (node *Node) WaitForConsensusReady(readySignal chan struct{}, stopChan chan // keep waiting for Consensus ready select { case <-readySignal: - time.Sleep(100 * time.Millisecond) // Delay a bit so validator is catched up (test-only). + time.Sleep(1000 * time.Millisecond) // Delay a bit so validator is catched up (test-only). case <-time.After(ConsensusTimeOut * time.Second): node.Consensus.ResetState() timeoutCount++ diff --git a/node/service_setup.go b/node/service_setup.go index 304ea51e6..755c5c0e6 100644 --- a/node/service_setup.go +++ b/node/service_setup.go @@ -26,7 +26,7 @@ func (node *Node) setupForShardLeader() { node.serviceManager.RegisterService(service.NetworkInfo, networkinfo.New(node.host, node.NodeConfig.GetShardGroupID(), chanPeer, nil)) // Register explorer service. - node.serviceManager.RegisterService(service.SupportExplorer, explorer.New(&node.SelfPeer, node.Consensus.GetNodeIDs)) + node.serviceManager.RegisterService(service.SupportExplorer, explorer.New(&node.SelfPeer, node.Consensus.GetNodeIDs, node.GetBalanceOfAddress)) // Register consensus service. node.serviceManager.RegisterService(service.Consensus, consensus.New(node.BlockChannel, node.Consensus, node.startConsensus)) // Register new block service. @@ -68,7 +68,7 @@ func (node *Node) setupForBeaconLeader() { // Register randomness service node.serviceManager.RegisterService(service.Randomness, randomness.New(node.DRand)) // Register explorer service. - node.serviceManager.RegisterService(service.SupportExplorer, explorer.New(&node.SelfPeer, node.Consensus.GetNodeIDs)) + node.serviceManager.RegisterService(service.SupportExplorer, explorer.New(&node.SelfPeer, node.Consensus.GetNodeIDs, node.GetBalanceOfAddress)) } func (node *Node) setupForBeaconValidator() {