Skip to content

Commit

Permalink
add rotree test
Browse files Browse the repository at this point in the history
  • Loading branch information
leohhhn committed Feb 12, 2025
1 parent 92ffb4d commit a5a84af
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
14 changes: 4 additions & 10 deletions examples/gno.land/r/sys/users/admin.gno
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ const gusersv1 = "gno.land/r/gnoland/users/v1" // preregistered with store write
var controllers = addrset.Set{} // caller whitelist

func init() {
callerWhitelist.Add(std.DerivePkgAddr(gusersv1)) // initially whitelisted
}

// IsOnWhitelist checks if the given address has
// permission to write to the user store
func IsOnWhitelist(addr std.Address) bool {
return callerWhitelist.Has(addr)
controllers.Add(std.DerivePkgAddr(gusersv1)) // initially whitelisted
}

// NewAddWhitelistedAddrExecutor allows GovDAO to add a whitelisted caller
Expand All @@ -44,11 +38,11 @@ func NewDeleteWhitelistedAddrExecutor(addr std.Address) dao.Executor {
// Helpers

func deleteFromwhitelist(addr std.Address) error {
if !callerWhitelist.Has(addr) {
if !controllers.Has(addr) {
return ErrNotWhitelisted
}

if ok := callerWhitelist.Remove(addr); !ok {
if ok := controllers.Remove(addr); !ok {
panic("failed to remove address from whitelist")
}

Expand All @@ -60,7 +54,7 @@ func addToWhitelist(newCaller std.Address) error {
return ErrInvalidAddress
}

if !callerWhitelist.Add(newCaller) {
if !controllers.Add(newCaller) {
return ErrAlreadyWhitelisted
}

Expand Down
6 changes: 3 additions & 3 deletions examples/gno.land/r/sys/users/store.gno
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (u UserData) IsDeleted() bool {
// RegisterUser adds a new user to the system.
func RegisterUser(name string, address std.Address) error {
// Validate caller
if !IsOnWhitelist(std.PrevRealm().Addr()) {
if !controllers.Has(std.PrevRealm().Addr()) {
return ErrNotWhitelisted
}

Expand Down Expand Up @@ -101,7 +101,7 @@ func (u *UserData) UpdateName(newName string) error {
}

// Validate caller
if !IsOnWhitelist(std.PrevRealm().Addr()) {
if !controllers.Has(std.PrevRealm().Addr()) {
return ErrNotWhitelisted
}

Expand Down Expand Up @@ -129,7 +129,7 @@ func (u *UserData) Delete() error {
}

// Validate caller
if !IsOnWhitelist(std.PrevRealm().Addr()) {
if !controllers.Has(std.PrevRealm().Addr()) {
return ErrNotWhitelisted
}

Expand Down
2 changes: 1 addition & 1 deletion examples/gno.land/r/sys/users/users.gno
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func GetReadOnlyNameStore() *rotree.ReadOnlyTree {
}

func makeUserDataSafe(data interface{}) interface{} {
u := *(data.(*UserData))
u := data.(*UserData) // WARN: roTree will return ok = true when getting in this case
if u.deleted {
return nil
}
Expand Down
47 changes: 47 additions & 0 deletions examples/gno.land/r/sys/users/users_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,50 @@ func TestResolveAddress(t *testing.T) {
uassert.Equal(t, "alice4", res.Name())
})
}

func TestROStores(t *testing.T) {
std.TestSetOrigCaller(whitelistedCallerAddr)
cleanStore(t)

urequire.NoError(t, RegisterUser(alice, aliceAddr))
roNS := GetReadOnlyNameStore()
roAS := GetReadonlyAddrStore()

t.Run("get user data", func(t *testing.T) {
// Name store
aliceDataRaw, ok := roNS.Get(alice)
uassert.True(t, ok)

_, ok = aliceDataRaw.(*UserData)
uassert.True(t, ok, "Could not cast data from RO tree to UserData")

// Addr store
aliceDataRaw, ok = roAS.Get(aliceAddr.String())
uassert.True(t, ok)

_, ok = aliceDataRaw.(*UserData)
uassert.True(t, ok, "Could not cast data from RO tree to UserData")
})

t.Run("get deleted data", func(t *testing.T) {
raw, _ := nameStore.Get(alice)
aliceData := raw.(*UserData)

urequire.NoError(t, aliceData.Delete())
urequire.True(t, aliceData.IsDeleted())

// Should be nil because of makeSafeFn
rawRoData, ok := roNS.Get(alice)

// uassert.False(t, ok)
// XXX: not sure what to do here, as the tree has the data so returns ok
// However the data is intercepted and something else (nilin this case) is returned.
// should we handle this somehow?

uassert.Equal(t, rawRoData, nil)

_, ok = rawRoData.(*UserData) // shouldn't be castable
uassert.False(t, ok)
})

}

0 comments on commit a5a84af

Please sign in to comment.