github.com/MetalBlockchain/subnet-evm@v0.4.9/plugin/evm/client.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package evm
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  
    10  	"github.com/MetalBlockchain/metalgo/api"
    11  	"github.com/MetalBlockchain/metalgo/utils/rpc"
    12  	"github.com/ethereum/go-ethereum/log"
    13  )
    14  
    15  // Interface compliance
    16  var _ Client = (*client)(nil)
    17  
    18  // Client interface for interacting with EVM [chain]
    19  type Client interface {
    20  	StartCPUProfiler(ctx context.Context) error
    21  	StopCPUProfiler(ctx context.Context) error
    22  	MemoryProfile(ctx context.Context) error
    23  	LockProfile(ctx context.Context) error
    24  	SetLogLevel(ctx context.Context, level log.Lvl) error
    25  	GetVMConfig(ctx context.Context) (*Config, error)
    26  }
    27  
    28  // Client implementation for interacting with EVM [chain]
    29  type client struct {
    30  	requester rpc.EndpointRequester
    31  }
    32  
    33  // NewClient returns a Client for interacting with EVM [chain]
    34  func NewClient(uri, chain string) Client {
    35  	return &client{
    36  		requester: rpc.NewEndpointRequester(fmt.Sprintf("%s/ext/bc/%s/admin", uri, chain)),
    37  	}
    38  }
    39  
    40  // NewCChainClient returns a Client for interacting with the C Chain
    41  func NewCChainClient(uri string) Client {
    42  	// TODO: Update for Subnet-EVM compatibility
    43  	return NewClient(uri, "C")
    44  }
    45  
    46  func (c *client) StartCPUProfiler(ctx context.Context) error {
    47  	return c.requester.SendRequest(ctx, "admin.startCPUProfiler", struct{}{}, &api.EmptyReply{})
    48  }
    49  
    50  func (c *client) StopCPUProfiler(ctx context.Context) error {
    51  	return c.requester.SendRequest(ctx, "admin.stopCPUProfiler", struct{}{}, &api.EmptyReply{})
    52  }
    53  
    54  func (c *client) MemoryProfile(ctx context.Context) error {
    55  	return c.requester.SendRequest(ctx, "admin.memoryProfile", struct{}{}, &api.EmptyReply{})
    56  }
    57  
    58  func (c *client) LockProfile(ctx context.Context) error {
    59  	return c.requester.SendRequest(ctx, "admin.lockProfile", struct{}{}, &api.EmptyReply{})
    60  }
    61  
    62  // SetLogLevel dynamically sets the log level for the C Chain
    63  func (c *client) SetLogLevel(ctx context.Context, level log.Lvl) error {
    64  	return c.requester.SendRequest(ctx, "admin.setLogLevel", &SetLogLevelArgs{
    65  		Level: level.String(),
    66  	}, &api.EmptyReply{})
    67  }
    68  
    69  // GetVMConfig returns the current config of the VM
    70  func (c *client) GetVMConfig(ctx context.Context) (*Config, error) {
    71  	res := &ConfigReply{}
    72  	err := c.requester.SendRequest(ctx, "admin.getVMConfig", struct{}{}, res)
    73  	return res.Config, err
    74  }