-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: alternate consensus engines (#1490)
* 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
1 parent
d740575
commit 4e4e953
Showing
14 changed files
with
359 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.