filecoin lotus rpc调用

rpc调用

参考地址
https://filecoin-shipyard.github.io/js-lotus-client/api/full-node/chain.html

调用示例

终端调用

curl -X POST \
  -H "Content-Type: application/json" \
  --data '{ 
      "jsonrpc": "2.0", 
      "method": "Filecoin.Version", 
      "params": [], 
      "id": 1 
    }' \
  http://127.0.0.1:1234/rpc/v0 

postman调用

http://127.0.0.1:1234/rpc/v0
参数 body raw 
{"jsonrpc":"2.0","method":"Filecoin.Version","params":[],"id":1}

返回值
{"jsonrpc":"2.0","result":{"Version":"0.5.4+git.d4fef1b5.dirty","APIVersion":3072,"BlockDelay":30},"id":1}


lotus代码地址
node/impl/common/common.go

func (a *CommonAPI) Version(context.Context) (api.Version, error) {
	return api.Version{
		Version:    build.UserVersion(),
		APIVersion: build.APIVersion,

		BlockDelay: build.BlockDelaySecs,
	}, nil
}

lotus rpc

主要说明lotus rpc的代码实现,以及除了上述网站所写的其他rpc接口

代码调用

代码路径
cmd/lotus/rpc.go

func serveRPC(a api.FullNode, stop node.StopFunc, addr multiaddr.Multiaddr, shutdownCh <-chan struct{}) error {
	rpcServer := jsonrpc.NewServer()
	rpcServer.Register("Filecoin", apistruct.PermissionedFullAPI(a))

	ah := &auth.Handler{
		Verify: a.AuthVerify,
		Next:   rpcServer.ServeHTTP,
	}

	http.Handle("/rpc/v0", ah)
}

apistruct.PermissionedFullAPI(a)
api/apistruct/struct.go
//这里相当于包装了一下,添加了权限要求

func PermissionedFullAPI(a api.FullNode) api.FullNode {
	var out FullNodeStruct
	auth.PermissionedProxy(AllPermissions, DefaultPerms, a, &out.Internal)
	auth.PermissionedProxy(AllPermissions, DefaultPerms, a, &out.CommonStruct.Internal)
	return &out
}

rpcServer.Register(“Filecoin”, apistruct.PermissionedFullAPI(a))
go-sonrpc 包, 内部使用反射 namespace+"."+method.Name

func (s *RPCServer) register(namespace string, r interface{}) {
	val := reflect.ValueOf(r)
	//TODO: expect ptr

	for i := 0; i < val.NumMethod(); i++ {
		method := val.Type().Method(i)

		funcType := method.Func.Type()
		hasCtx := 0
		if funcType.NumIn() >= 2 && funcType.In(1) == contextType {
			hasCtx = 1
		}

		ins := funcType.NumIn() - 1 - hasCtx
		recvs := make([]reflect.Type, ins)
		for i := 0; i < ins; i++ {
			recvs[i] = method.Type.In(i + 1 + hasCtx)
		}

		valOut, errOut, _ := processFuncOut(funcType)
        //
		s.methods[namespace+"."+method.Name] = rpcHandler{
			paramReceivers: recvs,
			nParams:        ins,

			handlerFunc: method.Func,
			receiver:    val,

			hasCtx: hasCtx,

			errOut: errOut,
			valOut: valOut,
		}
	}
}

其他rpc

上面代码中可看出,a api.FullNode 下面的都是可以调用的,有些上面链接中没有的。
举个例子
GasEstimateGasPremium

//node/impl/full/gas.go
GasEstimateGasPremium(_ context.Context, nblocksincl uint64,
		sender address.Address, gaslimit int64, tsk types.TipSetKey) (types.BigInt, error)

参数
除context外,有四个参数,基本参数好理解,第四个参数 types.TipSetKey是参考上面链接中弄出来的格式

{
    jsonrpc: "2.0", 
    method: "Filecoin.GasEstimateGasPremium", 
    params: [
        2, 
        "改成你的地址", 
        1, 
        [
            {
                /: "随便找个区块的消息ID,hash即可"
            }
        ]
    ], 
    id: 1
}

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

版权声明:本文为zgf1991原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/zgf1991/article/details/108403296