Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(network): simulate requests #1930

Merged
merged 34 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
641541c
init
lumtis Dec 9, 2021
b090944
Implement apply request
lumtis Dec 15, 2021
8c5bd84
fix prepare
lumtis Dec 15, 2021
b7ecf94
initialize apply test
lumtis Dec 15, 2021
19606bd
conflicts
lumtis Dec 15, 2021
f9f7653
Apply remove tests
lumtis Dec 15, 2021
b681000
refactor request
lumtis Dec 15, 2021
974e4ec
command verify
lumtis Dec 15, 2021
53389fc
fix chain home
lumtis Dec 15, 2021
b6d50bf
Add comment
lumtis Dec 16, 2021
b382bd2
Merge branch 'develop' into network/simulate-request
lumtis Dec 16, 2021
4fd779e
simulate and refactor
lumtis Dec 16, 2021
36aea7d
Lint
lumtis Dec 16, 2021
4fef1a0
refacotr
lumtis Dec 16, 2021
c34cdc6
refactor error
lumtis Dec 16, 2021
979fc98
conflicts
lumtis Dec 16, 2021
0eb9c11
changes in log
lumtis Dec 16, 2021
698f168
lint
lumtis Dec 16, 2021
2d04869
conflict
lumtis Dec 16, 2021
514037b
conflict
lumtis Dec 17, 2021
a31f7b6
Merge branch 'develop' into network/simulate-request
lumtis Dec 17, 2021
22a0937
Merge branch 'develop' into network/simulate-request
Pantani Dec 17, 2021
048b6af
Merge branch 'develop' into network/simulate-request
Pantani Dec 23, 2021
8a40df8
Update starport/cmd/network_request_verify.go
lumtis Jan 3, 2022
7c58169
Update starport/services/network/request.go
lumtis Jan 3, 2022
34b2b4e
Danilo suggest
lumtis Jan 3, 2022
6fbc2c2
Merge branch 'network/simulate-request' of https://github.com/tenderm…
lumtis Jan 3, 2022
30d3c40
Merge branch 'develop' into network/simulate-request
lumtis Jan 3, 2022
a1ae44c
lint
lumtis Jan 3, 2022
9a77390
Ilker comment
lumtis Jan 4, 2022
5d81a3d
comment
lumtis Jan 4, 2022
4653ff1
ilker comment
lumtis Jan 7, 2022
af15afa
Merge branch 'develop' into network/simulate-request
lumtis Jan 7, 2022
80b37b2
Merge branch 'develop' into network/simulate-request
lumtis Jan 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions starport/cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/tendermint/starport/starport/pkg/gitpod"
"github.com/tendermint/starport/starport/services/network"
"github.com/tendermint/starport/starport/services/network/networkchain"
"github.com/tendermint/starport/starport/services/network/networktypes"
)

var (
Expand Down Expand Up @@ -143,8 +144,8 @@ func getNetworkCosmosClient(cmd *cobra.Command) (cosmosclient.Client, error) {
cosmosOptions := []cosmosclient.Option{
cosmosclient.WithHome(cosmosaccount.KeyringHome),
cosmosclient.WithNodeAddress(spnNodeAddress),
cosmosclient.WithAddressPrefix(networkchain.SPN),
cosmosclient.WithUseFaucet(spnFaucetAddress, networkchain.SPNDenom, 5),
cosmosclient.WithAddressPrefix(networktypes.SPN),
cosmosclient.WithUseFaucet(spnFaucetAddress, networktypes.SPNDenom, 5),
cosmosclient.WithKeyringServiceName(cosmosaccount.KeyringServiceName),
}

Expand Down
1 change: 1 addition & 0 deletions starport/cmd/network_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func NewNetworkRequest() *cobra.Command {
NewNetworkRequestList(),
NewNetworkRequestApprove(),
NewNetworkRequestReject(),
NewNetworkRequestVerify(),
)

return c
Expand Down
9 changes: 6 additions & 3 deletions starport/cmd/network_request_approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package starportcmd
import (
"fmt"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/tendermint/starport/starport/pkg/clispinner"
"github.com/tendermint/starport/starport/pkg/numbers"
Expand Down Expand Up @@ -61,12 +62,14 @@ func networkRequestApproveHandler(cmd *cobra.Command, args []string) error {
return err
}

// if requests must be verified, we simulate the chain in a temporary directory with the requests
if !noVerification {
err := n.VerifyRequests(cmd.Context(), launchID, ids...)
if err != nil {
return err
if err := verifyRequest(cmd.Context(), nb, n, launchID, ids...); err != nil {
return errors.Wrap(err, "request(s) not valid")
}
fmt.Printf("%s Request(s) %s verified\n", clispinner.OK, numbers.List(ids, "#"))
}

// Submit the approved requests
reviewals := make([]network.Reviewal, 0)
for _, id := range ids {
Expand Down
114 changes: 114 additions & 0 deletions starport/cmd/network_request_verify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package starportcmd

import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/tendermint/starport/starport/pkg/chaincmd"
"github.com/tendermint/starport/starport/pkg/clispinner"
"github.com/tendermint/starport/starport/pkg/numbers"
"github.com/tendermint/starport/starport/services/network"
"github.com/tendermint/starport/starport/services/network/networkchain"
)

// NewNetworkRequestVerify creates a new request approve
// command to approve requests for a chain.
func NewNetworkRequestVerify() *cobra.Command {
c := &cobra.Command{
Use: "verify [launch-id] [number<,...>]",
Aliases: []string{"accept"},
Short: "Verify the request and simulate the chain genesis from them",
RunE: networkRequestVerifyHandler,
Args: cobra.ExactArgs(2),
}
c.Flags().AddFlagSet(flagNetworkFrom())
c.Flags().AddFlagSet(flagSetHome())
c.Flags().AddFlagSet(flagSetKeyringBackend())
return c
}

func networkRequestVerifyHandler(cmd *cobra.Command, args []string) error {
// initialize network common methods
nb, err := newNetworkBuilder(cmd)
if err != nil {
return err
}
defer nb.Cleanup()

// parse launch ID
launchID, err := network.ParseLaunchID(args[0])
if err != nil {
return err
}

// get the list of request ids
ids, err := numbers.ParseList(args[1])
if err != nil {
return err
}

n, err := nb.Network()
if err != nil {
return err
}

// verify the requests
if err := verifyRequest(cmd.Context(), nb, n, launchID, ids...); err != nil {
fmt.Printf("%s Request(s) %s not valid\n", clispinner.NotOK, numbers.List(ids, "#"))
return err
}

nb.Spinner.Stop()
fmt.Printf("%s Request(s) %s verified\n", clispinner.OK, numbers.List(ids, "#"))
return nil
}

// verifyRequest initialize the chain from the launch ID in a temporary directory
// and simulate the launch of the chain from genesis with the request IDs
func verifyRequest(
ctx context.Context,
nb NetworkBuilder,
n network.Network,
launchID uint64,
requestIDs ...uint64,
) error {
// initialize the chain with a temporary dir
chainLaunch, err := n.ChainLaunch(ctx, launchID)
if err != nil {
return err
}

homeDir, err := os.MkdirTemp("", "")
if err != nil {
return err
}
defer os.RemoveAll(homeDir)

c, err := nb.Chain(
networkchain.SourceLaunch(chainLaunch),
networkchain.WithHome(homeDir),
networkchain.WithKeyringBackend(chaincmd.KeyringBackendTest),
)
if err != nil {
return err
}

// fetch the current genesis information and the requests for the chain for simulation
genesisInformation, err := n.GenesisInformation(ctx, launchID)
if err != nil {
return err
}

requests, err := n.RequestFromIDs(ctx, launchID, requestIDs...)
if err != nil {
return err
}

if err := c.SimulateRequests(ctx, genesisInformation, requests); err != nil {
return err
}

return nil
}
4 changes: 3 additions & 1 deletion starport/pkg/clispinner/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import "github.com/fatih/color"

var (
// OK is an OK mark.
OK = color.New(color.FgGreen).SprintFunc()("✔")
OK = color.New(color.FgGreen).SprintFunc()("✔")
// NotOK is a red cross mark
NotOK = color.New(color.FgRed).SprintFunc()("✘")
Bullet = color.New(color.FgYellow).SprintFunc()("⋆")
)
10 changes: 5 additions & 5 deletions starport/services/network/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/tendermint/starport/starport/pkg/cosmoserror"
"github.com/tendermint/starport/starport/pkg/cosmosutil"
"github.com/tendermint/starport/starport/pkg/events"
"github.com/tendermint/starport/starport/services/network/networkchain"
"github.com/tendermint/starport/starport/services/network/networktypes"
)

// Join to the network.
Expand Down Expand Up @@ -49,7 +49,7 @@ func (n Network) Join(
}

// change the chain address prefix to spn
accountAddress, err := cosmosutil.ChangeAddressPrefix(gentxInfo.DelegatorAddress, networkchain.SPN)
accountAddress, err := cosmosutil.ChangeAddressPrefix(gentxInfo.DelegatorAddress, networktypes.SPN)
if err != nil {
return err
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func (n Network) sendAccountRequest(
accountAddress string,
amount sdk.Coin,
) (err error) {
address := n.account.Address(networkchain.SPN)
address := n.account.Address(networktypes.SPN)
n.ev.Send(events.New(events.StatusOngoing, "Verifying account already exists "+address))

// if is custom gentx path, avoid to check account into genesis from the home folder
Expand All @@ -101,7 +101,7 @@ func (n Network) sendAccountRequest(
}

msg := launchtypes.NewMsgRequestAddAccount(
n.account.Address(networkchain.SPN),
n.account.Address(networktypes.SPN),
launchID,
accountAddress,
sdk.NewCoins(amount),
Expand Down Expand Up @@ -148,7 +148,7 @@ func (n Network) sendValidatorRequest(
}

msg := launchtypes.NewMsgRequestAddValidator(
n.account.Address(networkchain.SPN),
n.account.Address(networktypes.SPN),
launchID,
valAddress,
gentx,
Expand Down
4 changes: 2 additions & 2 deletions starport/services/network/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/tendermint/starport/starport/pkg/cosmoserror"
"github.com/tendermint/starport/starport/pkg/events"
"github.com/tendermint/starport/starport/pkg/xtime"
"github.com/tendermint/starport/starport/services/network/networkchain"
"github.com/tendermint/starport/starport/services/network/networktypes"
)

// LaunchParams fetches the chain launch module params from SPN
Expand All @@ -32,7 +32,7 @@ func (n Network) TriggerLaunch(ctx context.Context, launchID uint64, remainingTi
var (
minLaunch = xtime.Seconds(params.MinLaunchTime)
maxLaunch = xtime.Seconds(params.MaxLaunchTime)
address = n.account.Address(networkchain.SPN)
address = n.account.Address(networktypes.SPN)
)
switch {
case remainingTime == 0:
Expand Down
2 changes: 2 additions & 0 deletions starport/services/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type Chain interface {
GenesisPath() (string, error)
GentxsPath() (string, error)
DefaultGentxPath() (string, error)
AppTOMLPath() (string, error)
ConfigTOMLPath() (string, error)
Peer(ctx context.Context, addr string) (string, error)
}

Expand Down
4 changes: 3 additions & 1 deletion starport/services/network/networkchain/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"os"
"path/filepath"
"strconv"

"github.com/tendermint/starport/starport/services/network/networktypes"
)

// ChainHome returns the default home dir used for a chain from SPN.
Expand All @@ -13,7 +15,7 @@ func ChainHome(launchID uint64) (path string) {
panic(err)
}

return filepath.Join(home, SPN, strconv.FormatUint(launchID, 10))
return filepath.Join(home, networktypes.SPN, strconv.FormatUint(launchID, 10))
}

// IsChainHomeExist checks if a home with the provided launchID already exist.
Expand Down
5 changes: 3 additions & 2 deletions starport/services/network/networkchain/home_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (

"github.com/stretchr/testify/require"
"github.com/tendermint/starport/starport/services/network/networkchain"
"github.com/tendermint/starport/starport/services/network/networktypes"
)

func TestChainHome(t *testing.T) {
home, err := os.UserHomeDir()
require.NoError(t, err)

chainHome := networkchain.ChainHome(0)
require.Equal(t, filepath.Join(home, networkchain.SPN, "0"), chainHome)
require.Equal(t, filepath.Join(home, networktypes.SPN, "0"), chainHome)

chainHome = networkchain.ChainHome(10)
require.Equal(t, filepath.Join(home, networkchain.SPN, "10"), chainHome)
require.Equal(t, filepath.Join(home, networktypes.SPN, "10"), chainHome)
}
18 changes: 9 additions & 9 deletions starport/services/network/networkchain/networkchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ import (
"github.com/tendermint/starport/starport/services/network/networktypes"
)

const (
// SPN name used as an address prefix and as a home dir for chains to publish.
SPN = "spn"

// SPNDenom is the denom used for the spn chain native token
SPNDenom = "uspn"
)

// Chain represents a network blockchain and lets you interact with its source code and binary.
type Chain struct {
id string
Expand Down Expand Up @@ -132,10 +124,10 @@ func New(ctx context.Context, ar cosmosaccount.Registry, source SourceOption, op
c := &Chain{
ar: ar,
}
source(c)
for _, apply := range options {
apply(c)
}
source(c)

c.ev.Send(events.New(events.StatusOngoing, "Fetching the source code"))

Expand Down Expand Up @@ -204,6 +196,14 @@ func (c Chain) DefaultGentxPath() (path string, err error) {
return c.chain.DefaultGentxPath()
}

func (c Chain) AppTOMLPath() (string, error) {
return c.chain.AppTOMLPath()
}

func (c Chain) ConfigTOMLPath() (string, error) {
return c.chain.ConfigTOMLPath()
}

func (c Chain) SourceURL() string {
return c.url
}
Expand Down
14 changes: 11 additions & 3 deletions starport/services/network/networkchain/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func (c Chain) Prepare(ctx context.Context, gi networktypes.GenesisInformation)
if err != nil {
return err
}

// ensure genesis has a valid format
err = cmd.ValidateGenesis(ctx)
if err != nil {
return err
}

// reset the saved state in case the chain has been started before
return cmd.UnsafeReset(ctx)
}

Expand All @@ -69,13 +77,13 @@ func (c Chain) buildGenesis(ctx context.Context, gi networktypes.GenesisInformat
}

// apply genesis information to the genesis
if err := c.applyGenesisAccounts(ctx, gi.GenesisAccounts, addressPrefix); err != nil {
if err := c.applyGenesisAccounts(ctx, gi.GetGenesisAccounts(), addressPrefix); err != nil {
return errors.Wrap(err, "error applying genesis accounts to genesis")
}
if err := c.applyVestingAccounts(ctx, gi.VestingAccounts, addressPrefix); err != nil {
if err := c.applyVestingAccounts(ctx, gi.GetVestingAccounts(), addressPrefix); err != nil {
return errors.Wrap(err, "error applying vesting accounts to genesis")
}
if err := c.applyGenesisValidators(ctx, gi.GenesisValidators); err != nil {
if err := c.applyGenesisValidators(ctx, gi.GetGenesisValidators()); err != nil {
return errors.Wrap(err, "error applying genesis validators to genesis")
}

Expand Down
Loading