From 69ce5201e802db45235ceeb4e0e5b8de3ecc4b83 Mon Sep 17 00:00:00 2001 From: ltacker Date: Thu, 5 Jan 2023 13:33:07 +0100 Subject: [PATCH 01/11] request content method --- network/networktypes/request.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/network/networktypes/request.go b/network/networktypes/request.go index 24306cc..a7820c9 100644 --- a/network/networktypes/request.go +++ b/network/networktypes/request.go @@ -34,6 +34,20 @@ func ToRequest(request launchtypes.Request) Request { } } +// RequestsFromRequestContents creates a list of requests from a list request contents to simulate requests that have not been sent to request pool yet +// The request ID is set to 0 for the first request and incremented for each request, other values are not set +func RequestsFromRequestContents(launchID uint64, contents []launchtypes.RequestContent) []Request { + requests := make([]Request, len(contents)) + for i, content := range contents { + requests[i] = Request{ + LaunchID: launchID, + RequestID: uint64(i), + Content: content, + } + } + return requests +} + // VerifyRequest verifies the validity of the request from its content (static check). func VerifyRequest(request Request) error { req, ok := request.Content.Content.(*launchtypes.RequestContent_GenesisValidator) From af5dce127bf4d0bf88e414cd34ed071e678bb586 Mon Sep 17 00:00:00 2001 From: ltacker Date: Thu, 5 Jan 2023 13:56:06 +0100 Subject: [PATCH 02/11] test request content --- network/networktypes/request_test.go | 92 ++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/network/networktypes/request_test.go b/network/networktypes/request_test.go index b2fb98b..24bd4d7 100644 --- a/network/networktypes/request_test.go +++ b/network/networktypes/request_test.go @@ -14,6 +14,98 @@ import ( "github.com/ignite/cli-plugin-network/network/networktypes" ) +func TestRequestsFromRequestContents(t *testing.T) { + tests := []struct { + name string + launchID uint64 + reqs []launchtypes.RequestContent + want []networktypes.Request + }{ + { + name: "empty request contents", + launchID: 0, + reqs: []launchtypes.RequestContent{}, + want: []networktypes.Request{}, + }, + { + name: "one request content", + launchID: 1, + reqs: []launchtypes.RequestContent{ + launchtypes.NewGenesisAccount( + 1, + "spn1dd246y", + sdk.NewCoins(sdk. + NewCoin("stake", sdkmath.NewInt(1000)), + ), + ), + }, + want: []networktypes.Request{ + { + LaunchID: 1, + RequestID: 0, + Content: launchtypes.NewGenesisAccount( + 1, + "spn1dd246y", + sdk.NewCoins(sdk. + NewCoin("stake", sdkmath.NewInt(1000)), + ), + ), + }, + }, + }, + { + name: "multiple request contents", + launchID: 2, + reqs: []launchtypes.RequestContent{ + launchtypes.NewGenesisAccount( + 2, + "spn5s5z2x", + sdk.NewCoins(sdk. + NewCoin("foo", sdkmath.NewInt(2000)), + ), + ), + launchtypes.NewGenesisAccount( + 2, + "spn2x2x2x", + sdk.NewCoins(sdk. + NewCoin("bar", sdkmath.NewInt(5000)), + ), + ), + }, + want: []networktypes.Request{ + { + LaunchID: 2, + RequestID: 0, + Content: launchtypes.NewGenesisAccount( + 2, + "spn5s5z2x", + sdk.NewCoins(sdk. + NewCoin("foo", sdkmath.NewInt(2000)), + ), + ), + }, + { + LaunchID: 2, + RequestID: 1, + Content: launchtypes.NewGenesisAccount( + 2, + "spn2x2x2x", + sdk.NewCoins(sdk. + NewCoin("bar", sdkmath.NewInt(5000)), + ), + ), + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := networktypes.RequestsFromRequestContents(tt.launchID, tt.reqs) + require.Equal(t, tt.want, got) + }) + } +} + func TestVerifyAddValidatorRequest(t *testing.T) { gentx := []byte(`{ "body": { From dd733ef3849e9112932ac191ee61b53ac8f3a7ec Mon Sep 17 00:00:00 2001 From: ltacker Date: Thu, 5 Jan 2023 14:59:37 +0100 Subject: [PATCH 03/11] go mod --- go.mod | 2 ++ go.sum | 2 ++ 2 files changed, 4 insertions(+) diff --git a/go.mod b/go.mod index 5aa456f..9658b43 100644 --- a/go.mod +++ b/go.mod @@ -201,6 +201,7 @@ require ( github.com/hdevalence/ed25519consensus v0.0.0-20220222234857-c00d1f31bab3 // indirect github.com/hexops/gotextdiff v1.0.3 // indirect github.com/iancoleman/strcase v0.2.0 // indirect + github.com/ignite/modules v0.0.0-20220912090139-7c325cae763a // indirect github.com/ignite/web v0.4.3 // indirect github.com/imdario/mergo v0.3.13 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect @@ -324,6 +325,7 @@ require ( github.com/tdakkota/asciicheck v0.1.1 // indirect github.com/tendermint/btcd v0.1.1 // indirect github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15 // indirect + github.com/tendermint/fundraising v0.3.1 // indirect github.com/tendermint/go-amino v0.16.0 // indirect github.com/tendermint/tm-db v0.6.7 // indirect github.com/tetafro/godot v1.4.11 // indirect diff --git a/go.sum b/go.sum index 4876531..b3368cb 100644 --- a/go.sum +++ b/go.sum @@ -882,6 +882,7 @@ github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1: github.com/ignite/cli v0.25.3-0.20221216180535-bcbcd1d0bb57 h1:KYgSa36GfxhIS71nWp99/ttoa7McrfSpyoplx3veIX8= github.com/ignite/cli v0.25.3-0.20221216180535-bcbcd1d0bb57/go.mod h1:Dp0pX59gVacMwUJSyKe25JBb005YUcS6YoSrn7WzfwI= github.com/ignite/modules v0.0.0-20220912090139-7c325cae763a h1:0P8qg4YS0hb8jomZedhbooEKE3JZhRNAewV8+8otr6k= +github.com/ignite/modules v0.0.0-20220912090139-7c325cae763a/go.mod h1:BmcHZ5Q+9jrnL2k7U7sVOPh0FD7cshCJDxysAb8SCS0= github.com/ignite/web v0.4.3 h1:LHucUEXttzCf5JmxO5/xLKVx1Qz1o124HqF4Nii5uWQ= github.com/ignite/web v0.4.3/go.mod h1:WZWBaBYF8RazN7dE462BLpvXDY8ScacxcJ07BKwX/jY= github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= @@ -1459,6 +1460,7 @@ github.com/tendermint/btcd v0.1.1/go.mod h1:DC6/m53jtQzr/NFmMNEu0rxf18/ktVoVtMrn github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15 h1:hqAk8riJvK4RMWx1aInLzndwxKalgi5rTqgfXxOxbEI= github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15/go.mod h1:z4YtwM70uOnk8h0pjJYlj3zdYwi9l03By6iAIF5j/Pk= github.com/tendermint/fundraising v0.3.1 h1:S4uOV/T7YNBqXhsCZnq/TUoHB0d2kM+6tKeTD4WhLN0= +github.com/tendermint/fundraising v0.3.1/go.mod h1:7rCHXGOoFiUBD7sAZ4u+/EFq4MgQEn7tFopX6Qe6zpc= github.com/tendermint/go-amino v0.16.0 h1:GyhmgQKvqF82e2oZeuMSp9JTN0N09emoSZlb2lyGa2E= github.com/tendermint/go-amino v0.16.0/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME= github.com/tendermint/spn v0.2.1-0.20221125172725-052fbf576cb2 h1:3sf/j+p+XFSenqu1DNcjvW01dSPpvx06Xef3EiSvqhg= From 8eba75860aebcecc24f5aec8392798dfc94db327 Mon Sep 17 00:00:00 2001 From: ltacker Date: Thu, 5 Jan 2023 14:59:58 +0100 Subject: [PATCH 04/11] request action and result description methods --- network/networktypes/request.go | 59 ++++++++++++++ network/networktypes/request_test.go | 111 +++++++++++++++++++++++++++ 2 files changed, 170 insertions(+) diff --git a/network/networktypes/request.go b/network/networktypes/request.go index a7820c9..c1ed684 100644 --- a/network/networktypes/request.go +++ b/network/networktypes/request.go @@ -10,6 +10,25 @@ import ( "github.com/ignite/cli/ignite/pkg/xtime" ) +// Request action descriptions +const ( + RequestActionAddAccount = "add account to the network" + RequestActionAddValidator = "join the network as a validator" + RequestActionAddVestingAccount = "add vesting account to the network" + RequestActionRemoveAccount = "remove account from the network" + RequestActionRemoveValidator = "remove validator from the network" + RequestActionChangeParams = "change param on the network" + + RequestActionResultAddAccount = "account added to the network" + RequestActionResultAddValidator = "Validator added to the network" + RequestActionResultAddVestingAccount = "vesting account added to the network" + RequestActionResultRemoveAccount = "account removed from network" + RequestActionResultRemoveValidator = "validator removed from network" + RequestActionResultChangeParams = "param changed on network" + + RequestActionUnrecognized = "" +) + type ( // Request represents the launch Request of a chain on SPN. Request struct { @@ -48,6 +67,46 @@ func RequestsFromRequestContents(launchID uint64, contents []launchtypes.Request return requests } +// RequestActionDescriptionFromContent describes the action of the request from its content +func RequestActionDescriptionFromContent(content launchtypes.RequestContent) string { + switch content.Content.(type) { + case *launchtypes.RequestContent_GenesisAccount: + return RequestActionAddAccount + case *launchtypes.RequestContent_GenesisValidator: + return RequestActionAddValidator + case *launchtypes.RequestContent_VestingAccount: + return RequestActionAddVestingAccount + case *launchtypes.RequestContent_AccountRemoval: + return RequestActionRemoveAccount + case *launchtypes.RequestContent_ValidatorRemoval: + return RequestActionRemoveValidator + case *launchtypes.RequestContent_ParamChange: + return RequestActionChangeParams + default: + return RequestActionUnrecognized + } +} + +// RequestActionResultDescriptionFromContent describe the result of the action of the request from its content +func RequestActionResultDescriptionFromContent(content launchtypes.RequestContent) string { + switch content.Content.(type) { + case *launchtypes.RequestContent_GenesisAccount: + return RequestActionResultAddAccount + case *launchtypes.RequestContent_GenesisValidator: + return RequestActionResultAddValidator + case *launchtypes.RequestContent_VestingAccount: + return RequestActionResultAddVestingAccount + case *launchtypes.RequestContent_AccountRemoval: + return RequestActionResultRemoveAccount + case *launchtypes.RequestContent_ValidatorRemoval: + return RequestActionResultRemoveValidator + case *launchtypes.RequestContent_ParamChange: + return RequestActionResultChangeParams + default: + return RequestActionUnrecognized + } +} + // VerifyRequest verifies the validity of the request from its content (static check). func VerifyRequest(request Request) error { req, ok := request.Content.Content.(*launchtypes.RequestContent_GenesisValidator) diff --git a/network/networktypes/request_test.go b/network/networktypes/request_test.go index 24bd4d7..4c13f98 100644 --- a/network/networktypes/request_test.go +++ b/network/networktypes/request_test.go @@ -3,17 +3,36 @@ package networktypes_test import ( "encoding/base64" "fmt" + "math/rand" "testing" sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" + spnsample "github.com/tendermint/spn/testutil/sample" launchtypes "github.com/tendermint/spn/x/launch/types" "github.com/tendermint/tendermint/crypto/ed25519" "github.com/ignite/cli-plugin-network/network/networktypes" ) +var ( + r = rand.New(rand.NewSource(1)) + SampleRequestAddAccount = launchtypes.NewGenesisAccount(0, "spn1dd246y", spnsample.Coins(r)) + SampleRequestAddValidator = launchtypes.NewGenesisValidator( + 0, + "spn1dd246y", + spnsample.Bytes(r, 300), + spnsample.Bytes(r, 30), + spnsample.Coin(r), + spnsample.GenesisValidatorPeer(r), + ) + SampleRequestAddVestingAccount = launchtypes.NewVestingAccount(0, "spn1dd246y", spnsample.VestingOptions(r)) + SampleRequestRemoveAccount = launchtypes.NewAccountRemoval("spn1dd246y") + SampleRequestRemoveValidator = launchtypes.NewValidatorRemoval("spn1dd246y") + SampleRequestChangeParam = launchtypes.NewParamChange(0, "foo", "bar", spnsample.Bytes(r, 30)) +) + func TestRequestsFromRequestContents(t *testing.T) { tests := []struct { name string @@ -106,6 +125,98 @@ func TestRequestsFromRequestContents(t *testing.T) { } } +func TestRequestActionDescriptionFromContent(t *testing.T) { + tests := []struct { + name string + req launchtypes.RequestContent + want string + }{ + { + name: "add account request content should return correct description", + req: SampleRequestAddAccount, + want: networktypes.RequestActionAddAccount, + }, + { + name: "add validator request content should return correct description", + req: SampleRequestAddValidator, + want: networktypes.RequestActionAddValidator, + }, + { + name: "add vesting account request content should return correct description", + req: SampleRequestAddVestingAccount, + want: networktypes.RequestActionAddVestingAccount, + }, + { + name: "remove account request content should return correct description", + req: SampleRequestRemoveAccount, + want: networktypes.RequestActionRemoveAccount, + }, + { + name: "remove validator request content should return correct description", + req: SampleRequestRemoveValidator, + want: networktypes.RequestActionRemoveValidator, + }, + { + name: "change params request content should return correct description", + req: SampleRequestChangeParam, + want: networktypes.RequestActionChangeParams, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := networktypes.RequestActionDescriptionFromContent(tt.req) + require.Equal(t, tt.want, got) + }) + } +} + +func TestRequestActionResultDescriptionFromContent(t *testing.T) { + tests := []struct { + name string + req launchtypes.RequestContent + want string + }{ + { + name: "add account request content should return correct result description", + req: SampleRequestAddAccount, + want: networktypes.RequestActionResultAddAccount, + }, + { + name: "add validator request content should return correct result description", + req: SampleRequestAddValidator, + want: networktypes.RequestActionResultAddValidator, + }, + { + name: "add vesting account request content should return correct result description", + req: SampleRequestAddVestingAccount, + want: networktypes.RequestActionResultAddVestingAccount, + }, + { + name: "remove account request content should return correct result description", + req: SampleRequestRemoveAccount, + want: networktypes.RequestActionResultRemoveAccount, + }, + { + name: "remove validator request content should return correct result description", + req: SampleRequestRemoveValidator, + want: networktypes.RequestActionResultRemoveValidator, + }, + { + name: "change params request content should return correct result description", + req: SampleRequestChangeParam, + want: networktypes.RequestActionResultChangeParams, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := networktypes.RequestActionResultDescriptionFromContent(tt.req) + require.Equal(t, tt.want, got) + }) + } +} + func TestVerifyAddValidatorRequest(t *testing.T) { gentx := []byte(`{ "body": { From 507787e6074fa0f84b97494365713d7ff3e33d51 Mon Sep 17 00:00:00 2001 From: ltacker Date: Thu, 5 Jan 2023 15:00:23 +0100 Subject: [PATCH 05/11] rename submit request --- cmd/network_request_approve.go | 2 +- cmd/network_request_reject.go | 2 +- network/request.go | 46 ++++++++++++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/cmd/network_request_approve.go b/cmd/network_request_approve.go index ef752b4..922654d 100644 --- a/cmd/network_request_approve.go +++ b/cmd/network_request_approve.go @@ -102,7 +102,7 @@ func networkRequestApproveHandler(cmd *cobra.Command, args []string) error { for _, id := range ids { reviewals = append(reviewals, network.ApproveRequest(id)) } - if err := n.SubmitRequest(cmd.Context(), launchID, reviewals...); err != nil { + if err := n.SubmitRequestReviewals(cmd.Context(), launchID, reviewals...); err != nil { return err } diff --git a/cmd/network_request_reject.go b/cmd/network_request_reject.go index c8c06a2..575e4bd 100644 --- a/cmd/network_request_reject.go +++ b/cmd/network_request_reject.go @@ -63,7 +63,7 @@ func networkRequestRejectHandler(cmd *cobra.Command, args []string) error { for _, id := range ids { reviewals = append(reviewals, network.RejectRequest(id)) } - if err := n.SubmitRequest(cmd.Context(), launchID, reviewals...); err != nil { + if err := n.SubmitRequestReviewals(cmd.Context(), launchID, reviewals...); err != nil { return err } diff --git a/network/request.go b/network/request.go index a36e8eb..575f81d 100644 --- a/network/request.go +++ b/network/request.go @@ -75,8 +75,8 @@ func (n Network) RequestFromIDs(ctx context.Context, launchID uint64, requestIDs return reqs, nil } -// SubmitRequest submits reviewals for proposals in batch for chain. -func (n Network) SubmitRequest(ctx context.Context, launchID uint64, reviewal ...Reviewal) error { +// SubmitRequestReviewals submits reviewals for proposals in batch for chain. +func (n Network) SubmitRequestReviewals(ctx context.Context, launchID uint64, reviewal ...Reviewal) error { n.ev.Send("Submitting requests...", events.ProgressStart()) addr, err := n.account.Address(networktypes.SPN) @@ -339,3 +339,45 @@ func (n Network) SendParamChangeRequest( } return nil } + +// SendRequest creates the Request message to SPN. +func (n Network) SendRequest( + ctx context.Context, + launchID uint64, + content launchtypes.Content, +) error { + addr, err := n.account.Address(networktypes.SPN) + if err != nil { + return err + } + + msg := launchtypes.NewMsgSendRequest( + addr, + launchID, + content, + ) + + n.ev.Send("Broadcasting transaction", events.ProgressStart()) + + res, err := n.cosmos.BroadcastTx(ctx, n.account, msg) + if err != nil { + return err + } + + var requestRes launchtypes.MsgSendRequestResponse + if err := res.Decode(&requestRes); err != nil { + return err + } + + if requestRes.AutoApproved { + n.ev.Send("Request executed by the coordinator!", events.ProgressFinish()) + } else { + n.ev.Send( + fmt.Sprintf( + "Request %d has been submitted!", requestRes.RequestID, + ), + events.ProgressFinish(), + ) + } + return nil +} \ No newline at end of file From a41b4df5ec5d2c9231675cda2715e37174b6d48d Mon Sep 17 00:00:00 2001 From: ltacker Date: Thu, 5 Jan 2023 15:12:13 +0100 Subject: [PATCH 06/11] refactor send request logic --- network/request.go | 254 ++------------------------------------------- 1 file changed, 10 insertions(+), 244 deletions(-) diff --git a/network/request.go b/network/request.go index 575f81d..f57f792 100644 --- a/network/request.go +++ b/network/request.go @@ -5,8 +5,6 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/ignite/cli/ignite/pkg/cliui/icons" - "github.com/ignite/cli/ignite/pkg/cosmosutil" "github.com/ignite/cli/ignite/pkg/events" launchtypes "github.com/tendermint/spn/x/launch/types" @@ -103,248 +101,11 @@ func (n Network) SubmitRequestReviewals(ctx context.Context, launchID uint64, re return res.Decode(&requestRes) } -// SendAccountRequest creates an add AddAccount request message. -func (n Network) SendAccountRequest( - ctx context.Context, - launchID uint64, - address string, - amount sdk.Coins, -) error { - addr, err := n.account.Address(networktypes.SPN) - if err != nil { - return err - } - - msg := launchtypes.NewMsgSendRequest( - addr, - launchID, - launchtypes.NewGenesisAccount( - launchID, - address, - amount, - ), - ) - - n.ev.Send("Broadcasting account transactions", events.ProgressStart()) - - res, err := n.cosmos.BroadcastTx(ctx, n.account, msg) - if err != nil { - return err - } - - var requestRes launchtypes.MsgSendRequestResponse - if err := res.Decode(&requestRes); err != nil { - return err - } - - if requestRes.AutoApproved { - n.ev.Send( - "Account added to the network by the coordinator!", - events.Icon(icons.Bullet), - events.ProgressFinish(), - ) - } else { - n.ev.Send( - fmt.Sprintf("Request %d to add account to the network has been submitted!", requestRes.RequestID), - events.Icon(icons.Bullet), - events.ProgressFinish(), - ) - } - return nil -} - -// SendValidatorRequest creates the RequestAddValidator message into the SPN. -func (n Network) SendValidatorRequest( - ctx context.Context, - launchID uint64, - peer launchtypes.Peer, - valAddress string, - gentx []byte, - gentxInfo cosmosutil.GentxInfo, -) error { - addr, err := n.account.Address(networktypes.SPN) - if err != nil { - return err - } - - msg := launchtypes.NewMsgSendRequest( - addr, - launchID, - launchtypes.NewGenesisValidator( - launchID, - valAddress, - gentx, - gentxInfo.PubKey, - gentxInfo.SelfDelegation, - peer, - ), - ) - - n.ev.Send("Broadcasting validator transaction", events.ProgressStart()) - - res, err := n.cosmos.BroadcastTx(ctx, n.account, msg) - if err != nil { - return err - } - - var requestRes launchtypes.MsgSendRequestResponse - if err := res.Decode(&requestRes); err != nil { - return err - } - - if requestRes.AutoApproved { - n.ev.Send("Validator added to the network by the coordinator!", events.ProgressFinish()) - } else { - n.ev.Send( - fmt.Sprintf("Request %d to join the network as a validator has been submitted!", requestRes.RequestID), - events.ProgressFinish(), - ) - } - return nil -} - -// SendValidatorRemoveRequest creates the RequestRemoveValidator message to SPN. -func (n Network) SendValidatorRemoveRequest( - ctx context.Context, - launchID uint64, - valAddress string, -) error { - addr, err := n.account.Address(networktypes.SPN) - if err != nil { - return err - } - - msg := launchtypes.NewMsgSendRequest( - addr, - launchID, - launchtypes.NewValidatorRemoval( - valAddress, - ), - ) - - n.ev.Send("Broadcasting transaction", events.ProgressStart()) - - res, err := n.cosmos.BroadcastTx(ctx, n.account, msg) - if err != nil { - return err - } - - var requestRes launchtypes.MsgSendRequestResponse - if err := res.Decode(&requestRes); err != nil { - return err - } - - if requestRes.AutoApproved { - n.ev.Send("Validator removed from network by the coordinator!", events.ProgressFinish()) - } else { - n.ev.Send( - fmt.Sprintf( - "Request %d to remove validator from the network has been submitted!", requestRes.RequestID, - ), - events.ProgressFinish(), - ) - } - return nil -} - -// SendAccountRemoveRequest creates the RequestRemoveAccount message to SPN. -func (n Network) SendAccountRemoveRequest( - ctx context.Context, - launchID uint64, - address string, -) error { - addr, err := n.account.Address(networktypes.SPN) - if err != nil { - return err - } - - msg := launchtypes.NewMsgSendRequest( - addr, - launchID, - launchtypes.NewAccountRemoval( - address, - ), - ) - - n.ev.Send("Broadcasting transaction", events.ProgressStart()) - - res, err := n.cosmos.BroadcastTx(ctx, n.account, msg) - if err != nil { - return err - } - - var requestRes launchtypes.MsgSendRequestResponse - if err := res.Decode(&requestRes); err != nil { - return err - } - - if requestRes.AutoApproved { - n.ev.Send("Account removed from network by the coordinator!", events.ProgressFinish()) - } else { - n.ev.Send( - fmt.Sprintf( - "Request %d to remove account from the network has been submitted!", requestRes.RequestID, - ), - events.ProgressFinish(), - ) - } - return nil -} - -// SendParamChangeRequest creates the RequestParamChange message to SPN. -func (n Network) SendParamChangeRequest( - ctx context.Context, - launchID uint64, - module, - param string, - value []byte, -) error { - addr, err := n.account.Address(networktypes.SPN) - if err != nil { - return err - } - - msg := launchtypes.NewMsgSendRequest( - addr, - launchID, - launchtypes.NewParamChange( - launchID, - module, - param, - value, - ), - ) - - n.ev.Send("Broadcasting transaction", events.ProgressStart()) - - res, err := n.cosmos.BroadcastTx(ctx, n.account, msg) - if err != nil { - return err - } - - var requestRes launchtypes.MsgSendRequestResponse - if err := res.Decode(&requestRes); err != nil { - return err - } - - if requestRes.AutoApproved { - n.ev.Send("Param changed on network by the coordinator!", events.ProgressFinish()) - } else { - n.ev.Send( - fmt.Sprintf( - "Request %d to change param on the network has been submitted!", requestRes.RequestID, - ), - events.ProgressFinish(), - ) - } - return nil -} - -// SendRequest creates the Request message to SPN. +// SendRequest creates and sends the Request message to SPN. func (n Network) SendRequest( ctx context.Context, launchID uint64, - content launchtypes.Content, + content launchtypes.RequestContent, ) error { addr, err := n.account.Address(networktypes.SPN) if err != nil { @@ -370,14 +131,19 @@ func (n Network) SendRequest( } if requestRes.AutoApproved { - n.ev.Send("Request executed by the coordinator!", events.ProgressFinish()) + n.ev.Send(fmt.Sprintf( + "%s by the coordinator!", networktypes.RequestActionResultDescriptionFromContent(content), + ), + events.ProgressFinish()) } else { n.ev.Send( fmt.Sprintf( - "Request %d has been submitted!", requestRes.RequestID, + "Request %d to %s has been submitted!", + requestRes.RequestID, + networktypes.RequestActionDescriptionFromContent(content), ), events.ProgressFinish(), ) } return nil -} \ No newline at end of file +} From 7da3e18da716550476bb8998e78f13a0ef5ea292 Mon Sep 17 00:00:00 2001 From: ltacker Date: Fri, 6 Jan 2023 11:46:48 +0100 Subject: [PATCH 07/11] refactor join requests logic --- cmd/network_chain_join.go | 21 +++++++- cmd/network_chain_publish.go | 15 +++++- cmd/network_request_approve.go | 2 +- cmd/network_request_verify.go | 97 +++++++++++++++++++++++++--------- network/join.go | 41 +++++++------- network/network.go | 6 +++ network/request.go | 14 +++++ 7 files changed, 148 insertions(+), 48 deletions(-) diff --git a/cmd/network_chain_join.go b/cmd/network_chain_join.go index bf7cf2e..a8aec14 100644 --- a/cmd/network_chain_join.go +++ b/cmd/network_chain_join.go @@ -178,8 +178,25 @@ func networkChainJoinHandler(cmd *cobra.Command, args []string) error { } } - // create the message to add the validator. - return n.Join(cmd.Context(), c, launchID, gentxPath, joinOptions...) + // create requests to join as a validator + joinRequests, err := n.GetJoinRequestContents(cmd.Context(), c, launchID, gentxPath, joinOptions...) + if err != nil { + return err + } + + // simulate the join requests + if err := verifyRequestsFromRequestContents( + cmd.Context(), + cacheStorage, + nb, + launchID, + joinRequests..., + ); err != nil { + return err + } + + // send join requests + return n.SendRequests(cmd.Context(), launchID, joinRequests) } // askPublicAddress prepare questions to interactively ask for a publicAddress diff --git a/cmd/network_chain_publish.go b/cmd/network_chain_publish.go index 0961890..a76f202 100644 --- a/cmd/network_chain_publish.go +++ b/cmd/network_chain_publish.go @@ -11,6 +11,7 @@ import ( "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/tendermint/spn/pkg/chainid" + launchtypes "github.com/tendermint/spn/x/launch/types" "github.com/ignite/cli-plugin-network/network" "github.com/ignite/cli-plugin-network/network/networkchain" @@ -323,7 +324,19 @@ func networkChainPublishHandler(cmd *cobra.Command, args []string) error { } if !amountCoins.IsZero() { - if err := n.SendAccountRequestForCoordinator(cmd.Context(), launchID, amountCoins); err != nil { + // create a request to add an account to the genesis + addr, err := n.AccountAddress() + if err != nil { + return err + } + addAccountRequest := launchtypes.NewGenesisAccount( + launchID, + addr, + amountCoins, + ) + + // send the request + if err := n.SendRequest(cmd.Context(), launchID, addAccountRequest); err != nil { return err } } diff --git a/cmd/network_request_approve.go b/cmd/network_request_approve.go index 922654d..2a5ce02 100644 --- a/cmd/network_request_approve.go +++ b/cmd/network_request_approve.go @@ -91,7 +91,7 @@ func networkRequestApproveHandler(cmd *cobra.Command, args []string) error { // if requests must be verified, we simulate the chain in a temporary directory with the requests if !noVerification { - if err := verifyRequest(cmd.Context(), cacheStorage, nb, launchID, ids...); err != nil { + if err := verifyRequests(cmd.Context(), cacheStorage, nb, launchID, ids...); err != nil { return errors.Wrap(err, "request(s) not valid") } session.Printf("%s Request(s) %s verified\n", icons.OK, numbers.List(ids, "#")) diff --git a/cmd/network_request_verify.go b/cmd/network_request_verify.go index ef7397b..7d5946d 100644 --- a/cmd/network_request_verify.go +++ b/cmd/network_request_verify.go @@ -10,9 +10,11 @@ import ( "github.com/ignite/cli/ignite/pkg/cliui/icons" "github.com/ignite/cli/ignite/pkg/numbers" "github.com/spf13/cobra" + launchtypes "github.com/tendermint/spn/x/launch/types" "github.com/ignite/cli-plugin-network/network" "github.com/ignite/cli-plugin-network/network/networkchain" + "github.com/ignite/cli-plugin-network/network/networktypes" ) // NewNetworkRequestVerify verify the request and simulate the chain. @@ -64,8 +66,7 @@ func networkRequestVerifyHandler(cmd *cobra.Command, args []string) error { } // verify the requests - - if err := verifyRequest(cmd.Context(), cacheStorage, nb, launchID, ids...); err != nil { + if err := verifyRequests(cmd.Context(), cacheStorage, nb, launchID, ids...); err != nil { session.Printf("%s Request(s) %s not valid\n", icons.NotOK, numbers.List(ids, "#")) return err } @@ -73,56 +74,104 @@ func networkRequestVerifyHandler(cmd *cobra.Command, args []string) error { return session.Printf("%s Request(s) %s verified\n", icons.OK, numbers.List(ids, "#")) } -// 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( +// verifyRequests initializes the chain from the launch ID in a temporary directory +// and simulate the launch of the chain from genesis with the request IDs +func verifyRequests( ctx context.Context, cacheStorage cache.Storage, nb NetworkBuilder, launchID uint64, requestIDs ...uint64, ) error { - n, err := nb.Network() + // initialize the chain for simulation + c, n, genesisInformation, cleanup, err := initializeSimulationEnvironment(ctx, cacheStorage, nb, launchID) if err != nil { return err } + defer cleanup() - // initialize the chain with a temporary dir - chainLaunch, err := n.ChainLaunch(ctx, launchID) + // fetch the requests from the network + requests, err := n.RequestFromIDs(ctx, launchID, requestIDs...) if err != nil { return err } - homeDir, err := os.MkdirTemp("", "") + return c.SimulateRequests( + ctx, + cacheStorage, + genesisInformation, + requests, + ) +} + +// verifyRequestsFromRequestContents initializes the chain from the launch ID in a temporary directory +// and simulate the launch of the chain from genesis with the request contents +func verifyRequestsFromRequestContents( + ctx context.Context, + cacheStorage cache.Storage, + nb NetworkBuilder, + launchID uint64, + requestContents ...launchtypes.RequestContent, +) error { + // initialize the chain for simulation + c, _, genesisInformation, cleanup, err := initializeSimulationEnvironment(ctx, cacheStorage, nb, launchID) if err != nil { return err } - defer os.RemoveAll(homeDir) + defer cleanup() - c, err := nb.Chain( - networkchain.SourceLaunch(chainLaunch), - networkchain.WithHome(homeDir), - networkchain.WithKeyringBackend(chaincmd.KeyringBackendTest), + return c.SimulateRequests( + ctx, + cacheStorage, + genesisInformation, + networktypes.RequestsFromRequestContents(launchID, requestContents), ) +} + +// initializeSimulationEnvironment initializes the chain from the launch ID in a temporary directory for simulating requests +func initializeSimulationEnvironment( + ctx context.Context, + cacheStorage cache.Storage, + nb NetworkBuilder, + launchID uint64, +) ( + c *networkchain.Chain, + n network.Network, + gi networktypes.GenesisInformation, + cleanup func(), + err error, +) { + n, err = nb.Network() if err != nil { - return err + return c, n, gi, cleanup, err } // fetch the current genesis information and the requests for the chain for simulation - genesisInformation, err := n.GenesisInformation(ctx, launchID) + gi, err = n.GenesisInformation(ctx, launchID) if err != nil { - return err + return c, n, gi, cleanup, err } - requests, err := n.RequestFromIDs(ctx, launchID, requestIDs...) + // initialize the chain with a temporary dir + chainLaunch, err := n.ChainLaunch(ctx, launchID) if err != nil { - return err + return c, n, gi, cleanup, err } - return c.SimulateRequests( - ctx, - cacheStorage, - genesisInformation, - requests, + homeDir, err := os.MkdirTemp("", "") + if err != nil { + return c, n, gi, cleanup, err + } + + c, err = nb.Chain( + networkchain.SourceLaunch(chainLaunch), + networkchain.WithHome(homeDir), + networkchain.WithKeyringBackend(chaincmd.KeyringBackendTest), ) + if err != nil { + os.RemoveAll(homeDir) + return c, n, gi, cleanup, err + } + + return c, n, gi, func() { os.RemoveAll(homeDir) }, nil } diff --git a/network/join.go b/network/join.go index be24d4c..ad900d1 100644 --- a/network/join.go +++ b/network/join.go @@ -33,14 +33,14 @@ func WithPublicAddress(addr string) JoinOption { } } -// Join to the network. -func (n Network) Join( +// GetJoinRequestContents returns the request contents to join a chain as a validator +func (n Network) GetJoinRequestContents( ctx context.Context, c Chain, launchID uint64, gentxPath string, options ...JoinOption, -) error { +) (reqs []launchtypes.RequestContent, err error) { o := joinOptions{} for _, apply := range options { apply(&o) @@ -49,19 +49,18 @@ func (n Network) Join( var ( nodeID string peer launchtypes.Peer - err error ) // parse the gentx content gentxInfo, gentx, err := cosmosutil.GentxFromPath(gentxPath) if err != nil { - return err + return reqs, err } // get the peer address if o.publicAddress != "" { if nodeID, err = c.NodeID(ctx); err != nil { - return err + return reqs, err } if xurl.IsHTTP(o.publicAddress) { @@ -72,30 +71,32 @@ func (n Network) Join( } else { // if the peer address is not specified, we parse it from the gentx memo if peer, err = ParsePeerAddress(gentxInfo.Memo); err != nil { - return err + return reqs, err } } // change the chain address prefix to spn accountAddress, err := cosmosutil.ChangeAddressPrefix(gentxInfo.DelegatorAddress, networktypes.SPN) if err != nil { - return err + return reqs, err } if !o.accountAmount.IsZero() { - if err := n.SendAccountRequest(ctx, launchID, accountAddress, o.accountAmount); err != nil { - return err - } + reqs = append(reqs, launchtypes.NewGenesisAccount( + launchID, + accountAddress, + o.accountAmount, + )) } - return n.SendValidatorRequest(ctx, launchID, peer, accountAddress, gentx, gentxInfo) -} - -func (n Network) SendAccountRequestForCoordinator(ctx context.Context, launchID uint64, amount sdk.Coins) error { - addr, err := n.account.Address(networktypes.SPN) - if err != nil { - return err - } + reqs = append(reqs, launchtypes.NewGenesisValidator( + launchID, + accountAddress, + gentx, + gentxInfo.PubKey, + gentxInfo.SelfDelegation, + peer, + )) - return n.SendAccountRequest(ctx, launchID, addr, amount) + return reqs, nil } diff --git a/network/network.go b/network/network.go index c74e57f..7f60905 100644 --- a/network/network.go +++ b/network/network.go @@ -16,6 +16,7 @@ import ( rewardtypes "github.com/tendermint/spn/x/reward/types" ctypes "github.com/tendermint/tendermint/rpc/core/types" + "github.com/ignite/cli-plugin-network/network/networktypes" "github.com/ignite/cli/ignite/pkg/cosmosaccount" "github.com/ignite/cli/ignite/pkg/cosmosclient" "github.com/ignite/cli/ignite/pkg/events" @@ -150,3 +151,8 @@ func ParseID(id string) (uint64, error) { } return objID, nil } + +// AccountAddress returns the address of the account used by the network builder +func (n Network) AccountAddress() (string, error) { + return n.account.Address(networktypes.SPN) +} diff --git a/network/request.go b/network/request.go index f57f792..8f66ed9 100644 --- a/network/request.go +++ b/network/request.go @@ -147,3 +147,17 @@ func (n Network) SendRequest( } return nil } + +// SendRequests creates and sends the Request message to SPN +func (n Network) SendRequests( + ctx context.Context, + launchID uint64, + contents []launchtypes.RequestContent, +) error { + for _, content := range contents { + if err := n.SendRequest(ctx, launchID, content); err != nil { + return err + } + } + return nil +} From 0806450aad964a4d3d48dc8c7d8dfdc025976260 Mon Sep 17 00:00:00 2001 From: ltacker Date: Fri, 6 Jan 2023 11:57:41 +0100 Subject: [PATCH 08/11] add verification to request sending --- cmd/network_chain_publish.go | 11 +++++++++++ cmd/network_request_add_account.go | 24 ++++++++++++++++++++++-- cmd/network_request_change_param.go | 24 ++++++++++++++++++++++-- cmd/network_request_remove_account.go | 25 ++++++++++++++++++++++--- cmd/network_request_remove_validator.go | 25 ++++++++++++++++++++++--- 5 files changed, 99 insertions(+), 10 deletions(-) diff --git a/cmd/network_chain_publish.go b/cmd/network_chain_publish.go index a76f202..47283b3 100644 --- a/cmd/network_chain_publish.go +++ b/cmd/network_chain_publish.go @@ -335,6 +335,17 @@ func networkChainPublishHandler(cmd *cobra.Command, args []string) error { amountCoins, ) + // simulate the add account request + if err := verifyRequestsFromRequestContents( + cmd.Context(), + cacheStorage, + nb, + launchID, + addAccountRequest, + ); err != nil { + return err + } + // send the request if err := n.SendRequest(cmd.Context(), launchID, addAccountRequest); err != nil { return err diff --git a/cmd/network_request_add_account.go b/cmd/network_request_add_account.go index ec46e32..740368e 100644 --- a/cmd/network_request_add_account.go +++ b/cmd/network_request_add_account.go @@ -8,6 +8,7 @@ import ( "github.com/ignite/cli/ignite/pkg/cliui" "github.com/ignite/cli/ignite/pkg/cosmosutil" "github.com/spf13/cobra" + launchtypes "github.com/tendermint/spn/x/launch/types" "github.com/ignite/cli-plugin-network/network" "github.com/ignite/cli-plugin-network/network/networkchain" @@ -47,6 +48,11 @@ func networkRequestAddAccountHandler(cmd *cobra.Command, args []string) error { session := cliui.New(cliui.StartSpinner()) defer session.End() + cacheStorage, err := newCache(cmd) + if err != nil { + return err + } + nb, err := newNetworkBuilder(cmd, CollectEvents(session.EventBus())) if err != nil { return err @@ -99,10 +105,24 @@ func networkRequestAddAccountHandler(cmd *cobra.Command, args []string) error { } } - return n.SendAccountRequest( - cmd.Context(), + // create the add account request + addAccountRequest := launchtypes.NewGenesisAccount( launchID, address, balance, ) + + // simulate the add account request + if err := verifyRequestsFromRequestContents( + cmd.Context(), + cacheStorage, + nb, + launchID, + addAccountRequest, + ); err != nil { + return err + } + + // send the request + return n.SendRequest(cmd.Context(), launchID, addAccountRequest) } diff --git a/cmd/network_request_change_param.go b/cmd/network_request_change_param.go index abe353f..0228883 100644 --- a/cmd/network_request_change_param.go +++ b/cmd/network_request_change_param.go @@ -3,6 +3,7 @@ package cmd import ( "github.com/ignite/cli/ignite/pkg/cliui" "github.com/spf13/cobra" + launchtypes "github.com/tendermint/spn/x/launch/types" "github.com/ignite/cli-plugin-network/network" "github.com/ignite/cli-plugin-network/network/networkchain" @@ -29,6 +30,11 @@ func networkRequestChangeParamHandler(cmd *cobra.Command, args []string) error { session := cliui.New(cliui.StartSpinner()) defer session.End() + cacheStorage, err := newCache(cmd) + if err != nil { + return err + } + nb, err := newNetworkBuilder(cmd, CollectEvents(session.EventBus())) if err != nil { return err @@ -71,11 +77,25 @@ func networkRequestChangeParamHandler(cmd *cobra.Command, args []string) error { return err } - return n.SendParamChangeRequest( - cmd.Context(), + // create the param change request + paramChangeRequest := launchtypes.NewParamChange( launchID, module, param, value, ) + + // simulate the param change request + if err := verifyRequestsFromRequestContents( + cmd.Context(), + cacheStorage, + nb, + launchID, + paramChangeRequest, + ); err != nil { + return err + } + + // send the request + return n.SendRequest(cmd.Context(), launchID, paramChangeRequest) } diff --git a/cmd/network_request_remove_account.go b/cmd/network_request_remove_account.go index 8305107..9df841d 100644 --- a/cmd/network_request_remove_account.go +++ b/cmd/network_request_remove_account.go @@ -4,6 +4,7 @@ import ( "github.com/ignite/cli/ignite/pkg/cliui" "github.com/ignite/cli/ignite/pkg/cosmosutil" "github.com/spf13/cobra" + launchtypes "github.com/tendermint/spn/x/launch/types" "github.com/ignite/cli-plugin-network/network" "github.com/ignite/cli-plugin-network/network/networktypes" @@ -30,6 +31,11 @@ func networkRequestRemoveAccountHandler(cmd *cobra.Command, args []string) error session := cliui.New(cliui.StartSpinner()) defer session.End() + cacheStorage, err := newCache(cmd) + if err != nil { + return err + } + nb, err := newNetworkBuilder(cmd, CollectEvents(session.EventBus())) if err != nil { return err @@ -52,9 +58,22 @@ func networkRequestRemoveAccountHandler(cmd *cobra.Command, args []string) error return err } - return n.SendAccountRemoveRequest( - cmd.Context(), - launchID, + // create the add account request + removeAccountRequest := launchtypes.NewAccountRemoval( address, ) + + // simulate the add account request + if err := verifyRequestsFromRequestContents( + cmd.Context(), + cacheStorage, + nb, + launchID, + removeAccountRequest, + ); err != nil { + return err + } + + // send the request + return n.SendRequest(cmd.Context(), launchID, removeAccountRequest) } diff --git a/cmd/network_request_remove_validator.go b/cmd/network_request_remove_validator.go index b540c9a..0eaa7d1 100644 --- a/cmd/network_request_remove_validator.go +++ b/cmd/network_request_remove_validator.go @@ -4,6 +4,7 @@ import ( "github.com/ignite/cli/ignite/pkg/cliui" "github.com/ignite/cli/ignite/pkg/cosmosutil" "github.com/spf13/cobra" + launchtypes "github.com/tendermint/spn/x/launch/types" "github.com/ignite/cli-plugin-network/network" "github.com/ignite/cli-plugin-network/network/networktypes" @@ -30,6 +31,11 @@ func networkRequestRemoveValidatorHandler(cmd *cobra.Command, args []string) err session := cliui.New(cliui.StartSpinner()) defer session.End() + cacheStorage, err := newCache(cmd) + if err != nil { + return err + } + nb, err := newNetworkBuilder(cmd, CollectEvents(session.EventBus())) if err != nil { return err @@ -52,9 +58,22 @@ func networkRequestRemoveValidatorHandler(cmd *cobra.Command, args []string) err return err } - return n.SendValidatorRemoveRequest( - cmd.Context(), - launchID, + // create the remove validator request + removeValidatorRequest := launchtypes.NewValidatorRemoval( address, ) + + // simulate the remove validator request + if err := verifyRequestsFromRequestContents( + cmd.Context(), + cacheStorage, + nb, + launchID, + removeValidatorRequest, + ); err != nil { + return err + } + + // send the request + return n.SendRequest(cmd.Context(), launchID, removeValidatorRequest) } From 570b554c9b0ee22b1670a2ff438b9d752fd82386 Mon Sep 17 00:00:00 2001 From: ltacker Date: Fri, 6 Jan 2023 13:18:45 +0100 Subject: [PATCH 09/11] lint and format --- cmd/network_request_remove_account.go | 4 ++-- cmd/network_request_verify.go | 5 ++--- network/network.go | 3 ++- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/network_request_remove_account.go b/cmd/network_request_remove_account.go index 9df841d..6c34c3d 100644 --- a/cmd/network_request_remove_account.go +++ b/cmd/network_request_remove_account.go @@ -58,12 +58,12 @@ func networkRequestRemoveAccountHandler(cmd *cobra.Command, args []string) error return err } - // create the add account request + // create the remove account request removeAccountRequest := launchtypes.NewAccountRemoval( address, ) - // simulate the add account request + // simulate the remove account request if err := verifyRequestsFromRequestContents( cmd.Context(), cacheStorage, diff --git a/cmd/network_request_verify.go b/cmd/network_request_verify.go index 7d5946d..b957cdd 100644 --- a/cmd/network_request_verify.go +++ b/cmd/network_request_verify.go @@ -84,7 +84,7 @@ func verifyRequests( requestIDs ...uint64, ) error { // initialize the chain for simulation - c, n, genesisInformation, cleanup, err := initializeSimulationEnvironment(ctx, cacheStorage, nb, launchID) + c, n, genesisInformation, cleanup, err := initializeSimulationEnvironment(ctx, nb, launchID) if err != nil { return err } @@ -114,7 +114,7 @@ func verifyRequestsFromRequestContents( requestContents ...launchtypes.RequestContent, ) error { // initialize the chain for simulation - c, _, genesisInformation, cleanup, err := initializeSimulationEnvironment(ctx, cacheStorage, nb, launchID) + c, _, genesisInformation, cleanup, err := initializeSimulationEnvironment(ctx, nb, launchID) if err != nil { return err } @@ -131,7 +131,6 @@ func verifyRequestsFromRequestContents( // initializeSimulationEnvironment initializes the chain from the launch ID in a temporary directory for simulating requests func initializeSimulationEnvironment( ctx context.Context, - cacheStorage cache.Storage, nb NetworkBuilder, launchID uint64, ) ( diff --git a/network/network.go b/network/network.go index 7f60905..b0f6cb6 100644 --- a/network/network.go +++ b/network/network.go @@ -16,11 +16,12 @@ import ( rewardtypes "github.com/tendermint/spn/x/reward/types" ctypes "github.com/tendermint/tendermint/rpc/core/types" - "github.com/ignite/cli-plugin-network/network/networktypes" "github.com/ignite/cli/ignite/pkg/cosmosaccount" "github.com/ignite/cli/ignite/pkg/cosmosclient" "github.com/ignite/cli/ignite/pkg/events" "github.com/ignite/cli/ignite/pkg/xtime" + + "github.com/ignite/cli-plugin-network/network/networktypes" ) //go:generate mockery --name CosmosClient --case underscore From 4f9be545763a79468b204e0103fd8352c8ca9210 Mon Sep 17 00:00:00 2001 From: ltacker Date: Fri, 6 Jan 2023 13:58:18 +0100 Subject: [PATCH 10/11] refactor tests --- network/join_test.go | 249 +++++++++++----------------------------- network/request_test.go | 9 +- 2 files changed, 77 insertions(+), 181 deletions(-) diff --git a/network/join_test.go b/network/join_test.go index 6d67186..ea9c683 100644 --- a/network/join_test.go +++ b/network/join_test.go @@ -24,7 +24,7 @@ const ( ) func TestJoin(t *testing.T) { - t.Run("successfully send join request", func(t *testing.T) { + t.Run("successfully get join request", func(t *testing.T) { account := testutil.NewTestAccount(t, testutil.TestAccountName) tmp := t.TempDir() addr, err := account.Address(networktypes.SPN) @@ -39,96 +39,35 @@ func TestJoin(t *testing.T) { gentxPath := gentx.SaveTo(t, tmp) suite, network := newSuite(account) - suite.ChainMock.On("NodeID", context.Background()).Return(testutil.NodeID, nil).Once() - suite.CosmosClientMock. - On( - "BroadcastTx", - context.Background(), - account, - launchtypes.NewMsgSendRequest( - addr, - testutil.LaunchID, - launchtypes.NewGenesisValidator( - testutil.LaunchID, - addr, - gentx.JSON(t), - []byte{}, - sdk.NewCoin(TestDenom, sdkmath.NewInt(TestAmountInt)), - launchtypes.Peer{ - Id: testutil.NodeID, - Connection: &launchtypes.Peer_TcpAddress{ - TcpAddress: testutil.TCPAddress, - }, - }), - ), - ). - Return(testutil.NewResponse(&launchtypes.MsgSendRequestResponse{ - RequestID: TestGenesisValidatorRequestID, - AutoApproved: false, - }), nil). - Once() + expectedReqs := []launchtypes.RequestContent{ + launchtypes.NewGenesisValidator( + testutil.LaunchID, + addr, + gentx.JSON(t), + []byte{}, + sdk.NewCoin(TestDenom, sdkmath.NewInt(TestAmountInt)), + launchtypes.Peer{ + Id: testutil.NodeID, + Connection: &launchtypes.Peer_TcpAddress{ + TcpAddress: testutil.TCPAddress, + }, + }, + ), + } - joinErr := network.Join( + suite.ChainMock.On("NodeID", context.Background()).Return(testutil.NodeID, nil).Once() + reqs, err := network.GetJoinRequestContents( context.Background(), suite.ChainMock, testutil.LaunchID, gentxPath, WithPublicAddress(testutil.TCPAddress), ) - require.NoError(t, joinErr) - suite.AssertAllMocks(t) - }) - - t.Run("successfully send join request with custom gentx", func(t *testing.T) { - account := testutil.NewTestAccount(t, testutil.TestAccountName) - tmp := t.TempDir() - addr, err := account.Address(networktypes.SPN) require.NoError(t, err) - gentx := testutil.NewGentx( - addr, - TestDenom, - TestAmountString, - "", - testutil.PeerAddress, - ) - gentxPath := gentx.SaveTo(t, tmp) - suite, network := newSuite(account) - - suite.CosmosClientMock. - On( - "BroadcastTx", - context.Background(), - account, - launchtypes.NewMsgSendRequest( - addr, - testutil.LaunchID, - launchtypes.NewGenesisValidator( - testutil.LaunchID, - addr, - gentx.JSON(t), - []byte{}, - sdk.NewCoin(TestDenom, sdkmath.NewInt(TestAmountInt)), - launchtypes.Peer{ - Id: testutil.NodeID, - Connection: &launchtypes.Peer_TcpAddress{ - TcpAddress: testutil.TCPAddress, - }, - }, - ), - ), - ). - Return(testutil.NewResponse(&launchtypes.MsgSendRequestResponse{ - RequestID: TestGenesisValidatorRequestID, - AutoApproved: false, - }), nil). - Once() - - joinErr := network.Join(context.Background(), suite.ChainMock, testutil.LaunchID, gentxPath) - require.NoError(t, joinErr) - suite.AssertAllMocks(t) + require.ElementsMatch(t, expectedReqs, reqs) }) - t.Run("failed to send join request, failed to broadcast join tx", func(t *testing.T) { + t.Run("successfully get join request with custom gentx", func(t *testing.T) { account := testutil.NewTestAccount(t, testutil.TestAccountName) tmp := t.TempDir() addr, err := account.Address(networktypes.SPN) @@ -142,51 +81,34 @@ func TestJoin(t *testing.T) { ) gentxPath := gentx.SaveTo(t, tmp) suite, network := newSuite(account) - expectedError := errors.New("failed to add validator") - suite.ChainMock.On("NodeID", context.Background()).Return(testutil.NodeID, nil).Once() - suite.CosmosClientMock. - On( - "BroadcastTx", - context.Background(), - account, - launchtypes.NewMsgSendRequest( - addr, - testutil.LaunchID, - launchtypes.NewGenesisValidator( - testutil.LaunchID, - addr, - gentx.JSON(t), - []byte{}, - sdk.NewCoin(TestDenom, sdkmath.NewInt(TestAmountInt)), - launchtypes.Peer{ - Id: testutil.NodeID, - Connection: &launchtypes.Peer_TcpAddress{ - TcpAddress: testutil.TCPAddress, - }, - }, - ), - ), - ). - Return( - testutil.NewResponse(&launchtypes.MsgSendRequestResponse{}), - expectedError, - ). - Once() - - joinErr := network.Join( + expectedReqs := []launchtypes.RequestContent{ + launchtypes.NewGenesisValidator( + testutil.LaunchID, + addr, + gentx.JSON(t), + []byte{}, + sdk.NewCoin(TestDenom, sdkmath.NewInt(TestAmountInt)), + launchtypes.Peer{ + Id: testutil.NodeID, + Connection: &launchtypes.Peer_TcpAddress{ + TcpAddress: testutil.TCPAddress, + }, + }, + ), + } + + reqs, err := network.GetJoinRequestContents( context.Background(), suite.ChainMock, testutil.LaunchID, gentxPath, - WithPublicAddress(testutil.TCPAddress), ) - require.Error(t, joinErr) - require.Equal(t, expectedError, joinErr) - suite.AssertAllMocks(t) + require.NoError(t, err) + require.ElementsMatch(t, expectedReqs, reqs) }) - t.Run("successfully send join request with account request", func(t *testing.T) { + t.Run("successfully get join request with account request", func(t *testing.T) { account := testutil.NewTestAccount(t, testutil.TestAccountName) tmp := t.TempDir() addr, err := account.Address(networktypes.SPN) @@ -201,57 +123,29 @@ func TestJoin(t *testing.T) { gentxPath := gentx.SaveTo(t, tmp) suite, network := newSuite(account) - suite.ChainMock.On("NodeID", context.Background()).Return(testutil.NodeID, nil).Once() - suite.CosmosClientMock. - On( - "BroadcastTx", - context.Background(), - account, - launchtypes.NewMsgSendRequest( - addr, - testutil.LaunchID, - launchtypes.NewGenesisValidator( - testutil.LaunchID, - addr, - gentx.JSON(t), - []byte{}, - sdk.NewCoin(TestDenom, sdkmath.NewInt(TestAmountInt)), - launchtypes.Peer{ - Id: testutil.NodeID, - Connection: &launchtypes.Peer_TcpAddress{ - TcpAddress: testutil.TCPAddress, - }, - }, - ), - ), - ). - Return(testutil.NewResponse(&launchtypes.MsgSendRequestResponse{ - RequestID: TestGenesisValidatorRequestID, - AutoApproved: false, - }), nil). - Once() - suite.CosmosClientMock. - On( - "BroadcastTx", - context.Background(), - account, - launchtypes.NewMsgSendRequest( - addr, - testutil.LaunchID, - launchtypes.NewGenesisAccount( - testutil.LaunchID, - addr, - sdk.NewCoins(sdk.NewCoin(TestDenom, sdkmath.NewInt(TestAmountInt))), - ), - ), - ). - Return(testutil.NewResponse(&launchtypes.MsgSendRequestResponse{ - RequestID: TestAccountRequestID, - AutoApproved: false, - }), nil). - Once() + expectedReqs := []launchtypes.RequestContent{ + launchtypes.NewGenesisValidator( + testutil.LaunchID, + addr, + gentx.JSON(t), + []byte{}, + sdk.NewCoin(TestDenom, sdkmath.NewInt(TestAmountInt)), + launchtypes.Peer{ + Id: testutil.NodeID, + Connection: &launchtypes.Peer_TcpAddress{ + TcpAddress: testutil.TCPAddress, + }, + }, + ), + launchtypes.NewGenesisAccount( + testutil.LaunchID, + addr, + sdk.NewCoins(sdk.NewCoin(TestDenom, sdkmath.NewInt(TestAmountInt))), + ), + } - joinErr := network.Join( + suite.ChainMock.On("NodeID", context.Background()).Return(testutil.NodeID, nil).Once() + reqs, err := network.GetJoinRequestContents( context.Background(), suite.ChainMock, testutil.LaunchID, @@ -259,11 +153,11 @@ func TestJoin(t *testing.T) { WithAccountRequest(sdk.NewCoins(sdk.NewCoin(TestDenom, sdkmath.NewInt(TestAmountInt)))), WithPublicAddress(testutil.TCPAddress), ) - require.NoError(t, joinErr) - suite.AssertAllMocks(t) + require.NoError(t, err) + require.ElementsMatch(t, expectedReqs, reqs) }) - t.Run("failed to send join request, failed to read node id", func(t *testing.T) { + t.Run("failed to get join request, failed to read node id", func(t *testing.T) { account := testutil.NewTestAccount(t, testutil.TestAccountName) tmp := t.TempDir() addr, err := account.Address(networktypes.SPN) @@ -284,29 +178,26 @@ func TestJoin(t *testing.T) { Return("", expectedError). Once() - joinErr := network.Join( + _, err = network.GetJoinRequestContents( context.Background(), suite.ChainMock, testutil.LaunchID, gentxPath, WithPublicAddress(testutil.TCPAddress), ) - require.Error(t, joinErr) - require.Equal(t, expectedError, joinErr) + require.ErrorIs(t, err, expectedError) suite.AssertAllMocks(t) }) - t.Run("failed to send join request, failed to read gentx", func(t *testing.T) { + t.Run("failed to get join request, failed to read gentx", func(t *testing.T) { var ( account = testutil.NewTestAccount(t, testutil.TestAccountName) gentxPath = "invalid/path" suite, network = newSuite(account) - expectedError = errors.New("chain home folder is not initialized yet: invalid/path") ) - joinErr := network.Join(context.Background(), suite.ChainMock, testutil.LaunchID, gentxPath) - require.Error(t, joinErr) - require.Equal(t, expectedError, joinErr) + _, err := network.GetJoinRequestContents(context.Background(), suite.ChainMock, testutil.LaunchID, gentxPath) + require.Error(t, err) suite.AssertAllMocks(t) }) } diff --git a/network/request_test.go b/network/request_test.go index 021a81e..9085685 100644 --- a/network/request_test.go +++ b/network/request_test.go @@ -11,7 +11,7 @@ import ( "github.com/ignite/cli-plugin-network/network/testutil" ) -func TestRequestParamChange(t *testing.T) { +func TestSendRequest(t *testing.T) { t.Run("successfully send request", func(t *testing.T) { var ( account = testutil.NewTestAccount(t, testutil.TestAccountName) @@ -46,7 +46,12 @@ func TestRequestParamChange(t *testing.T) { }), nil). Once() - sendRequestError := network.SendParamChangeRequest(context.Background(), testutil.LaunchID, module, param, value) + sendRequestError := network.SendRequest(context.Background(), testutil.LaunchID, launchtypes.NewParamChange( + testutil.LaunchID, + module, + param, + value, + )) require.NoError(t, sendRequestError) suite.AssertAllMocks(t) }) From 8bd97c5cfd9e64df130d8216079aea6d9238bd58 Mon Sep 17 00:00:00 2001 From: ltacker Date: Fri, 6 Jan 2023 16:45:59 +0100 Subject: [PATCH 11/11] change test names --- network/join_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/network/join_test.go b/network/join_test.go index ea9c683..fe38814 100644 --- a/network/join_test.go +++ b/network/join_test.go @@ -24,7 +24,7 @@ const ( ) func TestJoin(t *testing.T) { - t.Run("successfully get join request", func(t *testing.T) { + t.Run("successfully get join request with custom public address", func(t *testing.T) { account := testutil.NewTestAccount(t, testutil.TestAccountName) tmp := t.TempDir() addr, err := account.Address(networktypes.SPN) @@ -67,7 +67,7 @@ func TestJoin(t *testing.T) { require.ElementsMatch(t, expectedReqs, reqs) }) - t.Run("successfully get join request with custom gentx", func(t *testing.T) { + t.Run("successfully get join request with public address read from the gentx", func(t *testing.T) { account := testutil.NewTestAccount(t, testutil.TestAccountName) tmp := t.TempDir() addr, err := account.Address(networktypes.SPN)