code.vegaprotocol.io/vega@v0.79.0/core/admin/client.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package admin
    17  
    18  import (
    19  	"bytes"
    20  	"context"
    21  	"fmt"
    22  	"net"
    23  	"net/http"
    24  	"net/url"
    25  
    26  	"code.vegaprotocol.io/vega/core/types"
    27  
    28  	"github.com/gorilla/rpc/json"
    29  )
    30  
    31  // Client implement a socket client allowing to run simple RPC commands.
    32  type Client struct {
    33  	cfg  Config
    34  	http *http.Client
    35  }
    36  
    37  // NewClient returns a new instance of the RPC socket client.
    38  func NewClient(config Config) *Client {
    39  	return &Client{
    40  		cfg: config,
    41  		http: &http.Client{
    42  			Transport: &http.Transport{
    43  				DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
    44  					return net.Dial("unix", config.Server.SocketPath)
    45  				},
    46  			},
    47  		},
    48  	}
    49  }
    50  
    51  func (s *Client) call(ctx context.Context, method string, args interface{}, reply interface{}) error {
    52  	req, err := json.EncodeClientRequest(method, args)
    53  	if err != nil {
    54  		return fmt.Errorf("failed to encode client JSON request: %w", err)
    55  	}
    56  
    57  	u := url.URL{
    58  		Scheme: "http",
    59  		Host:   "unix",
    60  		Path:   s.cfg.Server.HTTPPath,
    61  	}
    62  
    63  	httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), bytes.NewReader(req))
    64  	if err != nil {
    65  		return fmt.Errorf("failed to create HTTP request: %w", err)
    66  	}
    67  
    68  	httpReq.Header.Set("Content-Type", "application/json")
    69  
    70  	resp, err := s.http.Do(httpReq)
    71  	if err != nil {
    72  		return fmt.Errorf("request failed: %w", err)
    73  	}
    74  	defer resp.Body.Close()
    75  
    76  	if err := json.DecodeClientResponse(resp.Body, reply); err != nil {
    77  		return fmt.Errorf("failed to decode client JSON response: %w", err)
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  func (s *Client) NodeWalletReload(ctx context.Context, chain string) (*NodeWalletReloadReply, error) {
    84  	var reply NodeWalletReloadReply
    85  
    86  	if err := s.call(ctx, "NodeWallet.Reload", NodeWalletArgs{Chain: chain}, &reply); err != nil {
    87  		return nil, fmt.Errorf("failed to call NodeWallet.Reload method: %w", err)
    88  	}
    89  
    90  	return &reply, nil
    91  }
    92  
    93  func (s *Client) NodeWalletShow(ctx context.Context, chain string) (*Wallet, error) {
    94  	var reply Wallet
    95  
    96  	if err := s.call(ctx, "NodeWallet.Show", NodeWalletArgs{Chain: chain}, &reply); err != nil {
    97  		return nil, fmt.Errorf("failed to call NodeWallet.Show method: %w", err)
    98  	}
    99  
   100  	return &reply, nil
   101  }
   102  
   103  func (s *Client) UpgradeStatus(ctx context.Context) (*types.UpgradeStatus, error) {
   104  	var reply types.UpgradeStatus
   105  
   106  	if err := s.call(ctx, "protocolupgrade.UpgradeStatus", nil, &reply); err != nil {
   107  		return nil, fmt.Errorf("failed to get protocol update status: %w", err)
   108  	}
   109  
   110  	return &reply, nil
   111  }