Exposed method on go-ethereum/rpc doesn't seems to work

83 views Asked by At

I have the following working code, I can use cURL to verify that the RMI exists, but when I try to call it, it says that doesn't exists.

My minimal reproducible example:

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))
}

As you can see, the RMI example exists:

$ 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"}}

When I try to invoke it, it fails:

$ 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"}}

What I am doing wrong?

1

There are 1 answers

2
Rodrigo On

Found the problem, I should invoke this way:

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}

The method name is: calculator_add.