Skip to content

Commit

Permalink
Merge pull request #10340 from vegaprotocol/feat/10307
Browse files Browse the repository at this point in the history
Support updating profile for a party
  • Loading branch information
EVODelavega authored Jan 11, 2024
2 parents 835add5 + 095e588 commit 940d7f5
Show file tree
Hide file tree
Showing 41 changed files with 7,703 additions and 5,954 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
- [10349](https://github.com/vegaprotocol/vega/issues/10349) - Add oracle support to mark price configuration.
- [10350](https://github.com/vegaprotocol/vega/issues/10350) - Set mark price to uncrossing price if at the end of opening auction no price was yielded by the mark price methodology.
- [521](https://github.com/vegaprotocol/core-test-coverage/issues/521) - Add tests for allow list functionality when joining teams.
- [10340](https://github.com/vegaprotocol/core-test-coverage/issues/10340) - Support updating profile for a party.

### 🐛 Fixes

Expand Down
5 changes: 5 additions & 0 deletions commands/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ var (
ErrSizeIsTooLarge = errors.New("size is too large")
ErrCannotSetAllowListWhenTeamIsOpened = errors.New("cannot set allow list when team is opened")
ErrSettingAllowListRequireSettingClosedState = errors.New("setting an allow list requires setting the closed state")
ErrIsLimitedTo32Characters = errors.New("is limited to 32 characters")
ErrIsLimitedTo10Entries = errors.New("is limited to 10 entries")
ErrIsLimitedTo255Characters = errors.New("is limited to 255 characters")
ErrCannotBeBlank = errors.New("cannot be blank")
ErrIsDuplicated = errors.New("is duplicated")
)

type Errors map[string][]error
Expand Down
2 changes: 2 additions & 0 deletions commands/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ func CheckInputData(rawInputData []byte) (*commandspb.InputData, Errors) {
errs.Merge(checkUpdateMarginMode(cmd.UpdateMarginMode))
case *commandspb.InputData_JoinTeam:
errs.Merge(checkJoinTeam(cmd.JoinTeam))
case *commandspb.InputData_UpdatePartyProfile:
errs.Merge(checkUpdatePartyProfile(cmd.UpdatePartyProfile))
default:
errs.AddForProperty("tx.input_data.command", ErrIsNotSupported)
}
Expand Down
63 changes: 63 additions & 0 deletions commands/update_party_profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package commands

import (
"fmt"

commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
)

func CheckUpdatePartyProfile(cmd *commandspb.UpdatePartyProfile) error {
return checkUpdatePartyProfile(cmd).ErrorOrNil()
}

func checkUpdatePartyProfile(cmd *commandspb.UpdatePartyProfile) Errors {
errs := NewErrors()

if cmd == nil {
return errs.FinalAddForProperty("update_party_profile", ErrIsRequired)
}

if len(cmd.Alias) > 32 {
errs.AddForProperty("update_party_profile.alias", ErrIsLimitedTo32Characters)
}

if len(cmd.Metadata) > 10 {
errs.AddForProperty("update_party_profile.metadata", ErrIsLimitedTo10Entries)
} else {
seenKeys := map[string]interface{}{}
for i, m := range cmd.Metadata {
if len(m.Key) > 32 {
errs.AddForProperty(fmt.Sprintf("update_party_profile.metadata.%d.key", i), ErrIsLimitedTo32Characters)
} else if len(m.Key) == 0 {
errs.AddForProperty(fmt.Sprintf("update_party_profile.metadata.%d.key", i), ErrCannotBeBlank)
}

_, alreadySeen := seenKeys[m.Key]
if alreadySeen {
errs.AddForProperty(fmt.Sprintf("update_party_profile.metadata.%d.key", i), ErrIsDuplicated)
}
seenKeys[m.Key] = nil

if len(m.Value) > 255 {
errs.AddForProperty(fmt.Sprintf("update_party_profile.metadata.%d.value", i), ErrIsLimitedTo255Characters)
}
}
}

return errs
}
187 changes: 187 additions & 0 deletions commands/update_party_profile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package commands_test

import (
"errors"
"testing"

"code.vegaprotocol.io/vega/commands"
vegapb "code.vegaprotocol.io/vega/protos/vega"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"

"github.com/stretchr/testify/assert"
)

func TestUpdatePartyProfile(t *testing.T) {
t.Run("Updating party's profile succeeds", testUpdatePartyProfileSucceeds)
t.Run("Updating party's profile with invalid alias fails", testUpdatePartyProfileWithInvalidAliasFails)
t.Run("Updating party's profile with invalid metadata fails", testUpdatePartyProfileWithInvalidMetadataFails)
}

func testUpdatePartyProfileSucceeds(t *testing.T) {
tcs := []struct {
name string
cmd *commandspb.UpdatePartyProfile
}{
{
name: "when empty",
cmd: &commandspb.UpdatePartyProfile{},
}, {
name: "with an alias",
cmd: &commandspb.UpdatePartyProfile{
Alias: "test",
},
}, {
name: "with metadata",
cmd: &commandspb.UpdatePartyProfile{
Metadata: []*vegapb.Metadata{
{
Key: "key",
Value: "value",
},
},
},
}, {
name: "with both",
cmd: &commandspb.UpdatePartyProfile{
Alias: "test",
Metadata: []*vegapb.Metadata{
{
Key: "key",
Value: "value",
},
},
},
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
err := checkUpdatePartyProfile(t, tc.cmd)

assert.Empty(t, err)
})
}
}

func testUpdatePartyProfileWithInvalidAliasFails(t *testing.T) {
tcs := []struct {
name string
alias string
err error
}{
{
name: "with more than 32 characters",
alias: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
err: commands.ErrIsLimitedTo32Characters,
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
err := checkUpdatePartyProfile(t, &commandspb.UpdatePartyProfile{
Alias: tc.alias,
})

assert.Contains(t, err.Get("update_party_profile.alias"), tc.err)
})
}
}

func testUpdatePartyProfileWithInvalidMetadataFails(t *testing.T) {
tcs := []struct {
name string
metadata []*vegapb.Metadata
field string
err error
}{
{
name: "with more than 10 entries",
metadata: []*vegapb.Metadata{{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}},
field: "update_party_profile.metadata",
err: commands.ErrIsLimitedTo10Entries,
}, {
name: "with empty key",
metadata: []*vegapb.Metadata{
{
Key: "",
Value: "",
},
},
field: "update_party_profile.metadata.0.key",
err: commands.ErrCannotBeBlank,
}, {
name: "with key more than 32 characters",
metadata: []*vegapb.Metadata{
{
Key: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
Value: "",
},
},
field: "update_party_profile.metadata.0.key",
err: commands.ErrIsLimitedTo32Characters,
}, {
name: "with duplicated key",
metadata: []*vegapb.Metadata{
{
Key: "hello",
Value: "value1",
}, {
Key: "hello",
Value: "value2",
},
},
field: "update_party_profile.metadata.1.key",
err: commands.ErrIsDuplicated,
}, {
name: "with value more than 255 characters",
metadata: []*vegapb.Metadata{
{
Key: "test",
Value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
},
},
field: "update_party_profile.metadata.0.value",
err: commands.ErrIsLimitedTo255Characters,
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
err := checkUpdatePartyProfile(t, &commandspb.UpdatePartyProfile{
Metadata: tc.metadata,
})

assert.Contains(t, err.Get(tc.field), tc.err)
})
}
}

func checkUpdatePartyProfile(t *testing.T, cmd *commandspb.UpdatePartyProfile) commands.Errors {
t.Helper()

err := commands.CheckUpdatePartyProfile(cmd)

var e commands.Errors
if ok := errors.As(err, &e); !ok {
return commands.NewErrors()
}

return e
}
2 changes: 1 addition & 1 deletion core/broker/socket_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (s *socketClient) Receive(ctx context.Context) (<-chan events.Event, <-chan

evt := toEvent(ctx, &be)
if evt == nil {
s.log.Error("Can not convert proto event to internal event", logging.String("event_type", be.GetType().String()))
s.log.Error("Cannot convert proto event to internal event", logging.String("event_type", be.GetType().String()))
continue
}

Expand Down
4 changes: 4 additions & 0 deletions core/events/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ const (
TransferFeesEvent
TransferFeesDiscountUpdatedEvent
PartyMarginModeUpdatedEvent
PartyProfileUpdatedEvent
)

var (
Expand Down Expand Up @@ -264,6 +265,7 @@ var (
eventspb.BusEventType_BUS_EVENT_TYPE_TRANSFER_FEES_PAID: TransferFeesEvent,
eventspb.BusEventType_BUS_EVENT_TYPE_TRANSFER_FEES_DISCOUNT_UPDATED: TransferFeesDiscountUpdatedEvent,
eventspb.BusEventType_BUS_EVENT_TYPE_PARTY_MARGIN_MODE_UPDATED: PartyMarginModeUpdatedEvent,
eventspb.BusEventType_BUS_EVENT_TYPE_PARTY_PROFILE_UPDATED: PartyProfileUpdatedEvent,
// If adding a type here, please also add it to data-node/broker/convert.go
}

Expand Down Expand Up @@ -352,6 +354,7 @@ var (
TransferFeesEvent: eventspb.BusEventType_BUS_EVENT_TYPE_TRANSFER_FEES_PAID,
TransferFeesDiscountUpdatedEvent: eventspb.BusEventType_BUS_EVENT_TYPE_TRANSFER_FEES_DISCOUNT_UPDATED,
PartyMarginModeUpdatedEvent: eventspb.BusEventType_BUS_EVENT_TYPE_PARTY_MARGIN_MODE_UPDATED,
PartyProfileUpdatedEvent: eventspb.BusEventType_BUS_EVENT_TYPE_PARTY_PROFILE_UPDATED,
// If adding a type here, please also add it to data-node/broker/convert.go
}

Expand Down Expand Up @@ -439,6 +442,7 @@ var (
PaidLiquidityFeesStatsEvent: "LiquidityFeesStatsEvent",
VestingBalancesSummaryEvent: "VestingBalancesSummaryEvent",
PartyMarginModeUpdatedEvent: "PartyMarginModeUpdatedEvent",
PartyProfileUpdatedEvent: "PartyProfileUpdatedEvent",
}
)

Expand Down
Loading

0 comments on commit 940d7f5

Please sign in to comment.