Skip to content

Commit

Permalink
chore: lint
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton committed Oct 18, 2023
1 parent 6d761d7 commit 19cfdb0
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 77 deletions.
3 changes: 2 additions & 1 deletion gno.land/cmd/gnoweb/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func TestRoutes(t *testing.T) {
node, remoteAddr := integration.TestingInMemoryNode(t, log.NewNopLogger(), config)
defer node.Stop()

// set the `remoteAddr` of the client to the listening address of the node, which is randomly assigned.
// set the `remoteAddr` of the client to the listening address of the
// node, which is randomly assigned.
flags.remoteAddr = remoteAddr

app := makeApp()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package integration
package gnoland

import (
"fmt"
"time"

"github.com/gnolang/gno/gno.land/pkg/gnoland"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
bftconfig "github.com/gnolang/gno/tm2/pkg/bft/config"
tmcfg "github.com/gnolang/gno/tm2/pkg/bft/config"
"github.com/gnolang/gno/tm2/pkg/bft/node"
"github.com/gnolang/gno/tm2/pkg/bft/proxy"
bft "github.com/gnolang/gno/tm2/pkg/bft/types"
Expand All @@ -17,20 +16,20 @@ import (
"github.com/gnolang/gno/tm2/pkg/std"
)

type TestingNodeConfig struct {
BFTConfig *bftconfig.Config
type InMemoryNodeConfig struct {
TMConfig *tmcfg.Config
ConsensusParams abci.ConsensusParams
GenesisValidator []bft.GenesisValidator
Packages []gnoland.PackagePath
Balances []gnoland.Balance
Packages []PackagePath
Balances []Balance
GenesisTXs []std.Tx
SkipFailingGenesisTxs bool
GenesisMaxVMCycles int64
}

func NewTestingNodeConfig() *TestingNodeConfig {
return &TestingNodeConfig{
BFTConfig: bftconfig.TestConfig(),
func NewInMemoryNodeConfig(tmcfg *tmcfg.Config) *InMemoryNodeConfig {
return &InMemoryNodeConfig{
TMConfig: tmcfg,
ConsensusParams: abci.ConsensusParams{
Block: &abci.BlockParams{
MaxTxBytes: 1_000_000, // 1MB,
Expand All @@ -43,11 +42,16 @@ func NewTestingNodeConfig() *TestingNodeConfig {
}
}

func NewTestingNode(logger log.Logger, cfg *TestingNodeConfig) (*node.Node, error) {
if cfg.BFTConfig == nil {
return nil, fmt.Errorf("no BFTConfig given")
// NewInMemoryNode create an inMemeory gnoland node. In this mode, the node will
// not persist any data using an InMemory Database. For now the only indirect
// requirement is that `InMemoryNodeConfig.TMConfig.RootDir` is pointing to
// correct gno repository so that the node can load stdlibs
func NewInMemoryNode(logger log.Logger, cfg *InMemoryNodeConfig) (*node.Node, error) {
if cfg.TMConfig == nil {
return nil, fmt.Errorf("no TMConfig given")
}

// Create Identity
nodekey := &p2p.NodeKey{PrivKey: ed25519.GenPrivKey()}
pv := bft.NewMockPVWithParams(ed25519.GenPrivKey(), false, false)

Expand All @@ -56,7 +60,7 @@ func NewTestingNode(logger log.Logger, cfg *TestingNodeConfig) (*node.Node, erro
{
gen.GenesisTime = time.Now()

gen.ChainID = cfg.BFTConfig.ChainID()
gen.ChainID = cfg.TMConfig.ChainID()

gen.ConsensusParams = cfg.ConsensusParams

Expand All @@ -76,22 +80,22 @@ func NewTestingNode(logger log.Logger, cfg *TestingNodeConfig) (*node.Node, erro
}
}

// XXX: maybe let the user do this manually and pass it to genesisTXs
// XXX: Maybe let the user do this manually and pass it to genesisTXs
txs, err := LoadPackages(cfg.Packages)
if err != nil {
return nil, fmt.Errorf("uanble to load genesis packages: %w", err)
}

txs = append(txs, cfg.GenesisTXs...)

gen.AppState = gnoland.GnoGenesisState{
gen.AppState = GnoGenesisState{
Balances: cfg.Balances,
Txs: txs,
}

gnoApp, err := gnoland.NewAppWithOptions(&gnoland.AppOptions{
gnoApp, err := NewAppWithOptions(&AppOptions{
Logger: logger,
GnoRootDir: cfg.BFTConfig.RootDir,
GnoRootDir: cfg.TMConfig.RootDir,
SkipFailingGenesisTxs: cfg.SkipFailingGenesisTxs,
MaxCycles: cfg.GenesisMaxVMCycles,
DB: db.NewMemDB(),
Expand All @@ -100,22 +104,22 @@ func NewTestingNode(logger log.Logger, cfg *TestingNodeConfig) (*node.Node, erro
return nil, fmt.Errorf("error in creating new app: %w", err)
}

cfg.BFTConfig.LocalApp = gnoApp
cfg.TMConfig.LocalApp = gnoApp

// Get app client creator.
// Get app client creator
appClientCreator := proxy.DefaultClientCreator(
cfg.BFTConfig.LocalApp,
cfg.BFTConfig.ProxyApp,
cfg.BFTConfig.ABCI,
cfg.BFTConfig.DBDir(),
cfg.TMConfig.LocalApp,
cfg.TMConfig.ProxyApp,
cfg.TMConfig.ABCI,
cfg.TMConfig.DBDir(),
)

// Create genesis factory.
// Create genesis factory
genProvider := func() (*bft.GenesisDoc, error) {
return gen, nil
}

return node.NewNode(cfg.BFTConfig,
return node.NewNode(cfg.TMConfig,
pv, nodekey,
appClientCreator,
genProvider,
Expand All @@ -124,7 +128,7 @@ func NewTestingNode(logger log.Logger, cfg *TestingNodeConfig) (*node.Node, erro
)
}

func LoadPackages(pkgs []gnoland.PackagePath) ([]std.Tx, error) {
func LoadPackages(pkgs []PackagePath) ([]std.Tx, error) {
txs := []std.Tx{}
for _, pkg := range pkgs {
tx, err := pkg.Load()
Expand Down
27 changes: 19 additions & 8 deletions gno.land/pkg/integration/testing_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"

"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/tm2/pkg/bft/config"
"github.com/gnolang/gno/tm2/pkg/bft/node"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/crypto/keys"
Expand Down Expand Up @@ -47,10 +48,6 @@ func SetupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params {
// `gnoHomeDir` should be the local directory where gnokey stores keys.
gnoHomeDir := filepath.Join(tmpdir, "gno")

// `gnoDataDir` should refer to the local location where the gnoland node
// stores its configuration and data.
// gnoDataDir := filepath.Join(tmpdir, "data")

// Testscripts run concurrently by default, so we need to be prepared for that.
var muNodes sync.Mutex
nodes := map[string]*testNode{}
Expand Down Expand Up @@ -108,9 +105,21 @@ func SetupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params {
logger = getTestingLogger(ts, logname)
}

// Generate node config, we can't use a uniq config, because
nodeConfig := DefaultTestingNodeConfig(t, gnoRootDir)
node, remoteAddr := TestingInMemoryNode(t, logger, nodeConfig)
// Generate node config
cfg := config.TestConfig().SetRootDir(gnoRootDir)

// Use random port be able to run test in parallel without conflict
cfg.P2P.ListenAddress = "tcp://127.0.0.1:0"
cfg.RPC.ListenAddress = "tcp://127.0.0.1:0"

// Create the Node
nodecfg := gnoland.NewInMemoryNodeConfig(cfg)

node, err := gnoland.NewInMemoryNode(logger, nodecfg)
if err != nil {
err = fmt.Errorf("unable to start node: %w", err)
break
}

// Setup cleanup
ts.Defer(func() {
Expand All @@ -130,6 +139,8 @@ func SetupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params {
logger: logger,
}

remoteAddr := node.Config().RPC.ListenAddress

// Add default environements.
ts.Setenv("RPC_ADDR", remoteAddr)

Expand Down Expand Up @@ -256,7 +267,7 @@ func tsValidateError(ts *testscript.TestScript, cmd string, neg bool, err error)
}
} else {
if neg {
ts.Fatalf("unexpected %s command success", cmd)
ts.Fatalf("unexpected %q command success", cmd)
}
}
}
88 changes: 47 additions & 41 deletions gno.land/pkg/integration/testing_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package integration

import (
"path/filepath"
"sync"
"testing"
"time"

"github.com/gnolang/gno/gno.land/pkg/gnoland"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
"github.com/gnolang/gno/tm2/pkg/bft/config"
tmcfg "github.com/gnolang/gno/tm2/pkg/bft/config"
"github.com/gnolang/gno/tm2/pkg/bft/node"
bft "github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/crypto"
Expand All @@ -24,53 +25,33 @@ const (
)

// Should return an already starting node
func TestingInMemoryNode(t *testing.T, logger log.Logger, config *TestingNodeConfig) (*node.Node, string) {
func TestingInMemoryNode(t *testing.T, logger log.Logger, config *gnoland.InMemoryNodeConfig) (*node.Node, string) {
t.Helper()

node, err := NewTestingNode(logger, config)
node, err := gnoland.NewInMemoryNode(logger, config)
require.NoError(t, err)

err = node.Start()
require.NoError(t, err)

// XXX: This should be replace by https://github.com/gnolang/gno/pull/1216
//---
// Wait for first block by waiting for `EventNewBlock` event.
const listenerID = "testing_listener"

nb := make(chan struct{}, 1)
node.EventSwitch().AddListener(listenerID, func(ev events.Event) {
if _, ok := ev.(bft.EventNewBlock); ok {
select {
case nb <- struct{}{}:
default:
}
}
})

if node.BlockStore().Height() == 0 {
select {
case <-nb: // ok
case <-time.After(time.Second * 6):
t.Fatal("timeout while waiting for the node to start")
}
select {
case <-waitForNodeReadiness(node):
case <-time.After(time.Second * 6):
require.FailNow(t, "timeout while waiting for the node to start")
}

node.EventSwitch().RemoveListener(listenerID)
// ---

return node, node.Config().RPC.ListenAddress
}

func DefaultTestingNodeConfig(t *testing.T, gnoroot string) *TestingNodeConfig {
func DefaultTestingNodeConfig(t *testing.T, gnoroot string) *gnoland.InMemoryNodeConfig {
t.Helper()

bftconfig := DefaultTestingBFTConfig(t, gnoroot)
return &TestingNodeConfig{
tmconfig := DefaultTestingTMConfig(t, gnoroot)
return &gnoland.InMemoryNodeConfig{
Balances: LoadDefaultGenesisBalanceFile(t, gnoroot),
GenesisTXs: LoadDefaultGenesisTXsFile(t, bftconfig.ChainID(), gnoroot),
GenesisTXs: LoadDefaultGenesisTXsFile(t, tmconfig.ChainID(), gnoroot),
ConsensusParams: DefaultConsensusParams(t),
BFTConfig: bftconfig,
TMConfig: tmconfig,
Packages: LoadDefaultPackages(t, crypto.MustAddressFromString(DefaultAccountAddress), gnoroot),
}
}
Expand Down Expand Up @@ -105,8 +86,8 @@ func LoadDefaultGenesisTXsFile(t *testing.T, chainid string, gnoroot string) []s

txsFile := filepath.Join(gnoroot, "gno.land", "genesis", "genesis_txs.txt")

// NOTE: we dont care about giving a correct address here, as it's only visual
// XXX: do we care loading this file ?
// NOTE: We dont care about giving a correct address here, as it's only for display
// XXX: Do we care loading this TXs for testing ?
genesisTXs, err := gnoland.LoadGenesisTxsFile(txsFile, chainid, "https://127.0.0.1:26657")
require.NoError(t, err)

Expand All @@ -126,15 +107,40 @@ func DefaultConsensusParams(t *testing.T) abci.ConsensusParams {
}
}

func DefaultTestingBFTConfig(t *testing.T, gnoroot string) *config.Config {
func DefaultTestingTMConfig(t *testing.T, gnoroot string) *tmcfg.Config {
t.Helper()

const defaultListner = "tcp://127.0.0.1:0"

bftconfig := config.TestConfig().SetRootDir(gnoroot)
bftconfig.Consensus.CreateEmptyBlocks = true
bftconfig.Consensus.CreateEmptyBlocksInterval = time.Duration(0)
bftconfig.RPC.ListenAddress = defaultListner
bftconfig.P2P.ListenAddress = defaultListner
return bftconfig
tmconfig := tmcfg.TestConfig().SetRootDir(gnoroot)
tmconfig.Consensus.CreateEmptyBlocks = true
tmconfig.Consensus.CreateEmptyBlocksInterval = time.Duration(0)
tmconfig.RPC.ListenAddress = defaultListner
tmconfig.P2P.ListenAddress = defaultListner
return tmconfig
}

// XXX: This should be replace by https://github.com/gnolang/gno/pull/1216
func waitForNodeReadiness(n *node.Node) <-chan struct{} {
const listenerID = "first_block_listener"

var once sync.Once

nb := make(chan struct{})
ready := func() {
close(nb)
n.EventSwitch().RemoveListener(listenerID)
}

n.EventSwitch().AddListener(listenerID, func(ev events.Event) {
if _, ok := ev.(bft.EventNewBlock); ok {
once.Do(ready)
}
})

if n.BlockStore().Height() > 0 {
once.Do(ready)
}

return nb
}

0 comments on commit 19cfdb0

Please sign in to comment.