Nothing Special   »   [go: up one dir, main page]

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

feat: add grc20reg that works... today #3135

Open
wants to merge 20 commits into
base: master
Choose a base branch
from

Conversation

moul
Copy link
Member
@moul moul commented Nov 16, 2024
  • Switch to storing a type XXX func() grc20.Token instead of a grc20.Token directly.
  • Implement grc20reg.
  • Add new tests in gnovm/tests to demonstrate the current VM's management of the cross-realm feature and support potential changes in Gno Realm and Ownership Spec #2743.
  • Create a demo in atomicswap or a similar application. (feat: add r/demo/atomicswap #2510 (comment))
  • Try using a Token.Getter() helper. (Works! f99654e)
  • Demonstrate how to manage "disappearing" functions during garbage collection by checking if the function pointer is nil or non-resolvable.

Alternative to #2516
NOT(!) depending on #2743

Signed-off-by: moul <94029+moul@users.noreply.github.com>
@moul moul self-assigned this Nov 16, 2024
@github-actions github-actions bot added the 🧾 package/realm Tag used for new Realms or Packages. label Nov 16, 2024
Signed-off-by: moul <94029+moul@users.noreply.github.com>
Copy link
codecov bot commented Nov 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 63.75%. Comparing base (a1a7cb3) to head (db1e4a6).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3135      +/-   ##
==========================================
- Coverage   63.79%   63.75%   -0.05%     
==========================================
  Files         549      549              
  Lines       78819    79449     +630     
==========================================
+ Hits        50281    50651     +370     
- Misses      25146    25380     +234     
- Partials     3392     3418      +26     
Flag Coverage Δ
contribs/gnodev 61.16% <ø> (ø)
contribs/gnofaucet 14.82% <ø> (ø)
gno.land 73.70% <ø> (ø)
gnovm 67.95% <ø> (+0.01%) ⬆️
misc/genstd 79.72% <ø> (ø)
tm2 62.34% <ø> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

This was referenced Nov 16, 2024
Signed-off-by: moul <94029+moul@users.noreply.github.com>
Signed-off-by: moul <94029+moul@users.noreply.github.com>
Signed-off-by: moul <94029+moul@users.noreply.github.com>
Signed-off-by: moul <94029+moul@users.noreply.github.com>
Signed-off-by: moul <94029+moul@users.noreply.github.com>
Signed-off-by: moul <94029+moul@users.noreply.github.com>
@github-actions github-actions bot added the 📦 🤖 gnovm Issues or PRs gnovm related label Nov 16, 2024
Signed-off-by: moul <94029+moul@users.noreply.github.com>
Signed-off-by: moul <94029+moul@users.noreply.github.com>
Signed-off-by: moul <94029+moul@users.noreply.github.com>
Signed-off-by: moul <94029+moul@users.noreply.github.com>
Signed-off-by: moul <94029+moul@users.noreply.github.com>
Signed-off-by: moul <94029+moul@users.noreply.github.com>
Signed-off-by: moul <94029+moul@users.noreply.github.com>
Signed-off-by: moul <94029+moul@users.noreply.github.com>
Signed-off-by: moul <94029+moul@users.noreply.github.com>
Signed-off-by: moul <94029+moul@users.noreply.github.com>
Signed-off-by: moul <94029+moul@users.noreply.github.com>
@moul moul mentioned this pull request Nov 16, 2024
5 tasks
@dongwon8247
Copy link
Member

@onlyhyde @notJoon @r3v4s

@moul moul marked this pull request as ready for review November 16, 2024 10:15
@moul
Copy link
Member Author
moul commented Nov 16, 2024

It seems to work, unless we consider the current behavior a bug that we want to fix. In that case, we can create gno object registries by avoiding the storage of remotely defined objects. Instead, we can store remotely defined functions that dynamically return the remote object when needed.

I’m not a big fan of this indirection because I believe Go interfaces are excellent. However, since we fully preserve Go's type safety, I think it’s a good compromise that allows for greater independence between realms.

The last checkbox in the original comment raises an open question about how we plan to manage realm deprovisionning in the future. One idea I have that would avoid the need to track which remote realms are storing a locally defined function (remote backref). We could implement a special rule in the VM so that if you:

  1. Store a remotely defined function,
  2. Deprovision the remote realm,
  3. Attempt to call the locally stored (now dead) function, instead of panicking, we could simulate that the pointer is now nil at runtime.

This would mean we only need to check if the pointer is nil to manage custom error handling, or we could allow the VM to panic due to the missing pointer.

@n0izn0iz
Copy link
Contributor
n0izn0iz commented Nov 16, 2024

Did you try to call mutating functions from the calling realm?
The tests seem to be read-only

@moul
Copy link
Member Author
moul commented Nov 16, 2024

I have some tests that perform mutations. However, if you want an advanced case, check out #2510 (comment).

@n0izn0iz
Copy link
Contributor
n0izn0iz commented Nov 16, 2024

Can you link the mutation tests pls? I can't find them

On the atomic swap branch, the swap with registered token is not tested currently.
I added the test and it doesn't work

PR: moul#21
Test result:

❯ go run ./gnovm/cmd/gno test ./examples/gno.land/r/demo/atomicswap                                                                                                                 Native
--- FAIL: TestNewGRC20Registered_Claim (0.00s)
uassert.Equal: same type but different value
	expected: 60000
	actual:   30000
uassert.Equal: same type but different value
	expected: 70000
	actual:   0
uassert.Equal: same type but different value
	expected: 60000
	actual:   30000
uassert.Equal: same type but different value
	expected: 140000
	actual:   70000
./examples/gno.land/r/demo/atomicswap: test pkg: failed: "TestNewGRC20Registered_Claim"
FAIL
FAIL    ./examples/gno.land/r/demo/atomicswap 	1.17s
FAIL
FAIL
FAIL: 0 build errors, 1 test errors
exit status 1

Just FYI we have this pattern on teritori repo for a long time but mutations never worked (https://github.com/TERITORI/teritori-dapp/blob/main/gno/r/dao_registry/dao_registry.gno)

@moul
Copy link
Member Author
moul commented Nov 17, 2024

Here's one: https://github.com/gnolang/gno/pull/3135/files#diff-57ef3b955ce6dd26e824d52cfa9fd0d84e2b4f67e50abdd3f5d117458a14e59cR15

You're right; the atomic swap wasn't tested, and I forgot to remove a "panic not implemented" blocker. However, the test output you shared isn't related to mutation. It's due to reusing the same "sender" and "recipient" addresses in independent tests. In other words, if you run a single unit test with -run, it works, but running everything together does not. Here is a commit that makes the sender and recipient independent for each test: 3a5acf1.

And here a21ddd5 is the commit that removes the panic and adds the missing tests for atomic swap. It works.

$>  gno test -v ./examples/gno.land/r/demo/atomicswap
=== RUN   TestNewCustomCoinSwap_Claim
--- PASS: TestNewCustomCoinSwap_Claim (0.00s)
=== RUN   TestNewCustomCoinSwap_Refund
--- PASS: TestNewCustomCoinSwap_Refund (0.00s)
=== RUN   TestNewCustomGRC20Swap_Claim
--- PASS: TestNewCustomGRC20Swap_Claim (0.00s)
=== RUN   TestNewCustomGRC20Swap_Refund
--- PASS: TestNewCustomGRC20Swap_Refund (0.00s)
=== RUN   TestNewGRC20Swap_Claim
--- PASS: TestNewGRC20Swap_Claim (0.00s)
=== RUN   TestNewGRC20Swap_Refund
--- PASS: TestNewGRC20Swap_Refund (0.00s)
=== RUN   TestRender
--- PASS: TestRender (0.00s)
ok      ./examples/gno.land/r/demo/atomicswap   1.44s

@Kouteki Kouteki added this to the 🚀 Mainnet launch milestone Nov 18, 2024
@Kouteki Kouteki added the in focus Core team is prioritizing this work label Nov 18, 2024
examples/gno.land/r/demo/grc20reg/grc20reg.gno Outdated Show resolved Hide resolved
Copy link
Member
@notJoon notJoon left a comment

Choose a reason for hiding this comment

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

It looks valid to me 👍

@ltzmaxwell
Copy link
Contributor
ltzmaxwell commented Nov 21, 2024
  1. this pattern is no different with this, except for the addition of Getter for convenience, right?
// PKGPATH: gno.land/r/crossrealm_test
package crossrealm_test

import (
	"std"

	crossrealm "gno.land/r/demo/tests/crossrealm"
)

type fooer struct {
	v string
}

func (f *fooer) Foo() { println("hello " + f.v + " " + std.CurrentRealm().PkgPath()) }

func (f *fooer) Mutate() {
	f.v = "b"
}

var f *fooer

func init() {
	f = &fooer{v: "a"} // attach first
}

func main() {
	crossrealm.SetFooer(f)
	crossrealm.CallFooerFoo()

	crossrealm.MutateFooer()
	crossrealm.CallFooerFoo()
}

// Output:
// hello a gno.land/r/crossrealm_test
// hello b gno.land/r/crossrealm_test
  1. There are underlying bugs that we need to fix (though they do not block the Getter pattern):
// PKGPATH: gno.land/r/crossrealm_test
package crossrealm_test

import (
	"std"

	crossrealm "gno.land/r/demo/tests/crossrealm"
)

type fooer struct {
	s string
}

func (f *fooer) Foo() {
	println("hello " + f.s + " " + std.CurrentRealm().PkgPath())
}

func (f *fooer) Mutate() {}

func init() {
	f := &fooer{s: "B"} // XXX, note this is incorrectly attached to "r/demo/crossrealm", by the next line.
	fg := func() crossrealm.Fooer { return f }
	crossrealm.SetFooerGetter(fg)
	crossrealm.CallFooerGetterFoo()

	f.s = "C" // XXX, so this cause a panic while modifying external realm obejct
	crossrealm.CallFooerGetterFoo()
}

func main() {
	print(".")
}

// Error:
// cannot modify external-realm or non-realm object

Copy link
Contributor
@ltzmaxwell ltzmaxwell left a comment

Choose a reason for hiding this comment

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

LGTM.

}

// Error:
// gno.land/r/crossrealm_test/main.gno:19:2: name CallFoo not declared
Copy link
Contributor

Choose a reason for hiding this comment

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

This error message doesn’t seem intended for this pattern. Consider modifying the code or simply removing the message entirely to eliminate any ambiguity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in focus Core team is prioritizing this work 📦 🤖 gnovm Issues or PRs gnovm related 🧾 package/realm Tag used for new Realms or Packages.
Projects
Status: 📥 Inbox
Status: In Review
Development

Successfully merging this pull request may close these issues.

7 participants