Skip to content

Commit

Permalink
Merge pull request #10344 from vegaprotocol/fix/allow_list
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinTrinque authored Jan 9, 2024
2 parents 8d9d93c + 77480fe commit 097fd8a
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 11 deletions.
12 changes: 7 additions & 5 deletions core/teams/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (e *Engine) CreateTeam(ctx context.Context, referrer types.PartyID, determi
Closed: params.Closed,
}

if teamToAdd.AllowList != nil {
if len(params.AllowList) > 0 {
teamToAdd.AllowList = make([]types.PartyID, 0, len(params.AllowList))
for _, key := range params.AllowList {
teamToAdd.AllowList = append(teamToAdd.AllowList, types.PartyID(key))
Expand Down Expand Up @@ -142,7 +142,7 @@ func (e *Engine) UpdateTeam(ctx context.Context, referrer types.PartyID, teamID
teamsToUpdate.Closed = ptr.UnBox(params.Closed)
}

if teamsToUpdate != nil {
if len(params.AllowList) > 0 {
teamsToUpdate.AllowList = make([]types.PartyID, 0, len(params.AllowList))
for _, key := range params.AllowList {
teamsToUpdate.AllowList = append(teamsToUpdate.AllowList, types.PartyID(key))
Expand Down Expand Up @@ -352,9 +352,11 @@ func (e *Engine) loadTeamsFromSnapshot(teamsSnapshot []*snapshotpb.Team) {
Closed: teamSnapshot.Closed,
}

t.AllowList = make([]types.PartyID, 0, len(teamSnapshot.AllowList))
for _, partyIDStr := range teamSnapshot.AllowList {
t.AllowList = append(t.AllowList, types.PartyID(partyIDStr))
if len(teamSnapshot.AllowList) > 0 {
t.AllowList = make([]types.PartyID, 0, len(teamSnapshot.AllowList))
for _, partyIDStr := range teamSnapshot.AllowList {
t.AllowList = append(t.AllowList, types.PartyID(partyIDStr))
}
}

e.teams[teamID] = t
Expand Down
80 changes: 80 additions & 0 deletions core/teams/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"code.vegaprotocol.io/vega/core/types"
vgrand "code.vegaprotocol.io/vega/libs/rand"
vgtest "code.vegaprotocol.io/vega/libs/test"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -93,10 +94,13 @@ func testAdministrateTeamSucceeds(t *testing.T) {
JoinedAt: team1CreationDate,
StartedAtEpoch: te.currentEpoch,
},
Referees: nil,
Name: name,
TeamURL: teamURL,
AvatarURL: avatarURL,
CreatedAt: team1CreationDate,
Closed: false,
AllowList: nil,
},
}, te.engine.ListTeams())

Expand Down Expand Up @@ -542,6 +546,7 @@ func testJoiningTeamSucceeds(t *testing.T) {
Name: team2Name,
CreatedAt: team2CreationDate,
Closed: true,
AllowList: []types.PartyID{referee2},
},
}, te.engine.ListTeams())

Expand Down Expand Up @@ -586,6 +591,81 @@ func testJoiningTeamSucceeds(t *testing.T) {
Name: team2Name,
CreatedAt: team2CreationDate,
Closed: true,
AllowList: []types.PartyID{referee2},
},
}, te.engine.ListTeams())

referee4 := newPartyID(t)

team3CreationDate := time.Now()
te.timeService.EXPECT().GetTimeNow().Return(team3CreationDate).Times(1)
team3Name := vgrand.RandomStr(5)
teamID3, referrer3 := newTeamWithCmd(t, ctx, te, &commandspb.CreateReferralSet_Team{
Name: team3Name,
Closed: true,
AllowList: []string{referee4.String()},
})

expectRefereeJoinedTeamEvent(t, te)
referee4JoiningDate := time.Now()
te.timeService.EXPECT().GetTimeNow().Return(referee4JoiningDate).Times(1)
require.NoError(t, te.engine.JoinTeam(ctx, referee4, joinTeamCmd(t, teamID3)))
require.True(t, te.engine.IsTeamMember(referee4))

// This shows the referee1 did not moved to team 2.
assertEqualTeams(t, []types.Team{
{
ID: teamID1,
Referrer: &types.Membership{
PartyID: referrer1,
JoinedAt: team1CreationDate,
StartedAtEpoch: te.currentEpoch - 5,
},
Referees: []*types.Membership{
{
PartyID: referee1,
JoinedAt: referee1JoiningDate,
StartedAtEpoch: te.currentEpoch - 5,
},
},
Name: team1Name,
CreatedAt: team1CreationDate,
}, {
ID: teamID2,
Referrer: &types.Membership{
PartyID: referrer2,
JoinedAt: team2CreationDate,
StartedAtEpoch: te.currentEpoch - 5,
},
Referees: []*types.Membership{
{
PartyID: referee2,
JoinedAt: referee2JoiningDate3,
StartedAtEpoch: te.currentEpoch - 1,
},
},
Name: team2Name,
CreatedAt: team2CreationDate,
Closed: true,
AllowList: []types.PartyID{referee2},
}, {
ID: teamID3,
Referrer: &types.Membership{
PartyID: referrer3,
JoinedAt: team3CreationDate,
StartedAtEpoch: te.currentEpoch,
},
Referees: []*types.Membership{
{
PartyID: referee4,
JoinedAt: referee4JoiningDate,
StartedAtEpoch: te.currentEpoch,
},
},
Name: team3Name,
CreatedAt: team3CreationDate,
Closed: true,
AllowList: []types.PartyID{referee4},
},
}, te.engine.ListTeams())
}
18 changes: 18 additions & 0 deletions core/teams/helpers_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func assertEqualTeams(t *testing.T, expected, actual []types.Team) {
assert.Equal(tt, expectedTeam.TeamURL, actualTeam.TeamURL)
assert.Equal(tt, expectedTeam.AvatarURL, actualTeam.AvatarURL)
assert.Equal(tt, expectedTeam.CreatedAt.UnixNano(), actualTeam.CreatedAt.UnixNano())
assert.Equal(tt, expectedTeam.Closed, actualTeam.Closed)
assert.Equal(tt, expectedTeam.AllowList, actualTeam.AllowList)
assertEqualMembership(tt, expectedTeam.Referrer, actualTeam.Referrer)

if len(expectedTeam.Referees) != len(actualTeam.Referees) {
Expand Down Expand Up @@ -213,6 +215,22 @@ func newTeam(t *testing.T, ctx context.Context, te *testEngine) (types.TeamID, t
return teamID, referrer, teamName
}

func newTeamWithCmd(t *testing.T, ctx context.Context, te *testEngine, cmd *commandspb.CreateReferralSet_Team) (types.TeamID, types.PartyID) {
t.Helper()

teamID := newTeamID(t)
referrer := newPartyID(t)

expectTeamCreatedEvent(t, te)

err := te.engine.CreateTeam(ctx, referrer, teamID, cmd)
require.NoError(t, err)
require.NotEmpty(t, teamID)
require.True(t, te.engine.IsTeamMember(referrer))

return teamID, referrer
}

func createTeamCmd(t *testing.T, name, teamURL, avatarURL string) *commandspb.CreateReferralSet_Team {
t.Helper()

Expand Down
8 changes: 5 additions & 3 deletions core/teams/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,11 @@ func (e *SnapshottedEngine) serialiseTeams() ([]byte, error) {
Closed: team.Closed,
}

teamSnapshot.AllowList = make([]string, 0, len(team.AllowList))
for _, partyID := range team.AllowList {
teamSnapshot.AllowList = append(teamSnapshot.AllowList, partyID.String())
if len(team.AllowList) > 0 {
teamSnapshot.AllowList = make([]string, 0, len(team.AllowList))
for _, partyID := range team.AllowList {
teamSnapshot.AllowList = append(teamSnapshot.AllowList, partyID.String())
}
}

teamsSnapshot = append(teamsSnapshot, teamSnapshot)
Expand Down
10 changes: 7 additions & 3 deletions core/teams/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ func TestTakingAndRestoringSnapshotSucceeds(t *testing.T) {
JoinedAt: team2CreationDate,
StartedAtEpoch: te1.currentEpoch - 1,
},
Name: name2,
TeamURL: teamURL2,
AvatarURL: avatarURL2,
Referees: []*types.Membership{
{
PartyID: referee2,
Expand All @@ -142,7 +139,12 @@ func TestTakingAndRestoringSnapshotSucceeds(t *testing.T) {
StartedAtEpoch: te1.currentEpoch,
},
},
Name: name2,
TeamURL: teamURL2,
AvatarURL: avatarURL2,
CreatedAt: team2CreationDate,
Closed: true,
AllowList: []types.PartyID{referee3},
},
}, te1.engine.ListTeams())

Expand Down Expand Up @@ -218,6 +220,8 @@ func TestTakingAndRestoringSnapshotSucceeds(t *testing.T) {
},
},
CreatedAt: team2CreationDate,
Closed: true,
AllowList: []types.PartyID{referee3},
},
}, te2.engine.ListTeams())

Expand Down

0 comments on commit 097fd8a

Please sign in to comment.