refactor: merged signed back into protocol
This commit is contained in:
parent
b49dd976b5
commit
ff134ece14
|
@ -4,7 +4,6 @@ import (
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/config"
|
"git.lumeweb.com/LumeWeb/libs5-go/config"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
|
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol/signed"
|
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/service"
|
"git.lumeweb.com/LumeWeb/libs5-go/service"
|
||||||
_default "git.lumeweb.com/LumeWeb/libs5-go/service/default"
|
_default "git.lumeweb.com/LumeWeb/libs5-go/service/default"
|
||||||
bolt "go.etcd.io/bbolt"
|
bolt "go.etcd.io/bbolt"
|
||||||
|
@ -51,7 +50,7 @@ func (n *Node) Db() *bolt.DB {
|
||||||
|
|
||||||
func (n *Node) Start() error {
|
func (n *Node) Start() error {
|
||||||
protocol.RegisterProtocols()
|
protocol.RegisterProtocols()
|
||||||
signed.RegisterSignedProtocols()
|
protocol.RegisterSignedProtocols()
|
||||||
|
|
||||||
return n.services.Start()
|
return n.services.Start()
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol/signed"
|
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||||
"github.com/vmihailenco/msgpack/v5"
|
"github.com/vmihailenco/msgpack/v5"
|
||||||
)
|
)
|
||||||
|
@ -98,7 +97,7 @@ func (h *HandshakeOpen) HandleMessage(message IncomingMessageData) error {
|
||||||
return fmt.Errorf("Peer is in different network: %s", h.networkId)
|
return fmt.Errorf("Peer is in different network: %s", h.networkId)
|
||||||
}
|
}
|
||||||
|
|
||||||
handshake := signed.NewHandshakeDoneRequest(h.handshake, types.SupportedFeatures, services.P2P().SelfConnectionUris())
|
handshake := NewHandshakeDoneRequest(h.handshake, types.SupportedFeatures, services.P2P().SelfConnectionUris())
|
||||||
hsMessage, err := msgpack.Marshal(handshake)
|
hsMessage, err := msgpack.Marshal(handshake)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package protocol
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol/signed"
|
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -29,7 +28,7 @@ func RegisterProtocols() {
|
||||||
return NewEmptyRegistryQuery()
|
return NewEmptyRegistryQuery()
|
||||||
})
|
})
|
||||||
RegisterMessageType(int(types.ProtocolMethodSignedMessage), func() IncomingMessage {
|
RegisterMessageType(int(types.ProtocolMethodSignedMessage), func() IncomingMessage {
|
||||||
return signed.NewSignedMessage()
|
return NewSignedMessage()
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
package protocol
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
||||||
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||||
|
"github.com/vmihailenco/msgpack/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
type IncomingMessageDataSigned struct {
|
||||||
|
IncomingMessageData
|
||||||
|
NodeId *encoding.NodeId
|
||||||
|
}
|
||||||
|
|
||||||
|
type IncomingMessageSigned interface {
|
||||||
|
HandleMessage(message IncomingMessageDataSigned) error
|
||||||
|
DecodeMessage(dec *msgpack.Decoder, message IncomingMessageDataSigned) error
|
||||||
|
HandshakeRequirer
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
signedMessageTypes map[int]func() IncomingMessageSigned
|
||||||
|
)
|
||||||
|
|
||||||
|
func RegisterSignedProtocols() {
|
||||||
|
signedMessageTypes = make(map[int]func() IncomingMessageSigned)
|
||||||
|
|
||||||
|
RegisterSignedMessageType(int(types.ProtocolMethodHandshakeDone), func() IncomingMessageSigned {
|
||||||
|
return NewHandshakeDone()
|
||||||
|
})
|
||||||
|
RegisterSignedMessageType(int(types.ProtocolMethodAnnouncePeers), func() IncomingMessageSigned {
|
||||||
|
return NewAnnouncePeers()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterSignedMessageType(messageType int, factoryFunc func() IncomingMessageSigned) {
|
||||||
|
if factoryFunc == nil {
|
||||||
|
panic("factoryFunc cannot be nil")
|
||||||
|
}
|
||||||
|
signedMessageTypes[messageType] = factoryFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetSignedMessageType(kind int) (IncomingMessageSigned, bool) {
|
||||||
|
value, ok := signedMessageTypes[kind]
|
||||||
|
if !ok {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
return value(), true
|
||||||
|
}
|
|
@ -1,50 +0,0 @@
|
||||||
package signed
|
|
||||||
|
|
||||||
import (
|
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
|
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
|
||||||
"github.com/vmihailenco/msgpack/v5"
|
|
||||||
)
|
|
||||||
|
|
||||||
type IncomingMessageDataSigned struct {
|
|
||||||
protocol.IncomingMessageData
|
|
||||||
NodeId *encoding.NodeId
|
|
||||||
}
|
|
||||||
|
|
||||||
type IncomingMessageSigned interface {
|
|
||||||
HandleMessage(message IncomingMessageDataSigned) error
|
|
||||||
DecodeMessage(dec *msgpack.Decoder, message IncomingMessageDataSigned) error
|
|
||||||
protocol.HandshakeRequirer
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
messageTypes map[int]func() IncomingMessageSigned
|
|
||||||
)
|
|
||||||
|
|
||||||
func RegisterSignedProtocols() {
|
|
||||||
messageTypes = make(map[int]func() IncomingMessageSigned)
|
|
||||||
|
|
||||||
RegisterMessageType(int(types.ProtocolMethodHandshakeDone), func() IncomingMessageSigned {
|
|
||||||
return NewHandshakeDone()
|
|
||||||
})
|
|
||||||
RegisterMessageType(int(types.ProtocolMethodAnnouncePeers), func() IncomingMessageSigned {
|
|
||||||
return NewAnnouncePeers()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func RegisterMessageType(messageType int, factoryFunc func() IncomingMessageSigned) {
|
|
||||||
if factoryFunc == nil {
|
|
||||||
panic("factoryFunc cannot be nil")
|
|
||||||
}
|
|
||||||
messageTypes[messageType] = factoryFunc
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetMessageType(kind int) (IncomingMessageSigned, bool) {
|
|
||||||
value, ok := messageTypes[kind]
|
|
||||||
if !ok {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
return value(), true
|
|
||||||
}
|
|
|
@ -1,9 +1,8 @@
|
||||||
package signed
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
|
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||||
"github.com/vmihailenco/msgpack/v5"
|
"github.com/vmihailenco/msgpack/v5"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -17,7 +16,7 @@ type AnnouncePeers struct {
|
||||||
peer net.Peer
|
peer net.Peer
|
||||||
connectionUris []*url.URL
|
connectionUris []*url.URL
|
||||||
peersToSend []net.Peer
|
peersToSend []net.Peer
|
||||||
protocol.HandshakeRequirement
|
HandshakeRequirement
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AnnouncePeers) PeersToSend() []net.Peer {
|
func (a *AnnouncePeers) PeersToSend() []net.Peer {
|
|
@ -1,11 +1,10 @@
|
||||||
package signed
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/ed25519"
|
"crypto/ed25519"
|
||||||
"errors"
|
"errors"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/config"
|
"git.lumeweb.com/LumeWeb/libs5-go/config"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
|
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||||
"github.com/vmihailenco/msgpack/v5"
|
"github.com/vmihailenco/msgpack/v5"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
@ -13,9 +12,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_ protocol.IncomingMessage = (*SignedMessage)(nil)
|
_ IncomingMessage = (*SignedMessage)(nil)
|
||||||
_ msgpack.CustomDecoder = (*signedMessageReader)(nil)
|
_ msgpack.CustomDecoder = (*signedMessageReader)(nil)
|
||||||
_ msgpack.CustomEncoder = (*SignedMessage)(nil)
|
_ msgpack.CustomEncoder = (*SignedMessage)(nil)
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -26,7 +25,7 @@ type SignedMessage struct {
|
||||||
nodeId *encoding.NodeId
|
nodeId *encoding.NodeId
|
||||||
signature []byte
|
signature []byte
|
||||||
message []byte
|
message []byte
|
||||||
protocol.HandshakeRequirement
|
HandshakeRequirement
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SignedMessage) NodeId() *encoding.NodeId {
|
func (s *SignedMessage) NodeId() *encoding.NodeId {
|
||||||
|
@ -81,7 +80,7 @@ func NewSignedMessage() *SignedMessage {
|
||||||
return sm
|
return sm
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SignedMessage) HandleMessage(message protocol.IncomingMessageData) error {
|
func (s *SignedMessage) HandleMessage(message IncomingMessageData) error {
|
||||||
var payload signedMessageReader
|
var payload signedMessageReader
|
||||||
peer := message.Peer
|
peer := message.Peer
|
||||||
logger := message.Logger
|
logger := message.Logger
|
||||||
|
@ -91,7 +90,7 @@ func (s *SignedMessage) HandleMessage(message protocol.IncomingMessageData) erro
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if msgHandler, valid := GetMessageType(payload.kind); valid {
|
if msgHandler, valid := GetSignedMessageType(payload.kind); valid {
|
||||||
logger.Debug("SignedMessage", zap.Any("type", types.ProtocolMethodMap[types.ProtocolMethod(payload.kind)]))
|
logger.Debug("SignedMessage", zap.Any("type", types.ProtocolMethodMap[types.ProtocolMethod(payload.kind)]))
|
||||||
if msgHandler.RequiresHandshake() && !peer.IsHandshakeDone() {
|
if msgHandler.RequiresHandshake() && !peer.IsHandshakeDone() {
|
||||||
logger.Debug("Peer is not handshake done, ignoring message", zap.Any("type", types.ProtocolMethodMap[types.ProtocolMethod(payload.kind)]))
|
logger.Debug("Peer is not handshake done, ignoring message", zap.Any("type", types.ProtocolMethodMap[types.ProtocolMethod(payload.kind)]))
|
||||||
|
@ -116,7 +115,7 @@ func (s *SignedMessage) HandleMessage(message protocol.IncomingMessageData) erro
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SignedMessage) DecodeMessage(dec *msgpack.Decoder, message protocol.IncomingMessageData) error {
|
func (s *SignedMessage) DecodeMessage(dec *msgpack.Decoder, message IncomingMessageData) error {
|
||||||
nodeId, err := dec.DecodeBytes()
|
nodeId, err := dec.DecodeBytes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
|
@ -1,11 +1,10 @@
|
||||||
package signed
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
|
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/utils"
|
"git.lumeweb.com/LumeWeb/libs5-go/utils"
|
||||||
"github.com/vmihailenco/msgpack/v5"
|
"github.com/vmihailenco/msgpack/v5"
|
||||||
|
@ -13,7 +12,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ IncomingMessageSigned = (*HandshakeDone)(nil)
|
var _ IncomingMessageSigned = (*HandshakeDone)(nil)
|
||||||
var _ protocol.EncodeableMessage = (*HandshakeDone)(nil)
|
var _ EncodeableMessage = (*HandshakeDone)(nil)
|
||||||
|
|
||||||
type HandshakeDone struct {
|
type HandshakeDone struct {
|
||||||
challenge []byte
|
challenge []byte
|
||||||
|
@ -21,7 +20,7 @@ type HandshakeDone struct {
|
||||||
supportedFeatures int
|
supportedFeatures int
|
||||||
connectionUris []*url.URL
|
connectionUris []*url.URL
|
||||||
handshake []byte
|
handshake []byte
|
||||||
protocol.HandshakeRequirement
|
HandshakeRequirement
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHandshakeDoneRequest(handshake []byte, supportedFeatures int, connectionUris []*url.URL) *HandshakeDone {
|
func NewHandshakeDoneRequest(handshake []byte, supportedFeatures int, connectionUris []*url.URL) *HandshakeDone {
|
|
@ -1,4 +1,4 @@
|
||||||
package signed
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
|
@ -427,7 +427,7 @@ func (p *P2PServiceDefault) OnNewPeerListen(peer net.Peer, verifyId bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now, get the specific message handler based on the message kind
|
// Now, get the specific message handler based on the message kind
|
||||||
handler, ok := protocol.GetMessageType(reader.Kind)
|
handler, ok := protocol.GetSignedMessageType(reader.Kind)
|
||||||
if !ok {
|
if !ok {
|
||||||
p.Logger().Error("Unknown message type", zap.Int("type", reader.Kind))
|
p.Logger().Error("Unknown message type", zap.Int("type", reader.Kind))
|
||||||
return fmt.Errorf("unknown message type: %d", reader.Kind)
|
return fmt.Errorf("unknown message type: %d", reader.Kind)
|
||||||
|
|
Loading…
Reference in New Issue