Skip to content

Commit

Permalink
Verify send/receive by hash, fix emptyWo method
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed Sep 27, 2024
1 parent 2f3a43a commit 781c506
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
3 changes: 2 additions & 1 deletion core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions p2p/node/pubsubManager/gossipsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 10 additions & 8 deletions p2p/node/pubsubManager/gossipsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pubsubManager

import (
"context"
"math/big"
"sync"
"testing"

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 781c506

Please sign in to comment.