Skip to content

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions contribs/gnodev/pkg/cachepath/cache_path_manual_deploy.go
Copy link
Member

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.

Copy link
Author

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

Copy link
Member

Choose a reason for hiding this comment

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

sounds resonable 👍

Copy link
Member

Choose a reason for hiding this comment

The 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 dev.Node struct.

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]
}
45 changes: 45 additions & 0 deletions contribs/gnodev/pkg/cachepath/cache_path_manual_deploy_test.go
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")
})
}
18 changes: 16 additions & 2 deletions contribs/gnodev/pkg/dev/node.go
Copy link
Member

Choose a reason for hiding this comment

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

Can you add some pertinent tests in node_test?

Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Check warning on line 287 in contribs/gnodev/pkg/dev/node.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/pkg/dev/node.go#L286-L287

Added lines #L286 - L287 were not covered by tests
}

metaTxs = append(metaTxs, gnoland.TxWithMetadata{
Tx: tx,
Expand Down Expand Up @@ -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)
}

Check warning on line 512 in contribs/gnodev/pkg/dev/node.go

View check run for this annotation

Codecov / codecov/patch

contribs/gnodev/pkg/dev/node.go#L511-L512

Added lines #L511 - L512 were not covered by tests
}

Comment on lines +507 to +514
Copy link
Member

Choose a reason for hiding this comment

The 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 generateTx

if err != nil {
return fmt.Errorf("unable to load pkgs: %w", err)
}
Expand All @@ -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.
Expand All @@ -520,7 +535,6 @@
// Update node infos
n.pkgs = pkgs
n.loadedPackages = len(pkgsTxs)

// Emit reload event
n.emitter.Emit(&events.Reload{})
return nil
Expand Down
Loading