Skip to content

Commit

Permalink
Improved profile validation performance
Browse files Browse the repository at this point in the history
See PR #557

(cherry picked from commit be23505)
Signed-off-by: Riccardo Montagnin <[email protected]>
  • Loading branch information
leobragaz authored and RiccardoM committed Aug 6, 2021
1 parent 96fc38f commit 6752e60
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## Version 1.0.0
- Require chain name to be lowercase ([#533](https://github.com/desmos-labs/desmos/pull/533))
- Improved pagination ([#544](https://github.com/desmos-labs/desmos/pull/544))

- Improved the performance of profile validation checks ([\#548](https://github.com/desmos-labs/desmos/pull/548))

## Version 0.17.6
### Bug fixes
Expand Down
10 changes: 3 additions & 7 deletions x/profiles/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,9 @@ func NewProfileUpdate(dTag, nickname, bio string, pictures Pictures) *ProfileUpd
}
}

// Update updates the fields of a given profile. An error is
// returned if the resulting profile contains invalid values.
// Update updates the fields of a given profile without validating it.
// Before storing the updated profile, a validation with Validate() should
// be performed.
func (p *Profile) Update(update *ProfileUpdate) (*Profile, error) {
if update.DTag == DoNotModify {
update.DTag = p.DTag
Expand Down Expand Up @@ -296,10 +297,5 @@ func (p *Profile) Update(update *ProfileUpdate) (*Profile, error) {
return nil, err
}

err = newProfile.Validate()
if err != nil {
return nil, err
}

return newProfile, nil
}
51 changes: 30 additions & 21 deletions x/profiles/types/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,6 @@ func TestProfile_Update(t *testing.T) {
shouldErr bool
expProfile *types.Profile
}{
{
name: "invalid fields are not allowed",
original: testutil.AssertNoProfileError(types.NewProfile(
"dtag",
"nickname",
"bio",
types.NewPictures(
"https://example.com",
"https://example.com",
),
time.Unix(100, 0),
testutil.AccountFromAddr("cosmos1y54exmx84cqtasvjnskf9f63djuuj68p7hqf47"),
)),
update: types.NewProfileUpdate(
"",
"",
"",
types.NewPictures("", ""),
),
shouldErr: true,
},
{
name: "DoNotModify does not update original values",
original: testutil.AssertNoProfileError(types.NewProfile(
Expand Down Expand Up @@ -373,3 +352,33 @@ func TestProfileSerialization(t *testing.T) {
require.Equal(t, profile.GetAccountNumber(), serialized.GetAccountNumber(), "account numbers do not match")
require.Equal(t, profile.GetSequence(), serialized.GetSequence(), "sequences do not match")
}

func BenchmarkProfile_Update(b *testing.B) {
profile := testutil.AssertNoProfileError(types.NewProfile(
"dtag",
"nickname",
"bio",
types.NewPictures(
"https://example.com",
"https://example.com",
),
time.Unix(100, 0),
testutil.AccountFromAddr("cosmos1y54exmx84cqtasvjnskf9f63djuuj68p7hqf47"),
))

update := types.NewProfileUpdate(
"dtag-2",
"nickname-2",
"bio-2",
types.NewPictures(
"https://example.com/2",
"https://example.com/2",
),
)

b.ResetTimer()

for i := 0; i < b.N; i++ {
_, _ = profile.Update(update)
}
}

0 comments on commit 6752e60

Please sign in to comment.