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

feat: gordian consensus support #1492

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
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
149 changes: 149 additions & 0 deletions cclient/consensus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package cclient

import (
"context"
"strings"
"time"

abci "github.com/cometbft/cometbft/abci/types"
bytes "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"
types "github.com/cosmos/cosmos-sdk/types"
)

// TODO(reece): get off cometbft types into internal relayer.
type ConsensusClient interface {
GetBlockTime(ctx context.Context, height uint64) (time.Time, error)
GetStatus(ctx context.Context) (*Status, error)
GetBlockResults(ctx context.Context, height uint64) (*BlockResults, error)
GetABCIQuery(ctx context.Context, queryPath string, data bytes.HexBytes) (*ABCIQueryResponse, error)
GetValidators(
ctx context.Context,
height *int64,
page, perPage *int,
) (*ResultValidators, error)
GetTxSearch(
ctx context.Context,
query string,
prove bool,
page, perPage *int,
orderBy string,
) (*ResultTxSearch, error)
DoBroadcastTxSync(ctx context.Context, tx []byte) (*TxResultResponse, error) // TODO: is tx []byte fine or does it need to be tx tmtypes.Tx?
DoBroadcastTxAsync(ctx context.Context, tx tmtypes.Tx) (*TxResultResponse, error)
GetTx(ctx context.Context, hash []byte, prove bool) (*coretypes.ResultTx, error)
GetBlockSearch(
ctx context.Context,
query string,
page, perPage *int,
orderBy string,
) (*coretypes.ResultBlockSearch, error)
GetCommit(ctx context.Context, height uint64) (*ResultCommit, error)
GetABCIQueryWithOptions(
ctx context.Context,
path string,
data bytes.HexBytes,
opts rpcclient.ABCIQueryOptions,
) (*coretypes.ResultABCIQuery, error)

SimulateTransaction(ctx context.Context, tx []byte, cfg *SimTxConfig) (types.GasInfo, error)
}

type ResultCommit struct {
Time time.Time `json:"time"`
AppHash []byte `json:"app_hash"`
}

type SimTxConfig struct {
// CometBFT only function (QueryABCI).
QueryABCIFunc func(ctx context.Context, req abci.RequestQuery) (abci.ResponseQuery, error)
}

type Status struct {
CatchingUp bool
LatestBlockHeight uint64
}

type BlockResults struct {
FinalizeBlockEvents []abci.Event `json:"finalize_block_events"`
TxsResults []*abci.ExecTxResult `json:"txs_results"`
}

type ABCIQueryResponse struct {
Code uint32 `json:"code,omitempty"`
Value []byte `json:"value,omitempty"`
}

// The response value contains the data link escape control character which must be removed before parsing.
func (q ABCIQueryResponse) ValueCleaned() string {
return strings.ReplaceAll(strings.TrimSpace(string(q.Value)), "\u0010", "")
}

// coretypes.ResultTxSearch
type ResultTxSearch struct {
Txs []*coretypes.ResultTx `json:"txs"`
}

type ResultValidators struct {
Validators []*tmtypes.Validator `json:"validators"`
}

// type Validator struct {
// Address crypto.Address `json:"address"`
// PubKey crypto.PubKey `json:"pub_key"`
// VotingPower int64 `json:"voting_power"`
// ProposerPriority int64 `json:"proposer_priority"`
// }

type ResultBroadcastTx struct {
Code uint32 `json:"code"`
Data bytes.HexBytes `json:"data"`
Log string `json:"log"`
Codespace string `json:"codespace"`
Hash bytes.HexBytes `json:"hash"`
}

type TxResultResponse struct {
Events []*Event `protobuf:"bytes,1,rep,name=events,proto3" json:"events,omitempty"`
// bytes resp = 2; // []transaction.Msg
Error string `protobuf:"bytes,3,opt,name=error,proto3" json:"error,omitempty"`
Code uint32 `protobuf:"varint,4,opt,name=code,proto3" json:"code,omitempty"`
Data []byte `protobuf:"bytes,5,opt,name=data,proto3" json:"data,omitempty"`
Log string `protobuf:"bytes,6,opt,name=log,proto3" json:"log,omitempty"`
Info string `protobuf:"bytes,7,opt,name=info,proto3" json:"info,omitempty"`
GasWanted uint64 `protobuf:"varint,8,opt,name=gas_wanted,proto3" json:"gas_wanted,omitempty"`
GasUsed uint64 `protobuf:"varint,9,opt,name=gas_used,proto3" json:"gas_used,omitempty"`
Codespace string `protobuf:"bytes,10,opt,name=codespace,proto3" json:"codespace,omitempty"`
TxHash string `protobuf:"bytes,11,opt,name=tx_hash,proto3" json:"tx_hash,omitempty"`
}

type Event struct {
Type string `protobuf:"bytes,1,opt,name=type,proto3" json:"type,omitempty"`
Attributes []*EventAttribute `protobuf:"bytes,2,rep,name=attributes,proto3" json:"attributes,omitempty"`
}

func convertConsensusEvents(e []*Event) []abci.Event {
events := make([]abci.Event, len(e))
for _, ev := range e {
attributes := make([]abci.EventAttribute, len(ev.Attributes))
for idx, attr := range ev.Attributes {
attributes[idx] = abci.EventAttribute{
Key: attr.Key,
Value: attr.Value,
}
}

events = append(events, abci.Event{
Type: ev.Type,
Attributes: attributes,
})
}
return events
}

type EventAttribute struct {
Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
}
Loading
Loading