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

refactor: alternate consensus engines #1490

Merged
merged 9 commits into from
Sep 16, 2024
75 changes: 75 additions & 0 deletions client/bytes/bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// FROM: COMETBFT
// IBC-Go also uses this package. Maybe the SDK in v2 upstreams this?

package bytes

import (
"encoding/hex"
"fmt"
"strings"

"github.com/cometbft/cometbft/libs/bytes"
)

// HexBytes enables HEX-encoding for json/encoding.
type HexBytes []byte

// Marshal needed for protobuf compatibility
func (bz HexBytes) Marshal() ([]byte, error) {
return bz, nil
}

// Unmarshal needed for protobuf compatibility
func (bz *HexBytes) Unmarshal(data []byte) error {
*bz = data
return nil
}

// This is the point of Bytes.
func (bz HexBytes) MarshalJSON() ([]byte, error) {
s := strings.ToUpper(hex.EncodeToString(bz))
jbz := make([]byte, len(s)+2)
jbz[0] = '"'
copy(jbz[1:], s)
jbz[len(jbz)-1] = '"'
return jbz, nil
}

// This is the point of Bytes.
func (bz *HexBytes) UnmarshalJSON(data []byte) error {
if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' {
return fmt.Errorf("invalid hex string: %s", data)
}
bz2, err := hex.DecodeString(string(data[1 : len(data)-1]))
if err != nil {
return err
}
*bz = bz2
return nil
}

// Bytes fulfills various interfaces in light-client, etc...
func (bz HexBytes) Bytes() []byte {
return bz
}

func (bz HexBytes) String() string {
return strings.ToUpper(hex.EncodeToString(bz))
}

// Format writes either address of 0th element in a slice in base 16 notation,
// with leading 0x (%p), or casts HexBytes to bytes and writes as hexadecimal
// string to s.
func (bz HexBytes) Format(s fmt.State, verb rune) {
switch verb {
case 'p':
s.Write([]byte(fmt.Sprintf("%p", bz)))
default:
s.Write([]byte(fmt.Sprintf("%X", []byte(bz))))
}
}

// ConvertCometBFTToHexBytes converts a cometbft bytes.HexBytes to relayer HexBytes.
func ConvertCometBFTToHexBytes(cbft bytes.HexBytes) HexBytes {
return HexBytes(cbft)
}
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
Loading
Loading