Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

difftest-core (driver): refactor setup to make it more maintainable #658

Merged
merged 66 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
3df828e
Renames init state -> init state var
Jan 10, 2023
d716ecc
Renames block seconds -> block interval
Jan 10, 2023
3e5cb19
Removes one reference to init state var in setup
Jan 10, 2023
2d46eb8
Adds and inits initState field in core suite
Jan 10, 2023
c6c7895
Uses instance of init state on suite in core_test
Jan 10, 2023
f12c3d6
Removes one hardcoded param in setup
Jan 10, 2023
cb6beab
Removes one hardcoded param
Jan 10, 2023
f053e73
Adds test validator creation util
Jan 10, 2023
50525b8
Deletes one todo
Jan 10, 2023
1e4b437
Use crypto util in create Validators
Jan 10, 2023
513fc39
Uses b.initState.UnbondingC in consumer genesis
Jan 10, 2023
98bd16f
Extract func run some protocol steps
Jan 10, 2023
df30a08
Renames setup util methods
Jan 10, 2023
e520a6c
Removes builder build
Jan 10, 2023
adc2d07
Renames setup method
Jan 10, 2023
693929f
Moves link creation call in setup
Jan 10, 2023
567c6f1
Simplifies creation of new validators in setup
Jan 10, 2023
fcf510b
Replaces some uses of P, C in setup
Jan 10, 2023
2c376a2
Removes many uses of P, C
Jan 10, 2023
0272807
Removes getValidatorPK
Jan 10, 2023
391682f
Moves param related statements
Jan 10, 2023
598a077
Moves slash param
Jan 10, 2023
a53a6cd
Rename method
Jan 10, 2023
b0a026b
Squashed refactors
Jan 11, 2023
1e226c6
Style changes
Jan 11, 2023
348da48
Removes chainID helper
Jan 11, 2023
de59e3b
Move method
Jan 11, 2023
48f65e5
Removes SendEmptyVSC
Jan 11, 2023
2cd4b9e
Style
Jan 11, 2023
55d7d9f
Extract stuff to remove to isolated file
Jan 11, 2023
c5da62b
Moves method
Jan 11, 2023
fdc7f11
Moves createLink
Jan 11, 2023
5fbaa68
Extracts BHelper
Jan 11, 2023
872b361
Squashed commit of the following:
Jan 11, 2023
95c389d
Small QoL changes
Jan 11, 2023
f9d4f35
Factor out dynamic endpoint helper
Jan 11, 2023
3e13c76
renames createChains
Jan 11, 2023
8f8dfec
Factors out params setting method
Jan 11, 2023
d948ef9
Rename path method
Jan 11, 2023
30e1b23
Fix client config line
Jan 11, 2023
0d933f7
Method renames
Jan 11, 2023
272cad0
QoL changes
Jan 11, 2023
de4c01c
Extract setup test
Jan 11, 2023
01d077f
Merge branch 'main' into danwt/difftest-core-setup-refactor
danwt Jan 11, 2023
54b6acb
Squashed commit of the following:
Jan 12, 2023
4a91e6f
Merge branch 'main' into danwt/difftest-core-setup-refactor
danwt Jan 12, 2023
25f62c7
add test
sainoe Jan 5, 2023
3378fee
save
sainoe Jan 17, 2023
1e26dc0
improve naming
sainoe Jan 17, 2023
5654a11
update naming
sainoe Jan 17, 2023
b3f65a0
update test comments
sainoe Jan 18, 2023
7c08b3b
fix bug
sainoe Jan 18, 2023
55f3a80
patch for saino/crypto (#673)
danwt Jan 18, 2023
e28b872
Merge branch 'main' into sainoe/crypto
danwt Jan 18, 2023
df5a370
Merge branch 'main' into danwt/difftest-core-setup-refactor
danwt Jan 18, 2023
f415920
reformat
sainoe Jan 18, 2023
ab5b535
Update testutil/crypto/crypto.go
sainoe Jan 24, 2023
1ae644f
Update x/ccv/provider/keeper/hooks_test.go
sainoe Jan 24, 2023
9a21d69
builds now
shaspitz Jan 24, 2023
7111241
nit
sainoe Jan 25, 2023
a22d85f
Merge branch 'main' into sainoe/crypto
sainoe Jan 25, 2023
3fb3d29
Merge branch 'main' into sainoe/crypto
sainoe Jan 25, 2023
b66e168
add validator logs
sainoe Feb 1, 2023
74a8728
Merge branch 'sainoe/crypto' into danwt/difftest-core-setup-refactor
sainoe Feb 1, 2023
bb5e06d
Merge remote-tracking branch 'upstream/main' into danwt/difftest-core…
sainoe Feb 1, 2023
7c7eaa3
fix cryptolib bug introduced after merging with main
sainoe Feb 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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