Skip to content

Commit

Permalink
fix: use nanoseconds in Header.Time everywhere (#1225)
Browse files Browse the repository at this point in the history
  • Loading branch information
keruch authored Nov 14, 2024
1 parent a11a511 commit 792e757
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 105 deletions.
2 changes: 1 addition & 1 deletion block/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (e *Executor) CreateBlock(
},
ChainID: e.chainID,
Height: height,
Time: uint64(time.Now().UTC().UnixNano()),
Time: time.Now().UTC().UnixNano(),
LastHeaderHash: lastHeaderHash,
DataHash: [32]byte{},
ConsensusHash: [32]byte{},
Expand Down
12 changes: 6 additions & 6 deletions block/production_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func TestCreateEmptyBlocksEnableDisable(t *testing.T) {

block, err := managerWithEmptyBlocks.Store.LoadBlock(i)
assert.NoError(err)
assert.NotZero(block.Header.Time)
assert.NotZero(block.Header.GetTimestamp())

diff := time.Unix(0, int64(block.Header.Time)).Sub(time.Unix(0, int64(prevBlock.Header.Time)))
diff := block.Header.GetTimestamp().Sub(prevBlock.Header.GetTimestamp())
assert.Greater(diff, blockTime-blockTime/10)
assert.Less(diff, blockTime+blockTime/10)
}
Expand All @@ -118,9 +118,9 @@ func TestCreateEmptyBlocksEnableDisable(t *testing.T) {

block, err := manager.Store.LoadBlock(i)
assert.NoError(err)
assert.NotZero(block.Header.Time)
assert.NotZero(block.Header.GetTimestamp())

diff := time.Unix(0, int64(block.Header.Time)).Sub(time.Unix(0, int64(prevBlock.Header.Time)))
diff := block.Header.GetTimestamp().Sub(prevBlock.Header.GetTimestamp())
assert.Greater(diff, manager.Conf.MaxIdleTime)
}
}
Expand Down Expand Up @@ -185,9 +185,9 @@ func TestCreateEmptyBlocksNew(t *testing.T) {

block, err := manager.Store.LoadBlock(i)
assert.NoError(err)
assert.NotZero(block.Header.Time)
assert.NotZero(block.Header.GetTimestamp())

diff := time.Unix(0, int64(block.Header.Time)).Sub(time.Unix(0, int64(prevBlock.Header.Time)))
diff := block.Header.GetTimestamp().Sub(prevBlock.Header.GetTimestamp())
txsCount := len(block.Data.Txs)
if txsCount == 0 {
assert.Greater(diff, manager.Conf.MaxIdleTime)
Expand Down
4 changes: 2 additions & 2 deletions proto/types/dymint/dymint.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ message Header {
// Block height
uint64 height = 3;

// Block creation time
uint64 time = 4;
// Block creation time in nanoseconds. Use int64 as Golang stores UNIX nanoseconds in int64.
int64 time = 4;

// Previous block info
bytes last_header_hash = 5;
Expand Down
7 changes: 4 additions & 3 deletions rpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import (
tmtypes "github.com/tendermint/tendermint/types"
tm_version "github.com/tendermint/tendermint/version"

"github.com/dymensionxyz/gerr-cosmos/gerrc"

"github.com/dymensionxyz/dymint/mempool"
"github.com/dymensionxyz/dymint/node"
"github.com/dymensionxyz/dymint/types"
"github.com/dymensionxyz/dymint/version"
"github.com/dymensionxyz/gerr-cosmos/gerrc"
)

const (
Expand Down Expand Up @@ -718,7 +719,7 @@ func (c *Client) Status(ctx context.Context) (*ctypes.ResultStatus, error) {
latestBlockHash := latest.Header.DataHash
latestAppHash := latest.Header.AppHash
latestHeight := latest.Header.Height
latestBlockTimeNano := latest.Header.Time
latestBlockTime := latest.Header.GetTimestamp()

proposer, err := c.node.Store.LoadProposer(latest.Header.Height)
if err != nil {
Expand Down Expand Up @@ -755,7 +756,7 @@ func (c *Client) Status(ctx context.Context) (*ctypes.ResultStatus, error) {
LatestBlockHash: latestBlockHash[:],
LatestAppHash: latestAppHash[:],
LatestBlockHeight: int64(latestHeight),
LatestBlockTime: time.Unix(0, int64(latestBlockTimeNano)),
LatestBlockTime: latestBlockTime,
// CatchingUp is true if the node is not at the latest height received from p2p or da.
CatchingUp: c.node.BlockManager.TargetHeight.Load() > latestHeight,
// TODO(tzdybal): add missing fields
Expand Down
9 changes: 5 additions & 4 deletions test/loadtime/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"sync"
"time"

"github.com/dymensionxyz/dymint/test/loadtime/payload"
"github.com/dymensionxyz/dymint/types"
"github.com/gofrs/uuid"
"gonum.org/v1/gonum/stat"

"github.com/dymensionxyz/dymint/test/loadtime/payload"
"github.com/dymensionxyz/dymint/types"
)

// BlockStore defines the set of methods needed by the report generator from
Expand Down Expand Up @@ -226,8 +227,8 @@ func GenerateFromBlockStore(s BlockStore) (*Reports, error) {
panic(err)
}
for _, tx := range cur.Data.Txs {
UTCfromUnixNano := time.Unix(0, int64(cur.Header.Time))
txc <- txData{tx: tx, bt: UTCfromUnixNano}
utcFromUnixNano := cur.Header.GetTimestamp()
txc <- txData{tx: tx, bt: utcFromUnixNano}
}
}
close(txc)
Expand Down
11 changes: 6 additions & 5 deletions test/loadtime/report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"testing"
"time"

"github.com/google/uuid"

"github.com/dymensionxyz/dymint/test/loadtime/payload"
"github.com/dymensionxyz/dymint/test/loadtime/report"
"github.com/dymensionxyz/dymint/test/pb/loadtime"
"github.com/dymensionxyz/dymint/types"
"github.com/google/uuid"
)

type mockBlockStore struct {
Expand Down Expand Up @@ -63,15 +64,15 @@ func TestGenerateReport(t *testing.T) {
blocks: []*types.Block{
{
Header: types.Header{
Time: uint64(t1.UTC().UnixNano()),
Time: t1.UTC().UnixNano(),
},
Data: types.Data{
Txs: []types.Tx{b1, b2},
},
},
{
Header: types.Header{
Time: uint64(t1.UTC().UnixNano()),
Time: t1.UTC().UnixNano(),
},
Data: types.Data{
Txs: []types.Tx{[]byte("error")},
Expand All @@ -82,12 +83,12 @@ func TestGenerateReport(t *testing.T) {
Txs: []types.Tx{b3, b3},
},
Header: types.Header{
Time: uint64(t2.UTC().UnixNano()),
Time: t2.UTC().UnixNano(),
},
},
{
Header: types.Header{
Time: uint64(t2.UTC().UnixNano()),
Time: t2.UTC().UnixNano(),
},
Data: types.Data{
Txs: []types.Tx{},
Expand Down
4 changes: 2 additions & 2 deletions types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Header struct {
Version Version

Height uint64
Time uint64 // UNIX time in milliseconds
Time int64 // UNIX time in nanoseconds. Use int64 as Golang stores UNIX nanoseconds in int64.

// prev block info
LastHeaderHash [32]byte
Expand Down Expand Up @@ -46,7 +46,7 @@ type Header struct {
}

func (h Header) GetTimestamp() time.Time {
return time.Unix(0, int64(h.Time))
return time.Unix(0, h.Time)
}

var (
Expand Down
6 changes: 2 additions & 4 deletions types/conv.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package types

import (
"time"

"github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/proto/tendermint/version"
tmtypes "github.com/tendermint/tendermint/types"
Expand All @@ -24,7 +22,7 @@ func ToABCIHeader(header *Header) tmtypes.Header {
App: header.Version.App,
},
Height: int64(header.Height),
Time: time.Unix(0, int64(header.Time)),
Time: header.GetTimestamp(),
LastBlockID: tmtypes.BlockID{
Hash: header.LastHeaderHash[:],
PartSetHeader: tmtypes.PartSetHeader{
Expand Down Expand Up @@ -120,7 +118,7 @@ func ToABCICommit(commit *Commit, header *Header) *tmtypes.Commit {
// This assumes that we have only one signature
if len(commit.Signatures) == 1 {
tmCommit.Signatures[0].ValidatorAddress = header.ProposerAddress
tmCommit.Signatures[0].Timestamp = time.Unix(0, int64(header.Time))
tmCommit.Signatures[0].Timestamp = header.GetTimestamp()
}
} else {
tmCommit.Signatures = append(tmCommit.Signatures, commit.TMSignature)
Expand Down
5 changes: 3 additions & 2 deletions types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,15 @@ type ErrTimeFraud struct {
}

func NewErrTimeFraud(block *Block, currentTime time.Time) error {
drift := time.Unix(int64(block.Header.Time), 0).Sub(currentTime)
headerTime := block.Header.GetTimestamp()
drift := headerTime.Sub(currentTime)

return ErrTimeFraud{
Drift: drift,
ProposerAddress: block.Header.ProposerAddress,
HeaderHash: block.Header.Hash(),
HeaderHeight: block.Header.Height,
HeaderTime: time.Unix(int64(block.Header.Time), 0),
HeaderTime: headerTime,
CurrentTime: currentTime,
}
}
Expand Down
122 changes: 61 additions & 61 deletions types/pb/dymint/dymint.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion types/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (b *Block) ValidateWithState(state *State) error {
}

currentTime := time.Now().UTC()
if currentTime.Add(TimeFraudMaxDrift).Before(time.Unix(0, int64(b.Header.Time))) {
if currentTime.Add(TimeFraudMaxDrift).Before(b.Header.GetTimestamp()) {
return NewErrTimeFraud(b, currentTime)
}

Expand Down
Loading

0 comments on commit 792e757

Please sign in to comment.