Skip to content

Commit

Permalink
feat: ConsensusClient interface & comet integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Sep 2, 2024
1 parent 4ed2615 commit 81c9e37
Show file tree
Hide file tree
Showing 3 changed files with 305 additions and 20 deletions.
40 changes: 20 additions & 20 deletions client/client_wrapper.go → client/cmbft_client_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ import (
types2 "github.com/strangelove-ventures/cometbft-client/types"
)

// RPCClient wraps our slimmed down CometBFT client and converts the returned types to the upstream CometBFT types.
// CometRPCClient wraps our slimmed down CometBFT client and converts the returned types to the upstream CometBFT types.
// This is useful so that it can be used in any function calls that expect the upstream types.
type RPCClient struct {
type CometRPCClient struct {
c *client.Client
}

func NewRPCClient(c *client.Client) RPCClient {
return RPCClient{c: c}
func NewRPCClient(c *client.Client) CometRPCClient {
return CometRPCClient{c: c}
}

func (r RPCClient) ABCIInfo(ctx context.Context) (*coretypes.ResultABCIInfo, error) {
func (r CometRPCClient) ABCIInfo(ctx context.Context) (*coretypes.ResultABCIInfo, error) {
res, err := r.c.ABCIInfo(ctx)
if err != nil {
return nil, err
Expand All @@ -55,7 +55,7 @@ func (r RPCClient) ABCIInfo(ctx context.Context) (*coretypes.ResultABCIInfo, err
}, nil
}

func (r RPCClient) ABCIQuery(
func (r CometRPCClient) ABCIQuery(
ctx context.Context,
path string,
data bytes.HexBytes,
Expand All @@ -68,7 +68,7 @@ func (r RPCClient) ABCIQuery(
return convertResultABCIQuery(res), nil
}

func (r RPCClient) ABCIQueryWithOptions(
func (r CometRPCClient) ABCIQueryWithOptions(
ctx context.Context,
path string,
data bytes.HexBytes,
Expand All @@ -87,7 +87,7 @@ func (r RPCClient) ABCIQueryWithOptions(
return convertResultABCIQuery(res), nil
}

func (r RPCClient) BroadcastTxCommit(ctx context.Context, tx tmtypes.Tx) (*coretypes.ResultBroadcastTxCommit, error) {
func (r CometRPCClient) BroadcastTxCommit(ctx context.Context, tx tmtypes.Tx) (*coretypes.ResultBroadcastTxCommit, error) {
res, err := r.c.BroadcastTxCommit(ctx, types2.Tx(tx))
if err != nil {
return nil, err
Expand Down Expand Up @@ -119,7 +119,7 @@ func (r RPCClient) BroadcastTxCommit(ctx context.Context, tx tmtypes.Tx) (*coret
}, nil
}

func (r RPCClient) BroadcastTxAsync(ctx context.Context, tx tmtypes.Tx) (*coretypes.ResultBroadcastTx, error) {
func (r CometRPCClient) BroadcastTxAsync(ctx context.Context, tx tmtypes.Tx) (*coretypes.ResultBroadcastTx, error) {
res, err := r.c.BroadcastTxAsync(ctx, types2.Tx(tx))
if err != nil {
return nil, err
Expand All @@ -134,7 +134,7 @@ func (r RPCClient) BroadcastTxAsync(ctx context.Context, tx tmtypes.Tx) (*corety
}, nil
}

func (r RPCClient) BroadcastTxSync(ctx context.Context, tx tmtypes.Tx) (*coretypes.ResultBroadcastTx, error) {
func (r CometRPCClient) BroadcastTxSync(ctx context.Context, tx tmtypes.Tx) (*coretypes.ResultBroadcastTx, error) {
res, err := r.c.BroadcastTxSync(ctx, types2.Tx(tx))
if err != nil {
return nil, err
Expand All @@ -149,7 +149,7 @@ func (r RPCClient) BroadcastTxSync(ctx context.Context, tx tmtypes.Tx) (*coretyp
}, nil
}

func (r RPCClient) Validators(
func (r CometRPCClient) Validators(
ctx context.Context,
height *int64,
page, perPage *int,
Expand Down Expand Up @@ -177,7 +177,7 @@ func (r RPCClient) Validators(
}, nil
}

func (r RPCClient) Status(ctx context.Context) (*coretypes.ResultStatus, error) {
func (r CometRPCClient) Status(ctx context.Context) (*coretypes.ResultStatus, error) {
res, err := r.c.Status(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -220,7 +220,7 @@ func (r RPCClient) Status(ctx context.Context) (*coretypes.ResultStatus, error)
}, nil
}

func (r RPCClient) Block(ctx context.Context, height *int64) (*coretypes.ResultBlock, error) {
func (r CometRPCClient) Block(ctx context.Context, height *int64) (*coretypes.ResultBlock, error) {
res, err := r.c.Block(ctx, height)
if err != nil {
return nil, err
Expand All @@ -232,7 +232,7 @@ func (r RPCClient) Block(ctx context.Context, height *int64) (*coretypes.ResultB
}, nil
}

func (r RPCClient) BlockByHash(ctx context.Context, hash []byte) (*coretypes.ResultBlock, error) {
func (r CometRPCClient) BlockByHash(ctx context.Context, hash []byte) (*coretypes.ResultBlock, error) {
res, err := r.c.BlockByHash(ctx, hash)
if err != nil {
return nil, err
Expand All @@ -244,7 +244,7 @@ func (r RPCClient) BlockByHash(ctx context.Context, hash []byte) (*coretypes.Res
}, nil
}

func (r RPCClient) BlockResults(ctx context.Context, height *int64) (*coretypes.ResultBlockResults, error) {
func (r CometRPCClient) BlockResults(ctx context.Context, height *int64) (*coretypes.ResultBlockResults, error) {
res, err := r.c.BlockResults(ctx, height)
if err != nil {
return nil, err
Expand Down Expand Up @@ -274,7 +274,7 @@ func (r RPCClient) BlockResults(ctx context.Context, height *int64) (*coretypes.
}, nil
}

func (r RPCClient) BlockchainInfo(
func (r CometRPCClient) BlockchainInfo(
ctx context.Context,
minHeight, maxHeight int64,
) (*coretypes.ResultBlockchainInfo, error) {
Expand Down Expand Up @@ -305,7 +305,7 @@ func (r RPCClient) BlockchainInfo(
}, nil
}

func (r RPCClient) Commit(ctx context.Context, height *int64) (*coretypes.ResultCommit, error) {
func (r CometRPCClient) Commit(ctx context.Context, height *int64) (*coretypes.ResultCommit, error) {
res, err := r.c.Commit(ctx, height)
if err != nil {
return nil, err
Expand Down Expand Up @@ -336,7 +336,7 @@ func (r RPCClient) Commit(ctx context.Context, height *int64) (*coretypes.Result
}, nil
}

func (r RPCClient) Tx(ctx context.Context, hash []byte, prove bool) (*coretypes.ResultTx, error) {
func (r CometRPCClient) Tx(ctx context.Context, hash []byte, prove bool) (*coretypes.ResultTx, error) {
res, err := r.c.Tx(ctx, hash, prove)
if err != nil {
return nil, err
Expand All @@ -345,7 +345,7 @@ func (r RPCClient) Tx(ctx context.Context, hash []byte, prove bool) (*coretypes.
return convertResultTx(res), nil
}

func (r RPCClient) TxSearch(
func (r CometRPCClient) TxSearch(
ctx context.Context,
query string,
prove bool,
Expand All @@ -368,7 +368,7 @@ func (r RPCClient) TxSearch(
}, nil
}

func (r RPCClient) BlockSearch(
func (r CometRPCClient) BlockSearch(
ctx context.Context,
query string,
page, perPage *int,
Expand Down
173 changes: 173 additions & 0 deletions client/cmbft_consensus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package client

import (
"context"
"fmt"
"time"

"github.com/cometbft/cometbft/libs/bytes"
rpcclient "github.com/cometbft/cometbft/rpc/client"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
tmtypes "github.com/cometbft/cometbft/types"
rbytes "github.com/cosmos/relayer/v2/client/bytes"
)

// ConsensusRelayerI is the itnerface we will use across the relayer so we can swap out the underlying consensus engine client.
var _ ConsensusClient = (*CometRPCClient)(nil)

// GetBlock implements ConsensusRelayerI.
func (r CometRPCClient) GetBlockTime(ctx context.Context, height uint64) (time.Time, error) {
h := int64(height)

b, err := r.Block(ctx, &h)
if err != nil {
return time.Time{}, fmt.Errorf("failed to get block: %w", err)
}

return b.Block.Header.Time, nil
}

// GetBlockResults implements ConsensusRelayerI.
func (r CometRPCClient) GetBlockResults(ctx context.Context, height uint64) (*BlockResults, error) {
h := int64(height)
br, err := r.BlockResults(ctx, &h)
if err != nil {
return nil, fmt.Errorf("failed to get block results: %w", err)
}
return &BlockResults{
TxsResults: br.TxsResults,
FinalizeBlockEvents: br.FinalizeBlockEvents,
}, nil
}

// GetABCIQuery implements ConsensusRelayerI.
func (r CometRPCClient) GetABCIQuery(ctx context.Context, queryPath string, data bytes.HexBytes) (*ABCIQueryResponse, error) {
resp, err := r.ABCIQuery(ctx, queryPath, data)
if err != nil {
return nil, fmt.Errorf("failed to get ABCI query: %w", err)
}
return &ABCIQueryResponse{
Code: resp.Response.Code,
Value: resp.Response.Value,
}, nil
}

// GetTx implements ConsensusRelayerI.
func (r CometRPCClient) GetTx(ctx context.Context, hash []byte, prove bool) (*coretypes.ResultTx, error) {
resp, err := r.Tx(ctx, hash, prove)
if err != nil {
return nil, fmt.Errorf("failed to get tx: %w", err)
}
// return &Transaction{
// Height: uint64(resp.Height),
// TxHash: resp.Hash,
// Code: resp.TxResult.Code,
// Data: string(resp.TxResult.Data),
// Events: resp.TxResult.Events,
// }, nil
return resp, nil
}

// GetTxSearch implements ConsensusRelayerI.
func (r CometRPCClient) GetTxSearch(ctx context.Context, query string, prove bool, page *int, perPage *int, orderBy string) (*ResultTxSearch, error) {
resp, err := r.TxSearch(ctx, query, prove, page, perPage, orderBy)
if err != nil {
return nil, fmt.Errorf("failed to get tx search: %w", err)
}
return &ResultTxSearch{
Txs: resp.Txs,
TotalCount: resp.TotalCount,
}, nil
}

// GetBlockSearch implements ConsensusRelayerI.
func (r CometRPCClient) GetBlockSearch(ctx context.Context, query string, page *int, perPage *int, orderBy string) (*coretypes.ResultBlockSearch, error) {
resp, err := r.BlockSearch(ctx, query, page, perPage, orderBy)
if err != nil {
return nil, fmt.Errorf("failed to get block search: %w", err)
}
return resp, nil
}

// GetCommit implements ConsensusRelayerI.
func (r CometRPCClient) GetCommit(ctx context.Context, height uint64) (*coretypes.ResultCommit, error) {
h := int64(height)
c, err := r.Commit(ctx, &h)
if err != nil {
return nil, fmt.Errorf("failed to get commit: %w", err)
}
return c, nil
}

// GetValidators implements ConsensusRelayerI.
func (r CometRPCClient) GetValidators(ctx context.Context, height *int64, page *int, perPage *int) (*ResultValidators, error) {
v, err := r.Validators(ctx, height, page, perPage)
if err != nil {
return nil, fmt.Errorf("failed to get validators: %w", err)
}

vals := make([]*tmtypes.Validator, len(v.Validators))
for i, val := range v.Validators {
vals[i] = &tmtypes.Validator{
Address: val.Address,
PubKey: val.PubKey,
VotingPower: val.VotingPower,
ProposerPriority: val.ProposerPriority,
}
}

return &ResultValidators{
Validators: vals,
}, nil
}

// DoBroadcastTxAsync implements ConsensusRelayerI.
func (r CometRPCClient) DoBroadcastTxAsync(ctx context.Context, tx tmtypes.Tx) (*ResultBroadcastTx, error) {
b, err := r.BroadcastTxAsync(ctx, tx)
if err != nil {
return nil, fmt.Errorf("failed to broadcast tx async: %w", err)
}
return &ResultBroadcastTx{
Code: b.Code,
Data: rbytes.ConvertCometBFTToHexBytes(b.Data),
Log: b.Log,
Codespace: b.Codespace,
Hash: rbytes.ConvertCometBFTToHexBytes(b.Hash),
}, nil
}

// DoBroadcastTxSync implements ConsensusRelayerI.
func (r CometRPCClient) DoBroadcastTxSync(ctx context.Context, tx tmtypes.Tx) (*ResultBroadcastTx, error) {
b, err := r.BroadcastTxSync(ctx, tx)
if err != nil {
return nil, fmt.Errorf("failed to broadcast tx sync: %w", err)
}
return &ResultBroadcastTx{
Code: b.Code,
Data: rbytes.ConvertCometBFTToHexBytes(b.Data),
Log: b.Log,
Codespace: b.Codespace,
Hash: rbytes.ConvertCometBFTToHexBytes(b.Hash),
}, nil
}

// GetABCIQueryWithOptions implements ConsensusRelayerI.
func (r CometRPCClient) GetABCIQueryWithOptions(ctx context.Context, path string, data bytes.HexBytes, opts rpcclient.ABCIQueryOptions) (*coretypes.ResultABCIQuery, error) {
q, err := r.ABCIQueryWithOptions(ctx, path, data, opts)
if err != nil {
return nil, fmt.Errorf("failed to get ABCI query with options: %w", err)
}
return q, nil
}

// GetStatus implements ConsensusRelayerI.
func (r CometRPCClient) GetStatus(ctx context.Context) (*Status, error) {
s, err := r.Status(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get status: %w", err)
}
return &Status{
CatchingUp: s.SyncInfo.CatchingUp,
LatestBlockHeight: uint64(s.SyncInfo.LatestBlockHeight),
}, nil
}
Loading

0 comments on commit 81c9e37

Please sign in to comment.