Skip to content
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
67 changes: 67 additions & 0 deletions gno.land/pkg/integration/testdata/addpkg_private_basic.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Test private realm basic functionality

# start a new node
gnoland start

# add a public realm
gnokey maketx addpkg -pkgdir $WORK/publicrealm -pkgpath gno.land/r/foobar/publicrealm -gas-fee 10000000ugnot -gas-wanted 20000000 -broadcast -chainid=tendermint_test $test1_user_addr
stdout OK!

# update the public realm
! gnokey maketx addpkg -pkgdir $WORK/publicrealm -pkgpath gno.land/r/foobar/publicrealm -gas-fee 10000000ugnot -gas-wanted 20000000 -broadcast -chainid=tendermint_test $test1_user_addr
stderr 'package already exists'

# add a private realm
gnokey maketx addpkg -pkgdir $WORK/privaterealm -pkgpath gno.land/r/foobar/privaterealm -gas-fee 10000000ugnot -gas-wanted 20000000 -broadcast -chainid=tendermint_test $test1_user_addr
stdout OK!

# update the same private realm
gnokey maketx addpkg -pkgdir $WORK/privaterealm -pkgpath gno.land/r/foobar/privaterealm -gas-fee 10000000ugnot -gas-wanted 20000000 -broadcast -chainid=tendermint_test $test1_user_addr
stdout OK!

# update the edited private realm over the original
gnokey maketx addpkg -pkgdir $WORK/privaterealmedited -pkgpath gno.land/r/foobar/privaterealm -gas-fee 10000000ugnot -gas-wanted 20000000 -broadcast -chainid=tendermint_test $test1_user_addr
stdout OK!

# overwrite public realm with private realm
! gnokey maketx addpkg -pkgdir $WORK/privaterealm -pkgpath gno.land/r/foobar/publicrealm -gas-fee 10000000ugnot -gas-wanted 20000000 -broadcast -chainid=tendermint_test $test1_user_addr
stderr 'package already exists'

# overwrite private realm with public realm
! gnokey maketx addpkg -pkgdir $WORK/publicrealm -pkgpath gno.land/r/foobar/privaterealm -gas-fee 10000000ugnot -gas-wanted 20000000 -broadcast -chainid=tendermint_test $test1_user_addr
stderr 'cannot change private package to public package'

-- privaterealm/gnomod.toml --
module = "gno.land/r/test"
gno = "0.9"
private = true

-- privaterealm/privaterealm.gno --
package test

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

-- privaterealmedited/gnomod.toml --
module = "gno.land/r/test"
gno = "0.9"
private = true

-- privaterealmedited/privaterealm.gno --
package test

func Echo(cur realm) string {
return "hello private edited world"
}

-- publicrealm/gnomod.toml --
module = "gno.land/r/foobar/publicrealm"
gno = "0.9"

-- publicrealm/publicrealm.gno --
package publicrealm

func Echo(cur realm) string {
return "hello public world"
}
8 changes: 7 additions & 1 deletion gno.land/pkg/sdk/vm/keeper.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 also add a .txtar integration test to ensure this behaviour works on-chain?

Try making sure also that we can modify function and method signatures, and that if a method/function is removed it doesn't exist anymore.

Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,12 @@ func (vm *VMKeeper) AddPackage(ctx sdk.Context, msg MsgAddPackage) (err error) {
if !strings.HasPrefix(pkgPath, chainDomain+"/") {
return ErrInvalidPkgPath("invalid domain: " + pkgPath)
}
if pv := gnostore.GetPackage(pkgPath, false); pv != nil {

pv := gnostore.GetPackage(pkgPath, false)
if pv != nil && !pv.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 Down Expand Up @@ -476,6 +479,9 @@ func (vm *VMKeeper) AddPackage(ctx sdk.Context, msg MsgAddPackage) (err error) {
if gm.HasReplaces() {
return ErrInvalidPackage("development packages are not allowed")
}
if pv != nil && pv.Private && !gm.Private {
return ErrInvalidPackage("cannot change private package to public package")
}
if gm.Private && !gno.IsRealmPath(pkgPath) {
return ErrInvalidPackage("private packages must be realm packages")
}
Expand Down
174 changes: 174 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 Expand Up @@ -315,6 +384,111 @@ func Echo(cur realm) string {
assert.Error(t, err, ErrTypeCheck(gnolang.ImportPrivateError{PkgPath: pkgPath}))
}

func TestVMKeeperAddPackage_ChangePublicToPrivate(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))

const pkgPath = "gno.land/r/test"
files := []*std.MemFile{
{Name: "gnomod.toml", Body: gnolang.GenGnoModLatest(pkgPath)},
{
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)

// Try to upload a private version of the same package.
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 private world"
}`,
},
}

msg2 := NewMsgAddPackage(addr, pkgPath, files2)
err = env.vmk.AddPackage(ctx, msg2)
assert.Error(t, err, ErrInvalidPackage(""))
}

func TestVMKeeperAddPackage_ChangePrivateToPublic(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 a private test package first.
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 private 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)

// Try to upload a public version of the same package.
files2 := []*std.MemFile{
{Name: "gnomod.toml", Body: gnolang.GenGnoModLatest(pkgPath)},
{
Name: "test.gno",
Body: `package test

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

msg2 := NewMsgAddPackage(addr, pkgPath, files2)
err = env.vmk.AddPackage(ctx, msg2)
assert.Error(t, err, ErrInvalidPackage(""))
}

// Sending total send amount succeeds.
func TestVMKeeperOriginSend1(t *testing.T) {
env := setupTestEnv()
Expand Down