diff --git a/client/rpc.go b/client/rpc.go index 7a431c5..832bca0 100644 --- a/client/rpc.go +++ b/client/rpc.go @@ -1,7 +1,6 @@ package client import ( - "context" "encoding/json" "fmt" "github.com/BlocSoc-iitr/selene/common" @@ -29,18 +28,36 @@ func (r Rpc) New(node *Node, ip *string, port uint16) Rpc { Address: address, } } -func (r Rpc) Start(ctx context.Context) (string, error) { - rpcInner := RpcInner{ - Node: r.Node, - Address: r.Address, - } - handle, addr, err := rpcInner.StartServer() - if err != nil { - return "", err - } - r.Handle = handle - logrus.WithField("target", "selene::rpc").Infof("rpc server started at %s", addr) - return addr, nil + +func (r Rpc) Start() (string, error) { + // Create channels to send the address and error back asynchronously + addrChan := make(chan string) + errChan := make(chan error) + + // Run the function in a separate goroutine + go func() { + rpcInner := RpcInner{ + Node: r.Node, + Address: r.Address, + } + handle, addr, err := rpcInner.StartServer() + if err != nil { + // Send the error on the error channel and close the channels + errChan <- err + close(addrChan) + close(errChan) + return + } + r.Handle = handle + logrus.WithField("target", "selene::rpc").Infof("rpc server started at %s", addr) + + // Send the address on the address channel and close the channels + addrChan <- addr + close(addrChan) + close(errChan) + }() + + return <-addrChan, <-errChan } type RpcInner struct {