Skip to content

Commit

Permalink
chore: update client log (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
themantre authored Dec 30, 2024
1 parent baa65bf commit be94a18
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 168 deletions.
6 changes: 3 additions & 3 deletions internal/engine/command/voucher/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
"go.uber.org/mock/gomock"
)

func setup(t *testing.T) (*Voucher, repository.MockDatabase, client.MockManager, wallet.MockIWallet) {
func setup(t *testing.T) (*Voucher, repository.MockIDatabase, client.MockIManager, wallet.MockIWallet) {
t.Helper()

ctrl := gomock.NewController(t)

mockDB := repository.NewMockDatabase(ctrl)
mockClient := client.NewMockManager(ctrl)
mockDB := repository.NewMockIDatabase(ctrl)
mockClient := client.NewMockIManager(ctrl)
mockWallet := wallet.NewMockIWallet(ctrl)

mockVoucher := NewVoucher(mockDB, mockWallet, mockClient)
Expand Down
130 changes: 65 additions & 65 deletions internal/repository/mock.go

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,30 @@ type Client struct {
networkClient pactus.NetworkClient
transactionClient pactus.TransactionClient
conn *grpc.ClientConn
target string
}

func NewClient(endpoint string) (*Client, error) {
conn, err := grpc.NewClient(endpoint, grpc.WithTransportCredentials(insecure.NewCredentials()))
func NewClient(target string) (*Client, error) {
conn, err := grpc.NewClient(target, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, err
}

log.Info("establishing new connection", "addr", endpoint)
log.Info("establishing new connection", "addr", target)

return &Client{
blockchainClient: pactus.NewBlockchainClient(conn),
networkClient: pactus.NewNetworkClient(conn),
transactionClient: pactus.NewTransactionClient(conn),
conn: conn,
target: target,
}, nil
}

func (c *Client) Target() string {
return c.target
}

func (c *Client) GetBlockchainInfo(ctx context.Context) (*pactus.GetBlockchainInfoResponse, error) {
blockchainInfo, err := c.blockchainClient.GetBlockchainInfo(ctx, &pactus.GetBlockchainInfoRequest{})
if err != nil {
Expand Down
13 changes: 6 additions & 7 deletions pkg/client/client_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,28 @@ func (cm *Manager) Stop() {
func (cm *Manager) updateValMap() {
freshValMap := make(map[string]*pactus.PeerInfo)

for _, c := range cm.clients {
networkInfo, err := c.GetNetworkInfo(cm.ctx)
for _, client := range cm.clients {
networkInfo, err := client.GetNetworkInfo(cm.ctx)
if err != nil {
logger.Warn("cannot connect to client", "err", err)
logger.Warn("cannot connect to client", "err", err, "target", client.Target())

continue
}

if networkInfo == nil {
logger.Warn("network info is nil")
logger.Warn("network info is nil", "target", client.Target())

continue
}

if len(networkInfo.GetConnectedPeers()) == 0 {
logger.Warn("no connected peers")
logger.Warn("no connected peers", "target", client.Target())

continue
}

logger.Info("connected peers = ", "count", networkInfo.ConnectedPeersCount)
logger.Info("fetching network information", "target", client.Target(), "connected", networkInfo.ConnectedPeersCount)
for _, peer := range networkInfo.ConnectedPeers {
logger.Info("sync peer", "peerId", peer.PeerId)
for _, addr := range peer.ConsensusAddresses {
current := freshValMap[addr]
if current != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/client/client_mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func setup(t *testing.T) (*Manager, *MockIClient) {
func TestFindPublicKey(t *testing.T) {
clientMgr, mockClient := setup(t)

mockClient.EXPECT().Target().AnyTimes().Return("target")
mockClient.EXPECT().GetNetworkInfo(gomock.Any()).Return(
&pactus.GetNetworkInfoResponse{
ConnectedPeers: []*pactus.PeerInfo{
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

type IClient interface {
Target() string
GetBlockchainInfo(context.Context) (*pactus.GetBlockchainInfoResponse, error)
GetBlockchainHeight(context.Context) (uint32, error)
GetLastBlockTime(context.Context) (uint32, uint32)
Expand All @@ -22,7 +23,6 @@ type IClient interface {
type IManager interface {
Start()
Stop()
updateValMap()
AddClient(c IClient)
GetLocalClient() IClient
GetRandomClient() IClient
Expand Down
Loading

0 comments on commit be94a18

Please sign in to comment.