diff --git a/common/common.go b/common/common.go index 7e69d9ba..bcdf757c 100644 --- a/common/common.go +++ b/common/common.go @@ -161,16 +161,11 @@ type ( func FirstRPC() { retryCount := 0 for { - result, rpcErr := RawRequest("getblockchaininfo", []json.RawMessage{}) - if rpcErr == nil { + _, err := GetBlockChainInfo() + if err == nil { if retryCount > 0 { Log.Warn("getblockchaininfo RPC successful") } - var getblockchaininfo ZcashdRpcReplyGetblockchaininfo - err := json.Unmarshal(result, &getblockchaininfo) - if err != nil { - Log.Fatalf("error parsing JSON getblockchaininfo response: %v", err) - } break } retryCount++ @@ -180,13 +175,26 @@ func FirstRPC() { }).Fatal("unable to issue getblockchaininfo RPC call to zcashd node") } Log.WithFields(logrus.Fields{ - "error": rpcErr.Error(), + "error": err.Error(), "retry": retryCount, }).Warn("error with getblockchaininfo rpc, retrying...") Time.Sleep(time.Duration(10+retryCount*5) * time.Second) // backoff } } +func GetBlockChainInfo() (*ZcashdRpcReplyGetblockchaininfo, error) { + result, rpcErr := RawRequest("getblockchaininfo", []json.RawMessage{}) + if rpcErr != nil { + return nil, rpcErr + } + var getblockchaininfoReply ZcashdRpcReplyGetblockchaininfo + err := json.Unmarshal(result, &getblockchaininfoReply) + if err != nil { + return nil, err + } + return &getblockchaininfoReply, nil +} + func GetLightdInfo() (*walletrpc.LightdInfo, error) { result, rpcErr := RawRequest("getinfo", []json.RawMessage{}) if rpcErr != nil { diff --git a/common/darkside.go b/common/darkside.go index 1dcc2da2..8ded2271 100644 --- a/common/darkside.go +++ b/common/darkside.go @@ -405,13 +405,23 @@ func DarksideClearIncomingTransactions() { func darksideRawRequest(method string, params []json.RawMessage) (json.RawMessage, error) { switch method { case "getblockchaininfo": + state.mutex.RLock() + defer state.mutex.RUnlock() + if len(state.activeBlocks) == 0 { + Log.Fatal("getblockchaininfo: no blocks") + } + index := state.latestHeight - state.startHeight + block := parser.NewBlock() + block.ParseFromSlice(state.activeBlocks[index]) + hash := hex.EncodeToString(block.GetDisplayHash()) blockchaininfo := &ZcashdRpcReplyGetblockchaininfo{ Chain: state.chainName, Upgrades: map[string]Upgradeinfo{ "76b809bb": {ActivationHeight: state.startHeight}, }, - Blocks: state.latestHeight, - Consensus: ConsensusInfo{state.branchID, state.branchID}, + Blocks: state.latestHeight, + Consensus: ConsensusInfo{state.branchID, state.branchID}, + BestBlockHash: hash, } return json.Marshal(blockchaininfo) diff --git a/common/mempool.go b/common/mempool.go index 244b1c21..241e0520 100644 --- a/common/mempool.go +++ b/common/mempool.go @@ -46,7 +46,7 @@ func GetMempool(sendToClient func(*walletrpc.RawTransaction) error) error { // Don't fetch the mempool more often than every 2 seconds. now := Time.Now() if now.After(g_lastTime.Add(2 * time.Second)) { - blockChainInfo, err := getLatestBlockChainInfo() + blockChainInfo, err := GetBlockChainInfo() if err != nil { g_lock.Unlock() return err @@ -137,16 +137,3 @@ func refreshMempoolTxns() error { } return nil } - -func getLatestBlockChainInfo() (*ZcashdRpcReplyGetblockchaininfo, error) { - result, rpcErr := RawRequest("getblockchaininfo", []json.RawMessage{}) - if rpcErr != nil { - return nil, rpcErr - } - var getblockchaininfoReply ZcashdRpcReplyGetblockchaininfo - err := json.Unmarshal(result, &getblockchaininfoReply) - if err != nil { - return nil, err - } - return &getblockchaininfoReply, nil -} diff --git a/frontend/service.go b/frontend/service.go index 497d88c4..d0171fff 100644 --- a/frontend/service.go +++ b/frontend/service.go @@ -62,14 +62,15 @@ func (s *lwdStreamer) GetLatestBlock(ctx context.Context, placeholder *walletrpc // Lock to ensure we return consistent height and hash s.mutex.Lock() defer s.mutex.Unlock() - latestBlock := s.cache.GetLatestHeight() - - if latestBlock == -1 { - return nil, errors.New("cache is empty, server is probably not yet ready") + blockChainInfo, err := common.GetBlockChainInfo() + if err != nil { + return nil, err } - latestHash := s.cache.GetLatestHash() - - return &walletrpc.BlockID{Height: uint64(latestBlock), Hash: latestHash}, nil + bestBlockHash, err := hex.DecodeString(blockChainInfo.BestBlockHash) + if err != nil { + return nil, err + } + return &walletrpc.BlockID{Height: uint64(blockChainInfo.Blocks), Hash: []byte(bestBlockHash)}, nil } // GetTaddressTxids is a streaming RPC that returns transaction IDs that have @@ -237,11 +238,11 @@ func (s *lwdStreamer) GetTreeState(ctx context.Context, id *walletrpc.BlockID) ( } func (s *lwdStreamer) GetLatestTreeState(ctx context.Context, in *walletrpc.Empty) (*walletrpc.TreeState, error) { - latestHeight := s.cache.GetLatestHeight() - - if latestHeight == -1 { - return nil, errors.New("Cache is empty. Server is probably not yet ready") + blockChainInfo, err := common.GetBlockChainInfo() + if err != nil { + return nil, err } + latestHeight := blockChainInfo.Blocks return s.GetTreeState(ctx, &walletrpc.BlockID{Height: uint64(latestHeight)}) }