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/encoding"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol/signed"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/service"
|
||||
_default "git.lumeweb.com/LumeWeb/libs5-go/service/default"
|
||||
bolt "go.etcd.io/bbolt"
|
||||
|
@ -51,7 +50,7 @@ func (n *Node) Db() *bolt.DB {
|
|||
|
||||
func (n *Node) Start() error {
|
||||
protocol.RegisterProtocols()
|
||||
signed.RegisterSignedProtocols()
|
||||
protocol.RegisterSignedProtocols()
|
||||
|
||||
return n.services.Start()
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package protocol
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol/signed"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||
"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)
|
||||
}
|
||||
|
||||
handshake := signed.NewHandshakeDoneRequest(h.handshake, types.SupportedFeatures, services.P2P().SelfConnectionUris())
|
||||
handshake := NewHandshakeDoneRequest(h.handshake, types.SupportedFeatures, services.P2P().SelfConnectionUris())
|
||||
hsMessage, err := msgpack.Marshal(handshake)
|
||||
|
||||
if err != nil {
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package protocol
|
||||
|
||||
import (
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol/signed"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||
)
|
||||
|
||||
|
@ -29,7 +28,7 @@ func RegisterProtocols() {
|
|||
return NewEmptyRegistryQuery()
|
||||
})
|
||||
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 (
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
"net/url"
|
||||
|
@ -17,7 +16,7 @@ type AnnouncePeers struct {
|
|||
peer net.Peer
|
||||
connectionUris []*url.URL
|
||||
peersToSend []net.Peer
|
||||
protocol.HandshakeRequirement
|
||||
HandshakeRequirement
|
||||
}
|
||||
|
||||
func (a *AnnouncePeers) PeersToSend() []net.Peer {
|
|
@ -1,11 +1,10 @@
|
|||
package signed
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"errors"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/config"
|
||||
"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"
|
||||
"go.uber.org/zap"
|
||||
|
@ -13,7 +12,7 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
_ protocol.IncomingMessage = (*SignedMessage)(nil)
|
||||
_ IncomingMessage = (*SignedMessage)(nil)
|
||||
_ msgpack.CustomDecoder = (*signedMessageReader)(nil)
|
||||
_ msgpack.CustomEncoder = (*SignedMessage)(nil)
|
||||
)
|
||||
|
@ -26,7 +25,7 @@ type SignedMessage struct {
|
|||
nodeId *encoding.NodeId
|
||||
signature []byte
|
||||
message []byte
|
||||
protocol.HandshakeRequirement
|
||||
HandshakeRequirement
|
||||
}
|
||||
|
||||
func (s *SignedMessage) NodeId() *encoding.NodeId {
|
||||
|
@ -81,7 +80,7 @@ func NewSignedMessage() *SignedMessage {
|
|||
return sm
|
||||
}
|
||||
|
||||
func (s *SignedMessage) HandleMessage(message protocol.IncomingMessageData) error {
|
||||
func (s *SignedMessage) HandleMessage(message IncomingMessageData) error {
|
||||
var payload signedMessageReader
|
||||
peer := message.Peer
|
||||
logger := message.Logger
|
||||
|
@ -91,7 +90,7 @@ func (s *SignedMessage) HandleMessage(message protocol.IncomingMessageData) erro
|
|||
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)]))
|
||||
if msgHandler.RequiresHandshake() && !peer.IsHandshakeDone() {
|
||||
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
|
||||
}
|
||||
|
||||
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()
|
||||
if err != nil {
|
||||
return err
|
|
@ -1,11 +1,10 @@
|
|||
package signed
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"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/utils"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
|
@ -13,7 +12,7 @@ import (
|
|||
)
|
||||
|
||||
var _ IncomingMessageSigned = (*HandshakeDone)(nil)
|
||||
var _ protocol.EncodeableMessage = (*HandshakeDone)(nil)
|
||||
var _ EncodeableMessage = (*HandshakeDone)(nil)
|
||||
|
||||
type HandshakeDone struct {
|
||||
challenge []byte
|
||||
|
@ -21,7 +20,7 @@ type HandshakeDone struct {
|
|||
supportedFeatures int
|
||||
connectionUris []*url.URL
|
||||
handshake []byte
|
||||
protocol.HandshakeRequirement
|
||||
HandshakeRequirement
|
||||
}
|
||||
|
||||
func NewHandshakeDoneRequest(handshake []byte, supportedFeatures int, connectionUris []*url.URL) *HandshakeDone {
|
|
@ -1,4 +1,4 @@
|
|||
package signed
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"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
|
||||
handler, ok := protocol.GetMessageType(reader.Kind)
|
||||
handler, ok := protocol.GetSignedMessageType(reader.Kind)
|
||||
if !ok {
|
||||
p.Logger().Error("Unknown message type", zap.Int("type", reader.Kind))
|
||||
return fmt.Errorf("unknown message type: %d", reader.Kind)
|
||||
|
|
Loading…
Reference in New Issue