Skip to content

Commit

Permalink
refactor: alternate consensus engines (#1490)
Browse files Browse the repository at this point in the history
* feat: `ConsensusClient` interface & comet integration

* chore: cometbft bytes lib

* refactor: migrate to ConsensusClient

* rename

* simplify

* refactor: just use cometbft bytes. helper lib for now

* refactor: rename rclient -> cclient (consensus client)

* remove WithClient
  • Loading branch information
Reecepbcups authored Sep 16, 2024
1 parent d740575 commit 4e4e953
Show file tree
Hide file tree
Showing 14 changed files with 359 additions and 115 deletions.
45 changes: 22 additions & 23 deletions client/client_wrapper.go → cclient/cmbft_client_wrapper.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package client
package cclient

import (
"context"
Expand Down 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 NewCometRPCClient(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 All @@ -388,8 +388,7 @@ func (r RPCClient) BlockSearch(
}

return &coretypes.ResultBlockSearch{
Blocks: blocks,
TotalCount: res.TotalCount,
Blocks: blocks,
}, nil
}

Expand Down
153 changes: 153 additions & 0 deletions cclient/cmbft_consensus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package cclient

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"
)

var _ ConsensusClient = (*CometRPCClient)(nil)

// GetBlock implements ConsensusClient.
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 ConsensusClient.
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 ConsensusClient.
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 ConsensusClient.
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 resp, nil
}

// GetTxSearch implements ConsensusClient.
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,
}, nil
}

// GetBlockSearch implements ConsensusClient.
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 ConsensusClient.
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 ConsensusClient.
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)
}

return &ResultValidators{
Validators: v.Validators,
}, nil
}

// DoBroadcastTxAsync implements ConsensusClient.
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: b.Data,
Log: b.Log,
Codespace: b.Codespace,
Hash: b.Hash,
}, nil
}

// DoBroadcastTxSync implements ConsensusClient.
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: b.Data,
Log: b.Log,
Codespace: b.Codespace,
Hash: b.Hash,
}, nil
}

// GetABCIQueryWithOptions implements ConsensusClient.
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 ConsensusClient.
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 4e4e953

Please sign in to comment.