Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas Management work and Other Features #25

Merged
merged 8 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions config/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ func ExpandHomeDir(path string) string {
return strings.Replace(path, "~", usr.HomeDir, 1)
}

func FileExists(filePath string) (bool, error) {
expanded := ExpandHomeDir(filePath)
exists := os.FileExists(expanded)

return exists, nil
}

func folderExists(folderPath string) (bool, error) {
fileInfo, err := os2.Stat(folderPath)
if err != nil {
Expand Down
27 changes: 23 additions & 4 deletions cosmos/chain-registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func (rc *chainRegistryClient) ChainInfo(ctx context.Context, chainName string)
if err != nil {
return nil, err
}

// Add data to cache
rc.chainNameToChainID[chainName] = chainInfo.ChainID

return chainInfo, nil
}

Expand All @@ -72,13 +76,13 @@ func (rc *chainRegistryClient) ChainNameForChainID(ctx context.Context, targetCh
if refreshCache {
rc.chainNames = []string{}
rc.chainNameToChainID = make(map[string]string)
rc.log.Debug().Msg("reset chain names and chain ids caches")
rc.log.Debug().Msg("reset chain names and chain ids caches per client request")
}

// Fetch chain names if they are not cached, or if we requested a refetch from the cache
chainNames := rc.chainNames
if len(chainNames) == 0 {
rc.log.Debug().Msg("no cached name found, reloading from registry")
rc.log.Debug().Msg("no index of chain names, reloading from registry")

var err error
chainNames, err = rc.AllChainNames(ctx)
Expand All @@ -88,14 +92,17 @@ func (rc *chainRegistryClient) ChainNameForChainID(ctx context.Context, targetCh

rc.log.Debug().Int("num_chains", len(chainNames)).Msg("loaded chains from the registry")
rc.chainNames = chainNames
rc.log.Debug().Int("num_chains", len(rc.chainNames)).Msg("updated chain name index in client")
}

// For each chain name, get the chain id from the cache or from fetching
for _, chainName := range chainNames {
rc.log.Debug().Str("chain_name", chainName).Msg("processing chain")
for chainIdx, chainName := range chainNames {
rc.log.Debug().Str("chain_name", chainName).Int("chain_index", chainIdx).Msg("processing chain")

chainID, isSet := rc.chainNameToChainID[chainName]
if !isSet {
rc.log.Debug().Str("chain_name", chainName).Msg("no chain data found in cache, requesting from registry")

// Fetch the chain ID for the chain
// NOTE: No retries because GetChainInfo manages that for us.
chainInfo, err := rc.ChainInfo(ctx, chainName)
Expand All @@ -105,6 +112,11 @@ func (rc *chainRegistryClient) ChainNameForChainID(ctx context.Context, targetCh
}

chainID = chainInfo.ChainID

// Set in cache
rc.chainNameToChainID[chainName] = chainID
} else {
rc.log.Debug().Str("chain_name", chainName).Msg("found chain id in cache")
}

if strings.EqualFold(targetChainID, chainID) {
Expand Down Expand Up @@ -179,8 +191,15 @@ func (rc *chainRegistryClient) makeRequest(ctx context.Context, url string) ([]b
if err != nil {
return nil, err
}
rc.log.Debug().Msg("received http 200 response from chain registry")

return data, nil
} else {
data, err := io.ReadAll(resp.Body)
if err == nil {
rc.log.Debug().Str("response", string(data)).Int("status_code", resp.StatusCode).Msg("received bad response from chain registry")
}

return nil, fmt.Errorf("received non-OK HTTP status: %d", resp.StatusCode)
}
}
Expand Down
31 changes: 3 additions & 28 deletions cosmos/rpc/grpc-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"github.com/tessellated-io/pickaxe/cosmos/util"
"github.com/tessellated-io/pickaxe/grpc"
"github.com/tessellated-io/pickaxe/log"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -26,7 +24,7 @@ import (
// Page size to use
const pageSize = 100

// grpcClient is the private and default implementation for restake.
// grpcClient is the private and default implementation.
type grpcClient struct {
cdc *codec.ProtoCodec

Expand All @@ -49,7 +47,7 @@ type paginatedRpcResponse[dataType any] struct {
// Ensure that grpcClient implements RpcClient
var _ RpcClient = (*grpcClient)(nil)

// NewRpcClient makes a new RpcClient for the Restake Go App.
// NewRpcClient makes a new RpcClient.
func NewGrpcClient(nodeGrpcUri string, cdc *codec.ProtoCodec, log *log.Logger) (RpcClient, error) {
conn, err := grpc.GetGrpcConnection(nodeGrpcUri)
if err != nil {
Expand Down Expand Up @@ -151,30 +149,7 @@ func (r *grpcClient) Broadcast(
)
}

func (r *grpcClient) CheckIncluded(ctx context.Context, txHash string) (bool, error) {
txStatus, err := r.getTxStatus(ctx, txHash)
if err != nil {
// Check if the error was gRPC not found
grpcErr, ok := status.FromError(err)
if ok && grpcErr.Code() == codes.NotFound {
return false, nil
} else if ok {
// TODO: Deleteme
fmt.Println("FYI, unwrapped grpc error to ", grpcErr)
}

return false, err
}

height := txStatus.TxResponse.Height
if height != 0 {
return true, nil
} else {
return false, nil
}
}

func (r *grpcClient) getTxStatus(ctx context.Context, txHash string) (*txtypes.GetTxResponse, error) {
func (r *grpcClient) GetTxStatus(ctx context.Context, txHash string) (*txtypes.GetTxResponse, error) {
request := &txtypes.GetTxRequest{Hash: txHash}
return r.txClient.GetTx(ctx, request)
}
Expand Down
14 changes: 7 additions & 7 deletions cosmos/rpc/retryable_rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,26 @@ func (r *retryableRpcClient) Broadcast(ctx context.Context, txBytes []byte) (*tx
return result, nil
}

func (r *retryableRpcClient) CheckIncluded(ctx context.Context, txHash string) (bool, error) {
var result bool
func (r *retryableRpcClient) GetTxStatus(ctx context.Context, txHash string) (*txtypes.GetTxResponse, error) {
var result *txtypes.GetTxResponse
var err error

err = retry.Do(func() error {
result, err = r.wrappedClient.CheckIncluded(ctx, txHash)

result, err = r.wrappedClient.GetTxStatus(ctx, txHash)
if err != nil {
r.logger.Error().Err(err).Str("method", "check_included").Msg("failed call in rpc client, will retry")
r.logger.Error().Err(err).Str("method", "tx_status").Msg("failed call in rpc client, will retry")
}

return err
}, r.delay, r.attempts, retry.Context(ctx))

if err != nil {
// If err is an error from a context, unwrapping will write out nil
unwrappedErr := errors.Unwrap(err)
if unwrappedErr != nil {
return result, unwrappedErr
return nil, unwrappedErr
} else {
return result, err
return nil, err
}
}

Expand Down
4 changes: 2 additions & 2 deletions cosmos/rpc/rpc-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

// Handles RPCs for Restake
// Handles RPCs with Tendermint nodes
type RpcClient interface {
Broadcast(ctx context.Context, txBytes []byte) (*txtypes.BroadcastTxResponse, error)
CheckIncluded(ctx context.Context, txHash string) (bool, error)

Simulate(ctx context.Context, txBytes []byte) (*txtypes.SimulateResponse, error)

Expand All @@ -24,4 +23,5 @@ type RpcClient interface {
GetDenomMetadata(ctx context.Context, denom string) (*banktypes.Metadata, error)
GetGrants(ctx context.Context, botAddress string) ([]*authztypes.GrantAuthorization, error)
GetPendingRewards(ctx context.Context, delegator, validator, stakingDenom string) (sdk.Dec, error)
GetTxStatus(ctx context.Context, txHash string) (*txtypes.GetTxResponse, error)
}
5 changes: 4 additions & 1 deletion cosmos/tx/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ package tx

import "errors"

var ErrNoGasPrice = errors.New("no known gas price")
var (
ErrNoGasPrice = errors.New("no known gas price")
ErrNoGasFactor = errors.New("no known gas factor")
)
Loading