From d84e65889dfa5f93a8a466b4d4ecbe089f5e3ca5 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Wed, 20 Feb 2019 19:59:56 -0800 Subject: [PATCH] fix typos and add check nil condition for beaconchain in staking service. fix comments --- api/service/staking/service.go | 22 ++++++++++++++-------- node/node.go | 2 +- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/api/service/staking/service.go b/api/service/staking/service.go index d27aefc37..fa67091da 100644 --- a/api/service/staking/service.go +++ b/api/service/staking/service.go @@ -16,10 +16,13 @@ import ( "github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/p2p" + "github.com/harmony-one/harmony/p2p/host" ) const ( - WaitTime = 5 * time.Second + // WaitTime is the delay time for resending staking transaction if the previous transaction did not get approved. + WaitTime = 5 * time.Second + // StakingContractAddress is the staking deployed contract address StakingContractAddress = "TODO(minhdoan): Create a PR to generate staking contract address" ) @@ -68,7 +71,7 @@ func (s *Service) Run() { for { select { case <-tick.C: - if s.Staked() { + if s.IsStaked() { return } s.DoService() @@ -79,16 +82,21 @@ func (s *Service) Run() { }() } -func (s *Service) Staked() bool { +// IsStaked checks if the txn gets accepted and approved in the beacon chain. +func (s *Service) IsStaked() bool { return false } // DoService does staking. func (s *Service) DoService() { utils.GetLogInstance().Info("Trying to send a staking transaction.") + if s.beaconChain == nil { + utils.GetLogInstance().Info("Can not send a staking transaction because of nil beacon chain.") + return + } if msg := s.createStakingMessage(); msg != nil { - s.host.SendMessageToGroups([]p2p.GroupID{p2p.GroupIDBeacon}, msg) + s.host.SendMessageToGroups([]p2p.GroupID{p2p.GroupIDBeacon}, host.ConstructP2pMessage(byte(17), msg)) } else { utils.GetLogInstance().Error("Can not create staking transaction") } @@ -131,11 +139,9 @@ func constructStakingMessage(ts types.Transactions) []byte { } if data, err := protobuf.Marshal(msg); err == nil { return data - } else { - utils.GetLogInstance().Error("Error when creating staking message") - return nil } - + utils.GetLogInstance().Error("Error when creating staking message") + return nil } func (s *Service) createStakingMessage() []byte { diff --git a/node/node.go b/node/node.go index ad655ef87..42e2ac0d7 100644 --- a/node/node.go +++ b/node/node.go @@ -683,7 +683,7 @@ func (node *Node) setupForNewNode() { nodeConfig, chanPeer := node.initNodeConfiguration() // Register staking service. - node.serviceManager.RegisterService(service_manager.Staking, staking.New(node.host, node.AccountKey, 0, stakingPeer, nil)) + node.serviceManager.RegisterService(service_manager.Staking, staking.New(node.host, node.AccountKey, 0, node.beaconChain)) // Register peer discovery service. "0" is the beacon shard ID node.serviceManager.RegisterService(service_manager.PeerDiscovery, discovery.New(node.host, nodeConfig, chanPeer)) // Register networkinfo service. "0" is the beacon shard ID