forked from gnolang/gno
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: properly mark array elements when an realm slice is updated (gno…
…lang#1305) Addresses gnolang#1167, gnolang#960, and gnolang#1170 Consider the following situation: - A slice of structs exists with a length of zero and a capacity of one - A new struct literal is appended to the slice - The code panics because the newly allocated struct literal was never marked as "new" ``` go package append import ( "gno.land/p/demo/ufmt" ) type T struct{ i int } var a []T func init() { a = make([]T, 0, 1) } func Append(i int) { a = append(a, T{i: i}) } ``` Invoking the `Append` function will cause a panic. The solution is to traverse each of the array elements after slice append assignment to make sure any new or updated elements are marked as such. This PR also includes a change to ensure that marking an object as dirty and managing references to the object are mutually exclusive. I think this is correct but am not sure. The changes include txtar test cases that incorporate the issue described by @tbruyelle in gnolang#1170 --------- Co-authored-by: jaekwon <[email protected]>
- Loading branch information
Showing
6 changed files
with
328 additions
and
16 deletions.
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,129 @@ | ||
# start a new node | ||
gnoland start | ||
|
||
gnokey maketx addpkg -pkgdir $WORK -pkgpath gno.land/r/append -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 | ||
stdout OK! | ||
|
||
# Call Append 1 | ||
gnokey maketx call -pkgpath gno.land/r/append -func Append -gas-fee 1000000ugnot -gas-wanted 2000000 -args '1' -broadcast -chainid=tendermint_test test1 | ||
stdout OK! | ||
|
||
gnokey maketx call -pkgpath gno.land/r/append -func AppendNil -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 | ||
stdout OK! | ||
|
||
# Call Append 2 | ||
gnokey maketx call -pkgpath gno.land/r/append -func Append -gas-fee 1000000ugnot -gas-wanted 2000000 -args '2' -broadcast -chainid=tendermint_test test1 | ||
stdout OK! | ||
|
||
# Call Append 3 | ||
gnokey maketx call -pkgpath gno.land/r/append -func Append -gas-fee 1000000ugnot -gas-wanted 2000000 -args '3' -broadcast -chainid=tendermint_test test1 | ||
stdout OK! | ||
|
||
# Call render | ||
gnokey maketx call -pkgpath gno.land/r/append -func Render -gas-fee 1000000ugnot -gas-wanted 2000000 -args '' -broadcast -chainid=tendermint_test test1 | ||
stdout '("1-2-3-" string)' | ||
stdout OK! | ||
|
||
# Call Pop | ||
gnokey maketx call -pkgpath gno.land/r/append -func Pop -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 | ||
stdout OK! | ||
|
||
# Call render | ||
gnokey maketx call -pkgpath gno.land/r/append -func Render -gas-fee 1000000ugnot -gas-wanted 2000000 -args '' -broadcast -chainid=tendermint_test test1 | ||
stdout '("2-3-" string)' | ||
stdout OK! | ||
|
||
# Call Append 42 | ||
gnokey maketx call -pkgpath gno.land/r/append -func Append -gas-fee 1000000ugnot -gas-wanted 2000000 -args '42' -broadcast -chainid=tendermint_test test1 | ||
stdout OK! | ||
|
||
# Call render | ||
gnokey maketx call -pkgpath gno.land/r/append -func Render -gas-fee 1000000ugnot -gas-wanted 2000000 -args '' -broadcast -chainid=tendermint_test test1 | ||
stdout '("2-3-42-" string)' | ||
stdout OK! | ||
|
||
gnokey maketx call -pkgpath gno.land/r/append -func CopyAppend -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 | ||
stdout OK! | ||
|
||
gnokey maketx call -pkgpath gno.land/r/append -func PopB -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 | ||
stdout OK! | ||
|
||
# Call render | ||
gnokey maketx call -pkgpath gno.land/r/append -func Render -gas-fee 1000000ugnot -gas-wanted 2000000 -args '' -broadcast -chainid=tendermint_test test1 | ||
stdout '("2-3-42-" string)' | ||
stdout OK! | ||
|
||
gnokey maketx call -pkgpath gno.land/r/append -func AppendMoreAndC -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 | ||
stdout OK! | ||
|
||
gnokey maketx call -pkgpath gno.land/r/append -func ReassignC -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 | ||
stdout OK! | ||
|
||
gnokey maketx call -pkgpath gno.land/r/append -func Render -gas-fee 1000000ugnot -gas-wanted 2000000 -args '' -broadcast -chainid=tendermint_test test1 | ||
stdout '("2-3-42-70-100-" string)' | ||
stdout OK! | ||
|
||
gnokey maketx call -pkgpath gno.land/r/append -func Render -gas-fee 1000000ugnot -gas-wanted 2000000 -args 'd' -broadcast -chainid=tendermint_test test1 | ||
stdout '("1-" string)' | ||
stdout OK! | ||
|
||
-- append.gno -- | ||
package append | ||
|
||
import ( | ||
"gno.land/p/demo/ufmt" | ||
) | ||
|
||
type T struct{ i int } | ||
|
||
var a, b, d []T | ||
var c = []T{{i: 100}} | ||
|
||
|
||
func init() { | ||
a = make([]T, 0, 1) | ||
} | ||
|
||
func Pop() { | ||
a = append(a[:0], a[1:]...) | ||
} | ||
|
||
func Append(i int) { | ||
a = append(a, T{i: i}) | ||
} | ||
|
||
func CopyAppend() { | ||
b = append(a, T{i: 50}, T{i: 60}) | ||
} | ||
|
||
func PopB() { | ||
b = append(b[:0], b[1:]...) | ||
} | ||
|
||
func AppendMoreAndC() { | ||
// Fill to capacity | ||
a = append(a, T{i: 70}) | ||
// Above capacity; make new array | ||
a = append(a, c...) | ||
} | ||
|
||
func ReassignC() { | ||
c[0] = T{i: 200} | ||
} | ||
|
||
func AppendNil() { | ||
d = append(d, a...) | ||
} | ||
|
||
func Render(path string) string { | ||
source := a | ||
if path == "d" { | ||
source = d | ||
} | ||
|
||
var s string | ||
for i:=0;i<len(source);i++{ | ||
s+=ufmt.Sprintf("%d-", source[i].i) | ||
} | ||
return s | ||
} |
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,114 @@ | ||
# Reproducible Test for https://github.com/gnolang/gno/issues/1167 | ||
|
||
gnoland start | ||
|
||
# add contract | ||
gnokey maketx addpkg -pkgdir $WORK -pkgpath gno.land/r/demo/xx -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 | ||
stdout OK! | ||
|
||
# execute New | ||
gnokey maketx call -pkgpath gno.land/r/demo/xx -func New -args X -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 | ||
stdout OK! | ||
|
||
# execute Delta for the first time | ||
gnokey maketx call -pkgpath gno.land/r/demo/xx -func Delta -args X -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 | ||
stdout OK! | ||
stdout '"1,1,1;" string' | ||
|
||
# execute Delta for the second time | ||
gnokey maketx call -pkgpath gno.land/r/demo/xx -func Delta -args X -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 | ||
stdout OK! | ||
stdout '1,1,1;2,2,2;" string' | ||
|
||
# execute Delta for the third time | ||
gnokey maketx call -pkgpath gno.land/r/demo/xx -func Delta -args X -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 | ||
stdout OK! | ||
stdout '1,1,1;2,2,2;3,3,3;" string' | ||
|
||
# execute Render | ||
gnokey maketx call -pkgpath gno.land/r/demo/xx -func Render -args X -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 | ||
stdout OK! | ||
stdout '1,1,1;2,2,2;3,3,3;" string' | ||
|
||
-- gno.mod -- | ||
module gno.land/r/demo/xx | ||
|
||
require ( | ||
gno.land/p/demo/avl v0.0.0-latest | ||
) | ||
|
||
-- realm.gno -- | ||
package xx | ||
|
||
import ( | ||
"strconv" | ||
|
||
"gno.land/p/demo/avl" | ||
) | ||
|
||
type Move struct { | ||
N1, N2, N3 byte | ||
} | ||
|
||
type Position struct { | ||
Moves []Move | ||
} | ||
|
||
func (p Position) clone() Position { | ||
mv := p.Moves | ||
return Position{Moves: mv} | ||
} | ||
|
||
func (oldp Position) update() Position { | ||
p := oldp.clone() | ||
|
||
counter++ | ||
// This is a workaround for the wrong behaviour (ie. uncomment this line): | ||
// p.Moves = append([]Move{}, p.Moves...) | ||
p.Moves = append(p.Moves, Move{counter, counter, counter}) | ||
return p | ||
} | ||
|
||
type Game struct { | ||
Position Position | ||
} | ||
|
||
var games avl.Tree // id -> *Game | ||
|
||
var counter byte | ||
|
||
func New(s string) string { | ||
// Bug shows if Moves has a cap > 0 when initialised. | ||
el := &Game{Position: Position{Moves: make([]Move, 0, 2)}} | ||
games.Set(s, el) | ||
return values(el.Position) | ||
} | ||
|
||
func Delta(s string) string { | ||
v, _ := games.Get(s) | ||
g, ok := v.(*Game) | ||
if !ok { | ||
panic("invalid game") | ||
} | ||
n := g.Position.update() | ||
g.Position = n | ||
ret := values(n) | ||
return ret | ||
} | ||
|
||
func Render(s string) string { | ||
v, _ := games.Get(s) | ||
g, ok := v.(*Game) | ||
if !ok { | ||
panic("invalid game") | ||
} | ||
return values(g.Position) | ||
} | ||
|
||
func values(x Position) string { | ||
s := "" | ||
for _, val := range x.Moves { | ||
s += strconv.Itoa(int(val.N1)) + "," + strconv.Itoa(int(val.N2)) + "," + strconv.Itoa(int(val.N3)) + ";" | ||
} | ||
return s | ||
} |
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
Oops, something went wrong.