Skip to content

Commit

Permalink
go/consensus/api: Remove stuttering when creating clients
Browse files Browse the repository at this point in the history
  • Loading branch information
peternose committed Feb 22, 2025
1 parent 24cb5e2 commit e22cea4
Show file tree
Hide file tree
Showing 42 changed files with 368 additions and 330 deletions.
37 changes: 20 additions & 17 deletions go/beacon/api/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,47 +295,55 @@ func RegisterService(server *grpc.Server, service Backend) {
server.RegisterService(&serviceDesc, service)
}

type beaconClient struct {
// Client is a gRPC beacon client.
type Client struct {
conn *grpc.ClientConn
}

func (c *beaconClient) GetBaseEpoch(ctx context.Context) (EpochTime, error) {
// NewClient creates a new gRPC beacon client.
func NewClient(c *grpc.ClientConn) *Client {
return &Client{
conn: c,
}
}

func (c *Client) GetBaseEpoch(ctx context.Context) (EpochTime, error) {

Check warning on line 310 in go/beacon/api/grpc.go

View check run for this annotation

Codecov / codecov/patch

go/beacon/api/grpc.go#L310

Added line #L310 was not covered by tests
var rsp EpochTime
if err := c.conn.Invoke(ctx, methodGetBaseEpoch.FullName(), nil, &rsp); err != nil {
return 0, err
}
return rsp, nil
}

func (c *beaconClient) GetEpoch(ctx context.Context, height int64) (EpochTime, error) {
func (c *Client) GetEpoch(ctx context.Context, height int64) (EpochTime, error) {
var rsp EpochTime
if err := c.conn.Invoke(ctx, methodGetEpoch.FullName(), height, &rsp); err != nil {
return 0, err
}
return rsp, nil
}

func (c *beaconClient) GetFutureEpoch(ctx context.Context, height int64) (*EpochTimeState, error) {
func (c *Client) GetFutureEpoch(ctx context.Context, height int64) (*EpochTimeState, error) {

Check warning on line 326 in go/beacon/api/grpc.go

View check run for this annotation

Codecov / codecov/patch

go/beacon/api/grpc.go#L326

Added line #L326 was not covered by tests
var rsp EpochTimeState
if err := c.conn.Invoke(ctx, methodGetFutureEpoch.FullName(), height, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}

func (c *beaconClient) GetEpochBlock(ctx context.Context, epoch EpochTime) (int64, error) {
func (c *Client) GetEpochBlock(ctx context.Context, epoch EpochTime) (int64, error) {

Check warning on line 334 in go/beacon/api/grpc.go

View check run for this annotation

Codecov / codecov/patch

go/beacon/api/grpc.go#L334

Added line #L334 was not covered by tests
var rsp int64
if err := c.conn.Invoke(ctx, methodGetEpochBlock.FullName(), epoch, &rsp); err != nil {
return 0, err
}
return rsp, nil
}

func (c *beaconClient) WaitEpoch(ctx context.Context, epoch EpochTime) error {
func (c *Client) WaitEpoch(ctx context.Context, epoch EpochTime) error {
return c.conn.Invoke(ctx, methodWaitEpoch.FullName(), epoch, nil)
}

func (c *beaconClient) WatchEpochs(ctx context.Context) (<-chan EpochTime, pubsub.ClosableSubscription, error) {
func (c *Client) WatchEpochs(ctx context.Context) (<-chan EpochTime, pubsub.ClosableSubscription, error) {
ctx, sub := pubsub.NewContextSubscription(ctx)

stream, err := c.conn.NewStream(ctx, &serviceDesc.Streams[0], methodWatchEpochs.FullName())
Expand Down Expand Up @@ -370,40 +378,35 @@ func (c *beaconClient) WatchEpochs(ctx context.Context) (<-chan EpochTime, pubsu
return ch, sub, nil
}

func (c *beaconClient) WatchLatestEpoch(context.Context) (<-chan EpochTime, pubsub.ClosableSubscription, error) {
func (c *Client) WatchLatestEpoch(context.Context) (<-chan EpochTime, pubsub.ClosableSubscription, error) {

Check warning on line 381 in go/beacon/api/grpc.go

View check run for this annotation

Codecov / codecov/patch

go/beacon/api/grpc.go#L381

Added line #L381 was not covered by tests
// The only thing that uses this is the registration worker, and it
// is not over gRPC.
return nil, nil, fmt.Errorf("beacon: gRPC method not implemented")
}

func (c *beaconClient) GetBeacon(ctx context.Context, height int64) ([]byte, error) {
func (c *Client) GetBeacon(ctx context.Context, height int64) ([]byte, error) {

Check warning on line 387 in go/beacon/api/grpc.go

View check run for this annotation

Codecov / codecov/patch

go/beacon/api/grpc.go#L387

Added line #L387 was not covered by tests
var rsp []byte
if err := c.conn.Invoke(ctx, methodGetBeacon.FullName(), height, &rsp); err != nil {
return nil, err
}
return rsp, nil
}

func (c *beaconClient) StateToGenesis(ctx context.Context, height int64) (*Genesis, error) {
func (c *Client) StateToGenesis(ctx context.Context, height int64) (*Genesis, error) {

Check warning on line 395 in go/beacon/api/grpc.go

View check run for this annotation

Codecov / codecov/patch

go/beacon/api/grpc.go#L395

Added line #L395 was not covered by tests
var rsp Genesis
if err := c.conn.Invoke(ctx, methodStateToGenesis.FullName(), height, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}

func (c *beaconClient) ConsensusParameters(ctx context.Context, height int64) (*ConsensusParameters, error) {
func (c *Client) ConsensusParameters(ctx context.Context, height int64) (*ConsensusParameters, error) {
var rsp ConsensusParameters
if err := c.conn.Invoke(ctx, methodConsensusParameters.FullName(), height, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}

func (c *beaconClient) Cleanup() {
}

// NewBeaconClient creates a new gRPC scheduler client service.
func NewBeaconClient(c *grpc.ClientConn) Backend {
return &beaconClient{c}
func (c *Client) Cleanup() {

Check warning on line 411 in go/beacon/api/grpc.go

View check run for this annotation

Codecov / codecov/patch

go/beacon/api/grpc.go#L411

Added line #L411 was not covered by tests
}
93 changes: 47 additions & 46 deletions go/consensus/api/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,99 +692,107 @@ func RegisterService(server *grpc.Server, service ClientBackend) {
server.RegisterService(&serviceDesc, service)
}

type consensusClient struct {
// Client is a gRPC consensus client.
type Client struct {
conn *grpc.ClientConn
}

func (c *consensusClient) SubmitTx(ctx context.Context, tx *transaction.SignedTransaction) error {
// NewClient creates a new gRPC consensus client.
func NewClient(c *grpc.ClientConn) *Client {
return &Client{
conn: c,
}
}

func (c *Client) SubmitTx(ctx context.Context, tx *transaction.SignedTransaction) error {
return c.conn.Invoke(ctx, methodSubmitTx.FullName(), tx, nil)
}

func (c *consensusClient) SubmitTxNoWait(ctx context.Context, tx *transaction.SignedTransaction) error {
func (c *Client) SubmitTxNoWait(ctx context.Context, tx *transaction.SignedTransaction) error {
return c.conn.Invoke(ctx, methodSubmitTxNoWait.FullName(), tx, nil)
}

func (c *consensusClient) SubmitTxWithProof(ctx context.Context, tx *transaction.SignedTransaction) (*transaction.Proof, error) {
func (c *Client) SubmitTxWithProof(ctx context.Context, tx *transaction.SignedTransaction) (*transaction.Proof, error) {

Check warning on line 715 in go/consensus/api/grpc.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/api/grpc.go#L715

Added line #L715 was not covered by tests
var proof transaction.Proof
if err := c.conn.Invoke(ctx, methodSubmitTxWithProof.FullName(), tx, &proof); err != nil {
return nil, err
}
return &proof, nil
}

func (c *consensusClient) StateToGenesis(ctx context.Context, height int64) (*genesis.Document, error) {
func (c *Client) StateToGenesis(ctx context.Context, height int64) (*genesis.Document, error) {
var rsp genesis.Document
if err := c.conn.Invoke(ctx, methodStateToGenesis.FullName(), height, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}

func (c *consensusClient) EstimateGas(ctx context.Context, req *EstimateGasRequest) (transaction.Gas, error) {
func (c *Client) EstimateGas(ctx context.Context, req *EstimateGasRequest) (transaction.Gas, error) {
var gas transaction.Gas
if err := c.conn.Invoke(ctx, methodEstimateGas.FullName(), req, &gas); err != nil {
return transaction.Gas(0), err
}
return gas, nil
}

func (c *consensusClient) MinGasPrice(ctx context.Context) (*quantity.Quantity, error) {
func (c *Client) MinGasPrice(ctx context.Context) (*quantity.Quantity, error) {

Check warning on line 739 in go/consensus/api/grpc.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/api/grpc.go#L739

Added line #L739 was not covered by tests
var rsp quantity.Quantity
if err := c.conn.Invoke(ctx, methodMinGasPrice.FullName(), nil, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}

func (c *consensusClient) GetSignerNonce(ctx context.Context, req *GetSignerNonceRequest) (uint64, error) {
func (c *Client) GetSignerNonce(ctx context.Context, req *GetSignerNonceRequest) (uint64, error) {
var nonce uint64
if err := c.conn.Invoke(ctx, methodGetSignerNonce.FullName(), req, &nonce); err != nil {
return nonce, err
}
return nonce, nil
}

func (c *consensusClient) GetBlock(ctx context.Context, height int64) (*Block, error) {
func (c *Client) GetBlock(ctx context.Context, height int64) (*Block, error) {
var rsp Block
if err := c.conn.Invoke(ctx, methodGetBlock.FullName(), height, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}

func (c *consensusClient) GetLightBlock(ctx context.Context, height int64) (*LightBlock, error) {
func (c *Client) GetLightBlock(ctx context.Context, height int64) (*LightBlock, error) {
var rsp LightBlock
if err := c.conn.Invoke(ctx, methodGetLightBlock.FullName(), height, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}

func (c *consensusClient) GetTransactions(ctx context.Context, height int64) ([][]byte, error) {
func (c *Client) GetTransactions(ctx context.Context, height int64) ([][]byte, error) {
var rsp [][]byte
if err := c.conn.Invoke(ctx, methodGetTransactions.FullName(), height, &rsp); err != nil {
return nil, err
}
return rsp, nil
}

func (c *consensusClient) GetTransactionsWithResults(ctx context.Context, height int64) (*TransactionsWithResults, error) {
func (c *Client) GetTransactionsWithResults(ctx context.Context, height int64) (*TransactionsWithResults, error) {
var rsp TransactionsWithResults
if err := c.conn.Invoke(ctx, methodGetTransactionsWithResults.FullName(), height, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}

func (c *consensusClient) GetTransactionsWithProofs(ctx context.Context, height int64) (*TransactionsWithProofs, error) {
func (c *Client) GetTransactionsWithProofs(ctx context.Context, height int64) (*TransactionsWithProofs, error) {
var rsp TransactionsWithProofs
if err := c.conn.Invoke(ctx, methodGetTransactionsWithProofs.FullName(), height, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}

func (c *consensusClient) GetUnconfirmedTransactions(ctx context.Context) ([][]byte, error) {
func (c *Client) GetUnconfirmedTransactions(ctx context.Context) ([][]byte, error) {
var rsp [][]byte
if err := c.conn.Invoke(ctx, methodGetUnconfirmedTransactions.FullName(), nil, &rsp); err != nil {
return nil, err
Expand All @@ -793,7 +801,7 @@ func (c *consensusClient) GetUnconfirmedTransactions(ctx context.Context) ([][]b
}

type stateReadSync struct {
c *consensusClient
c *Client
}

// Implements syncer.ReadSyncer.
Expand Down Expand Up @@ -823,55 +831,55 @@ func (rs *stateReadSync) SyncIterate(ctx context.Context, request *syncer.Iterat
return &rsp, nil
}

func (c *consensusClient) State() syncer.ReadSyncer {
func (c *Client) State() syncer.ReadSyncer {
return &stateReadSync{c}
}

func (c *consensusClient) GetGenesisDocument(ctx context.Context) (*genesis.Document, error) {
func (c *Client) GetGenesisDocument(ctx context.Context) (*genesis.Document, error) {
var rsp genesis.Document
if err := c.conn.Invoke(ctx, methodGetGenesisDocument.FullName(), nil, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}

func (c *consensusClient) GetChainContext(ctx context.Context) (string, error) {
func (c *Client) GetChainContext(ctx context.Context) (string, error) {
var rsp string
if err := c.conn.Invoke(ctx, methodGetChainContext.FullName(), nil, &rsp); err != nil {
return "", err
}
return rsp, nil
}

func (c *consensusClient) GetStatus(ctx context.Context) (*Status, error) {
func (c *Client) GetStatus(ctx context.Context) (*Status, error) {
var rsp Status
if err := c.conn.Invoke(ctx, methodGetStatus.FullName(), nil, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}

func (c *consensusClient) GetNextBlockState(ctx context.Context) (*NextBlockState, error) {
func (c *Client) GetNextBlockState(ctx context.Context) (*NextBlockState, error) {

Check warning on line 862 in go/consensus/api/grpc.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/api/grpc.go#L862

Added line #L862 was not covered by tests
var rsp NextBlockState
if err := c.conn.Invoke(ctx, methodGetNextBlockState.FullName(), nil, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}

func (c *consensusClient) GetParameters(ctx context.Context, height int64) (*Parameters, error) {
func (c *Client) GetParameters(ctx context.Context, height int64) (*Parameters, error) {
var rsp Parameters
if err := c.conn.Invoke(ctx, methodGetParameters.FullName(), height, &rsp); err != nil {
return nil, err
}
return &rsp, nil
}

func (c *consensusClient) SubmitEvidence(ctx context.Context, evidence *Evidence) error {
func (c *Client) SubmitEvidence(ctx context.Context, evidence *Evidence) error {
return c.conn.Invoke(ctx, methodSubmitEvidence.FullName(), evidence, nil)
}

func (c *consensusClient) WatchBlocks(ctx context.Context) (<-chan *Block, pubsub.ClosableSubscription, error) {
func (c *Client) WatchBlocks(ctx context.Context) (<-chan *Block, pubsub.ClosableSubscription, error) {
ctx, sub := pubsub.NewContextSubscription(ctx)

stream, err := c.conn.NewStream(ctx, &serviceDesc.Streams[0], methodWatchBlocks.FullName())
Expand Down Expand Up @@ -906,41 +914,34 @@ func (c *consensusClient) WatchBlocks(ctx context.Context) (<-chan *Block, pubsu
return ch, sub, nil
}

func (c *consensusClient) Beacon() beacon.Backend {
return beacon.NewBeaconClient(c.conn)
func (c *Client) Beacon() beacon.Backend {
return beacon.NewClient(c.conn)
}

func (c *consensusClient) Registry() registry.Backend {
return registry.NewRegistryClient(c.conn)
func (c *Client) Registry() registry.Backend {
return registry.NewClient(c.conn)
}

func (c *consensusClient) Staking() staking.Backend {
return staking.NewStakingClient(c.conn)
func (c *Client) Staking() staking.Backend {
return staking.NewClient(c.conn)
}

func (c *consensusClient) Scheduler() scheduler.Backend {
return scheduler.NewSchedulerClient(c.conn)
func (c *Client) Scheduler() scheduler.Backend {
return scheduler.NewClient(c.conn)
}

func (c *consensusClient) Governance() governance.Backend {
return governance.NewGovernanceClient(c.conn)
func (c *Client) Governance() governance.Backend {
return governance.NewClient(c.conn)
}

func (c *consensusClient) RootHash() roothash.Backend {
return roothash.NewRootHashClient(c.conn)
func (c *Client) RootHash() roothash.Backend {
return roothash.NewClient(c.conn)
}

func (c *consensusClient) Vault() vault.Backend {
return vault.NewVaultClient(c.conn)
func (c *Client) Vault() vault.Backend {
return vault.NewClient(c.conn)

Check warning on line 942 in go/consensus/api/grpc.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/api/grpc.go#L941-L942

Added lines #L941 - L942 were not covered by tests
}

func (c *consensusClient) KeyManager() keymanager.Backend {
return keymanager.NewKeymanagerClient(c.conn)
}

// NewConsensusClient creates a new gRPC consensus client service.
func NewConsensusClient(c *grpc.ClientConn) ClientBackend {
return &consensusClient{
conn: c,
}
func (c *Client) KeyManager() keymanager.Backend {
return keymanager.NewClient(c.conn)

Check warning on line 946 in go/consensus/api/grpc.go

View check run for this annotation

Codecov / codecov/patch

go/consensus/api/grpc.go#L945-L946

Added lines #L945 - L946 were not covered by tests
}
Loading

0 comments on commit e22cea4

Please sign in to comment.