监听以太坊节点的交易pending数以及queued数并查看每个节点的区块高度及区块内交易数量

package main

import (
	"net/http"
	"bytes"
	"time"
	"io/ioutil"
	"encoding/json"
	"fmt"
	"strconv"
)

var urls=[]string{
	"http://192.168.1.183:6001",
	"http://192.168.1.183:6002",
	"http://192.168.1.183:6003",
	"http://192.168.1.185:6001",
	"http://192.168.1.186:6001",
	"http://192.168.1.186:6002",
}

func main() {

	for  {
		ticker := time.NewTicker(2*time.Second)
		now := time.Now()
		fmt.Println(now.String(),"\n\n")

		for _,url := range urls{
			pengNum, queued,err := getTxpoolStatus(url)
			if err!=nil{
				fmt.Println("have err" )
				return
			}

			blocknumber, txCount, e := getTxCountNum(url)
			if e!=nil{
				fmt.Println("have err")
				return
			}

			fmt.Println("url   "+url+"\tblockNumber:",blocknumber,"txCount:",txCount,"\n","\t\tpending :",pengNum,"\n","\t\tqueued :",queued)

		}

		<- ticker.C
	}



}

func getTxpoolStatus(url string) (pengNum , queued  uint64,err error) {

	s := `{"jsonrpc":"2.0","method":"` + "txpool_status" + `","id":1}`

	//s:=`{"jsonrpc":"2.0","method":"txpool_status","id":6}`
	jsonStr := []byte(s)

	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
	// req.Header.Set("X-Custom-Header", "myvalue")
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{
		Timeout: 5 * time.Second,
	}

	resp, err := client.Do(req)

	if err != nil {
		return 0,0,err
	}

	body, e := ioutil.ReadAll(resp.Body)
	if e != nil {
		return 0,0,e
	}

	//处理body数据

	var res map[string]interface{}
	unmarshal := json.Unmarshal(body, &res)

	if unmarshal != nil {
		return 0,0,unmarshal
	}

	if i, ok := res["result"]; ok {

		i2 := i.(map[string]interface{})

		sp:= i2["pending"].(string)
		sq := i2["queued"].(string)

		u, i3 := strconv.ParseUint(sp, 0, 64)
		if i3!=nil{
			fmt.Println(i3)
		}
		parseUint, i4 := strconv.ParseUint(sq, 0, 64)
		if i4!=nil{
			fmt.Println(i4)
		}

		pengNum,queued = u,parseUint

		return
	} else {
		fmt.Println(res)
		return
	}

	return
}


func getTxCountNum(url string)(blocknumber,txCount uint64,err error){

	s := `{"jsonrpc":"2.0","method":"` + "eth_blockNumber" + `","id":1}`

	//s:=`{"jsonrpc":"2.0","method":"txpool_status","id":6}`
	jsonStr := []byte(s)

	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
	// req.Header.Set("X-Custom-Header", "myvalue")
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{
		Timeout: 5 * time.Second,
	}

	resp, err := client.Do(req)

	if err != nil {
		return 0,0,err
	}

	body, e := ioutil.ReadAll(resp.Body)
	if e != nil {
		return 0,0,e
	}

	//处理body数据

	var res map[string]interface{}
	unmarshal := json.Unmarshal(body, &res)

	if unmarshal != nil {
		return 0,0,unmarshal
	}

	if i, ok := res["result"]; ok {
		 sp := i.(string)
		u, i3 := strconv.ParseUint(sp, 0, 64)
		if i3!=nil{
			fmt.Println(i3)
		}

		s := `{"jsonrpc":"2.0","method":"eth_getBlockTransactionCountByNumber","params":["`+sp +`"],"id":1}`


		jsonStr := []byte(s)

		req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))

		req.Header.Set("Content-Type", "application/json")

		client := &http.Client{
			Timeout: 5 * time.Second,
		}

		resp, err := client.Do(req)

		if err != nil {
			return 0,0,err
		}

		body, e := ioutil.ReadAll(resp.Body)
		if e != nil {
			return 0,0,e
		}

		//处理body数据

		var res map[string]interface{}
		unmarshal := json.Unmarshal(body, &res)

		if unmarshal != nil {
			return 0,0,unmarshal
		}

		if y, ok := res["result"]; ok {

			sp1 := y.(string)
			count, i4 := strconv.ParseUint(sp1, 0, 64)
			if i4!=nil{
				fmt.Println(i4)
			}

			return u,count,nil
		} else {
			fmt.Println(res)
			return 0,0,nil
		}


	} else {
		fmt.Println(res)
		return 0,0,nil
	}


}

运行效果:

监听以太坊节点的交易pending数以及queued数并查看每个节点的区块高度及区块内交易数量_第1张图片

你可能感兴趣的:(以太坊笔记整理)