Skip to content

Commit

Permalink
reconcile: network testutils
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Feb 8, 2024
1 parent b509058 commit 9727c64
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
package network

import (
"encoding/json"
"fmt"
"testing"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/client/flags"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/app"
"github.com/pokt-network/poktroll/cmd/poktrolld/cmd"
)

type (
Network = network.Network
Config = network.Config
)

func init() {
cmd.InitSDKConfig()
}

// New creates instance with fully configured cosmos network.
// Accepts optional config, that will be used in place of the DefaultConfig() if provided.
func New(t *testing.T, configs ...Config) *Network {
Expand Down Expand Up @@ -59,6 +70,38 @@ func DefaultConfig() network.Config {
return cfg
}

// InitAccountWithSequence initializes an Account by sending it some funds from
// the validator in the network to the address provided
func InitAccountWithSequence(
t *testing.T,
net *Network,
addr sdk.AccAddress,
signatureSequencerNumber int,
) {
t.Helper()
val := net.Validators[0]
signerAccountNumber := 0
ctx := val.ClientCtx
args := []string{
fmt.Sprintf("--%s=true", flags.FlagOffline),
fmt.Sprintf("--%s=%d", flags.FlagAccountNumber, signerAccountNumber),
fmt.Sprintf("--%s=%d", flags.FlagSequence, signatureSequencerNumber),

fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()),
}
amount := sdk.NewCoins(sdk.NewCoin("stake", sdkmath.NewInt(200)))
addrCodec := addresscodec.NewBech32Codec(app.AccountAddressPrefix)
responseRaw, err := clitestutil.MsgSendExec(ctx, val.Address, addr, amount, addrCodec, args...)
require.NoError(t, err)
var responseJSON map[string]interface{}
err = json.Unmarshal(responseRaw.Bytes(), &responseJSON)
require.NoError(t, err)
require.Equal(t, float64(0), responseJSON["code"], "code is not 0 in the response: %v", responseJSON)
}

// freePorts return the available ports based on the number of requested ports.
func freePorts(n int) ([]string, error) {
closeFns := make([]func() error, n)
Expand Down

0 comments on commit 9727c64

Please sign in to comment.