Skip to content

Commit

Permalink
fix(gno.land): improve test speed (#3396)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfanton authored Jan 6, 2025
1 parent beb48e7 commit 74b077f
Show file tree
Hide file tree
Showing 22 changed files with 1,801 additions and 863 deletions.
72 changes: 62 additions & 10 deletions gno.land/pkg/gnoclient/integration_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package gnoclient

import (
"path/filepath"
"testing"

"github.com/gnolang/gno/gnovm/pkg/gnolang"

"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/gno.land/pkg/gnoland/ugnot"
"github.com/gnolang/gno/gno.land/pkg/integration"
"github.com/gnolang/gno/gno.land/pkg/sdk/vm"
Expand All @@ -21,8 +23,14 @@ import (
)

func TestCallSingle_Integration(t *testing.T) {
// Set up in-memory node
config, _ := integration.TestingNodeConfig(t, gnoenv.RootDir())
// Setup packages
rootdir := gnoenv.RootDir()
config := integration.TestingMinimalNodeConfig(gnoenv.RootDir())
meta := loadpkgs(t, rootdir, "gno.land/r/demo/deep/very/deep")
state := config.Genesis.AppState.(gnoland.GnoGenesisState)
state.Txs = append(state.Txs, meta...)
config.Genesis.AppState = state

node, remoteAddr := integration.TestingInMemoryNode(t, log.NewNoopLogger(), config)
defer node.Stop()

Expand Down Expand Up @@ -74,8 +82,14 @@ func TestCallSingle_Integration(t *testing.T) {
}

func TestCallMultiple_Integration(t *testing.T) {
// Set up in-memory node
config, _ := integration.TestingNodeConfig(t, gnoenv.RootDir())
// Setup packages
rootdir := gnoenv.RootDir()
config := integration.TestingMinimalNodeConfig(gnoenv.RootDir())
meta := loadpkgs(t, rootdir, "gno.land/r/demo/deep/very/deep")
state := config.Genesis.AppState.(gnoland.GnoGenesisState)
state.Txs = append(state.Txs, meta...)
config.Genesis.AppState = state

node, remoteAddr := integration.TestingInMemoryNode(t, log.NewNoopLogger(), config)
defer node.Stop()

Expand Down Expand Up @@ -137,7 +151,7 @@ func TestCallMultiple_Integration(t *testing.T) {

func TestSendSingle_Integration(t *testing.T) {
// Set up in-memory node
config, _ := integration.TestingNodeConfig(t, gnoenv.RootDir())
config := integration.TestingMinimalNodeConfig(gnoenv.RootDir())
node, remoteAddr := integration.TestingInMemoryNode(t, log.NewNoopLogger(), config)
defer node.Stop()

Expand Down Expand Up @@ -201,7 +215,7 @@ func TestSendSingle_Integration(t *testing.T) {

func TestSendMultiple_Integration(t *testing.T) {
// Set up in-memory node
config, _ := integration.TestingNodeConfig(t, gnoenv.RootDir())
config := integration.TestingMinimalNodeConfig(gnoenv.RootDir())
node, remoteAddr := integration.TestingInMemoryNode(t, log.NewNoopLogger(), config)
defer node.Stop()

Expand Down Expand Up @@ -273,8 +287,15 @@ func TestSendMultiple_Integration(t *testing.T) {

// Run tests
func TestRunSingle_Integration(t *testing.T) {
// Setup packages
rootdir := gnoenv.RootDir()
config := integration.TestingMinimalNodeConfig(gnoenv.RootDir())
meta := loadpkgs(t, rootdir, "gno.land/p/demo/ufmt", "gno.land/r/demo/tests")
state := config.Genesis.AppState.(gnoland.GnoGenesisState)
state.Txs = append(state.Txs, meta...)
config.Genesis.AppState = state

// Set up in-memory node
config, _ := integration.TestingNodeConfig(t, gnoenv.RootDir())
node, remoteAddr := integration.TestingInMemoryNode(t, log.NewNoopLogger(), config)
defer node.Stop()

Expand Down Expand Up @@ -342,7 +363,17 @@ func main() {
// Run tests
func TestRunMultiple_Integration(t *testing.T) {
// Set up in-memory node
config, _ := integration.TestingNodeConfig(t, gnoenv.RootDir())
rootdir := gnoenv.RootDir()
config := integration.TestingMinimalNodeConfig(rootdir)
meta := loadpkgs(t, rootdir,
"gno.land/p/demo/ufmt",
"gno.land/r/demo/tests",
"gno.land/r/demo/deep/very/deep",
)
state := config.Genesis.AppState.(gnoland.GnoGenesisState)
state.Txs = append(state.Txs, meta...)
config.Genesis.AppState = state

node, remoteAddr := integration.TestingInMemoryNode(t, log.NewNoopLogger(), config)
defer node.Stop()

Expand Down Expand Up @@ -434,7 +465,7 @@ func main() {

func TestAddPackageSingle_Integration(t *testing.T) {
// Set up in-memory node
config, _ := integration.TestingNodeConfig(t, gnoenv.RootDir())
config := integration.TestingMinimalNodeConfig(gnoenv.RootDir())
node, remoteAddr := integration.TestingInMemoryNode(t, log.NewNoopLogger(), config)
defer node.Stop()

Expand Down Expand Up @@ -519,7 +550,7 @@ func Echo(str string) string {

func TestAddPackageMultiple_Integration(t *testing.T) {
// Set up in-memory node
config, _ := integration.TestingNodeConfig(t, gnoenv.RootDir())
config := integration.TestingMinimalNodeConfig(gnoenv.RootDir())
node, remoteAddr := integration.TestingInMemoryNode(t, log.NewNoopLogger(), config)
defer node.Stop()

Expand Down Expand Up @@ -670,3 +701,24 @@ func newInMemorySigner(t *testing.T, chainid string) *SignerFromKeybase {
ChainID: chainid, // Chain ID for transaction signing
}
}

func loadpkgs(t *testing.T, rootdir string, paths ...string) []gnoland.TxWithMetadata {
t.Helper()

loader := integration.NewPkgsLoader()
examplesDir := filepath.Join(rootdir, "examples")
for _, path := range paths {
path = filepath.Clean(path)
path = filepath.Join(examplesDir, path)
err := loader.LoadPackage(examplesDir, path, "")
require.NoErrorf(t, err, "`loadpkg` unable to load package(s) from %q: %s", path, err)
}

creator := crypto.MustAddressFromString(integration.DefaultAccount_Address)
defaultFee := std.NewFee(50000, std.MustParseCoin(ugnot.ValueString(1000000)))

meta, err := loader.LoadPackages(creator, defaultFee, nil)
require.NoError(t, err)

return meta
}
4 changes: 2 additions & 2 deletions gno.land/pkg/gnoland/node_inmemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type InMemoryNodeConfig struct {
PrivValidator bft.PrivValidator // identity of the validator
Genesis *bft.GenesisDoc
TMConfig *tmcfg.Config
DB *memdb.MemDB // will be initialized if nil
VMOutput io.Writer // optional
DB db.DB // will be initialized if nil
VMOutput io.Writer // optional

// If StdlibDir not set, then it's filepath.Join(TMConfig.RootDir, "gnovm", "stdlibs")
InitChainerConfig
Expand Down
7 changes: 0 additions & 7 deletions gno.land/pkg/integration/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,6 @@
//
// Input:
//
// - LOG_LEVEL:
// The logging level to be used, which can be one of "error", "debug", "info", or an empty string.
// If empty, the log level defaults to "debug".
//
// - LOG_DIR:
// If set, logs will be directed to the specified directory.
//
// - TESTWORK:
// A boolean that, when enabled, retains working directories after tests for
// inspection. If enabled, gnoland logs will be persisted inside this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestingInMemoryNode(t TestingTS, logger *slog.Logger, config *gnoland.InMem
// with default packages and genesis transactions already loaded.
// It will return the default creator address of the loaded packages.
func TestingNodeConfig(t TestingTS, gnoroot string, additionalTxs ...gnoland.TxWithMetadata) (*gnoland.InMemoryNodeConfig, bft.Address) {
cfg := TestingMinimalNodeConfig(t, gnoroot)
cfg := TestingMinimalNodeConfig(gnoroot)

creator := crypto.MustAddressFromString(DefaultAccount_Address) // test1

Expand All @@ -65,24 +65,24 @@ func TestingNodeConfig(t TestingTS, gnoroot string, additionalTxs ...gnoland.TxW
txs = append(txs, LoadDefaultPackages(t, creator, gnoroot)...)
txs = append(txs, additionalTxs...)

ggs := cfg.Genesis.AppState.(gnoland.GnoGenesisState)
ggs.Balances = balances
ggs.Txs = txs
ggs.Params = params
cfg.Genesis.AppState = ggs
cfg.Genesis.AppState = gnoland.GnoGenesisState{
Balances: balances,
Txs: txs,
Params: params,
}

return cfg, creator
}

// TestingMinimalNodeConfig constructs the default minimal in-memory node configuration for testing.
func TestingMinimalNodeConfig(t TestingTS, gnoroot string) *gnoland.InMemoryNodeConfig {
func TestingMinimalNodeConfig(gnoroot string) *gnoland.InMemoryNodeConfig {
tmconfig := DefaultTestingTMConfig(gnoroot)

// Create Mocked Identity
pv := gnoland.NewMockedPrivValidator()

// Generate genesis config
genesis := DefaultTestingGenesisConfig(t, gnoroot, pv.GetPubKey(), tmconfig)
genesis := DefaultTestingGenesisConfig(gnoroot, pv.GetPubKey(), tmconfig)

return &gnoland.InMemoryNodeConfig{
PrivValidator: pv,
Expand All @@ -96,16 +96,7 @@ func TestingMinimalNodeConfig(t TestingTS, gnoroot string) *gnoland.InMemoryNode
}
}

func DefaultTestingGenesisConfig(t TestingTS, gnoroot string, self crypto.PubKey, tmconfig *tmcfg.Config) *bft.GenesisDoc {
genState := gnoland.DefaultGenState()
genState.Balances = []gnoland.Balance{
{
Address: crypto.MustAddressFromString(DefaultAccount_Address),
Amount: std.MustParseCoins(ugnot.ValueString(10000000000000)),
},
}
genState.Txs = []gnoland.TxWithMetadata{}
genState.Params = []gnoland.Param{}
func DefaultTestingGenesisConfig(gnoroot string, self crypto.PubKey, tmconfig *tmcfg.Config) *bft.GenesisDoc {
return &bft.GenesisDoc{
GenesisTime: time.Now(),
ChainID: tmconfig.ChainID(),
Expand All @@ -125,7 +116,16 @@ func DefaultTestingGenesisConfig(t TestingTS, gnoroot string, self crypto.PubKey
Name: "self",
},
},
AppState: genState,
AppState: gnoland.GnoGenesisState{
Balances: []gnoland.Balance{
{
Address: crypto.MustAddressFromString(DefaultAccount_Address),
Amount: std.MustParseCoins(ugnot.ValueString(10_000_000_000_000)),
},
},
Txs: []gnoland.TxWithMetadata{},
Params: []gnoland.Param{},
},
}
}

Expand Down Expand Up @@ -178,8 +178,9 @@ func DefaultTestingTMConfig(gnoroot string) *tmcfg.Config {

tmconfig := tmcfg.TestConfig().SetRootDir(gnoroot)
tmconfig.Consensus.WALDisabled = true
tmconfig.Consensus.SkipTimeoutCommit = true
tmconfig.Consensus.CreateEmptyBlocks = true
tmconfig.Consensus.CreateEmptyBlocksInterval = time.Duration(0)
tmconfig.Consensus.CreateEmptyBlocksInterval = time.Millisecond * 100
tmconfig.RPC.ListenAddress = defaultListner
tmconfig.P2P.ListenAddress = defaultListner
return tmconfig
Expand Down
Loading

0 comments on commit 74b077f

Please sign in to comment.