From 794aa6bccb21679fe33ad480c2e3d8e142d2d420 Mon Sep 17 00:00:00 2001 From: tbssajal Date: Fri, 23 Dec 2022 21:57:01 +0600 Subject: [PATCH] added return type --- Lachain.CommunicationHub.Net/Hub.cs | 4 ++-- embedded_hub.go | 4 ++-- peer_service/connection/connection.go | 9 +++++---- peer_service/peer_service.go | 4 ++-- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Lachain.CommunicationHub.Net/Hub.cs b/Lachain.CommunicationHub.Net/Hub.cs index 7cf7a24..4bcbc72 100644 --- a/Lachain.CommunicationHub.Net/Hub.cs +++ b/Lachain.CommunicationHub.Net/Hub.cs @@ -157,13 +157,13 @@ public static void Send(byte[] publicKey, byte[] data, int flag) } } - public static void SetPeerPublicKey(byte[] publicKey, int peerId) + public static bool SetPeerPublicKey(byte[] publicKey, int peerId) { unsafe { fixed (byte* publicKeyPtr = publicKey) { - Imports.HubSetPeerPublicKey.Value(publicKeyPtr, publicKey.Length, peerId); + return Imports.HubSetPeerPublicKey.Value(publicKeyPtr, publicKey.Length, peerId); } } } diff --git a/embedded_hub.go b/embedded_hub.go index 31cef3f..c103b6f 100644 --- a/embedded_hub.go +++ b/embedded_hub.go @@ -158,12 +158,12 @@ func SendMessage(pubKeyPtr unsafe.Pointer, pubKeyLen C.int, dataPtr unsafe.Point } //export SetPeerPublicKey -func SetPeerPublicKey(pubKeyPtr unsafe.Pointer, pubKeyLen C.int, peerUniqId C.int) { +func SetPeerPublicKey(pubKeyPtr unsafe.Pointer, pubKeyLen C.int, peerUniqId C.int) bool { mutex.Lock() defer mutex.Unlock() pubKey := C.GoBytes(pubKeyPtr, pubKeyLen) pub := hex.EncodeToString(pubKey) - localPeer.SetPeerPublicKey(pub, uint32(peerUniqId)) + return localPeer.SetPeerPublicKey(pub, uint32(peerUniqId)) } //export BanPeer diff --git a/peer_service/connection/connection.go b/peer_service/connection/connection.go index 6e71a4b..5686ba3 100644 --- a/peer_service/connection/connection.go +++ b/peer_service/connection/connection.go @@ -456,9 +456,9 @@ func (connection *Connection) SetSignature(signature []byte) { } } -func (connection *Connection) TrySetPeerPublicKey(publicKey string) { +func (connection *Connection) TrySetPeerPublicKey(publicKey string) bool { if connection.status.Load() == Terminated { - return + return false } if (len(connection.PeerPublicKey) > 0) { if (publicKey != connection.PeerPublicKey) { @@ -467,13 +467,13 @@ func (connection *Connection) TrySetPeerPublicKey(publicKey string) { connection.PeerId.Pretty(), connection.PeerPublicKey, publicKey, ) connection.resetInboundStream() - return + return false } } else { ecdsaPubKey := utils.HexToPublicKey(publicKey) if connection.validatePublicKeyWithPeerId(ecdsaPubKey) == false { connection.resetInboundStream() - return + return false } // this indicates malicious behavior, because we can set verified public key from core only if we get a valid message // from peer. But peer is not supposed to deliver its signature before it starts sending messages. So it means peer @@ -484,6 +484,7 @@ func (connection *Connection) TrySetPeerPublicKey(publicKey string) { connection.status.CAS(JustConnected, HandshakeComplete) connection.onPublicKeyRecovered(connection, connection.PeerPublicKey) } + return true } func (connection *Connection) handleSignature(data []byte) { diff --git a/peer_service/peer_service.go b/peer_service/peer_service.go index 4a3d452..68338fe 100644 --- a/peer_service/peer_service.go +++ b/peer_service/peer_service.go @@ -277,12 +277,12 @@ func (peerService *PeerService) RemoveFromBanList(publicKey string) bool { return false } -func (peerService *PeerService) SetPeerPublicKey(publicKey string, peerUniqId uint32) { +func (peerService *PeerService) SetPeerPublicKey(publicKey string, peerUniqId uint32) bool { peerId, ok := peerService.peerIds[peerUniqId] if ok { connection, ok := peerService.connections[peerId] if ok { - connection.TrySetPeerPublicKey(publicKey) + return connection.TrySetPeerPublicKey(publicKey) } else { err := errors.New("No connection found by peer id " + peerId) panic(err)