Skip to content

Commit

Permalink
*: keep Timestamp in nanoseconds-precision state
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnaShaleva authored and fyrchik committed Apr 27, 2020
1 parent 6604647 commit 15a7927
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ func (b *neoBlock) SetMerkleRoot(r util.Uint256) {

// Timestamp implements Block interface.
func (b *neoBlock) Timestamp() uint64 {
return uint64(b.base.Timestamp)
return uint64(b.base.Timestamp) * 1000000000
}

// SetTimestamp implements Block interface.
func (b *neoBlock) SetTimestamp(ts uint64) {
b.base.Timestamp = uint32(ts)
b.base.Timestamp = uint32(ts / 1000000000)
}

// Index implements Block interface.
Expand Down
9 changes: 7 additions & 2 deletions block/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ func TestNeoBlock_Setters(t *testing.T) {
b.SetMerkleRoot(util.Uint256{13})
assert.Equal(t, util.Uint256{13}, b.MerkleRoot())

b.SetTimestamp(12345)
assert.EqualValues(t, 12345, b.Timestamp())
b.SetTimestamp(1234)
// 1234ns -> 0s -> 0ns
assert.EqualValues(t, uint64(0), b.Timestamp())

b.SetTimestamp(9123456789)
// 9123456789ns -> 9s -> 9000000000ns
assert.EqualValues(t, uint64(9000000000), b.Timestamp())

b.SetIndex(100)
assert.EqualValues(t, 100, b.Index())
Expand Down
8 changes: 7 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,19 @@ func (c *Context) Fill() {
validators := c.Config.GetValidators(txx...)
c.NextConsensus = c.Config.GetConsensusAddress(validators...)

if now := uint64(c.Config.Timer.Now().Unix()); now > c.Timestamp {
if now := c.getTimestamp(); now > c.Timestamp {
c.Timestamp = now
} else {
c.Timestamp++
}
}

// getTimestamp returns nanoseconds-precision timestamp using
// current context config.
func (c *Context) getTimestamp() uint64 {
return uint64(c.Config.Timer.Now().UnixNano())
}

// CreateBlock returns resulting block for the current epoch.
func (c *Context) CreateBlock() block.Block {
if c.block == nil {
Expand Down
4 changes: 2 additions & 2 deletions payload/prepare_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ func (p *prepareRequest) DecodeBinary(r *io.BinReader) {

// Timestamp implements PrepareRequest interface.
func (p prepareRequest) Timestamp() uint64 {
return uint64(p.timestamp)
return uint64(p.timestamp) * 1000000000
}

// SetTimestamp implements PrepareRequest interface.
func (p *prepareRequest) SetTimestamp(ts uint64) {
p.timestamp = uint32(ts)
p.timestamp = uint32(ts / 1000000000)
}

// Nonce implements PrepareRequest interface.
Expand Down
3 changes: 2 additions & 1 deletion payload/recovery_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ func (m *recoveryMessage) GetPrepareRequest(p ConsensusPayload, _ []crypto.Publi
}

req := fromPayload(PrepareRequestType, p, &prepareRequest{
timestamp: uint32(m.prepareRequest.Timestamp()),
// prepareRequest.Timestamp() here returns nanoseconds-precision value, so convert it to seconds again
timestamp: uint32(m.prepareRequest.Timestamp() / 1000000000),
nonce: m.prepareRequest.Nonce(),
transactionHashes: m.prepareRequest.TransactionHashes(),
nextConsensus: m.prepareRequest.NextConsensus(),
Expand Down

0 comments on commit 15a7927

Please sign in to comment.