Skip to content

Commit

Permalink
fix!: use millisecond resolution of block time (#650)
Browse files Browse the repository at this point in the history
* chore!: block time resolution changed to milliseconds

* test(consensus): adjust pbts tests for milliseconds

* doc(proto): block time is truncated to milliseconds

* chore(version): bump abci and block protocol version

* test(e2e): fix genesis time resolution to milliseconds

* chore: change StateID.time to sfixed64

* test: update test vectors for new StateID.time type
  • Loading branch information
lklimek authored Jun 28, 2023
1 parent 200c050 commit fdbf643
Show file tree
Hide file tree
Showing 23 changed files with 208 additions and 248 deletions.
4 changes: 2 additions & 2 deletions cmd/tenderdash/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"time"

"github.com/dashpay/dashd-go/btcjson"
"github.com/spf13/cobra"
Expand All @@ -13,6 +12,7 @@ import (
"github.com/tendermint/tendermint/libs/log"
tmos "github.com/tendermint/tendermint/libs/os"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmtime "github.com/tendermint/tendermint/libs/time"
"github.com/tendermint/tendermint/privval"
"github.com/tendermint/tendermint/types"
)
Expand Down Expand Up @@ -103,7 +103,7 @@ func initFilesWithConfig(ctx context.Context, conf nodeConfig, logger log.Logger

genDoc := types.GenesisDoc{
ChainID: fmt.Sprintf("test-chain-%v", tmrand.Str(6)),
GenesisTime: time.Now(),
GenesisTime: tmtime.Now(),
ConsensusParams: types.DefaultConsensusParams(),
QuorumType: btcjson.LLMQType(conf.quorumType),
InitialCoreChainLockedHeight: conf.coreChainLockedHeight,
Expand Down
2 changes: 1 addition & 1 deletion config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ func writeFile(filePath string, contents []byte, mode os.FileMode) error {
}

const testGenesisFmt = `{
"genesis_time": "2018-10-10T08:20:13.695936996Z",
"genesis_time": "2018-10-10T08:20:13.695Z",
"chain_id": "%s",
"initial_height": "1",
"initial_core_chain_locked_height": 1,
Expand Down
2 changes: 1 addition & 1 deletion docs/tendermint-core/using-tendermint.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ definition](https://github.com/tenderdash/tenderdash/blob/master/types/genesis.g

```json
{
"genesis_time": "2020-04-21T11:17:42.341227868Z",
"genesis_time": "2020-04-21T11:17:42.341Z",
"chain_id": "test-chain-ROp9KF",
"initial_height": "0",
"initial_core_chain_locked_height": 1,
Expand Down
14 changes: 7 additions & 7 deletions internal/consensus/pbts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,20 +413,20 @@ func TestProposerWaitTime(t *testing.T) {
}{
{
name: "block time greater than local time",
previousBlockTime: genesisTime.Add(5 * time.Nanosecond),
localTime: genesisTime.Add(1 * time.Nanosecond),
expectedWait: 4 * time.Nanosecond,
previousBlockTime: genesisTime.Add(5 * time.Millisecond),
localTime: genesisTime.Add(1 * time.Millisecond),
expectedWait: 4 * time.Millisecond,
},
{
name: "local time greater than block time",
previousBlockTime: genesisTime.Add(1 * time.Nanosecond),
localTime: genesisTime.Add(5 * time.Nanosecond),
previousBlockTime: genesisTime.Add(1 * time.Millisecond),
localTime: genesisTime.Add(5 * time.Millisecond),
expectedWait: 0,
},
{
name: "both times equal",
previousBlockTime: genesisTime.Add(5 * time.Nanosecond),
localTime: genesisTime.Add(5 * time.Nanosecond),
previousBlockTime: genesisTime.Add(5 * time.Millisecond),
localTime: genesisTime.Add(5 * time.Millisecond),
expectedWait: 0,
},
}
Expand Down
3 changes: 1 addition & 2 deletions internal/consensus/peer_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
cstypes "github.com/tendermint/tendermint/internal/consensus/types"
"github.com/tendermint/tendermint/libs/bits"
"github.com/tendermint/tendermint/libs/log"
tmtime "github.com/tendermint/tendermint/libs/time"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/types"
)
Expand Down Expand Up @@ -498,7 +497,7 @@ func (ps *PeerState) ApplyNewRoundStepMessage(msg *NewRoundStepMessage) {
psRound = ps.PRS.Round
psCatchupCommitRound = ps.PRS.CatchupCommitRound
psCatchupCommit = ps.PRS.CatchupCommit
startTime = tmtime.Now().Add(-1 * time.Duration(msg.SecondsSinceStartTime) * time.Second)
startTime = time.Now().Add(-1 * time.Duration(msg.SecondsSinceStartTime) * time.Second)
)

ps.PRS.Height = msg.Height
Expand Down
4 changes: 2 additions & 2 deletions internal/consensus/state_prevoter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"regexp"
"testing"
"time"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/tendermint/tendermint/internal/test/factory"
tmrequire "github.com/tendermint/tendermint/internal/test/require"
"github.com/tendermint/tendermint/libs/log"
tmtime "github.com/tendermint/tendermint/libs/time"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/types"
)
Expand Down Expand Up @@ -292,7 +292,7 @@ func (suite *PrevoterTestSuite) TestCheckPrevoteMaj23() {
}

func (suite *PrevoterTestSuite) makeValidStateData() StateData {
now := time.Now()
now := tmtime.Now()
validState := sm.State{
InitialHeight: 1000,
LastBlockTime: now,
Expand Down
2 changes: 1 addition & 1 deletion internal/consensus/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func TestStateProposalTime(t *testing.T) {
expectNewBlock: false,
},
{ // TEST 1: BLOCK TIME IS IN FUTURE
blockTimeFunc: func(s *State) time.Time { return time.Now().Add(delay + precision + 24*time.Hour) },
blockTimeFunc: func(s *State) time.Time { return tmtime.Now().Add(delay + precision + 24*time.Hour) },
expectNewBlock: true,
},
{ // TEST 2: BLOCK TIME IS OLDER THAN PREVIOUS BLOCK TIME
Expand Down
2 changes: 1 addition & 1 deletion libs/time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ func Now() time.Time {
// Stripping the monotonic component is for time equality.
// See https://github.com/tendermint/tendermint/pull/2203#discussion_r215064334
func Canonical(t time.Time) time.Time {
return t.Round(0).UTC()
return t.Truncate(time.Millisecond).UTC()
}
3 changes: 2 additions & 1 deletion privval/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/tendermint/tendermint/crypto/bls12381"
"github.com/tendermint/tendermint/libs/log"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmtime "github.com/tendermint/tendermint/libs/time"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/types"
)
Expand Down Expand Up @@ -424,7 +425,7 @@ func newProposal(height int64, coreChainLockedHeight uint32, round int32, blockI
CoreChainLockedHeight: coreChainLockedHeight,
Round: round,
BlockID: blockID,
Timestamp: time.Now(),
Timestamp: tmtime.Now(),
}
}

Expand Down
13 changes: 5 additions & 8 deletions privval/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/gogo/protobuf/proto"
gogotypes "github.com/gogo/protobuf/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand All @@ -23,10 +22,8 @@ var stamp = time.Date(2019, 10, 13, 16, 14, 44, 0, time.UTC)

func exampleVote() *types.Vote {

ts, err := gogotypes.TimestampProto(time.Date(2022, 3, 4, 5, 6, 7, 8, time.UTC))
if err != nil {
panic(err)
}
ts := uint64(time.Date(2022, 3, 4, 5, 6, 7, 8, time.UTC).UnixMilli())

return &types.Vote{
Type: tmproto.PrecommitType,
Height: 3,
Expand All @@ -42,7 +39,7 @@ func exampleVote() *types.Vote {
Height: 3,
AppHash: crypto.Checksum([]byte("apphash")),
CoreChainLockedHeight: 12345,
Time: *ts,
Time: ts,
}.Hash(),
},
ValidatorProTxHash: crypto.ProTxHashFromSeedBytes([]byte("validator_pro_tx_hash")),
Expand Down Expand Up @@ -98,8 +95,8 @@ func TestPrivvalVectors(t *testing.T) {
{"pubKey request", &privproto.PubKeyRequest{}, "0a00"},
{"pubKey response", &privproto.PubKeyResponse{PubKey: ppk, Error: nil}, "12340a321a30991a1c4f159f8e4730bf897e97e27c11f27ba0c1337111a3c102e1081a19372832b596623b1a248a0e00b156d80690cf"},
{"pubKey response with error", &privproto.PubKeyResponse{PubKey: cryptoproto.PublicKey{}, Error: remoteError}, "12140a0012100801120c697427732061206572726f72"},
{"Vote Request", &privproto.SignVoteRequest{Vote: votepb}, "1aaa010aa701080210031802226c0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a1a2071aa7631e86d2b19d27f0c63e41ed08e5f8d43cfbb69d35913a7731b61bbdc622a20959a8f5ef2be68d0ed3a07ed8cff85991ee7995c2ac17030f742c135f9729fbe30d5bb03420b1209657874656e73696f6e"},
{"Vote Response", &privproto.SignedVoteResponse{Vote: *votepb, Error: nil}, "22aa010aa701080210031802226c0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a1a2071aa7631e86d2b19d27f0c63e41ed08e5f8d43cfbb69d35913a7731b61bbdc622a20959a8f5ef2be68d0ed3a07ed8cff85991ee7995c2ac17030f742c135f9729fbe30d5bb03420b1209657874656e73696f6e"},
{"Vote Request", &privproto.SignVoteRequest{Vote: votepb}, "1aaa010aa701080210031802226c0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a1a20b583d49b95a0a5526966b519d8b7bba2aefc800b370a7438c7063728904e58ee2a20959a8f5ef2be68d0ed3a07ed8cff85991ee7995c2ac17030f742c135f9729fbe30d5bb03420b1209657874656e73696f6e"},
{"Vote Response", &privproto.SignedVoteResponse{Vote: *votepb, Error: nil}, "22aa010aa701080210031802226c0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a1a20b583d49b95a0a5526966b519d8b7bba2aefc800b370a7438c7063728904e58ee2a20959a8f5ef2be68d0ed3a07ed8cff85991ee7995c2ac17030f742c135f9729fbe30d5bb03420b1209657874656e73696f6e"},
{"Vote Response with error", &privproto.SignedVoteResponse{Vote: tmproto.Vote{}, Error: remoteError}, "22180a042202120012100801120c697427732061206572726f72"},
{"Proposal Request", &privproto.SignProposalRequest{Proposal: proposalpb}, "2a700a6e08011003180220022a4a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a320608f49a8ded053a10697427732061207369676e6174757265"},
{"Proposal Response", &privproto.SignedProposalResponse{Proposal: *proposalpb, Error: nil}, "32700a6e08011003180220022a4a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a320608f49a8ded053a10697427732061207369676e6174757265"},
Expand Down
12 changes: 5 additions & 7 deletions proto/tendermint/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"bytes"
"fmt"

"github.com/gogo/protobuf/types"

"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/internal/libs/protoio"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
Expand Down Expand Up @@ -120,7 +118,7 @@ func (s *StateID) IsZero() bool {
s.AppVersion == 0 &&
s.CoreChainLockedHeight == 0 &&
s.Height == 0 &&
s.Time.Equal(types.Timestamp{}))
s.Time == 0)
}

// Copy returns new StateID that is equal to this one
Expand All @@ -134,12 +132,12 @@ func (s StateID) Copy() StateID {

func (s StateID) String() string {
return fmt.Sprintf(
`v%d:h=%d,cl=%d,ah=%s,t=%s`,
`v%d:h=%d,cl=%d,ah=%s,t=%d`,
s.AppVersion,
s.Height,
s.CoreChainLockedHeight,
tmbytes.HexBytes(s.AppHash).ShortString(),
s.Time.String(),
s.Time,
)
}

Expand All @@ -159,8 +157,8 @@ func (s StateID) Equal(other StateID) bool {

// ValidateBasic performs basic validation.
func (s StateID) ValidateBasic() error {
if s.Time.Equal(types.Timestamp{}) {
return fmt.Errorf("invalid stateID time %s", s.Time.String())
if s.Time == 0 {
return fmt.Errorf("invalid stateID time %d", s.Time)
}
if len(s.AppHash) != crypto.DefaultAppHashSize {
return fmt.Errorf(
Expand Down
Loading

0 comments on commit fdbf643

Please sign in to comment.