Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add p2p handler for acp-118 sig request #1260

Merged
merged 5 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 36 additions & 23 deletions warp/handlers/signature_request_p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
avalancheWarp "github.com/ava-labs/avalanchego/vms/platformvm/warp"
"github.com/ava-labs/avalanchego/vms/platformvm/warp/payload"
"github.com/ava-labs/subnet-evm/warp"
"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -59,28 +60,46 @@ func (s *SignatureRequestHandlerP2P) AppRequest(
}
}

var (
sig [bls.SignatureLen]byte
err error
)
if len(req.Message) == ids.IDLen {
// Treat requests with 32-byte [message] as a block hash.
var blockID ids.ID
copy(blockID[:], req.Message)
sig, err = s.GetBlockSignature(blockID)
if err != nil {
s.stats.IncBlockSignatureMiss()
} else {
s.stats.IncBlockSignatureHit()
unsignedMessage, err := avalancheWarp.ParseUnsignedMessage(req.Message)
if err != nil {
return nil, &common.AppError{
Code: ErrFailedToParse,
Message: "failed to parse unsigned message: " + err.Error(),
}
} else {
// Otherwise, treat the request as an unsigned warp message.
sig, err = s.GetMessageSignature(req.Message)
}
parsed, err := payload.Parse(unsignedMessage.Payload)
if err != nil {
return nil, &common.AppError{
Code: ErrFailedToParse,
Message: "failed to parse payload: " + err.Error(),
}
}

var sig [bls.SignatureLen]byte
switch p := parsed.(type) {
case *payload.AddressedCall:
// Note we pass the unsigned message ID to GetMessageSignature since
// that is what the backend expects.
// However, we verify the types and format of the payload to ensure
// the message conforms to the ACP-118 spec.
sig, err = s.GetMessageSignature(unsignedMessage.ID())
if err != nil {
s.stats.IncMessageSignatureMiss()
} else {
s.stats.IncMessageSignatureHit()
}
case *payload.Hash:
sig, err = s.GetBlockSignature(p.Hash)
if err != nil {
s.stats.IncBlockSignatureMiss()
} else {
s.stats.IncBlockSignatureHit()
}
default:
return nil, &common.AppError{
Code: ErrFailedToParse,
Message: "unknown payload type",
}
}
if err != nil {
return nil, &common.AppError{
Expand All @@ -102,7 +121,7 @@ func (s *SignatureRequestHandlerP2P) AppRequest(
return respBytes, nil
}

func (s *SignatureRequestHandlerP2P) GetMessageSignature(message []byte) ([bls.SignatureLen]byte, error) {
func (s *SignatureRequestHandlerP2P) GetMessageSignature(messageID ids.ID) ([bls.SignatureLen]byte, error) {
darioush marked this conversation as resolved.
Show resolved Hide resolved
startTime := time.Now()
s.stats.IncMessageSignatureRequest()

Expand All @@ -111,12 +130,6 @@ func (s *SignatureRequestHandlerP2P) GetMessageSignature(message []byte) ([bls.S
s.stats.UpdateMessageSignatureRequestTime(time.Since(startTime))
}()

unsignedMessage, err := avalancheWarp.ParseUnsignedMessage(message)
if err != nil {
return [bls.SignatureLen]byte{}, err
}

messageID := unsignedMessage.ID()
return s.backend.GetMessageSignature(messageID)
}

Expand Down
25 changes: 21 additions & 4 deletions warp/handlers/signature_request_p2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func TestMessageSignatureHandlerP2P(t *testing.T) {
backend, err := warp.NewBackend(snowCtx.NetworkID, snowCtx.ChainID, warpSigner, warptest.EmptyBlockClient, database, 100, [][]byte{offchainMessage.Bytes()})
require.NoError(t, err)

msg, err := avalancheWarp.NewUnsignedMessage(snowCtx.NetworkID, snowCtx.ChainID, []byte("test"))
offchainPayload, err := payload.NewAddressedCall([]byte{0, 0, 0}, []byte("test"))
darioush marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)
msg, err := avalancheWarp.NewUnsignedMessage(snowCtx.NetworkID, snowCtx.ChainID, offchainPayload.Bytes())
require.NoError(t, err)
messageID := msg.ID()
require.NoError(t, backend.AddMessage(msg))
Expand All @@ -47,6 +49,11 @@ func TestMessageSignatureHandlerP2P(t *testing.T) {
offchainSignature, err := backend.GetMessageSignature(offchainMessage.ID())
require.NoError(t, err)

unknownPayload, err := payload.NewAddressedCall([]byte{0, 0, 0}, []byte("unknown message"))
require.NoError(t, err)
unknownMessage, err := avalancheWarp.NewUnsignedMessage(snowCtx.NetworkID, snowCtx.ChainID, unknownPayload.Bytes())
require.NoError(t, err)

tests := map[string]struct {
setup func() (request sdk.SignatureRequest, expectedResponse []byte)
verifyStats func(t *testing.T, stats *handlerStats)
Expand Down Expand Up @@ -80,7 +87,7 @@ func TestMessageSignatureHandlerP2P(t *testing.T) {
},
"unknown message": {
setup: func() (request sdk.SignatureRequest, expectedResponse []byte) {
return sdk.SignatureRequest{Message: []byte("unknown message")}, nil
return sdk.SignatureRequest{Message: unknownMessage.Bytes()}, nil
},
verifyStats: func(t *testing.T, stats *handlerStats) {
require.EqualValues(t, 1, stats.messageSignatureRequest.Snapshot().Count())
Expand Down Expand Up @@ -149,14 +156,24 @@ func TestBlockSignatureHandlerP2P(t *testing.T) {
require.NoError(t, err)
unknownBlockID := ids.GenerateTestID()

toMessageBytes := func(id ids.ID) []byte {
idPayload, err := payload.NewHash(id)
require.NoError(t, err)

msg, err := avalancheWarp.NewUnsignedMessage(snowCtx.NetworkID, snowCtx.ChainID, idPayload.Bytes())
require.NoError(t, err)

return msg.Bytes()
}

tests := map[string]struct {
setup func() (request sdk.SignatureRequest, expectedResponse []byte)
verifyStats func(t *testing.T, stats *handlerStats)
err error
}{
"known block": {
setup: func() (request sdk.SignatureRequest, expectedResponse []byte) {
return sdk.SignatureRequest{Message: blkID[:]}, signature[:]
return sdk.SignatureRequest{Message: toMessageBytes(blkID)}, signature[:]
},
verifyStats: func(t *testing.T, stats *handlerStats) {
require.EqualValues(t, 0, stats.messageSignatureRequest.Snapshot().Count())
Expand All @@ -169,7 +186,7 @@ func TestBlockSignatureHandlerP2P(t *testing.T) {
},
"unknown block": {
setup: func() (request sdk.SignatureRequest, expectedResponse []byte) {
return sdk.SignatureRequest{Message: unknownBlockID[:]}, nil
return sdk.SignatureRequest{Message: toMessageBytes(unknownBlockID)}, nil
},
verifyStats: func(t *testing.T, stats *handlerStats) {
require.EqualValues(t, 0, stats.messageSignatureRequest.Snapshot().Count())
Expand Down
Loading