Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gnoclient): support fetching blocks, block results, latest block number #1910

Merged
merged 15 commits into from
Apr 29, 2024
4 changes: 2 additions & 2 deletions gno.land/pkg/gnoclient/client_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@

block, err := c.RPCClient.Block(&height)
if err != nil {
return nil, fmt.Errorf("block query failed: %w", err)

Check warning on line 142 in gno.land/pkg/gnoclient/client_queries.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_queries.go#L142

Added line #L142 was not covered by tests
}

return block, nil
Expand All @@ -158,21 +158,21 @@

blockResults, err := c.RPCClient.BlockResults(&height)
if err != nil {
return nil, fmt.Errorf("block query failed: %w", err)

Check warning on line 161 in gno.land/pkg/gnoclient/client_queries.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_queries.go#L161

Added line #L161 was not covered by tests
}

return blockResults, nil
}

// BlockNumber gets the latest block height on the chain
func (c *Client) BlockNumber() (int64, error) {
// Head gets the head of the chain (latest block height)
func (c *Client) Head() (int64, error) {
leohhhn marked this conversation as resolved.
Show resolved Hide resolved
if err := c.validateRPCClient(); err != nil {
return 0, ErrMissingRPCClient
}

status, err := c.RPCClient.Status()
if err != nil {
return 0, fmt.Errorf("block number query failed: %w", err)

Check warning on line 175 in gno.land/pkg/gnoclient/client_queries.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/gnoclient/client_queries.go#L175

Added line #L175 was not covered by tests
}

return status.SyncInfo.LatestBlockHeight, nil
Expand Down
27 changes: 25 additions & 2 deletions gno.land/pkg/gnoclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,29 @@ func TestBlockResults(t *testing.T) {
assert.Equal(t, height, blockResult.Height)
}

func TestHead(t *testing.T) {
t.Parallel()

latestHeight := int64(5)

client := &Client{
Signer: &mockSigner{},
RPCClient: &mockRPCClient{
status: func() (*ctypes.ResultStatus, error) {
return &ctypes.ResultStatus{
SyncInfo: ctypes.SyncInfo{
LatestBlockHeight: latestHeight,
},
}, nil
},
},
}

head, err := client.Head()
require.NoError(t, err)
assert.Equal(t, latestHeight, head)
}

func TestBlockErrors(t *testing.T) {
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()

Expand Down Expand Up @@ -1214,7 +1237,7 @@ func TestBlockResultErrors(t *testing.T) {
}
}

func TestBlockNumberErrors(t *testing.T) {
func TestHeadErrors(t *testing.T) {
t.Parallel()

testCases := []struct {
Expand All @@ -1237,7 +1260,7 @@ func TestBlockNumberErrors(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

res, err := tc.client.BlockNumber()
res, err := tc.client.Head()
assert.Equal(t, int64(0), res)
assert.ErrorIs(t, err, tc.expectedError)
})
Expand Down
Loading