Skip to content

Commit

Permalink
feat(grpc): move all messages in the same file
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Nov 23, 2022
1 parent 0e64f47 commit 79c438b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
10 changes: 5 additions & 5 deletions app/actor/cosmos/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cosmos
import (
"context"

"okp4/nemeton-leaderboard/app/messages"
"okp4/nemeton-leaderboard/app/message"

"github.com/asynkron/protoactor-go/actor"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
Expand Down Expand Up @@ -34,27 +34,27 @@ func (client *GrpcClient) Receive(ctx actor.Context) {
if err := client.grpcConn.Close(); err != nil {
log.Warn().Err(err).Msg("😥 Could not close grpc connection.")
}
case *messages.GetBlock:
case *message.GetBlock:
goCTX, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()

block, err := client.GetBlock(goCTX, msg.Height)
if err != nil {
panic(err)
}
ctx.Respond(&messages.GetBlockResponse{
ctx.Respond(&message.GetBlockResponse{
Block: block,
})

case *messages.GetLatestBlock:
case *message.GetLatestBlock:
goCTX, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()

block, err := client.GetLatestBlock(goCTX)
if err != nil {
panic(err)
}
ctx.Respond(&messages.GetBlockResponse{
ctx.Respond(&message.GetBlockResponse{
Block: block,
})
}
Expand Down
10 changes: 5 additions & 5 deletions app/actor/synchronization/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"time"

"okp4/nemeton-leaderboard/app/messages"
"okp4/nemeton-leaderboard/app/message"

"github.com/asynkron/protoactor-go/actor"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
Expand Down Expand Up @@ -60,14 +60,14 @@ func (a *Actor) startSynchronization(ctx actor.Context) {

func (a *Actor) catchUpSyncBlocks(ctx actor.Context) error {
// Check first the latest latestBlock height before sync
result, err := ctx.RequestFuture(a.grpcClient, &messages.GetLatestBlock{}, 5*time.Second).Result()
result, err := ctx.RequestFuture(a.grpcClient, &message.GetLatestBlock{}, 5*time.Second).Result()
if err != nil {
return err
}

var latestBlock *tmservice.Block
switch resp := result.(type) {
case *messages.GetBlockResponse:
case *message.GetBlockResponse:
latestBlock = resp.Block
default:
return fmt.Errorf("wrong response message")
Expand Down Expand Up @@ -97,15 +97,15 @@ func (a *Actor) catchUpSyncBlocks(ctx actor.Context) error {
}

func (a *Actor) getBlock(ctx actor.Context, height int64) (*tmservice.Block, error) {
result, err := ctx.RequestFuture(a.grpcClient, &messages.GetBlock{Height: height}, 5*time.Second).Result()
result, err := ctx.RequestFuture(a.grpcClient, &message.GetBlock{Height: height}, 5*time.Second).Result()
if err != nil {
log.Err(err).Msg("⚠️ Failed request current block.")
return nil, err
}

var block *tmservice.Block
switch resp := result.(type) {
case *messages.GetBlockResponse:
case *message.GetBlockResponse:
block = resp.Block
default:
return nil, fmt.Errorf("wrong response message")
Expand Down
14 changes: 14 additions & 0 deletions app/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"okp4/nemeton-leaderboard/app/event"

"github.com/asynkron/protoactor-go/actor"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
"go.mongodb.org/mongo-driver/bson/primitive"
)

Expand All @@ -21,3 +22,16 @@ type NewEventMessage struct {
}

type BrokenStreamMessage struct{}

// GetBlock Ask to requets a block at a given height
type GetBlock struct {
// Height of the block to get
Height int64
}

// GetLatestBlock Request the latest block of the chain.
type GetLatestBlock struct{}

type GetBlockResponse struct {
Block *tmservice.Block
}
15 changes: 0 additions & 15 deletions app/messages/msg.go

This file was deleted.

0 comments on commit 79c438b

Please sign in to comment.