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
54 changes: 54 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,54 @@
# Test private realm basic functionality

# start a new node
gnoland start


# 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 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!


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

-- privaterealm/privaterealm.gno --
package test

var root any
var root2 any
var root3 any

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
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("a private package cannot be overridden by a 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
51 changes: 51 additions & 0 deletions gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ func (m *Machine) RunMemPackageWithOverrides(mpkg *std.MemPackage, save bool) (*
}

func (m *Machine) runMemPackage(mpkg *std.MemPackage, save, overrides bool) (*PackageNode, *PackageValue) {
// fmt.Println("======runMemPacakge, path: ", mpkg.Path)
// validate mpkg.Type.
mptype := mpkg.Type.(MemPackageType)
if save && !mptype.IsStorable() {
Expand All @@ -273,6 +274,56 @@ func (m *Machine) runMemPackage(mpkg *std.MemPackage, save, overrides bool) (*Pa
private = mod.Private
}

if oid := ObjectIDFromPkgPath(mpkg.Path); m.Store.HasObject(oid) && private {
// idx of the old package
pkgidx := m.Store.GetPackageIndexCounter(oid.PkgID)
// fmt.Println("======pkgidx: ", pkgidx-1)
objidx := m.Store.GetObjectIndexCounter(backendObjectIndexKey(oid.PkgID, pkgidx))
// fmt.Println("======objidx: ", objidx)

// fmt.Println("======prepare for cleaning MemPackage here...")
ctr2 := m.Store.GetPackageIndexCounter(ObjectIDFromPkgPath(mpkg.Path).PkgID)
// fmt.Println("======ctr2: ", ctr2)
idxkey := []byte(backendPackageIndexKey(ctr2))
if m.Store.HasMemPackage(idxkey) {
// fmt.Println("======found mempackage, clean it... idxkey: ", string(idxkey))
m.Store.DelMemPackage(idxkey)
}
//===========================================================
defer func() {
// fmt.Println("======defer...")
// idx of new package revision
pkgidx2 := m.Store.GetPackageIndexCounter(oid.PkgID)
// fmt.Println("======pkgidx2: ", pkgidx2)
objidx2 := m.Store.GetObjectIndexCounter(backendObjectIndexKey(oid.PkgID, pkgidx2))
// fmt.Println("======objidx2: ", objidx2)

if objidx2 >= objidx {
// fmt.Println("======nothing to clean")
return
}

// fmt.Println("======do clean..., num: ", objidx-objidx2)
// fmt.Println("======objidx: ", objidx)
for i := objidx2 + 1; ; i++ {
oid := ObjectID{PkgID: oid.PkgID, NewTime: i}
if m.Store.HasObject(oid) {
m.Store.DelObjectByID(oid)
} else {
// fmt.Println("============clean over...")
break
}
}
}()
}

// inc counter
ctr := m.Store.NextGlobalID() // global
m.Store.NextPackageRevision(ObjectIDFromPkgPath(mpkg.Path).PkgID, ctr) // per package

// if oid := ObjectIDFromPkgPath(mpkg.Path); m.Store.HasObject(oid) && private {
// }

// make and set package if doesn't exist.
pn := (*PackageNode)(nil)
pv := (*PackageValue)(nil)
Expand Down
Loading
Loading