-
Notifications
You must be signed in to change notification settings - Fork 375
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
chore(demo): upgradable realm demo #3147
Open
mconcat
wants to merge
1
commit into
gnolang:master
Choose a base branch
from
gnoswap-labs:mconcat/upgradable-realm-demo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+215
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,15 @@ | ||
package admin | ||
|
||
import ( | ||
"std" | ||
) | ||
|
||
var ReadCounter func() uint64 = nil | ||
var UpdateCounter func() = nil | ||
var DebugPrevRealm func() string = nil | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
func DebugAdminPrevRealm() string { | ||
return std.PrevRealm().Addr().String() | ||
} |
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,5 @@ | ||
module gno.land/r/demo/upgradable/admin | ||
|
||
require ( | ||
|
||
) |
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,21 @@ | ||
package admin | ||
|
||
import ( | ||
"std" | ||
) | ||
|
||
type Logic interface { | ||
ReadCounter() uint64 | ||
UpdateCounter() | ||
|
||
DebugPrevRealm() string | ||
} | ||
|
||
func RegisterLogic(l Logic, setStore func(Store)) { | ||
ReadCounter = l.ReadCounter | ||
UpdateCounter = l.UpdateCounter | ||
DebugPrevRealm = l.DebugPrevRealm | ||
|
||
newStore := store() | ||
setStore(newStore) | ||
} |
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,15 @@ | ||
package admin | ||
|
||
type Store interface { | ||
GetCounter() uint64 | ||
SetCounter(value uint64) | ||
} | ||
|
||
var store func() Store = nil | ||
|
||
func RegisterStore(newStore func() Store) { | ||
if store != nil { | ||
panic("Store already registered") | ||
} | ||
store = newStore | ||
} |
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,29 @@ | ||
package v1 | ||
|
||
import ( | ||
"std" | ||
|
||
"gno.land/r/demo/upgradable/admin" | ||
) | ||
|
||
var store admin.Store = nil | ||
|
||
func Init() { | ||
admin.RegisterLogic(&logic{}, func(_store admin.Store) { store = _store }) | ||
} | ||
|
||
var _ admin.Logic = &logic{} | ||
|
||
type logic struct {} | ||
|
||
func (l *logic) ReadCounter() uint64 { | ||
return store.GetCounter() | ||
} | ||
|
||
func (l *logic) UpdateCounter() { | ||
store.SetCounter(store.GetCounter() + 1) | ||
} | ||
|
||
func (l *logic) DebugPrevRealm() string { | ||
return std.PrevRealm().Addr().String() | ||
} |
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,5 @@ | ||
module gno.land/r/demo/upgradable/logic/v1 | ||
|
||
require ( | ||
gno.land/r/demo/upgradable/admin v0.0.0-latest | ||
) |
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,29 @@ | ||
package v2 | ||
|
||
import ( | ||
"std" | ||
|
||
"gno.land/r/demo/upgradable/admin" | ||
) | ||
|
||
var store admin.Store = nil | ||
|
||
func Init() { | ||
admin.RegisterLogic(&logic{}, func(_store admin.Store) { store = _store }) | ||
} | ||
|
||
var _ admin.Logic = &logic{} | ||
|
||
type logic struct {} | ||
|
||
func (l *logic) ReadCounter() uint64 { | ||
return store.GetCounter() | ||
} | ||
|
||
func (l *logic) UpdateCounter() { | ||
store.SetCounter(store.GetCounter() + 2) | ||
} | ||
|
||
func (l *logic) DebugPrevRealm() string { | ||
return std.PrevRealm().Addr().String() | ||
} |
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,5 @@ | ||
module gno.land/r/demo/upgradable/logic/v2 | ||
|
||
require ( | ||
gno.land/r/demo/upgradable/admin v0.0.0-latest | ||
) |
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,5 @@ | ||
module gno.land/r/demo/upgradable/store | ||
|
||
require ( | ||
gno.land/r/demo/upgradable/admin v0.0.0-latest | ||
) |
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,42 @@ | ||
package store | ||
|
||
import ( | ||
"std" | ||
"fmt" | ||
|
||
"gno.land/r/demo/upgradable/admin" | ||
) | ||
|
||
var counterValue uint64 | ||
|
||
var _ admin.Store = &store{} | ||
|
||
var currentStore *store | ||
|
||
type store struct {} | ||
|
||
func (s *store) GetCounter() uint64 { | ||
if s != currentStore { | ||
panic("Revoked store") | ||
} | ||
|
||
return counterValue | ||
} | ||
|
||
func (s *store) SetCounter(value uint64) { | ||
if s != currentStore { | ||
panic("Revoked store") | ||
} | ||
|
||
counterValue = value | ||
} | ||
|
||
func newStore() admin.Store { | ||
currentStore = &store{} | ||
|
||
return currentStore | ||
} | ||
|
||
func Init() { | ||
admin.RegisterStore(newStore) | ||
} |
8 changes: 8 additions & 0 deletions
8
examples/gno.land/r/demo/upgradable/upgradable_testing/gno.mod
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,8 @@ | ||
module gno.land/r/demo/upgradable/upgradable_testing | ||
|
||
require ( | ||
gno.land/r/demo/upgradable/admin v0.0.0-latest | ||
gno.land/r/demo/upgradable/store v0.0.0-latest | ||
gno.land/r/demo/upgradable/logic/v1 v0.0.0-latest | ||
gno.land/r/demo/upgradable/logic/v2 v0.0.0-latest | ||
) |
1 change: 1 addition & 0 deletions
1
examples/gno.land/r/demo/upgradable/upgradable_testing/upgradable.gno
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 @@ | ||
package upgradable_testing |
35 changes: 35 additions & 0 deletions
35
examples/gno.land/r/demo/upgradable/upgradable_testing/upgradable_test.gno
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,35 @@ | ||
package upgradable_testing | ||
|
||
import ( | ||
"std" | ||
"testing" | ||
|
||
"gno.land/p/demo/testutils" | ||
"gno.land/p/demo/urequire" | ||
|
||
"gno.land/r/demo/upgradable/admin" | ||
"gno.land/r/demo/upgradable/logic/v1" | ||
"gno.land/r/demo/upgradable/logic/v2" | ||
"gno.land/r/demo/upgradable/store" | ||
|
||
) | ||
|
||
func TestPackage(t *testing.T) { | ||
alice := testutils.TestAddress("alice") | ||
std.TestSetRealm(std.NewUserRealm(alice)) | ||
std.TestSetOrigCaller(alice) // XXX: should not need this | ||
|
||
store.Init() | ||
|
||
v1.Init() | ||
urequire.Equal(t, admin.ReadCounter(), uint64(0)) | ||
admin.UpdateCounter() | ||
urequire.Equal(t, admin.ReadCounter(), uint64(1)) | ||
|
||
v2.Init() | ||
urequire.Equal(t, admin.ReadCounter(), uint64(1)) | ||
admin.UpdateCounter() | ||
urequire.Equal(t, admin.ReadCounter(), uint64(3)) | ||
|
||
urequire.Equal(t, admin.DebugPrevRealm(), admin.DebugAdminPrevRealm()) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should consolidate all the upgrade demos in one location, and use distinctive names. If you'd like, I can move my other demos to a different folder. Ideally, we should also maintain a shared README that outlines the differences in each implementation; I believe I have already created one.