Skip to content

Commit

Permalink
feat: adds verbose logs to tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
ckartik committed Jul 8, 2024
1 parent 0910bc9 commit 8bc062a
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions p2p/pkg/preconfirmation/tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ func (t *Tracker) TrackCommitment(
func (t *Tracker) Metrics() []prometheus.Collector {
return t.metrics.Metrics()
}

func (t *Tracker) handleNewL1Block(
ctx context.Context,
newL1Block *blocktracker.BlocktrackerNewL1Block,
Expand All @@ -269,34 +268,42 @@ func (t *Tracker) handleNewL1Block(
// for bidder to open is only in cases of slashes as he will get refund. Only one
// of bidder or provider should open the commitment as 1 of the txns would
// fail. This delay is to ensure this.
t.logger.Info("Bidder detected, processing block 1 behind the current one")
t.winners[newL1Block.BlockNumber.Int64()] = newL1Block
pastBlock, ok := t.winners[newL1Block.BlockNumber.Int64()-2]
if !ok {
t.logger.Info("Past block not found, returning")
return nil
}
newL1Block = pastBlock
t.logger.Info("Processing past block", "blockNumber", pastBlock.BlockNumber)
for k := range t.winners {
if k < pastBlock.BlockNumber.Int64() {
t.logger.Info("Deleting old winner entry", "blockNumber", k)
delete(t.winners, k)
}
}
}

commitments, err := t.store.GetCommitmentsByBlockNumber(newL1Block.BlockNumber.Int64())
if err != nil {
t.logger.Error("Failed to get commitments by block number", "blockNumber", newL1Block.BlockNumber, "error", err)
return err
}

t.logger.Info("Commitments retrieved", "count", len(commitments))

failedCommitments := make([]common.Hash, 0)
settled := 0
for _, commitment := range commitments {
if commitment.CommitmentIndex == nil {
t.logger.Info("Commitment index is nil, adding to failed commitments", "txnHash", commitment.TxnHash)
failedCommitments = append(failedCommitments, commitment.TxnHash)
continue
}
if common.BytesToAddress(commitment.ProviderAddress).Cmp(newL1Block.Winner) != 0 {
t.logger.Debug(
"provider address does not match the winner",
"Provider address does not match the winner",
"providerAddress", commitment.ProviderAddress,
"winner", newL1Block.Winner,
)
Expand All @@ -309,16 +316,18 @@ func (t *Tracker) handleNewL1Block(

bidAmt, ok := new(big.Int).SetString(commitment.Bid.BidAmount, 10)
if !ok {
t.logger.Error("failed to parse bid amount", "bidAmount", commitment.Bid.BidAmount)
t.logger.Error("Failed to parse bid amount", "bidAmount", commitment.Bid.BidAmount)
continue
}

opts, err := t.optsGetter(ctx)
if err != nil {
t.logger.Error("failed to get transact opts", "error", err)
t.logger.Error("Failed to get transact opts", "error", err)
continue
}

t.logger.Info("Opening commitment", "commitmentIdx", commitmentIdx, "bidAmount", bidAmt)

txHash, err := t.preconfContract.OpenCommitment(
opts,
commitmentIdx,
Expand All @@ -333,11 +342,11 @@ func (t *Tracker) handleNewL1Block(
commitment.PreConfirmation.SharedSecret,
)
if err != nil {
t.logger.Error("failed to open commitment", "error", err)
t.logger.Error("Failed to open commitment", "error", err)
continue
}
duration := time.Since(startTime)
t.logger.Info("opened commitment",
t.logger.Info("Opened commitment",
"txHash", txHash, "duration", duration,
"blockNumber", newL1Block.BlockNumber,
"commiter", common.Bytes2Hex(commitment.ProviderAddress),
Expand All @@ -347,31 +356,32 @@ func (t *Tracker) handleNewL1Block(

err = t.store.DeleteCommitmentByBlockNumber(newL1Block.BlockNumber.Int64())
if err != nil {
t.logger.Error("failed to delete commitments by block number", "error", err)
t.logger.Error("Failed to delete commitments by block number", "blockNumber", newL1Block.BlockNumber, "error", err)
return err
}

openDuration := time.Since(openStart)
t.metrics.totalCommitmentsToOpen.Add(float64(len(commitments)))
t.metrics.totalOpenedCommitments.Add(float64(settled))
t.metrics.blockCommitmentProcessDuration.Set(float64(openDuration))

t.logger.Info("commitments opened",
t.logger.Info("Commitments opened",
"blockNumber", newL1Block.BlockNumber,
"total", len(commitments),
"settled", settled,
"failed", len(failedCommitments),
"duration", openDuration,
)

t.metrics.totalCommitmentsToOpen.Add(float64(len(commitments)))
t.metrics.totalOpenedCommitments.Add(float64(settled))
t.metrics.blockCommitmentProcessDuration.Set(float64(openDuration))

if len(failedCommitments) > 0 {
t.logger.Info("Processing failed commitments", "count", len(failedCommitments))
receipts, err := t.receiptGetter.BatchReceipts(ctx, failedCommitments)
if err != nil {
t.logger.Warn("failed to get receipts for failed commitments", "error", err)
t.logger.Warn("Failed to get receipts for failed commitments", "error", err)
return nil
}
for i, receipt := range receipts {
t.logger.Debug("receipt for failed commitment",
t.logger.Debug("Receipt for failed commitment",
"txHash", failedCommitments[i],
"error", receipt.Err,
)
Expand Down

0 comments on commit 8bc062a

Please sign in to comment.