From 641541cc99dda22718dc0d32d805929c1303f2b6 Mon Sep 17 00:00:00 2001 From: ltacker Date: Thu, 9 Dec 2021 17:20:56 +0100 Subject: [PATCH 01/22] init --- .../services/network/networktypes/genesisinformation.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/starport/services/network/networktypes/genesisinformation.go b/starport/services/network/networktypes/genesisinformation.go index 01966ed2e5..ecc6f2fb3a 100644 --- a/starport/services/network/networktypes/genesisinformation.go +++ b/starport/services/network/networktypes/genesisinformation.go @@ -77,3 +77,12 @@ func ToGenesisValidator(val launchtypes.GenesisValidator) GenesisValidator { Peer: val.Peer, } } + +// ApplyRequest applies to the genesisInformation the changes implied by the approval of a request +func ApplyRequest(genesisInformation GenesisInformation, request launchtypes.Request) (GenesisInformation, error) { + //switch requestContent := request.Content.Content.(type) { + //case *launchtypes.RequestContent_GenesisAccount: + // ga := requestContent.GenesisAccount + // + //} +} \ No newline at end of file From b0909445cb97d8b79d10130fd9a8eeb8a00d954c Mon Sep 17 00:00:00 2001 From: ltacker Date: Wed, 15 Dec 2021 10:02:00 +0100 Subject: [PATCH 02/22] Implement apply request --- .../networktypes/genesisinformation.go | 105 ++++++++++++++---- 1 file changed, 82 insertions(+), 23 deletions(-) diff --git a/starport/services/network/networktypes/genesisinformation.go b/starport/services/network/networktypes/genesisinformation.go index ecc6f2fb3a..e766fa4e17 100644 --- a/starport/services/network/networktypes/genesisinformation.go +++ b/starport/services/network/networktypes/genesisinformation.go @@ -1,16 +1,19 @@ package networktypes import ( - "errors" + "github.com/pkg/errors" launchtypes "github.com/tendermint/spn/x/launch/types" ) +// errInvalidRequests is an error returned in methods manipulating requests when they are invalid +var errInvalidRequests = errors.New("requests are invalid") + // GenesisInformation represents all information for a chain to construct the genesis. type GenesisInformation struct { - GenesisAccounts []GenesisAccount - VestingAccounts []VestingAccount - GenesisValidators []GenesisValidator + GenesisAccounts map[string]GenesisAccount + VestingAccounts map[string]VestingAccount + GenesisValidators map[string]GenesisValidator } // GenesisAccount represents an account with initial coin allocation for the chain for the chain genesis @@ -30,23 +33,11 @@ type VestingAccount struct { // GenesisValidator represents a genesis validator associated with a gentx in the chain genesis type GenesisValidator struct { + Address string Gentx []byte Peer string } -// NewGenesisInformation initializes a new GenesisInformation -func NewGenesisInformation( - genAccs []GenesisAccount, - vestingAccs []VestingAccount, - genVals []GenesisValidator, -) GenesisInformation { - return GenesisInformation{ - GenesisAccounts: genAccs, - VestingAccounts: vestingAccs, - GenesisValidators: genVals, - } -} - // ToGenesisAccount converts genesis account from SPN func ToGenesisAccount(acc launchtypes.GenesisAccount) GenesisAccount { return GenesisAccount{ @@ -73,16 +64,84 @@ func ToVestingAccount(acc launchtypes.VestingAccount) (VestingAccount, error) { // ToGenesisValidator converts genesis validator from SPN func ToGenesisValidator(val launchtypes.GenesisValidator) GenesisValidator { return GenesisValidator{ + Address: val.Address, Gentx: val.GenTx, Peer: val.Peer, } } +// NewGenesisInformation initializes a new GenesisInformation +func NewGenesisInformation( + genAccs []GenesisAccount, + vestingAccs []VestingAccount, + genVals []GenesisValidator, +) (gi GenesisInformation) { + // convert accounts array into map + for _, genAcc := range genAccs { + gi.GenesisAccounts[genAcc.Address] = genAcc + } + for _, vestingAcc := range vestingAccs { + gi.VestingAccounts[vestingAcc.Address] = vestingAcc + } + for _, genVal := range genVals { + gi.GenesisValidators[genVal.Address] = genVal + } + return gi +} + // ApplyRequest applies to the genesisInformation the changes implied by the approval of a request -func ApplyRequest(genesisInformation GenesisInformation, request launchtypes.Request) (GenesisInformation, error) { - //switch requestContent := request.Content.Content.(type) { - //case *launchtypes.RequestContent_GenesisAccount: - // ga := requestContent.GenesisAccount - // - //} +func ApplyRequest(gi GenesisInformation, request launchtypes.Request) (GenesisInformation, error) { + switch requestContent := request.Content.Content.(type) { + case *launchtypes.RequestContent_GenesisAccount: + // new genesis account in the genesis + ga := ToGenesisAccount(*requestContent.GenesisAccount) + if _, ok := gi.GenesisAccounts[ga.Address]; ok { + return gi, errors.Wrapf(errInvalidRequests, "genesis account %s already in genesis", ga.Address) + } + gi.GenesisAccounts[ga.Address] = ga + + case *launchtypes.RequestContent_VestingAccount: + // new vesting account in the genesis + va, err := ToVestingAccount(*requestContent.VestingAccount) + if err != nil { + // we don't treat this error as errInvalidRequests + // because it can occur if we don't support this format of vesting account + // but the request is still correct + return gi, err + } + + if _, ok := gi.VestingAccounts[va.Address]; ok { + return gi, errors.Wrapf(errInvalidRequests, "vesting account %s already in genesis", va.Address) + } + gi.VestingAccounts[va.Address] = va + + case *launchtypes.RequestContent_AccountRemoval: + // account removed from the genesis + ar := requestContent.AccountRemoval + _, genExist := gi.GenesisAccounts[ar.Address] + _, vestingExist := gi.VestingAccounts[ar.Address] + if !genExist && !vestingExist { + return gi, errors.Wrapf(errInvalidRequests, "account %s can't be removed because it doesn't exist", ar.Address) + } + delete(gi.GenesisAccounts, ar.Address) + delete(gi.VestingAccounts, ar.Address) + + case *launchtypes.RequestContent_GenesisValidator: + // new genesis validator in the genesis + gv := ToGenesisValidator(*requestContent.GenesisValidator) + if _, ok := gi.GenesisValidators[gv.Address]; ok { + return gi, errors.Wrapf(errInvalidRequests, "genesis validator %s already in genesis", gv.Address) + } + gi.GenesisValidators[gv.Address] = gv + + + case *launchtypes.RequestContent_ValidatorRemoval: + // validator removed from the genesis + vr := requestContent.ValidatorRemoval + if _, ok := gi.GenesisValidators[vr.ValAddress]; !ok { + return gi, errors.Wrapf(errInvalidRequests, "genesis validator %s can't be removed because it doesn't exist", vr.ValAddress) + } + } + + return gi, nil } \ No newline at end of file From 8c5bd84365f72f0909157a00d7e33211bcc159bf Mon Sep 17 00:00:00 2001 From: ltacker Date: Wed, 15 Dec 2021 10:22:40 +0100 Subject: [PATCH 03/22] fix prepare --- .../services/network/networkchain/prepare.go | 6 +-- .../networktypes/genesisinformation.go | 39 +++++++++++++++---- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/starport/services/network/networkchain/prepare.go b/starport/services/network/networkchain/prepare.go index 0db709ed02..eb1f944d3b 100644 --- a/starport/services/network/networkchain/prepare.go +++ b/starport/services/network/networkchain/prepare.go @@ -56,13 +56,13 @@ func (c Chain) buildGenesis(ctx context.Context, gi networktypes.GenesisInformat c.ev.Send(events.New(events.StatusOngoing, "Building the genesis")) // apply genesis information to the genesis - if err := c.applyGenesisAccounts(ctx, gi.GenesisAccounts); err != nil { + if err := c.applyGenesisAccounts(ctx, gi.GetGenesisAccounts()); err != nil { return errors.Wrap(err, "error applying genesis accounts to genesis") } - if err := c.applyVestingAccounts(ctx, gi.VestingAccounts); err != nil { + if err := c.applyVestingAccounts(ctx, gi.GetVestingAccounts()); 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") } diff --git a/starport/services/network/networktypes/genesisinformation.go b/starport/services/network/networktypes/genesisinformation.go index e766fa4e17..d34efca267 100644 --- a/starport/services/network/networktypes/genesisinformation.go +++ b/starport/services/network/networktypes/genesisinformation.go @@ -34,8 +34,8 @@ type VestingAccount struct { // GenesisValidator represents a genesis validator associated with a gentx in the chain genesis type GenesisValidator struct { Address string - Gentx []byte - Peer string + Gentx []byte + Peer string } // ToGenesisAccount converts genesis account from SPN @@ -65,8 +65,8 @@ func ToVestingAccount(acc launchtypes.VestingAccount) (VestingAccount, error) { func ToGenesisValidator(val launchtypes.GenesisValidator) GenesisValidator { return GenesisValidator{ Address: val.Address, - Gentx: val.GenTx, - Peer: val.Peer, + Gentx: val.GenTx, + Peer: val.Peer, } } @@ -76,7 +76,7 @@ func NewGenesisInformation( vestingAccs []VestingAccount, genVals []GenesisValidator, ) (gi GenesisInformation) { - // convert accounts array into map + // convert account arrays into maps for _, genAcc := range genAccs { gi.GenesisAccounts[genAcc.Address] = genAcc } @@ -89,8 +89,32 @@ func NewGenesisInformation( return gi } +// GetGenesisAccounts converts into array and returns genesis accounts +func (gi GenesisInformation) GetGenesisAccounts() (accs []GenesisAccount) { + for _, genAcc := range gi.GenesisAccounts { + accs = append(accs, genAcc) + } + return accs +} + +// GetVestingAccounts converts into array and returns vesting accounts +func (gi GenesisInformation) GetVestingAccounts() (accs []VestingAccount) { + for _, vestingAcc := range gi.VestingAccounts { + accs = append(accs, vestingAcc) + } + return accs +} + +// GetGenesisValidators converts into array and returns genesis validators +func (gi GenesisInformation) GetGenesisValidators() (vals []GenesisValidator) { + for _, genVal := range gi.GenesisValidators { + vals = append(vals, genVal) + } + return vals +} + // ApplyRequest applies to the genesisInformation the changes implied by the approval of a request -func ApplyRequest(gi GenesisInformation, request launchtypes.Request) (GenesisInformation, error) { +func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisInformation, error) { switch requestContent := request.Content.Content.(type) { case *launchtypes.RequestContent_GenesisAccount: // new genesis account in the genesis @@ -134,7 +158,6 @@ func ApplyRequest(gi GenesisInformation, request launchtypes.Request) (GenesisIn } gi.GenesisValidators[gv.Address] = gv - case *launchtypes.RequestContent_ValidatorRemoval: // validator removed from the genesis vr := requestContent.ValidatorRemoval @@ -144,4 +167,4 @@ func ApplyRequest(gi GenesisInformation, request launchtypes.Request) (GenesisIn } return gi, nil -} \ No newline at end of file +} From b7ecf949c20f00f6df96fd86d189cc500d867653 Mon Sep 17 00:00:00 2001 From: ltacker Date: Wed, 15 Dec 2021 11:29:39 +0100 Subject: [PATCH 04/22] initialize apply test --- go.mod | 2 +- go.sum | 2 + .../networktypes/genesisinformation.go | 14 +-- .../networktypes/genesisinformation_test.go | 107 +++++++++++++++++- 4 files changed, 111 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index db0d1daad1..d68f3e6b9a 100644 --- a/go.mod +++ b/go.mod @@ -46,7 +46,7 @@ require ( github.com/stretchr/testify v1.7.0 github.com/tendermint/flutter v1.0.2 github.com/tendermint/spm v0.1.8 - github.com/tendermint/spn v0.1.1-0.20211209093049-eade64462922 + github.com/tendermint/spn v0.1.1-0.20211210111100-92566247ddf2 github.com/tendermint/tendermint v0.34.14 github.com/tendermint/vue v0.1.55 golang.org/x/mod v0.4.2 diff --git a/go.sum b/go.sum index 35e3fff2be..7371331bc5 100644 --- a/go.sum +++ b/go.sum @@ -1481,6 +1481,8 @@ github.com/tendermint/spn v0.1.1-0.20211208205850-47a65d0f1da6 h1:Bi1AgF6gHW+c+y github.com/tendermint/spn v0.1.1-0.20211208205850-47a65d0f1da6/go.mod h1:p4BO8YC6kOKSKqMfySqaLHfwBmuPE/QcLwnnVhh7H9M= github.com/tendermint/spn v0.1.1-0.20211209093049-eade64462922 h1:97K3TpQ2Jm3E6WRWAPb+iMKr6gQykQSuIpFzHUbYG1M= github.com/tendermint/spn v0.1.1-0.20211209093049-eade64462922/go.mod h1:p4BO8YC6kOKSKqMfySqaLHfwBmuPE/QcLwnnVhh7H9M= +github.com/tendermint/spn v0.1.1-0.20211210111100-92566247ddf2 h1:124+NcQNEC7wy76cADmItBRLh6q5xto5LtM6mZrbc3M= +github.com/tendermint/spn v0.1.1-0.20211210111100-92566247ddf2/go.mod h1:p4BO8YC6kOKSKqMfySqaLHfwBmuPE/QcLwnnVhh7H9M= github.com/tendermint/tendermint v0.34.0-rc4/go.mod h1:yotsojf2C1QBOw4dZrTcxbyxmPUrT4hNuOQWX9XUwB4= github.com/tendermint/tendermint v0.34.0-rc6/go.mod h1:ugzyZO5foutZImv0Iyx/gOFCX6mjJTgbLHTwi17VDVg= github.com/tendermint/tendermint v0.34.0/go.mod h1:Aj3PIipBFSNO21r+Lq3TtzQ+uKESxkbA3yo/INM4QwQ= diff --git a/starport/services/network/networktypes/genesisinformation.go b/starport/services/network/networktypes/genesisinformation.go index d34efca267..2fc33c1204 100644 --- a/starport/services/network/networktypes/genesisinformation.go +++ b/starport/services/network/networktypes/genesisinformation.go @@ -6,8 +6,8 @@ import ( launchtypes "github.com/tendermint/spn/x/launch/types" ) -// errInvalidRequests is an error returned in methods manipulating requests when they are invalid -var errInvalidRequests = errors.New("requests are invalid") +// errInvalidRequest is an error returned in methods manipulating requests when they are invalid +var errInvalidRequest = errors.New("request is invalid") // GenesisInformation represents all information for a chain to construct the genesis. type GenesisInformation struct { @@ -120,7 +120,7 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI // new genesis account in the genesis ga := ToGenesisAccount(*requestContent.GenesisAccount) if _, ok := gi.GenesisAccounts[ga.Address]; ok { - return gi, errors.Wrapf(errInvalidRequests, "genesis account %s already in genesis", ga.Address) + return gi, errors.Wrapf(errInvalidRequest, "genesis account %s already in genesis", ga.Address) } gi.GenesisAccounts[ga.Address] = ga @@ -135,7 +135,7 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI } if _, ok := gi.VestingAccounts[va.Address]; ok { - return gi, errors.Wrapf(errInvalidRequests, "vesting account %s already in genesis", va.Address) + return gi, errors.Wrapf(errInvalidRequest, "vesting account %s already in genesis", va.Address) } gi.VestingAccounts[va.Address] = va @@ -145,7 +145,7 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI _, genExist := gi.GenesisAccounts[ar.Address] _, vestingExist := gi.VestingAccounts[ar.Address] if !genExist && !vestingExist { - return gi, errors.Wrapf(errInvalidRequests, "account %s can't be removed because it doesn't exist", ar.Address) + return gi, errors.Wrapf(errInvalidRequest, "account %s can't be removed because it doesn't exist", ar.Address) } delete(gi.GenesisAccounts, ar.Address) delete(gi.VestingAccounts, ar.Address) @@ -154,7 +154,7 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI // new genesis validator in the genesis gv := ToGenesisValidator(*requestContent.GenesisValidator) if _, ok := gi.GenesisValidators[gv.Address]; ok { - return gi, errors.Wrapf(errInvalidRequests, "genesis validator %s already in genesis", gv.Address) + return gi, errors.Wrapf(errInvalidRequest, "genesis validator %s already in genesis", gv.Address) } gi.GenesisValidators[gv.Address] = gv @@ -162,7 +162,7 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI // validator removed from the genesis vr := requestContent.ValidatorRemoval if _, ok := gi.GenesisValidators[vr.ValAddress]; !ok { - return gi, errors.Wrapf(errInvalidRequests, "genesis validator %s can't be removed because it doesn't exist", vr.ValAddress) + return gi, errors.Wrapf(errInvalidRequest, "genesis validator %s can't be removed because it doesn't exist", vr.ValAddress) } } diff --git a/starport/services/network/networktypes/genesisinformation_test.go b/starport/services/network/networktypes/genesisinformation_test.go index 99d72ea381..7702f6d29a 100644 --- a/starport/services/network/networktypes/genesisinformation_test.go +++ b/starport/services/network/networktypes/genesisinformation_test.go @@ -34,9 +34,7 @@ func TestToGenesisAccount(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - t.Run(tt.name, func(t *testing.T) { require.EqualValues(t, tt.expected, networktypes.ToGenesisAccount(tt.fetched)) - }) }) } } @@ -79,12 +77,10 @@ func TestToVestingAccount(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - t.Run(tt.name, func(t *testing.T) { vestingAcc, err := networktypes.ToVestingAccount(tt.fetched) require.EqualValues(t, tt.isError, err != nil) require.EqualValues(t, tt.expected, vestingAcc) }) - }) } } @@ -108,9 +104,108 @@ func TestToGenesisValidator(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - t.Run(tt.name, func(t *testing.T) { require.EqualValues(t, tt.expected, networktypes.ToGenesisValidator(tt.fetched)) - }) + }) + } +} + +func TestGenesisInformation_ApplyRequest(t *testing.T) { + // used as a template for tests + genesisInformation := networktypes.NewGenesisInformation( + []networktypes.GenesisAccount{ + { + Address: "spn1g50xher44l9hjuatjdfxgv254jh2wgzfs55yu3", + Coins: "1000foo", + }, + { + Address: "spn1sgphx4vxt63xhvgp9wpewajyxeqt04twfj7gcc", + Coins: "1000bar", + }, + }, + []networktypes.VestingAccount{ + { + + }, + }, + []networktypes.GenesisValidator{ + { + + }, + }, + ) + + launchtypes.NewMsgRequestAddAccount() + + tests := []struct { + name string + gi networktypes.GenesisInformation + r launchtypes.Request + err error + }{ + { + name: "genesis account request", + }, + { + name: "vesting account request", + }, + { + name: "genesis validator request", + }, + { + name: "genesis account: existing genesis account", + }, + { + name: "genesis account: existing vesting account", + }, + { + name: "vesting account: existing genesis account", + }, + { + name: "vesting account: existing vesting account", + }, + { + name: "existing genesis validator", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + newGi, err := tt.gi.ApplyRequest(tt.r) + if tt.err != nil { + require.ErrorIs(t, err, tt.err) + return + } + + // parse difference following request + switch rc := tt.r.Content.Content.(type) { + case *launchtypes.RequestContent_GenesisAccount: + ga := networktypes.ToGenesisAccount(*rc.GenesisAccount) + got, ok := newGi.GenesisAccounts[ga.Address] + require.True(t, ok) + require.EqualValues(t, ga, got) + + case *launchtypes.RequestContent_VestingAccount: + va, err := networktypes.ToVestingAccount(*rc.VestingAccount) + require.NoError(t, err) + got, ok := newGi.VestingAccounts[va.Address] + require.True(t, ok) + require.EqualValues(t, va, got) + + case *launchtypes.RequestContent_AccountRemoval: + _, ok := newGi.GenesisAccounts[rc.AccountRemoval.Address] + require.False(t, ok) + _, ok = newGi.VestingAccounts[rc.AccountRemoval.Address] + require.False(t, ok) + + case *launchtypes.RequestContent_GenesisValidator: + gv := networktypes.ToGenesisValidator(*rc.GenesisValidator) + got, ok := newGi.GenesisValidators[gv.Address] + require.True(t, ok) + require.EqualValues(t, gv, got) + + case *launchtypes.RequestContent_ValidatorRemoval: + _, ok := newGi.GenesisAccounts[rc.ValidatorRemoval.ValAddress] + require.False(t, ok) + } }) } } From f9f76533646fa6e74898a403b8a790c332a4a106 Mon Sep 17 00:00:00 2001 From: ltacker Date: Wed, 15 Dec 2021 13:52:21 +0100 Subject: [PATCH 05/22] Apply remove tests --- starport/cmd/generate_dart.go | 6 +- .../networktypes/genesisinformation.go | 26 ++- .../networktypes/genesisinformation_test.go | 175 ++++++++++++++++-- 3 files changed, 175 insertions(+), 32 deletions(-) diff --git a/starport/cmd/generate_dart.go b/starport/cmd/generate_dart.go index f0d6ffb106..22c8ba1a6d 100644 --- a/starport/cmd/generate_dart.go +++ b/starport/cmd/generate_dart.go @@ -10,9 +10,9 @@ import ( func NewGenerateDart() *cobra.Command { c := &cobra.Command{ - Use: "dart", - Short: "Generate a Dart client", - RunE: generateDartHandler, + Use: "dart", + Short: "Generate a Dart client", + RunE: generateDartHandler, } return c } diff --git a/starport/services/network/networktypes/genesisinformation.go b/starport/services/network/networktypes/genesisinformation.go index 397f4f771b..a788cb370e 100644 --- a/starport/services/network/networktypes/genesisinformation.go +++ b/starport/services/network/networktypes/genesisinformation.go @@ -6,8 +6,8 @@ import ( launchtypes "github.com/tendermint/spn/x/launch/types" ) -// errInvalidRequest is an error returned in methods manipulating requests when they are invalid -var errInvalidRequest = errors.New("request is invalid") +// ErrInvalidRequest is an error returned in methods manipulating requests when they are invalid +var ErrInvalidRequest = errors.New("request is invalid") // GenesisInformation represents all information for a chain to construct the genesis. type GenesisInformation struct { @@ -76,13 +76,17 @@ func NewGenesisInformation( vestingAccs []VestingAccount, genVals []GenesisValidator, ) (gi GenesisInformation) { + // convert account arrays into maps + gi.GenesisAccounts = make(map[string]GenesisAccount) for _, genAcc := range genAccs { gi.GenesisAccounts[genAcc.Address] = genAcc } + gi.VestingAccounts = make(map[string]VestingAccount) for _, vestingAcc := range vestingAccs { gi.VestingAccounts[vestingAcc.Address] = vestingAcc } + gi.GenesisValidators = make(map[string]GenesisValidator) for _, genVal := range genVals { gi.GenesisValidators[genVal.Address] = genVal } @@ -119,8 +123,10 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI case *launchtypes.RequestContent_GenesisAccount: // new genesis account in the genesis ga := ToGenesisAccount(*requestContent.GenesisAccount) - if _, ok := gi.GenesisAccounts[ga.Address]; ok { - return gi, errors.Wrapf(errInvalidRequest, "genesis account %s already in genesis", ga.Address) + _, genExist := gi.GenesisAccounts[ga.Address] + _, vestingExist := gi.VestingAccounts[ga.Address] + if genExist || vestingExist { + return gi, errors.Wrapf(ErrInvalidRequest, "genesis account %s already in genesis", ga.Address) } gi.GenesisAccounts[ga.Address] = ga @@ -134,8 +140,10 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI return gi, err } - if _, ok := gi.VestingAccounts[va.Address]; ok { - return gi, errors.Wrapf(errInvalidRequest, "vesting account %s already in genesis", va.Address) + _, genExist := gi.GenesisAccounts[va.Address] + _, vestingExist := gi.VestingAccounts[va.Address] + if genExist || vestingExist { + return gi, errors.Wrapf(ErrInvalidRequest, "vesting account %s already in genesis", va.Address) } gi.VestingAccounts[va.Address] = va @@ -145,7 +153,7 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI _, genExist := gi.GenesisAccounts[ar.Address] _, vestingExist := gi.VestingAccounts[ar.Address] if !genExist && !vestingExist { - return gi, errors.Wrapf(errInvalidRequest, "account %s can't be removed because it doesn't exist", ar.Address) + return gi, errors.Wrapf(ErrInvalidRequest, "account %s can't be removed because it doesn't exist", ar.Address) } delete(gi.GenesisAccounts, ar.Address) delete(gi.VestingAccounts, ar.Address) @@ -154,7 +162,7 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI // new genesis validator in the genesis gv := ToGenesisValidator(*requestContent.GenesisValidator) if _, ok := gi.GenesisValidators[gv.Address]; ok { - return gi, errors.Wrapf(errInvalidRequest, "genesis validator %s already in genesis", gv.Address) + return gi, errors.Wrapf(ErrInvalidRequest, "genesis validator %s already in genesis", gv.Address) } gi.GenesisValidators[gv.Address] = gv @@ -162,7 +170,7 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI // validator removed from the genesis vr := requestContent.ValidatorRemoval if _, ok := gi.GenesisValidators[vr.ValAddress]; !ok { - return gi, errors.Wrapf(errInvalidRequest, "genesis validator %s can't be removed because it doesn't exist", vr.ValAddress) + return gi, errors.Wrapf(ErrInvalidRequest, "genesis validator %s can't be removed because it doesn't exist", vr.ValAddress) } } diff --git a/starport/services/network/networktypes/genesisinformation_test.go b/starport/services/network/networktypes/genesisinformation_test.go index 2dffeee7d5..3f03612f90 100644 --- a/starport/services/network/networktypes/genesisinformation_test.go +++ b/starport/services/network/networktypes/genesisinformation_test.go @@ -2,6 +2,7 @@ package networktypes_test import ( "testing" + "time" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" @@ -34,7 +35,7 @@ func TestToGenesisAccount(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - require.EqualValues(t, tt.expected, networktypes.ToGenesisAccount(tt.fetched)) + require.EqualValues(t, tt.expected, networktypes.ToGenesisAccount(tt.fetched)) }) } } @@ -76,10 +77,10 @@ func TestToVestingAccount(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - vestingAcc, err := networktypes.ToVestingAccount(tt.fetched) - require.EqualValues(t, tt.isError, err != nil) - require.EqualValues(t, tt.expected, vestingAcc) - }) + vestingAcc, err := networktypes.ToVestingAccount(tt.fetched) + require.EqualValues(t, tt.isError, err != nil) + require.EqualValues(t, tt.expected, vestingAcc) + }) } } @@ -103,67 +104,201 @@ func TestToGenesisValidator(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - require.EqualValues(t, tt.expected, networktypes.ToGenesisValidator(tt.fetched)) + require.EqualValues(t, tt.expected, networktypes.ToGenesisValidator(tt.fetched)) }) } } func TestGenesisInformation_ApplyRequest(t *testing.T) { + newCoin := func(str string) sdk.Coin { + c, err := sdk.ParseCoinNormalized(str) + require.NoError(t, err) + return c + } + newCoins := func(str string) sdk.Coins { + c, err := sdk.ParseCoinsNormalized(str) + require.NoError(t, err) + return c + } + // used as a template for tests genesisInformation := networktypes.NewGenesisInformation( []networktypes.GenesisAccount{ { Address: "spn1g50xher44l9hjuatjdfxgv254jh2wgzfs55yu3", - Coins: "1000foo", - }, - { - Address: "spn1sgphx4vxt63xhvgp9wpewajyxeqt04twfj7gcc", - Coins: "1000bar", + Coins: "1000foo", }, }, []networktypes.VestingAccount{ { - + Address: "spn1gkzf4e0x6wr4djfd8h82v6cy507gy5v4spaus3", + TotalBalance: "1000foo", + Vesting: "500foo", + EndTime: time.Now().Unix(), }, }, []networktypes.GenesisValidator{ { - + Address: "spn1pquxnnpnjyl3ptz3uxs0lrs93s5ljepzq4wyp6", + Gentx: []byte("aaa"), + Peer: "foo", }, }, - ) - - launchtypes.NewMsgRequestAddAccount() + ) tests := []struct { - name string - gi networktypes.GenesisInformation - r launchtypes.Request - err error + name string + gi networktypes.GenesisInformation + r launchtypes.Request + err error }{ { name: "genesis account request", + gi: genesisInformation, + r: launchtypes.Request{ + Content: launchtypes.NewGenesisAccount( + 0, + "spn1sgphx4vxt63xhvgp9wpewajyxeqt04twfj7gcc", + newCoins("1000bar"), + ), + }, }, { name: "vesting account request", + gi: genesisInformation, + r: launchtypes.Request{ + Content: launchtypes.NewVestingAccount( + 0, + "spn19klee4szqpeu0laqze5srhdxtp6fuhcztdrh7c", + *launchtypes.NewDelayedVesting( + newCoins("1000bar"), + newCoins("500bar"), + time.Now().Unix(), + ), + ), + }, }, { name: "genesis validator request", + gi: genesisInformation, + r: launchtypes.Request{ + Content: launchtypes.NewGenesisValidator( + 0, + "spn1xnn9w76mf42t249486ss65lvga7gqs02erpw24", + []byte("bbb"), + []byte("ccc"), + newCoin("1000bar"), + "bar", + ), + }, }, { name: "genesis account: existing genesis account", + gi: genesisInformation, + r: launchtypes.Request{ + Content: launchtypes.NewGenesisAccount( + 0, + "spn1g50xher44l9hjuatjdfxgv254jh2wgzfs55yu3", + newCoins("1000bar"), + ), + }, + err: networktypes.ErrInvalidRequest, }, { name: "genesis account: existing vesting account", + gi: genesisInformation, + r: launchtypes.Request{ + Content: launchtypes.NewGenesisAccount( + 0, + "spn1gkzf4e0x6wr4djfd8h82v6cy507gy5v4spaus3", + newCoins("1000bar"), + ), + }, + err: networktypes.ErrInvalidRequest, }, { name: "vesting account: existing genesis account", + gi: genesisInformation, + r: launchtypes.Request{ + Content: launchtypes.NewVestingAccount( + 0, + "spn1g50xher44l9hjuatjdfxgv254jh2wgzfs55yu3", + *launchtypes.NewDelayedVesting( + newCoins("1000bar"), + newCoins("500bar"), + time.Now().Unix(), + ), + ), + }, + err: networktypes.ErrInvalidRequest, }, { name: "vesting account: existing vesting account", + gi: genesisInformation, + r: launchtypes.Request{ + Content: launchtypes.NewVestingAccount( + 0, + "spn1gkzf4e0x6wr4djfd8h82v6cy507gy5v4spaus3", + *launchtypes.NewDelayedVesting( + newCoins("1000bar"), + newCoins("500bar"), + time.Now().Unix(), + ), + ), + }, + err: networktypes.ErrInvalidRequest, }, { name: "existing genesis validator", + gi: genesisInformation, + r: launchtypes.Request{ + Content: launchtypes.NewGenesisValidator( + 0, + "spn1pquxnnpnjyl3ptz3uxs0lrs93s5ljepzq4wyp6", + []byte("bbb"), + []byte("ccc"), + newCoin("1000bar"), + "bar", + ), + }, + err: networktypes.ErrInvalidRequest, + }, + { + name: "remove genesis account", + gi: genesisInformation, + r: launchtypes.Request{ + Content: launchtypes.NewAccountRemoval("spn1g50xher44l9hjuatjdfxgv254jh2wgzfs55yu3"), + }, + }, + { + name: "remove vesting account", + gi: genesisInformation, + r: launchtypes.Request{ + Content: launchtypes.NewAccountRemoval("spn1gkzf4e0x6wr4djfd8h82v6cy507gy5v4spaus3"), + }, + }, + { + name: "remove genesis validator", + gi: genesisInformation, + r: launchtypes.Request{ + Content: launchtypes.NewValidatorRemoval("spn1pquxnnpnjyl3ptz3uxs0lrs93s5ljepzq4wyp6"), + }, + }, + { + name: "remove account: non-existent account", + gi: genesisInformation, + r: launchtypes.Request{ + Content: launchtypes.NewAccountRemoval("spn1pquxnnpnjyl3ptz3uxs0lrs93s5ljepzq4wyp6"), + }, + err: networktypes.ErrInvalidRequest, + }, + { + name: "remove account: non-existent genesis validator", + gi: genesisInformation, + r: launchtypes.Request{ + Content: launchtypes.NewValidatorRemoval("spn1g50xher44l9hjuatjdfxgv254jh2wgzfs55yu3"), + }, + err: networktypes.ErrInvalidRequest, }, } for _, tt := range tests { From b6810000fb79cb78b51c159f428d85a615993951 Mon Sep 17 00:00:00 2001 From: ltacker Date: Wed, 15 Dec 2021 15:28:19 +0100 Subject: [PATCH 06/22] refactor request --- starport/cmd/network_request.go | 1 + starport/cmd/network_request_approve.go | 14 +-- starport/cmd/network_request_verify.go | 71 ++++++++++++ .../{ => networkchain}/request_test.go | 8 +- .../services/network/networktypes/error.go | 18 +++ .../networktypes/genesisinformation.go | 14 +-- .../networktypes/genesisinformation_test.go | 26 ++--- starport/services/network/request.go | 106 +++--------------- 8 files changed, 133 insertions(+), 125 deletions(-) create mode 100644 starport/cmd/network_request_verify.go rename starport/services/network/{ => networkchain}/request_test.go (96%) create mode 100644 starport/services/network/networktypes/error.go diff --git a/starport/cmd/network_request.go b/starport/cmd/network_request.go index 04c950d826..f7f31976e8 100644 --- a/starport/cmd/network_request.go +++ b/starport/cmd/network_request.go @@ -15,6 +15,7 @@ func NewNetworkRequest() *cobra.Command { NewNetworkRequestList(), NewNetworkRequestApprove(), NewNetworkRequestReject(), + NewNetworkRequestVerify(), ) return c diff --git a/starport/cmd/network_request_approve.go b/starport/cmd/network_request_approve.go index 6f465be8c8..8ffe7cf35d 100644 --- a/starport/cmd/network_request_approve.go +++ b/starport/cmd/network_request_approve.go @@ -51,7 +51,7 @@ func networkRequestApproveHandler(cmd *cobra.Command, args []string) error { } // Verify the requests are valid - noVerification, err := cmd.Flags().GetBool(flagNoVerification) + _, err = cmd.Flags().GetBool(flagNoVerification) if err != nil { return err } @@ -61,12 +61,12 @@ func networkRequestApproveHandler(cmd *cobra.Command, args []string) error { return err } - if !noVerification { - err := n.VerifyRequests(cmd.Context(), launchID, ids...) - if err != nil { - return err - } - } + // if !noVerification { + // err := n.VerifyRequests(cmd.Context(), launchID, ids...) + // if err != nil { + // return err + // } + // } // Submit the approved requests reviewals := make([]network.Reviewal, 0) for _, id := range ids { diff --git a/starport/cmd/network_request_verify.go b/starport/cmd/network_request_verify.go new file mode 100644 index 0000000000..2da3554fbd --- /dev/null +++ b/starport/cmd/network_request_verify.go @@ -0,0 +1,71 @@ +package starportcmd + +import ( + "fmt" + + "github.com/spf13/cobra" + "github.com/tendermint/starport/starport/pkg/clispinner" + "github.com/tendermint/starport/starport/pkg/numbers" + "github.com/tendermint/starport/starport/services/network" +) + +// NewNetworkRequestVerify creates a new request approve +// command to approve requests for a chain. +func NewNetworkRequestVerify() *cobra.Command { + c := &cobra.Command{ + Use: "simulate [launch-id] [number<,...>]", + Aliases: []string{"accept"}, + Short: "Simulate requests and check generated genesis", + 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 + } + + // if !noVerification { + // err := n.VerifyRequests(cmd.Context(), launchID, ids...) + // if err != nil { + // return err + // } + // } + // Submit the approved requests + reviewals := make([]network.Reviewal, 0) + for _, id := range ids { + reviewals = append(reviewals, network.ApproveRequest(id)) + } + if err := n.SubmitRequest(launchID, reviewals...); err != nil { + return err + } + + nb.Spinner.Stop() + fmt.Printf("%s Request(s) %s approved\n", clispinner.OK, numbers.List(ids, "#")) + return nil +} diff --git a/starport/services/network/request_test.go b/starport/services/network/networkchain/request_test.go similarity index 96% rename from starport/services/network/request_test.go rename to starport/services/network/networkchain/request_test.go index 4988936c1f..17934c1ce4 100644 --- a/starport/services/network/request_test.go +++ b/starport/services/network/networkchain/request_test.go @@ -1,7 +1,8 @@ -package network +package networkchain_test import ( "fmt" + "github.com/tendermint/starport/starport/services/network/networkchain" "testing" sdk "github.com/cosmos/cosmos-sdk/types" @@ -9,7 +10,7 @@ import ( launchtypes "github.com/tendermint/spn/x/launch/types" ) -func TestBuilderVerifyAddValidatorRequest(t *testing.T) { +func TestVerifyAddValidatorRequest(t *testing.T) { gentx := []byte(`{ "body": { "messages": [ @@ -140,8 +141,7 @@ func TestBuilderVerifyAddValidatorRequest(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - n := Network{} - err := n.verifyAddValidatorRequest(tt.req) + err := networkchain.VerifyAddValidatorRequest(tt.req) if tt.want != nil { require.Error(t, err) require.Equal(t, tt.want.Error(), err.Error()) diff --git a/starport/services/network/networktypes/error.go b/starport/services/network/networktypes/error.go new file mode 100644 index 0000000000..020e396297 --- /dev/null +++ b/starport/services/network/networktypes/error.go @@ -0,0 +1,18 @@ +package networktypes + +import "fmt" + +// ErrInvalidRequest is an error returned in methods manipulating requests when they are invalid +type ErrInvalidRequest struct { + requestID uint64 +} + +// Error implements error +func (err ErrInvalidRequest) Error() string { + return fmt.Sprintf("request %d is invalid", err.requestID) +} + +// NewErrInvalidRequest returns a new ErrInvalidRequest +func NewErrInvalidRequest(requestID uint64) ErrInvalidRequest { + return ErrInvalidRequest{requestID: requestID} +} diff --git a/starport/services/network/networktypes/genesisinformation.go b/starport/services/network/networktypes/genesisinformation.go index a788cb370e..286709757e 100644 --- a/starport/services/network/networktypes/genesisinformation.go +++ b/starport/services/network/networktypes/genesisinformation.go @@ -2,13 +2,9 @@ package networktypes import ( "github.com/pkg/errors" - launchtypes "github.com/tendermint/spn/x/launch/types" ) -// ErrInvalidRequest is an error returned in methods manipulating requests when they are invalid -var ErrInvalidRequest = errors.New("request is invalid") - // GenesisInformation represents all information for a chain to construct the genesis. type GenesisInformation struct { GenesisAccounts map[string]GenesisAccount @@ -126,7 +122,7 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI _, genExist := gi.GenesisAccounts[ga.Address] _, vestingExist := gi.VestingAccounts[ga.Address] if genExist || vestingExist { - return gi, errors.Wrapf(ErrInvalidRequest, "genesis account %s already in genesis", ga.Address) + return gi, errors.Wrapf(NewErrInvalidRequest(request.RequestID), "genesis account %s already in genesis", ga.Address) } gi.GenesisAccounts[ga.Address] = ga @@ -143,7 +139,7 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI _, genExist := gi.GenesisAccounts[va.Address] _, vestingExist := gi.VestingAccounts[va.Address] if genExist || vestingExist { - return gi, errors.Wrapf(ErrInvalidRequest, "vesting account %s already in genesis", va.Address) + return gi, errors.Wrapf(NewErrInvalidRequest(request.RequestID), "vesting account %s already in genesis", va.Address) } gi.VestingAccounts[va.Address] = va @@ -153,7 +149,7 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI _, genExist := gi.GenesisAccounts[ar.Address] _, vestingExist := gi.VestingAccounts[ar.Address] if !genExist && !vestingExist { - return gi, errors.Wrapf(ErrInvalidRequest, "account %s can't be removed because it doesn't exist", ar.Address) + return gi, errors.Wrapf(NewErrInvalidRequest(request.RequestID), "account %s can't be removed because it doesn't exist", ar.Address) } delete(gi.GenesisAccounts, ar.Address) delete(gi.VestingAccounts, ar.Address) @@ -162,7 +158,7 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI // new genesis validator in the genesis gv := ToGenesisValidator(*requestContent.GenesisValidator) if _, ok := gi.GenesisValidators[gv.Address]; ok { - return gi, errors.Wrapf(ErrInvalidRequest, "genesis validator %s already in genesis", gv.Address) + return gi, errors.Wrapf(NewErrInvalidRequest(request.RequestID), "genesis validator %s already in genesis", gv.Address) } gi.GenesisValidators[gv.Address] = gv @@ -170,7 +166,7 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI // validator removed from the genesis vr := requestContent.ValidatorRemoval if _, ok := gi.GenesisValidators[vr.ValAddress]; !ok { - return gi, errors.Wrapf(ErrInvalidRequest, "genesis validator %s can't be removed because it doesn't exist", vr.ValAddress) + return gi, errors.Wrapf(NewErrInvalidRequest(request.RequestID), "genesis validator %s can't be removed because it doesn't exist", vr.ValAddress) } } diff --git a/starport/services/network/networktypes/genesisinformation_test.go b/starport/services/network/networktypes/genesisinformation_test.go index 3f03612f90..a9ce20e06b 100644 --- a/starport/services/network/networktypes/genesisinformation_test.go +++ b/starport/services/network/networktypes/genesisinformation_test.go @@ -147,10 +147,10 @@ func TestGenesisInformation_ApplyRequest(t *testing.T) { ) tests := []struct { - name string - gi networktypes.GenesisInformation - r launchtypes.Request - err error + name string + gi networktypes.GenesisInformation + r launchtypes.Request + invalidRequest bool }{ { name: "genesis account request", @@ -202,7 +202,7 @@ func TestGenesisInformation_ApplyRequest(t *testing.T) { newCoins("1000bar"), ), }, - err: networktypes.ErrInvalidRequest, + invalidRequest: true, }, { name: "genesis account: existing vesting account", @@ -214,7 +214,7 @@ func TestGenesisInformation_ApplyRequest(t *testing.T) { newCoins("1000bar"), ), }, - err: networktypes.ErrInvalidRequest, + invalidRequest: true, }, { name: "vesting account: existing genesis account", @@ -230,7 +230,7 @@ func TestGenesisInformation_ApplyRequest(t *testing.T) { ), ), }, - err: networktypes.ErrInvalidRequest, + invalidRequest: true, }, { name: "vesting account: existing vesting account", @@ -246,7 +246,7 @@ func TestGenesisInformation_ApplyRequest(t *testing.T) { ), ), }, - err: networktypes.ErrInvalidRequest, + invalidRequest: true, }, { name: "existing genesis validator", @@ -261,7 +261,7 @@ func TestGenesisInformation_ApplyRequest(t *testing.T) { "bar", ), }, - err: networktypes.ErrInvalidRequest, + invalidRequest: true, }, { name: "remove genesis account", @@ -290,7 +290,7 @@ func TestGenesisInformation_ApplyRequest(t *testing.T) { r: launchtypes.Request{ Content: launchtypes.NewAccountRemoval("spn1pquxnnpnjyl3ptz3uxs0lrs93s5ljepzq4wyp6"), }, - err: networktypes.ErrInvalidRequest, + invalidRequest: true, }, { name: "remove account: non-existent genesis validator", @@ -298,14 +298,14 @@ func TestGenesisInformation_ApplyRequest(t *testing.T) { r: launchtypes.Request{ Content: launchtypes.NewValidatorRemoval("spn1g50xher44l9hjuatjdfxgv254jh2wgzfs55yu3"), }, - err: networktypes.ErrInvalidRequest, + invalidRequest: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { newGi, err := tt.gi.ApplyRequest(tt.r) - if tt.err != nil { - require.ErrorIs(t, err, tt.err) + if tt.invalidRequest { + require.ErrorAs(t, err, &networktypes.ErrInvalidRequest{}) return } diff --git a/starport/services/network/request.go b/starport/services/network/request.go index a9cd145b78..f9407d2a6f 100644 --- a/starport/services/network/request.go +++ b/starport/services/network/request.go @@ -2,11 +2,9 @@ package network import ( "context" - "fmt" sdk "github.com/cosmos/cosmos-sdk/types" launchtypes "github.com/tendermint/spn/x/launch/types" - "github.com/tendermint/starport/starport/pkg/cosmosutil" "github.com/tendermint/starport/starport/pkg/events" "github.com/tendermint/starport/starport/services/network/networkchain" ) @@ -33,7 +31,7 @@ func RejectRequest(requestID uint64) Reviewal { } } -// Requests fetches the chain requests from SPN by launch id +// Requests fetches all the chain requests from SPN by launch id func (n Network) Requests(ctx context.Context, launchID uint64) ([]launchtypes.Request, error) { res, err := launchtypes.NewQueryClient(n.cosmos.Context).RequestAll(ctx, &launchtypes.QueryAllRequestRequest{ LaunchID: launchID, @@ -57,6 +55,19 @@ func (n Network) Request(ctx context.Context, launchID, requestID uint64) (launc return res.Request, err } +// RequestFromIDs fetches the chain requestd from SPN by launch and ptovided request IDs +// TODO: once implemented, use the SPN query from https://github.com/tendermint/spn/issues/420 +func (n Network) RequestFromIDs(ctx context.Context, launchID uint64, requestIDs ...uint64) (reqs []launchtypes.Request, err error) { + for _, id := range requestIDs { + req, err := n.Request(ctx, launchID, id) + if err != nil { + return reqs, err + } + reqs = append(reqs, req) + } + return reqs, nil +} + // SubmitRequest submits reviewals for proposals in batch for chain. func (n Network) SubmitRequest(launchID uint64, reviewal ...Reviewal) error { n.ev.Send(events.New(events.StatusOngoing, "Submitting requests...")) @@ -79,92 +90,3 @@ func (n Network) SubmitRequest(launchID uint64, reviewal ...Reviewal) error { var requestRes launchtypes.MsgSettleRequestResponse return res.Decode(&requestRes) } - -// verifyAddValidatorRequest verify the validator request parameters -func (Network) verifyAddValidatorRequest(req *launchtypes.RequestContent_GenesisValidator) error { - // If this is an add validator request - var ( - peer = req.GenesisValidator.Peer - valAddress = req.GenesisValidator.Address - consPubKey = req.GenesisValidator.ConsPubKey - selfDelegation = req.GenesisValidator.SelfDelegation - ) - - // Check values inside the gentx are correct - info, _, err := cosmosutil.ParseGentx(req.GenesisValidator.GenTx) - if err != nil { - return fmt.Errorf("cannot parse gentx %s", err.Error()) - } - - // Change the address prefix fetched from the gentx to the one used on SPN - // Because all on-chain stored address on SPN uses the SPN prefix - spnFetchedAddress, err := cosmosutil.ChangeAddressPrefix(info.DelegatorAddress, networkchain.SPN) - if err != nil { - return err - } - - // Check validator address - if valAddress != spnFetchedAddress { - return fmt.Errorf( - "the validator address %s doesn't match the one inside the gentx %s", - valAddress, - spnFetchedAddress, - ) - } - - // Check validator address - if !info.PubKey.Equal(consPubKey) { - return fmt.Errorf( - "the consensus pub key %s doesn't match the one inside the gentx %s", - string(consPubKey), - string(info.PubKey), - ) - } - - // Check self delegation - if selfDelegation.Denom != info.SelfDelegation.Denom || - !selfDelegation.IsEqual(info.SelfDelegation) { - return fmt.Errorf( - "the self delegation %s doesn't match the one inside the gentx %s", - selfDelegation.String(), - info.SelfDelegation.String(), - ) - } - - // Check the format of the peer - if !cosmosutil.VerifyPeerFormat(peer) { - return fmt.Errorf( - "the peer %s doesn't match the peer format @", - peer, - ) - } - return nil -} - -// VerifyRequests if the requests are correct and simulate them with the current launch information -// Correctness means checks that have to be performed off-chain -func (n Network) VerifyRequests(ctx context.Context, launchID uint64, requests ...uint64) error { - n.ev.Send(events.New(events.StatusOngoing, "Verifying requests...")) - // Check all request - for _, id := range requests { - request, err := n.Request(ctx, launchID, id) - if err != nil { - return err - } - - req, ok := request.Content.Content.(*launchtypes.RequestContent_GenesisValidator) - if ok { - err := n.verifyAddValidatorRequest(req) - if err != nil { - return fmt.Errorf("request %d error: %s", id, err.Error()) - } - } - } - n.ev.Send(events.New(events.StatusDone, "Requests verified")) - - // TODO simulate the requests - // If all requests are correct, simulate them - // return n.SimulateRequests(ctx, launchID, requests) - - return nil -} From 974e4ec6f8c86c744847f66d31222b0107097018 Mon Sep 17 00:00:00 2001 From: ltacker Date: Wed, 15 Dec 2021 17:19:02 +0100 Subject: [PATCH 07/22] command verify --- starport/cmd/network_request_approve.go | 14 +++--- starport/cmd/network_request_verify.go | 65 +++++++++++++++++++------ 2 files changed, 58 insertions(+), 21 deletions(-) diff --git a/starport/cmd/network_request_approve.go b/starport/cmd/network_request_approve.go index 8ffe7cf35d..22c86825dd 100644 --- a/starport/cmd/network_request_approve.go +++ b/starport/cmd/network_request_approve.go @@ -51,7 +51,7 @@ func networkRequestApproveHandler(cmd *cobra.Command, args []string) error { } // Verify the requests are valid - _, err = cmd.Flags().GetBool(flagNoVerification) + noVerification, err := cmd.Flags().GetBool(flagNoVerification) if err != nil { return err } @@ -61,12 +61,12 @@ func networkRequestApproveHandler(cmd *cobra.Command, args []string) error { return err } - // if !noVerification { - // err := n.VerifyRequests(cmd.Context(), launchID, ids...) - // if err != nil { - // return err - // } - // } + // if requests must be verified, we simulate the chain in a temporary directory with the requests + if !noVerification { + // add verify request + _ = noVerification + } + // Submit the approved requests reviewals := make([]network.Reviewal, 0) for _, id := range ids { diff --git a/starport/cmd/network_request_verify.go b/starport/cmd/network_request_verify.go index 2da3554fbd..d2ceb1fb81 100644 --- a/starport/cmd/network_request_verify.go +++ b/starport/cmd/network_request_verify.go @@ -1,12 +1,15 @@ package starportcmd import ( + "context" "fmt" + "os" "github.com/spf13/cobra" "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 @@ -39,7 +42,7 @@ func networkRequestVerifyHandler(cmd *cobra.Command, args []string) error { return err } - // Get the list of request ids + // get the list of request ids ids, err := numbers.ParseList(args[1]) if err != nil { return err @@ -50,22 +53,56 @@ func networkRequestVerifyHandler(cmd *cobra.Command, args []string) error { return err } - // if !noVerification { - // err := n.VerifyRequests(cmd.Context(), launchID, ids...) - // if err != nil { - // return err - // } - // } - // Submit the approved requests - reviewals := make([]network.Reviewal, 0) - for _, id := range ids { - reviewals = append(reviewals, network.ApproveRequest(id)) - } - if err := n.SubmitRequest(launchID, reviewals...); err != nil { + // verify the requests + if err := verifyRequest(cmd.Context(), nb, n, launchID, ids...); err != nil { return err } nb.Spinner.Stop() - fmt.Printf("%s Request(s) %s approved\n", clispinner.OK, numbers.List(ids, "#")) + 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)) + 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 } From 53389fcfac8d5211e6ef6f6da7c924189a55b812 Mon Sep 17 00:00:00 2001 From: ltacker Date: Wed, 15 Dec 2021 21:41:40 +0100 Subject: [PATCH 08/22] fix chain home --- starport/cmd/network_request_verify.go | 16 ++- .../network/networkchain/networkchain.go | 2 +- .../services/network/networkchain/request.go | 108 ++++++++++++++++++ 3 files changed, 120 insertions(+), 6 deletions(-) create mode 100644 starport/services/network/networkchain/request.go diff --git a/starport/cmd/network_request_verify.go b/starport/cmd/network_request_verify.go index d2ceb1fb81..f38432f16f 100644 --- a/starport/cmd/network_request_verify.go +++ b/starport/cmd/network_request_verify.go @@ -6,6 +6,7 @@ import ( "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" @@ -16,9 +17,9 @@ import ( // command to approve requests for a chain. func NewNetworkRequestVerify() *cobra.Command { c := &cobra.Command{ - Use: "simulate [launch-id] [number<,...>]", + Use: "verify [launch-id] [number<,...>]", Aliases: []string{"accept"}, - Short: "Simulate requests and check generated genesis", + Short: "Verify the request and simulate the chain genesis from them", RunE: networkRequestVerifyHandler, Args: cobra.ExactArgs(2), } @@ -82,9 +83,14 @@ func verifyRequest( if err != nil { return err } - defer os.RemoveAll(homeDir) - - c, err := nb.Chain(networkchain.SourceLaunch(chainLaunch), networkchain.WithHome(homeDir)) + fmt.Println(homeDir) + // defer os.RemoveAll(homeDir) + + c, err := nb.Chain( + networkchain.SourceLaunch(chainLaunch), + networkchain.WithHome(homeDir), + networkchain.WithKeyringBackend(chaincmd.KeyringBackendTest), + ) if err != nil { return err } diff --git a/starport/services/network/networkchain/networkchain.go b/starport/services/network/networkchain/networkchain.go index 16bd20cf61..f2aa194640 100644 --- a/starport/services/network/networkchain/networkchain.go +++ b/starport/services/network/networkchain/networkchain.go @@ -132,10 +132,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")) diff --git a/starport/services/network/networkchain/request.go b/starport/services/network/networkchain/request.go new file mode 100644 index 0000000000..937100d605 --- /dev/null +++ b/starport/services/network/networkchain/request.go @@ -0,0 +1,108 @@ +package networkchain + +import ( + "context" + "fmt" + + "github.com/pkg/errors" + launchtypes "github.com/tendermint/spn/x/launch/types" + "github.com/tendermint/starport/starport/pkg/cosmosutil" + "github.com/tendermint/starport/starport/services/network/networktypes" +) + +// SimulateRequests simulates the genesis creation and the start of the network from the provided requests +func (c Chain) SimulateRequests(ctx context.Context, gi networktypes.GenesisInformation, reqs []launchtypes.Request) (err error) { + for _, req := range reqs { + // static verification of the request + if err := VerifyRequest(req); err != nil { + return err + } + + // apply the request to the genesis information + gi, err = gi.ApplyRequest(req) + if err != nil { + return err + } + } + + // prepare the chain with the requests + if err := c.Prepare(ctx, gi); err != nil { + return err + } + + return nil +} + +// VerifyRequest verifies the validity of the request from its content (static check) +func VerifyRequest(request launchtypes.Request) error { + req, ok := request.Content.Content.(*launchtypes.RequestContent_GenesisValidator) + if ok { + err := VerifyAddValidatorRequest(req) + if err != nil { + return errors.Wrapf(networktypes.NewErrInvalidRequest(request.RequestID), err.Error()) + } + } + + return nil +} + +// VerifyAddValidatorRequest verify the validator request parameters +func VerifyAddValidatorRequest(req *launchtypes.RequestContent_GenesisValidator) error { + // If this is an add validator request + var ( + peer = req.GenesisValidator.Peer + valAddress = req.GenesisValidator.Address + consPubKey = req.GenesisValidator.ConsPubKey + selfDelegation = req.GenesisValidator.SelfDelegation + ) + + // Check values inside the gentx are correct + info, _, err := cosmosutil.ParseGentx(req.GenesisValidator.GenTx) + if err != nil { + return fmt.Errorf("cannot parse gentx %s", err.Error()) + } + + // Change the address prefix fetched from the gentx to the one used on SPN + // Because all on-chain stored address on SPN uses the SPN prefix + spnFetchedAddress, err := cosmosutil.ChangeAddressPrefix(info.DelegatorAddress, SPN) + if err != nil { + return err + } + + // Check validator address + if valAddress != spnFetchedAddress { + return fmt.Errorf( + "the validator address %s doesn't match the one inside the gentx %s", + valAddress, + spnFetchedAddress, + ) + } + + // Check validator address + if !info.PubKey.Equal(consPubKey) { + return fmt.Errorf( + "the consensus pub key %s doesn't match the one inside the gentx %s", + string(consPubKey), + string(info.PubKey), + ) + } + + // Check self delegation + if selfDelegation.Denom != info.SelfDelegation.Denom || + !selfDelegation.IsEqual(info.SelfDelegation) { + return fmt.Errorf( + "the self delegation %s doesn't match the one inside the gentx %s", + selfDelegation.String(), + info.SelfDelegation.String(), + ) + } + + // Check the format of the peer + if !cosmosutil.VerifyPeerFormat(peer) { + return fmt.Errorf( + "the peer %s doesn't match the peer format @", + peer, + ) + } + return nil +} From b6d50bf20a586f2e9e5b4e260c94bfb9ea45e95f Mon Sep 17 00:00:00 2001 From: ltacker Date: Thu, 16 Dec 2021 08:57:45 +0100 Subject: [PATCH 09/22] Add comment --- starport/services/network/networkchain/request.go | 1 + 1 file changed, 1 insertion(+) diff --git a/starport/services/network/networkchain/request.go b/starport/services/network/networkchain/request.go index 937100d605..0bafc8ee5d 100644 --- a/starport/services/network/networkchain/request.go +++ b/starport/services/network/networkchain/request.go @@ -30,6 +30,7 @@ func (c Chain) SimulateRequests(ctx context.Context, gi networktypes.GenesisInfo return err } + // TODO: execute chain return nil } From 4fd779ec02ad8d15cb97f388753ebaa236a7c92e Mon Sep 17 00:00:00 2001 From: ltacker Date: Thu, 16 Dec 2021 10:39:34 +0100 Subject: [PATCH 10/22] simulate and refactor --- starport/services/network/join.go | 10 +- starport/services/network/launch.go | 4 +- starport/services/network/network.go | 2 + .../services/network/networkchain/home.go | 4 +- .../network/networkchain/home_test.go | 5 +- .../network/networkchain/networkchain.go | 16 +- .../services/network/networkchain/prepare.go | 8 + .../services/network/networkchain/simulate.go | 157 ++++++++++++++++++ .../network/networktypes/networktypes.go | 9 + .../{networkchain => networktypes}/request.go | 30 +--- .../request_test.go | 6 +- starport/services/network/publish.go | 6 +- starport/services/network/request.go | 4 +- 13 files changed, 207 insertions(+), 54 deletions(-) create mode 100644 starport/services/network/networkchain/simulate.go create mode 100644 starport/services/network/networktypes/networktypes.go rename starport/services/network/{networkchain => networktypes}/request.go (73%) rename starport/services/network/{networkchain => networktypes}/request_test.go (98%) diff --git a/starport/services/network/join.go b/starport/services/network/join.go index f5a4294c58..587f61711e 100644 --- a/starport/services/network/join.go +++ b/starport/services/network/join.go @@ -9,7 +9,7 @@ import ( "github.com/tendermint/starport/starport/pkg/cosmosutil" "github.com/tendermint/starport/starport/pkg/events" "github.com/tendermint/starport/starport/pkg/sdkerror" - "github.com/tendermint/starport/starport/services/network/networkchain" + "github.com/tendermint/starport/starport/services/network/networktypes" ) // Join to the network. @@ -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 } @@ -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 @@ -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), @@ -148,7 +148,7 @@ func (n Network) sendValidatorRequest( } msg := launchtypes.NewMsgRequestAddValidator( - n.account.Address(networkchain.SPN), + n.account.Address(networktypes.SPN), launchID, valAddress, gentx, diff --git a/starport/services/network/launch.go b/starport/services/network/launch.go index c01e1418ad..71d44521a8 100644 --- a/starport/services/network/launch.go +++ b/starport/services/network/launch.go @@ -9,7 +9,7 @@ import ( "github.com/tendermint/starport/starport/pkg/events" "github.com/tendermint/starport/starport/pkg/sdkerror" "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 @@ -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: diff --git a/starport/services/network/network.go b/starport/services/network/network.go index 27e4ef3bba..48946303c9 100644 --- a/starport/services/network/network.go +++ b/starport/services/network/network.go @@ -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) } diff --git a/starport/services/network/networkchain/home.go b/starport/services/network/networkchain/home.go index 0e9217d3f5..9b440e6c57 100644 --- a/starport/services/network/networkchain/home.go +++ b/starport/services/network/networkchain/home.go @@ -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. @@ -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. diff --git a/starport/services/network/networkchain/home_test.go b/starport/services/network/networkchain/home_test.go index da8441251d..3d644869d3 100644 --- a/starport/services/network/networkchain/home_test.go +++ b/starport/services/network/networkchain/home_test.go @@ -7,6 +7,7 @@ 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) { @@ -14,8 +15,8 @@ func TestChainHome(t *testing.T) { 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) } diff --git a/starport/services/network/networkchain/networkchain.go b/starport/services/network/networkchain/networkchain.go index f2aa194640..1cac8abd0d 100644 --- a/starport/services/network/networkchain/networkchain.go +++ b/starport/services/network/networkchain/networkchain.go @@ -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 @@ -200,6 +192,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 } diff --git a/starport/services/network/networkchain/prepare.go b/starport/services/network/networkchain/prepare.go index db5fd7d2f9..c52c8478e2 100644 --- a/starport/services/network/networkchain/prepare.go +++ b/starport/services/network/networkchain/prepare.go @@ -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) } diff --git a/starport/services/network/networkchain/simulate.go b/starport/services/network/networkchain/simulate.go new file mode 100644 index 0000000000..f4667df103 --- /dev/null +++ b/starport/services/network/networkchain/simulate.go @@ -0,0 +1,157 @@ +package networkchain + +import ( + "context" + "fmt" + "github.com/pelletier/go-toml" + "github.com/tendermint/starport/starport/pkg/availableport" + "os" + "strings" + "time" + + "github.com/cenkalti/backoff" + "github.com/pkg/errors" + launchtypes "github.com/tendermint/spn/x/launch/types" + "github.com/tendermint/starport/starport/pkg/httpstatuschecker" + "github.com/tendermint/starport/starport/pkg/xurl" + "github.com/tendermint/starport/starport/services/network/networktypes" +) + +const ( + ListeningTimeout = time.Minute * 1 + ValidatorSetNilErrorMessage = "validator set is nil in genesis and still empty after InitChain" +) + +// SimulateRequests simulates the genesis creation and the start of the network from the provided requests +func (c Chain) SimulateRequests(ctx context.Context, gi networktypes.GenesisInformation, reqs []launchtypes.Request) (err error) { + for _, req := range reqs { + // static verification of the request + if err := networktypes.VerifyRequest(req); err != nil { + return err + } + + // apply the request to the genesis information + gi, err = gi.ApplyRequest(req) + if err != nil { + return err + } + } + + // prepare the chain with the requests + if err := c.Prepare(ctx, gi); err != nil { + return err + } + + return c.SimulateChainStart(ctx) +} + +// SimulateChainStart simulates and verify the chain start by starting it with a simulation config +// and checking if the gentxs execution is successful +func (c Chain) SimulateChainStart(ctx context.Context) error { + cmd, err := c.chain.Commands(ctx) + if err != nil { + return err + } + + // set the config with random ports to test the start command + addressAPI, err := c.setSimulationConfig() + if err != nil { + return err + } + + // verify that the chain can be started with a valid genesis + ctx, cancel := context.WithTimeout(ctx, ListeningTimeout) + exit := make(chan error) + + // routine to check the app is listening + go func() { + defer cancel() + exit <- isChainListening(ctx, addressAPI) + }() + + // routine chain start + go func() { + // If the error is validator set is nil, it means the genesis didn't get broken after a proposal + // The genesis was correctly generated but we don't have the necessary proposals to have a validator set + // after the execution of gentxs + err := cmd.Start(ctx) + if err != nil && strings.Contains(err.Error(), ValidatorSetNilErrorMessage) { + err = nil + } + exit <- err + }() + + return <-exit +} + +// setSimulationConfig sets in the config random available ports to allow check if the chain network can start +func (c Chain) setSimulationConfig() (string, error) { + // generate random server ports and servers list + ports, err := availableport.Find(5) + if err != nil { + return "", err + } + genAddr := func(port int) string { + return fmt.Sprintf("localhost:%d", port) + } + + // updating app toml + appPath, err := c.AppTOMLPath() + if err != nil { + return "", err + } + config, err := toml.LoadFile(appPath) + if err != nil { + return "", err + } + config.Set("api.enable", true) + config.Set("api.enabled-unsafe-cors", true) + config.Set("rpc.cors_allowed_origins", []string{"*"}) + config.Set("api.address", xurl.TCP(genAddr(ports[0]))) + config.Set("grpc.address", genAddr(ports[1])) + file, err := os.OpenFile(appPath, os.O_RDWR|os.O_TRUNC, 0644) + if err != nil { + return "", err + } + defer file.Close() + _, err = config.WriteTo(file) + if err != nil { + return "", err + } + + // updating config toml + configPath, err := c.ConfigTOMLPath() + if err != nil { + return "", err + } + config, err = toml.LoadFile(configPath) + if err != nil { + return "", err + } + config.Set("rpc.cors_allowed_origins", []string{"*"}) + config.Set("consensus.timeout_commit", "1s") + config.Set("consensus.timeout_propose", "1s") + config.Set("rpc.laddr", xurl.TCP(genAddr(ports[2]))) + config.Set("p2p.laddr", xurl.TCP(genAddr(ports[3]))) + config.Set("rpc.pprof_laddr", genAddr(ports[4])) + file, err = os.OpenFile(configPath, os.O_RDWR|os.O_TRUNC, 0644) + if err != nil { + return "", err + } + defer file.Close() + _, err = config.WriteTo(file) + + return genAddr(ports[0]), err +} + +// isChainListening checks if the chain is listening for API queries on the specified address +func isChainListening(ctx context.Context, addressAPI string) error { + checkAlive := func() error { + ok, err := httpstatuschecker.Check(ctx, xurl.HTTP(addressAPI)+"/node_info") + if err == nil && !ok { + err = errors.New("app is not online") + } + return err + } + return backoff.Retry(checkAlive, backoff.WithContext(backoff.NewConstantBackOff(time.Second), ctx)) +} diff --git a/starport/services/network/networktypes/networktypes.go b/starport/services/network/networktypes/networktypes.go new file mode 100644 index 0000000000..0981152dfc --- /dev/null +++ b/starport/services/network/networktypes/networktypes.go @@ -0,0 +1,9 @@ +package 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" +) \ No newline at end of file diff --git a/starport/services/network/networkchain/request.go b/starport/services/network/networktypes/request.go similarity index 73% rename from starport/services/network/networkchain/request.go rename to starport/services/network/networktypes/request.go index 0bafc8ee5d..5e7ed57be5 100644 --- a/starport/services/network/networkchain/request.go +++ b/starport/services/network/networktypes/request.go @@ -1,46 +1,20 @@ -package networkchain +package networktypes import ( - "context" "fmt" "github.com/pkg/errors" launchtypes "github.com/tendermint/spn/x/launch/types" "github.com/tendermint/starport/starport/pkg/cosmosutil" - "github.com/tendermint/starport/starport/services/network/networktypes" ) -// SimulateRequests simulates the genesis creation and the start of the network from the provided requests -func (c Chain) SimulateRequests(ctx context.Context, gi networktypes.GenesisInformation, reqs []launchtypes.Request) (err error) { - for _, req := range reqs { - // static verification of the request - if err := VerifyRequest(req); err != nil { - return err - } - - // apply the request to the genesis information - gi, err = gi.ApplyRequest(req) - if err != nil { - return err - } - } - - // prepare the chain with the requests - if err := c.Prepare(ctx, gi); err != nil { - return err - } - - // TODO: execute chain - return nil -} - // VerifyRequest verifies the validity of the request from its content (static check) func VerifyRequest(request launchtypes.Request) error { req, ok := request.Content.Content.(*launchtypes.RequestContent_GenesisValidator) if ok { err := VerifyAddValidatorRequest(req) if err != nil { - return errors.Wrapf(networktypes.NewErrInvalidRequest(request.RequestID), err.Error()) + return errors.Wrapf(NewErrInvalidRequest(request.RequestID), err.Error()) } } diff --git a/starport/services/network/networkchain/request_test.go b/starport/services/network/networktypes/request_test.go similarity index 98% rename from starport/services/network/networkchain/request_test.go rename to starport/services/network/networktypes/request_test.go index 17934c1ce4..efb1677676 100644 --- a/starport/services/network/networkchain/request_test.go +++ b/starport/services/network/networktypes/request_test.go @@ -1,13 +1,13 @@ -package networkchain_test +package networktypes_test import ( "fmt" - "github.com/tendermint/starport/starport/services/network/networkchain" "testing" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" launchtypes "github.com/tendermint/spn/x/launch/types" + "github.com/tendermint/starport/starport/services/network/networktypes" ) func TestVerifyAddValidatorRequest(t *testing.T) { @@ -141,7 +141,7 @@ func TestVerifyAddValidatorRequest(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := networkchain.VerifyAddValidatorRequest(tt.req) + err := networktypes.VerifyAddValidatorRequest(tt.req) if tt.want != nil { require.Error(t, err) require.Equal(t, tt.want.Error(), err.Error()) diff --git a/starport/services/network/publish.go b/starport/services/network/publish.go index f1bf2be266..125d440ea2 100644 --- a/starport/services/network/publish.go +++ b/starport/services/network/publish.go @@ -9,7 +9,7 @@ import ( "github.com/tendermint/starport/starport/pkg/cosmosutil" "github.com/tendermint/starport/starport/pkg/events" "github.com/tendermint/starport/starport/pkg/sdkerror" - "github.com/tendermint/starport/starport/services/network/networkchain" + "github.com/tendermint/starport/starport/services/network/networktypes" ) // publishOptions holds info about how to create a chain. @@ -75,7 +75,7 @@ func (n Network) Publish(ctx context.Context, c Chain, options ...PublishOption) } } - coordinatorAddress := n.account.Address(networkchain.SPN) + coordinatorAddress := n.account.Address(networktypes.SPN) campaignID = o.campaignID n.ev.Send(events.New(events.StatusOngoing, "Publishing the network")) @@ -128,7 +128,7 @@ func (n Network) Publish(ctx context.Context, c Chain, options ...PublishOption) } msgCreateChain := launchtypes.NewMsgCreateChain( - n.account.Address(networkchain.SPN), + n.account.Address(networktypes.SPN), chainID, c.SourceURL(), c.SourceHash(), diff --git a/starport/services/network/request.go b/starport/services/network/request.go index e5832c10bd..a0497062d4 100644 --- a/starport/services/network/request.go +++ b/starport/services/network/request.go @@ -7,7 +7,7 @@ import ( launchtypes "github.com/tendermint/spn/x/launch/types" "github.com/tendermint/starport/starport/pkg/events" "github.com/tendermint/starport/starport/pkg/sdkerror" - "github.com/tendermint/starport/starport/services/network/networkchain" + "github.com/tendermint/starport/starport/services/network/networktypes" ) // Reviewal keeps a request's reviewal. @@ -75,7 +75,7 @@ func (n Network) SubmitRequest(launchID uint64, reviewal ...Reviewal) error { messages := make([]sdk.Msg, len(reviewal)) for i, reviewal := range reviewal { messages[i] = launchtypes.NewMsgSettleRequest( - n.account.Address(networkchain.SPN), + n.account.Address(networktypes.SPN), launchID, reviewal.RequestID, reviewal.IsApproved, From 36aea7db72c540d51041f9cee5429f621b94634c Mon Sep 17 00:00:00 2001 From: ltacker Date: Thu, 16 Dec 2021 10:41:31 +0100 Subject: [PATCH 11/22] Lint --- starport/cmd/network.go | 5 +++-- starport/services/network/networkchain/simulate.go | 6 +++--- starport/services/network/networktypes/networktypes.go | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/starport/cmd/network.go b/starport/cmd/network.go index d42786dc18..f24163dad4 100644 --- a/starport/cmd/network.go +++ b/starport/cmd/network.go @@ -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 ( @@ -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), } diff --git a/starport/services/network/networkchain/simulate.go b/starport/services/network/networkchain/simulate.go index f4667df103..e197dbeb2a 100644 --- a/starport/services/network/networkchain/simulate.go +++ b/starport/services/network/networkchain/simulate.go @@ -3,22 +3,22 @@ package networkchain import ( "context" "fmt" - "github.com/pelletier/go-toml" - "github.com/tendermint/starport/starport/pkg/availableport" "os" "strings" "time" "github.com/cenkalti/backoff" + "github.com/pelletier/go-toml" "github.com/pkg/errors" launchtypes "github.com/tendermint/spn/x/launch/types" + "github.com/tendermint/starport/starport/pkg/availableport" "github.com/tendermint/starport/starport/pkg/httpstatuschecker" "github.com/tendermint/starport/starport/pkg/xurl" "github.com/tendermint/starport/starport/services/network/networktypes" ) const ( - ListeningTimeout = time.Minute * 1 + ListeningTimeout = time.Minute * 1 ValidatorSetNilErrorMessage = "validator set is nil in genesis and still empty after InitChain" ) diff --git a/starport/services/network/networktypes/networktypes.go b/starport/services/network/networktypes/networktypes.go index 0981152dfc..bf43726ce4 100644 --- a/starport/services/network/networktypes/networktypes.go +++ b/starport/services/network/networktypes/networktypes.go @@ -6,4 +6,4 @@ const ( // SPNDenom is the denom used for the spn chain native token SPNDenom = "uspn" -) \ No newline at end of file +) From 4fef1a0a074ae9fa46a0150299637d10822b1170 Mon Sep 17 00:00:00 2001 From: ltacker Date: Thu, 16 Dec 2021 11:30:36 +0100 Subject: [PATCH 12/22] refacotr --- starport/cmd/network_request_verify.go | 4 ++-- starport/pkg/clispinner/icon.go | 4 +++- .../services/network/networkchain/simulate.go | 19 ++++++++++++++----- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/starport/cmd/network_request_verify.go b/starport/cmd/network_request_verify.go index f38432f16f..4996174fb6 100644 --- a/starport/cmd/network_request_verify.go +++ b/starport/cmd/network_request_verify.go @@ -56,6 +56,7 @@ func networkRequestVerifyHandler(cmd *cobra.Command, args []string) error { // 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 } @@ -83,8 +84,7 @@ func verifyRequest( if err != nil { return err } - fmt.Println(homeDir) - // defer os.RemoveAll(homeDir) + defer os.RemoveAll(homeDir) c, err := nb.Chain( networkchain.SourceLaunch(chainLaunch), diff --git a/starport/pkg/clispinner/icon.go b/starport/pkg/clispinner/icon.go index e7ddc59c0c..b0a4c1be17 100644 --- a/starport/pkg/clispinner/icon.go +++ b/starport/pkg/clispinner/icon.go @@ -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()("⋆") ) diff --git a/starport/services/network/networkchain/simulate.go b/starport/services/network/networkchain/simulate.go index e197dbeb2a..8ee6bb41f3 100644 --- a/starport/services/network/networkchain/simulate.go +++ b/starport/services/network/networkchain/simulate.go @@ -12,6 +12,7 @@ import ( "github.com/pkg/errors" launchtypes "github.com/tendermint/spn/x/launch/types" "github.com/tendermint/starport/starport/pkg/availableport" + "github.com/tendermint/starport/starport/pkg/events" "github.com/tendermint/starport/starport/pkg/httpstatuschecker" "github.com/tendermint/starport/starport/pkg/xurl" "github.com/tendermint/starport/starport/services/network/networktypes" @@ -24,6 +25,7 @@ const ( // SimulateRequests simulates the genesis creation and the start of the network from the provided requests func (c Chain) SimulateRequests(ctx context.Context, gi networktypes.GenesisInformation, reqs []launchtypes.Request) (err error) { + c.ev.Send(events.New(events.StatusOngoing, "Verifying requests format")) for _, req := range reqs { // static verification of the request if err := networktypes.VerifyRequest(req); err != nil { @@ -36,18 +38,25 @@ func (c Chain) SimulateRequests(ctx context.Context, gi networktypes.GenesisInfo return err } } + c.ev.Send(events.New(events.StatusDone, "Requests format verified")) // prepare the chain with the requests if err := c.Prepare(ctx, gi); err != nil { return err } - return c.SimulateChainStart(ctx) + c.ev.Send(events.New(events.StatusOngoing, "Trying starting the network with the requests")) + if err := c.simulateChainStart(ctx); err != nil { + return err + } + c.ev.Send(events.New(events.StatusDone, "The network can be started")) + + return nil } // SimulateChainStart simulates and verify the chain start by starting it with a simulation config // and checking if the gentxs execution is successful -func (c Chain) SimulateChainStart(ctx context.Context) error { +func (c Chain) simulateChainStart(ctx context.Context) error { cmd, err := c.chain.Commands(ctx) if err != nil { return err @@ -71,9 +80,9 @@ func (c Chain) SimulateChainStart(ctx context.Context) error { // routine chain start go func() { - // If the error is validator set is nil, it means the genesis didn't get broken after a proposal - // The genesis was correctly generated but we don't have the necessary proposals to have a validator set - // after the execution of gentxs + // if the error is validator set is nil, it means the genesis didn't get broken after an applied request + // the genesis was correctly generated but there is no gentxs so far + // so we don't consider it as an error making requests to verify as invalid err := cmd.Start(ctx) if err != nil && strings.Contains(err.Error(), ValidatorSetNilErrorMessage) { err = nil From c34cdc63e86934ff73c1a4ed6b318bfcf0643927 Mon Sep 17 00:00:00 2001 From: ltacker Date: Thu, 16 Dec 2021 12:05:15 +0100 Subject: [PATCH 13/22] refactor error --- starport/services/network/networkchain/simulate.go | 2 ++ starport/services/network/networktypes/error.go | 12 ++++++++---- .../network/networktypes/genesisinformation.go | 10 +++++----- starport/services/network/networktypes/request.go | 3 +-- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/starport/services/network/networkchain/simulate.go b/starport/services/network/networkchain/simulate.go index 8ee6bb41f3..3a0de8f201 100644 --- a/starport/services/network/networkchain/simulate.go +++ b/starport/services/network/networkchain/simulate.go @@ -86,6 +86,8 @@ func (c Chain) simulateChainStart(ctx context.Context) error { err := cmd.Start(ctx) if err != nil && strings.Contains(err.Error(), ValidatorSetNilErrorMessage) { err = nil + } else { + err = errors.Wrap(err, "the chain failed to start") } exit <- err }() diff --git a/starport/services/network/networktypes/error.go b/starport/services/network/networktypes/error.go index 020e396297..f54bff935a 100644 --- a/starport/services/network/networktypes/error.go +++ b/starport/services/network/networktypes/error.go @@ -1,6 +1,10 @@ package networktypes -import "fmt" +import ( + "fmt" + + "github.com/pkg/errors" +) // ErrInvalidRequest is an error returned in methods manipulating requests when they are invalid type ErrInvalidRequest struct { @@ -12,7 +16,7 @@ func (err ErrInvalidRequest) Error() string { return fmt.Sprintf("request %d is invalid", err.requestID) } -// NewErrInvalidRequest returns a new ErrInvalidRequest -func NewErrInvalidRequest(requestID uint64) ErrInvalidRequest { - return ErrInvalidRequest{requestID: requestID} +// NewWrappedErrInvalidRequest returns a wrapped ErrInvalidRequest +func NewWrappedErrInvalidRequest(requestID uint64, message string) error { + return errors.Wrap(ErrInvalidRequest{requestID: requestID}, message) } diff --git a/starport/services/network/networktypes/genesisinformation.go b/starport/services/network/networktypes/genesisinformation.go index 286709757e..cd3f33e170 100644 --- a/starport/services/network/networktypes/genesisinformation.go +++ b/starport/services/network/networktypes/genesisinformation.go @@ -122,7 +122,7 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI _, genExist := gi.GenesisAccounts[ga.Address] _, vestingExist := gi.VestingAccounts[ga.Address] if genExist || vestingExist { - return gi, errors.Wrapf(NewErrInvalidRequest(request.RequestID), "genesis account %s already in genesis", ga.Address) + return gi, NewWrappedErrInvalidRequest(request.RequestID, "genesis account already in genesis") } gi.GenesisAccounts[ga.Address] = ga @@ -139,7 +139,7 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI _, genExist := gi.GenesisAccounts[va.Address] _, vestingExist := gi.VestingAccounts[va.Address] if genExist || vestingExist { - return gi, errors.Wrapf(NewErrInvalidRequest(request.RequestID), "vesting account %s already in genesis", va.Address) + return gi, NewWrappedErrInvalidRequest(request.RequestID, "vesting account already in genesis") } gi.VestingAccounts[va.Address] = va @@ -149,7 +149,7 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI _, genExist := gi.GenesisAccounts[ar.Address] _, vestingExist := gi.VestingAccounts[ar.Address] if !genExist && !vestingExist { - return gi, errors.Wrapf(NewErrInvalidRequest(request.RequestID), "account %s can't be removed because it doesn't exist", ar.Address) + return gi, NewWrappedErrInvalidRequest(request.RequestID, "account can't be removed because it doesn't exist") } delete(gi.GenesisAccounts, ar.Address) delete(gi.VestingAccounts, ar.Address) @@ -158,7 +158,7 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI // new genesis validator in the genesis gv := ToGenesisValidator(*requestContent.GenesisValidator) if _, ok := gi.GenesisValidators[gv.Address]; ok { - return gi, errors.Wrapf(NewErrInvalidRequest(request.RequestID), "genesis validator %s already in genesis", gv.Address) + return gi, NewWrappedErrInvalidRequest(request.RequestID, "genesis validator already in genesis") } gi.GenesisValidators[gv.Address] = gv @@ -166,7 +166,7 @@ func (gi GenesisInformation) ApplyRequest(request launchtypes.Request) (GenesisI // validator removed from the genesis vr := requestContent.ValidatorRemoval if _, ok := gi.GenesisValidators[vr.ValAddress]; !ok { - return gi, errors.Wrapf(NewErrInvalidRequest(request.RequestID), "genesis validator %s can't be removed because it doesn't exist", vr.ValAddress) + return gi, NewWrappedErrInvalidRequest(request.RequestID, "genesis validator can't be removed because it doesn't exist") } } diff --git a/starport/services/network/networktypes/request.go b/starport/services/network/networktypes/request.go index 5e7ed57be5..57b5cded25 100644 --- a/starport/services/network/networktypes/request.go +++ b/starport/services/network/networktypes/request.go @@ -3,7 +3,6 @@ package networktypes import ( "fmt" - "github.com/pkg/errors" launchtypes "github.com/tendermint/spn/x/launch/types" "github.com/tendermint/starport/starport/pkg/cosmosutil" ) @@ -14,7 +13,7 @@ func VerifyRequest(request launchtypes.Request) error { if ok { err := VerifyAddValidatorRequest(req) if err != nil { - return errors.Wrapf(NewErrInvalidRequest(request.RequestID), err.Error()) + return NewWrappedErrInvalidRequest(request.RequestID, err.Error()) } } From 0eb9c1110e3cfebbfee882d8e6e69c2490ce8d0e Mon Sep 17 00:00:00 2001 From: ltacker Date: Thu, 16 Dec 2021 14:08:38 +0100 Subject: [PATCH 14/22] changes in log --- starport/cmd/network_request_approve.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/starport/cmd/network_request_approve.go b/starport/cmd/network_request_approve.go index 22c86825dd..1a31192d41 100644 --- a/starport/cmd/network_request_approve.go +++ b/starport/cmd/network_request_approve.go @@ -2,7 +2,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" @@ -63,8 +63,10 @@ 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 { - // add verify request - _ = noVerification + 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 From 698f168c3021e4c963a3499885892c7c46d1587e Mon Sep 17 00:00:00 2001 From: ltacker Date: Thu, 16 Dec 2021 14:09:03 +0100 Subject: [PATCH 15/22] lint --- starport/cmd/network_request_approve.go | 1 + 1 file changed, 1 insertion(+) diff --git a/starport/cmd/network_request_approve.go b/starport/cmd/network_request_approve.go index 1a31192d41..b22dd0aae9 100644 --- a/starport/cmd/network_request_approve.go +++ b/starport/cmd/network_request_approve.go @@ -2,6 +2,7 @@ package starportcmd import ( "fmt" + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/tendermint/starport/starport/pkg/clispinner" From 8a40df80fd4388073467ce3a812fbb8eddae9e2b Mon Sep 17 00:00:00 2001 From: Lucas Bertrand Date: Mon, 3 Jan 2022 09:56:48 +0100 Subject: [PATCH 16/22] Update starport/cmd/network_request_verify.go Co-authored-by: Danilo Pantani --- starport/cmd/network_request_verify.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/starport/cmd/network_request_verify.go b/starport/cmd/network_request_verify.go index 4996174fb6..f14d8eb95f 100644 --- a/starport/cmd/network_request_verify.go +++ b/starport/cmd/network_request_verify.go @@ -13,8 +13,7 @@ import ( "github.com/tendermint/starport/starport/services/network/networkchain" ) -// NewNetworkRequestVerify creates a new request approve -// command to approve requests for a chain. +// NewNetworkRequestVerify verify the request and simulate the chain. func NewNetworkRequestVerify() *cobra.Command { c := &cobra.Command{ Use: "verify [launch-id] [number<,...>]", From 7c58169d5e8f5db1e0fbf3524e221e47daef2daa Mon Sep 17 00:00:00 2001 From: Lucas Bertrand Date: Mon, 3 Jan 2022 10:11:07 +0100 Subject: [PATCH 17/22] Update starport/services/network/request.go Co-authored-by: Danilo Pantani --- starport/services/network/request.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/starport/services/network/request.go b/starport/services/network/request.go index d19dfda6fc..1a56aaece8 100644 --- a/starport/services/network/request.go +++ b/starport/services/network/request.go @@ -55,7 +55,7 @@ func (n Network) Request(ctx context.Context, launchID, requestID uint64) (launc return res.Request, nil } -// RequestFromIDs fetches the chain requestd from SPN by launch and ptovided request IDs +// RequestFromIDs fetches the chain requested from SPN by launch and provided request IDs // TODO: once implemented, use the SPN query from https://github.com/tendermint/spn/issues/420 func (n Network) RequestFromIDs(ctx context.Context, launchID uint64, requestIDs ...uint64) (reqs []launchtypes.Request, err error) { for _, id := range requestIDs { From 34b2b4ebe3f703e80365ed7e3771e04c279aa91e Mon Sep 17 00:00:00 2001 From: ltacker Date: Mon, 3 Jan 2022 10:11:47 +0100 Subject: [PATCH 18/22] Danilo suggest --- starport/cmd/network_request_verify.go | 1 - 1 file changed, 1 deletion(-) diff --git a/starport/cmd/network_request_verify.go b/starport/cmd/network_request_verify.go index 4996174fb6..d86f6e1ea2 100644 --- a/starport/cmd/network_request_verify.go +++ b/starport/cmd/network_request_verify.go @@ -18,7 +18,6 @@ import ( 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), From a1ae44ca76e13ecdecf6867b5022c8ab7c67ec22 Mon Sep 17 00:00:00 2001 From: ltacker Date: Mon, 3 Jan 2022 16:19:06 +0100 Subject: [PATCH 19/22] lint --- starport/cmd/network_request_verify.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/starport/cmd/network_request_verify.go b/starport/cmd/network_request_verify.go index 8ca567e39e..1fe5143b6b 100644 --- a/starport/cmd/network_request_verify.go +++ b/starport/cmd/network_request_verify.go @@ -16,10 +16,10 @@ import ( // NewNetworkRequestVerify verify the request and simulate the chain. func NewNetworkRequestVerify() *cobra.Command { c := &cobra.Command{ - Use: "verify [launch-id] [number<,...>]", - Short: "Verify the request and simulate the chain genesis from them", - RunE: networkRequestVerifyHandler, - Args: cobra.ExactArgs(2), + Use: "verify [launch-id] [number<,...>]", + 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()) From 9a7739070ee1996cf50be1ae99f13a1804c204af Mon Sep 17 00:00:00 2001 From: ltacker Date: Tue, 4 Jan 2022 15:48:10 +0100 Subject: [PATCH 20/22] Ilker comment --- starport/cmd/network_request_verify.go | 6 +----- starport/services/network/networkchain/prepare.go | 3 +-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/starport/cmd/network_request_verify.go b/starport/cmd/network_request_verify.go index 1fe5143b6b..23cedf6597 100644 --- a/starport/cmd/network_request_verify.go +++ b/starport/cmd/network_request_verify.go @@ -104,9 +104,5 @@ func verifyRequest( return err } - if err := c.SimulateRequests(ctx, genesisInformation, requests); err != nil { - return err - } - - return nil + return c.SimulateRequests(ctx, genesisInformation, requests) } diff --git a/starport/services/network/networkchain/prepare.go b/starport/services/network/networkchain/prepare.go index c52c8478e2..983a3d84bc 100644 --- a/starport/services/network/networkchain/prepare.go +++ b/starport/services/network/networkchain/prepare.go @@ -58,8 +58,7 @@ func (c Chain) Prepare(ctx context.Context, gi networktypes.GenesisInformation) } // ensure genesis has a valid format - err = cmd.ValidateGenesis(ctx) - if err != nil { + if err := cmd.ValidateGenesis(ctx); err != nil { return err } From 5d81a3d89a49af077d69ced50dabdef48419bd55 Mon Sep 17 00:00:00 2001 From: ltacker Date: Tue, 4 Jan 2022 15:54:48 +0100 Subject: [PATCH 21/22] comment --- starport/cmd/network_request_approve.go | 2 +- starport/cmd/network_request_verify.go | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/starport/cmd/network_request_approve.go b/starport/cmd/network_request_approve.go index b22dd0aae9..0473aaafe9 100644 --- a/starport/cmd/network_request_approve.go +++ b/starport/cmd/network_request_approve.go @@ -64,7 +64,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(), nb, n, launchID, ids...); err != nil { + if err := verifyRequest(cmd.Context(), nb, 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, "#")) diff --git a/starport/cmd/network_request_verify.go b/starport/cmd/network_request_verify.go index 23cedf6597..5bfe88715f 100644 --- a/starport/cmd/network_request_verify.go +++ b/starport/cmd/network_request_verify.go @@ -47,13 +47,8 @@ func networkRequestVerifyHandler(cmd *cobra.Command, args []string) error { 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 { + if err := verifyRequest(cmd.Context(), nb, launchID, ids...); err != nil { fmt.Printf("%s Request(s) %s not valid\n", clispinner.NotOK, numbers.List(ids, "#")) return err } @@ -68,10 +63,14 @@ func networkRequestVerifyHandler(cmd *cobra.Command, args []string) error { func verifyRequest( ctx context.Context, nb NetworkBuilder, - n network.Network, launchID uint64, requestIDs ...uint64, ) error { + n, err := nb.Network() + if err != nil { + return err + } + // initialize the chain with a temporary dir chainLaunch, err := n.ChainLaunch(ctx, launchID) if err != nil { From 4653ff12aa7e73ffcee489fdf5e069cad301bc76 Mon Sep 17 00:00:00 2001 From: ltacker Date: Fri, 7 Jan 2022 11:19:25 +0100 Subject: [PATCH 22/22] ilker comment --- starport/services/network/networkchain/simulate.go | 4 +--- starport/services/network/networktypes/genesisinformation.go | 1 + 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/starport/services/network/networkchain/simulate.go b/starport/services/network/networkchain/simulate.go index 3a0de8f201..371020f626 100644 --- a/starport/services/network/networkchain/simulate.go +++ b/starport/services/network/networkchain/simulate.go @@ -86,10 +86,8 @@ func (c Chain) simulateChainStart(ctx context.Context) error { err := cmd.Start(ctx) if err != nil && strings.Contains(err.Error(), ValidatorSetNilErrorMessage) { err = nil - } else { - err = errors.Wrap(err, "the chain failed to start") } - exit <- err + exit <- errors.Wrap(err, "the chain failed to start") }() return <-exit diff --git a/starport/services/network/networktypes/genesisinformation.go b/starport/services/network/networktypes/genesisinformation.go index c4b390f1cf..7493f6c625 100644 --- a/starport/services/network/networktypes/genesisinformation.go +++ b/starport/services/network/networktypes/genesisinformation.go @@ -8,6 +8,7 @@ import ( ) // GenesisInformation represents all information for a chain to construct the genesis. +// This structure indexes accounts and validators by their address for better performance type GenesisInformation struct { GenesisAccounts map[string]GenesisAccount VestingAccounts map[string]VestingAccount