From 781c5065c3a48d1e1a3452591a4bed09bd306cc5 Mon Sep 17 00:00:00 2001 From: Hussam Date: Fri, 27 Sep 2024 10:59:41 -0500 Subject: [PATCH] Verify send/receive by hash, fix emptyWo method --- core/types/block.go | 3 ++- p2p/node/pubsubManager/gossipsub.go | 6 +++--- p2p/node/pubsubManager/gossipsub_test.go | 18 ++++++++++-------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/core/types/block.go b/core/types/block.go index a5abbf2aa..4f80c5661 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -173,7 +173,8 @@ func EmptyWorkObject(nodeCtx int) *WorkObject { wo.woHeader.SetDifficulty(big.NewInt(0)) wo.woHeader.SetPrimeTerminusNumber(big.NewInt(0)) wo.woHeader.SetTxHash(EmptyRootHash) - wo.woHeader.SetLocation(common.Location{}) + wo.woHeader.SetLocation(common.Location{0, 0}) + wo.woHeader.SetCoinbase(common.ZeroAddress(wo.woHeader.location)) wo.woHeader.SetNonce(EncodeNonce(0)) wo.woHeader.SetTime(0) wo.woBody.SetHeader(h) diff --git a/p2p/node/pubsubManager/gossipsub.go b/p2p/node/pubsubManager/gossipsub.go index 731b56d74..def15a150 100644 --- a/p2p/node/pubsubManager/gossipsub.go +++ b/p2p/node/pubsubManager/gossipsub.go @@ -385,12 +385,12 @@ func (g *PubsubManager) Unsubscribe(location common.Location, datatype interface } // broadcasts data to subscribing peers -func (g *PubsubManager) Broadcast(location common.Location, datatype interface{}) error { - topicName, err := NewTopic(g.genesis, location, datatype) +func (g *PubsubManager) Broadcast(location common.Location, data interface{}) error { + topicName, err := NewTopic(g.genesis, location, data) if err != nil { return err } - protoData, err := pb.ConvertAndMarshal(datatype) + protoData, err := pb.ConvertAndMarshal(data) if err != nil { return err } diff --git a/p2p/node/pubsubManager/gossipsub_test.go b/p2p/node/pubsubManager/gossipsub_test.go index a5883f7c9..b10fa43c3 100644 --- a/p2p/node/pubsubManager/gossipsub_test.go +++ b/p2p/node/pubsubManager/gossipsub_test.go @@ -2,7 +2,6 @@ package pubsubManager import ( "context" - "math/big" "sync" "testing" @@ -105,17 +104,20 @@ func TestPubsubManager(t *testing.T) { ps.SetReceiveHandler(func(receivedFrom peer.ID, msgId string, msgTopic string, data interface{}, location common.Location) { testCh <- data }) - // Success case - newWoHeader := types.NewWorkObjectHeader(common.Hash{1}, common.Hash{1}, big.NewInt(1), big.NewInt(1), big.NewInt(1), - common.Hash{1}, types.BlockNonce{}, 0, common.Location{}, common.ZeroAddress(common.Location{0, 0})) - newWo := types.NewWorkObjectWithHeaderAndTx(newWoHeader, types.NewEmptyTx()) - broadcastedMessage := newWo.ConvertToHeaderView() + + // Create and broadcast a new WorkObjectBlock + newWo := types.EmptyWorkObject(common.ZONE_CTX) + broadcastedMessage := newWo.ConvertToBlockView() err = ps.Broadcast(common.Location{0, 0}, broadcastedMessage) require.NoError(t, err, "Failed to broadcast message") - // Verify if subscription received message + // Verify if subscription received correct message type receivedMessage := <-testCh - require.Equal(t, broadcastedMessage, receivedMessage, "Received message is not the same as broadcasted message") + recvdWorkObject, ok := receivedMessage.(types.WorkObjectBlockView) + require.True(t, ok, "Unable to cast workobject") + + // Verify equality of the send and receive + require.Equal(t, broadcastedMessage.Hash(), recvdWorkObject.Hash()) }) t.Run("Unsubscribe", func(t *testing.T) {