Skip to content

Commit

Permalink
difftest-core (driver): refactor setup to make it more maintainable (#…
Browse files Browse the repository at this point in the history
…658)

* Renames init state -> init state var

* Renames block seconds -> block interval

* Removes one reference to init state var in setup

* Adds and inits initState field in core suite

* Uses instance of init state on suite in core_test

* Removes one hardcoded param in setup

* Removes one hardcoded param

* Adds test validator creation util

* Deletes one todo

* Use crypto util in create Validators

* Uses b.initState.UnbondingC in consumer genesis

* Extract func run some protocol steps

* Renames setup util methods

* Removes builder build

* Renames setup method

* Moves link creation call in setup

* Simplifies creation of new validators in setup

* Replaces some uses of P, C in setup

* Removes many uses of P, C

* Removes getValidatorPK

* Moves param related statements

* Moves slash param

* Rename method

* Squashed refactors

commit d1c3ac45289e4e0e0614e4133978f1807401d544
Author: Daniel <[email protected]>
Date:   Wed Jan 11 09:15:17 2023 +0000

    Inlines doStuff

commit 5490df90919f11f0d5cc794fa4b7eadc6ba32343
Author: Daniel <[email protected]>
Date:   Wed Jan 11 09:14:55 2023 +0000

    Deletes unnecesssary line

commit 8b19fcb42e0f235d129c1b63830c562749142238
Author: Daniel <[email protected]>
Date:   Wed Jan 11 09:13:51 2023 +0000

    Extract consumer genesis

commit 4195b858fe5c1cb1c5212a8efbf0cee460cc44a8
Author: Daniel <[email protected]>
Date:   Wed Jan 11 09:10:53 2023 +0000

    Extract createConsumerClientGenesisState

commit 08a4d4804926ac029d35937606c17c9a6fe8eb7c
Author: Daniel <[email protected]>
Date:   Wed Jan 11 09:07:52 2023 +0000

    Amend: move forgotten line

commit c3637354c649c67bf2fb5fca547a01ca5cd5aea9
Author: Daniel <[email protected]>
Date:   Wed Jan 11 09:07:28 2023 +0000

    Extract configureConsumerClientOnProvider

commit 236cf1f0f5eac25607f3354521e474c0bfe98596
Author: Daniel <[email protected]>
Date:   Wed Jan 11 09:06:02 2023 +0000

    Move consumer client init on provider

commit 04a438dbddd403d069eb9b6fc260c021c0d752d9
Author: Daniel <[email protected]>
Date:   Wed Jan 11 08:59:11 2023 +0000

    Move configureIBCTestingPath

commit e977309b4205be6078bf9cde2f48f767928723e6
Author: Daniel <[email protected]>
Date:   Wed Jan 11 08:58:44 2023 +0000

    Extract configureIBCTestingPath

commit 4d8d3e83999486dc1c63fa83fc8667dc24c1ca38
Author: Daniel <[email protected]>
Date:   Tue Jan 10 19:48:07 2023 +0000

    Moves channel, connections HS to setup body

commit 30d583446e0d70b4ef3d1d5521a60105f0500c35
Author: Daniel <[email protected]>
Date:   Tue Jan 10 19:47:03 2023 +0000

    Inlines ibc handshake

commit 0b3d6c4029950e4bc19b871376ae4a0d36d42c87
Author: Daniel <[email protected]>
Date:   Tue Jan 10 19:45:13 2023 +0000

    Inlinen create consumer gen

commit 27949cb4f917ac88c0262b95d1c2a85e072e4d42
Author: Daniel <[email protected]>
Date:   Tue Jan 10 19:44:12 2023 +0000

    Extract doStuff *fix

commit eed615ecd987497dee9a7b452e9507f7cc97263d
Author: Daniel <[email protected]>
Date:   Tue Jan 10 19:43:24 2023 +0000

    Extract doStuff

commit ded978c4270c474c878577267690f04bcc5ab19e
Author: Daniel <[email protected]>
Date:   Tue Jan 10 19:41:48 2023 +0000

    Extracts 1 method

* Style changes

* Removes chainID helper

* Move method

* Removes SendEmptyVSC
Squashed commit of the following:

commit 21bfdab6d3c98932bf89a36a596a89234c46d83b
Author: Daniel <[email protected]>
Date:   Wed Jan 11 11:31:13 2023 +0000

    Gets rid of sendEmpty

commit c7564905073427329114e665d4b6f270e7055960
Author: Daniel <[email protected]>
Date:   Wed Jan 11 09:57:31 2023 +0000

    Adds debug session

* Style

* Extract stuff to remove to isolated file

* Moves method

* Moves createLink

* Extracts BHelper

* Squashed commit of the following:

commit a42c4c08970189d2ddbf9665b911142071a6e57f
Author: Daniel <[email protected]>
Date:   Wed Jan 11 13:32:55 2023 +0000

    Finish offshoot

commit d2d4b22b009bb91302a7cdaf3c86f167a1bd973a
Author: Daniel <[email protected]>
Date:   Wed Jan 11 13:29:54 2023 +0000

    Deletes unused functions

commit 907f791dee9d562e5dbc856494057f2f003ffbf7
Author: Daniel <[email protected]>
Date:   Wed Jan 11 13:29:34 2023 +0000

    Moves extracted to setup

commit 7ad303c5f330dbffa811d3fcab16244aa7639727
Author: Daniel <[email protected]>
Date:   Wed Jan 11 13:26:01 2023 +0000

    Finish refactor

commit f51297d9c3ce6202ef09a76212cf35e3a780237e
Author: Daniel <[email protected]>
Date:   Wed Jan 11 13:19:40 2023 +0000

    Deletes unused code

commit e32acc4146c2f12180c17cf78fecc1df519247fa
Author: Daniel <[email protected]>
Date:   Wed Jan 11 13:04:21 2023 +0000

    Uses chainUtil directly

commit 7ed48fe00c1bf4bf439966fad4d21fd3ef7ebe1d
Author: Daniel <[email protected]>
Date:   Wed Jan 11 12:50:38 2023 +0000

    Moves 1 lien

commit 0b938bbc7ea3780c7e9b2a885eaa6fb58eaec704
Author: Daniel <[email protected]>
Date:   Wed Jan 11 12:49:47 2023 +0000

    Small ref

commit 10d6ada224548e8fb0c3b43418d53a9109215c16
Author: Daniel <[email protected]>
Date:   Wed Jan 11 12:47:37 2023 +0000

    double check all tests

commit 49c5c122e17f441a0dd5be77f8be383dc5d0a09c
Author: Daniel <[email protected]>
Date:   Wed Jan 11 12:46:33 2023 +0000

    Small ref

commit f284a0641118c723c78aaa4670f975ff95a82447
Author: Daniel <[email protected]>
Date:   Wed Jan 11 12:44:13 2023 +0000

    Small inline

commit ebcb021cd7eb47f961b03a6ec4e534702f05108b
Author: Daniel <[email protected]>
Date:   Wed Jan 11 12:38:52 2023 +0000

    Removes mustBegin

commit 036faf330f55fce90ddf3d6bd2091917edc33ee8
Author: Daniel <[email protected]>
Date:   Wed Jan 11 12:33:08 2023 +0000

    Hedge

commit ecdc15811185aedca62fb60ce0046eb6cfb62b58
Author: Daniel <[email protected]>
Date:   Wed Jan 11 12:27:52 2023 +0000

    Confirm a further removal

commit 46b175c2a1103db2fc787e9267f720a0eb8220d7
Author: Daniel <[email protected]>
Date:   Wed Jan 11 12:26:07 2023 +0000

    Confirm further removal

commit cce946d84ee8cd3e4e9b9d4175416611269cb7cc
Author: Daniel <[email protected]>
Date:   Wed Jan 11 12:17:06 2023 +0000

    Removes unused code

commit 3e3e7322f54a1fee45415dd7807adfe345c70b82
Author: Daniel <[email protected]>
Date:   Wed Jan 11 12:15:15 2023 +0000

    Confirm pass with major removal

commit 3798ca67ef5c347d0b0dc4c846e2320e41a1edc5
Author: Daniel <[email protected]>
Date:   Wed Jan 11 12:01:23 2023 +0000

    Prove 0 progression

* Small QoL changes

* Factor out dynamic endpoint helper

* renames createChains

* Factors out params setting method

* Rename path method

* Fix client config line

* Method renames

* QoL changes

* Extract setup test

* Squashed commit of the following:

commit 463ec20
Author: Daniel T <[email protected]>
Date:   Wed Jan 11 18:24:12 2023 +0000

    Difftest core: small PR removes some todos (#656)

    * Removes model TODO

    * Removes 1 TODO

    * Removes 1 TODO

    * Clarify 1 TODO

    Co-authored-by: Daniel <[email protected]>
    Co-authored-by: Shawn <[email protected]>

commit 35f1e26
Author: Shawn <[email protected]>
Date:   Wed Jan 11 09:10:44 2023 -0800

    Removes depreciated keyring dependency  (#657)

    changes

* add test

* save

* improve naming

* update naming

* update test comments

* fix bug

* patch for saino/crypto (#673)

* Use secpk

* Refactor

* Bump

* bump

Co-authored-by: Daniel <[email protected]>

* reformat

* Update testutil/crypto/crypto.go

Co-authored-by: Shawn <[email protected]>

* Update x/ccv/provider/keeper/hooks_test.go

* builds now

* nit

* add validator logs

* fix cryptolib bug introduced after merging with main

---------

Co-authored-by: Daniel <[email protected]>
Co-authored-by: Simon Noetzlin <[email protected]>
Co-authored-by: Shawn <[email protected]>
  • Loading branch information
4 people authored Feb 1, 2023
1 parent e6e26f3 commit b89c46e
Show file tree
Hide file tree
Showing 4 changed files with 348 additions and 507 deletions.
8 changes: 4 additions & 4 deletions tests/difference/core/driver/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ type InitState struct {
UnbondingC time.Duration
Trusting time.Duration
MaxClockDrift time.Duration
BlockSeconds time.Duration
BlockInterval time.Duration
ConsensusParams *abci.ConsensusParams
ValStates ValStates
MaxEntries int
}

var initState InitState
var initStateVar InitState

func init() {
// tokens === power
sdk.DefaultPowerReduction = sdk.NewInt(1)
initState = InitState{
initStateVar = InitState{
PKSeeds: []string{
// Fixed seeds are used to create the private keys for validators.
// The seeds are chosen to ensure that the resulting validators are
Expand All @@ -62,7 +62,7 @@ func init() {
UnbondingC: time.Second * 50,
Trusting: time.Second * 49,
MaxClockDrift: time.Second * 10000,
BlockSeconds: time.Second * 6,
BlockInterval: time.Second * 6,
ValStates: ValStates{
Delegation: []int{4000, 3000, 2000, 1000},
Tokens: []int{5000, 4000, 3000, 2000},
Expand Down
157 changes: 13 additions & 144 deletions tests/difference/core/driver/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package core

import (
"fmt"
"math"
"testing"
"time"

Expand All @@ -27,6 +26,8 @@ import (
type CoreSuite struct {
suite.Suite

initState InitState

// the current traces being executed
traces Traces

Expand Down Expand Up @@ -209,43 +210,43 @@ func (s *CoreSuite) deliver(chain string, numPackets int) {
}

func (s *CoreSuite) endAndBeginBlock(chain string) {
s.simibc.EndAndBeginBlock(s.chainID(chain), initState.BlockSeconds, func() {
s.matchState()
s.simibc.EndAndBeginBlock(s.chainID(chain), s.initState.BlockInterval, func() {
s.compareModelAndSystemState()
})
}

// matchState compares the state in the SUT to the state in the
// compareModelAndSystemState compares the state in the SUT to the state in the
// the model.
func (s *CoreSuite) matchState() {
func (s *CoreSuite) compareModelAndSystemState() {

// Get a diagnostic for debugging
diagnostic := s.traces.Diagnostic()
chain := s.traces.Action().Chain

// Model time, height start at 0 so we need an offset for comparisons.
sutTimeOffset := time.Unix(s.offsetTimeUnix, 0).Add(-initState.BlockSeconds).UTC()
sutTimeOffset := time.Unix(s.offsetTimeUnix, 0).Add(-s.initState.BlockInterval).UTC()
modelTimeOffset := time.Duration(s.traces.Time()) * time.Second
sutHeightOffset := s.offsetHeight - 1
modelHeightOffset := int64(s.traces.Height())
s.Require().Equalf(sutTimeOffset.Add(modelTimeOffset), s.time(chain), diagnostic+"%s Time mismatch", chain)
s.Require().Equalf(sutHeightOffset+modelHeightOffset, s.height(chain), diagnostic+"%s Time mismatch", chain)
if chain == P {
for j := 0; j < initState.NumValidators; j++ {
for j := 0; j < s.initState.NumValidators; j++ {
have := s.validatorStatus(int64(j))
s.Require().Equalf(s.traces.Status(j), have, diagnostic+"P bond status mismatch for val %d, expect %s, have %s", j, s.traces.Status(j).String(), have.String())
}
for j := 0; j < initState.NumValidators; j++ {
for j := 0; j < s.initState.NumValidators; j++ {
s.Require().Equalf(int64(s.traces.Tokens(j)), s.providerTokens(int64(j)), diagnostic+"P tokens mismatch for val %d", j)
}
s.Require().Equalf(int64(s.traces.DelegatorTokens()), s.delegatorBalance(), diagnostic+"P del balance mismatch")
for j := 0; j < initState.NumValidators; j++ {
for j := 0; j < s.initState.NumValidators; j++ {
a := s.traces.Jailed(j) != nil
b := s.isJailed(int64(j))
s.Require().Equalf(a, b, diagnostic+"P jail status mismatch for val %d", j)
}
}
if chain == C {
for j := 0; j < initState.NumValidators; j++ {
for j := 0; j < s.initState.NumValidators; j++ {
exp := s.traces.ConsumerPower(j)
actual, err := s.consumerPower(int64(j))
if exp != nil {
Expand Down Expand Up @@ -296,138 +297,6 @@ func (s *CoreSuite) executeTrace() {
}
}

// TestAssumptions tests that the assumptions used to write the difftest
// driver hold. This test therefore does not test the system, but only that
// the driver is correctly setup.
func (s *CoreSuite) TestAssumptions() {

const FAIL_MSG = "Assumptions for core diff test failed: there is a problem with the driver or how the test is setup."

// Staking module maxValidators param is correct
maxValsE := uint32(initState.MaxValidators)
maxVals := s.providerStakingKeeper().GetParams(s.ctx(P)).MaxValidators

if maxValsE != maxVals {
s.T().Fatal(FAIL_MSG)
}

// TODO: Write a check to make sure that the slash throttle params are set correctly.
// The params should be set such that the slash throttle never kicks in and stop a slash.
// This is because the model assumes that a slash will always be executed, no matter
// how many. This can be achieve by setting the slash factor to e.g. 1.0 and the refresh
// period to 1 block.

// Delegator balance is correct
s.Require().Equal(int64(initState.InitialDelegatorTokens), s.delegatorBalance())

// Slash factors are correct
s.Require().Equal(initState.SlashDowntime, s.providerSlashingKeeper().SlashFractionDowntime(s.ctx(P)))
s.Require().Equal(initState.SlashDoublesign, s.providerSlashingKeeper().SlashFractionDoubleSign(s.ctx(P)))

// Provider unbonding period is correct
stakeParams := s.providerStakingKeeper().GetParams(s.ctx(P))
s.Require().Equal(stakeParams.UnbondingTime, initState.UnbondingP)
// Consumer unbonding period is correct
s.Require().Equal(s.consumerKeeper().UnbondingTime(s.ctx(C)), initState.UnbondingC)

// 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)))
if !found {
s.Require().FailNow(FAIL_MSG)
}
}

// Provider delegations are correct
for i := 0; i < len(initState.ValStates.Delegation); i++ {
E := int64(initState.ValStates.Delegation[i])
A := s.delegation(int64(i))
if E != A {
s.T().Fatal(FAIL_MSG)
}
}

// Provider validator tokens are correct
for i := 0; i < len(initState.ValStates.Tokens); i++ {
E := int64(initState.ValStates.Tokens[i])
A := s.providerTokens(int64(i))
if E != A {
s.T().Fatal(FAIL_MSG)
}
}

// Provider validator status is correct
for i := 0; i < len(initState.ValStates.Status); i++ {
E := initState.ValStates.Status[i]
A := s.validatorStatus(int64(i))
if E != A {
s.T().Fatal(FAIL_MSG)
}
}

// Staking module does not contain undelegations
s.providerStakingKeeper().IterateUnbondingDelegations(s.ctx(P),
func(index int64, ubd stakingtypes.UnbondingDelegation) bool {
s.T().Fatal(FAIL_MSG)
return false // Don't stop
})

// Staking module does contain redelegations
s.providerStakingKeeper().IterateRedelegations(s.ctx(P),
func(index int64, ubd stakingtypes.Redelegation) bool {
s.T().Fatal(FAIL_MSG)
return false // Don't stop
})

// Staking module does not contain unbonding validators
endTime := time.Unix(math.MaxInt64, 0)
endHeight := int64(math.MaxInt64)
unbondingValIterator := s.providerStakingKeeper().ValidatorQueueIterator(s.ctx(P), endTime, endHeight)
defer unbondingValIterator.Close()
for ; unbondingValIterator.Valid(); unbondingValIterator.Next() {
s.T().Fatal(FAIL_MSG)
}

// Consumer has no pending data packets
s.Require().Empty(s.consumerKeeper().GetPendingPackets(s.ctx(C)))

// Consumer has no maturities
for range s.consumerKeeper().GetAllPacketMaturityTimes(s.ctx(C)) {
s.T().Fatal(FAIL_MSG)
}

// Consumer power
for i := 0; i < len(initState.ValStates.Status); i++ {
expectFound := initState.ValStates.Status[i] == stakingtypes.Bonded
expectPower := initState.ValStates.Tokens[i]
addr := s.validator(int64(i))
val, found := s.consumerKeeper().GetCCValidator(s.ctx(C), addr)
s.Require().Equal(expectFound, found)
if expectFound {
if int64(expectPower) != val.Power {
s.T().Fatal(FAIL_MSG)
}
}
}

// The offset time is the last committed time, but the SUT is +1 block ahead
// because the currentHeader time is ahead of the last committed. Therefore sub
// the difference (duration of 1 block).
s.Require().Equal(int64(s.offsetTimeUnix), s.time(P).Add(-initState.BlockSeconds).Unix())
s.Require().Equal(int64(s.offsetTimeUnix), s.time(C).Add(-initState.BlockSeconds).Unix())

// The offset height is the last committed height, but the SUT is +1 because
// the currentHeader is +1 ahead of the last committed. Therefore sub 1.
s.Require().Equal(s.offsetHeight, s.height(P)-1)
s.Require().Equal(s.offsetHeight, s.height(C)-1)

// Network is empty
s.Require().Empty(s.simibc.Link.OutboxPackets[P])
s.Require().Empty(s.simibc.Link.OutboxPackets[C])
s.Require().Empty(s.simibc.Link.OutboxAcks[P])
s.Require().Empty(s.simibc.Link.OutboxAcks[C])
}

// Test a set of traces
func (s *CoreSuite) TestTraces() {
s.traces = Traces{
Expand Down Expand Up @@ -472,8 +341,8 @@ func TestCoreSuite(t *testing.T) {
// SetupTest sets up the test suite in a 'zero' state which matches
// the initial state in the model.
func (s *CoreSuite) SetupTest() {
state := initState
path, valAddresses, offsetHeight, offsetTimeUnix := GetZeroState(&s.Suite, state)
path, valAddresses, offsetHeight, offsetTimeUnix := GetZeroState(&s.Suite, initStateVar)
s.initState = initStateVar
s.valAddresses = valAddresses
s.offsetHeight = offsetHeight
s.offsetTimeUnix = offsetTimeUnix
Expand Down
Loading

0 comments on commit b89c46e

Please sign in to comment.