Skip to content

Commit

Permalink
Add http block headers: age and x-tray to logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadeuchi committed Jun 6, 2023
1 parent 0043636 commit 442e0f6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/load/block_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (self *BlockDownloader) run() (err error) {

retry:

block, err := self.client.GetBlockByHeight(self.Ctx, networkInfo.Height)
block, _, err := self.client.GetBlockByHeight(self.Ctx, networkInfo.Height)
if err != nil {
self.Log.WithError(err).Error("Failed to get block")
time.Sleep(10 * time.Second)
Expand Down
5 changes: 3 additions & 2 deletions src/utils/arweave/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/base64"
"errors"
"github.com/go-resty/resty/v2"
"math/big"
"net/http"
"strconv"
Expand Down Expand Up @@ -110,11 +111,11 @@ func (self *Client) CheckPeerConnection(ctx context.Context, peer string) (out *
}

// https://docs.arweave.org/developers/server/http-api#get-block-by-height
func (self *Client) GetBlockByHeight(ctx context.Context, height int64) (out *Block, err error) {
func (self *Client) GetBlockByHeight(ctx context.Context, height int64) (out *Block, resp *resty.Response, err error) {
req, cancel := self.Request(ctx)
defer cancel()

resp, err := req.
resp, err = req.
SetResult(&Block{}).
SetPathParam("height", strconv.FormatInt(height, 10)).
ForceContentType("application/json").
Expand Down
2 changes: 1 addition & 1 deletion src/utils/arweave/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (s *ClientTestSuite) TestCheckPeer() {
}

func (s *ClientTestSuite) TestGetBlockByHeight() {
out, err := s.client.GetBlockByHeight(s.ctx, 1082024)
out, _, err := s.client.GetBlockByHeight(s.ctx, 1082024)
require.Nil(s.T(), err)
require.NotNil(s.T(), out)
require.NotZero(s.T(), out.Height)
Expand Down
17 changes: 13 additions & 4 deletions src/utils/listener/block_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"errors"
"github.com/go-resty/resty/v2"
"math"
"time"

Expand Down Expand Up @@ -77,7 +78,7 @@ func (self *BlockDownloader) WithBackoff(maxElapsedTime, maxInterval time.Durati

func (self *BlockDownloader) WithHeightRange(start, stop uint64) *BlockDownloader {
self.Task = self.Task.WithOnBeforeStart(func() (err error) {
block, err := self.client.GetBlockByHeight(self.Ctx, int64(start-1))
block, _, err := self.client.GetBlockByHeight(self.Ctx, int64(start-1))
if err != nil {
return err
}
Expand Down Expand Up @@ -162,14 +163,18 @@ func (self *BlockDownloader) run() error {
return err
}).
Run(func() (err error) {
block, err = self.client.GetBlockByHeight(self.Ctx, int64(height))
var resp *resty.Response
block, resp, err = self.client.GetBlockByHeight(self.Ctx, int64(height))
if err != nil {
return err
}

if len(lastProcessedBlockHash) > 0 &&
!bytes.Equal(lastProcessedBlockHash, block.PreviousBlock) {
self.Log.WithField("height", height).
self.Log.
WithField("height", height).
WithField("age", resp.Header().Get("Age")).
WithField("x-trace", resp.Header().Get("X-Trace")).
WithField("last_processed_block_hash", lastProcessedBlockHash).
WithField("previous_block", block.PreviousBlock).
Error("Previous block hash isn't valid")
Expand All @@ -183,7 +188,11 @@ func (self *BlockDownloader) run() error {
}

if !block.IsValid() {
self.Log.WithField("height", height).Error("Block hash isn't valid")
self.Log.
WithField("height", height).
WithField("age", resp.Header().Get("Age")).
WithField("x-trace", resp.Header().Get("X-Trace")).
Error("Block hash isn't valid")
self.monitor.GetReport().BlockDownloader.Errors.BlockValidationErrors.Inc()
err = errors.New("block isn't valid")
return
Expand Down

0 comments on commit 442e0f6

Please sign in to comment.