github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/services/stateroot/message.go (about)

     1  package stateroot
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/nspcc-dev/neo-go/pkg/core/state"
     7  	"github.com/nspcc-dev/neo-go/pkg/io"
     8  )
     9  
    10  type (
    11  	// MessageType represents message type.
    12  	MessageType byte
    13  
    14  	// Message represents a state-root related message.
    15  	Message struct {
    16  		Type    MessageType
    17  		Payload io.Serializable
    18  	}
    19  )
    20  
    21  // Various message types.
    22  const (
    23  	VoteT MessageType = 0
    24  	RootT MessageType = 1
    25  )
    26  
    27  // NewMessage creates a new message of the specified type.
    28  func NewMessage(typ MessageType, p io.Serializable) *Message {
    29  	return &Message{
    30  		Type:    typ,
    31  		Payload: p,
    32  	}
    33  }
    34  
    35  // EncodeBinary implements the io.Serializable interface.
    36  func (m *Message) EncodeBinary(w *io.BinWriter) {
    37  	w.WriteB(byte(m.Type))
    38  	m.Payload.EncodeBinary(w)
    39  }
    40  
    41  // DecodeBinary implements the io.Serializable interface.
    42  func (m *Message) DecodeBinary(r *io.BinReader) {
    43  	switch m.Type = MessageType(r.ReadB()); m.Type {
    44  	case VoteT:
    45  		m.Payload = new(Vote)
    46  	case RootT:
    47  		m.Payload = new(state.MPTRoot)
    48  	default:
    49  		r.Err = fmt.Errorf("invalid type: %x", m.Type)
    50  		return
    51  	}
    52  	m.Payload.DecodeBinary(r)
    53  }