github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/mempool/queue/rpcInspectionRequest_test.go (about)

     1  package queue_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	pubsub "github.com/libp2p/go-libp2p-pubsub"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/onflow/flow-go/engine"
    10  	"github.com/onflow/flow-go/module/mempool/queue"
    11  	"github.com/onflow/flow-go/network/p2p/inspector/validation"
    12  	p2ptest "github.com/onflow/flow-go/network/p2p/test"
    13  	"github.com/onflow/flow-go/utils/unittest"
    14  )
    15  
    16  // TestMessageEntity_InspectRPCRequest_ID tests that the ID of a MessageEntity created from an InspectRPCRequest is
    17  // only dependent of the Nonce and PeerID fields of the InspectRPCRequest; and is independent of the RPC field.
    18  // Unique identifier for the HeroCache is imperative to prevent false-positive de-duplication.
    19  // However, the RPC field contains a bulk of the data in the InspectRPCRequest, and including it in the ID would
    20  // cause the InspectRPCRequest store and retrieval to be resource intensive.
    21  func TestMessageEntity_InspectRPCRequest_ID(t *testing.T) {
    22  	rpcs := p2ptest.GossipSubRpcFixtures(t, 2)
    23  	rpc1 := rpcs[0]
    24  	rpc2 := rpcs[1]
    25  	peerId1 := unittest.PeerIdFixture(t)
    26  
    27  	// creates two InspectRPCRequest structs with the same Nonce and PeerID fields
    28  	req1, err := validation.NewInspectRPCRequest(peerId1, &pubsub.RPC{
    29  		RPC: *rpc1,
    30  	})
    31  	require.NoError(t, err)
    32  
    33  	req2, err := validation.NewInspectRPCRequest(peerId1, &pubsub.RPC{
    34  		RPC: *rpc1,
    35  	})
    36  	require.NoError(t, err)
    37  	// Set the Nonce field of the second InspectRPCRequest struct to the Nonce field of the first
    38  	req2.Nonce = req1.Nonce
    39  
    40  	// creates a third InspectRPCRequest struct with the same Nonce and PeerID fields as the first two
    41  	// but with a different RPC field
    42  	req3, err := validation.NewInspectRPCRequest(peerId1, &pubsub.RPC{
    43  		RPC: *rpc2,
    44  	})
    45  	require.NoError(t, err)
    46  	req3.Nonce = req1.Nonce
    47  
    48  	// now convert to MessageEntity
    49  	entity1 := queue.NewMessageEntity(&engine.Message{Payload: req1})
    50  	entity2 := queue.NewMessageEntity(&engine.Message{Payload: req2})
    51  	entity3 := queue.NewMessageEntity(&engine.Message{Payload: req3})
    52  
    53  	// as the Nonce and PeerID fields are the same, the ID of the MessageEntity should be the same accross all three
    54  	// in other words, the RPC field should not affect the ID
    55  	require.Equal(t, entity1.ID(), entity2.ID())
    56  	require.Equal(t, entity1.ID(), entity3.ID())
    57  }