neoneo-go/pkg/consensus/change_view.go

46 lines
1.5 KiB
Go
Raw Normal View History

2019-11-08 15:40:21 +00:00
package consensus
2019-11-15 10:32:40 +00:00
import (
"github.com/nspcc-dev/dbft/payload"
"github.com/nspcc-dev/neo-go/pkg/io"
2019-11-15 10:32:40 +00:00
)
2019-11-08 15:40:21 +00:00
// changeView represents dBFT ChangeView message.
type changeView struct {
2019-11-15 10:32:40 +00:00
newViewNumber byte
timestamp uint64
reason payload.ChangeViewReason
2019-11-08 15:40:21 +00:00
}
2019-11-15 10:32:40 +00:00
var _ payload.ChangeView = (*changeView)(nil)
// EncodeBinary implements the io.Serializable interface.
2019-11-08 15:40:21 +00:00
func (c *changeView) EncodeBinary(w *io.BinWriter) {
w.WriteU64LE(c.timestamp)
w.WriteB(byte(c.reason))
2019-11-08 15:40:21 +00:00
}
// DecodeBinary implements the io.Serializable interface.
2019-11-08 15:40:21 +00:00
func (c *changeView) DecodeBinary(r *io.BinReader) {
c.timestamp = r.ReadU64LE()
c.reason = payload.ChangeViewReason(r.ReadB())
2019-11-08 15:40:21 +00:00
}
2019-11-15 10:32:40 +00:00
// NewViewNumber implements the payload.ChangeView interface.
2019-11-15 10:32:40 +00:00
func (c changeView) NewViewNumber() byte { return c.newViewNumber }
// SetNewViewNumber implements the payload.ChangeView interface.
2019-11-15 10:32:40 +00:00
func (c *changeView) SetNewViewNumber(view byte) { c.newViewNumber = view }
// Timestamp implements the payload.ChangeView interface.
func (c changeView) Timestamp() uint64 { return c.timestamp * nsInMs }
2019-11-15 10:32:40 +00:00
// SetTimestamp implements the payload.ChangeView interface.
func (c *changeView) SetTimestamp(ts uint64) { c.timestamp = ts / nsInMs }
// Reason implements the payload.ChangeView interface.
func (c changeView) Reason() payload.ChangeViewReason { return c.reason }
// SetReason implements the payload.ChangeView interface.
func (c *changeView) SetReason(reason payload.ChangeViewReason) { c.reason = reason }