Skip to content

Commit

Permalink
Hash Pubsub Messages Correctly (#7177)
Browse files Browse the repository at this point in the history
* check message id

* gaz

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
  • Loading branch information
nisdas and prylabs-bulldozer[bot] authored Sep 6, 2020
1 parent 245c187 commit a812142
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions beacon-chain/p2p/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ go_test(
"//proto/testing:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/featureconfig:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/iputils:go_default_library",
"//shared/p2putils:go_default_library",
"//shared/params:go_default_library",
Expand All @@ -138,6 +139,7 @@ go_test(
"@com_github_libp2p_go_libp2p_core//network:go_default_library",
"@com_github_libp2p_go_libp2p_core//peer:go_default_library",
"@com_github_libp2p_go_libp2p_pubsub//:go_default_library",
"@com_github_libp2p_go_libp2p_pubsub//pb:go_default_library",
"@com_github_libp2p_go_libp2p_swarm//testing:go_default_library",
"@com_github_multiformats_go_multiaddr//:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
Expand Down
6 changes: 4 additions & 2 deletions beacon-chain/p2p/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ func (s *Service) SubscribeToTopic(topic string, opts ...pubsub.SubOpt) (*pubsub
//
// ETH2 spec defines the message ID as:
// message-id: base64(SHA256(message.data))
// where base64 is the URL-safe base64 alphabet with
// padding characters omitted.
func msgIDFunction(pmsg *pubsub_pb.Message) string {
h := hashutil.FastSum256(pmsg.Data)
return base64.URLEncoding.EncodeToString(h[:])
h := hashutil.Hash(pmsg.Data)
return base64.RawURLEncoding.EncodeToString(h[:])
}

func setPubSubParameters() {
Expand Down
11 changes: 11 additions & 0 deletions beacon-chain/p2p/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package p2p

import (
"context"
"encoding/base64"
"fmt"
"sync"
"testing"

pubsubpb "github.com/libp2p/go-libp2p-pubsub/pb"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)
Expand All @@ -26,3 +29,11 @@ func TestService_PublishToTopicConcurrentMapWrite(t *testing.T) {
}
wg.Wait()
}

func TestMessageIDFunction_HashesCorrectly(t *testing.T) {
msg := [32]byte{'J', 'U', 'N', 'K'}
pMsg := &pubsubpb.Message{Data: msg[:]}
hashedData := hashutil.Hash(pMsg.Data)
msgID := base64.RawURLEncoding.EncodeToString(hashedData[:])
assert.Equal(t, msgID, msgIDFunction(pMsg), "Got incorrect msg id")
}

0 comments on commit a812142

Please sign in to comment.