Skip to content
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

Fix JSON serialization of timestamp field for CommitSig::BlockIdFlagAbsent #1353

Merged
merged 5 commits into from
Sep 14, 2023

Conversation

romac
Copy link
Member

@romac romac commented Sep 14, 2023

See #1352 for the rationale behind this fix.

  • Referenced an issue explaining the need for the change
  • Updated all relevant documentation in docs
  • Updated all code comments where relevant
  • Wrote tests
  • Added entry in .changelog/

…agAbsent`

`CommitSig::BlockIdFlagAbsent` currently serializes to JSON as

```json
{
  "block_id_flag": 1,
  "validator_address": "",
  "timestamp": "1970-01-01T00:00:00Z",
  "signature": ""
}```

which fails the CometBFT check `CommitSig.ValidateBasic()` with `time is present`

This check is performed as

```go
switch cs.BlockIDFlag {
  case BlockIDFlagAbsent:
    if len(cs.ValidatorAddress) != 0 {
      return errors.New("validator address is present")
    }
    if !cs.Timestamp.IsZero() {
      return errors.New("time is present")
}
```

`IsZero()` is defined as

```go
// IsZero reports whether t represents the zero time instant,
// January 1, year 1, 00:00:00 UTC.
func (t Time) IsZero() bool {
  return t.sec() == 0 && t.nsec() == 0
}
```

From the documentation of the `Time` struct, we see that it expects a
zero time to be `0001-01-01T00:00:00Z` and not `1970-01-01T00:00:00Z`:

```go
// The zero value for a Time is defined to be
//	January 1, year 1, 00:00:00.000000000 UTC
// which (1) looks like a zero, or as close as you can get in a date
// (1-1-1 00:00:00 UTC), (2) is unlikely enough to arise in practice to
// be a suitable "not set" sentinel, unlike Jan 1 1970, and (3) has a
// non-negative year even in time zones west of UTC, unlike 1-1-0
// 00:00:00 UTC, which would be 12-31-(-1) 19:00:00 in New York.
```

This PR fixes this discrepency for the case of `BlockIDFlagAbsent`.

There may be other instances where the timestamp is formatted
incorrectly, but those haven't been seen in production yet.

We should follow-up on that eventually.
@romac romac requested a review from mzabaluev September 14, 2023 10:31
@romac romac marked this pull request as ready for review September 14, 2023 13:24
Copy link
Contributor

@mzabaluev mzabaluev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants