Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dot/network): split stored streams and handshakeData into inbound and outbound #1553

Merged
merged 9 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions dot/network/block_announce.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ type BlockAnnounceHandshake struct {
GenesisHash common.Hash
}

func (hs *BlockAnnounceHandshake) sizeof() uint32 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compute the sizeof using this.

var maxHandshakeSize = unsafe.Sizeof(BlockAnnounceHandshake{})

Also, you can store it in a constant, since size wouldn't be changing.
Add a test for it as well. The size will be 72 due to the padding of the first byte.

func TestSizeOf(t *testing.T) {
	require.Equal(t, maxHandshakeSize, uint32(72))
}

Reference: https://dlintw.github.io/gobyexample/public/memory-and-sizeof.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

return 1 + 4 + 32 + 32
}

// SubProtocol returns the block-announces sub-protocol
func (hs *BlockAnnounceHandshake) SubProtocol() string {
return blockAnnounceID
Expand Down Expand Up @@ -220,18 +224,14 @@ func (s *Service) validateBlockAnnounceHandshake(peer peer.ID, hs Handshake) err

// don't need to lock here, since function is always called inside the func returned by
// `createNotificationsMessageHandler` which locks the map beforehand.
data, ok := np.getHandshakeData(peer)
if !ok {
np.handshakeData.Store(peer, handshakeData{
received: true,
validated: true,
})
data, _ = np.getHandshakeData(peer)
data, ok := np.getHandshakeData(peer, true)
if ok {
data.handshake = hs
// TODO: since this is used only for rpc system_peers only,
// we can just set the inbound handshake and use that in Peers()
np.inboundHandshakeData.Store(peer, data)
}

data.handshake = hs
np.handshakeData.Store(peer, data)

// if peer has higher best block than us, begin syncing
latestHeader, err := s.blockState.BestBlockHeader()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions dot/network/block_announce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ func TestValidateBlockAnnounceHandshake(t *testing.T) {
nodeA := createTestService(t, configA)
nodeA.noGossip = true
nodeA.notificationsProtocols[BlockAnnounceMsgType] = &notificationsProtocol{
handshakeData: new(sync.Map),
inboundHandshakeData: new(sync.Map),
}
testPeerID := peer.ID("noot")
nodeA.notificationsProtocols[BlockAnnounceMsgType].handshakeData.Store(testPeerID, handshakeData{})
nodeA.notificationsProtocols[BlockAnnounceMsgType].inboundHandshakeData.Store(testPeerID, handshakeData{})

err := nodeA.validateBlockAnnounceHandshake(testPeerID, &BlockAnnounceHandshake{
BestBlockNumber: 100,
Expand Down
6 changes: 3 additions & 3 deletions dot/network/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,13 @@ func TestStreamCloseMetadataCleanup(t *testing.T) {
info := nodeA.notificationsProtocols[BlockAnnounceMsgType]

// Set handshake data to received
info.handshakeData.Store(nodeB.host.id(), handshakeData{
info.inboundHandshakeData.Store(nodeB.host.id(), handshakeData{
received: true,
validated: true,
})

// Verify that handshake data exists.
_, ok := info.getHandshakeData(nodeB.host.id())
_, ok := info.getHandshakeData(nodeB.host.id(), true)
require.True(t, ok)

time.Sleep(time.Second)
Expand All @@ -368,7 +368,7 @@ func TestStreamCloseMetadataCleanup(t *testing.T) {
time.Sleep(time.Second)

// Verify that handshake data is cleared.
_, ok = info.getHandshakeData(nodeB.host.id())
_, ok = info.getHandshakeData(nodeB.host.id(), true)
require.False(t, ok)
}

Expand Down
4 changes: 2 additions & 2 deletions dot/network/light_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestDecodeLightMessage(t *testing.T) {
reqEnc, err := testLightRequest.Encode()
require.NoError(t, err)

msg, err := s.decodeLightMessage(reqEnc, testPeer)
msg, err := s.decodeLightMessage(reqEnc, testPeer, true)
require.NoError(t, err)

req, ok := msg.(*LightRequest)
Expand All @@ -36,7 +36,7 @@ func TestDecodeLightMessage(t *testing.T) {
respEnc, err := testLightResponse.Encode()
require.NoError(t, err)

msg, err = s.decodeLightMessage(respEnc, testPeer)
msg, err = s.decodeLightMessage(respEnc, testPeer, true)
require.NoError(t, err)
resp, ok := msg.(*LightResponse)
require.True(t, ok)
Expand Down
Loading