-
Notifications
You must be signed in to change notification settings - Fork 10
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: added migration & ownable #166
Open
leohhhn
wants to merge
4
commits into
main
Choose a base branch
from
feat/migrate
base: main
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 chess | ||
|
||
func checkMigrated() { | ||
if migrated { | ||
msg := "realm has migrated to " + migratedTo | ||
panic(msg) | ||
} | ||
} | ||
|
||
func Migrate(newRealmPath string) string { | ||
if newRealmPath == "" { | ||
panic("new realm path cannot be empty") | ||
} | ||
|
||
if !isOwner() { | ||
panic("not owner") | ||
} | ||
|
||
migrated = true | ||
migratedTo = newRealmPath | ||
return "successfully migrated to " + newRealmPath | ||
} | ||
|
||
func GetMigration() string { | ||
if migratedTo == "" { | ||
return "realm has not migrated" | ||
} | ||
return migratedTo | ||
} |
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,46 @@ | ||
package chess | ||
|
||
import ( | ||
"std" | ||
"testing" | ||
) | ||
|
||
func TestMigrate(t *testing.T) { | ||
std.TestSetOrigCaller("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm") | ||
if migrated != false || migratedTo != "" { | ||
t.Fatal("Bad init") | ||
} | ||
|
||
Migrate("gno.land/r/demo/chessV2") | ||
|
||
if migrated != true || migratedTo != "gno.land/r/demo/chessV2" { | ||
t.Fatal("Migration failed") | ||
} | ||
} | ||
|
||
func TestMigrate_EmptyPath(t *testing.T) { | ||
std.TestSetOrigCaller("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm") | ||
|
||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Error("Expected Migrate to panic with an empty path, but it didn't") | ||
} | ||
}() | ||
|
||
Migrate("") | ||
} | ||
|
||
func TestMigrate_BlockedFunction(t *testing.T) { | ||
std.TestSetOrigCaller("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm") | ||
|
||
Migrate("example/path") | ||
|
||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Error("Expected ClaimTimeout to panic with migrated realm, but did not") | ||
} | ||
}() | ||
|
||
// example blocked function | ||
ClaimTimeout("exampleGameID") | ||
} |
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,30 @@ | ||
package chess | ||
|
||
import "std" | ||
|
||
// TransferOwnership | ||
// Warning: Use carefully - if owner is misconfigured, realm will be bricked | ||
func TransferOwnership(newOwner std.Address) string { | ||
if newOwner == "" { | ||
panic("new owner cannot be empty") | ||
} | ||
|
||
if !isOwner() { | ||
panic("not owner") | ||
} | ||
|
||
owner = newOwner | ||
return "new owner is " + string(owner) | ||
} | ||
|
||
func isOwner() bool { | ||
caller := std.GetOrigCaller() | ||
if caller != GetOwner() { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func GetOwner() std.Address { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this exported? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So that it can be called by |
||
return owner | ||
} |
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,47 @@ | ||
package chess | ||
|
||
import ( | ||
"std" | ||
"testing" | ||
) | ||
|
||
func TestTransferOwnership_NotOwner(t *testing.T) { | ||
std.TestSetOrigCaller("g185rxzrsc0j69amfsl255k2fxtlnt3cw8f3eu6h") // random address | ||
|
||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Error("Expected TransferOwnership to panic with not owner, but did not") | ||
} | ||
}() | ||
|
||
TransferOwnership("newOwner") | ||
} | ||
|
||
func TestTransferOwnership_EmptyNewOwner(t *testing.T) { | ||
std.TestSetOrigCaller("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm") | ||
|
||
defer func() { | ||
if r := recover(); r == nil { | ||
t.Error("Expected TransferOwnership to panic with empty new owner, but did not") | ||
} | ||
}() | ||
|
||
TransferOwnership("") | ||
} | ||
|
||
func TestTransferOwnership(t *testing.T) { | ||
// Note: | ||
// it seems that this address is the result of std.GetOrigCaller() in the init function when testing | ||
// will open up an issue to address this | ||
std.TestSetOrigCaller("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm") | ||
|
||
if GetOwner() != "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm" { | ||
t.Fatal("original owner not correct") | ||
} | ||
|
||
TransferOwnership("g185rxzrsc0j69amfsl255k2fxtlnt3cw8f3eu6h") | ||
|
||
if GetOwner() != "g185rxzrsc0j69amfsl255k2fxtlnt3cw8f3eu6h" { | ||
t.Fatal("new owner not set correctly") | ||
} | ||
} |
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,30 @@ | ||
package chess | ||
|
||
import ( | ||
"gno.land/p/demo/avl" | ||
) | ||
|
||
type RealmState struct { | ||
// chess.gno | ||
gameStore avl.Tree // string (game ID) -> *Game | ||
gameIDCounter uint64 | ||
user2Games avl.Tree // std.Address -> []*Game | ||
|
||
// discovery.gno | ||
playerStore avl.Tree // std.Address -> *Player | ||
leaderboard [CategoryMax]leaderboardType | ||
|
||
// glicko2.gno | ||
playerRatings [CategoryMax][]*PlayerRating | ||
} | ||
|
||
func RetrieveRealmState() *RealmState { | ||
return &RealmState{ | ||
gameStore: gameStore, | ||
gameIDCounter: gameIDCounter, | ||
user2Games: user2Games, | ||
playerStore: playerStore, | ||
leaderboard: leaderboard, | ||
playerRatings: playerRatings, | ||
} | ||
} |
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.
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.
Do you intend to handle the situation where the realm has not migrated yet? If that is the case, since
migrated
is a bool, the only stateful change that will happen is if it istrue
. Why check otherwise?