-
Notifications
You must be signed in to change notification settings - Fork 409
feat(gnodev): cache path and notify conflict path #4298
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
base: master
Are you sure you want to change the base?
Changes from all commits
1b69e7a
b13a5f0
9d67d91
64c1d4c
4458564
83fdfd8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. please remove this package; a simple sync map in this scenario doesn't deserve its own package as it's not shared among other packages or even other structs. This cache map should exist inside the |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cachepath | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type CachePath struct { | ||
data map[string]bool | ||
mu sync.RWMutex | ||
} | ||
|
||
var cache *CachePath | ||
|
||
func init() { | ||
cache = &CachePath{ | ||
data: make(map[string]bool), | ||
} | ||
} | ||
|
||
func Set(key string) { | ||
cache.mu.Lock() | ||
defer cache.mu.Unlock() | ||
cache.data[key] = true | ||
} | ||
|
||
func Get(key string) bool { | ||
cache.mu.RLock() | ||
defer cache.mu.RUnlock() | ||
return cache.data[key] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package cachepath | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCachePath(t *testing.T) { | ||
assert.NotNil(t, cache, "Cache should be initialized") | ||
assert.NotNil(t, cache.data, "Cache data map should be initialized") | ||
t.Run("Set and Get", func(t *testing.T) { | ||
path := "gno.land/r/test/example" | ||
|
||
exists := Get(path) | ||
assert.False(t, exists, "Path should not exist before setting") | ||
|
||
Set(path) | ||
|
||
exists = Get(path) | ||
assert.True(t, exists, "Path should exist after setting") | ||
}) | ||
|
||
t.Run("Multiple paths", func(t *testing.T) { | ||
cache.data = make(map[string]bool) | ||
paths := []string{ | ||
"gno.land/r/test/path1", | ||
"gno.land/r/test/path2", | ||
"gno.land/p/demo/path3", | ||
} | ||
|
||
for _, path := range paths { | ||
Set(path) | ||
} | ||
|
||
for _, path := range paths { | ||
exists := Get(path) | ||
assert.True(t, exists, "Path %s should exist after setting", path) | ||
} | ||
|
||
nonExistentPath := "gno.land/r/test/nonexistent" | ||
exists := Get(nonExistentPath) | ||
assert.False(t, exists, "Non-existent path should not exist") | ||
}) | ||
} |
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. Can you add some pertinent tests in |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
"time" | ||
"unicode" | ||
|
||
"github.com/gnolang/gno/contribs/gnodev/pkg/cachepath" | ||
"github.com/gnolang/gno/contribs/gnodev/pkg/emitter" | ||
"github.com/gnolang/gno/contribs/gnodev/pkg/events" | ||
"github.com/gnolang/gno/contribs/gnodev/pkg/packages" | ||
|
@@ -280,6 +281,11 @@ | |
if unmarshalErr := amino.Unmarshal(encodedTx, &tx); unmarshalErr != nil { | ||
return nil, fmt.Errorf("unable to unmarshal tx: %w", unmarshalErr) | ||
} | ||
for _, msg := range tx.Msgs { | ||
if addpkg, ok := msg.(vm.MsgAddPackage); ok && addpkg.Package != nil { | ||
cachepath.Set(addpkg.Package.Path) | ||
} | ||
} | ||
|
||
metaTxs = append(metaTxs, gnoland.TxWithMetadata{ | ||
Tx: tx, | ||
|
@@ -497,6 +503,15 @@ | |
|
||
// Load genesis packages | ||
pkgs, err := n.loader.Load(n.paths...) | ||
filterPackages := make([]packages.Package, 0, len(pkgs)) | ||
for _, pkg := range pkgs { | ||
if !cachepath.Get(pkg.Path) { | ||
filterPackages = append(filterPackages, pkg) | ||
} else { | ||
n.logger.Error("Can't reload package, package conflict in ", "path", pkg.Path) | ||
} | ||
} | ||
|
||
Comment on lines
+507
to
+514
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. no need to allocate another array here, you should filter this directly in |
||
if err != nil { | ||
return fmt.Errorf("unable to load pkgs: %w", err) | ||
} | ||
|
@@ -506,7 +521,7 @@ | |
genesis.Balances = n.config.BalancesList | ||
|
||
// Generate txs | ||
pkgsTxs := n.generateTxs(DefaultFee, pkgs) | ||
pkgsTxs := n.generateTxs(DefaultFee, filterPackages) | ||
genesis.Txs = append(pkgsTxs, state...) | ||
|
||
// Reset the node with the new genesis state. | ||
|
@@ -520,7 +535,6 @@ | |
// Update node infos | ||
n.pkgs = pkgs | ||
n.loadedPackages = len(pkgsTxs) | ||
|
||
// Emit reload event | ||
n.emitter.Emit(&events.Reload{}) | ||
return nil | ||
|
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.
Is a cache clearing function unnecessary? it doesn't seem like there's any part that handles cache invalidation.
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.
I think the cache clearing function is unnecessary, because once a package is added using gnokey addpkg, it cannot be reloaded. Moreover, the cache is reset whenever gnodev is restarted
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.
sounds resonable 👍