Giter VIP home page Giter VIP logo

cln4go's Introduction

Vincenzo Palazzo

Passion, action and noble intentions create progress!

Github Sponsor | Bitcoin Sponsor

Official roles in some external repositories

Collab

Appendix

โšก Lightning Network node: https://bruce.lnmetrics.info

โšก BTC donation https://coinos.io/vincenzopalazzo

๐Ÿ”‘ GPG Fingerprint: DDE3 16C1 0965 F23D CCDD 0420 8B6D C2B8 70B8 0D5F

cln4go's People

Contributors

gabbyprecious avatar vincenzopalazzo avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

cln4go's Issues

hide the request complexity in the Call method

We should be able to hide the complexity of the JSON rpc protocol under the API call.

Foom this

 request := Request{method: "getinfo", params: make(map[string]interface{}) };
response, err := client.Call(request)

to this

response, err := client.Call( "getinfo", make(map[string]interface{}))

the Request{method: "getinfo", params: make(map[string]interface{}) }; should be done inside the client.Call method, and the signature of the Call method should be somethings like that

func (instance UnixRPC) Call(method string, data any) (*Response, error)

using generics to encode and decode response and request

This is an example on how to implement the generics side in the UNIX method, so you are able to build your type and unwrap the type in the response

diff --git a/client/request/types.go b/client/request/types.go
deleted file mode 100644
index e69de29..0000000
diff --git a/client/unix.go b/client/unix.go
index 6d9bbcc..f18669d 100644
--- a/client/unix.go
+++ b/client/unix.go
@@ -8,18 +8,22 @@ import (
 	"net"
 )
 
-type Request struct {
-	Method  string                 `json:"method"`
-	Params  map[string]interface{} `json:"params"`
-	Jsonrpc string                 `json:"jsonrpc"`
-	Id      int                    `json:"id"`
+type Request[T any] struct {
+	Method  string `json:"method"`
+	Params  T      `json:"params"`
+	Jsonrpc string `json:"jsonrpc"`
+	Id      int    `json:"id"`
+}
+
+func (instance Request[T]) ToByte() []byte {
+	return EncodeToBytes(instance)
 }
 
 type Response struct {
-	// Result string `json:result`
-	Error   string `json:"error"`
-	Jsonrpc string `json:jsonrpc`
-	Id      int    `json:"id"`
+	Result  map[string]any `json:result`
+	Error   string         `json:"error"`
+	Jsonrpc string         `json:jsonrpc`
+	Id      int            `json:"id"`
 }
 type UnixRPC struct {
 	socket net.Conn
@@ -35,7 +39,7 @@ func NewUnix(path string) (*UnixRPC, error) {
 	}, nil
 }
 
-func EncodeToBytes(p interface{}) []byte {
+func EncodeToBytes(p any) []byte {
 	buf := bytes.Buffer{}
 	enc := json.NewEncoder(&buf)
 	err := enc.Encode(p)
@@ -57,9 +61,9 @@ func DecodeToResponse(s []byte) *Response {
 	return &r
 }
 
-func (instance UnixRPC) Call(data Request) (*Response, error) {
+func (instance UnixRPC) Call(data []byte) (*Response, error) {
 	//change request to bytes
-	dataBytes := EncodeToBytes(data)
+	dataBytes := data
 	log.Printf(string(dataBytes))
 	//send data
 	_, err := instance.socket.Write(dataBytes)
diff --git a/client/unix_test.go b/client/unix_test.go
index 88f3188..522838f 100644
--- a/client/unix_test.go
+++ b/client/unix_test.go
@@ -16,8 +16,8 @@ func TestUnixCallOne(t *testing.T) {
 	if err != nil {
 		panic(err)
 	}
-	request := Request{Method: "getinfo", Params: make(map[string]interface{}), Jsonrpc: "2.0", Id: 0}
-	response, err := client.Call(request)
+	request := Request[map[string]any]{Method: "getinfo", Params: make(map[string]any), Jsonrpc: "2.0", Id: 0}
+	response, err := client.Call(request.ToByte())
 	if err != nil {
 		panic(err)
 	}

client: reading from buffer crash in some cases

While I was testing lnmetrics plugin I found that there is some very bad case of crash while reading from the socket and this is the stackatrace

โžœ  VincentSSD panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x6c2c8c]

goroutine 41 [running]:
github.com/vincenzopalazzo/cln4go/client.(*UnixRPC).decodeToResponse(0xc000631bb0, {0xc00419e000, 0x8ec0, 0xa000})
	/home/vincent/Github/OpenLNMetrics/go-lnmetrics.reporter/vendor/github.com/vincenzopalazzo/cln4go/client/unix.go:51 +0xcc
github.com/vincenzopalazzo/cln4go/client.UnixRPC.Call({{0x805c28, 0xc000014270}, {0x0, 0x0}, {0x8045e0, 0xa1a568}}, {0x772b6c, 0x9}, 0xc00063c540)
	/home/vincent/Github/OpenLNMetrics/go-lnmetrics.reporter/vendor/github.com/vincenzopalazzo/cln4go/client/unix.go:91 +0x2e9
github.com/vincenzopalazzo/cln4go/client.Call[...]({0x801200, 0xc000128360}, {0x772b6c, 0x9}, {0x4d5aa5})
	/home/vincent/Github/OpenLNMetrics/go-lnmetrics.reporter/vendor/github.com/vincenzopalazzo/cln4go/client/client.go:14 +0x70
github.com/LNOpenMetrics/go-lnmetrics.reporter/pkg/ln.ListPeers({0x801200?, 0xc000128360?}, 0xc000629d50?)
	/home/vincent/Github/OpenLNMetrics/go-lnmetrics.reporter/pkg/ln/client.go:85 +0x3f
github.com/LNOpenMetrics/go-lnmetrics.reporter/internal/plugin.(*MetricOne).snapshotListPeers(0xc0001301c0, {0x801200, 0xc000128360})
	/home/vincent/Github/OpenLNMetrics/go-lnmetrics.reporter/internal/plugin/metrics_one.go:88 +0x7c
github.com/LNOpenMetrics/go-lnmetrics.reporter/internal/plugin.(*MetricOne).onEvent(0x803880?, {0x772680, 0x8}, {0x801200, 0xc000128360})
	/home/vincent/Github/OpenLNMetrics/go-lnmetrics.reporter/internal/plugin/metrics_one.go:106 +0x59
github.com/LNOpenMetrics/go-lnmetrics.reporter/internal/plugin.(*MetricOne).OnInit(0xc0001301c0, {0x801200, 0xc000128360})
	/home/vincent/Github/OpenLNMetrics/go-lnmetrics.reporter/internal/plugin/metrics_one.go:184 +0x27b
github.com/LNOpenMetrics/go-lnmetrics.reporter/internal/plugin.(*MetricsPlugin).RegisterOneTimeEvt.func1.1(0x9ea320, {0x805df8, 0xc0001301c0})
	/home/vincent/Github/OpenLNMetrics/go-lnmetrics.reporter/internal/plugin/plugin.go:143 +0x3e
created by github.com/LNOpenMetrics/go-lnmetrics.reporter/internal/plugin.(*MetricsPlugin).RegisterOneTimeEvt.func1
	/home/vincent/Github/OpenLNMetrics/go-lnmetrics.reporter/internal/plugin/plugin.go:142 +0xac

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.