Skip to content

Commit

Permalink
Merge branch 'grc721Royalty' of https://github.com/linhpn99/gno into …
Browse files Browse the repository at this point in the history
…grc721Royalty
  • Loading branch information
linhpn99 committed May 10, 2024
2 parents ea00ba1 + 6cd4caf commit b811cd5
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 25 deletions.
6 changes: 6 additions & 0 deletions examples/gno.land/r/demo/tests/tests.gno
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ func CurrentRealmPath() string {
return std.CurrentRealmPath()
}

var initOrigCaller = std.GetOrigCaller()

func InitOrigCaller() std.Address {
return initOrigCaller
}

func AssertOriginCall() {
std.AssertOriginCall()
}
Expand Down
18 changes: 18 additions & 0 deletions examples/gno.land/r/x/map_delete/map_delete.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package mapdelete

var mapus map[uint64]string = make(map[uint64]string)

func init() {
mapus[3] = "three"
mapus[5] = "five"
mapus[9] = "nine"
}

func DeleteMap(k uint64) {
delete(mapus, k)
}

func GetMap(k uint64) bool {
_, exist := mapus[k]
return exist
}
21 changes: 21 additions & 0 deletions examples/gno.land/r/x/map_delete/map_delete_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package mapdelete

import (
"testing"
)

func TestGetMap(t *testing.T) {
if !(GetMap(3)) {
t.Error("Expected true, got ", GetMap(3))
}
}

func TestDeleteMap(t *testing.T) {
DeleteMap(3)
}

func TestGetMapAfterDelete(t *testing.T) {
if GetMap(3) {
t.Error("Expected false, got ", GetMap(3))
}
}
27 changes: 27 additions & 0 deletions gno.land/cmd/gnoland/testdata/initctx.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
loadpkg gno.land/r/foobar/bar $WORK/bar

## start a new node
gnoland start

# execute Render
gnokey maketx call -pkgpath gno.land/r/foobar/bar -func Render -args X -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout OK!
stdout '(" orig=g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 prev=g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" string)'
stdout 'OK!'

-- bar/bar.gno --
package bar

import "std"

var orig = std.Address("orig")
var prev = std.Address("prev")

func init() {
orig = std.GetOrigCaller()
prev = std.PrevRealm().Addr()
}

func Render(addr string) string {
return " orig="+orig.String()+" prev="+prev.String()
}
41 changes: 41 additions & 0 deletions gno.land/cmd/gnoland/testdata/map-delete.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
gnoland start

# add contract
gnokey maketx addpkg -pkgdir $WORK -pkgpath gno.land/r/demo/mapdelete -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout OK!

# delete map
gnokey maketx call -pkgpath gno.land/r/demo/mapdelete -func DeleteMap -args 3 -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout OK!

# check deletion
gnokey maketx call -pkgpath gno.land/r/demo/mapdelete -func GetMap -args 3 -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout OK!
stdout 'false bool'
# XXX without patching uverse.go, expected stdout is
# stdout 'true bool'



-- gno.mod --
module gno.land/r/demo/mapdelete

-- realm.gno --
package mapdelete

var mapus map[uint64]string = make(map[uint64]string)

func init() {
mapus[3] = "three"
mapus[5] = "five"
mapus[9] = "nine"
}

func DeleteMap(k uint64) {
delete(mapus, k)
}

func GetMap(k uint64) bool {
_, exist := mapus[k]
return exist
}
17 changes: 17 additions & 0 deletions gnovm/pkg/gnolang/uverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,24 @@ func UverseNode() *PackageNode {
switch cbt := baseOf(arg0.TV.T).(type) {
case *MapType:
mv := arg0.TV.V.(*MapValue)
val, ok := mv.GetValueForKey(m.Store, &itv)
if !ok {
return
}

// delete
mv.DeleteForKey(m.Store, &itv)

if m.Realm != nil {
// mark key as deleted
keyObj := itv.GetFirstObject(m.Store)
m.Realm.DidUpdate(mv, keyObj, nil)

// mark value as deleted
valObj := val.GetFirstObject(m.Store)
m.Realm.DidUpdate(mv, valObj, nil)
}

return
case *NativeType:
krv := reflect.New(cbt.Type.Key()).Elem()
Expand Down
3 changes: 1 addition & 2 deletions gnovm/pkg/gnolang/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -1543,8 +1543,7 @@ func (tv *TypedValue) ComputeMapKey(store Store, omitType bool) MapKey {
bz = append(bz, '{')
for i := 0; i < sl; i++ {
fv := fillValueTV(store, &sv.Fields[i])
ft := bt.Fields[i]
omitTypes := ft.Elem().Kind() != InterfaceKind
omitTypes := bt.Fields[i].Type.Kind() != InterfaceKind
bz = append(bz, fv.ComputeMapKey(store, omitTypes)...)
if i != sl-1 {
bz = append(bz, ',')
Expand Down
21 changes: 13 additions & 8 deletions gnovm/tests/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ func TestMachine(store gno.Store, stdout io.Writer, pkgPath string) *gno.Machine
}

func testMachineCustom(store gno.Store, pkgPath string, stdout io.Writer, maxAlloc int64, send std.Coins) *gno.Machine {
ctx := testContext(pkgPath, send)
m := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: "", // set later.
Output: stdout,
Store: store,
Context: ctx,
MaxAllocBytes: maxAlloc,
})
return m
}

func testContext(pkgPath string, send std.Coins) stdlibs.ExecContext {
// FIXME: create a better package to manage this, with custom constructors
pkgAddr := gno.DerivePkgAddr(pkgPath) // the addr of the pkgPath called.
caller := gno.DerivePkgAddr("user1.gno")
Expand All @@ -53,14 +65,7 @@ func testMachineCustom(store gno.Store, pkgPath string, stdout io.Writer, maxAll
Banker: banker,
EventLogger: sdk.NewEventLogger(),
}
m := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: "", // set later.
Output: stdout,
Store: store,
Context: ctx,
MaxAllocBytes: maxAlloc,
})
return m
return ctx
}

type runFileTestOptions struct {
Expand Down
23 changes: 23 additions & 0 deletions gnovm/tests/files/struct58.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

type I interface {
dummy()
}
type IS struct{}

func (iss IS) dummy() {
}

type S struct {
x int
I
}

func main() {
m := make(map[S]string)
m[S{0, IS{}}] = "test"
println(m)
}

// Output:
// map{(struct{(0 int),(struct{} main.IS)} main.S):("test" string)}
27 changes: 27 additions & 0 deletions gnovm/tests/files/zrealm_initctx.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// PKGPATH: gno.land/r/demo/tests_test
package tests_test

import (
"gno.land/r/demo/tests"
"std"
)

var addr = std.Address("test")
var addrInit = std.Address("addrInit")

func init() {
addr = std.GetOrigCaller()
addrInit = tests.InitOrigCaller()
}

func main() {
println(addr)
println(addrInit)
}

// Output:
// g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm
// g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm

// Realm:
// switchrealm["gno.land/r/demo/tests_test"]
Loading

0 comments on commit b811cd5

Please sign in to comment.