Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
fix: two-way handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
alok committed Oct 2, 2023
1 parent 3443c24 commit 006f5ab
Showing 1 changed file with 61 additions and 27 deletions.
88 changes: 61 additions & 27 deletions pkg/p2p/libp2p/internal/handshake/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,9 @@ type HandshakeReq struct {
type HandshakeResp struct {
ObservedAddress common.Address
PeerType string
Ack *HandshakeReq
}

func (h *Service) verifySignature(
func (h *Service) verifyReq(
req *HandshakeReq,
peerID core.PeerID,
) (common.Address, error) {
Expand All @@ -100,6 +99,17 @@ func (h *Service) verifySignature(
return common.Address{}, errors.New("observed address mismatch")
}

if req.PeerType == "builder" {
stake, err := h.register.GetStake(ethAddress)
if err != nil {
return common.Address{}, err
}

if stake.Cmp(h.minimumStake) < 0 {
return common.Address{}, errors.New("stake insufficient")
}
}

return ethAddress, nil
}

Expand All @@ -113,6 +123,18 @@ func (h *Service) createSignature() ([]byte, error) {
return sig, nil
}

func (h *Service) verifyResp(resp *HandshakeResp) error {
if !bytes.Equal(resp.ObservedAddress.Bytes(), h.ethAddress.Bytes()) {
return errors.New("observed address mismatch")
}

if resp.PeerType != h.peerType.String() {
return errors.New("peer type mismatch")
}

return nil
}

func (h *Service) Handle(
ctx context.Context,
stream p2p.Stream,
Expand All @@ -125,22 +147,11 @@ func (h *Service) Handle(
return p2p.Peer{}, err
}

ethAddress, err := h.verifySignature(req, peerID)
ethAddress, err := h.verifyReq(req, peerID)
if err != nil {
return p2p.Peer{}, err
}

if req.PeerType == "builder" {
stake, err := h.register.GetStake(ethAddress)
if err != nil {
return p2p.Peer{}, err
}

if stake.Cmp(h.minimumStake) < 0 {
return p2p.Peer{}, errors.New("stake insufficient")
}
}

sig, err := h.createSignature()
if err != nil {
return p2p.Peer{}, err
Expand All @@ -149,17 +160,33 @@ func (h *Service) Handle(
resp := &HandshakeResp{
ObservedAddress: ethAddress,
PeerType: req.PeerType,
Ack: &HandshakeReq{
PeerType: h.peerType.String(),
Token: h.passcode,
Sig: sig,
},
}

if err := w.WriteMsg(ctx, resp); err != nil {
return p2p.Peer{}, err
}

ar, aw := msgpack.NewReaderWriter[HandshakeResp, HandshakeReq](stream)

err = aw.WriteMsg(ctx, &HandshakeReq{
PeerType: h.peerType.String(),
Token: h.passcode,
Sig: sig,
},
)
if err != nil {
return p2p.Peer{}, err
}

ack, err := ar.ReadMsg(ctx)
if err != nil {
return p2p.Peer{}, err
}

if err := h.verifyResp(ack); err != nil {
return p2p.Peer{}, err
}

return p2p.Peer{
EthAddress: ethAddress,
Type: p2p.FromString(req.PeerType),
Expand Down Expand Up @@ -194,25 +221,32 @@ func (h *Service) Handshake(
return p2p.Peer{}, err
}

if !bytes.Equal(resp.ObservedAddress.Bytes(), h.ethAddress.Bytes()) {
return p2p.Peer{}, errors.New("observed address mismatch")
if err := h.verifyResp(resp); err != nil {
return p2p.Peer{}, err
}

if resp.PeerType != h.peerType.String() {
return p2p.Peer{}, errors.New("peer type mismatch")
ar, aw := msgpack.NewReaderWriter[HandshakeReq, HandshakeResp](stream)

ack, err := ar.ReadMsg(ctx)
if err != nil {
return p2p.Peer{}, err
}

if resp.Ack == nil {
return p2p.Peer{}, errors.New("ack not received")
ethAddress, err := h.verifyReq(ack, peerID)
if err != nil {
return p2p.Peer{}, err
}

ethAddress, err := h.verifySignature(resp.Ack, peerID)
err = aw.WriteMsg(ctx, &HandshakeResp{
ObservedAddress: ethAddress,
PeerType: ack.PeerType,
})
if err != nil {
return p2p.Peer{}, err
}

return p2p.Peer{
EthAddress: ethAddress,
Type: p2p.FromString(resp.Ack.PeerType),
Type: p2p.FromString(ack.PeerType),
}, nil
}

0 comments on commit 006f5ab

Please sign in to comment.