consensus: replace magic 1000000 with something more meaningful

This commit is contained in:
Roman Khimov 2020-07-11 15:22:14 +03:00
parent 579630a3fa
commit a43e374ac6
9 changed files with 17 additions and 14 deletions

View file

@ -65,7 +65,7 @@ func (n *neoBlock) PrevHash() util.Uint256 { return n.Block.PrevHash }
func (n *neoBlock) MerkleRoot() util.Uint256 { return n.Block.MerkleRoot }
// Timestamp implements block.Block interface.
func (n *neoBlock) Timestamp() uint64 { return n.Block.Timestamp * 1000000 }
func (n *neoBlock) Timestamp() uint64 { return n.Block.Timestamp * nsInMs }
// Index implements block.Block interface.
func (n *neoBlock) Index() uint32 { return n.Block.Index }

View file

@ -33,10 +33,10 @@ func (c changeView) NewViewNumber() byte { return c.newViewNumber }
func (c *changeView) SetNewViewNumber(view byte) { c.newViewNumber = view }
// Timestamp implements payload.ChangeView interface.
func (c changeView) Timestamp() uint64 { return c.timestamp * 1000000 }
func (c changeView) Timestamp() uint64 { return c.timestamp * nsInMs }
// SetTimestamp implements payload.ChangeView interface.
func (c *changeView) SetTimestamp(ts uint64) { c.timestamp = ts / 1000000 }
func (c *changeView) SetTimestamp(ts uint64) { c.timestamp = ts / nsInMs }
// Reason implements payload.ChangeView interface.
func (c changeView) Reason() payload.ChangeViewReason { return c.reason }

View file

@ -9,8 +9,8 @@ import (
func TestChangeView_Setters(t *testing.T) {
var c changeView
c.SetTimestamp(123 * 1000000)
require.EqualValues(t, 123*1000000, c.Timestamp())
c.SetTimestamp(123 * nsInMs)
require.EqualValues(t, 123*nsInMs, c.Timestamp())
c.SetNewViewNumber(2)
require.EqualValues(t, 2, c.NewViewNumber())

View file

@ -32,6 +32,9 @@ const cacheMaxCapacity = 100
// defaultTimePerBlock is a period between blocks which is used in NEO.
const defaultTimePerBlock = 15 * time.Second
// Number of nanoseconds in millisecond.
const nsInMs = 1000000
// Service represents consensus instance.
type Service interface {
// Start initializes dBFT and starts event loop for consensus service.
@ -488,7 +491,7 @@ func (s *service) newBlockFromContext(ctx *dbft.Context) block.Block {
}
block.Block.Network = s.network
block.Block.Timestamp = ctx.Timestamp / 1000000
block.Block.Timestamp = ctx.Timestamp / nsInMs
block.Block.Index = ctx.BlockIndex
validators, err := s.Chain.GetValidators()

View file

@ -63,7 +63,7 @@ func TestService_GetVerified(t *testing.T) {
p.SetPayload(&prepareRequest{transactionHashes: hashes})
} else {
p.SetType(payload.ChangeViewType)
p.SetPayload(&changeView{newViewNumber: 1, timestamp: uint64(time.Now().UnixNano() / 1000000)})
p.SetPayload(&changeView{newViewNumber: 1, timestamp: uint64(time.Now().UnixNano() / nsInMs)})
}
p.SetHeight(1)
p.SetValidatorIndex(uint16(i))

View file

@ -30,10 +30,10 @@ func (p *prepareRequest) DecodeBinary(r *io.BinReader) {
}
// Timestamp implements payload.PrepareRequest interface.
func (p *prepareRequest) Timestamp() uint64 { return p.timestamp * 1000000 }
func (p *prepareRequest) Timestamp() uint64 { return p.timestamp * nsInMs }
// SetTimestamp implements payload.PrepareRequest interface.
func (p *prepareRequest) SetTimestamp(ts uint64) { p.timestamp = ts / 1000000 }
func (p *prepareRequest) SetTimestamp(ts uint64) { p.timestamp = ts / nsInMs }
// Nonce implements payload.PrepareRequest interface.
func (p *prepareRequest) Nonce() uint64 { return p.nonce }

View file

@ -164,7 +164,7 @@ func (m *recoveryMessage) AddPayload(p payload.ConsensusPayload) {
m.changeViewPayloads = append(m.changeViewPayloads, &changeViewCompact{
ValidatorIndex: p.ValidatorIndex(),
OriginalViewNumber: p.ViewNumber(),
Timestamp: p.GetChangeView().Timestamp() / 1000000,
Timestamp: p.GetChangeView().Timestamp() / nsInMs,
InvocationScript: p.(*Payload).Witness.InvocationScript,
})
case payload.CommitType:

View file

@ -23,7 +23,7 @@ func (m *recoveryRequest) EncodeBinary(w *io.BinWriter) {
}
// Timestamp implements payload.RecoveryRequest interface.
func (m *recoveryRequest) Timestamp() uint64 { return m.timestamp * 1000000 }
func (m *recoveryRequest) Timestamp() uint64 { return m.timestamp * nsInMs }
// SetTimestamp implements payload.RecoveryRequest interface.
func (m *recoveryRequest) SetTimestamp(ts uint64) { m.timestamp = ts / 1000000 }
func (m *recoveryRequest) SetTimestamp(ts uint64) { m.timestamp = ts / nsInMs }

View file

@ -9,6 +9,6 @@ import (
func TestRecoveryRequest_Setters(t *testing.T) {
var r recoveryRequest
r.SetTimestamp(123 * 1000000)
require.EqualValues(t, 123*1000000, r.Timestamp())
r.SetTimestamp(123 * nsInMs)
require.EqualValues(t, 123*nsInMs, r.Timestamp())
}