consensus: update dbft, use millisecond-precision time, add CV reason

This commit is contained in:
Roman Khimov 2020-07-11 10:48:25 +03:00
parent 97ea5593b0
commit 815c075112
9 changed files with 34 additions and 25 deletions

2
go.mod
View file

@ -9,7 +9,7 @@ require (
github.com/go-yaml/yaml v2.1.0+incompatible github.com/go-yaml/yaml v2.1.0+incompatible
github.com/gorilla/websocket v1.4.2 github.com/gorilla/websocket v1.4.2
github.com/mr-tron/base58 v1.1.2 github.com/mr-tron/base58 v1.1.2
github.com/nspcc-dev/dbft v0.0.0-20200623100921-5a182c20965e github.com/nspcc-dev/dbft v0.0.0-20200711144034-c526ccc6f570
github.com/nspcc-dev/rfc6979 v0.2.0 github.com/nspcc-dev/rfc6979 v0.2.0
github.com/pierrec/lz4 v2.5.2+incompatible github.com/pierrec/lz4 v2.5.2+incompatible
github.com/pkg/errors v0.8.1 github.com/pkg/errors v0.8.1

4
go.sum
View file

@ -137,8 +137,8 @@ github.com/nspcc-dev/dbft v0.0.0-20200117124306-478e5cfbf03a h1:ajvxgEe9qY4vvoSm
github.com/nspcc-dev/dbft v0.0.0-20200117124306-478e5cfbf03a/go.mod h1:/YFK+XOxxg0Bfm6P92lY5eDSLYfp06XOdL8KAVgXjVk= github.com/nspcc-dev/dbft v0.0.0-20200117124306-478e5cfbf03a/go.mod h1:/YFK+XOxxg0Bfm6P92lY5eDSLYfp06XOdL8KAVgXjVk=
github.com/nspcc-dev/dbft v0.0.0-20200219114139-199d286ed6c1 h1:yEx9WznS+rjE0jl0dLujCxuZSIb+UTjF+005TJu/nNI= github.com/nspcc-dev/dbft v0.0.0-20200219114139-199d286ed6c1 h1:yEx9WznS+rjE0jl0dLujCxuZSIb+UTjF+005TJu/nNI=
github.com/nspcc-dev/dbft v0.0.0-20200219114139-199d286ed6c1/go.mod h1:O0qtn62prQSqizzoagHmuuKoz8QMkU3SzBoKdEvm3aQ= github.com/nspcc-dev/dbft v0.0.0-20200219114139-199d286ed6c1/go.mod h1:O0qtn62prQSqizzoagHmuuKoz8QMkU3SzBoKdEvm3aQ=
github.com/nspcc-dev/dbft v0.0.0-20200623100921-5a182c20965e h1:QOT9slflIkEKb5wY0ZUC0dCmCgoqGlhOAh9+xWMIxfg= github.com/nspcc-dev/dbft v0.0.0-20200711144034-c526ccc6f570 h1:EHBwlOyd2m06C3dnxhpPokpYqlNg7u5ZX/uPBhjYuZ4=
github.com/nspcc-dev/dbft v0.0.0-20200623100921-5a182c20965e/go.mod h1:1FYQXSbb6/9HQIkoF8XO7W/S8N7AZRkBsgwbcXRvk0E= github.com/nspcc-dev/dbft v0.0.0-20200711144034-c526ccc6f570/go.mod h1:1FYQXSbb6/9HQIkoF8XO7W/S8N7AZRkBsgwbcXRvk0E=
github.com/nspcc-dev/neo-go v0.73.1-pre.0.20200303142215-f5a1b928ce09/go.mod h1:pPYwPZ2ks+uMnlRLUyXOpLieaDQSEaf4NM3zHVbRjmg= github.com/nspcc-dev/neo-go v0.73.1-pre.0.20200303142215-f5a1b928ce09/go.mod h1:pPYwPZ2ks+uMnlRLUyXOpLieaDQSEaf4NM3zHVbRjmg=
github.com/nspcc-dev/neofs-crypto v0.2.0 h1:ftN+59WqxSWz/RCgXYOfhmltOOqU+udsNQSvN6wkFck= github.com/nspcc-dev/neofs-crypto v0.2.0 h1:ftN+59WqxSWz/RCgXYOfhmltOOqU+udsNQSvN6wkFck=
github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA= github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA=

View file

@ -8,19 +8,22 @@ import (
// changeView represents dBFT ChangeView message. // changeView represents dBFT ChangeView message.
type changeView struct { type changeView struct {
newViewNumber byte newViewNumber byte
timestamp uint32 timestamp uint64
reason payload.ChangeViewReason
} }
var _ payload.ChangeView = (*changeView)(nil) var _ payload.ChangeView = (*changeView)(nil)
// EncodeBinary implements io.Serializable interface. // EncodeBinary implements io.Serializable interface.
func (c *changeView) EncodeBinary(w *io.BinWriter) { func (c *changeView) EncodeBinary(w *io.BinWriter) {
w.WriteU32LE(c.timestamp) w.WriteU64LE(c.timestamp)
w.WriteB(byte(c.reason))
} }
// DecodeBinary implements io.Serializable interface. // DecodeBinary implements io.Serializable interface.
func (c *changeView) DecodeBinary(r *io.BinReader) { func (c *changeView) DecodeBinary(r *io.BinReader) {
c.timestamp = r.ReadU32LE() c.timestamp = r.ReadU64LE()
c.reason = payload.ChangeViewReason(r.ReadB())
} }
// NewViewNumber implements payload.ChangeView interface. // NewViewNumber implements payload.ChangeView interface.
@ -30,7 +33,13 @@ func (c changeView) NewViewNumber() byte { return c.newViewNumber }
func (c *changeView) SetNewViewNumber(view byte) { c.newViewNumber = view } func (c *changeView) SetNewViewNumber(view byte) { c.newViewNumber = view }
// Timestamp implements payload.ChangeView interface. // Timestamp implements payload.ChangeView interface.
func (c changeView) Timestamp() uint32 { return c.timestamp } func (c changeView) Timestamp() uint64 { return c.timestamp * 1000000 }
// SetTimestamp implements payload.ChangeView interface. // SetTimestamp implements payload.ChangeView interface.
func (c *changeView) SetTimestamp(ts uint32) { c.timestamp = ts } func (c *changeView) SetTimestamp(ts uint64) { c.timestamp = ts / 1000000 }
// Reason implements payload.ChangeView interface.
func (c changeView) Reason() payload.ChangeViewReason { return c.reason }
// SetReason implements payload.ChangeView interface.
func (c *changeView) SetReason(reason payload.ChangeViewReason) { c.reason = reason }

View file

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

View file

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

View file

@ -224,7 +224,7 @@ func randomMessage(t *testing.T, mt messageType) io.Serializable {
switch mt { switch mt {
case changeViewType: case changeViewType:
return &changeView{ return &changeView{
timestamp: rand.Uint32(), timestamp: rand.Uint64(),
} }
case prepareRequestType: case prepareRequestType:
return randomPrepareRequest(t) return randomPrepareRequest(t)
@ -235,7 +235,7 @@ func randomMessage(t *testing.T, mt messageType) io.Serializable {
random.Fill(c.signature[:]) random.Fill(c.signature[:])
return &c return &c
case recoveryRequestType: case recoveryRequestType:
return &recoveryRequest{timestamp: rand.Uint32()} return &recoveryRequest{timestamp: rand.Uint64()}
case recoveryMessageType: case recoveryMessageType:
return randomRecoveryMessage(t) return randomRecoveryMessage(t)
default: default:
@ -289,7 +289,7 @@ func randomRecoveryMessage(t *testing.T) *recoveryMessage {
}, },
changeViewPayloads: []*changeViewCompact{ changeViewPayloads: []*changeViewCompact{
{ {
Timestamp: rand.Uint32(), Timestamp: rand.Uint64(),
ValidatorIndex: 3, ValidatorIndex: 3,
OriginalViewNumber: 3, OriginalViewNumber: 3,
InvocationScript: random.Bytes(4), InvocationScript: random.Bytes(4),

View file

@ -21,7 +21,7 @@ type (
changeViewCompact struct { changeViewCompact struct {
ValidatorIndex uint16 ValidatorIndex uint16
OriginalViewNumber byte OriginalViewNumber byte
Timestamp uint32 Timestamp uint64
InvocationScript []byte InvocationScript []byte
} }
@ -95,7 +95,7 @@ func (m *recoveryMessage) EncodeBinary(w *io.BinWriter) {
func (p *changeViewCompact) DecodeBinary(r *io.BinReader) { func (p *changeViewCompact) DecodeBinary(r *io.BinReader) {
p.ValidatorIndex = r.ReadU16LE() p.ValidatorIndex = r.ReadU16LE()
p.OriginalViewNumber = r.ReadB() p.OriginalViewNumber = r.ReadB()
p.Timestamp = r.ReadU32LE() p.Timestamp = r.ReadU64LE()
p.InvocationScript = r.ReadVarBytes(1024) p.InvocationScript = r.ReadVarBytes(1024)
} }
@ -103,7 +103,7 @@ func (p *changeViewCompact) DecodeBinary(r *io.BinReader) {
func (p *changeViewCompact) EncodeBinary(w *io.BinWriter) { func (p *changeViewCompact) EncodeBinary(w *io.BinWriter) {
w.WriteU16LE(p.ValidatorIndex) w.WriteU16LE(p.ValidatorIndex)
w.WriteB(p.OriginalViewNumber) w.WriteB(p.OriginalViewNumber)
w.WriteU32LE(p.Timestamp) w.WriteU64LE(p.Timestamp)
w.WriteVarBytes(p.InvocationScript) w.WriteVarBytes(p.InvocationScript)
} }
@ -164,7 +164,7 @@ func (m *recoveryMessage) AddPayload(p payload.ConsensusPayload) {
m.changeViewPayloads = append(m.changeViewPayloads, &changeViewCompact{ m.changeViewPayloads = append(m.changeViewPayloads, &changeViewCompact{
ValidatorIndex: p.ValidatorIndex(), ValidatorIndex: p.ValidatorIndex(),
OriginalViewNumber: p.ViewNumber(), OriginalViewNumber: p.ViewNumber(),
Timestamp: p.GetChangeView().Timestamp(), Timestamp: p.GetChangeView().Timestamp() / 1000000,
InvocationScript: p.(*Payload).Witness.InvocationScript, InvocationScript: p.(*Payload).Witness.InvocationScript,
}) })
case payload.CommitType: case payload.CommitType:

View file

@ -7,23 +7,23 @@ import (
// recoveryRequest represents dBFT RecoveryRequest message. // recoveryRequest represents dBFT RecoveryRequest message.
type recoveryRequest struct { type recoveryRequest struct {
timestamp uint32 timestamp uint64
} }
var _ payload.RecoveryRequest = (*recoveryRequest)(nil) var _ payload.RecoveryRequest = (*recoveryRequest)(nil)
// DecodeBinary implements io.Serializable interface. // DecodeBinary implements io.Serializable interface.
func (m *recoveryRequest) DecodeBinary(r *io.BinReader) { func (m *recoveryRequest) DecodeBinary(r *io.BinReader) {
m.timestamp = r.ReadU32LE() m.timestamp = r.ReadU64LE()
} }
// EncodeBinary implements io.Serializable interface. // EncodeBinary implements io.Serializable interface.
func (m *recoveryRequest) EncodeBinary(w *io.BinWriter) { func (m *recoveryRequest) EncodeBinary(w *io.BinWriter) {
w.WriteU32LE(m.timestamp) w.WriteU64LE(m.timestamp)
} }
// Timestamp implements payload.RecoveryRequest interface. // Timestamp implements payload.RecoveryRequest interface.
func (m *recoveryRequest) Timestamp() uint32 { return m.timestamp } func (m *recoveryRequest) Timestamp() uint64 { return m.timestamp * 1000000 }
// SetTimestamp implements payload.RecoveryRequest interface. // SetTimestamp implements payload.RecoveryRequest interface.
func (m *recoveryRequest) SetTimestamp(ts uint32) { m.timestamp = ts } func (m *recoveryRequest) SetTimestamp(ts uint64) { m.timestamp = ts / 1000000 }

View file

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