Skip to content

Commit

Permalink
fix: Switch cosmos latest block endpoint (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidNix authored May 15, 2023
1 parent d2f1f1e commit 2250c46
Show file tree
Hide file tree
Showing 6 changed files with 1,129 additions and 1,125 deletions.
5 changes: 2 additions & 3 deletions cosmos/rest_latest_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"
)

// Block represents a block on a Cosmos blockchain.
type Block struct {
BlockID struct {
Hash string `json:"hash"`
Expand Down Expand Up @@ -56,7 +55,7 @@ type Block struct {
} `json:"parts"`
} `json:"block_id"`
Signatures []struct {
BlockIDFlag int `json:"block_id_flag"`
BlockIDFlag string `json:"block_id_flag"`
ValidatorAddress string `json:"validator_address"`
Timestamp time.Time `json:"timestamp"`
Signature string `json:"signature"`
Expand All @@ -68,6 +67,6 @@ type Block struct {
// LatestBlock queries the latest block from the Cosmos REST API given the baseURL.
func (c RestClient) LatestBlock(ctx context.Context) (Block, error) {
var latestBlock Block
err := c.get(ctx, "/blocks/latest", &latestBlock)
err := c.get(ctx, "/cosmos/base/tendermint/v1beta1/blocks/latest", &latestBlock)
return latestBlock, err
}
12 changes: 7 additions & 5 deletions cosmos/rest_latest_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/stretchr/testify/require"
)

//go:embed testdata/block.json
var blockFixture []byte
//go:embed testdata/latest_block.json
var latestBlockFixture []byte

type mockHTTPClient struct {
GetFn func(ctx context.Context, path string) (*http.Response, error)
Expand All @@ -35,18 +35,20 @@ func TestClient_LatestBlock(t *testing.T) {
var httpClient mockHTTPClient
httpClient.GetFn = func(ctx context.Context, path string) (*http.Response, error) {
require.NotNil(t, ctx)
require.Equal(t, "/blocks/latest", path)
require.Equal(t, "/cosmos/base/tendermint/v1beta1/blocks/latest", path)

return &http.Response{
StatusCode: 200,
Body: io.NopCloser(bytes.NewReader(blockFixture)),
Body: io.NopCloser(bytes.NewReader(latestBlockFixture)),
}, nil
}
client := NewRestClient(httpClient)
got, err := client.LatestBlock(ctx)

require.NoError(t, err)
require.Equal(t, "15226219", got.Block.Header.Height)
require.Equal(t, "15312655", got.Block.Header.Height)
require.Equal(t, "15312654", got.Block.LastCommit.Height)
require.Equal(t, "cosmoshub-4", got.Block.Header.ChainID)
})

t.Run("error", func(t *testing.T) {
Expand Down
1,109 changes: 0 additions & 1,109 deletions cosmos/testdata/block.json

This file was deleted.

1,110 changes: 1,110 additions & 0 deletions cosmos/testdata/latest_block.json

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions cosmos/validator_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cosmos
import (
"bytes"
"context"
"encoding/hex"
"encoding/base64"
"errors"
"fmt"
"strconv"
Expand Down Expand Up @@ -86,19 +86,21 @@ func (task ValidatorTask) processSignedBlocks(ctx context.Context) error {
if err != nil {
return err
}
height, err := strconv.ParseFloat(block.Block.LastCommit.Height, 64)
if err != nil {
return fmt.Errorf("parse block last commit height: %w", err)
}

for _, sig := range block.Block.LastCommit.Signatures {
sigHex, err := hex.DecodeString(sig.ValidatorAddress)
sigHex, err := base64.StdEncoding.DecodeString(sig.ValidatorAddress)
if err != nil {
return err
}
if bytes.Equal(sigHex, valHex) {
task.metrics.SetValSignedBlock(task.chainID, task.consaddress, height)
task.metrics.IncValSignedBlocks(task.chainID, task.consaddress)

height, err := strconv.ParseFloat(block.Block.LastCommit.Height, 64)
if err != nil {
return fmt.Errorf("parse block last commit height: %w", err)
}
task.metrics.SetValSignedBlock(task.chainID, task.consaddress, height)

break
}
}
Expand Down
2 changes: 1 addition & 1 deletion cosmos/validator_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestValidatorTask_Run(t *testing.T) {
require.Zero(t, metrics.GotSignedBlock)

var block Block
require.NoError(t, json.Unmarshal(blockFixture, &block))
require.NoError(t, json.Unmarshal(latestBlockFixture, &block))
block.Block.LastCommit.Height = "9001"
client.StubBlock = block

Expand Down

0 comments on commit 2250c46

Please sign in to comment.