From c8ee8059332301e89d4d3c31f36f4521eddee91d Mon Sep 17 00:00:00 2001 From: Larry Ruane Date: Sun, 10 May 2020 09:44:45 -0600 Subject: [PATCH] remove Blockchaininfo json type assertions, no functional change --- common/common.go | 40 ++++++++++++++++++++++++---------------- common/darkside.go | 19 +++---------------- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/common/common.go b/common/common.go index b0329c7..513d1d5 100644 --- a/common/common.go +++ b/common/common.go @@ -50,10 +50,27 @@ var Sleep func(d time.Duration) // Log as a global variable simplifies logging var Log *logrus.Entry +type ( + Upgradeinfo struct { + // there are other fields that aren't needed here, omit them + ActivationHeight int + } + ConsensusInfo struct { + Nextblock string + Chaintip string + } + Blockchaininfo struct { + Chain string + Upgrades map[string]Upgradeinfo + Headers int + Consensus ConsensusInfo + } +) + // GetSaplingInfo returns the result of the getblockchaininfo RPC to zcashd func GetSaplingInfo() (int, int, string, string) { // This request must succeed or we can't go on; give zcashd time to start up - var f interface{} + var blockchaininfo Blockchaininfo retryCount := 0 for { result, rpcErr := RawRequest("getblockchaininfo", []json.RawMessage{}) @@ -61,7 +78,7 @@ func GetSaplingInfo() (int, int, string, string) { if retryCount > 0 { Log.Warn("getblockchaininfo RPC successful") } - err := json.Unmarshal(result, &f) + err := json.Unmarshal(result, &blockchaininfo) if err != nil { Log.Fatalf("error parsing JSON getblockchaininfo response: %v", err) } @@ -80,23 +97,14 @@ func GetSaplingInfo() (int, int, string, string) { Sleep(time.Duration(10+retryCount*5) * time.Second) // backoff } - chainName := f.(map[string]interface{})["chain"].(string) - - upgradeJSON := f.(map[string]interface{})["upgrades"] - // If the sapling consensus branch doesn't exist, it must be regtest - saplingHeight := float64(0) - if saplingJSON, ok := upgradeJSON.(map[string]interface{})["76b809bb"]; ok { // Sapling ID - saplingHeight = saplingJSON.(map[string]interface{})["activationheight"].(float64) + var saplingHeight int + if saplingJSON, ok := blockchaininfo.Upgrades["76b809bb"]; ok { // Sapling ID + saplingHeight = saplingJSON.ActivationHeight } - blockHeight := f.(map[string]interface{})["headers"].(float64) - - consensus := f.(map[string]interface{})["consensus"] - - branchID := consensus.(map[string]interface{})["nextblock"].(string) - - return int(saplingHeight), int(blockHeight), chainName, branchID + return saplingHeight, blockchaininfo.Headers, blockchaininfo.Chain, + blockchaininfo.Consensus.Nextblock } func getBlockFromRPC(height int) (*walletrpc.CompactBlock, error) { diff --git a/common/darkside.go b/common/darkside.go index 9dbc80d..5192463 100644 --- a/common/darkside.go +++ b/common/darkside.go @@ -163,26 +163,13 @@ func DarksideSendTransaction(txHex []byte) ([]byte, error) { func darksideRawRequest(method string, params []json.RawMessage) (json.RawMessage, error) { switch method { case "getblockchaininfo": - type upgradeinfo struct { - // there are other fields that aren't needed here, omit them - ActivationHeight int `json:"activationheight"` - } - type consensus struct { - Nextblock string `json:"nextblock"` - Chaintip string `json:"chaintip"` - } - blockchaininfo := struct { - Chain string `json:"chain"` - Upgrades map[string]upgradeinfo `json:"upgrades"` - Headers int `json:"headers"` - Consensus consensus `json:"consensus"` - }{ + blockchaininfo := Blockchaininfo{ Chain: state.chainName, - Upgrades: map[string]upgradeinfo{ + Upgrades: map[string]Upgradeinfo{ "76b809bb": {ActivationHeight: state.saplingActivation}, }, Headers: state.startHeight + len(state.blocks) - 1, - Consensus: consensus{state.branchID, state.branchID}, + Consensus: ConsensusInfo{state.branchID, state.branchID}, } return json.Marshal(blockchaininfo)