2019-11-08 15:40:21 +00:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/io"
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
2019-12-02 15:32:14 +00:00
|
|
|
"github.com/nspcc-dev/dbft/crypto"
|
2019-11-15 10:32:40 +00:00
|
|
|
"github.com/nspcc-dev/dbft/payload"
|
2019-11-08 15:40:21 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// recoveryMessage represents dBFT Recovery message.
|
|
|
|
recoveryMessage struct {
|
2019-11-15 10:32:40 +00:00
|
|
|
preparationHash *util.Uint256
|
|
|
|
preparationPayloads []*preparationCompact
|
|
|
|
commitPayloads []*commitCompact
|
|
|
|
changeViewPayloads []*changeViewCompact
|
|
|
|
prepareRequest *prepareRequest
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
changeViewCompact struct {
|
|
|
|
ValidatorIndex uint16
|
|
|
|
OriginalViewNumber byte
|
|
|
|
Timestamp uint32
|
|
|
|
InvocationScript []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
commitCompact struct {
|
|
|
|
ViewNumber byte
|
|
|
|
ValidatorIndex uint16
|
2019-11-15 10:32:40 +00:00
|
|
|
Signature [signatureSize]byte
|
2019-11-08 15:40:21 +00:00
|
|
|
InvocationScript []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
preparationCompact struct {
|
|
|
|
ValidatorIndex uint16
|
|
|
|
InvocationScript []byte
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-11-15 10:32:40 +00:00
|
|
|
var _ payload.RecoveryMessage = (*recoveryMessage)(nil)
|
2019-11-08 15:40:21 +00:00
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable interface.
|
|
|
|
func (m *recoveryMessage) DecodeBinary(r *io.BinReader) {
|
2019-11-15 10:32:40 +00:00
|
|
|
r.ReadArray(&m.changeViewPayloads)
|
2019-11-08 15:40:21 +00:00
|
|
|
|
2019-12-12 15:52:23 +00:00
|
|
|
var hasReq = r.ReadBool()
|
2019-11-08 15:40:21 +00:00
|
|
|
if hasReq {
|
2019-11-15 10:32:40 +00:00
|
|
|
m.prepareRequest = new(prepareRequest)
|
|
|
|
m.prepareRequest.DecodeBinary(r)
|
2019-11-08 15:40:21 +00:00
|
|
|
} else {
|
|
|
|
l := r.ReadVarUint()
|
|
|
|
if l != 0 {
|
2019-11-15 10:32:40 +00:00
|
|
|
if l == util.Uint256Size {
|
|
|
|
m.preparationHash = new(util.Uint256)
|
2019-12-06 15:37:46 +00:00
|
|
|
r.ReadBytes(m.preparationHash[:])
|
2019-11-08 15:40:21 +00:00
|
|
|
} else {
|
|
|
|
r.Err = errors.New("invalid data")
|
|
|
|
}
|
|
|
|
} else {
|
2019-11-15 10:32:40 +00:00
|
|
|
m.preparationHash = nil
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:32:40 +00:00
|
|
|
r.ReadArray(&m.preparationPayloads)
|
|
|
|
r.ReadArray(&m.commitPayloads)
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements io.Serializable interface.
|
|
|
|
func (m *recoveryMessage) EncodeBinary(w *io.BinWriter) {
|
2019-11-15 10:32:40 +00:00
|
|
|
w.WriteArray(m.changeViewPayloads)
|
2019-11-08 15:40:21 +00:00
|
|
|
|
2019-11-15 10:32:40 +00:00
|
|
|
hasReq := m.prepareRequest != nil
|
2019-12-12 15:52:23 +00:00
|
|
|
w.WriteBool(hasReq)
|
2019-11-08 15:40:21 +00:00
|
|
|
if hasReq {
|
2019-11-15 10:32:40 +00:00
|
|
|
m.prepareRequest.EncodeBinary(w)
|
2019-11-08 15:40:21 +00:00
|
|
|
} else {
|
2019-11-15 10:32:40 +00:00
|
|
|
if m.preparationHash == nil {
|
2019-11-08 15:40:21 +00:00
|
|
|
w.WriteVarUint(0)
|
|
|
|
} else {
|
2019-11-15 10:32:40 +00:00
|
|
|
w.WriteVarUint(util.Uint256Size)
|
2019-12-06 15:22:21 +00:00
|
|
|
w.WriteBytes(m.preparationHash[:])
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-15 10:32:40 +00:00
|
|
|
|
|
|
|
w.WriteArray(m.preparationPayloads)
|
|
|
|
w.WriteArray(m.commitPayloads)
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable interface.
|
|
|
|
func (p *changeViewCompact) DecodeBinary(r *io.BinReader) {
|
2019-12-12 15:52:23 +00:00
|
|
|
p.ValidatorIndex = r.ReadU16LE()
|
2019-12-12 17:17:50 +00:00
|
|
|
p.OriginalViewNumber = r.ReadB()
|
2019-12-12 15:52:23 +00:00
|
|
|
p.Timestamp = r.ReadU32LE()
|
2019-12-06 15:37:37 +00:00
|
|
|
p.InvocationScript = r.ReadVarBytes()
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements io.Serializable interface.
|
|
|
|
func (p *changeViewCompact) EncodeBinary(w *io.BinWriter) {
|
2019-12-12 15:52:23 +00:00
|
|
|
w.WriteU16LE(p.ValidatorIndex)
|
2019-12-12 17:17:50 +00:00
|
|
|
w.WriteB(p.OriginalViewNumber)
|
2019-12-12 15:52:23 +00:00
|
|
|
w.WriteU32LE(p.Timestamp)
|
2019-11-22 10:34:06 +00:00
|
|
|
w.WriteVarBytes(p.InvocationScript)
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable interface.
|
|
|
|
func (p *commitCompact) DecodeBinary(r *io.BinReader) {
|
2019-12-12 17:17:50 +00:00
|
|
|
p.ViewNumber = r.ReadB()
|
2019-12-12 15:52:23 +00:00
|
|
|
p.ValidatorIndex = r.ReadU16LE()
|
2019-12-06 15:37:46 +00:00
|
|
|
r.ReadBytes(p.Signature[:])
|
2019-12-06 15:37:37 +00:00
|
|
|
p.InvocationScript = r.ReadVarBytes()
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements io.Serializable interface.
|
|
|
|
func (p *commitCompact) EncodeBinary(w *io.BinWriter) {
|
2019-12-12 17:17:50 +00:00
|
|
|
w.WriteB(p.ViewNumber)
|
2019-12-12 15:52:23 +00:00
|
|
|
w.WriteU16LE(p.ValidatorIndex)
|
2019-12-06 15:22:21 +00:00
|
|
|
w.WriteBytes(p.Signature[:])
|
2019-11-22 10:34:06 +00:00
|
|
|
w.WriteVarBytes(p.InvocationScript)
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable interface.
|
|
|
|
func (p *preparationCompact) DecodeBinary(r *io.BinReader) {
|
2019-12-12 15:52:23 +00:00
|
|
|
p.ValidatorIndex = r.ReadU16LE()
|
2019-12-06 15:37:37 +00:00
|
|
|
p.InvocationScript = r.ReadVarBytes()
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements io.Serializable interface.
|
|
|
|
func (p *preparationCompact) EncodeBinary(w *io.BinWriter) {
|
2019-12-12 15:52:23 +00:00
|
|
|
w.WriteU16LE(p.ValidatorIndex)
|
2019-11-22 10:34:06 +00:00
|
|
|
w.WriteVarBytes(p.InvocationScript)
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
2019-11-15 10:32:40 +00:00
|
|
|
|
|
|
|
// AddPayload implements payload.RecoveryMessage interface.
|
|
|
|
func (m *recoveryMessage) AddPayload(p payload.ConsensusPayload) {
|
|
|
|
switch p.Type() {
|
|
|
|
case payload.PrepareRequestType:
|
|
|
|
m.prepareRequest = p.GetPrepareRequest().(*prepareRequest)
|
2019-12-02 13:18:54 +00:00
|
|
|
h := p.Hash()
|
|
|
|
m.preparationHash = &h
|
2019-12-02 15:32:14 +00:00
|
|
|
m.preparationPayloads = append(m.preparationPayloads, &preparationCompact{
|
|
|
|
ValidatorIndex: p.ValidatorIndex(),
|
|
|
|
InvocationScript: p.(*Payload).Witness.InvocationScript,
|
|
|
|
})
|
2019-11-15 10:32:40 +00:00
|
|
|
case payload.PrepareResponseType:
|
|
|
|
m.preparationPayloads = append(m.preparationPayloads, &preparationCompact{
|
|
|
|
ValidatorIndex: p.ValidatorIndex(),
|
|
|
|
InvocationScript: p.(*Payload).Witness.InvocationScript,
|
|
|
|
})
|
2019-12-02 13:18:54 +00:00
|
|
|
|
|
|
|
if m.preparationHash == nil {
|
|
|
|
h := p.GetPrepareResponse().PreparationHash()
|
|
|
|
m.preparationHash = &h
|
|
|
|
}
|
2019-11-15 10:32:40 +00:00
|
|
|
case payload.ChangeViewType:
|
|
|
|
m.changeViewPayloads = append(m.changeViewPayloads, &changeViewCompact{
|
|
|
|
ValidatorIndex: p.ValidatorIndex(),
|
|
|
|
OriginalViewNumber: p.ViewNumber(),
|
|
|
|
Timestamp: p.GetChangeView().Timestamp(),
|
|
|
|
InvocationScript: p.(*Payload).Witness.InvocationScript,
|
|
|
|
})
|
|
|
|
case payload.CommitType:
|
|
|
|
m.commitPayloads = append(m.commitPayloads, &commitCompact{
|
|
|
|
ValidatorIndex: p.ValidatorIndex(),
|
|
|
|
ViewNumber: p.ViewNumber(),
|
|
|
|
Signature: p.GetCommit().(*commit).signature,
|
|
|
|
InvocationScript: p.(*Payload).Witness.InvocationScript,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPrepareRequest implements payload.RecoveryMessage interface.
|
2019-12-02 15:32:14 +00:00
|
|
|
func (m *recoveryMessage) GetPrepareRequest(p payload.ConsensusPayload, validators []crypto.PublicKey, primary uint16) payload.ConsensusPayload {
|
2019-11-15 10:32:40 +00:00
|
|
|
if m.prepareRequest == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:32:14 +00:00
|
|
|
var compact *preparationCompact
|
|
|
|
for _, p := range m.preparationPayloads {
|
|
|
|
if p != nil && p.ValidatorIndex == primary {
|
|
|
|
compact = p
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if compact == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
req := fromPayload(prepareRequestType, p.(*Payload), m.prepareRequest)
|
|
|
|
req.SetValidatorIndex(primary)
|
|
|
|
req.Witness.InvocationScript = compact.InvocationScript
|
|
|
|
req.Witness.VerificationScript = getVerificationScript(primary, validators)
|
|
|
|
|
|
|
|
return req
|
2019-11-15 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPrepareResponses implements payload.RecoveryMessage interface.
|
2019-12-02 15:32:14 +00:00
|
|
|
func (m *recoveryMessage) GetPrepareResponses(p payload.ConsensusPayload, validators []crypto.PublicKey) []payload.ConsensusPayload {
|
2019-11-15 10:32:40 +00:00
|
|
|
if m.preparationHash == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ps := make([]payload.ConsensusPayload, len(m.preparationPayloads))
|
|
|
|
|
|
|
|
for i, resp := range m.preparationPayloads {
|
2019-12-02 15:32:14 +00:00
|
|
|
r := fromPayload(prepareResponseType, p.(*Payload), &prepareResponse{
|
2019-11-15 10:32:40 +00:00
|
|
|
preparationHash: *m.preparationHash,
|
|
|
|
})
|
2019-12-02 15:32:14 +00:00
|
|
|
r.SetValidatorIndex(resp.ValidatorIndex)
|
|
|
|
r.Witness.InvocationScript = resp.InvocationScript
|
|
|
|
r.Witness.VerificationScript = getVerificationScript(resp.ValidatorIndex, validators)
|
|
|
|
|
|
|
|
ps[i] = r
|
2019-11-15 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ps
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetChangeViews implements payload.RecoveryMessage interface.
|
2019-12-02 15:32:14 +00:00
|
|
|
func (m *recoveryMessage) GetChangeViews(p payload.ConsensusPayload, validators []crypto.PublicKey) []payload.ConsensusPayload {
|
2019-11-15 10:32:40 +00:00
|
|
|
ps := make([]payload.ConsensusPayload, len(m.changeViewPayloads))
|
|
|
|
|
|
|
|
for i, cv := range m.changeViewPayloads {
|
2019-12-02 15:32:14 +00:00
|
|
|
c := fromPayload(changeViewType, p.(*Payload), &changeView{
|
2019-11-15 10:32:40 +00:00
|
|
|
newViewNumber: cv.OriginalViewNumber + 1,
|
|
|
|
timestamp: cv.Timestamp,
|
|
|
|
})
|
2019-12-02 15:32:14 +00:00
|
|
|
c.SetValidatorIndex(cv.ValidatorIndex)
|
|
|
|
c.Witness.InvocationScript = cv.InvocationScript
|
|
|
|
c.Witness.VerificationScript = getVerificationScript(cv.ValidatorIndex, validators)
|
|
|
|
|
|
|
|
ps[i] = c
|
2019-11-15 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ps
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCommits implements payload.RecoveryMessage interface.
|
2019-12-02 15:32:14 +00:00
|
|
|
func (m *recoveryMessage) GetCommits(p payload.ConsensusPayload, validators []crypto.PublicKey) []payload.ConsensusPayload {
|
2019-11-15 10:32:40 +00:00
|
|
|
ps := make([]payload.ConsensusPayload, len(m.commitPayloads))
|
|
|
|
|
|
|
|
for i, c := range m.commitPayloads {
|
2019-12-02 15:32:14 +00:00
|
|
|
cc := fromPayload(commitType, p.(*Payload), &commit{signature: c.Signature})
|
|
|
|
cc.SetValidatorIndex(c.ValidatorIndex)
|
|
|
|
cc.Witness.InvocationScript = c.InvocationScript
|
|
|
|
cc.Witness.VerificationScript = getVerificationScript(c.ValidatorIndex, validators)
|
|
|
|
|
|
|
|
ps[i] = cc
|
2019-11-15 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ps
|
|
|
|
}
|
|
|
|
|
|
|
|
// PreparationHash implements payload.RecoveryMessage interface.
|
|
|
|
func (m *recoveryMessage) PreparationHash() *util.Uint256 {
|
|
|
|
return m.preparationHash
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPreparationHash implements payload.RecoveryMessage interface.
|
|
|
|
func (m *recoveryMessage) SetPreparationHash(h *util.Uint256) {
|
|
|
|
m.preparationHash = h
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:32:14 +00:00
|
|
|
func getVerificationScript(i uint16, validators []crypto.PublicKey) []byte {
|
|
|
|
if int(i) >= len(validators) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pub, ok := validators[i].(*publicKey)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return pub.GetVerificationScript()
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:32:40 +00:00
|
|
|
func fromPayload(t messageType, recovery *Payload, p io.Serializable) *Payload {
|
|
|
|
return &Payload{
|
|
|
|
message: message{
|
|
|
|
Type: t,
|
|
|
|
ViewNumber: recovery.message.ViewNumber,
|
|
|
|
payload: p,
|
|
|
|
},
|
|
|
|
version: recovery.Version(),
|
|
|
|
prevHash: recovery.PrevHash(),
|
|
|
|
height: recovery.Height(),
|
|
|
|
}
|
|
|
|
}
|