-
Notifications
You must be signed in to change notification settings - Fork 379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: r/system/names public functions and checks with AddPackage #384
Closed
Closed
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
51322b7
feat: r/system/names public functions
anarcher 2a9fee6
test: names basic test
anarcher db517fd
chore: default inpause false
anarcher 65fbc4d
feat(render): render index and namespace
anarcher 180fb33
feat: reName and HasPerm
anarcher 32a086f
feat: check namespace perm in vmkeeper.AddPackage
anarcher 3b865ef
refactor: separate space methods and public function
anarcher 5e18c06
refactor: assertIsAdmin
anarcher 4a3c896
Update examples/gno.land/r/system/names/names.gno
anarcher 2e9e7a5
Update examples/gno.land/r/system/names/names.gno
anarcher a0a83e8
refactor: move public functions to public.gno
anarcher 32c816a
chore: comment TODO: prevent self
anarcher 5f1c6c4
feat: caller can't remove self
anarcher 905a458
feat: global variable `enabled` for skipping to check perms if adding
anarcher 1595785
chore: remove todo comment
anarcher 904bd0b
fix: avl.iterate
anarcher 715e72f
chore: check enabled
anarcher d66c615
fix: avl.Tree.Iterate
anarcher 6852b17
chore: make fmt
anarcher a61c9df
chore: update gno.mod
anarcher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package names | ||
|
||
// if enabled is true, `HasPerm` will check the permissions of names. | ||
// todo: this is a temporary solution, we should use a more robust | ||
var enabled bool = false |
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
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,124 @@ | ||
package names | ||
|
||
import ( | ||
"std" | ||
|
||
"gno.land/p/demo/ufmt" | ||
) | ||
|
||
func Register(namespace string) { | ||
// TODO: input sanitization: | ||
// - already exists / reserved. | ||
// - min/max length, format. | ||
// - fees (dynamic, based on length). | ||
if existsNamespace(namespace) { | ||
panic("namespace already exists") | ||
} | ||
|
||
err := validateNamespace(namespace) | ||
checkErr(err) | ||
|
||
caller := std.GetOrigCaller() | ||
namespaces.Set(namespace, &Space{ | ||
Admins: []std.Address{caller}, | ||
}) | ||
} | ||
|
||
func AddAdmin(namespace string, newAdmin std.Address) { | ||
space := getSpace(namespace) | ||
assertIsAdmin(space) | ||
space.addAdmin(newAdmin) | ||
} | ||
|
||
func RemoveAdmin(namespace string, admin std.Address) { | ||
space := getSpace(namespace) | ||
assertIsAdmin(space) | ||
err := space.removeAdmin(admin) | ||
checkErr(err) | ||
} | ||
|
||
func AddEditor(namespace string, newEditor std.Address) { | ||
space := getSpace(namespace) | ||
assertIsAdmin(space) | ||
space.addEditor(newEditor) | ||
} | ||
|
||
func RemoveEditor(namespace string, editor std.Address) { | ||
space := getSpace(namespace) | ||
assertIsAdmin(space) | ||
err := space.removeEditor(editor) | ||
checkErr(err) | ||
} | ||
|
||
func SetInPause(namespace string, state bool) { | ||
space := getSpace(namespace) | ||
assertIsAdmin(space) | ||
space.InPause = state | ||
} | ||
|
||
// HasPerm returns true if the caller has permission of the namespace. | ||
// If the namespace does not exist, it will return panic. | ||
// If the namespace exists but the caller is not an admin or editor, | ||
// it will return false. | ||
// The vm keeper will use this function to check to add package | ||
func HasPerm(namespace string) bool { | ||
// if enabled is false, it always returns true for dev and testing. | ||
if !enabled { | ||
return true | ||
} | ||
caller := std.GetOrigCaller() | ||
space := getSpace(namespace) | ||
return space.hasPerm(caller) | ||
} | ||
|
||
func Render(path string) string { | ||
// TODO: by address. | ||
|
||
if path == "" { | ||
return renderIndex() | ||
} else if path[:2] == "n/" { | ||
return renderNamespace(path[2:]) | ||
} | ||
return "" | ||
} | ||
|
||
func renderNamespace(namespace string) string { | ||
space := getSpace(namespace) | ||
output := ufmt.Sprintf(` | ||
# %s | ||
|
||
## Admins | ||
|
||
%s | ||
|
||
## Editors | ||
|
||
%s | ||
|
||
## InPause | ||
|
||
%s | ||
|
||
`, namespace, renderAddresses(space.Admins), renderAddresses(space.Editors), formatBool(space.InPause)) | ||
return output | ||
} | ||
|
||
func renderIndex() string { | ||
output := "## Namespaces \n" | ||
namespaces.Iterate("", "", func(namespace string, value interface{}) bool { | ||
space := value.(*Space) | ||
output += ufmt.Sprintf("* [%s](/r/system/names:n/%s) - admins: %d editors: %d inPause: %s \n", | ||
namespace, namespace, len(space.Admins), len(space.Editors), formatBool(space.InPause)) | ||
return false | ||
}) | ||
|
||
return output | ||
} | ||
|
||
func renderAddresses(addresses []std.Address) string { | ||
output := "" | ||
for _, address := range addresses { | ||
output += ufmt.Sprintf("* %s \n", string(address)) | ||
} | ||
return output | ||
} |
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,74 @@ | ||
package names | ||
|
||
import ( | ||
"std" | ||
"testing" | ||
) | ||
|
||
func TestRegisterNamespace(t *testing.T) { | ||
std.TestSetOrigCaller(std.Address("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq")) | ||
creator := std.GetOrigCaller() | ||
|
||
n := "test2" | ||
Register(n) | ||
s := getSpace(n) | ||
assertTrue(t, containsAddress(s.Admins, creator)) | ||
} | ||
|
||
func TestAdminFuncs(t *testing.T) { | ||
std.TestSetOrigCaller(std.Address("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq")) | ||
creator := std.GetOrigCaller() | ||
|
||
test1 := std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") | ||
test2 := std.Address("g1r6casl322p5adkmmjjkjea404e7f6w29y6gzwg") | ||
|
||
ns := "test3" | ||
|
||
Register(ns) | ||
s := getSpace(ns) | ||
|
||
AddAdmin(ns, test1) | ||
AddAdmin(ns, test2) | ||
assertTrue(t, containsAddress(s.Admins, test1)) | ||
assertTrue(t, containsAddress(s.Admins, test2)) | ||
|
||
RemoveAdmin(ns, test1) | ||
assertFalse(t, containsAddress(s.Admins, test1)) | ||
assertTrue(t, containsAddress(s.Admins, test2)) | ||
|
||
AddEditor(ns, test1) | ||
assertTrue(t, containsAddress(s.Editors, test1)) | ||
|
||
RemoveEditor(ns, test1) | ||
assertTrue(t, !containsAddress(s.Editors, test1)) | ||
} | ||
|
||
func TestSpaceMethods(t *testing.T) { | ||
std.TestSetOrigCaller(std.Address("g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq")) | ||
creator := std.GetOrigCaller() | ||
|
||
test1 := std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") | ||
test2 := std.Address("g1r6casl322p5adkmmjjkjea404e7f6w29y6gzwg") | ||
|
||
ns := "test4" | ||
Register(ns) | ||
s := getSpace(ns) | ||
|
||
assertTrue(t, s.isAdmin(creator)) | ||
assertTrue(t, s.removeAdmin(creator) != nil) | ||
|
||
s.addAdmin(test1) | ||
assertTrue(t, s.removeAdmin(test1) == nil) | ||
} | ||
|
||
func assertTrue(t *testing.T, b bool) { | ||
if !b { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func assertFalse(t *testing.T, b bool) { | ||
if b { | ||
t.Fail() | ||
} | ||
} |
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,64 @@ | ||
package names | ||
|
||
import ( | ||
"std" | ||
) | ||
|
||
func getSpace(namespace string) *Space { | ||
s, ok := namespaces.Get(namespace) | ||
if !ok { | ||
panic("namespace does not exist") | ||
} | ||
return s.(*Space) | ||
} | ||
|
||
func existsNamespace(name string) bool { | ||
_, ok := namespaces.Get(name) | ||
return ok | ||
} | ||
|
||
func assertIsAdmin(space *Space) { | ||
caller := std.GetOrigCaller() | ||
if !space.isAdmin(caller) { | ||
panic("Only admins can call this function") | ||
} | ||
} | ||
|
||
func removeAddress(addrs []std.Address, addr std.Address) []std.Address { | ||
var newAddrs []std.Address | ||
for _, a := range addrs { | ||
if a != addr { | ||
newAddrs = append(newAddrs, a) | ||
} | ||
} | ||
return newAddrs | ||
} | ||
|
||
func containsAddress(addrs []std.Address, addr std.Address) bool { | ||
for _, a := range addrs { | ||
if a == addr { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func isCallerAddress(addr std.Address) bool { | ||
if addr == std.GetOrigCaller() { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func checkErr(err error) { | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func formatBool(b bool) string { | ||
if b { | ||
return "true" | ||
} | ||
return "false" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should remove the /r/system from the name space here to prevent adding admin to the /r/system contracts