Skip to content

Commit

Permalink
Add function to initialize set from elements (ava-labs#1808)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-kim authored Aug 4, 2023
1 parent 3822500 commit 1bb3a00
Show file tree
Hide file tree
Showing 25 changed files with 130 additions and 150 deletions.
3 changes: 1 addition & 2 deletions api/health/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ func (w *worker) Results(tags ...string) (map[string]Result, bool) {
}

names := set.Set[string]{}
tagSet := set.NewSet[string](len(tags) + 1)
tagSet.Add(tags...)
tagSet := set.Of(tags...)
tagSet.Add(ApplicationTag) // we always want to include the application tag
for tag := range tagSet {
if set, ok := w.tags[tag]; ok {
Expand Down
3 changes: 1 addition & 2 deletions network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,8 @@ func TestSendAndGossipWithFilter(t *testing.T) {
outboundGetMsg, err := mc.Get(ids.Empty, 1, time.Second, ids.Empty, p2p.EngineType_ENGINE_TYPE_SNOWMAN)
require.NoError(err)

toSend := set.NewSet[ids.NodeID](3)
toSend := set.Of(nodeIDs...)
validNodeID := nodeIDs[1]
toSend.Add(nodeIDs...)
sentTo := net0.Send(outboundGetMsg, toSend, constants.PrimaryNetworkID, newNodeIDConnector(validNodeID))
require.Len(sentTo, 1)
require.Contains(sentTo, validNodeID)
Expand Down
6 changes: 2 additions & 4 deletions network/p2p/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ func (c *Client) AppRequestAny(
return ErrNoPeers
}

nodeIDs := set.Set[ids.NodeID]{
peers[0]: struct{}{},
}
nodeIDs := set.Of(peers[0])
return c.AppRequest(ctx, nodeIDs, appRequestBytes, onResponse)
}

Expand Down Expand Up @@ -89,7 +87,7 @@ func (c *Client) AppRequest(

if err := c.sender.SendAppRequest(
ctx,
set.Set[ids.NodeID]{nodeID: struct{}{}},
set.Of(nodeID),
requestID,
appRequestBytes,
); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions network/p2p/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestAppRequestResponse(t *testing.T) {
require.Nil(t, actualResponse)
}

require.NoError(t, client.AppRequest(context.Background(), set.Set[ids.NodeID]{nodeID: struct{}{}}, request, callback))
require.NoError(t, client.AppRequest(context.Background(), set.Of(nodeID), request, callback))
},
},
{
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestAppRequestResponse(t *testing.T) {
return nil
})

require.NoError(t, client.AppGossipSpecific(context.Background(), set.Set[ids.NodeID]{nodeID: struct{}{}}, request))
require.NoError(t, client.AppGossipSpecific(context.Background(), set.Of(nodeID), request))
},
},
}
Expand Down Expand Up @@ -333,13 +333,13 @@ func TestAppRequestDuplicateRequestIDs(t *testing.T) {
client, err := router.RegisterAppProtocol(0x1, handler)
require.NoError(err)

require.NoError(client.AppRequest(context.Background(), set.Set[ids.NodeID]{nodeID: struct{}{}}, []byte{}, nil))
require.NoError(client.AppRequest(context.Background(), set.Of(nodeID), []byte{}, nil))
requestSent.Wait()

// force the router to use the same requestID
router.requestID = 0
timeout.Add(1)
err = client.AppRequest(context.Background(), set.Set[ids.NodeID]{nodeID: struct{}{}}, []byte{}, nil)
err = client.AppRequest(context.Background(), set.Of(nodeID), []byte{}, nil)
requestSent.Wait()
require.ErrorIs(err, ErrRequestPending)

Expand Down
3 changes: 1 addition & 2 deletions network/peer/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,7 @@ func TestPingUptimes(t *testing.T) {
trackedSubnetID := ids.GenerateTestID()
untrackedSubnetID := ids.GenerateTestID()

trackedSubnets := set.NewSet[ids.ID](1)
trackedSubnets.Add(trackedSubnetID)
trackedSubnets := set.Of(trackedSubnetID)

mc := newMessageCreator(t)

Expand Down
6 changes: 2 additions & 4 deletions snow/engine/avalanche/bootstrap/bootstrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,8 @@ func TestBootstrapperTxDependencies(t *testing.T) {
IDV: txID1,
StatusV: choices.Processing,
},
DependenciesV: set.Set[ids.ID]{
innerTx0.IDV: struct{}{},
},
BytesV: txBytes1,
DependenciesV: set.Of(innerTx0.IDV),
BytesV: txBytes1,
}

tx0 := &testTx{
Expand Down
2 changes: 1 addition & 1 deletion snow/engine/common/queue/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func testJob(t *testing.T, jobID ids.ID, executed *bool, parentID ids.ID, parent
},
MissingDependenciesF: func(context.Context) (set.Set[ids.ID], error) {
if parentID != ids.Empty && !*parentExecuted {
return set.Set[ids.ID]{parentID: struct{}{}}, nil
return set.Of(parentID), nil
}
return set.Set[ids.ID]{}, nil
},
Expand Down
6 changes: 2 additions & 4 deletions snow/engine/snowman/transitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,8 +704,7 @@ func (t *Transitive) pullQuery(ctx context.Context, blkID ids.ID) {
t.RequestID++
if t.polls.Add(t.RequestID, vdrBag) {
vdrList := vdrBag.List()
vdrSet := set.NewSet[ids.NodeID](len(vdrList))
vdrSet.Add(vdrList...)
vdrSet := set.Of(vdrList...)
t.Sender.SendPullQuery(ctx, vdrSet, t.RequestID, blkID)
}
}
Expand Down Expand Up @@ -735,8 +734,7 @@ func (t *Transitive) sendQuery(ctx context.Context, blk snowman.Block, push bool
t.RequestID++
if t.polls.Add(t.RequestID, vdrBag) {
vdrs := vdrBag.List()
sendTo := set.NewSet[ids.NodeID](len(vdrs))
sendTo.Add(vdrs...)
sendTo := set.Of(vdrs...)

if push {
t.Sender.SendPushQuery(ctx, sendTo, t.RequestID, blk.Bytes())
Expand Down
3 changes: 1 addition & 2 deletions snow/networking/router/chain_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1547,8 +1547,7 @@ func TestValidatorOnlyAllowedNodeMessageDrops(t *testing.T) {

ctx := snow.DefaultConsensusContextTest()
allowedID := ids.GenerateTestNodeID()
allowedSet := set.NewSet[ids.NodeID](1)
allowedSet.Add(allowedID)
allowedSet := set.Of(allowedID)
sb := subnets.New(ctx.NodeID, subnets.Config{ValidatorOnly: true, AllowedNodes: allowedSet})

vdrs := validators.NewSet()
Expand Down
30 changes: 10 additions & 20 deletions snow/networking/sender/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ func (s *sender) SendStateSummaryFrontier(ctx context.Context, nodeID ids.NodeID
}

// Send the message over the network.
nodeIDs := set.NewSet[ids.NodeID](1)
nodeIDs.Add(nodeID)
nodeIDs := set.Of(nodeID)
sentTo := s.sender.Send(
outMsg,
nodeIDs,
Expand Down Expand Up @@ -351,8 +350,7 @@ func (s *sender) SendAcceptedStateSummary(ctx context.Context, nodeID ids.NodeID
}

// Send the message over the network.
nodeIDs := set.NewSet[ids.NodeID](1)
nodeIDs.Add(nodeID)
nodeIDs := set.Of(nodeID)
sentTo := s.sender.Send(
outMsg,
nodeIDs,
Expand Down Expand Up @@ -487,8 +485,7 @@ func (s *sender) SendAcceptedFrontier(ctx context.Context, nodeID ids.NodeID, re
}

// Send the message over the network.
nodeIDs := set.NewSet[ids.NodeID](1)
nodeIDs.Add(nodeID)
nodeIDs := set.Of(nodeID)
sentTo := s.sender.Send(
outMsg,
nodeIDs,
Expand Down Expand Up @@ -621,8 +618,7 @@ func (s *sender) SendAccepted(ctx context.Context, nodeID ids.NodeID, requestID
}

// Send the message over the network.
nodeIDs := set.NewSet[ids.NodeID](1)
nodeIDs.Add(nodeID)
nodeIDs := set.Of(nodeID)
sentTo := s.sender.Send(
outMsg,
nodeIDs,
Expand Down Expand Up @@ -702,8 +698,7 @@ func (s *sender) SendGetAncestors(ctx context.Context, nodeID ids.NodeID, reques
}

// Send the message over the network.
nodeIDs := set.NewSet[ids.NodeID](1)
nodeIDs.Add(nodeID)
nodeIDs := set.Of(nodeID)
sentTo := s.sender.Send(
outMsg,
nodeIDs,
Expand Down Expand Up @@ -742,8 +737,7 @@ func (s *sender) SendAncestors(_ context.Context, nodeID ids.NodeID, requestID u
}

// Send the message over the network.
nodeIDs := set.NewSet[ids.NodeID](1)
nodeIDs.Add(nodeID)
nodeIDs := set.Of(nodeID)
sentTo := s.sender.Send(
outMsg,
nodeIDs,
Expand Down Expand Up @@ -817,8 +811,7 @@ func (s *sender) SendGet(ctx context.Context, nodeID ids.NodeID, requestID uint3
// Send the message over the network.
var sentTo set.Set[ids.NodeID]
if err == nil {
nodeIDs := set.NewSet[ids.NodeID](1)
nodeIDs.Add(nodeID)
nodeIDs := set.Of(nodeID)
sentTo = s.sender.Send(
outMsg,
nodeIDs,
Expand Down Expand Up @@ -869,8 +862,7 @@ func (s *sender) SendPut(_ context.Context, nodeID ids.NodeID, requestID uint32,
}

// Send the message over the network.
nodeIDs := set.NewSet[ids.NodeID](1)
nodeIDs.Add(nodeID)
nodeIDs := set.Of(nodeID)
sentTo := s.sender.Send(
outMsg,
nodeIDs,
Expand Down Expand Up @@ -1185,8 +1177,7 @@ func (s *sender) SendChits(ctx context.Context, nodeID ids.NodeID, requestID uin
}

// Send the message over the network.
nodeIDs := set.NewSet[ids.NodeID](1)
nodeIDs.Add(nodeID)
nodeIDs := set.Of(nodeID)
sentTo := s.sender.Send(
outMsg,
nodeIDs,
Expand Down Expand Up @@ -1413,8 +1404,7 @@ func (s *sender) SendAppResponse(ctx context.Context, nodeID ids.NodeID, request
}

// Send the message over the network.
nodeIDs := set.NewSet[ids.NodeID](1)
nodeIDs.Add(nodeID)
nodeIDs := set.Of(nodeID)
sentTo := s.sender.Send(
outMsg,
nodeIDs,
Expand Down
Loading

0 comments on commit 1bb3a00

Please sign in to comment.