go-ethereum/rpc 上公开的方法似乎不起作用

问题描述 投票:0回答:1

我有以下工作代码,我可以使用 cURL 来验证 RMI 是否存在,但是当我尝试调用它时,它说不存在。

我的最小可重现示例:

package main

import (
    "log"
    "net/http"

    "github.com/ethereum/go-ethereum/rpc"
)

type ExampleService struct{}

func (e *ExampleService) Add(a, b int) int {
    return a + b
}

func main() {
    // Create a new RPC server
    server := rpc.NewServer()

    // Register a function to be called via JSON-RPC
    server.RegisterName("example", new(ExampleService))

    // Start an HTTP server to listen for requests
    http.Handle("/", server)
    log.Fatal(http.ListenAndServe(":8545", nil))
}

如您所见,RMI 示例已存在:

$ curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"rpc_modules","params":[],"id":1}' http://localhost:8545   

{"jsonrpc":"2.0","id":1,"result":{"example":"1.0","rpc":"1.0"}}

当我尝试调用它时,它失败了:

$ curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"example.Add","params":[3,4],"id":1}' http://localhost:8545

{"jsonrpc":"2.0","id":1,"error":{"code":-32601,"message":"the method example.Add does not exist/is not available"}}

我做错了什么?

go ethereum json-rpc
1个回答
0
投票

发现问题了,我应该这样调用:

curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"calculator_add","params":[3, 2],"id":1}' http://localhost:8000

{"jsonrpc":"2.0","id":1,"result":5}

方法名称为:calculator_add。

有谁知道是否可以更改为这样的内容:“calculator.add”?

© www.soinside.com 2019 - 2024. All rights reserved.