Skip to content

Commit

Permalink
chore: change StateID.time to sfixed64
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Jun 28, 2023
1 parent c2285a4 commit 585388d
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 213 deletions.
9 changes: 3 additions & 6 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
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
282 changes: 128 additions & 154 deletions proto/tendermint/types/types.pb.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions proto/tendermint/types/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ message StateID {
bytes app_hash = 3 [(gogoproto.customname) = "AppHash"];
// CoreChainLockedHeight for the block, equal to Header.CoreChainLockedHeight.
fixed32 core_chain_locked_height = 4 [(gogoproto.customname) = "CoreChainLockedHeight"];
// Time of the block, truncated (rounded down) to millisecond
google.protobuf.Timestamp time = 5 [(gogoproto.nullable) = false];
// Time of the block in milliseconds since epoch, truncated (rounded down) to milliseconds
fixed64 time = 5;
}

// --------------------------------
Expand Down
33 changes: 15 additions & 18 deletions proto/tendermint/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"testing"
time "time"

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

"github.com/tendermint/tendermint/internal/libs/protoio"
"github.com/tendermint/tendermint/libs/rand"
tmtime "github.com/tendermint/tendermint/libs/time"
)

// TestVoteSignBytes checks if sign bytes are generated correctly.
Expand All @@ -24,7 +24,7 @@ func TestVoteSignBytes(t *testing.T) {
round = 2
chainID = "some-chain"
)
ts := &gogotypes.Timestamp{}
ts := uint64(0)
h := bytes.Repeat([]byte{1, 2, 3, 4}, 8)

type testCase struct {
Expand All @@ -40,7 +40,7 @@ func TestVoteSignBytes(t *testing.T) {
Height: height,
AppHash: h,
CoreChainLockedHeight: 1,
Time: *ts,
Time: ts,
},
vote: Vote{
Type: PrevoteType,
Expand All @@ -62,7 +62,7 @@ func TestVoteSignBytes(t *testing.T) {
Height: height,
AppHash: h,
CoreChainLockedHeight: 1,
Time: *ts,
Time: ts,
},
vote: Vote{
Type: PrecommitType,
Expand Down Expand Up @@ -98,6 +98,7 @@ func TestVoteSignBytes(t *testing.T) {
}

func TestStateID_Equals(t *testing.T) {
ts := mustTimestamp(time.Date(2019, 1, 2, 3, 4, 5, 6, time.UTC))
tests := []struct {
state1 StateID
state2 StateID
Expand All @@ -109,14 +110,14 @@ func TestStateID_Equals(t *testing.T) {
Height: 123,
AppHash: []byte("12345678901234567890123456789012"),
CoreChainLockedHeight: 12,
Time: *mustTimestamp(time.Date(2019, 1, 2, 3, 4, 5, 6, time.UTC)),
Time: ts,
},
StateID{
AppVersion: 12,
Height: 123,
AppHash: []byte("12345678901234567890123456789012"),
CoreChainLockedHeight: 12,
Time: *mustTimestamp(time.Date(2019, 1, 2, 3, 4, 5, 6, time.UTC)),
Time: ts,
},
true,
},
Expand All @@ -126,14 +127,14 @@ func TestStateID_Equals(t *testing.T) {
Height: 123,
AppHash: []byte("12345678901234567890123456789012"),
CoreChainLockedHeight: 12,
Time: *mustTimestamp(time.Date(2019, 1, 2, 3, 4, 5, 6, time.UTC)),
Time: ts,
},
StateID{
AppVersion: 12,
Height: 124,
AppHash: []byte("12345678901234567890123456789012"),
CoreChainLockedHeight: 12,
Time: *mustTimestamp(time.Date(2019, 1, 2, 3, 4, 5, 6, time.UTC)),
Time: ts,
},
false,
},
Expand All @@ -143,14 +144,14 @@ func TestStateID_Equals(t *testing.T) {
Height: 123,
AppHash: []byte("12345678901234567890123456789012"),
CoreChainLockedHeight: 12,
Time: *mustTimestamp(time.Date(2019, 1, 2, 3, 4, 5, 6, time.UTC)),
Time: ts,
},
StateID{
AppVersion: 12,
Height: 123,
AppHash: []byte("12345678901234567890123456789021"),
CoreChainLockedHeight: 12,
Time: *mustTimestamp(time.Date(2019, 1, 2, 3, 4, 5, 6, time.UTC)),
Time: ts,
},
false,
},
Expand All @@ -173,7 +174,7 @@ func TestStateIDIsZero(t *testing.T) {
expectZero: true,
},
{
StateID: StateID{Time: *gogotypes.TimestampNow()},
StateID: StateID{Time: uint64(tmtime.Now().UnixMilli())},
expectZero: false,
},
}
Expand All @@ -195,7 +196,7 @@ func TestStateIDSignBytes(t *testing.T) {
Height: 1,
AppHash: rand.Bytes(32),
CoreChainLockedHeight: 123,
Time: *mustTimestamp(time.Date(2022, 3, 4, 5, 6, 7, 8, time.UTC)),
Time: mustTimestamp(time.Date(2022, 3, 4, 5, 6, 7, 8, time.UTC)),
},
}

Expand All @@ -211,10 +212,6 @@ func TestStateIDSignBytes(t *testing.T) {
}
}

func mustTimestamp(t time.Time) *gogotypes.Timestamp {
ts, err := gogotypes.TimestampProto(t)
if err != nil {
panic(err)
}
return ts
func mustTimestamp(t time.Time) uint64 {
return uint64(t.UnixMilli())
}
7 changes: 2 additions & 5 deletions types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,17 +534,14 @@ func (h *Header) StateID() tmproto.StateID {
appHash = make([]byte, crypto.DefaultAppHashSize)
}

ts, err := gogotypes.TimestampProto(h.Time)
if err != nil || ts == nil {
panic("cannot convert time " + h.Time.String() + " to Timesstamp: " + err.Error())
}
ts := uint64(h.Time.UnixMilli())

return tmproto.StateID{
AppVersion: h.Version.App,
Height: uint64(h.Height),
AppHash: appHash,
CoreChainLockedHeight: h.CoreChainLockedHeight,
Time: *ts,
Time: ts,
}
}

Expand Down
10 changes: 5 additions & 5 deletions types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/tendermint/tendermint/crypto/merkle"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmtime "github.com/tendermint/tendermint/libs/time"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
"github.com/tendermint/tendermint/version"
Expand Down Expand Up @@ -337,8 +338,7 @@ func TestMaxCommitBytes(t *testing.T) {
}

func TestHeaderHash(t *testing.T) {
ts, err := gogotypes.TimestampProto(time.Date(2022, 3, 4, 5, 6, 7, 8, time.UTC))
require.NoError(t, err)
ts := uint64(time.Date(2022, 3, 4, 5, 6, 7, 8, time.UTC).UnixMilli())

testCases := []struct {
desc string
Expand All @@ -360,7 +360,7 @@ func TestHeaderHash(t *testing.T) {
Height: 3,
AppHash: crypto.Checksum([]byte("app_hash")),
CoreChainLockedHeight: 1,
Time: *ts,
Time: ts,
}.Hash(),
),
LastCommitHash: crypto.Checksum([]byte("last_commit_hash")),
Expand Down Expand Up @@ -1259,7 +1259,7 @@ func TestStateID_ValidateBasic(t *testing.T) {
AppVersion: StateIDVersion,
Height: 0,
AppHash: tmrand.Bytes(crypto.DefaultAppHashSize),
Time: *gogotypes.TimestampNow(),
Time: uint64(tmtime.Now().UnixMilli()),
},
wantErr: "",
},
Expand All @@ -1269,7 +1269,7 @@ func TestStateID_ValidateBasic(t *testing.T) {
AppVersion: StateIDVersion,
Height: 12,
AppHash: tmrand.Bytes(crypto.DefaultAppHashSize),
Time: *gogotypes.TimestampNow(),
Time: uint64(tmtime.Now().UnixMilli()),
},
wantErr: "",
},
Expand Down
6 changes: 2 additions & 4 deletions types/evidence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/dashpay/dashd-go/btcjson"
"github.com/gogo/protobuf/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -280,14 +279,13 @@ func TestEvidenceVectors(t *testing.T) {
val := NewMockPVForQuorum(quorumHash)
val.ProTxHash = make([]byte, crypto.ProTxHashSize)
key := bls12381.GenPrivKeyFromSecret([]byte("it's a secret")) // deterministic key
ts, err := types.TimestampProto(time.Date(2022, 1, 2, 3, 4, 5, 6, time.UTC))
require.NoError(t, err)
ts := uint64(time.Date(2022, 1, 2, 3, 4, 5, 6, time.UTC).UnixMilli())
stateID := tmproto.StateID{
AppVersion: StateIDVersion,
Height: 1,
AppHash: make([]byte, crypto.DefaultAppHashSize),
CoreChainLockedHeight: 1,
Time: *ts,
Time: ts,
}.Hash()
val.UpdatePrivateKey(context.Background(), key, quorumHash, key.PubKey(), 10)
blockID := makeBlockID(crypto.Checksum([]byte("blockhash")), math.MaxInt32, crypto.Checksum([]byte("partshash")), stateID)
Expand Down
6 changes: 2 additions & 4 deletions types/proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/dashpay/dashd-go/btcjson"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand All @@ -25,15 +24,14 @@ func getTestProposal(t testing.TB) *Proposal {
stamp, err := time.Parse(TimeFormat, "2018-02-11T07:09:22.765Z")
require.NoError(t, err)

ts, err := types.TimestampProto(stamp)
require.NoError(t, err)
ts := uint64(stamp.UnixMilli())

stateID := tmproto.StateID{
AppVersion: StateIDVersion,
Height: 12345,
AppHash: []byte("12345678901234567890123456789012"),
CoreChainLockedHeight: math.MaxUint32,
Time: *ts,
Time: ts,
}

return &Proposal{
Expand Down
5 changes: 2 additions & 3 deletions types/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"fmt"
"math/rand"

gogotypes "github.com/gogo/protobuf/types"

"github.com/tendermint/tendermint/crypto"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmtime "github.com/tendermint/tendermint/libs/time"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

Expand All @@ -20,7 +19,7 @@ func RandStateID() tmproto.StateID {
AppHash: tmrand.Bytes(crypto.DefaultAppHashSize),
AppVersion: StateIDVersion,
CoreChainLockedHeight: rand.Uint32(), //nolint:gosec
Time: *gogotypes.TimestampNow(),
Time: uint64(tmtime.Now().UnixMilli()),
}
}

Expand Down
5 changes: 2 additions & 3 deletions types/vote_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

"github.com/dashpay/dashd-go/btcjson"
"github.com/gogo/protobuf/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -681,13 +680,13 @@ func withBlockHash(vote *Vote, blockHash []byte) *Vote {
vote = vote.Copy()
vote.BlockID.Hash = blockHash

ts, _ := types.TimestampProto(time.Date(2022, 1, 2, 3, 4, 5, 6, time.UTC))
ts := uint64(time.Date(2022, 1, 2, 3, 4, 5, 6, time.UTC).UnixMilli())
vote.BlockID.StateID = tmproto.StateID{
AppVersion: StateIDVersion,
Height: uint64(vote.Height),
AppHash: blockHash,
CoreChainLockedHeight: 1,
Time: *ts,
Time: ts,
}.Hash()
return vote
}
Expand Down
3 changes: 1 addition & 2 deletions types/vote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
bls "github.com/dashpay/bls-signatures/go-bindings"
"github.com/dashpay/dashd-go/btcjson"
"github.com/gogo/protobuf/proto"
gogotypes "github.com/gogo/protobuf/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -59,7 +58,7 @@ func exampleVote(tb testing.TB, t byte) *Vote {
AppHash: appHash,
AppVersion: StateIDVersion,
CoreChainLockedHeight: 3,
Time: gogotypes.Timestamp{},
Time: 0,
}
return &Vote{
Type: tmproto.SignedMsgType(t),
Expand Down

0 comments on commit 585388d

Please sign in to comment.