diff --git a/block/block.go b/block/block.go index d5248399..98fc8fcd 100644 --- a/block/block.go +++ b/block/block.go @@ -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. diff --git a/block/block_test.go b/block/block_test.go index d8aebde6..22c6e61e 100644 --- a/block/block_test.go +++ b/block/block_test.go @@ -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()) diff --git a/context.go b/context.go index 31222fa9..2d89ac2e 100644 --- a/context.go +++ b/context.go @@ -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 { diff --git a/payload/prepare_request.go b/payload/prepare_request.go index 6ef1cad8..793c2dba 100644 --- a/payload/prepare_request.go +++ b/payload/prepare_request.go @@ -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. diff --git a/payload/recovery_message.go b/payload/recovery_message.go index 827a6484..4faf0305 100644 --- a/payload/recovery_message.go +++ b/payload/recovery_message.go @@ -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(),