Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
leohhhn committed Dec 18, 2024
1 parent e1f8e58 commit c4098d9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
9 changes: 7 additions & 2 deletions examples/gno.land/r/sys/users/store.gno
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ var (
aliasStore = avl.NewTree() // username > address
callerWhitelist = avl.NewTree() // addr > struct{}{}

reAddressLookalike = regexp.MustCompile(`^g1[a-z0-9]{38}$`)
reAddressLookalike = regexp.MustCompile(`^g1[a-z0-9]{20,38}$`)
)

const maxUsernameLen = 50
const (
maxUsernameLen = 50
WriteUserEvent = "WriteUser"
)

// WriteUser writes a *new* user to the store
// Only whitelisted callers can call this function
Expand Down Expand Up @@ -53,6 +56,8 @@ func WriteUser(name string, addr std.Address) error {
usernameStore.Set(trimmedName, addr)
addressStore.Set(addr.String(), trimmedName)

std.Emit(WriteUserEvent, "name", name, "address", addr.String())

return nil
}

Expand Down
29 changes: 8 additions & 21 deletions examples/gno.land/r/sys/users/users.gno
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package users

import (
"github.com/gnolang/gno/examples/gno.land/p/demo/avl/rotree"
"github.com/gnolang/gno/examples/gno.land/p/sys/users"
"std"
"strings"

"gno.land/p/demo/avl"
"gno.land/p/demo/avlhelpers"
"gno.land/p/sys/users"
)

func ResolveName(username string) users.UserLookup {
Expand Down Expand Up @@ -54,6 +54,10 @@ func ResolveAddressOrName(aon string) users.UserLookup {
return ResolveAddress(std.Address(aon))
}

func GetReadonlyStore() *rotree.ReadOnlyTree {
return rotree.Wrap(usernameStore, nil)
}

// Users returns a paginated list of user lookups
// Looks users up by username; does not look up aliases
func Users(offset, count uint64) []users.UserLookup {
Expand All @@ -64,21 +68,14 @@ func Users(offset, count uint64) []users.UserLookup {
res := make([]users.UserLookup, 0, count)

usernameStore.IterateByOffset(int(offset), int(count), func(key string, value interface{}) bool {
ul := users.UserLookup{
Name: key,
Addr: value.(std.Address),
Alias: false,
}

res = append(res, ul)

res = append(res, users.NewUserLookup(key, value.(std.Address), false))
return false
})

return res
}

// GetAllNames gets all names for a specific address
// GetAllNames gets the name and all aliases for a specific address
func GetAllNames(addr std.Address) []string {
var res []string

Expand Down Expand Up @@ -108,13 +105,3 @@ func UsersByPrefix(prefix string, maxResults int) []string {
// in that case, should we get a value copy of the uStore available to whitelisted callers?
return avlhelpers.ListByteStringKeysByPrefix(*usernameStore, prefix, maxResults)
}

// UserCount returns the number of registered users
func UserCount() int {
return addressStore.Size()
}

// XXX: Should we have this? it's not modifiable directly.
func GetUserStore() *avl.Tree {
return usernameStore
}

0 comments on commit c4098d9

Please sign in to comment.