Update Message ID (#7304)

* shorten message id
* Merge refs/heads/master into updateMessageID
* Merge refs/heads/master into updateMessageID
This commit is contained in:
Nishant Das 2020-09-22 23:23:33 +08:00 committed by GitHub
parent ba440abe2d
commit 22bcfd2c34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 7 deletions

View File

@ -2,7 +2,6 @@ package p2p
import ( import (
"context" "context"
"encoding/base64"
"time" "time"
pubsub "github.com/libp2p/go-libp2p-pubsub" pubsub "github.com/libp2p/go-libp2p-pubsub"
@ -80,12 +79,10 @@ func (s *Service) SubscribeToTopic(topic string, opts ...pubsub.SubOpt) (*pubsub
// Content addressable ID function. // Content addressable ID function.
// //
// ETH2 spec defines the message ID as: // ETH2 spec defines the message ID as:
// message-id: base64(SHA256(message.data)) // message-id: SHA256(message.data)[:8]
// where base64 is the URL-safe base64 alphabet with
// padding characters omitted.
func msgIDFunction(pmsg *pubsub_pb.Message) string { func msgIDFunction(pmsg *pubsub_pb.Message) string {
h := hashutil.Hash(pmsg.Data) h := hashutil.Hash(pmsg.Data)
return base64.RawURLEncoding.EncodeToString(h[:]) return string(h[:8])
} }
func setPubSubParameters() { func setPubSubParameters() {

View File

@ -2,7 +2,6 @@ package p2p
import ( import (
"context" "context"
"encoding/base64"
"fmt" "fmt"
"sync" "sync"
"testing" "testing"
@ -34,6 +33,6 @@ func TestMessageIDFunction_HashesCorrectly(t *testing.T) {
msg := [32]byte{'J', 'U', 'N', 'K'} msg := [32]byte{'J', 'U', 'N', 'K'}
pMsg := &pubsubpb.Message{Data: msg[:]} pMsg := &pubsubpb.Message{Data: msg[:]}
hashedData := hashutil.Hash(pMsg.Data) hashedData := hashutil.Hash(pMsg.Data)
msgID := base64.RawURLEncoding.EncodeToString(hashedData[:]) msgID := string(hashedData[:8])
assert.Equal(t, msgID, msgIDFunction(pMsg), "Got incorrect msg id") assert.Equal(t, msgID, msgIDFunction(pMsg), "Got incorrect msg id")
} }