Skip to content

Commit

Permalink
Fix the nonce starting point debug log (#408)
Browse files Browse the repository at this point in the history
I saw this log
```
$ docker logs libxmtp-repnode-1
2025-01-13T19:00:47.094Z        INFO    replication     Version: v0.1.3-38-g0a896ad
2025-01-13T19:00:47.190Z        INFO    replication     Starting server with blockchain nonce: 18446744073709551615
2025-01-13T19:00:47.192Z        INFO    replication     Registrant identified   {"nodeId": 100}
```

and realized that we are printing the nonce is a bit of a confusing
fashion.

The issue with the code lies in attempting to decrement a uint64 value
(nonce) and ensuring it doesn't underflow. Since uint64 is an unsigned
integer, it cannot hold negative values. If nonce is already 0,
decrementing it would result in an underflow, wrapping around to its
maximum value (18446744073709551615).

The resulting behavior is exactly the same, but the log no longer looks
like an error.
  • Loading branch information
mkysel authored Jan 13, 2025
1 parent 0a896ad commit 9d0de26
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pkg/blockchain/blockchainPublisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ func NewBlockchainPublisher(
return nil, err
}

logger.Info(fmt.Sprintf("Starting server with blockchain nonce: %d", nonce))

// The nonce is the next ID to be used, not the current highest
// The nonce member variable represents the last recenty used, so it is pending-1
nonce = max(nonce-1, uint64(0))

logger.Info(fmt.Sprintf("Starting server with blockchain nonce: %d", nonce))
// If the nonce is 0, it will underflow to 18446744073709551615, which at +1 generates the correct next nonce of 0
nonce--

return &BlockchainPublisher{
signer: signer,
Expand Down

0 comments on commit 9d0de26

Please sign in to comment.