-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(zeromq): add block info publisher (#1666)
- Loading branch information
Showing
16 changed files
with
225 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package zmq | ||
|
||
import ( | ||
"github.com/go-zeromq/zmq4" | ||
"github.com/pactus-project/pactus/types/block" | ||
"github.com/pactus-project/pactus/util/logger" | ||
) | ||
|
||
type blockInfoPub struct { | ||
basePub | ||
} | ||
|
||
func newBlockInfoPub(socket zmq4.Socket, logger *logger.SubLogger) Publisher { | ||
return &blockInfoPub{ | ||
basePub: basePub{ | ||
topic: TopicBlockInfo, | ||
seqNo: 0, | ||
zmqSocket: socket, | ||
logger: logger, | ||
}, | ||
} | ||
} | ||
|
||
func (b *blockInfoPub) onNewBlock(blk *block.Block) { | ||
rawMsg := b.makeTopicMsg( | ||
blk.Header().ProposerAddress(), | ||
blk.Header().UnixTime(), | ||
uint16(len(blk.Transactions())), | ||
blk.Height(), | ||
) | ||
|
||
message := zmq4.NewMsg(rawMsg) | ||
|
||
if err := b.zmqSocket.Send(message); err != nil { | ||
b.logger.Error("zmq publish message error", "err", err, "publisher", b.TopicName()) | ||
} | ||
|
||
b.logger.Debug("zmq published message success", | ||
"publisher", b.TopicName(), | ||
"block_height", blk.Height()) | ||
|
||
b.seqNo++ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package zmq | ||
|
||
import ( | ||
"context" | ||
"encoding/binary" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/go-zeromq/zmq4" | ||
"github.com/pactus-project/pactus/util/testsuite" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBlockInfoPublisher(t *testing.T) { | ||
port := testsuite.FindFreePort() | ||
addr := fmt.Sprintf("tcp://localhost:%d", port) | ||
conf := DefaultConfig() | ||
conf.ZmqPubBlockInfo = addr | ||
|
||
td := setup(t, conf) | ||
defer td.closeServer() | ||
|
||
td.server.Publishers() | ||
|
||
sub := zmq4.NewSub(context.TODO(), zmq4.WithAutomaticReconnect(false)) | ||
|
||
err := sub.Dial(addr) | ||
require.NoError(t, err) | ||
|
||
err = sub.SetOption(zmq4.OptionSubscribe, string(TopicBlockInfo.Bytes())) | ||
require.NoError(t, err) | ||
|
||
blk, _ := td.TestSuite.GenerateTestBlock(td.RandHeight()) | ||
|
||
td.eventCh <- blk | ||
|
||
received, err := sub.Recv() | ||
require.NoError(t, err) | ||
|
||
require.NotNil(t, received.Frames) | ||
require.GreaterOrEqual(t, len(received.Frames), 1) | ||
|
||
msg := received.Frames[0] | ||
require.Len(t, msg, 37) | ||
|
||
topic := msg[:2] | ||
proposerBytes := msg[2:23] | ||
timestamp := binary.BigEndian.Uint32(msg[23:27]) | ||
txCount := binary.BigEndian.Uint16(msg[27:29]) | ||
height := binary.BigEndian.Uint32(msg[29:33]) | ||
seqNo := binary.BigEndian.Uint32(msg[33:]) | ||
|
||
require.Equal(t, TopicBlockInfo.Bytes(), topic) | ||
require.Equal(t, blk.Header().ProposerAddress().Bytes(), proposerBytes) | ||
require.Equal(t, blk.Header().UnixTime(), timestamp) | ||
require.Equal(t, uint16(len(blk.Transactions())), txCount) | ||
require.Equal(t, blk.Height(), height) | ||
require.Equal(t, uint32(0), seqNo) | ||
|
||
require.NoError(t, sub.Close()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.