Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix GetLatestBlock() gRPC (pure block cache) #415

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand All @@ -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 {
Expand Down
14 changes: 12 additions & 2 deletions common/darkside.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 1 addition & 14 deletions common/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
23 changes: 12 additions & 11 deletions frontend/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)})
}

Expand Down