Skip to content

Commit

Permalink
cache signatures only in acp118 handler (#3474)
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyonur authored Oct 17, 2024
1 parent e5ca053 commit abf76fd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
25 changes: 14 additions & 11 deletions network/p2p/acp118/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,26 @@ func NewHandler(verifier Verifier, signer warp.Signer) *Handler {
}

// NewCachedHandler returns an instance of Handler that caches successful
// requests.
// signatures.
func NewCachedHandler(
cacher cache.Cacher[ids.ID, []byte],
verifier Verifier,
signer warp.Signer,
) *Handler {
return &Handler{
cacher: cacher,
verifier: verifier,
signer: signer,
signatureCache: cacher,
verifier: verifier,
signer: signer,
}
}

// Handler signs warp messages
type Handler struct {
p2p.NoOpHandler

cacher cache.Cacher[ids.ID, []byte]
verifier Verifier
signer warp.Signer
signatureCache cache.Cacher[ids.ID, []byte]
verifier Verifier
signer warp.Signer
}

func (h *Handler) AppRequest(
Expand All @@ -84,8 +84,8 @@ func (h *Handler) AppRequest(
}

msgID := msg.ID()
if responseBytes, ok := h.cacher.Get(msgID); ok {
return responseBytes, nil
if signatureBytes, ok := h.signatureCache.Get(msgID); ok {
return signatureToResponse(signatureBytes)
}

if err := h.verifier.Verify(ctx, msg, request.Justification); err != nil {
Expand All @@ -100,6 +100,11 @@ func (h *Handler) AppRequest(
}
}

h.signatureCache.Put(msgID, signature)
return signatureToResponse(signature)
}

func signatureToResponse(signature []byte) ([]byte, *common.AppError) {
response := &sdk.SignatureResponse{
Signature: signature,
}
Expand All @@ -111,7 +116,5 @@ func (h *Handler) AppRequest(
Message: fmt.Sprintf("failed to marshal response: %s", err),
}
}

h.cacher.Put(msgID, responseBytes)
return responseBytes, nil
}
6 changes: 6 additions & 0 deletions network/p2p/acp118/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ func TestHandler(t *testing.T) {
require.NoError(err)

require.True(bls.Verify(pk, signature, request.Message))

// Ensure the cache is populated with correct signature
sig, ok := tt.cacher.Get(unsignedMessage.ID())
if ok {
require.Equal(sig, response.Signature)
}
}

for _, expectedErr = range tt.expectedErrs {
Expand Down

0 comments on commit abf76fd

Please sign in to comment.