-
Notifications
You must be signed in to change notification settings - Fork 1
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
bugfixes: small tidy ups and bug fixes #13
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"log/slog" | ||
"net/http" | ||
"sync" | ||
"time" | ||
|
||
"github.com/duneanalytics/blockchain-ingester/lib/hexutils" | ||
"github.com/duneanalytics/blockchain-ingester/models" | ||
|
@@ -21,7 +22,8 @@ type BlockchainClient interface { | |
} | ||
|
||
const ( | ||
MaxRetries = 10 | ||
MaxRetries = 10 | ||
DefaultRequestTimeout = 30 * time.Second | ||
) | ||
|
||
type rpcClient struct { | ||
|
@@ -37,6 +39,7 @@ func NewClient(log *slog.Logger, cfg Config) (*rpcClient, error) { // revive:dis | |
client.Logger = log | ||
client.CheckRetry = retryablehttp.DefaultRetryPolicy | ||
client.Backoff = retryablehttp.LinearJitterBackoff | ||
client.HTTPClient.Timeout = DefaultRequestTimeout | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice |
||
rpc := &rpcClient{ | ||
client: client, | ||
cfg: cfg, | ||
|
@@ -81,7 +84,7 @@ func (c *rpcClient) LatestBlockNumber() (int64, error) { | |
|
||
// getResponseBody sends a request to the server and returns the response body | ||
func (c *rpcClient) getResponseBody( | ||
ctx context.Context, method string, params interface{}, output *bytes.Buffer, | ||
ctx context.Context, method string, params []interface{}, output *bytes.Buffer, | ||
) error { | ||
reqData := map[string]interface{}{ | ||
"jsonrpc": "2.0", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,29 +56,33 @@ func (i *ingester) ConsumeBlocks( | |
dontStop := endBlockNumber <= startBlockNumber | ||
latestBlockNumber := i.tryUpdateLatestBlockNumber() | ||
|
||
waitForBlock := func(blockNumber, latestBlockNumber int64) int64 { | ||
waitForBlock := func(ctx context.Context, blockNumber, latestBlockNumber int64) int64 { | ||
for blockNumber > latestBlockNumber { | ||
select { | ||
case <-ctx.Done(): | ||
return latestBlockNumber | ||
default: | ||
case <-time.After(i.cfg.PollInterval): | ||
} | ||
i.log.Info(fmt.Sprintf("Waiting %v for block to be available..", i.cfg.PollInterval), | ||
i.log.Debug(fmt.Sprintf("Waiting %v for block to be available..", i.cfg.PollInterval), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -> waited? 😄 |
||
"blockNumber", blockNumber, | ||
"latestBlockNumber", latestBlockNumber, | ||
) | ||
time.Sleep(i.cfg.PollInterval) | ||
latestBlockNumber = i.tryUpdateLatestBlockNumber() | ||
} | ||
return latestBlockNumber | ||
} | ||
|
||
for blockNumber := startBlockNumber; dontStop || blockNumber <= endBlockNumber; blockNumber++ { | ||
latestBlockNumber = waitForBlock(blockNumber, latestBlockNumber) | ||
latestBlockNumber = waitForBlock(ctx, blockNumber, latestBlockNumber) | ||
startTime := time.Now() | ||
|
||
block, err := i.node.BlockByNumber(ctx, blockNumber) | ||
if err != nil { | ||
if errors.Is(err, context.Canceled) { | ||
i.log.Info("Context canceled, stopping..") | ||
return err | ||
} | ||
Comment on lines
+81
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah nice, this fixed the racy test perhaps? |
||
|
||
i.log.Error("Failed to get block by number, continuing..", | ||
"blockNumber", blockNumber, | ||
"latestBlockNumber", latestBlockNumber, | ||
|
@@ -90,10 +94,6 @@ func (i *ingester) ConsumeBlocks( | |
Error: err, | ||
}) | ||
|
||
if errors.Is(err, context.Canceled) { | ||
return err | ||
} | ||
|
||
// TODO: should I sleep (backoff) here? | ||
continue | ||
} | ||
|
@@ -159,7 +159,7 @@ func (i *ingester) tryUpdateLatestBlockNumber() int64 { | |
} | ||
|
||
func (i *ingester) ReportProgress(ctx context.Context) error { | ||
timer := time.NewTicker(20 * time.Second) | ||
timer := time.NewTicker(i.cfg.ReportProgressInterval) | ||
defer timer.Stop() | ||
|
||
previousTime := time.Now() | ||
|
@@ -173,20 +173,32 @@ func (i *ingester) ReportProgress(ctx context.Context) error { | |
case tNow := <-timer.C: | ||
latest := atomic.LoadInt64(&i.info.LatestBlockNumber) | ||
lastIngested := atomic.LoadInt64(&i.info.IngestedBlockNumber) | ||
lastConsumed := atomic.LoadInt64(&i.info.ConsumedBlockNumber) | ||
|
||
blocksPerSec := float64(lastIngested-previousIngested) / tNow.Sub(previousTime).Seconds() | ||
newDistance := latest - lastIngested | ||
fallingBehind := newDistance > (previousDistance + 1) // TODO: make is more stable | ||
|
||
i.log.Info("Info", | ||
rpcErrors := len(i.info.RPCErrors) | ||
duneErrors := len(i.info.DuneErrors) | ||
fields := []interface{}{ | ||
"blocksPerSec", fmt.Sprintf("%.2f", blocksPerSec), | ||
"latestBlockNumber", latest, | ||
"ingestedBlockNumber", lastIngested, | ||
"consumedBlockNumber", lastConsumed, | ||
"distanceFromLatest", latest-lastIngested, | ||
"FallingBehind", fallingBehind, | ||
"blocksPerSec", fmt.Sprintf("%.2f", blocksPerSec), | ||
) | ||
} | ||
if fallingBehind { | ||
fields = append(fields, "fallingBehind", fallingBehind) | ||
} | ||
if newDistance > 1 { | ||
fields = append(fields, "distanceFromLatest", newDistance) | ||
} | ||
if rpcErrors > 0 { | ||
fields = append(fields, "rpcErrors", rpcErrors) | ||
} | ||
if duneErrors > 0 { | ||
fields = append(fields, "duneErrors", duneErrors) | ||
} | ||
|
||
i.log.Info("ProgressReport", fields...) | ||
previousIngested = lastIngested | ||
previousDistance = newDistance | ||
previousTime = tNow | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it going to be always JSON ? Or JSON just for now ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for now, but great question