|
| 1 | +package zmq |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/binary" |
| 7 | + "fmt" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/go-zeromq/zmq4" |
| 11 | + "github.com/pactus-project/pactus/crypto/hash" |
| 12 | + "github.com/pactus-project/pactus/types/block" |
| 13 | + "github.com/pactus-project/pactus/types/tx" |
| 14 | + "github.com/pactus-project/pactus/util/testsuite" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +func TestPublisherOnSameSockets(t *testing.T) { |
| 19 | + port := testsuite.FindFreePort() |
| 20 | + addr := fmt.Sprintf("tcp://localhost:%d", port) |
| 21 | + conf := DefaultConfig() |
| 22 | + conf.ZmqPubRawTx = addr |
| 23 | + conf.ZmqPubTxInfo = addr |
| 24 | + conf.ZmqPubRawBlock = addr |
| 25 | + conf.ZmqPubBlockInfo = addr |
| 26 | + |
| 27 | + td := setup(t, conf) |
| 28 | + defer td.closeServer() |
| 29 | + |
| 30 | + td.server.Publishers() |
| 31 | + |
| 32 | + sub := zmq4.NewSub(context.TODO(), zmq4.WithAutomaticReconnect(false)) |
| 33 | + |
| 34 | + err := sub.Dial(addr) |
| 35 | + require.NoError(t, err) |
| 36 | + |
| 37 | + err = sub.SetOption(zmq4.OptionSubscribe, string(TopicTransactionInfo.Bytes())) |
| 38 | + require.NoError(t, err) |
| 39 | + |
| 40 | + err = sub.SetOption(zmq4.OptionSubscribe, string(TopicRawTransaction.Bytes())) |
| 41 | + require.NoError(t, err) |
| 42 | + |
| 43 | + err = sub.SetOption(zmq4.OptionSubscribe, string(TopicBlockInfo.Bytes())) |
| 44 | + require.NoError(t, err) |
| 45 | + |
| 46 | + err = sub.SetOption(zmq4.OptionSubscribe, string(TopicRawBlock.Bytes())) |
| 47 | + require.NoError(t, err) |
| 48 | + |
| 49 | + blk, _ := td.TestSuite.GenerateTestBlock(td.RandHeight()) |
| 50 | + |
| 51 | + td.eventCh <- blk |
| 52 | + |
| 53 | + for i := 0; i < (len(blk.Transactions())*2)+2; i++ { |
| 54 | + received, err := sub.Recv() |
| 55 | + require.NoError(t, err) |
| 56 | + |
| 57 | + require.NotNil(t, received.Frames) |
| 58 | + require.GreaterOrEqual(t, len(received.Frames), 1) |
| 59 | + |
| 60 | + msg := received.Frames[0] |
| 61 | + |
| 62 | + topic := TopicFromBytes(msg[:2]) |
| 63 | + blockNumberOffset := len(msg) - 8 |
| 64 | + height := binary.BigEndian.Uint32(msg[blockNumberOffset : blockNumberOffset+4]) |
| 65 | + seqNo := binary.BigEndian.Uint32(msg[len(msg)-4:]) |
| 66 | + t.Logf("[%s] %d", topic, seqNo) |
| 67 | + |
| 68 | + require.Equal(t, height, blk.Height()) |
| 69 | + |
| 70 | + switch topic { |
| 71 | + case TopicRawTransaction: |
| 72 | + rawTx := msg[2 : len(msg)-8] |
| 73 | + |
| 74 | + txn, err := tx.FromBytes(rawTx) |
| 75 | + |
| 76 | + require.NoError(t, err) |
| 77 | + require.NotNil(t, txn) |
| 78 | + require.Equal(t, TopicRawTransaction, topic) |
| 79 | + require.NotEqual(t, 0, txn.SerializeSize()) |
| 80 | + case TopicTransactionInfo: |
| 81 | + txHash := msg[2:34] |
| 82 | + id, err := hash.FromBytes(txHash) |
| 83 | + |
| 84 | + require.NoError(t, err) |
| 85 | + require.NotNil(t, id) |
| 86 | + require.Equal(t, TopicTransactionInfo, topic) |
| 87 | + |
| 88 | + case TopicRawBlock: |
| 89 | + blockHeader := msg[2 : len(msg)-8] |
| 90 | + buf := bytes.NewBuffer(blockHeader) |
| 91 | + header := new(block.Header) |
| 92 | + |
| 93 | + require.NoError(t, header.Decode(buf)) |
| 94 | + require.NotNil(t, header) |
| 95 | + require.Equal(t, TopicRawBlock, topic) |
| 96 | + require.Equal(t, header.PrevBlockHash(), blk.Header().PrevBlockHash()) |
| 97 | + require.Equal(t, header.StateRoot(), blk.Header().StateRoot()) |
| 98 | + case TopicBlockInfo: |
| 99 | + proposerBytes := msg[2:23] |
| 100 | + timestamp := binary.BigEndian.Uint32(msg[23:27]) |
| 101 | + txCount := binary.BigEndian.Uint16(msg[27:29]) |
| 102 | + |
| 103 | + require.Equal(t, TopicBlockInfo, topic) |
| 104 | + require.Equal(t, blk.Header().ProposerAddress().Bytes(), proposerBytes) |
| 105 | + require.Equal(t, blk.Header().UnixTime(), timestamp) |
| 106 | + require.Equal(t, uint16(len(blk.Transactions())), txCount) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + require.NoError(t, sub.Close()) |
| 111 | +} |
0 commit comments