Skip to content

Commit b3102dd

Browse files
authored
perf(header): MsgID func only needs to unmarshal Commit from ExtendedHeader (#4149)
1 parent f75a208 commit b3102dd

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

header/headertest/serde_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ package headertest
33
import (
44
"testing"
55

6+
pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb"
67
"github.com/stretchr/testify/assert"
78
"github.com/stretchr/testify/require"
9+
"golang.org/x/crypto/blake2b"
810

911
"github.com/celestiaorg/celestia-node/header"
12+
"github.com/celestiaorg/celestia-node/share/eds/edstest"
1013
)
1114

1215
func TestMarshalUnmarshalExtendedHeader(t *testing.T) {
@@ -30,6 +33,51 @@ func TestMarshalUnmarshalExtendedHeader(t *testing.T) {
3033
equalExtendedHeader(t, in, out)
3134
}
3235

36+
func TestMsgIDEquivalency(t *testing.T) {
37+
randHeader := RandExtendedHeader(t)
38+
bin, err := randHeader.MarshalBinary()
39+
require.NoError(t, err)
40+
41+
oldMsgIDFunc := func(message *pubsub_pb.Message) string {
42+
mID := func(data []byte) string {
43+
hash := blake2b.Sum256(data)
44+
return string(hash[:])
45+
}
46+
47+
h, _ := header.UnmarshalExtendedHeader(message.Data)
48+
if h == nil || h.RawHeader.ValidateBasic() != nil {
49+
return mID(message.Data)
50+
}
51+
52+
return h.Commit.BlockID.String()
53+
}
54+
55+
inboundMsg := &pubsub_pb.Message{Data: bin}
56+
57+
expectedHash := oldMsgIDFunc(inboundMsg)
58+
gotHash := header.MsgID(inboundMsg)
59+
60+
assert.Equal(t, expectedHash, gotHash)
61+
}
62+
63+
// Before changes (with 256 EDS and 100 validators):
64+
// BenchmarkMsgID-8 5203 224681 ns/op 511253 B/op 4252 allocs/op
65+
// After changes (with 256 EDS and 100 validators):
66+
// BenchmarkMsgID-8 23559 48399 ns/op 226858 B/op 1282 allocs/op
67+
func BenchmarkMsgID(b *testing.B) {
68+
eds := edstest.RandomAxisRoots(b, 256)
69+
randHeader := RandExtendedHeaderWithRoot(b, eds)
70+
bin, err := randHeader.MarshalBinary()
71+
require.NoError(b, err)
72+
msg := &pubsub_pb.Message{Data: bin}
73+
74+
b.ReportAllocs()
75+
76+
for i := 0; i < b.N; i++ {
77+
_ = header.MsgID(msg)
78+
}
79+
}
80+
3381
func equalExtendedHeader(t *testing.T, in, out *header.ExtendedHeader) {
3482
// ValidatorSet.totalVotingPower is not set (is a cached value that can be recomputed client side)
3583
assert.Equal(t, in.ValidatorSet.Validators, out.ValidatorSet.Validators)

header/serde.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,30 @@ func UnmarshalExtendedHeader(data []byte) (*ExtendedHeader, error) {
6464
return out, nil
6565
}
6666

67-
// msgID computes an id for a pubsub message
67+
// unmarhsalCommit exists to assist the MsgID function in generating a unique
68+
// message ID without the additional allocations that the full
69+
// UnmarshalExtendedHeader would cause.
70+
func unmarshalCommit(data []byte) (*core.Commit, error) {
71+
in := &header_pb.ExtendedHeader{}
72+
err := in.Unmarshal(data)
73+
if err != nil {
74+
return nil, err
75+
}
76+
77+
return core.CommitFromProto(in.Commit)
78+
}
79+
80+
// MsgID computes an id for a pubsub message
6881
// TODO(@Wondertan): This cause additional allocations per each recvd message in the topic
69-
//
70-
// Find a way to avoid those.
82+
// TODO(@renaynay): We will still allocate now but we're minimizing surface only to Commit
7183
func MsgID(pmsg *pb.Message) string {
7284
mID := func(data []byte) string {
7385
hash := blake2b.Sum256(data)
7486
return string(hash[:])
7587
}
7688

77-
h, _ := UnmarshalExtendedHeader(pmsg.Data)
78-
if h == nil || h.RawHeader.ValidateBasic() != nil {
89+
commit, err := unmarshalCommit(pmsg.Data)
90+
if commit == nil || err != nil {
7991
// There is nothing we can do about the error, and it will be anyway caught during validation.
8092
// We also *have* to return some ID for the msg, so give the hash of even faulty msg
8193
return mID(pmsg.Data)
@@ -92,5 +104,5 @@ func MsgID(pmsg *pb.Message) string {
92104
//
93105
// To solve the nondeterminism problem above, we don't compute msg id on message body and take
94106
// the actual header hash as an id.
95-
return h.Commit.BlockID.String()
107+
return commit.BlockID.String()
96108
}

0 commit comments

Comments
 (0)