@@ -3,10 +3,13 @@ package headertest
3
3
import (
4
4
"testing"
5
5
6
+ pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb"
6
7
"github.com/stretchr/testify/assert"
7
8
"github.com/stretchr/testify/require"
9
+ "golang.org/x/crypto/blake2b"
8
10
9
11
"github.com/celestiaorg/celestia-node/header"
12
+ "github.com/celestiaorg/celestia-node/share/eds/edstest"
10
13
)
11
14
12
15
func TestMarshalUnmarshalExtendedHeader (t * testing.T ) {
@@ -30,6 +33,51 @@ func TestMarshalUnmarshalExtendedHeader(t *testing.T) {
30
33
equalExtendedHeader (t , in , out )
31
34
}
32
35
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
+
33
81
func equalExtendedHeader (t * testing.T , in , out * header.ExtendedHeader ) {
34
82
// ValidatorSet.totalVotingPower is not set (is a cached value that can be recomputed client side)
35
83
assert .Equal (t , in .ValidatorSet .Validators , out .ValidatorSet .Validators )
0 commit comments