fixed proto message
This commit is contained in:
@@ -19,7 +19,12 @@ import (
|
||||
// Clients holds pre-initialised RPC clients keyed by network name.
|
||||
type Clients struct {
|
||||
logger mlogger.Logger
|
||||
clients map[string]*ethclient.Client
|
||||
clients map[string]clientEntry
|
||||
}
|
||||
|
||||
type clientEntry struct {
|
||||
eth *ethclient.Client
|
||||
rpc *rpc.Client
|
||||
}
|
||||
|
||||
// Prepare dials all configured networks up front and returns a ready-to-use client set.
|
||||
@@ -30,7 +35,7 @@ func Prepare(ctx context.Context, logger mlogger.Logger, networks []shared.Netwo
|
||||
clientLogger := logger.Named("rpc_client")
|
||||
result := &Clients{
|
||||
logger: clientLogger,
|
||||
clients: make(map[string]*ethclient.Client),
|
||||
clients: make(map[string]clientEntry),
|
||||
}
|
||||
|
||||
for _, network := range networks {
|
||||
@@ -55,9 +60,10 @@ func Prepare(ctx context.Context, logger mlogger.Logger, networks []shared.Netwo
|
||||
dialCtx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
||||
httpClient := &http.Client{
|
||||
Transport: &loggingRoundTripper{
|
||||
logger: clientLogger,
|
||||
network: name,
|
||||
base: http.DefaultTransport,
|
||||
logger: clientLogger,
|
||||
network: name,
|
||||
endpoint: rpcURL,
|
||||
base: http.DefaultTransport,
|
||||
},
|
||||
}
|
||||
rpcCli, err := rpc.DialOptions(dialCtx, rpcURL, rpc.WithHTTPClient(httpClient))
|
||||
@@ -68,8 +74,10 @@ func Prepare(ctx context.Context, logger mlogger.Logger, networks []shared.Netwo
|
||||
return nil, merrors.Internal(fmt.Sprintf("rpc dial failed for %s: %s", name, err.Error()))
|
||||
}
|
||||
client := ethclient.NewClient(rpcCli)
|
||||
|
||||
result.clients[name] = client
|
||||
result.clients[name] = clientEntry{
|
||||
eth: client,
|
||||
rpc: rpcCli,
|
||||
}
|
||||
clientLogger.Info("rpc client ready", fields...)
|
||||
}
|
||||
|
||||
@@ -89,11 +97,24 @@ func (c *Clients) Client(network string) (*ethclient.Client, error) {
|
||||
return nil, merrors.Internal("rpc clients not initialised")
|
||||
}
|
||||
name := strings.ToLower(strings.TrimSpace(network))
|
||||
client, ok := c.clients[name]
|
||||
if !ok {
|
||||
entry, ok := c.clients[name]
|
||||
if !ok || entry.eth == nil {
|
||||
return nil, merrors.InvalidArgument(fmt.Sprintf("rpc client not configured for network %s", name))
|
||||
}
|
||||
return client, nil
|
||||
return entry.eth, nil
|
||||
}
|
||||
|
||||
// RPCClient returns the raw RPC client for low-level calls.
|
||||
func (c *Clients) RPCClient(network string) (*rpc.Client, error) {
|
||||
if c == nil {
|
||||
return nil, merrors.Internal("rpc clients not initialised")
|
||||
}
|
||||
name := strings.ToLower(strings.TrimSpace(network))
|
||||
entry, ok := c.clients[name]
|
||||
if !ok || entry.rpc == nil {
|
||||
return nil, merrors.InvalidArgument(fmt.Sprintf("rpc client not configured for network %s", name))
|
||||
}
|
||||
return entry.rpc, nil
|
||||
}
|
||||
|
||||
// Close tears down all RPC clients, logging each close.
|
||||
@@ -101,8 +122,12 @@ func (c *Clients) Close() {
|
||||
if c == nil {
|
||||
return
|
||||
}
|
||||
for name, client := range c.clients {
|
||||
client.Close()
|
||||
for name, entry := range c.clients {
|
||||
if entry.rpc != nil {
|
||||
entry.rpc.Close()
|
||||
} else if entry.eth != nil {
|
||||
entry.eth.Close()
|
||||
}
|
||||
if c.logger != nil {
|
||||
c.logger.Info("rpc client closed", zap.String("network", name))
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
"github.com/tech/sendico/gateway/chain/internal/service/gateway/shared"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
)
|
||||
@@ -39,6 +40,14 @@ func (r *Registry) Client(key string) (*ethclient.Client, error) {
|
||||
return r.clients.Client(strings.ToLower(strings.TrimSpace(key)))
|
||||
}
|
||||
|
||||
// RPCClient returns the raw RPC client for low-level calls.
|
||||
func (r *Registry) RPCClient(key string) (*rpc.Client, error) {
|
||||
if r == nil || r.clients == nil {
|
||||
return nil, merrors.Internal("rpc clients not initialised")
|
||||
}
|
||||
return r.clients.RPCClient(strings.ToLower(strings.TrimSpace(key)))
|
||||
}
|
||||
|
||||
// Networks exposes the registry map for iteration when needed.
|
||||
func (r *Registry) Networks() map[string]shared.Network {
|
||||
return r.networks
|
||||
|
||||
Reference in New Issue
Block a user