From 187fe36b54121e78bd61bba9e3eb66672882a6e0 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 27 Oct 2022 12:35:22 +0100 Subject: [PATCH] cp --- tests/difference/core/driver/core_test.go | 67 +++++++++++++++++++---- tests/difference/core/model/src/main.ts | 4 -- tests/difference/core/model/src/model.ts | 2 + testutil/sample/sample.go | 10 ++-- x/ccv/consumer/keeper/relay.go | 1 + x/ccv/consumer/module.go | 1 + x/ccv/provider/keeper/keymap_test.go | 3 +- x/ccv/provider/keeper/relay.go | 10 ++++ 8 files changed, 77 insertions(+), 21 deletions(-) diff --git a/tests/difference/core/driver/core_test.go b/tests/difference/core/driver/core_test.go index 2fd3bf23de..bceae62f3a 100644 --- a/tests/difference/core/driver/core_test.go +++ b/tests/difference/core/driver/core_test.go @@ -22,6 +22,7 @@ import ( "math/rand" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper" testutil "github.com/cosmos/interchain-security/testutil/sample" consumerkeeper "github.com/cosmos/interchain-security/x/ccv/consumer/keeper" @@ -322,20 +323,37 @@ func (s *CoreSuite) matchState() { } func (s *CoreSuite) executeTrace() { + + for i := 0; i < initState.NumValidators; i++ { + cons := s.consAddr(int64(i)) + fmt.Println(i, " cons:", cons.String()[14:20]) + } + for i := range s.traces.Actions() { s.traces.CurrentActionIx = i a := s.traces.Action() - if rand.Intn(100) < 10 { - // TODO: - j := rand.Intn(initState.NumValidators) - pk := s.providerValidatorPubKey(int64(j)) - k := rand.Intn(50) - ck := testutil.GetTMCryptoPublicKeyFromSeed(uint64(k)) - _ = pk - _ = ck - // s.providerKeeper().KeyMap(s.ctx(P), s.chainID(C)).SetProviderPubKeyToConsumerPubKey(pk, ck) + for y := 0; y < rand.Intn(5); y++ { + if rand.Intn(100) < 10 { + j := rand.Intn(initState.NumValidators) + pk := s.providerValidatorPubKey(int64(j)) + k := rand.Intn(50) + privK, ck := testutil.GetTMCryptoPublicKeyFromSeed(uint64(k)) + cki, err := cryptocodec.FromTmProtoPublicKey(ck) + if err != nil { + panic("unexpected err A") + } + cki2, err := cryptocodec.ToTmPubKeyInterface(cki) + if err != nil { + panic("unexpected err B") + } + // signers[validatorKeyData.pubKey.Address().String()] + s.chain(C).Signers[cki2.Address().String()] = privK + _ = pk + _ = ck + s.providerKeeper().KeyMap(s.ctx(P), s.chainID(C)).SetProviderPubKeyToConsumerPubKey(pk, ck) + } } switch a.Kind { @@ -350,6 +368,7 @@ func (s *CoreSuite) executeTrace() { int64(a.Amt), ) case "ConsumerSlash": + // fmt.Printf("trace %d, slash: val %d vscid %d\n", s.traces.CurrentTraceIx, a.Val, a.Vscid) s.consumerSlash( int64(a.Val), uint64(a.Vscid), @@ -403,6 +422,27 @@ func (s *CoreSuite) TestAssumptions() { // Consumer last vscid is correct s.Require().Equal(int(16), int(s.consumerLastCommittedVscId())) + // Check that consumer uses current provider mapping + s.consumerKeeper().IterateValidators(s.ctx(C), func(_ int64, cval stakingtypes.ValidatorI) bool { + cpkActual, err := cval.TmConsPublicKey() + s.Require().NoError(err) + good := false + s.providerStakingKeeper().IterateValidators(s.ctx(P), func(_ int64, pval stakingtypes.ValidatorI) bool { + if pval.GetTokens().Equal(cval.GetTokens()) { + good = true + } + tmkey, err := pval.TmConsPublicKey() + s.Require().NoError(err) + km := s.providerKeeper().KeyMap(s.ctx(P), s.chainID(C)) + cpkFound, found := km.GetCurrentConsumerPubKeyFromProviderPubKey(tmkey) + s.Require().True(found) + s.Require().True(cpkFound.Equal(cpkActual)) + return false + }) + s.Require().True(good) + return false + }) + // Each validator has signing info for i := 0; i < len(initState.ValStates.Tokens); i++ { _, found := s.providerSlashingKeeper().GetValidatorSigningInfo(s.ctx(P), s.consAddr(int64(i))) @@ -508,14 +548,19 @@ func (s *CoreSuite) TestTraces() { s.traces = Traces{ Data: LoadTraces("tracesAlt.json"), } - // s.traces.Data = []TraceData{s.traces.Data[74]} + s.traces.Data = []TraceData{s.traces.Data[211]} for i := range s.traces.Data { s.Run(fmt.Sprintf("Trace num: %d", i), func() { // Setup a new pair of chains for each trace + rand.Seed(2) + fmt.Println("pre setup") + s.SetupTest() + fmt.Println("post setup") + s.actualVscidToMapping = map[uint64]map[int64]providerkeeper.ConsumerPubKey{} s.actualVscidToMapping[s.offsetProviderVscId] = s.buildMapping() - s.actualVscidToMapping[16] = s.buildMapping() + s.actualVscidToMapping[s.offsetProviderVscId] = s.buildMapping() s.traces.CurrentTraceIx = i defer func() { diff --git a/tests/difference/core/model/src/main.ts b/tests/difference/core/model/src/main.ts index 1f40eaf315..96d39fc7ee 100644 --- a/tests/difference/core/model/src/main.ts +++ b/tests/difference/core/model/src/main.ts @@ -377,13 +377,9 @@ function replay(actions: TraceAction[]) { const hist = new BlockHistory(); const events: Event[] = []; const model = new Model(hist, events, MODEL_INIT_STATE); - const actionGenerator = new ActionGenerator(model); for (let i = 0; i < actions.length; i++) { const a = actions[i]; - console.log(a); - actionGenerator.do(a.action); doAction(model, a.action); - bondBasedConsumerVotingPower(hist); } } diff --git a/tests/difference/core/model/src/model.ts b/tests/difference/core/model/src/model.ts index ae6b38836f..d201f9ca6a 100644 --- a/tests/difference/core/model/src/model.ts +++ b/tests/difference/core/model/src/model.ts @@ -438,6 +438,7 @@ class CCVProvider { }; onReceiveSlash = (data: Slash) => { + console.log(`recv slash, `, data); let infractionHeight = undefined; if (data.vscID === 0) { @@ -461,6 +462,7 @@ class CCVProvider { return; } + console.log(`actu slash, `, data); this.m.staking.slash(data.val, infractionHeight); this.m.staking.jailUntil(data.val, this.m.t[P] + JAIL_SECONDS); if (data.isDowntime) { diff --git a/testutil/sample/sample.go b/testutil/sample/sample.go index d4f6e9ad6b..c806a63c96 100644 --- a/testutil/sample/sample.go +++ b/testutil/sample/sample.go @@ -21,24 +21,24 @@ func AccAddress() string { return sdk.AccAddress(addr).String() } -func GetTMCryptoPublicKeyFromSeed(i uint64) tmprotocrypto.PublicKey { +func GetTMCryptoPublicKeyFromSeed(i uint64) (mock.PV, tmprotocrypto.PublicKey) { - fromSeed := func(seed []byte) tmprotocrypto.PublicKey { + fromSeed := func(seed []byte) (mock.PV, tmprotocrypto.PublicKey) { //lint:ignore SA1019 We don't care because this is only a test. privKey := mock.PV{PrivKey: &ed25519.PrivKey{Key: cryptoEd25519.NewKeyFromSeed(seed)}} pubKey, err := privKey.GetPubKey() if err != nil { panic(err) } - sdkVer, err := cryptocodec.FromTmPubKeyInterface(pubKey) + sdkKey, err := cryptocodec.FromTmPubKeyInterface(pubKey) if err != nil { panic(err) } - pk, err := cryptocodec.ToTmProtoPublicKey(sdkVer) + tmProtoKey, err := cryptocodec.ToTmProtoPublicKey(sdkKey) if err != nil { panic(err) } - return pk + return privKey, tmProtoKey } seed := []byte("AAAAAAAAabcdefghijklmnopqrstuvwx") // 8+24 bytes diff --git a/x/ccv/consumer/keeper/relay.go b/x/ccv/consumer/keeper/relay.go index e15343b1bb..4766ac745a 100644 --- a/x/ccv/consumer/keeper/relay.go +++ b/x/ccv/consumer/keeper/relay.go @@ -60,6 +60,7 @@ func (k Keeper) OnRecvVSCPacket(ctx sdk.Context, packet channeltypes.Packet, new } maturityTime := ctx.BlockTime().Add(unbondingPeriod) k.SetPacketMaturityTime(ctx, newChanges.ValsetUpdateId, uint64(maturityTime.UnixNano())) + fmt.Println("consumer receive vscid: ", newChanges.ValsetUpdateId) // set height to VSC id mapping k.SetHeightValsetUpdateID(ctx, uint64(ctx.BlockHeight())+1, newChanges.ValsetUpdateId) diff --git a/x/ccv/consumer/module.go b/x/ccv/consumer/module.go index ae6f6bfda8..c233d8e941 100644 --- a/x/ccv/consumer/module.go +++ b/x/ccv/consumer/module.go @@ -191,6 +191,7 @@ func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.V // apply changes to cross-chain validator set tendermintUpdates := am.keeper.ApplyCCValidatorChanges(ctx, data.ValidatorUpdates) am.keeper.DeletePendingChanges(ctx) + fmt.Println("consumer endblock") return tendermintUpdates } diff --git a/x/ccv/provider/keeper/keymap_test.go b/x/ccv/provider/keeper/keymap_test.go index f18fed6b69..059b4f3be2 100644 --- a/x/ccv/provider/keeper/keymap_test.go +++ b/x/ccv/provider/keeper/keymap_test.go @@ -16,7 +16,8 @@ import ( ) func key(k uint64) crypto.PublicKey { - return testutil.GetTMCryptoPublicKeyFromSeed(k) + _, pubKey := testutil.GetTMCryptoPublicKeyFromSeed(k) + return pubKey } // TODO: all the map lookups are probably gonna fail because the objects are different diff --git a/x/ccv/provider/keeper/relay.go b/x/ccv/provider/keeper/relay.go index 130911c6bd..fb9a5bf54c 100644 --- a/x/ccv/provider/keeper/relay.go +++ b/x/ccv/provider/keeper/relay.go @@ -210,11 +210,17 @@ func GetProviderConsAddr(keymap *KeyMap, consumerConsAddress sdk.ConsAddress) (s return PubKeyToConsAddr(providerPublicKey), nil } +func debugStr(s string, pcons ProviderConsAddr, data ccv.SlashPacketData) { + p := pcons.String()[14:20] + fmt.Println(s, "pcons: ", p, data.ValsetUpdateId, data.Infraction == stakingtypes.Downtime) +} + // HandleSlashPacket slash and jail a misbehaving validator according the infraction type func (k Keeper) HandleSlashPacket(ctx sdk.Context, chainID string, data ccv.SlashPacketData) (success bool, err error) { // map VSC ID to infraction height for the given chain ID var infractionHeight uint64 var found bool + if data.ValsetUpdateId == 0 { infractionHeight, found = k.GetInitChainHeight(ctx, chainID) } else { @@ -229,7 +235,10 @@ func (k Keeper) HandleSlashPacket(ctx sdk.Context, chainID string, data ccv.Slas // TODO: document better consumerConsAddr := sdk.ConsAddress(data.Validator.Address) providerConsAddr, err := GetProviderConsAddr(k.KeyMap(ctx, chainID), consumerConsAddr) + debugStr("recv slash, ", providerConsAddr, data) + if err != nil { + fmt.Println("could not find providerConsAddr using keymap lookup") return false, nil } validator, found := k.stakingKeeper.GetValidatorByConsAddr(ctx, providerConsAddr) @@ -270,6 +279,7 @@ func (k Keeper) HandleSlashPacket(ctx sdk.Context, chainID string, data ccv.Slas default: return false, fmt.Errorf("invalid infraction type: %v", data.Infraction) } + debugStr("actu slash, ", providerConsAddr, data) // slash validator k.stakingKeeper.Slash(