监听以太坊节点的交易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
    }
 
 
}

运行效果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在ndnSIM中,可以通过在节点的应用程序中添加相应的代码来输出每个节点的RTT和当前未处理兴趣,以及节点之间的带宽。以下是一个简单的例子: 1. 输出节点的RTT 在节点的应用程序中,可以使用以下代码来输出节点的RTT: ```c++ void MyNode::onData(shared_ptr<const Data> data) { // 获取当前时间 Time now = Simulator::Now(); // 获取据包的接收时间 Time receiveTime = data->GetWire()->GetDelay(); // 计算RTT Time rtt = now - receiveTime; // 输出RTT cout << "RTT for node " << GetId() << " is " << rtt.GetMilliSeconds() << "ms" << endl; } ``` 在这个例子中,我们在节点的`onData`回调函中计算接收到据包的RTT,并输出结果。 2. 输出当前未处理兴趣节点的应用程序中,可以使用以下代码来输出当前未处理兴趣: ```c++ void MyNode::onInterest(shared_ptr<const Interest> interest) { // 获取当前未处理兴趣 int numPendingInterests = m_face->GetNPendingInterests(); // 输出未处理兴趣 cout << "Number of pending interests for node " << GetId() << " is " << numPendingInterests << endl; } ``` 在这个例子中,我们在节点的`onInterest`回调函中获取当前未处理兴趣,并输出结果。 3. 输出节点之间的带宽 在ndnSIM中,可以使用`CsTracer::InstallAll`函来安装一个跟踪器,用于跟踪每个节点之间的据包传输情况。在安装跟踪器之后,可以使用以下代码来输出节点之间的带宽: ```c++ // 安装跟踪器 CsTracer::InstallAll("trace.txt"); // 输出节点之间的带宽 cout << "Bandwidth between nodes " << node1->GetId() << " and " << node2->GetId() << " is " << CsTracer::Get(node1->GetObject<ndn::L3Protocol>(), node2->GetObject<ndn::L3Protocol>())->GetAvgBandwidth() << "bps" << endl; ``` 在这个例子中,我们安装了一个跟踪器,并使用`CsTracer::Get`函获取节点之间的跟踪器实例,从而获取节点之间的平均带宽。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值