Skip to content

Commit

Permalink
cp
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel committed Oct 27, 2022
1 parent b8fb229 commit 187fe36
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 21 deletions.
67 changes: 56 additions & 11 deletions tests/difference/core/driver/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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),
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 0 additions & 4 deletions tests/difference/core/model/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/difference/core/model/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ class CCVProvider {
};

onReceiveSlash = (data: Slash) => {
console.log(`recv slash, `, data);
let infractionHeight = undefined;

if (data.vscID === 0) {
Expand All @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions testutil/sample/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions x/ccv/consumer/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions x/ccv/consumer/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion x/ccv/provider/keeper/keymap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions x/ccv/provider/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 187fe36

Please sign in to comment.