英文:
why does my "newPendingTransactions" geth subscription not get any events?
问题
我面临以下挑战。我尝试通过WebSocket订阅"newPendingTransactions"。我可以成功连接到WebSocket。连接成功后,我期望会有一系列新的待处理交易流入,我可以读取并传播到某个通道(chan json.RawMessage)。
...
c,httpResponse,err := websocket.DefaultDialer.Dial(u.String(),req.Header)
...
for {
messageType,message,err := c.ReadMessage()
if err != nil {
log.Println("ERROR:",err)
os.Exit(1)
return
}
, = message,messageType
// s.Out是传出的chan json.RawMessage
s.Out <- message
}
...
不幸的是,我没有收到任何消息(待处理的交易),只有在关闭整个结构时才收到一条消息。当我直接在控制台上使用"txpool.status"检查我的节点时,我可以看到一直有新的待处理交易进来。它们只是不想传播到我的WebSocket连接。有人可以帮我解决这个问题吗?也许我在启动geth节点本身时缺少某个参数?
这是我启动geth节点的方式:
geth --http.api eth,web3,debug,txpool,net,shh,db,admin,debug --http --ws --ws.api eth,web3,debug,txpool,net,shh,db,admin,debug --ws.origins localhost --gcmode full --http.port=8545 --maxpeers 120
这是我的"admin.nodeInfo":
Geth/v1.10.16-stable-20356e57/linux-amd64/go1.17.5
英文:
Im facing the following challenge. I try to subscribe to "newPendingTransactions" via websocket. I can successfully connect to the websocket. When connected, I would expect a stream of new incoming pending transactions, which I can read out and propagate it to some channel (chan json.RawMessage).
...
c, httpResponse, err := websocket.DefaultDialer.Dial(u.String(), req.Header)
...
for {
messageType, message, err := c.ReadMessage()
if err != nil {
log.Println("ERROR:", err)
os.Exit(1)
return
}
_, _ = message, messageType
// s.Out is the outgoing chan json.RawMessage
s.Out <- message
}
...
sadly I dont receive any message (pending tx).. only one on closing the whole construct. When I check on my node directly with "txpool.status" in console, then I can see that there are new pending txs incoming all the time. They just dont wanna get propagated to my websocket connection. Is there anyone who can help me out here? Maybe I am missing a parameter for starting the geth node itself?
here is how I start my geth node:
geth --http.api eth,web3,debug,txpool,net,shh,db,admin,debug --http --ws --ws.api eth,web3,debug,txpool,net,shh,db,admin,debug --ws.origins localhost --gcmode full --http.port=8545 --maxpeers 120
here is my "admin.nodeInfo":
Geth/v1.10.16-stable-20356e57/linux-amd64/go1.17.5
答案1
得分: 0
我发现了我做错的地方:
问题不在于任何遗漏的geth参数,即使"newPendingTransactions"也被正确传播了。
我使用另一个名为"wscat"的工具测试了websocket连接,并通过控制台发送了必要的rpc(结果是一系列的交易哈希)。
{"id": 1, "method": "eth_subscribe", "params": ["newPendingTransactions"]}
它告诉我错误一定是关于golang代码本身的。
在for循环的最后一行
s.Out <- message
将消息发送到了一个无缓冲的通道。为了使其工作,必须有另一个goroutine在另一端消费通道的消息。
使用缓冲通道也会有所帮助,像这样:
s.Out = make(chan Message, 1000)
...这样至少会发送1000条消息。
英文:
I found out what I did wrong:
It was not about any forgotten geth parameter and even that the "newPendingTransactions" were propagated correctly..
I tested the websocket connection with another tool called "wscat" and sent the necessary rpc via console (resulting in a stream of tx hashes!)
{"id": 1, "method": "eth_subscribe", "params": ["newPendingTransactions"]}
It showed me that the error must be about the golang code itself..
The last line inside the for loop
s.Out <- message
was sending messages into an unbuffered channel. In order for this to work, some other go routine must consume the channels messages on the other side.
It also helps to use a buffered channel like this:
s.Out = make(chan Message, 1000)
... so at least a 1000 messages will be sent out
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论