Skip to content

Commit

Permalink
fix: refactor difftest setup procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
MSalopek committed May 5, 2023
1 parent d09d4d9 commit 99747c3
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 79 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ install: go.sum
export CGO_CPPFLAGS="-D_FORTIFY_SOURCE=2"
export CGO_LDFLAGS="-Wl,-z,relro,-z,now -fstack-protector"
go install $(BUILD_FLAGS) ./cmd/interchain-security-pd
# go install $(BUILD_FLAGS) ./cmd/interchain-security-cd
# go install $(BUILD_FLAGS) ./cmd/interchain-security-cdd
go install $(BUILD_FLAGS) ./cmd/interchain-security-cd
go install $(BUILD_FLAGS) ./cmd/interchain-security-cdd

# run all tests: unit, integration, diff, and E2E
test:
Expand Down
8 changes: 1 addition & 7 deletions app/consumer/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,17 +611,11 @@ func (app *App) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.Respo
func (app *App) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
var genesisState GenesisState
if err := tmjson.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
// fmt.Println("CONSUGEN:// ERRD OND tmjsonUnmarshal genesis", err)
panic(err)
}

app.UpgradeKeeper.SetModuleVersionMap(ctx, app.MM.GetVersionMap())
// // fmt.Println(string(req.AppStateBytes))
// fmt.Println("CONSUGEN:// EXEC INIT GENESIS ###")
// fmt.Println("## CALLING INIT GENESIS from app.go ###")
val := app.MM.InitGenesis(ctx, app.appCodec, genesisState)
// fmt.Println("CONSUGEN:// RAN GENESIS WITH NO ISSUES ###")
return val
return app.MM.InitGenesis(ctx, app.appCodec, genesisState)
}

// LoadHeight loads a particular height
Expand Down
5 changes: 1 addition & 4 deletions app/provider/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,10 +710,7 @@ func (app *App) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.Res

app.UpgradeKeeper.SetModuleVersionMap(ctx, app.MM.GetVersionMap())

vals := app.MM.InitGenesis(ctx, app.appCodec, genesisState)
// fmt.Println("PPPPL//:: REQ ABCI VALSET", req.Validators)
// fmt.Println("PPPPL//:: PROVIDER VALSET UPDATES ###", len(vals.Validators), "\n\n", vals.Validators)
return vals
return app.MM.InitGenesis(ctx, app.appCodec, genesisState)
}

// LoadHeight loads a particular height
Expand Down
21 changes: 0 additions & 21 deletions legacy_ibc_testing/testing/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,6 @@ type TestingApp interface {
func SetupWithGenesisValSet(t *testing.T, appIniter AppIniter, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, chainID string, powerReduction math.Int, balances ...banktypes.Balance) TestingApp {
t.Helper()
app, genesisState := appIniter()
// for k, _ := range genesisState {
// fmt.Println(k, "genesis")
// if k == "ccvconsumer" {
// fmt.Println(string(genesisState[k]))
// }
// }
// fmt.Println("AA::// EXECUTED APP INITER ###")
baseapp.SetChainID(chainID)(app.GetBaseApp())

// set genesis accounts
Expand Down Expand Up @@ -113,7 +106,6 @@ func SetupWithGenesisValSet(t *testing.T, appIniter AppIniter, valSet *tmtypes.V
PubKey: pub.PubKey,
})
}
// fmt.Println("DONE VALS", len(validators), len(delegations))

// set validators and delegations
var (
Expand All @@ -128,14 +120,11 @@ func SetupWithGenesisValSet(t *testing.T, appIniter AppIniter, valSet *tmtypes.V
bondDenom = sdk.DefaultBondDenom
}

// fmt.Println("DONE STAKING")

// add bonded amount to bonded pool module account
balances = append(balances, banktypes.Balance{
Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(),
Coins: sdk.Coins{sdk.NewCoin(bondDenom, bondAmt.Mul(sdk.NewInt(int64(len(valSet.Validators)))))},
})
// fmt.Println("DONE balances", len(validators), len(delegations))

// set validators and delegations
stakingGenesis = *stakingtypes.NewGenesisState(stakingGenesis.Params, validators, delegations)
Expand All @@ -148,19 +137,13 @@ func SetupWithGenesisValSet(t *testing.T, appIniter AppIniter, valSet *tmtypes.V
if genesisState[consumertypes.ModuleName] != nil {
app.AppCodec().MustUnmarshalJSON(genesisState[consumertypes.ModuleName], &consumerGenesis)
consumerGenesis.InitialValSet = initValPowers
// consumerGenesis.NewChain = true
consumerGenesis.Params.Enabled = true
// fmt.Println("##", len(consumerGenesis.InitialValSet))
genesisState[consumertypes.ModuleName] = app.AppCodec().MustMarshalJSON(&consumerGenesis)
}

stateBytes, err := json.MarshalIndent(genesisState, "", " ")
// fmt.Println(string(stateBytes))
require.NoError(t, err)
// fmt.Println("DONE STATE BYTES")

// fmt.Println("TRY INIT")
// // fmt.Println("## STATE ##\n\n", string(stateBytes))
// init chain will set the validator set and initialize the genesis accounts
app.InitChain(
abci.RequestInitChain{
Expand All @@ -170,11 +153,9 @@ func SetupWithGenesisValSet(t *testing.T, appIniter AppIniter, valSet *tmtypes.V
AppStateBytes: stateBytes,
},
)
// fmt.Println("DONE INIT")

// commit genesis changes
app.Commit()
// fmt.Println("DONE COMMIT")

app.BeginBlock(
abci.RequestBeginBlock{
Expand All @@ -188,7 +169,5 @@ func SetupWithGenesisValSet(t *testing.T, appIniter AppIniter, valSet *tmtypes.V
},
)

// fmt.Printf("$$$$$$$$$$$$$$$$$$$ done on %v\n\n", chainID)

return app
}
10 changes: 0 additions & 10 deletions legacy_ibc_testing/testing/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ func NewTestChainWithValSet(t *testing.T, coord *Coordinator, appIniter AppInite
genBals := []banktypes.Balance{}
senderAccs := []SenderAccount{}

// fmt.Println("GGG://: INIT NewTestChainWithValSet")
// fmt.Println("NewTestChainWithValSet called", chainID)

// generate genesis accounts
for i := 0; i < MaxAccounts; i++ {
senderPrivKey := secp256k1.GenPrivKey()
Expand All @@ -136,22 +133,17 @@ func NewTestChainWithValSet(t *testing.T, coord *Coordinator, appIniter AppInite

senderAccs = append(senderAccs, senderAcc)
}
// fmt.Println("GGG://: GEN ACCS", len(genAccs), len(genBals), len(senderAccs))
// fmt.Println("GGG://: ABOUT TO RUN SetupWithGenesisValSet")

app := SetupWithGenesisValSet(t, appIniter, valSet, genAccs, chainID, sdk.DefaultPowerReduction, genBals...)
// fmt.Println("GGG://: DONE SetupWithGenesisValSet")

// create current header and call begin block
header := tmproto.Header{
ChainID: chainID,
Height: 1,
Time: coord.CurrentTime.UTC(),
}
// fmt.Println("GGG://: WROTE HEADER")

txConfig := app.GetTxConfig()
// fmt.Println("GGG://: GOT TX CONFIG")

// create an account to send transactions from
chain := &TestChain{
Expand All @@ -171,10 +163,8 @@ func NewTestChainWithValSet(t *testing.T, coord *Coordinator, appIniter AppInite
SenderAccount: senderAccs[0].SenderAccount,
SenderAccounts: senderAccs,
}
// fmt.Println("GGG://: HAVE TEST CHAIN")

coord.CommitBlock(chain)
// fmt.Println("GGG://: COMMITED BLOCK")

return chain
}
Expand Down
2 changes: 1 addition & 1 deletion tests/difference/core/driver/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (s *CoreSuite) consumerSlash(val sdk.ConsAddress, h int64, isDowntime bool)
}
ctx := s.ctx(C)
before := len(ctx.EventManager().Events())
s.consumerKeeper().Slash(ctx, val, h, 0, sdk.Dec{}, kind)
s.consumerKeeper().SlashWithInfractionReason(ctx, val, h, 0, sdk.Dec{}, kind)
// consumer module emits packets on slash, so these must be collected.
evts := ctx.EventManager().ABCIEvents()
for _, e := range evts[before:] {
Expand Down
16 changes: 16 additions & 0 deletions tests/difference/core/driver/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func (b *Builder) getAppBytesAndSenders(

// Sum bonded is needed for BondedPool account
sumBonded := sdk.NewInt(0)
initValPowers := []abci.ValidatorUpdate{}

for i, val := range validators.Validators {
status := b.initState.ValStates.Status[i]
Expand Down Expand Up @@ -196,17 +197,32 @@ func (b *Builder) getAppBytesAndSenders(
delegations = append(delegations, stakingtypes.NewDelegation(accounts[0].GetAddress(), val.Address.Bytes(), delShares))
// Remaining delegation is from extra account
delegations = append(delegations, stakingtypes.NewDelegation(accounts[1].GetAddress(), val.Address.Bytes(), sumShares.Sub(delShares)))

// add initial validator powers so consumer InitGenesis runs correctly
pub, _ := val.ToProto()
initValPowers = append(initValPowers, abci.ValidatorUpdate{
Power: val.VotingPower,
PubKey: pub.PubKey,
})
}

bondDenom := sdk.DefaultBondDenom
genesisStaking := stakingtypes.GenesisState{}
genesisConsumer := consumertypes.GenesisState{}

if genesis[stakingtypes.ModuleName] != nil {
// If staking module genesis already exists
app.AppCodec().MustUnmarshalJSON(genesis[stakingtypes.ModuleName], &genesisStaking)
bondDenom = genesisStaking.Params.BondDenom
}

if genesis[consumertypes.ModuleName] != nil {
app.AppCodec().MustUnmarshalJSON(genesis[consumertypes.ModuleName], &genesisConsumer)
genesisConsumer.InitialValSet = initValPowers
genesisConsumer.Params.Enabled = true
genesis[consumertypes.ModuleName] = app.AppCodec().MustMarshalJSON(&genesisConsumer)
}

// Set model parameters
genesisStaking.Params.MaxEntries = uint32(b.initState.MaxEntries)
genesisStaking.Params.MaxValidators = uint32(b.initState.MaxValidators)
Expand Down
5 changes: 0 additions & 5 deletions tests/integration/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,7 @@ func (suite *CCVTestSuite) SetupTest() {
suite.consumerBundles = make(map[string]*icstestingutils.ConsumerBundle)
for i := 0; i < numConsumers; i++ {
bundle := suite.setupConsumerCallback(&suite.Suite, suite.coordinator, i)
// fmt.Println("setupConsumerCallback done for bundle")
suite.consumerBundles[bundle.Chain.ChainID] = bundle
// TODO: remove break; this will make the tests initialize a single consumer to ease debugging
// break
}

// initialize each consumer chain with it's corresponding genesis state
Expand Down Expand Up @@ -157,7 +154,6 @@ func initConsumerChain(
chainID string,
genesisState *consumertypes.GenesisState,
) {
// fmt.Println("-----> initConsumerChain <-----")
providerKeeper := s.providerApp.GetProviderKeeper()
bundle := s.consumerBundles[chainID]

Expand All @@ -170,7 +166,6 @@ func initConsumerChain(

// confirm client and cons state for consumer were set correctly in InitGenesis;
// NOTE: on restart, both ProviderClientState and ProviderConsensusState are nil
// fmt.Println("?? new chain:", chainID, genesisState.NewChain, len(genesisState.InitialValSet))
if genesisState.NewChain {
consumerEndpointClientState,
consumerEndpointConsState := s.GetConsumerEndpointClientAndConsState(*bundle)
Expand Down
13 changes: 0 additions & 13 deletions testutil/ibc_testing/generic_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,15 @@ func AddConsumer[Tp testutil.ProviderApp, Tc testutil.ConsumerApp](
index int,
appIniter ibctesting.AppIniter,
) *ConsumerBundle {
// fmt.Println("ADD_CONSU:// ### RUNNING ADD CONSUMER ###")
// consumer chain ID
chainID := ibctesting.GetChainID(index + 2)
// fmt.Println("AddConsumer WAS CALLED", chainID)

// create client to the consumer on the provider chain
providerChain := coordinator.Chains[provChainID]
providerApp := providerChain.App.(Tp)
providerKeeper := providerApp.GetProviderKeeper()
// fmt.Println("ADD_CONSU:// ### GOT PROVI ###")

prop := testkeeper.GetTestConsumerAdditionProp()
// fmt.Println("ADD_CONSU:// ### MADE PROP ###")
prop.ChainId = chainID
// NOTE: the initial height passed to CreateConsumerClient
// must be the height on the consumer when InitGenesis is called
Expand All @@ -112,9 +108,7 @@ func AddConsumer[Tp testutil.ProviderApp, Tc testutil.ConsumerApp](
prop,
)
s.Require().NoError(err)
// fmt.Println("ADD_CONSU:// ### ADDED CLIENTS ###")

// fmt.Println("ADD_CONSU:// ### TRY GENESIS ###")
// commit the state on the provider chain
coordinator.CommitBlock(providerChain)

Expand All @@ -124,9 +118,7 @@ func AddConsumer[Tp testutil.ProviderApp, Tc testutil.ConsumerApp](
chainID,
)
s.Require().True(found, "consumer genesis not found")
// fmt.Println("ADD_CONSU:// ### DONE GENESIS ###")

// fmt.Println("ADD_CONSU:// ### TRY LOOP VALIDATORS ###")
// use InitialValSet as the valset on the consumer
var valz []*tmtypes.Validator
for _, update := range consumerGenesisState.InitialValSet {
Expand All @@ -140,17 +132,12 @@ func AddConsumer[Tp testutil.ProviderApp, Tc testutil.ConsumerApp](
ProposerPriority: 0,
})
}
// fmt.Println("AddConsumer initial valset", len(valz))
// fmt.Println("ADD_CONSU:// ### DONE LOOP VALIDATORS ###", len(valz))

// fmt.Println("ADD_CONSU:// ### ABOUT TO RUN NewTestChainWithValSet ###")
// create and instantiate consumer chain
// fmt.Println("createConsu", len(valz))
testChain := ibctesting.NewTestChainWithValSet(s.T(), coordinator,
appIniter, chainID, tmtypes.NewValidatorSet(valz), providerChain.Signers)
coordinator.Chains[chainID] = testChain

// fmt.Println("ADD_CONSU:// ### NewTestChainWithValSet DONE ###")
consumerToReturn, ok := testChain.App.(Tc)
if !ok {
panic(fmt.Sprintf("consumer app type returned from app initer does not match app type passed in as type param: %T, %T",
Expand Down
3 changes: 0 additions & 3 deletions testutil/keeper/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions x/ccv/consumer/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ import (
// 2. A consumer chain restarts after a client to the provider was created, but the CCV channel handshake is still in progress
// 3. A consumer chain restarts after the CCV channel handshake was completed.
func (k Keeper) InitGenesis(ctx sdk.Context, state *consumertypes.GenesisState) []abci.ValidatorUpdate {
// fmt.Println("### MAIN CONSUMER GENESIS ###", state.PreCCV, state.NewChain, len(state.InitialValSet))
// PreCCV is true during the process of a standalone to consumer changeover.
// At the PreCCV point in the process, the standalone chain has just been upgraded to include
// the consumer ccv module, but the standalone staking keeper is still managing the validator set.
// Once the provider validator set starts validating blocks, the consumer CCV module
// will take over proof of stake capabilities, but the standalone staking keeper will
// stick around for slashing/jailing purposes.
// fmt.Println("Consumer.InitGenesis called", state.NewChain, len(state.InitialValSet))
if state.PreCCV {
k.SetPreCCVTrue(ctx)
k.MarkAsPrevStandaloneChain(ctx)
Expand All @@ -36,7 +34,6 @@ func (k Keeper) InitGenesis(ctx sdk.Context, state *consumertypes.GenesisState)
// TODO: Remove enabled flag and find a better way to setup integration tests
// See: https://github.com/cosmos/interchain-security/issues/339
if !state.Params.Enabled {
// fmt.Println("### SET PARAMS NOT ENABLED")
return nil
}

Expand All @@ -56,14 +53,10 @@ func (k Keeper) InitGenesis(ctx sdk.Context, state *consumertypes.GenesisState)

// initialValSet is checked in NewChain case by ValidateGenesis
// start a new chain
// fmt.Println("!!! new chain on first call:", state.NewChain, len(state.InitialValSet))
if state.NewChain {
// fmt.Println("## Before Create Client ")
// create the provider client in InitGenesis for new consumer chain. CCV Handshake must be established with this client id.
clientID, err := k.clientKeeper.CreateClient(ctx, state.ProviderClientState, state.ProviderConsensusState)
// fmt.Println("## After Create Client ")
if err != nil {
// fmt.Println("## PANIC IN InitGenesis ")
// If the client creation fails, the chain MUST NOT start
panic(err)
}
Expand Down Expand Up @@ -110,13 +103,11 @@ func (k Keeper) InitGenesis(ctx sdk.Context, state *consumertypes.GenesisState)
}

if state.PreCCV {
// fmt.Println("PRE CCV")
return []abci.ValidatorUpdate{}
}

// populate cross chain validators states with initial valset
k.ApplyCCValidatorChanges(ctx, state.InitialValSet)
// fmt.Println("## Consumer.InitGenesis INIT VALSET ###", len(state.InitialValSet))
return state.InitialValSet
}

Expand Down
3 changes: 0 additions & 3 deletions x/ccv/consumer/keeper/validators.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper

import (
"fmt"
"time"

"cosmossdk.io/math"
Expand Down Expand Up @@ -115,15 +114,13 @@ func (k Keeper) Slash(ctx sdk.Context, addr sdk.ConsAddress, infractionHeight, p
// All queued slashing requests will be cleared in EndBlock
// Called by Slashing keeper in SlashWithInfractionReason
func (k Keeper) SlashWithInfractionReason(ctx sdk.Context, addr sdk.ConsAddress, infractionHeight, power int64, slashFactor sdk.Dec, infraction stakingtypes.Infraction) math.Int {
// fmt.Println("SLASHING", infraction)
if infraction == stakingtypes.Infraction_INFRACTION_UNSPECIFIED {
return math.NewInt(0)
}

// If this is a previously standalone chain and infraction happened before the changeover was completed,
// slash only on the standalone staking keeper.
if k.IsPrevStandaloneChain(ctx) && infractionHeight < k.FirstConsumerHeight(ctx) {
fmt.Println("HAVE STANDALONE WITH INFRACTION", infraction)
// NOTE: I'm not sure this code is 100% correct and it's relatively newly implemented
// That is bothering me is that we call SlashWithInfractionReason without an infraction reason every time
return k.standaloneStakingKeeper.SlashWithInfractionReason(ctx, addr, infractionHeight, power, slashFactor, stakingtypes.Infraction_INFRACTION_UNSPECIFIED)
Expand Down
Loading

0 comments on commit 99747c3

Please sign in to comment.