Misc, environment variables

This commit is contained in:
Christopher Goes 2018-07-18 07:37:38 +02:00
parent c61b1aa591
commit 6c61577b0b
6 changed files with 54 additions and 13 deletions

View File

@ -6,7 +6,7 @@ BUILD_TAGS = netgo ledger
BUILD_FLAGS = -tags "${BUILD_TAGS}" -ldflags "-X github.com/cosmos/cosmos-sdk/version.GitCommit=${COMMIT_HASH}"
GCC := $(shell command -v gcc 2> /dev/null)
LEDGER_ENABLED ?= true
all: get_tools get_vendor_deps install install_examples test_lint test test_sim
all: get_tools get_vendor_deps install install_examples test_lint test
########################################
### CI
@ -114,10 +114,10 @@ test_race:
@go test -race $(PACKAGES_NOCLITEST)
test_sim:
@echo "Running individual module simulations..."
@echo "Running individual module simulations."
@go test $(PACKAGES_SIMTEST) -v
@echo "Running full Gaia simulation..."
@ENABLE_GAIA_SIMULATION=1 go test ./cmd/gaia/app -run TestFullGaiaSimulation -v
@echo "Running full Gaia simulation. This may take several minutes. Set the environment variable 'GAIA_SIMULATION_SEED' to run with a constant seed."
@GAIA_SIMULATION_ENABLED=1 go test ./cmd/gaia/app -run TestFullGaiaSimulation -v
test_cover:
@bash tests/test_cover.sh

View File

@ -20,9 +20,13 @@ import (
const (
NumKeys = 10
NumBlocks = 100
BlockSize = 500
BlockSize = 100
simulationEnv = "ENABLE_GAIA_SIMULATION"
simulationEnvEnable = "GAIA_SIMULATION_ENABLED"
simulationEnvSeed = "GAIA_SIMULATION_SEED"
simulationEnvKeys = "GAIA_SIMULATION_KEYS"
simulationEnvBlocks = "GAIA_SIMULATION_BLOCKS"
simulationEnvBlockSize = "GAIA_SIMULATION_BLOCK_SIZE"
)
func appStateFn(r *rand.Rand, accs []sdk.AccAddress) json.RawMessage {
@ -55,7 +59,7 @@ func appStateFn(r *rand.Rand, accs []sdk.AccAddress) json.RawMessage {
}
func TestFullGaiaSimulation(t *testing.T) {
if os.Getenv(simulationEnv) == "" {
if os.Getenv(simulationEnvEnable) == "" {
t.Skip("Skipping Gaia simulation")
}
@ -65,11 +69,26 @@ func TestFullGaiaSimulation(t *testing.T) {
app := NewGaiaApp(logger, db, nil)
require.Equal(t, "GaiaApp", app.Name())
var seed int64
envSeed := os.Getenv(simulationEnvSeed)
if envSeed != "" {
seed, err = strconv.ParseInt(envSeed, 10, 64)
require.Nil(t, err)
} else {
seed = time.Now().UnixNano()
}
// Run randomized simulation
simulation.Simulate(
t, app.BaseApp, appStateFn,
simulation.SimulateFromSeed(
t, app.BaseApp, appStateFn, seed,
[]simulation.TestAndRunTx{
stakesim.SimulateMsgCreateValidator(app.accountMapper, app.stakeKeeper),
stakesim.SimulateMsgEditValidator(app.accountMapper, app.stakeKeeper),
stakesim.SimulateMsgDelegate(app.accountMapper, app.stakeKeeper),
stakesim.SimulateMsgBeginUnbonding(app.accountMapper, app.stakeKeeper),
stakesim.SimulateMsgCompleteUnbonding(app.stakeKeeper),
stakesim.SimulateMsgBeginRedelegate(app.accountMapper, app.stakeKeeper),
stakesim.SimulateMsgCompleteRedelegate(app.stakeKeeper),
},
[]simulation.RandSetup{},
[]simulation.Invariant{

View File

@ -6,6 +6,7 @@ import (
)
// shamelessly copied from https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang#31832326
// TODO we should probably move this to tendermint/libs/common/random.go
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (

View File

@ -110,6 +110,22 @@ func (k Keeper) GetUnbondingDelegationsFromValidator(ctx sdk.Context, valAddr sd
return ubds
}
// iterate through all of the unbonding delegations
func (k Keeper) IterateUnbondingDelegations(ctx sdk.Context, fn func(index int64, ubd types.UnbondingDelegation) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, UnbondingDelegationKey)
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
ubd := types.MustUnmarshalUBD(k.cdc, iterator.Key(), iterator.Value())
stop := fn(i, ubd)
if stop {
break
}
i++
}
iterator.Close()
}
// set the unbonding delegation and associated index
func (k Keeper) SetUnbondingDelegation(ctx sdk.Context, ubd types.UnbondingDelegation) {
store := ctx.KVStore(k.storeKey)

View File

@ -1,6 +1,7 @@
package simulation
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
@ -30,12 +31,17 @@ func SupplyInvariants(ck bank.Keeper, k stake.Keeper, am auth.AccountMapper) sim
ctx := app.NewContext(false, abci.Header{})
pool := k.GetPool(ctx)
// Loose tokens should equal coin supply
// Loose tokens should equal coin supply plus unbonding delegations
loose := sdk.ZeroInt()
am.IterateAccounts(ctx, func(acc auth.Account) bool {
loose = loose.Add(acc.GetCoins().AmountOf("steak"))
return false
})
k.IterateUnbondingDelegations(ctx, func(_ int64, ubd stake.UnbondingDelegation) bool {
fmt.Printf("found ubd with balance: %v\n", ubd.Balance)
loose = loose.Add(ubd.Balance.Amount)
return false
})
require.True(t, pool.LooseTokens.RoundInt64() == loose.Int64(), "expected loose tokens to equal total steak held by accounts - pool.LooseTokens: %v, sum of account tokens: %v\nlog: %s",
pool.LooseTokens, loose, log)
// stats["stake/invariant/looseTokens"] += 1

View File

@ -46,8 +46,7 @@ func TestStakeWithRandomMessages(t *testing.T) {
SimulateMsgCreateValidator(mapper, stakeKeeper),
SimulateMsgEditValidator(stakeKeeper),
SimulateMsgDelegate(mapper, stakeKeeper),
// XXX TODO
// SimulateMsgBeginUnbonding(mapper, stakeKeeper),
SimulateMsgBeginUnbonding(mapper, stakeKeeper),
SimulateMsgCompleteUnbonding(stakeKeeper),
SimulateMsgBeginRedelegate(mapper, stakeKeeper),
SimulateMsgCompleteRedelegate(stakeKeeper),
@ -55,6 +54,6 @@ func TestStakeWithRandomMessages(t *testing.T) {
SimulationSetup(mapp, stakeKeeper),
}, []simulation.Invariant{
AllInvariants(coinKeeper, stakeKeeper, mapp.AccountMapper),
}, 10, 100, 500,
}, 10, 100, 100,
)
}