Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gnovm): load map from state #932

Merged
merged 3 commits into from
Jun 27, 2023
Merged

fix(gnovm): load map from state #932

merged 3 commits into from
Jun 27, 2023

Conversation

tbruyelle
Copy link
Contributor

@tbruyelle tbruyelle commented Jun 26, 2023

Fix #817

There was actually 2 bugs in the code responsible for loading a map from the state:

  • UnmarshalAmino doesn't check that ml.Tail is nil before accessing it. It is correctly done in MapList.Append for instance. The fix just ensures that ml.Tail is not nil.
  • fillTypesOfValue which is called just after the store data is unmarshalSized, resets the map data by calling MapValue.MakeMap(). So at the end the map is always empty, whatever was stored before the transaction. The fix replaces the call of MakeMap to a simple map creation.

This has to be confirmed, but this change may also fix #311, because I'm not convinced that the indetermenistic behavior of the golang map can be a problem when saved to the store. Indeed, a gno map is serialized with the help of a slice, which should remove any indetermenistic problem.

Contributors Checklist

  • Added new tests, or not needed, or not feasible
  • Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory
  • Updated the official documentation or not needed
  • No breaking changes were made, or a BREAKING CHANGE: xxx message was included in the description
  • Added references to related issues and PRs
  • Provided any useful hints for running manual tests
  • Added new benchmarks to generated graphs, if any. More info here.

Maintainers Checklist

  • Checked that the author followed the guidelines in CONTRIBUTING.md
  • Checked the conventional-commit (especially PR title and verb, presence of BREAKING CHANGE: in the body)
  • Ensured that this PR is not a significant change or confirmed that the review/consideration process was appropriate for the change

Fix gnolang#817

There was actually 2 bugs in the code responsible for loading a map from
the state:
- `UnmarshalAmino` doesn't check that `ml.Tail` is nil before accessing
  it. It is correctly done for instance in `MapList.Append` for
  instance. The fix just ensures that `ml.Tail` is not nil.
- `fillTypesOfValue` which is called just after the store data is
  unmarshalSized, resets the map data by calling `MapValue.MakeMap()`.
  So at the end the map is always empty, whatever was stored before the
  transaction. The fix replaces the call of `MakeMap` to a simple map
  creation.

This has to be confirmed, but this change may also fix gnolang#311, because I'm
not convinced that the indetermenistic behavior of the golang map can be
a problem when saved to the store. Indeed, a gno map is serialized with
the help of a slice, which should remove any indetermenistic problem.
Copy link
Member

@moul moul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, better than my own version of the same fix 👍

@moul
Copy link
Member

moul commented Jun 27, 2023

Can you update the docs/ folder which says that maps are temporarily not persisted?

@tbruyelle
Copy link
Contributor Author

Can you update the docs/ folder which says that maps are temporarily not persisted?

@moul sure, done here 7dccbba

@moul moul merged commit d684985 into gnolang:master Jun 27, 2023
@piux2
Copy link
Contributor

piux2 commented Jul 5, 2023

@tbruyelle confirmed #311 is fixed

@tbruyelle tbruyelle deleted the fix/map branch July 28, 2023 20:25
Doozers pushed a commit to Doozers/gno that referenced this pull request Aug 31, 2023
@r3v4s r3v4s mentioned this pull request Sep 12, 2023
8 tasks
thehowl pushed a commit that referenced this pull request Mar 4, 2024
<!-- please provide a detailed description of the changes made in this
pull request. -->

```go
package mapindex

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

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

func FindMapWithKey(k uint64) string {
	return mapus[k]
}

```


`println(mapus[5])` will return 'five' in test cases run by `gno test`
which is expected

HOWEVER, with deployed package
```bash
$ gnokey maketx addpkg \
-remote 127.0.0.1:26657 \
-broadcast=true \
-chainid dev \
-gas-fee 1ugnot \
-gas-wanted 9000000 \
-memo "" \
-pkgdir ./ \
-pkgpath gno.land/r/demo/mapindex \
test1

```

calling
```bash
$ gnokey maketx call \
-remote 127.0.0.1:26657 \
-broadcast=true \
-chainid dev \
-gas-fee 1ugnot \
-gas-wanted 9000000 \
-memo "" \
-pkgpath gno.land/r/demo/mapindex \
-func FindMapWithKey \
-args "3" \
test1
```

will return nothing for value, and 'string' for type
```bash
$ gnokey maketx call \
-remote 127.0.0.1:26657 \
-broadcast=true \
-chainid dev \
-gas-fee 1ugnot \
-gas-wanted 9000000 \
-memo "" \
-pkgpath gno.land/r/demo/mapindex \
-func FindMapWithKey \
-args "3" \
test1
Enter password.

( string)
OK!
GAS WANTED: 9000000
GAS USED:   73301
```

## Related PR
- [x] #932 @tbruyelle 


<details><summary>Contributors' checklist...</summary>

- [x] Added new tests, or not needed, or not feasible
- [x] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [x] Updated the official documentation or not needed
- [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [x] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>
leohhhn pushed a commit to leohhhn/gno that referenced this pull request Mar 6, 2024
<!-- please provide a detailed description of the changes made in this
pull request. -->

```go
package mapindex

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

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

func FindMapWithKey(k uint64) string {
	return mapus[k]
}

```


`println(mapus[5])` will return 'five' in test cases run by `gno test`
which is expected

HOWEVER, with deployed package
```bash
$ gnokey maketx addpkg \
-remote 127.0.0.1:26657 \
-broadcast=true \
-chainid dev \
-gas-fee 1ugnot \
-gas-wanted 9000000 \
-memo "" \
-pkgdir ./ \
-pkgpath gno.land/r/demo/mapindex \
test1

```

calling
```bash
$ gnokey maketx call \
-remote 127.0.0.1:26657 \
-broadcast=true \
-chainid dev \
-gas-fee 1ugnot \
-gas-wanted 9000000 \
-memo "" \
-pkgpath gno.land/r/demo/mapindex \
-func FindMapWithKey \
-args "3" \
test1
```

will return nothing for value, and 'string' for type
```bash
$ gnokey maketx call \
-remote 127.0.0.1:26657 \
-broadcast=true \
-chainid dev \
-gas-fee 1ugnot \
-gas-wanted 9000000 \
-memo "" \
-pkgpath gno.land/r/demo/mapindex \
-func FindMapWithKey \
-args "3" \
test1
Enter password.

( string)
OK!
GAS WANTED: 9000000
GAS USED:   73301
```

## Related PR
- [x] gnolang#932 @tbruyelle 


<details><summary>Contributors' checklist...</summary>

- [x] Added new tests, or not needed, or not feasible
- [x] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [x] Updated the official documentation or not needed
- [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [x] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 🤖 gnovm Issues or PRs gnovm related
Projects
Status: 🚀 Needed for Launch
Archived in project
3 participants