Skip to content

Commit

Permalink
add SubscribeNewBlocks test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera committed Dec 5, 2024
1 parent b19de8b commit 9145d3a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
28 changes: 24 additions & 4 deletions zetaclient/testutils/mocks/cometbft_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mocks
import (
"context"
"encoding/hex"
"sync"
"testing"

abci "github.com/cometbft/cometbft/abci/types"
Expand All @@ -20,6 +21,9 @@ type CometBFTClient struct {
err error
code uint32
txHash bytes.HexBytes

subscribers map[string]chan<- coretypes.ResultEvent
subscribersLock sync.Mutex
}

func (c *CometBFTClient) BroadcastTxCommit(
Expand Down Expand Up @@ -82,11 +86,27 @@ func (c *CometBFTClient) SetError(err error) *CometBFTClient {
return c
}

// PublishToSubscribers will publish an event to all subscribers (mock only)
func (c *CometBFTClient) PublishToSubscribers(event coretypes.ResultEvent) {
c.subscribersLock.Lock()
defer c.subscribersLock.Unlock()
for _, ch := range c.subscribers {
ch <- event
}
}

func (c *CometBFTClient) Subscribe(ctx context.Context, subscriber, _ string, _ ...int) (out <-chan coretypes.ResultEvent, err error) {

Check failure on line 98 in zetaclient/testutils/mocks/cometbft_client.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
outChan := make(chan coretypes.ResultEvent)
c.subscribers[subscriber] = outChan
return outChan, nil
}

func NewSDKClientWithErr(t *testing.T, err error, code uint32) *CometBFTClient {
return &CometBFTClient{
t: t,
Client: mock.Client{},
err: err,
code: code,
t: t,
Client: mock.Client{},
err: err,
code: code,
subscribers: make(map[string]chan<- coretypes.ResultEvent),
}
}
6 changes: 1 addition & 5 deletions zetaclient/zetacore/client_subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ import (
cometbft_types "github.com/cometbft/cometbft/types"
)

const (
newBlockSubscriptionFilter = "tm.event='NewBlock'"
)

// NewBlockSubscriber subscribes to cometbft new block events
func (c *Client) NewBlockSubscriber(ctx context.Context) (chan cometbft_types.EventDataNewBlock, error) {
rawBlockEventChan, err := c.cometBFTClient.Subscribe(ctx, "", newBlockSubscriptionFilter)
rawBlockEventChan, err := c.cometBFTClient.Subscribe(ctx, "", cometbft_types.EventQueryNewBlock.String())
if err != nil {
return nil, err
}
Expand Down
30 changes: 30 additions & 0 deletions zetaclient/zetacore/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"go.nhat.io/grpcmock/planner"

cometbft_rpc_client "github.com/cometbft/cometbft/rpc/client"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
cometbft_types "github.com/cometbft/cometbft/types"
"github.com/zeta-chain/node/cmd/zetacored/config"
crosschaintypes "github.com/zeta-chain/node/x/crosschain/types"
"github.com/zeta-chain/node/zetaclient/keys"
Expand Down Expand Up @@ -237,3 +239,31 @@ func TestZetacore_GetAllOutboundTrackerByChain(t *testing.T) {
require.NoError(t, err)
require.Equal(t, expectedOutput.OutboundTracker, resp)
}

func TestZetacore_SubscribeNewBlocks(t *testing.T) {
ctx := context.Background()
cometBFTClient := mocks.NewSDKClientWithErr(t, nil, 0)
client := setupZetacoreClient(
t,
withDefaultObserverKeys(),
withTendermint(cometBFTClient),
)

newBlockChan, err := client.NewBlockSubscriber(ctx)
require.NoError(t, err)

height := int64(10)

cometBFTClient.PublishToSubscribers(coretypes.ResultEvent{
Data: cometbft_types.EventDataNewBlock{
Block: &cometbft_types.Block{
Header: cometbft_types.Header{
Height: height,
},
},
},
})

newBlockEvent := <-newBlockChan
require.Equal(t, height, newBlockEvent.Block.Header.Height)
}

0 comments on commit 9145d3a

Please sign in to comment.