Skip to content

Commit

Permalink
Merge branch 'develop' into fix/localnet-lastblockheight
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis authored Apr 25, 2022
2 parents 14960b5 + 424657d commit 9519b7a
Show file tree
Hide file tree
Showing 25 changed files with 1,237 additions and 91 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ release/
.envrc
.vscode

# OS
.DS_Store

# Localnet
localnet/node?/data/*.db
localnet/node?/data/snapshots
Expand Down
3 changes: 2 additions & 1 deletion app/simulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/rand"
"os"
"testing"
"time"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -50,7 +51,6 @@ func interBlockCacheOpt() func(*baseapp.BaseApp) {
return baseapp.SetInterBlockCache(store.NewCommitKVStoreCacheManager())
}

// go test ./app -v -benchmem -run=^$ -bench ^BenchmarkSimulation -Commit=true -cpuprofile cpu.out
func BenchmarkSimulation(b *testing.B) {
simapp.FlagVerboseValue = true
simapp.FlagOnOperationValue = true
Expand Down Expand Up @@ -118,6 +118,7 @@ func TestAppStateDeterminism(t *testing.T) {
config.OnOperation = true
config.AllInvariants = true

rand.Seed(time.Now().Unix())
numSeeds := 3
numTimesToRunPerSeed := 5
appHashList := make([]json.RawMessage, numTimesToRunPerSeed)
Expand Down
25 changes: 25 additions & 0 deletions proto/profile/events.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
syntax = "proto3";
package tendermint.spn.profile;

import "gogoproto/gogo.proto";
import "profile/coordinator.proto";
import "profile/validator.proto";


option go_package = "github.com/tendermint/spn/x/profile/types";

message EventCoordinatorCreated {
Coordinator coordinator = 1 [(gogoproto.nullable) = false];
}

message EventCoordinatorUpdated {
Coordinator coordinator = 1 [(gogoproto.nullable) = false];
}

message EventValidatorCreated {
Validator validator = 1 [(gogoproto.nullable) = false];
}

message EventValidatorUpdated {
Validator validator = 1 [(gogoproto.nullable) = false];
}
2 changes: 1 addition & 1 deletion testutil/sample/campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func CampaignParams(r *rand.Rand) campaign.Params {
maxTotalSupply := campaign.DefaultMaxTotalSupply

// assign random small amount of staking denom
campaignCreationFee := sdk.NewCoins(sdk.NewInt64Coin(BondDenom, r.Int63n(100)+1))
campaignCreationFee := sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, r.Int63n(100)+1))

return campaign.NewParams(minTotalSupply, maxTotalSupply, campaignCreationFee)
}
Expand Down
2 changes: 1 addition & 1 deletion testutil/sample/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func LaunchParams(r *rand.Rand) launch.Params {
minLaunchTime := r.Int63n(10) + launch.DefaultMinLaunchTime

// assign random small amount of staking denom
chainCreationFee := sdk.NewCoins(sdk.NewInt64Coin(BondDenom, r.Int63n(100)+1))
chainCreationFee := sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, r.Int63n(100)+1))

return launch.NewParams(minLaunchTime, maxLaunchTime, launch.DefaultRevertDelay, chainCreationFee)
}
Expand Down
29 changes: 26 additions & 3 deletions testutil/sample/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import (
)

const (
// BondDenom defines the bond denom used in testing
BondDenom = "stake"

simAccountsNb = 100
)

Expand All @@ -36,3 +33,29 @@ func SimAccounts() (accounts []simtypes.Account) {
func Rand() *rand.Rand {
return rand.New(rand.NewSource(time.Now().Unix()))
}

// Fees returns a random fee by selecting a random amount of bond denomination
// from the account's available balance. If the user doesn't have enough funds for
// paying fees, it returns empty coins.
func Fees(r *rand.Rand, spendableCoins sdk.Coins) (sdk.Coins, error) {
if spendableCoins.Empty() {
return nil, nil
}

bondDenomAmt := spendableCoins.AmountOf(sdk.DefaultBondDenom)
if bondDenomAmt.IsZero() {
return nil, nil
}

amt, err := simtypes.RandPositiveInt(r, bondDenomAmt)
if err != nil {
return nil, err
}

if amt.IsZero() {
return nil, nil
}

fees := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, amt))
return fees, nil
}
56 changes: 56 additions & 0 deletions testutil/simulation/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package simulation

import (
"github.com/cosmos/cosmos-sdk/simapp/helpers"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
sdksimulation "github.com/cosmos/cosmos-sdk/x/simulation"

"github.com/tendermint/spn/testutil/sample"
)

// GenAndDeliverTxWithRandFees generates a transaction with a random fee and delivers it.
func GenAndDeliverTxWithRandFees(txCtx sdksimulation.OperationInput, gas uint64) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
account := txCtx.AccountKeeper.GetAccount(txCtx.Context, txCtx.SimAccount.Address)
spendable := txCtx.Bankkeeper.SpendableCoins(txCtx.Context, account.GetAddress())

var fees sdk.Coins
var err error

coins, hasNeg := spendable.SafeSub(txCtx.CoinsSpentInMsg)
if hasNeg {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "message doesn't leave room for fees"), nil, err
}

fees, err = sample.Fees(txCtx.R, coins)
if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to generate fees"), nil, err
}
return GenAndDeliverTx(txCtx, fees, gas)
}

// GenAndDeliverTx generates a transactions and delivers it.
func GenAndDeliverTx(txCtx sdksimulation.OperationInput, fees sdk.Coins, gas uint64) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
account := txCtx.AccountKeeper.GetAccount(txCtx.Context, txCtx.SimAccount.Address)
tx, err := helpers.GenTx(
txCtx.TxGen,
[]sdk.Msg{txCtx.Msg},
fees,
gas,
txCtx.Context.ChainID(),
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
txCtx.SimAccount.PrivKey,
)

if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to generate mock tx"), nil, err
}

_, _, err = txCtx.App.Deliver(txCtx.TxGen.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to deliver tx"), nil, err
}

return simtypes.NewOperationMsg(txCtx.Msg, true, "", txCtx.Cdc), nil, nil
}
8 changes: 5 additions & 3 deletions x/campaign/simulation/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"math/rand"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/simapp/helpers"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/simulation"
sdksimulation "github.com/cosmos/cosmos-sdk/x/simulation"

"github.com/tendermint/spn/testutil/sample"
"github.com/tendermint/spn/testutil/simulation"
"github.com/tendermint/spn/x/campaign/keeper"
"github.com/tendermint/spn/x/campaign/types"
)
Expand All @@ -32,7 +34,7 @@ func deliverSimTx(
msg TypedMsg,
coinsSpent sdk.Coins,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
txCtx := simulation.OperationInput{
txCtx := sdksimulation.OperationInput{
R: r,
App: app,
TxGen: simappparams.MakeTestEncodingConfig().TxConfig,
Expand All @@ -46,7 +48,7 @@ func deliverSimTx(
ModuleName: types.ModuleName,
CoinsSpentInMsg: coinsSpent,
}
return simulation.GenAndDeliverTxWithRandFees(txCtx)
return simulation.GenAndDeliverTxWithRandFees(txCtx, helpers.DefaultGenTxGas)
}

// SimulateMsgCreateCampaign simulates a MsgCreateCampaign message
Expand Down
Loading

0 comments on commit 9519b7a

Please sign in to comment.