Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 12 additions & 6 deletions gno.land/pkg/sdk/vm/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,20 @@ func (vm *VMKeeper) AddPackage(ctx sdk.Context, msg MsgAddPackage) (err error) {
if !strings.HasPrefix(pkgPath, chainDomain+"/") {
return ErrInvalidPkgPath("invalid domain: " + pkgPath)
}

// Extra keeper-only checks.
gm, err := gnomod.ParseMemPackage(memPkg)
if err != nil {
return ErrInvalidPackage(err.Error())
}

if pv := gnostore.GetPackage(pkgPath, false); pv != nil {
return ErrPkgAlreadyExists("package already exists: " + pkgPath)
// Private packages can be re-uploaded (overwritten).
if !gm.Private {
return ErrPkgAlreadyExists("package already exists: " + pkgPath)
}
}

if !gno.IsRealmPath(pkgPath) && !gno.IsPPackagePath(pkgPath) {
return ErrInvalidPkgPath("package path must be valid realm or p package path")
}
Expand All @@ -467,11 +478,6 @@ func (vm *VMKeeper) AddPackage(ctx sdk.Context, msg MsgAddPackage) (err error) {
return ErrTypeCheck(err)
}

// Extra keeper-only checks.
gm, err := gnomod.ParseMemPackage(memPkg)
if err != nil {
return ErrInvalidPackage(err.Error())
}
// no development packages.
if gm.HasReplaces() {
return ErrInvalidPackage("development packages are not allowed")
Expand Down
69 changes: 69 additions & 0 deletions gno.land/pkg/sdk/vm/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,75 @@ func Echo(cur realm) string {
assert.NoError(t, err)
}

func TestVMKeeperAddPackage_UpdatePrivatePackage(t *testing.T) {
env := setupTestEnv()
ctx := env.vmk.MakeGnoTransactionStore(env.ctx)

// Give "addr1" some gnots.
addr := crypto.AddressFromPreimage([]byte("addr1"))
acc := env.acck.NewAccountWithAddress(ctx, addr)
env.acck.SetAccount(ctx, acc)
env.bankk.SetCoins(ctx, addr, initialBalance)
assert.True(t, env.bankk.GetCoins(ctx, addr).IsEqual(initialBalance))

// Create private test package.
const pkgPath = "gno.land/r/test"
files := []*std.MemFile{
{
Name: "gnomod.toml",
Body: `module = "gno.land/r/test"
gno = "0.9"
private = true`,
},
{
Name: "test.gno",
Body: `package test

func Echo(cur realm) string {
return "hello world"
}`,
},
}

msg1 := NewMsgAddPackage(addr, pkgPath, files)
assert.Nil(t, env.vmk.getGnoTransactionStore(ctx).GetPackage(pkgPath, false))
err := env.vmk.AddPackage(ctx, msg1)
assert.NoError(t, err)

// Re-upload the same private package with updated content.
files2 := []*std.MemFile{
{
Name: "gnomod.toml",
Body: `module = "gno.land/r/test"
gno = "0.9"
private = true`,
},
{
Name: "test.gno",
Body: `package test

func Echo(cur realm) string {
return "hello updated world"
}`,
},
}

msg2 := NewMsgAddPackage(addr, pkgPath, files2)
err = env.vmk.AddPackage(ctx, msg2)
assert.NoError(t, err)

// Verify the package was updated with the new content.
store := env.vmk.getGnoTransactionStore(ctx)
memFile := store.GetMemFile(pkgPath, "test.gno")
assert.NotNil(t, memFile)
expected := `package test

func Echo(cur realm) string {
return "hello updated world"
}`
assert.Equal(t, expected, memFile.Body)
}

func TestVMKeeperAddPackage_ImportPrivate(t *testing.T) {
env := setupTestEnv()
ctx := env.vmk.MakeGnoTransactionStore(env.ctx)
Expand Down
Loading