Skip to content

Commit

Permalink
fix(gnovm): save object when refCount changed
Browse files Browse the repository at this point in the history
  • Loading branch information
omarsy committed Oct 26, 2024
1 parent 24f0a0d commit 2dfe9bf
Show file tree
Hide file tree
Showing 7 changed files with 587 additions and 0 deletions.
41 changes: 41 additions & 0 deletions gno.land/cmd/gnoland/testdata/issue_1543.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# test issue

loadpkg gno.land/r/demo/realm $WORK

# start a new node
gnoland start


gnokey maketx call -pkgpath gno.land/r/demo/realm --func Fill --args 0 --gas-fee 1000000ugnot --gas-wanted 2000000 --broadcast -chainid=tendermint_test test1
gnokey maketx call -pkgpath gno.land/r/demo/realm --func UnFill --args 0 --gas-fee 1000000ugnot --gas-wanted 2000000 --broadcast -chainid=tendermint_test test1
gnokey maketx call -pkgpath gno.land/r/demo/realm --func Fill --args 0 --gas-fee 1000000ugnot --gas-wanted 2000000 --broadcast -chainid=tendermint_test test1


-- realm.gno --
package main

type A struct {
A string
}
type B struct {
A *A
B string
}

var (
a = &A{A: "here"}
b [2]*B
)

func Fill(i int) {
c := B{
A: a,
B: "",
}
b[i] = &c
}

func UnFill(i int) {
b[i] = nil
}

42 changes: 42 additions & 0 deletions gno.land/cmd/gnoland/testdata/issue_2266.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# test issue

loadpkg gno.land/r/demo/realm $WORK

# start a new node
gnoland start


gnokey maketx call -pkgpath gno.land/r/demo/realm --func Fill --args 0 --gas-fee 1000000ugnot --gas-wanted 2000000 --broadcast -chainid=tendermint_test test1
gnokey maketx call -pkgpath gno.land/r/demo/realm --func Fill --args 1 --gas-fee 1000000ugnot --gas-wanted 2000000 --broadcast -chainid=tendermint_test test1
gnokey maketx call -pkgpath gno.land/r/demo/realm --func UnFill --gas-fee 1000000ugnot --gas-wanted 2000000 --broadcast -chainid=tendermint_test test1


-- realm.gno --
package main

type A struct {
A string
}
type B struct {
A *A
B string
}

var (
a = &A{A: "here"}
b [2]*B
)

func Fill(i int) {
c := B{
A: a,
B: "",
}
b[i] = &c
}

func UnFill() {
b[0] = nil
b[1] = nil
}

4 changes: 4 additions & 0 deletions gnovm/pkg/gnolang/realm.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ func (rlm *Realm) DidUpdate(po, xo, co Object) {
if xo.GetIsReal() {
rlm.MarkNewDeleted(xo)
}
} else {
rlm.MarkDirty(xo)
}
}
}
Expand Down Expand Up @@ -436,6 +438,7 @@ func (rlm *Realm) incRefCreatedDescendants(store Store, oo Object) {
child.SetIsNewReal(true)
}
} else if rc > 1 {
rlm.MarkDirty(child)
if child.GetIsEscaped() {
// already escaped, do nothing.
} else {
Expand Down Expand Up @@ -509,6 +512,7 @@ func (rlm *Realm) decRefDeletedDescendants(store Store, oo Object) {
if rc == 0 {
rlm.decRefDeletedDescendants(store, child)
} else if rc > 0 {
rlm.MarkDirty(child)
// do nothing
} else {
panic("deleted descendants should not have a reference count of less than zero")
Expand Down
20 changes: 20 additions & 0 deletions gnovm/tests/files/heap_item_value_init.gno
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,23 @@ func main() {
// }
// ]
// }
// u[a8ada09dee16d791fd406d629fe29bb0ed084a30:4]={
// "ObjectInfo": {
// "ID": "a8ada09dee16d791fd406d629fe29bb0ed084a30:4",
// "IsEscaped": true,
// "ModTime": "6",
// "OwnerID": "a8ada09dee16d791fd406d629fe29bb0ed084a30:2",
// "RefCount": "2"
// },
// "Value": {
// "T": {
// "@type": "/gno.RefType",
// "ID": "gno.land/r/test.S"
// },
// "V": {
// "@type": "/gno.RefValue",
// "Hash": "a05e5e1e2d2a27d94408a9325a58068e60b504df",
// "ObjectID": "a8ada09dee16d791fd406d629fe29bb0ed084a30:5"
// }
// }
// }
125 changes: 125 additions & 0 deletions gnovm/tests/files/zrealm13.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// PKGPATH: gno.land/r/test
package test

var (
a = &A{A: "here"}
b [2]*B
)


type A struct {
A string
}
type B struct {
A *A
B string
}

func init() {
c := B{
A: a,
B: "c",
}
b[0] = &c

d := B{
A: a,
B: "d",
}
b[1] = &d
}

func main() {
b[0] = nil
}


// Realm:
// switchrealm["gno.land/r/test"]
// u[a8ada09dee16d791fd406d629fe29bb0ed084a30:5]={
// "Data": null,
// "List": [
// {
// "T": {
// "@type": "/gno.PointerType",
// "Elt": {
// "@type": "/gno.RefType",
// "ID": "gno.land/r/test.B"
// }
// }
// },
// {
// "T": {
// "@type": "/gno.PointerType",
// "Elt": {
// "@type": "/gno.RefType",
// "ID": "gno.land/r/test.B"
// }
// },
// "V": {
// "@type": "/gno.PointerValue",
// "Base": {
// "@type": "/gno.RefValue",
// "Escaped": true,
// "ObjectID": "a8ada09dee16d791fd406d629fe29bb0ed084a30:7"
// },
// "Index": "1",
// "TV": null
// }
// }
// ],
// "ObjectInfo": {
// "ID": "a8ada09dee16d791fd406d629fe29bb0ed084a30:5",
// "ModTime": "9",
// "OwnerID": "a8ada09dee16d791fd406d629fe29bb0ed084a30:2",
// "RefCount": "1"
// }
// }
// u[a8ada09dee16d791fd406d629fe29bb0ed084a30:7]={
// "Blank": {},
// "ObjectInfo": {
// "ID": "a8ada09dee16d791fd406d629fe29bb0ed084a30:7",
// "IsEscaped": true,
// "ModTime": "9",
// "RefCount": "1"
// },
// "Parent": {
// "@type": "/gno.RefValue",
// "Escaped": true,
// "ObjectID": "a8ada09dee16d791fd406d629fe29bb0ed084a30:6"
// },
// "Source": {
// "@type": "/gno.RefNode",
// "BlockNode": null,
// "Location": {
// "Column": "1",
// "File": "main.gno",
// "Line": "18",
// "PkgPath": "gno.land/r/test"
// }
// },
// "Values": [
// {
// "T": {
// "@type": "/gno.RefType",
// "ID": "gno.land/r/test.B"
// },
// "V": {
// "@type": "/gno.RefValue",
// "Hash": "c22ccb7832b422c83fec9943b751cb134fcbed0b",
// "ObjectID": "a8ada09dee16d791fd406d629fe29bb0ed084a30:8"
// }
// },
// {
// "T": {
// "@type": "/gno.RefType",
// "ID": "gno.land/r/test.B"
// },
// "V": {
// "@type": "/gno.RefValue",
// "Hash": "86c916fd78da57d354cb38019923bf64c1a3471c",
// "ObjectID": "a8ada09dee16d791fd406d629fe29bb0ed084a30:9"
// }
// }
// ]
// }
114 changes: 114 additions & 0 deletions gnovm/tests/files/zrealm14.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// PKGPATH: gno.land/r/test
package test

var (
a = &A{A: "here"}
b [2]*B
)


type A struct {
A string
}
type B struct {
A *A
B string
}

func init() {
c := B{
A: a,
B: "c",
}
b[0] = &c

d := B{
A: a,
B: "d",
}
b[1] = &d
}

func main() {
b[0] = nil
b[1] = nil
}

// Realm:
// switchrealm["gno.land/r/test"]
// u[a8ada09dee16d791fd406d629fe29bb0ed084a30:5]={
// "Data": null,
// "List": [
// {
// "T": {
// "@type": "/gno.PointerType",
// "Elt": {
// "@type": "/gno.RefType",
// "ID": "gno.land/r/test.B"
// }
// }
// },
// {
// "T": {
// "@type": "/gno.PointerType",
// "Elt": {
// "@type": "/gno.RefType",
// "ID": "gno.land/r/test.B"
// }
// }
// }
// ],
// "ObjectInfo": {
// "ID": "a8ada09dee16d791fd406d629fe29bb0ed084a30:5",
// "ModTime": "9",
// "OwnerID": "a8ada09dee16d791fd406d629fe29bb0ed084a30:2",
// "RefCount": "1"
// }
// }
// u[a8ada09dee16d791fd406d629fe29bb0ed084a30:3]={
// "ObjectInfo": {
// "ID": "a8ada09dee16d791fd406d629fe29bb0ed084a30:3",
// "IsEscaped": true,
// "ModTime": "9",
// "RefCount": "1"
// },
// "Value": {
// "T": {
// "@type": "/gno.RefType",
// "ID": "gno.land/r/test.A"
// },
// "V": {
// "@type": "/gno.RefValue",
// "Hash": "95dca2f5b12899b6367402ecdac04c7ca59a03d9",
// "ObjectID": "a8ada09dee16d791fd406d629fe29bb0ed084a30:4"
// }
// }
// }
// u[a8ada09dee16d791fd406d629fe29bb0ed084a30:6]={
// "Blank": {},
// "ObjectInfo": {
// "ID": "a8ada09dee16d791fd406d629fe29bb0ed084a30:6",
// "IsEscaped": true,
// "ModTime": "9",
// "RefCount": "3"
// },
// "Parent": {
// "@type": "/gno.RefValue",
// "Escaped": true,
// "ObjectID": "a8ada09dee16d791fd406d629fe29bb0ed084a30:2"
// },
// "Source": {
// "@type": "/gno.RefNode",
// "BlockNode": null,
// "Location": {
// "Column": "1",
// "File": "main.gno",
// "Line": "2",
// "PkgPath": "gno.land/r/test"
// }
// },
// "Values": []
// }
// d[a8ada09dee16d791fd406d629fe29bb0ed084a30:7]
// d[a8ada09dee16d791fd406d629fe29bb0ed084a30:8]
// d[a8ada09dee16d791fd406d629fe29bb0ed084a30:9]
Loading

0 comments on commit 2dfe9bf

Please sign in to comment.