Skip to content

Commit

Permalink
swamp: separate tendermint node creation from the swamp (#394)
Browse files Browse the repository at this point in the history
* swamp: separate tendermint node creation from the swamp
* add a config to set whatever app is needed from the test-case
* move sync test from swamp dir to tests
* adapt Makefile
* add comments to config.go
  • Loading branch information
Bidon15 committed Feb 11, 2022
1 parent 14bdc9a commit fb0f7ef
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ test-unit-race:
## test-swamp: Running swamp tests located in node/tests
test-swamp:
@echo "--> Running swamp tests"
@go test -v ./node/tests/swamp
@go test -v ./node/tests
.PHONY: test-swamp

## test-swamp: Running swamp tests with data race detector located in node/tests
test-swamp-race:
@echo "--> Running swamp tests with data race detector"
@go test -v -race ./node/tests/swamp
@go test -v -race ./node/tests
.PHONY: test-swamp-race

## test-all: Running both unit and swamp tests
Expand Down
28 changes: 28 additions & 0 deletions node/tests/swamp/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package swamp

import (
"time"

"github.com/tendermint/tendermint/abci/types"
tn "github.com/tendermint/tendermint/config"

"github.com/celestiaorg/celestia-node/core"
)

// Config struct represents a set of pre-requisite attributes from the test scenario
type Config struct {
App types.Application
CoreCfg *tn.Config
}

// DefaultConfig creates a KvStore with a block retention of 200
// In addition, the empty block interval is set to 200ms
func DefaultConfig() *Config {
app := core.CreateKvStore(200)
tnCfg := tn.TestConfig()
tnCfg.Consensus.CreateEmptyBlocksInterval = 200 * time.Millisecond
return &Config{
App: app,
CoreCfg: tnCfg,
}
}
38 changes: 25 additions & 13 deletions node/tests/swamp/swamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"math/rand"
"net"
"testing"
"time"

"github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p-core/host"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
tn "github.com/tendermint/tendermint/node"
rpctest "github.com/tendermint/tendermint/rpc/test"

"github.com/celestiaorg/celestia-node/core"
"github.com/celestiaorg/celestia-node/libs/keystore"
Expand All @@ -37,24 +38,21 @@ type Swamp struct {
}

// NewSwamp creates a new instance of Swamp.
func NewSwamp(t *testing.T) *Swamp {
func NewSwamp(t *testing.T, cfg *Config) *Swamp {
if testing.Verbose() {
log.SetDebugLogging()
}

var err error
ctx := context.Background()
// TODO(@Bidon15): Rework this to be configurable by the swamp's test
// case, not here.
kvStore := core.CreateKvStore(200)
coreNode := core.StartMockNode(kvStore)

coreNode.Config().Consensus.CreateEmptyBlocksInterval = 200 * time.Millisecond
tn, err := newTendermintCoreNode(cfg)
require.NoError(t, err)

swp := &Swamp{
t: t,
Network: mocknet.New(ctx),
CoreClient: core.NewEmbeddedFromNode(coreNode),
CoreClient: core.NewEmbeddedFromNode(tn),
}

swp.trustedHash, err = swp.getTrustedHash(ctx)
Expand All @@ -63,9 +61,28 @@ func NewSwamp(t *testing.T) *Swamp {
swp.t.Cleanup(func() {
swp.stopAllNodes(ctx, swp.BridgeNodes, swp.LightNodes)
})

return swp
}

// TODO(@Bidon15): CoreClient(limitation)
// Now, we are making an assumption that consensus mechanism is already tested out
// so, we are not creating bridge nodes with each one containing its own core client
// instead we are assigning all created BNs to 1 Core from the swamp

// newTendermintCoreNode creates a new instance of Tendermint Core with a kvStore
func newTendermintCoreNode(cfg *Config) (*tn.Node, error) {
var opt rpctest.Options
rpctest.RecreateConfig(&opt)

tn := rpctest.NewTendermint(cfg.App, &opt)

// rewriting the created config with test's one
tn.Config().Consensus = cfg.CoreCfg.Consensus

return tn, tn.Start()
}

// stopAllNodes goes through all received slices of Nodes and stops one-by-one
// this eliminates a manual clean-up in the test-cases itself in the end
func (s *Swamp) stopAllNodes(ctx context.Context, allNodes ...[]*node.Node) {
Expand Down Expand Up @@ -146,11 +163,6 @@ func (s *Swamp) getTrustedHash(ctx context.Context) (string, error) {
}
}

// TODO(@Bidon15): CoreClient(limitation)
// Now, we are making an assumption that consensus mechanism is already tested out
// so, we are not creating bridge nodes with each one containing its own core client
// instead we are assigning all created BNs to 1 Core from the swamp

// NewBridgeNode creates a new instance of BridgeNodes. Afterwards,
// the instance is store in the swamp's BridgeNodes slice.
func (s *Swamp) NewBridgeNode(options ...node.Option) *node.Node {
Expand Down
6 changes: 4 additions & 2 deletions node/tests/swamp/sync_test.go → node/tests/sync_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package swamp
package tests

import (
"context"
Expand All @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/celestiaorg/celestia-node/node"
"github.com/celestiaorg/celestia-node/node/tests/swamp"
)

/*
Expand All @@ -24,7 +25,8 @@ Steps:
6. Check LN is synced with BN
*/
func TestSyncLightWithBridge(t *testing.T) {
sw := NewSwamp(t)
sw := swamp.NewSwamp(t, swamp.DefaultConfig())

bridge := sw.NewBridgeNode()

ctx := context.Background()
Expand Down

0 comments on commit fb0f7ef

Please sign in to comment.