Skip to content

Commit

Permalink
further gnoland app tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thehowl committed Aug 20, 2024
1 parent b0a67ae commit 3e6bb61
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 6 deletions.
85 changes: 79 additions & 6 deletions gno.land/pkg/gnoland/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ import (
"context"
"errors"
"fmt"
"path/filepath"
"strings"
"testing"
"time"

"github.com/gnolang/gno/gnovm/stdlibs/std"
"github.com/gnolang/gno/gno.land/pkg/sdk/vm"
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
gnostd "github.com/gnolang/gno/gnovm/stdlibs/std"
"github.com/gnolang/gno/tm2/pkg/amino"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
bft "github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/db/memdb"
"github.com/gnolang/gno/tm2/pkg/events"
"github.com/gnolang/gno/tm2/pkg/log"
"github.com/gnolang/gno/tm2/pkg/sdk"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/tm2/pkg/store"
"github.com/gnolang/gno/tm2/pkg/store/dbadapter"
"github.com/gnolang/gno/tm2/pkg/store/iavl"
Expand All @@ -23,11 +30,77 @@ import (

// Tests that NewAppWithOptions works even when only providing a simple DB.
func TestNewAppWithOptions(t *testing.T) {
app, err := NewAppWithOptions(&AppOptions{DB: memdb.NewMemDB()})
app, err := NewAppWithOptions(&AppOptions{
DB: memdb.NewMemDB(),
InitChainerConfig: InitChainerConfig{
CacheStdlibLoad: true,
GenesisTxResultHandler: PanicOnFailingTxResultHandler,
StdlibDir: filepath.Join(gnoenv.RootDir(), "gnovm", "stdlibs"),
},
})
require.NoError(t, err)
bapp := app.(*sdk.BaseApp)
assert.Equal(t, "dev", bapp.AppVersion())
assert.Equal(t, "gnoland", bapp.Name())

addr := crypto.AddressFromPreimage([]byte("test1"))
resp := bapp.InitChain(abci.RequestInitChain{
Time: time.Now(),
ChainID: "dev",
ConsensusParams: &abci.ConsensusParams{
Block: &abci.BlockParams{
MaxTxBytes: 1_000_000, // 1MB,
MaxDataBytes: 2_000_000, // 2MB,
MaxGas: 100_000_000, // 100M gas
TimeIotaMS: 100, // 100ms
},
},
Validators: []abci.ValidatorUpdate{},
AppState: GnoGenesisState{
Balances: []Balance{
{
Address: addr,
Amount: []std.Coin{{Amount: 1e15, Denom: "ugnot"}},
},
},
Txs: []std.Tx{
{
Msgs: []std.Msg{vm.NewMsgAddPackage(addr, "gno.land/r/demo", []*std.MemFile{
{
Name: "demo.gno",
Body: "package demo; func Hello() string { return `hello`; }",
},
})},
Fee: std.Fee{GasWanted: 1e6, GasFee: std.Coin{Amount: 1e6, Denom: "ugnot"}},
Signatures: []std.Signature{{}}, // one empty signature
},
},
},
})

if !resp.IsOK() {
t.Fatal(resp)
}

tx := amino.MustMarshal(std.Tx{
Msgs: []std.Msg{vm.NewMsgCall(addr, nil, "gno.land/r/demo", "Hello", nil)},
Fee: std.Fee{
GasWanted: 100_000,
GasFee: std.Coin{
Denom: "ugnot",
Amount: 1_000_000,
},
},
Signatures: []std.Signature{{}}, // one empty signature
Memo: "",
})
dtxResp := bapp.DeliverTx(abci.RequestDeliverTx{
RequestBase: abci.RequestBase{},
Tx: tx,
})
if !dtxResp.IsOK() {
t.Fatal(dtxResp)
}
}

func TestNewAppWithOptions_ErrNoDB(t *testing.T) {
Expand Down Expand Up @@ -206,7 +279,7 @@ func TestEndBlocker(t *testing.T) {
c := newCollector[validatorUpdate](mockEventSwitch, noFilter)

// Fire a GnoVM event
mockEventSwitch.FireEvent(std.GnoEvent{})
mockEventSwitch.FireEvent(gnostd.GnoEvent{})

// Create the EndBlocker
eb := EndBlocker(c, mockVMKeeper, &mockEndBlockerApp{})
Expand Down Expand Up @@ -249,7 +322,7 @@ func TestEndBlocker(t *testing.T) {
c := newCollector[validatorUpdate](mockEventSwitch, noFilter)

// Fire a GnoVM event
mockEventSwitch.FireEvent(std.GnoEvent{})
mockEventSwitch.FireEvent(gnostd.GnoEvent{})

// Create the EndBlocker
eb := EndBlocker(c, mockVMKeeper, &mockEndBlockerApp{})
Expand Down Expand Up @@ -288,7 +361,7 @@ func TestEndBlocker(t *testing.T) {
// Construct the GnoVM events
vmEvents := make([]abci.Event, 0, len(changes))
for index := range changes {
event := std.GnoEvent{
event := gnostd.GnoEvent{
Type: validatorAddedEvent,
PkgPath: valRealm,
}
Expand All @@ -297,7 +370,7 @@ func TestEndBlocker(t *testing.T) {
if index%2 == 0 {
changes[index].Power = 0

event = std.GnoEvent{
event = gnostd.GnoEvent{
Type: validatorRemovedEvent,
PkgPath: valRealm,
}
Expand Down
62 changes: 62 additions & 0 deletions gno.land/pkg/sdk/vm/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/gnolang/gno/gno.land/pkg/gnoland/ugnot"
"github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/db/memdb"
"github.com/gnolang/gno/tm2/pkg/log"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/tm2/pkg/store/dbadapter"
"github.com/gnolang/gno/tm2/pkg/store/types"
)

var coinsString = ugnot.ValueString(10000000)
Expand Down Expand Up @@ -455,3 +461,59 @@ func Echo(msg string) string {
},
)
}

func TestVMKeeperReinitialize(t *testing.T) {
env := setupTestEnv()
ctx := env.ctx

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

// Create test package.
files := []*std.MemFile{
{"init.gno", `
package test
func Echo(msg string) string {
return "echo:"+msg
}`},
}
pkgPath := "gno.land/r/test"
msg1 := NewMsgAddPackage(addr, pkgPath, files)
err := env.vmk.AddPackage(ctx, msg1)
require.NoError(t, err)

// Run Echo function.
msg2 := NewMsgCall(addr, nil, pkgPath, "Echo", []string{"hello world"})
res, err := env.vmk.Call(ctx, msg2)
require.NoError(t, err)
assert.Equal(t, `("echo:hello world" string)`+"\n\n", res)

// Clear out gnovm and reinitialize.
env.vmk.gnoStore = nil
mcw := env.ctx.MultiStore().MultiCacheWrap()
env.vmk.Initialize(log.NewNoopLogger(), mcw)
mcw.MultiWrite()

// Run echo again, and it should still work.
res, err = env.vmk.Call(ctx, msg2)
require.NoError(t, err)
assert.Equal(t, `("echo:hello world" string)`+"\n\n", res)
}

func Test_loadStdlibPackage(t *testing.T) {
mdb := memdb.NewMemDB()
cs := dbadapter.StoreConstructor(mdb, types.StoreOptions{})

gs := gnolang.NewStore(nil, cs, cs)
assert.PanicsWithValue(t, `failed loading stdlib "notfound": does not exist`, func() {
loadStdlibPackage("notfound", "./testdata", gs)
})
assert.PanicsWithValue(t, `failed loading stdlib "emptystdlib": not a valid MemPackage`, func() {
loadStdlibPackage("emptystdlib", "./testdata", gs)
})
}
1 change: 1 addition & 0 deletions gno.land/pkg/sdk/vm/testdata/emptystdlib/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
see keeper_test.go

0 comments on commit 3e6bb61

Please sign in to comment.