Skip to content

Commit

Permalink
added return type
Browse files Browse the repository at this point in the history
  • Loading branch information
tbssajal committed Jan 13, 2023
1 parent 7b5c942 commit 794aa6b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Lachain.CommunicationHub.Net/Hub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions embedded_hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions peer_service/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions peer_service/peer_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 794aa6b

Please sign in to comment.