2019-11-08 15:40:21 +00:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2024-03-21 19:49:39 +00:00
|
|
|
"github.com/nspcc-dev/dbft"
|
2021-03-25 18:59:54 +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/io"
|
2021-01-14 11:17:00 +00:00
|
|
|
npayload "github.com/nspcc-dev/neo-go/pkg/network/payload"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
messageType byte
|
|
|
|
|
|
|
|
message struct {
|
2021-01-14 11:17:00 +00:00
|
|
|
Type messageType
|
|
|
|
BlockIndex uint32
|
|
|
|
ValidatorIndex byte
|
|
|
|
ViewNumber byte
|
2019-11-08 15:40:21 +00:00
|
|
|
|
|
|
|
payload io.Serializable
|
2020-11-17 12:57:50 +00:00
|
|
|
// stateRootEnabled specifies if state root is exchanged during consensus.
|
|
|
|
stateRootEnabled bool
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Payload is a type for consensus-related messages.
|
|
|
|
Payload struct {
|
2021-01-14 11:17:00 +00:00
|
|
|
npayload.Extensible
|
|
|
|
message
|
2021-03-25 18:59:54 +00:00
|
|
|
network netmode.Magic
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
changeViewType messageType = 0x00
|
|
|
|
prepareRequestType messageType = 0x20
|
|
|
|
prepareResponseType messageType = 0x21
|
|
|
|
commitType messageType = 0x30
|
|
|
|
recoveryRequestType messageType = 0x40
|
|
|
|
recoveryMessageType messageType = 0x41
|
2020-07-13 15:07:02 +00:00
|
|
|
|
|
|
|
payloadGasLimit = 2000000 // 0.02 GAS
|
2019-11-08 15:40:21 +00:00
|
|
|
)
|
|
|
|
|
2024-03-21 20:38:24 +00:00
|
|
|
var _ dbft.ConsensusPayload[util.Uint256] = &Payload{}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ViewNumber implements the payload.ConsensusPayload interface.
|
2019-11-15 10:32:40 +00:00
|
|
|
func (p Payload) ViewNumber() byte {
|
|
|
|
return p.message.ViewNumber
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// SetViewNumber implements the payload.ConsensusPayload interface.
|
2019-11-15 10:32:40 +00:00
|
|
|
func (p *Payload) SetViewNumber(view byte) {
|
|
|
|
p.message.ViewNumber = view
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Type implements the payload.ConsensusPayload interface.
|
2024-03-21 19:49:39 +00:00
|
|
|
func (p Payload) Type() dbft.MessageType {
|
|
|
|
return dbft.MessageType(p.message.Type)
|
2019-11-15 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Payload implements the payload.ConsensusPayload interface.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (p Payload) Payload() any {
|
2019-11-15 10:32:40 +00:00
|
|
|
return p.payload
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetChangeView implements the payload.ConsensusPayload interface.
|
2024-03-21 19:49:39 +00:00
|
|
|
func (p Payload) GetChangeView() dbft.ChangeView { return p.payload.(dbft.ChangeView) }
|
2019-11-15 10:32:40 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetPrepareRequest implements the payload.ConsensusPayload interface.
|
2024-03-21 19:49:39 +00:00
|
|
|
func (p Payload) GetPrepareRequest() dbft.PrepareRequest[util.Uint256] {
|
|
|
|
return p.payload.(dbft.PrepareRequest[util.Uint256])
|
2019-11-15 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetPrepareResponse implements the payload.ConsensusPayload interface.
|
2024-03-21 19:49:39 +00:00
|
|
|
func (p Payload) GetPrepareResponse() dbft.PrepareResponse[util.Uint256] {
|
|
|
|
return p.payload.(dbft.PrepareResponse[util.Uint256])
|
2019-11-15 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 10:06:02 +00:00
|
|
|
// GetPreCommit implements the payload.ConsensusPayload interface.
|
|
|
|
// It's a stub since PreCommits are never used on N3.
|
|
|
|
func (p Payload) GetPreCommit() dbft.PreCommit { return nil }
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetCommit implements the payload.ConsensusPayload interface.
|
2024-03-21 19:49:39 +00:00
|
|
|
func (p Payload) GetCommit() dbft.Commit { return p.payload.(dbft.Commit) }
|
2019-11-15 10:32:40 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetRecoveryRequest implements the payload.ConsensusPayload interface.
|
2024-03-21 19:49:39 +00:00
|
|
|
func (p Payload) GetRecoveryRequest() dbft.RecoveryRequest {
|
|
|
|
return p.payload.(dbft.RecoveryRequest)
|
2019-11-15 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// GetRecoveryMessage implements the payload.ConsensusPayload interface.
|
2024-03-21 19:49:39 +00:00
|
|
|
func (p Payload) GetRecoveryMessage() dbft.RecoveryMessage[util.Uint256] {
|
|
|
|
return p.payload.(dbft.RecoveryMessage[util.Uint256])
|
2019-11-15 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ValidatorIndex implements the payload.ConsensusPayload interface.
|
2019-11-15 10:32:40 +00:00
|
|
|
func (p Payload) ValidatorIndex() uint16 {
|
2021-01-14 11:17:00 +00:00
|
|
|
return uint16(p.message.ValidatorIndex)
|
2019-11-15 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// SetValidatorIndex implements the payload.ConsensusPayload interface.
|
2019-11-15 10:32:40 +00:00
|
|
|
func (p *Payload) SetValidatorIndex(i uint16) {
|
2021-01-14 11:17:00 +00:00
|
|
|
p.message.ValidatorIndex = byte(i)
|
2019-11-15 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Height implements the payload.ConsensusPayload interface.
|
2019-11-15 10:32:40 +00:00
|
|
|
func (p Payload) Height() uint32 {
|
2021-01-14 11:17:00 +00:00
|
|
|
return p.message.BlockIndex
|
2019-11-15 10:32:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// EncodeBinary implements the io.Serializable interface.
|
2019-11-08 15:40:21 +00:00
|
|
|
func (p *Payload) EncodeBinary(w *io.BinWriter) {
|
2021-01-14 11:17:00 +00:00
|
|
|
p.encodeData()
|
|
|
|
p.Extensible.EncodeBinary(w)
|
2020-06-26 15:27:18 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-01-14 11:17:00 +00:00
|
|
|
p.encodeData()
|
2021-03-25 18:59:54 +00:00
|
|
|
sig := key.PrivateKey.SignHashable(uint32(p.network), &p.Extensible)
|
2019-11-15 10:32:40 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Hash implements the payload.ConsensusPayload interface.
|
2019-11-08 15:40:21 +00:00
|
|
|
func (p *Payload) Hash() util.Uint256 {
|
2021-01-14 11:17:00 +00:00
|
|
|
if p.Extensible.Data == nil {
|
|
|
|
p.encodeData()
|
2020-01-30 12:51:49 +00:00
|
|
|
}
|
2021-01-14 11:17:00 +00:00
|
|
|
return p.Extensible.Hash()
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// DecodeBinary implements the io.Serializable interface.
|
2019-11-08 15:40:21 +00:00
|
|
|
func (p *Payload) DecodeBinary(r *io.BinReader) {
|
2021-01-14 11:17:00 +00:00
|
|
|
p.Extensible.DecodeBinary(r)
|
2021-05-12 17:14:52 +00:00
|
|
|
if r.Err == nil {
|
|
|
|
r.Err = p.decodeData()
|
|
|
|
}
|
2019-11-08 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// EncodeBinary implements the io.Serializable interface.
|
2019-11-08 15:40:21 +00:00
|
|
|
func (m *message) EncodeBinary(w *io.BinWriter) {
|
2021-01-14 11:17:00 +00:00
|
|
|
w.WriteB(byte(m.Type))
|
|
|
|
w.WriteU32LE(m.BlockIndex)
|
|
|
|
w.WriteB(m.ValidatorIndex)
|
2019-12-12 17:17:50 +00:00
|
|
|
w.WriteB(m.ViewNumber)
|
2019-11-08 15:40:21 +00:00
|
|
|
m.payload.EncodeBinary(w)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// DecodeBinary implements the io.Serializable interface.
|
2019-11-08 15:40:21 +00:00
|
|
|
func (m *message) DecodeBinary(r *io.BinReader) {
|
2019-12-12 17:17:50 +00:00
|
|
|
m.Type = messageType(r.ReadB())
|
2021-01-14 11:17:00 +00:00
|
|
|
m.BlockIndex = r.ReadU32LE()
|
|
|
|
m.ValidatorIndex = r.ReadB()
|
2019-12-12 17:17:50 +00:00
|
|
|
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:
|
2020-11-17 12:57:50 +00:00
|
|
|
r := new(prepareRequest)
|
|
|
|
if m.stateRootEnabled {
|
|
|
|
r.stateRootEnabled = true
|
|
|
|
}
|
|
|
|
m.payload = r
|
2019-11-08 15:40:21 +00:00
|
|
|
case prepareResponseType:
|
|
|
|
m.payload = new(prepareResponse)
|
|
|
|
case commitType:
|
|
|
|
m.payload = new(commit)
|
|
|
|
case recoveryRequestType:
|
|
|
|
m.payload = new(recoveryRequest)
|
|
|
|
case recoveryMessageType:
|
2020-11-17 12:57:50 +00:00
|
|
|
r := new(recoveryMessage)
|
|
|
|
if m.stateRootEnabled {
|
|
|
|
r.stateRootEnabled = true
|
|
|
|
}
|
|
|
|
m.payload = r
|
2019-11-08 15:40:21 +00:00
|
|
|
default:
|
2020-08-06 14:44:08 +00:00
|
|
|
r.Err = fmt.Errorf("invalid type: 0x%02x", byte(m.Type))
|
2019-11-08 15:40:21 +00:00
|
|
|
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
|
|
|
|
2021-01-14 11:17:00 +00:00
|
|
|
func (p *Payload) encodeData() {
|
|
|
|
if p.Extensible.Data == nil {
|
|
|
|
p.Extensible.ValidBlockStart = 0
|
|
|
|
p.Extensible.ValidBlockEnd = p.BlockIndex
|
|
|
|
bw := io.NewBufBinWriter()
|
|
|
|
p.message.EncodeBinary(bw.BinWriter)
|
|
|
|
p.Extensible.Data = bw.Bytes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 20:17:03 +00:00
|
|
|
// decode data of payload into its message.
|
2020-04-15 15:56:45 +00:00
|
|
|
func (p *Payload) decodeData() error {
|
2021-01-14 11:17:00 +00:00
|
|
|
br := io.NewBinReaderFromBuf(p.Extensible.Data)
|
|
|
|
p.message.DecodeBinary(br)
|
2020-04-15 15:56:45 +00:00
|
|
|
if br.Err != nil {
|
2020-08-06 14:44:08 +00:00
|
|
|
return fmt.Errorf("can't decode message: %w", br.Err)
|
2020-04-15 15:56:45 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|