2019-11-08 15:40:21 +00:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-11-15 10:32:40 +00:00
|
|
|
"github.com/nspcc-dev/dbft/payload"
|
2020-06-26 15:27:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core"
|
2020-04-14 14:24:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/crypto"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-04-21 13:45:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2019-11-08 15:40:21 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
messageType byte
|
|
|
|
|
|
|
|
message struct {
|
|
|
|
Type messageType
|
|
|
|
ViewNumber byte
|
|
|
|
|
|
|
|
payload io.Serializable
|
|
|
|
}
|
|
|
|
|
|
|
|
// Payload is a type for consensus-related messages.
|
|
|
|
Payload struct {
|
2020-04-15 15:56:45 +00:00
|
|
|
*message
|
2019-11-08 15:40:21 +00:00
|
|
|
|
2020-06-26 15:27:18 +00:00
|
|
|
network netmode.Magic
|
2020-04-15 15:56:45 +00:00
|
|
|
data []byte
|
2019-11-15 10:32:40 +00:00
|
|
|
version uint32
|
|
|
|
validatorIndex uint16
|
|
|
|
prevHash util.Uint256
|
|
|
|
height uint32
|
2019-11-08 15:40:21 +00:00
|
|
|
|
|
|
|
Witness transaction.Witness
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
changeViewType messageType = 0x00
|
|
|
|
prepareRequestType messageType = 0x20
|
|
|
|
prepareResponseType messageType = 0x21
|
|
|
|
commitType messageType = 0x30
|
|
|
|
recoveryRequestType messageType = 0x40
|
|
|
|
recoveryMessageType messageType = 0x41
|
|
|
|
)
|
|
|
|
|
2019-11-15 10:32:40 +00:00
|
|
|
// ViewNumber implements payload.ConsensusPayload interface.
|
|
|
|
func (p Payload) ViewNumber() byte {
|
|
|
|
return p.message.ViewNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetViewNumber implements payload.ConsensusPayload interface.
|
|
|
|
func (p *Payload) SetViewNumber(view byte) {
|
|
|
|
p.message.ViewNumber = view
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type implements payload.ConsensusPayload interface.
|
|
|
|
func (p Payload) Type() payload.MessageType {
|
|
|
|
return payload.MessageType(p.message.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetType implements payload.ConsensusPayload interface.
|
|
|
|
func (p *Payload) SetType(t payload.MessageType) {
|
|
|
|
p.message.Type = messageType(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Payload implements payload.ConsensusPayload interface.
|
|
|
|
func (p Payload) Payload() interface{} {
|
|
|
|
return p.payload
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPayload implements payload.ConsensusPayload interface.
|
|
|
|
func (p *Payload) SetPayload(pl interface{}) {
|
|
|
|
p.payload = pl.(io.Serializable)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetChangeView implements payload.ConsensusPayload interface.
|
|
|
|
func (p Payload) GetChangeView() payload.ChangeView { return p.payload.(payload.ChangeView) }
|
|
|
|
|
|
|
|
// GetPrepareRequest implements payload.ConsensusPayload interface.
|
|
|
|
func (p Payload) GetPrepareRequest() payload.PrepareRequest {
|
|
|
|
return p.payload.(payload.PrepareRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPrepareResponse implements payload.ConsensusPayload interface.
|
|
|
|
func (p Payload) GetPrepareResponse() payload.PrepareResponse {
|
|
|
|
return p.payload.(payload.PrepareResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCommit implements payload.ConsensusPayload interface.
|
|
|
|
func (p Payload) GetCommit() payload.Commit { return p.payload.(payload.Commit) }
|
|
|
|
|
|
|
|
// GetRecoveryRequest implements payload.ConsensusPayload interface.
|
|
|
|
func (p Payload) GetRecoveryRequest() payload.RecoveryRequest {
|
|
|
|
return p.payload.(payload.RecoveryRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRecoveryMessage implements payload.ConsensusPayload interface.
|
|
|
|
func (p Payload) GetRecoveryMessage() payload.RecoveryMessage {
|
|
|
|
return p.payload.(payload.RecoveryMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalUnsigned implements payload.ConsensusPayload interface.
|
|
|
|
func (p Payload) MarshalUnsigned() []byte {
|
|
|
|
w := io.NewBufBinWriter()
|
2020-06-26 15:27:18 +00:00
|
|
|
p.encodeHashData(w.BinWriter)
|
2019-11-15 10:32:40 +00:00
|
|
|
|
|
|
|
return w.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalUnsigned implements payload.ConsensusPayload interface.
|
|
|
|
func (p *Payload) UnmarshalUnsigned(data []byte) error {
|
|
|
|
r := io.NewBinReaderFromBuf(data)
|
2020-06-26 15:27:18 +00:00
|
|
|
p.network = netmode.Magic(r.ReadU32LE())
|
2019-11-15 10:32:40 +00:00
|
|
|
p.DecodeBinaryUnsigned(r)
|
|
|
|
|
|
|
|
return r.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version implements payload.ConsensusPayload interface.
|
|
|
|
func (p Payload) Version() uint32 {
|
|
|
|
return p.version
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetVersion implements payload.ConsensusPayload interface.
|
|
|
|
func (p *Payload) SetVersion(v uint32) {
|
|
|
|
p.version = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidatorIndex implements payload.ConsensusPayload interface.
|
|
|
|
func (p Payload) ValidatorIndex() uint16 {
|
|
|
|
return p.validatorIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetValidatorIndex implements payload.ConsensusPayload interface.
|
|
|
|
func (p *Payload) SetValidatorIndex(i uint16) {
|
|
|
|
p.validatorIndex = i
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrevHash implements payload.ConsensusPayload interface.
|
|
|
|
func (p Payload) PrevHash() util.Uint256 {
|
|
|
|
return p.prevHash
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPrevHash implements payload.ConsensusPayload interface.
|
|
|
|
func (p *Payload) SetPrevHash(h util.Uint256) {
|
|
|
|
p.prevHash = h
|
|
|
|
}
|
|
|
|
|
|
|
|
// Height implements payload.ConsensusPayload interface.
|
|
|
|
func (p Payload) Height() uint32 {
|
|
|
|
return p.height
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetHeight implements payload.ConsensusPayload interface.
|
|
|
|
func (p *Payload) SetHeight(h uint32) {
|
|
|
|
p.height = h
|
|
|
|
}
|
|
|
|
|
2019-11-08 15:40:21 +00:00
|
|
|
// EncodeBinaryUnsigned writes payload to w excluding signature.
|
2019-12-09 15:25:15 +00:00
|
|
|
func (p *Payload) EncodeBinaryUnsigned(w *io.BinWriter) {
|
2019-12-12 15:52:23 +00:00
|
|
|
w.WriteU32LE(p.version)
|
2019-12-06 15:22:21 +00:00
|
|
|
w.WriteBytes(p.prevHash[:])
|
2019-12-12 15:52:23 +00:00
|
|
|
w.WriteU32LE(p.height)
|
|
|
|
w.WriteU16LE(p.validatorIndex)
|
2019-11-08 15:40:21 +00:00
|
|
|
|
2020-04-15 15:56:45 +00:00
|
|
|
if p.message != nil {
|
|
|
|
ww := io.NewBufBinWriter()
|
|
|
|
p.message.EncodeBinary(ww.BinWriter)
|
|
|
|
p.data = ww.Bytes()
|
|
|
|
}
|
|
|
|
w.WriteVarBytes(p.data)
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements io.Serializable interface.
|
|
|
|
func (p *Payload) EncodeBinary(w *io.BinWriter) {
|
|
|
|
p.EncodeBinaryUnsigned(w)
|
|
|
|
|
2019-12-12 17:17:50 +00:00
|
|
|
w.WriteB(1)
|
2019-11-08 15:40:21 +00:00
|
|
|
p.Witness.EncodeBinary(w)
|
|
|
|
}
|
|
|
|
|
2020-06-26 15:27:18 +00:00
|
|
|
func (p *Payload) encodeHashData(w *io.BinWriter) {
|
|
|
|
w.WriteU32LE(uint32(p.network))
|
|
|
|
p.EncodeBinaryUnsigned(w)
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:32:40 +00:00
|
|
|
// Sign signs payload using the private key.
|
|
|
|
// It also sets corresponding verification and invocation scripts.
|
|
|
|
func (p *Payload) Sign(key *privateKey) error {
|
2020-04-14 14:45:02 +00:00
|
|
|
sig, err := key.Sign(p.GetSignedPart())
|
2019-11-15 10:32:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-21 13:45:48 +00:00
|
|
|
buf := io.NewBufBinWriter()
|
|
|
|
emit.Bytes(buf.BinWriter, sig)
|
|
|
|
p.Witness.InvocationScript = buf.Bytes()
|
2019-12-03 14:25:25 +00:00
|
|
|
p.Witness.VerificationScript = key.PublicKey().GetVerificationScript()
|
2019-11-15 10:32:40 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:45:02 +00:00
|
|
|
// GetSignedPart implements crypto.Verifiable interface.
|
|
|
|
func (p *Payload) GetSignedPart() []byte {
|
|
|
|
return p.MarshalUnsigned()
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:32:40 +00:00
|
|
|
// Verify verifies payload using provided Witness.
|
2019-12-16 08:57:49 +00:00
|
|
|
func (p *Payload) Verify(scriptHash util.Uint160) bool {
|
|
|
|
verification, err := core.ScriptFromWitness(scriptHash, &p.Witness)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:32:40 +00:00
|
|
|
v := vm.New()
|
2020-04-14 14:24:21 +00:00
|
|
|
v.RegisterInteropGetter(crypto.GetInterop(&interop.Context{Container: p}))
|
2019-12-16 08:57:49 +00:00
|
|
|
v.LoadScript(verification)
|
|
|
|
v.LoadScript(p.Witness.InvocationScript)
|
|
|
|
|
|
|
|
err = v.Run()
|
|
|
|
if err != nil || v.HasFailed() || v.Estack().Len() != 1 {
|
2019-11-15 10:32:40 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-04-28 08:23:58 +00:00
|
|
|
return v.Estack().Pop().Bool()
|
2019-11-15 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 15:40:21 +00:00
|
|
|
// DecodeBinaryUnsigned reads payload from w excluding signature.
|
|
|
|
func (p *Payload) DecodeBinaryUnsigned(r *io.BinReader) {
|
2019-12-12 15:52:23 +00:00
|
|
|
p.version = r.ReadU32LE()
|
2019-12-06 15:37:46 +00:00
|
|
|
r.ReadBytes(p.prevHash[:])
|
2019-12-12 15:52:23 +00:00
|
|
|
p.height = r.ReadU32LE()
|
|
|
|
p.validatorIndex = r.ReadU16LE()
|
2019-11-08 15:40:21 +00:00
|
|
|
|
2020-04-15 15:56:45 +00:00
|
|
|
p.data = r.ReadVarBytes()
|
2019-11-15 10:32:40 +00:00
|
|
|
if r.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 10:32:40 +00:00
|
|
|
// Hash implements payload.ConsensusPayload interface.
|
2019-11-08 15:40:21 +00:00
|
|
|
func (p *Payload) Hash() util.Uint256 {
|
|
|
|
w := io.NewBufBinWriter()
|
2020-06-26 15:27:18 +00:00
|
|
|
p.encodeHashData(w.BinWriter)
|
2020-01-30 12:51:49 +00:00
|
|
|
if w.Err != nil {
|
|
|
|
panic("failed to hash payload")
|
|
|
|
}
|
2019-11-08 15:40:21 +00:00
|
|
|
|
|
|
|
return hash.DoubleSha256(w.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable interface.
|
|
|
|
func (p *Payload) DecodeBinary(r *io.BinReader) {
|
|
|
|
p.DecodeBinaryUnsigned(r)
|
2019-11-15 10:32:40 +00:00
|
|
|
if r.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-11-08 15:40:21 +00:00
|
|
|
|
2019-12-12 17:17:50 +00:00
|
|
|
var b = r.ReadB()
|
2019-11-08 15:40:21 +00:00
|
|
|
if b != 1 {
|
|
|
|
r.Err = errors.New("invalid format")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Witness.DecodeBinary(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements io.Serializable interface.
|
|
|
|
func (m *message) EncodeBinary(w *io.BinWriter) {
|
2019-12-06 15:22:21 +00:00
|
|
|
w.WriteBytes([]byte{byte(m.Type)})
|
2019-12-12 17:17:50 +00:00
|
|
|
w.WriteB(m.ViewNumber)
|
2019-11-08 15:40:21 +00:00
|
|
|
m.payload.EncodeBinary(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable interface.
|
|
|
|
func (m *message) DecodeBinary(r *io.BinReader) {
|
2019-12-12 17:17:50 +00:00
|
|
|
m.Type = messageType(r.ReadB())
|
|
|
|
m.ViewNumber = r.ReadB()
|
2019-11-08 15:40:21 +00:00
|
|
|
|
|
|
|
switch m.Type {
|
|
|
|
case changeViewType:
|
|
|
|
cv := new(changeView)
|
2019-11-15 10:32:40 +00:00
|
|
|
// newViewNumber is not marshaled
|
|
|
|
cv.newViewNumber = m.ViewNumber + 1
|
2019-11-08 15:40:21 +00:00
|
|
|
m.payload = cv
|
|
|
|
case prepareRequestType:
|
|
|
|
m.payload = new(prepareRequest)
|
|
|
|
case prepareResponseType:
|
|
|
|
m.payload = new(prepareResponse)
|
|
|
|
case commitType:
|
|
|
|
m.payload = new(commit)
|
|
|
|
case recoveryRequestType:
|
|
|
|
m.payload = new(recoveryRequest)
|
|
|
|
case recoveryMessageType:
|
|
|
|
m.payload = new(recoveryMessage)
|
|
|
|
default:
|
|
|
|
r.Err = errors.Errorf("invalid type: 0x%02x", byte(m.Type))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.payload.DecodeBinary(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements fmt.Stringer interface.
|
|
|
|
func (t messageType) String() string {
|
|
|
|
switch t {
|
|
|
|
case changeViewType:
|
|
|
|
return "ChangeView"
|
|
|
|
case prepareRequestType:
|
|
|
|
return "PrepareRequest"
|
|
|
|
case prepareResponseType:
|
|
|
|
return "PrepareResponse"
|
|
|
|
case commitType:
|
|
|
|
return "Commit"
|
|
|
|
case recoveryRequestType:
|
|
|
|
return "RecoveryRequest"
|
|
|
|
case recoveryMessageType:
|
|
|
|
return "RecoveryMessage"
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("UNKNOWN(0x%02x)", byte(t))
|
|
|
|
}
|
|
|
|
}
|
2020-04-15 15:56:45 +00:00
|
|
|
|
|
|
|
// decode data of payload into it's message
|
|
|
|
func (p *Payload) decodeData() error {
|
|
|
|
m := new(message)
|
|
|
|
br := io.NewBinReaderFromBuf(p.data)
|
|
|
|
m.DecodeBinary(br)
|
|
|
|
if br.Err != nil {
|
|
|
|
return errors.Wrap(br.Err, "cannot decode data into message")
|
|
|
|
}
|
|
|
|
p.message = m
|
|
|
|
return nil
|
|
|
|
}
|