forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve
r/gnoland/valopers
implementation (gnolang#2509)
## Description This PR improves the gno implementation of the `valopers` Realm. Waiting on gnolang#2380 to add a context-based Govdao pattern before merging. <details><summary>Contributors' checklist...</summary> - [x] Added new tests, or not needed, or not feasible - [x] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [x] Updated the official documentation or not needed - [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [x] Added references to related issues and PRs - [ ] Provided any useful hints for running manual tests - [ ] Added new benchmarks to [generated graphs](https://gnoland.github.io/benchmarks), if any. More info [here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md). </details> --------- Co-authored-by: Manfred Touron <94029+moul@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
324 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
module gno.land/r/gnoland/valopers | ||
|
||
require ( | ||
gno.land/p/demo/ownable v0.0.0-latest | ||
gno.land/p/demo/avl v0.0.0-latest | ||
gno.land/p/demo/testutils v0.0.0-latest | ||
gno.land/p/demo/uassert v0.0.0-latest | ||
gno.land/p/demo/ufmt v0.0.0-latest | ||
gno.land/p/sys/validators v0.0.0-latest | ||
gno.land/r/gov/dao v0.0.0-latest | ||
gno.land/r/sys/validators v0.0.0-latest | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package valopers | ||
|
||
import "gno.land/p/demo/avl" | ||
|
||
func init() { | ||
valopers = avl.NewTree() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package valopers | ||
|
||
import ( | ||
"testing" | ||
|
||
"gno.land/p/demo/avl" | ||
"gno.land/p/demo/testutils" | ||
"gno.land/p/demo/uassert" | ||
) | ||
|
||
func TestValopers_Register(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("already a valoper", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Clear the set for the test | ||
valopers = avl.NewTree() | ||
|
||
v := Valoper{ | ||
Address: testutils.TestAddress("valoper"), | ||
} | ||
|
||
// Add the valoper | ||
valopers.Set(v.Address.String(), v) | ||
|
||
uassert.PanicsWithMessage(t, errValoperExists, func() { | ||
Register(v) | ||
}) | ||
}) | ||
|
||
t.Run("successful registration", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Clear the set for the test | ||
valopers = avl.NewTree() | ||
|
||
v := Valoper{ | ||
Address: testutils.TestAddress("valoper"), | ||
Name: "new valoper", | ||
PubKey: "pub key", | ||
} | ||
|
||
uassert.NotPanics(t, func() { | ||
Register(v) | ||
}) | ||
|
||
uassert.NotPanics(t, func() { | ||
valoper := GetByAddr(v.Address) | ||
|
||
uassert.Equal(t, v.Address, valoper.Address) | ||
uassert.Equal(t, v.Name, valoper.Name) | ||
uassert.Equal(t, v.PubKey, valoper.PubKey) | ||
}) | ||
}) | ||
} | ||
|
||
func TestValopers_Update(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("non-existing valoper", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Clear the set for the test | ||
valopers = avl.NewTree() | ||
|
||
v := Valoper{} | ||
|
||
// Update the valoper | ||
uassert.PanicsWithMessage(t, errValoperMissing, func() { | ||
Update(v.Address, v) | ||
}) | ||
}) | ||
|
||
t.Run("overwrite valoper", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Clear the set for the test | ||
valopers = avl.NewTree() | ||
|
||
one := Valoper{ | ||
Address: testutils.TestAddress("valoper 1"), | ||
} | ||
|
||
// Add the valoper | ||
uassert.NotPanics(t, func() { | ||
Register(one) | ||
}) | ||
|
||
initialAddress := testutils.TestAddress("valoper 2") | ||
two := Valoper{ | ||
Address: initialAddress, | ||
} | ||
|
||
// Add the valoper | ||
uassert.NotPanics(t, func() { | ||
Register(two) | ||
}) | ||
|
||
// Update the valoper address | ||
// so it overlaps | ||
two = Valoper{ | ||
Address: one.Address, | ||
} | ||
|
||
// Update the valoper | ||
uassert.PanicsWithMessage(t, errInvalidAddressUpdate, func() { | ||
Update(initialAddress, two) | ||
}) | ||
}) | ||
|
||
t.Run("successful update", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Clear the set for the test | ||
valopers = avl.NewTree() | ||
|
||
var ( | ||
name = "new valoper" | ||
v = Valoper{ | ||
Address: testutils.TestAddress("valoper"), | ||
Name: name, | ||
PubKey: "pub key", | ||
} | ||
) | ||
|
||
// Add the valoper | ||
uassert.NotPanics(t, func() { | ||
Register(v) | ||
}) | ||
|
||
// Update the valoper name | ||
v.Name = "new name" | ||
v.Active = false | ||
|
||
// Update the valoper | ||
uassert.NotPanics(t, func() { | ||
Update(v.Address, v) | ||
}) | ||
|
||
// Make sure the valoper is updated | ||
uassert.NotPanics(t, func() { | ||
valoper := GetByAddr(v.Address) | ||
|
||
uassert.Equal(t, v.Name, valoper.Name) | ||
uassert.Equal(t, v.Active, valoper.Active) | ||
}) | ||
}) | ||
} |