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 f7fb7d9 commit 330e280
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
9 changes: 8 additions & 1 deletion core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +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.SetNonce(EncodeNonce(0))
wo.woHeader.SetLocation(common.Location{})
wo.woHeader.SetTime(0)
wo.woBody.SetHeader(h)
wo.woBody.SetUncles([]*WorkObjectHeader{})
Expand All @@ -184,6 +184,13 @@ func EmptyWorkObject(nodeCtx int) *WorkObject {
return NewWorkObjectWithHeader(wo, NewEmptyTx(), nodeCtx, BlockObject)
}

func EmptyZoneWorkObject(nodeCtx int) *WorkObject {
emptyWo := EmptyWorkObject(nodeCtx)
emptyWo.woHeader.SetLocation(common.Location{0, 0})
emptyWo.woHeader.SetCoinbase(common.ZeroAddress(emptyWo.woHeader.location))
return emptyWo
}

// ProtoEncode serializes h into the Quai Proto Header format
func (h *Header) ProtoEncode() (*ProtoHeader, error) {
if h == nil {
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
21 changes: 11 additions & 10 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.EmptyZoneWorkObject(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 Expand Up @@ -151,8 +153,7 @@ func TestMultipleRequests(t *testing.T) {
quaiBackend, _ := quai.NewQuaiBackend()
ps.SetQuaiBackend(quaiBackend)

wo := types.EmptyWorkObject(common.ZONE_CTX)
wo.WorkObjectHeader().SetCoinbase(common.BytesToAddress([]byte{1}, common.Location{0, 0}))
wo := types.EmptyZoneWorkObject(common.ZONE_CTX)

tx := types.NewEmptyTx()
txs := types.Transactions{tx}
Expand Down

0 comments on commit 330e280

Please sign in to comment.