From 8c54a631d97a173025c3a9d46e2f9143622fe357 Mon Sep 17 00:00:00 2001 From: hackyminer Date: Fri, 12 Oct 2018 22:07:03 +0900 Subject: [PATCH] support both geth/parity --- proxy/blocks.go | 18 ++++++++++++++++-- rpc/rpc.go | 13 +++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/proxy/blocks.go b/proxy/blocks.go index 839834e19..17af14013 100644 --- a/proxy/blocks.go +++ b/proxy/blocks.go @@ -59,11 +59,25 @@ func (s *ProxyServer) fetchBlockTemplate() { return } diff := util.TargetHexToDiff(reply[2]) - height, err := strconv.ParseUint(strings.Replace(reply[3], "0x", "", -1), 16, 64) pendingReply := &rpc.GetBlockReplyPart{ Difficulty: util.ToHex(s.config.Proxy.Difficulty), - Number: reply[3], + } + + var height uint64 + if len(reply) > 3 { + // parity case + height, err = strconv.ParseUint(strings.Replace(reply[3], "0x", "", -1), 16, 64) + + pendingReply.Number = reply[3] + } else { + // geth case + pendingReply, err = r.GetPendingBlock() + if err != nil { + log.Printf("Error while refreshing pending block on %s: %s", r.Name, err) + return + } + height, err = strconv.ParseUint(strings.Replace(pendingReply.Number, "0x", "", -1), 16, 64) } newTemplate := BlockTemplate{ diff --git a/rpc/rpc.go b/rpc/rpc.go index e637b0550..08e6102c0 100644 --- a/rpc/rpc.go +++ b/rpc/rpc.go @@ -111,6 +111,19 @@ func (r *RPCClient) GetLatestBlock() (*GetBlockReplyPart, error) { return nil, nil } +func (r *RPCClient) GetPendingBlock() (*GetBlockReplyPart, error) { + rpcResp, err := r.doPost(r.Url, "eth_getBlockByNumber", []interface{}{"pending", false}) + if err != nil { + return nil, err + } + if rpcResp.Result != nil { + var reply *GetBlockReplyPart + err = json.Unmarshal(*rpcResp.Result, &reply) + return reply, err + } + return nil, nil +} + func (r *RPCClient) GetBlockByHeight(height int64) (*GetBlockReply, error) { params := []interface{}{fmt.Sprintf("0x%x", height), true} return r.getBlockBy("eth_getBlockByNumber", params)