From ee8bcbdaa5b507a4fc3bd9258fb32ae0f97d4d46 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Wed, 7 Nov 2018 14:10:20 +0100 Subject: [PATCH 01/18] PENDING => CHANGELOG --- CHANGELOG.md | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ PENDING.md | 27 +------------------------- 2 files changed, 56 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16e01163cfb6..9145933256a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,60 @@ # Changelog +## 0.26.0 + +BREAKING CHANGES + +* Gaia + * [gaiad init] \#2602 New genesis workflow + +* SDK + * [simulation] \#2665 only argument to simulation.Invariant is now app + +* Tendermint + * Upgrade to version 0.26.0 + +FEATURES + +* Gaia CLI (`gaiacli`) + * [cli] [\#2569](https://github.com/cosmos/cosmos-sdk/pull/2569) Add commands to query validator unbondings and redelegations + * [cli] [\#2569](https://github.com/cosmos/cosmos-sdk/pull/2569) Add commands to query validator unbondings and redelegations + * [cli] [\#2524](https://github.com/cosmos/cosmos-sdk/issues/2524) Add support offline mode to `gaiacli tx sign`. Lookups are not performed if the flag `--offline` is on. + * [cli] [\#2558](https://github.com/cosmos/cosmos-sdk/issues/2558) Rename --print-sigs to --validate-signatures. It now performs a complete set of sanity checks and reports to the user. Also added --print-signature-only to print the signature only, not the whole transaction. + +* SDK + * \#1336 Mechanism for SDK Users to configure their own Bech32 prefixes instead of using the default cosmos prefixes. + +IMPROVEMENTS + +* Gaia + * \#2637 [x/gov] Switched inactive and active proposal queues to an iterator based queue + +* SDK + * \#2573 [x/distribution] add accum invariance + * \#2556 [x/mock/simulation] Fix debugging output + * \#2396 [x/mock/simulation] Change parameters to get more slashes + * \#2617 [x/mock/simulation] Randomize all genesis parameters + * \#2669 [x/stake] Added invarant check to make sure validator's power aligns with its spot in the power store. + * \#1924 [x/mock/simulation] Use a transition matrix for block size + * \#2660 [x/mock/simulation] Staking transactions get tested far more frequently + * \#2610 [x/stake] Block redelegation to and from the same validator + * \#2652 [x/auth] Add benchmark for get and set account + * \#2685 [store] Add general merkle absence proof (also for empty substores) + * \#2708 [store] Disallow setting nil values + +BUG FIXES + +* Gaia + * \#2670 [x/stake] fixed incorrect `IterateBondedValidators` and split into two functions: `IterateBondedValidators` and `IterateLastBlockConsValidators` + * \#2691 Fix local testnet creation by using a single canonical genesis time + +* SDK + * \#2625 [x/gov] fix AppendTag function usage error + * \#2677 [x/stake, x/distribution] various staking/distribution fixes as found by the simulator + * \#2674 [types] Fix coin.IsLT() impl, coins.IsLT() impl, and renamed coins.Is\* to coins.IsAll\* (see \#2686) + * \#2711 [x/stake] Add commission data to `MsgCreateValidator` signature bytes. + + ## 0.25.0 *October 24th, 2018* diff --git a/PENDING.md b/PENDING.md index 8b2044ea29ee..4d87301f0d5b 100644 --- a/PENDING.md +++ b/PENDING.md @@ -7,28 +7,21 @@ BREAKING CHANGES * Gaia CLI (`gaiacli`) * Gaia - * [gaiad init] \#2602 New genesis workflow * SDK - * [simulation] \#2665 only argument to simulation.Invariant is now app * Tendermint - * Upgrade to version 0.26.0 + FEATURES * Gaia REST API (`gaiacli advanced rest-server`) * Gaia CLI (`gaiacli`) - * [cli] [\#2569](https://github.com/cosmos/cosmos-sdk/pull/2569) Add commands to query validator unbondings and redelegations - * [cli] [\#2569](https://github.com/cosmos/cosmos-sdk/pull/2569) Add commands to query validator unbondings and redelegations - * [cli] [\#2524](https://github.com/cosmos/cosmos-sdk/issues/2524) Add support offline mode to `gaiacli tx sign`. Lookups are not performed if the flag `--offline` is on. - * [cli] [\#2558](https://github.com/cosmos/cosmos-sdk/issues/2558) Rename --print-sigs to --validate-signatures. It now performs a complete set of sanity checks and reports to the user. Also added --print-signature-only to print the signature only, not the whole transaction. * Gaia * SDK - * (#1336) Mechanism for SDK Users to configure their own Bech32 prefixes instead of using the default cosmos prefixes. * Tendermint @@ -40,20 +33,8 @@ IMPROVEMENTS * Gaia CLI (`gaiacli`) * Gaia - - #2637 [x/gov] Switched inactive and active proposal queues to an iterator based queue * SDK - - \#2573 [x/distribution] add accum invariance - - \#2556 [x/mock/simulation] Fix debugging output - - \#2396 [x/mock/simulation] Change parameters to get more slashes - - \#2617 [x/mock/simulation] Randomize all genesis parameters - - \#2669 [x/stake] Added invarant check to make sure validator's power aligns with its spot in the power store. - - \#1924 [x/mock/simulation] Use a transition matrix for block size - - \#2660 [x/mock/simulation] Staking transactions get tested far more frequently - - \#2610 [x/stake] Block redelegation to and from the same validator - - \#2652 [x/auth] Add benchmark for get and set account - - \#2685 [store] Add general merkle absence proof (also for empty substores) - - \#2708 [store] Disallow setting nil values * Tendermint @@ -65,13 +46,7 @@ BUG FIXES * Gaia CLI (`gaiacli`) * Gaia - - \#2670 [x/stake] fixed incorrect `IterateBondedValidators` and split into two functions: `IterateBondedValidators` and `IterateLastBlockConsValidators` - - \#2691 Fix local testnet creation by using a single canonical genesis time * SDK - - \#2625 [x/gov] fix AppendTag function usage error - - \#2677 [x/stake, x/distribution] various staking/distribution fixes as found by the simulator - - \#2674 [types] Fix coin.IsLT() impl, coins.IsLT() impl, and renamed coins.Is\* to coins.IsAll\* (see \#2686) - - \#2711 [x/stake] Add commission data to `MsgCreateValidator` signature bytes. * Tendermint From ccb329b353fde34907f09bde21382f3a9d39ce33 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Wed, 7 Nov 2018 14:13:48 +0100 Subject: [PATCH 02/18] Linkify changelog --- CHANGELOG.md | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9145933256a1..5dc577c8a933 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,10 +5,10 @@ BREAKING CHANGES * Gaia - * [gaiad init] \#2602 New genesis workflow + * [gaiad init] [\#2602](https://github.com/cosmos/cosmos-sdk/issues/2602) New genesis workflow * SDK - * [simulation] \#2665 only argument to simulation.Invariant is now app + * [simulation] [\#2665](https://github.com/cosmos/cosmos-sdk/issues/2665) only argument to simulation.Invariant is now app * Tendermint * Upgrade to version 0.26.0 @@ -22,37 +22,37 @@ FEATURES * [cli] [\#2558](https://github.com/cosmos/cosmos-sdk/issues/2558) Rename --print-sigs to --validate-signatures. It now performs a complete set of sanity checks and reports to the user. Also added --print-signature-only to print the signature only, not the whole transaction. * SDK - * \#1336 Mechanism for SDK Users to configure their own Bech32 prefixes instead of using the default cosmos prefixes. + * [\#1336](https://github.com/cosmos/cosmos-sdk/issues/1336) Mechanism for SDK Users to configure their own Bech32 prefixes instead of using the default cosmos prefixes. IMPROVEMENTS * Gaia - * \#2637 [x/gov] Switched inactive and active proposal queues to an iterator based queue + * [\#2637](https://github.com/cosmos/cosmos-sdk/issues/2637) [x/gov] Switched inactive and active proposal queues to an iterator based queue * SDK - * \#2573 [x/distribution] add accum invariance - * \#2556 [x/mock/simulation] Fix debugging output - * \#2396 [x/mock/simulation] Change parameters to get more slashes - * \#2617 [x/mock/simulation] Randomize all genesis parameters - * \#2669 [x/stake] Added invarant check to make sure validator's power aligns with its spot in the power store. - * \#1924 [x/mock/simulation] Use a transition matrix for block size - * \#2660 [x/mock/simulation] Staking transactions get tested far more frequently - * \#2610 [x/stake] Block redelegation to and from the same validator - * \#2652 [x/auth] Add benchmark for get and set account - * \#2685 [store] Add general merkle absence proof (also for empty substores) - * \#2708 [store] Disallow setting nil values + * [\#2573](https://github.com/cosmos/cosmos-sdk/issues/2573) [x/distribution] add accum invariance + * [\#2556](https://github.com/cosmos/cosmos-sdk/issues/2556) [x/mock/simulation] Fix debugging output + * [\#2396](https://github.com/cosmos/cosmos-sdk/issues/2396) [x/mock/simulation] Change parameters to get more slashes + * [\#2617](https://github.com/cosmos/cosmos-sdk/issues/2617) [x/mock/simulation] Randomize all genesis parameters + * [\#2669](https://github.com/cosmos/cosmos-sdk/issues/2669) [x/stake] Added invarant check to make sure validator's power aligns with its spot in the power store. + * [\#1924](https://github.com/cosmos/cosmos-sdk/issues/1924) [x/mock/simulation] Use a transition matrix for block size + * [\#2660](https://github.com/cosmos/cosmos-sdk/issues/2660) [x/mock/simulation] Staking transactions get tested far more frequently + * [\#2610](https://github.com/cosmos/cosmos-sdk/issues/2610) [x/stake] Block redelegation to and from the same validator + * [\#2652](https://github.com/cosmos/cosmos-sdk/issues/2652) [x/auth] Add benchmark for get and set account + * [\#2685](https://github.com/cosmos/cosmos-sdk/issues/2685) [store] Add general merkle absence proof (also for empty substores) + * [\#2708](https://github.com/cosmos/cosmos-sdk/issues/2708) [store] Disallow setting nil values BUG FIXES * Gaia - * \#2670 [x/stake] fixed incorrect `IterateBondedValidators` and split into two functions: `IterateBondedValidators` and `IterateLastBlockConsValidators` - * \#2691 Fix local testnet creation by using a single canonical genesis time + * [\#2670](https://github.com/cosmos/cosmos-sdk/issues/2670) [x/stake] fixed incorrect `IterateBondedValidators` and split into two functions: `IterateBondedValidators` and `IterateLastBlockConsValidators` + * [\#2691](https://github.com/cosmos/cosmos-sdk/issues/2691) Fix local testnet creation by using a single canonical genesis time * SDK - * \#2625 [x/gov] fix AppendTag function usage error - * \#2677 [x/stake, x/distribution] various staking/distribution fixes as found by the simulator - * \#2674 [types] Fix coin.IsLT() impl, coins.IsLT() impl, and renamed coins.Is\* to coins.IsAll\* (see \#2686) - * \#2711 [x/stake] Add commission data to `MsgCreateValidator` signature bytes. + * [\#2625](https://github.com/cosmos/cosmos-sdk/issues/2625) [x/gov] fix AppendTag function usage error + * [\#2677](https://github.com/cosmos/cosmos-sdk/issues/2677) [x/stake, x/distribution] various staking/distribution fixes as found by the simulator + * [\#2674](https://github.com/cosmos/cosmos-sdk/issues/2674) [types] Fix coin.IsLT() impl, coins.IsLT() impl, and renamed coins.Is\* to coins.IsAll\* (see [\#2686](https://github.com/cosmos/cosmos-sdk/issues/2686)) + * [\#2711](https://github.com/cosmos/cosmos-sdk/issues/2711) [x/stake] Add commission data to `MsgCreateValidator` signature bytes. ## 0.25.0 From 74b2a90087b21027d745989bbf49c57b20217251 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 7 Nov 2018 10:28:18 -0500 Subject: [PATCH 03/18] whitespacing --- x/mock/simulation/doc.go | 33 +++--- x/mock/simulation/params.go | 7 +- x/mock/simulation/random_simulate_blocks.go | 100 +++++++++++------ x/mock/simulation/transition_matrix.go | 20 ++-- x/mock/simulation/types.go | 116 ++++++++++---------- x/mock/simulation/util.go | 10 +- 6 files changed, 164 insertions(+), 122 deletions(-) diff --git a/x/mock/simulation/doc.go b/x/mock/simulation/doc.go index 8b9a3f6932de..2febd9e4719d 100644 --- a/x/mock/simulation/doc.go +++ b/x/mock/simulation/doc.go @@ -2,26 +2,25 @@ Package simulation implements a simulation framework for any state machine built on the SDK which utilizes auth. -It is primarily intended for fuzz testing the integration of modules. -It will test that the provided operations are interoperable, -and that the desired invariants hold. -It can additionally be used to detect what the performance benchmarks in the -system are, by using benchmarking mode and cpu / mem profiling. -If it detects a failure, it provides the entire log of what was ran, +It is primarily intended for fuzz testing the integration of modules. It will +test that the provided operations are interoperable, and that the desired +invariants hold. It can additionally be used to detect what the performance +benchmarks in the system are, by using benchmarking mode and cpu / mem +profiling. If it detects a failure, it provides the entire log of what was +ran, -The simulator takes as input: a random seed, the set of operations to run, -the invariants to test, and additional parameters to configure how long to run, -and misc. parameters that affect simulation speed. +The simulator takes as input: a random seed, the set of operations to run, the +invariants to test, and additional parameters to configure how long to run, and +misc. parameters that affect simulation speed. -It is intended that every module provides a list of Operations which will randomly -create and run a message / tx in a manner that is interesting to fuzz, and verify that -the state transition was executed as expected. -Each module should additionally provide methods to assert that the desired invariants hold. +It is intended that every module provides a list of Operations which will +randomly create and run a message / tx in a manner that is interesting to fuzz, +and verify that the state transition was executed as expected. Each module +should additionally provide methods to assert that the desired invariants hold. Then to perform a randomized simulation, select the set of desired operations, -the weightings for each, the invariants you want to test, and how long to run it for. -Then run simulation.Simulate! -The simulator will handle things like ensuring that validators periodically double signing, -or go offline. +the weightings for each, the invariants you want to test, and how long to run +it for. Then run simulation.Simulate! The simulator will handle things like +ensuring that validators periodically double signing, or go offline. */ package simulation diff --git a/x/mock/simulation/params.go b/x/mock/simulation/params.go index 404a85e5441a..8499e6c1189b 100644 --- a/x/mock/simulation/params.go +++ b/x/mock/simulation/params.go @@ -15,15 +15,18 @@ const ( onOperation bool = false ) +// TODO explain transitional matrix usage var ( - // Currently there are 3 different liveness types, fully online, spotty connection, offline. + // Currently there are 3 different liveness types, + // fully online, spotty connection, offline. defaultLivenessTransitionMatrix, _ = CreateTransitionMatrix([][]int{ {90, 20, 1}, {10, 50, 5}, {0, 10, 1000}, }) - // 3 states: rand in range [0, 4*provided blocksize], rand in range [0, 2 * provided blocksize], 0 + // 3 states: rand in range [0, 4*provided blocksize], + // rand in range [0, 2 * provided blocksize], 0 defaultBlockSizeTransitionMatrix, _ = CreateTransitionMatrix([][]int{ {85, 5, 0}, {15, 92, 1}, diff --git a/x/mock/simulation/random_simulate_blocks.go b/x/mock/simulation/random_simulate_blocks.go index a568997e629f..17a26f762647 100644 --- a/x/mock/simulation/random_simulate_blocks.go +++ b/x/mock/simulation/random_simulate_blocks.go @@ -31,8 +31,11 @@ func Simulate(t *testing.T, app *baseapp.BaseApp, return SimulateFromSeed(t, app, appStateFn, time, ops, setups, invariants, numBlocks, blockSize, commit) } -func initChain(r *rand.Rand, params Params, accounts []Account, setups []RandSetup, app *baseapp.BaseApp, - appStateFn func(r *rand.Rand, accounts []Account) json.RawMessage) (validators map[string]mockValidator) { +func initChain(r *rand.Rand, params Params, + accounts []Account, setups []RandSetup, app *baseapp.BaseApp, + appStateFn func(r *rand.Rand, accounts []Account) json.RawMessage) ( + validators map[string]mockValidator) { + res := app.InitChain(abci.RequestInitChain{AppStateBytes: appStateFn(r, accounts)}) validators = make(map[string]mockValidator) for _, validator := range res.Validators { @@ -101,6 +104,7 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, var pastVoteInfos [][]abci.VoteInfo request := RandomRequestBeginBlock(r, params, validators, pastTimes, pastVoteInfos, event, header) + // These are operations which have been queued by previous operations operationQueue := make(map[int][]Operation) timeOperationQueue := []FutureOperation{} @@ -110,7 +114,11 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, blockLogBuilders = make([]*strings.Builder, numBlocks) } displayLogs := logPrinter(testingMode, blockLogBuilders) - blockSimulator := createBlockSimulator(testingMode, tb, t, params, event, invariants, ops, operationQueue, timeOperationQueue, numBlocks, blockSize, displayLogs) + blockSimulator := createBlockSimulator( + testingMode, tb, t, params, event, invariants, + ops, operationQueue, timeOperationQueue, + numBlocks, blockSize, displayLogs) + if !testingMode { b.ResetTimer() } else { @@ -147,8 +155,14 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, // Run queued operations. Ignores blocksize if blocksize is too small logWriter("Queued operations") - numQueuedOpsRan := runQueuedOperations(operationQueue, int(header.Height), tb, r, app, ctx, accs, logWriter, displayLogs, event) - numQueuedTimeOpsRan := runQueuedTimeOperations(timeOperationQueue, header.Time, tb, r, app, ctx, accs, logWriter, displayLogs, event) + numQueuedOpsRan := runQueuedOperations( + operationQueue, int(header.Height), + tb, r, app, ctx, accs, logWriter, + displayLogs, event) + numQueuedTimeOpsRan := runQueuedTimeOperations( + timeOperationQueue, header.Time, + tb, r, app, ctx, accs, + logWriter, displayLogs, event) if testingMode && onOperation { // Make sure invariants hold at end of queued operations assertAllInvariants(t, app, invariants, "QueuedOperations", displayLogs) @@ -164,7 +178,10 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, res := app.EndBlock(abci.RequestEndBlock{}) header.Height++ - header.Time = header.Time.Add(time.Duration(minTimePerBlock) * time.Second).Add(time.Duration(int64(r.Intn(int(timeDiff)))) * time.Second) + header.Time = header.Time.Add( + time.Duration(minTimePerBlock) * time.Second) + header.Time = header.Time.Add( + time.Duration(int64(r.Intn(int(timeDiff)))) * time.Second) header.ProposerAddress = randomProposer(r, validators) logWriter("EndBlock") @@ -198,16 +215,14 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, return nil } -type blockSimFn func( - r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, - accounts []Account, header abci.Header, logWriter func(string), -) (opCount int) +type blockSimFn func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, + accounts []Account, header abci.Header, logWriter func(string)) (opCount int) // Returns a function to simulate blocks. Written like this to avoid constant parameters being passed everytime, to minimize // memory overhead func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params Params, - event func(string), invariants []Invariant, - ops []WeightedOperation, operationQueue map[int][]Operation, timeOperationQueue []FutureOperation, + event func(string), invariants []Invariant, ops []WeightedOperation, + operationQueue map[int][]Operation, timeOperationQueue []FutureOperation, totalNumBlocks int, avgBlockSize int, displayLogs func()) blockSimFn { var ( @@ -233,23 +248,29 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []Account, header abci.Header, logWriter func(string)) (opCount int) { - fmt.Printf("\rSimulating... block %d/%d, operation %d/%d. ", header.Height, totalNumBlocks, opCount, blocksize) + + fmt.Printf("\rSimulating... block %d/%d, operation %d/%d. ", + header.Height, totalNumBlocks, opCount, blocksize) lastBlocksizeState, blocksize = getBlockSize(r, params, lastBlocksizeState, avgBlockSize) + for j := 0; j < blocksize; j++ { logUpdate, futureOps, err := selectOp(r)(r, app, ctx, accounts, event) logWriter(logUpdate) if err != nil { displayLogs() - tb.Fatalf("error on operation %d within block %d, %v", header.Height, opCount, err) + tb.Fatalf("error on operation %d within block %d, %v", + header.Height, opCount, err) } queueOperations(operationQueue, timeOperationQueue, futureOps) if testingMode { if onOperation { - assertAllInvariants(t, app, invariants, fmt.Sprintf("operation: %v", logUpdate), displayLogs) + assertAllInvariants(t, app, invariants, + fmt.Sprintf("operation: %v", logUpdate), displayLogs) } if opCount%50 == 0 { - fmt.Printf("\rSimulating... block %d/%d, operation %d/%d. ", header.Height, totalNumBlocks, opCount, blocksize) + fmt.Printf("\rSimulating... block %d/%d, operation %d/%d. ", + header.Height, totalNumBlocks, opCount, blocksize) } } opCount++ @@ -272,10 +293,11 @@ func getTestingMode(tb testing.TB) (testingMode bool, t *testing.T, b *testing.B // getBlockSize returns a block size as determined from the transition matrix. // It targets making average block size the provided parameter. The three // states it moves between are: -// "over stuffed" blocks with average size of 2 * avgblocksize, -// normal sized blocks, hitting avgBlocksize on average, -// and empty blocks, with no txs / only txs scheduled from the past. -func getBlockSize(r *rand.Rand, params Params, lastBlockSizeState, avgBlockSize int) (state, blocksize int) { +// - "over stuffed" blocks with average size of 2 * avgblocksize, +// - normal sized blocks, hitting avgBlocksize on average, +// - and empty blocks, with no txs / only txs scheduled from the past. +func getBlockSize(r *rand.Rand, params Params, + lastBlockSizeState, avgBlockSize int) (state, blocksize int) { // TODO: Make default blocksize transition matrix actually make the average // blocksize equal to avgBlockSize. state = params.BlockSizeTransitionMatrix.NextState(r, lastBlockSizeState) @@ -290,7 +312,10 @@ func getBlockSize(r *rand.Rand, params Params, lastBlockSizeState, avgBlockSize } // adds all future operations into the operation queue. -func queueOperations(queuedOperations map[int][]Operation, queuedTimeOperations []FutureOperation, futureOperations []FutureOperation) { +func queueOperations(queuedOperations map[int][]Operation, + queuedTimeOperations []FutureOperation, + futureOperations []FutureOperation) { + if futureOperations == nil { return } @@ -312,8 +337,11 @@ func queueOperations(queuedOperations map[int][]Operation, queuedTimeOperations } // nolint: errcheck -func runQueuedOperations(queueOperations map[int][]Operation, height int, tb testing.TB, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, - accounts []Account, logWriter func(string), displayLogs func(), event func(string)) (numOpsRan int) { +func runQueuedOperations(queueOperations map[int][]Operation, + height int, tb testing.TB, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, + accounts []Account, logWriter func(string), + displayLogs func(), event func(string)) (numOpsRan int) { + if queuedOps, ok := queueOperations[height]; ok { numOps := len(queuedOps) for i := 0; i < numOps; i++ { @@ -333,8 +361,10 @@ func runQueuedOperations(queueOperations map[int][]Operation, height int, tb tes return 0 } -func runQueuedTimeOperations(queueOperations []FutureOperation, currentTime time.Time, tb testing.TB, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, - accounts []Account, logWriter func(string), displayLogs func(), event func(string)) (numOpsRan int) { +func runQueuedTimeOperations(queueOperations []FutureOperation, + currentTime time.Time, tb testing.TB, r *rand.Rand, + app *baseapp.BaseApp, ctx sdk.Context, accounts []Account, + logWriter func(string), displayLogs func(), event func(string)) (numOpsRan int) { numOpsRan = 0 for len(queueOperations) > 0 && currentTime.After(queueOperations[0].BlockTime) { @@ -379,10 +409,13 @@ func randomProposer(r *rand.Rand, validators map[string]mockValidator) cmn.HexBy return pk.Address() } -// RandomRequestBeginBlock generates a list of signing validators according to the provided list of validators, signing fraction, and evidence fraction -// nolint: unparam -func RandomRequestBeginBlock(r *rand.Rand, params Params, validators map[string]mockValidator, - pastTimes []time.Time, pastVoteInfos [][]abci.VoteInfo, event func(string), header abci.Header) abci.RequestBeginBlock { +// RandomRequestBeginBlock generates a list of signing validators according to +// the provided list of validators, signing fraction, and evidence fraction +func RandomRequestBeginBlock(r *rand.Rand, params Params, + validators map[string]mockValidator, pastTimes []time.Time, + pastVoteInfos [][]abci.VoteInfo, + event func(string), header abci.Header) abci.RequestBeginBlock { + if len(validators) == 0 { return abci.RequestBeginBlock{Header: header} } @@ -459,7 +492,9 @@ func RandomRequestBeginBlock(r *rand.Rand, params Params, validators map[string] // updateValidators mimicks Tendermint's update logic // nolint: unparam -func updateValidators(tb testing.TB, r *rand.Rand, params Params, current map[string]mockValidator, updates []abci.ValidatorUpdate, event func(string)) map[string]mockValidator { +func updateValidators(tb testing.TB, r *rand.Rand, params Params, + current map[string]mockValidator, updates []abci.ValidatorUpdate, + event func(string)) map[string]mockValidator { for _, update := range updates { str := fmt.Sprintf("%v", update.PubKey) @@ -478,7 +513,10 @@ func updateValidators(tb testing.TB, r *rand.Rand, params Params, current map[st event("endblock/validatorupdates/updated") } else { // Set this new validator - current[str] = mockValidator{update, GetMemberOfInitialState(r, params.InitialLivenessWeightings)} + current[str] = mockValidator{ + update, + GetMemberOfInitialState(r, params.InitialLivenessWeightings), + } event("endblock/validatorupdates/added") } } diff --git a/x/mock/simulation/transition_matrix.go b/x/mock/simulation/transition_matrix.go index 39bdb1e4f956..97cd307e56cd 100644 --- a/x/mock/simulation/transition_matrix.go +++ b/x/mock/simulation/transition_matrix.go @@ -5,12 +5,11 @@ import ( "math/rand" ) -// TransitionMatrix is _almost_ a left stochastic matrix. -// It is technically not one due to not normalizing the column values. -// In the future, if we want to find the steady state distribution, -// it will be quite easy to normalize these values to get a stochastic matrix. -// Floats aren't currently used as the default due to non-determinism across -// architectures +// TransitionMatrix is _almost_ a left stochastic matrix. It is technically +// not one due to not normalizing the column values. In the future, if we want +// to find the steady state distribution, it will be quite easy to normalize +// these values to get a stochastic matrix. Floats aren't currently used as +// the default due to non-determinism across architectures type TransitionMatrix struct { weights [][]int // total in each column @@ -24,7 +23,8 @@ func CreateTransitionMatrix(weights [][]int) (TransitionMatrix, error) { n := len(weights) for i := 0; i < n; i++ { if len(weights[i]) != n { - return TransitionMatrix{}, fmt.Errorf("Transition Matrix: Non-square matrix provided, error on row %d", i) + return TransitionMatrix{}, + fmt.Errorf("Transition Matrix: Non-square matrix provided, error on row %d", i) } } totals := make([]int, n) @@ -36,8 +36,8 @@ func CreateTransitionMatrix(weights [][]int) (TransitionMatrix, error) { return TransitionMatrix{weights, totals, n}, nil } -// NextState returns the next state randomly chosen using r, and the weightings provided -// in the transition matrix. +// NextState returns the next state randomly chosen using r, and the weightings +// provided in the transition matrix. func (t TransitionMatrix) NextState(r *rand.Rand, i int) int { randNum := r.Intn(t.totals[i]) for row := 0; row < t.n; row++ { @@ -51,7 +51,7 @@ func (t TransitionMatrix) NextState(r *rand.Rand, i int) int { } // GetMemberOfInitialState takes an initial array of weights, of size n. -// It returns a weighted random number in [0,n). +// It returns a weighted random number in [0,n]. func GetMemberOfInitialState(r *rand.Rand, weights []int) int { n := len(weights) total := 0 diff --git a/x/mock/simulation/types.go b/x/mock/simulation/types.go index e601f2e1f9f5..198e2a4ff827 100644 --- a/x/mock/simulation/types.go +++ b/x/mock/simulation/types.go @@ -10,68 +10,71 @@ import ( "github.com/tendermint/tendermint/crypto" ) -type ( - // Operation runs a state machine transition, - // and ensures the transition happened as expected. - // The operation could be running and testing a fuzzed transaction, - // or doing the same for a message. - // - // For ease of debugging, - // an operation returns a descriptive message "action", - // which details what this fuzzed state machine transition actually did. - // - // Operations can optionally provide a list of "FutureOperations" to run later - // These will be ran at the beginning of the corresponding block. - Operation func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, - accounts []Account, event func(string), - ) (action string, futureOperations []FutureOperation, err error) +// Operation runs a state machine transition, +// and ensures the transition happened as expected. +// The operation could be running and testing a fuzzed transaction, +// or doing the same for a message. +// +// For ease of debugging, +// an operation returns a descriptive message "action", +// which details what this fuzzed state machine transition actually did. +// +// Operations can optionally provide a list of "FutureOperations" to run later +// These will be ran at the beginning of the corresponding block. +type Operation func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, + accounts []Account, event func(string)) ( + action string, futureOperations []FutureOperation, err error) - // RandSetup performs the random setup the mock module needs. - RandSetup func(r *rand.Rand, accounts []Account) +// RandSetup performs the random setup the mock module needs. +type RandSetup func(r *rand.Rand, accounts []Account) - // An Invariant is a function which tests a particular invariant. - // If the invariant has been broken, it should return an error - // containing a descriptive message about what happened. - // The simulator will then halt and print the logs. - Invariant func(app *baseapp.BaseApp) error +// An Invariant is a function which tests a particular invariant. +// If the invariant has been broken, it should return an error +// containing a descriptive message about what happened. +// The simulator will then halt and print the logs. +type Invariant func(app *baseapp.BaseApp) error - // Account contains a privkey, pubkey, address tuple - // eventually more useful data can be placed in here. - // (e.g. number of coins) - Account struct { - PrivKey crypto.PrivKey - PubKey crypto.PubKey - Address sdk.AccAddress - } +// Account contains a privkey, pubkey, address tuple +// eventually more useful data can be placed in here. +// (e.g. number of coins) +type Account struct { + PrivKey crypto.PrivKey + PubKey crypto.PubKey + Address sdk.AccAddress +} - mockValidator struct { - val abci.ValidatorUpdate - livenessState int - } +// are two accounts equal +func (acc Account) Equals(acc2 Account) bool { + return acc.Address.Equals(acc2.Address) +} - // FutureOperation is an operation which will be ran at the - // beginning of the provided BlockHeight. - // If both a BlockHeight and BlockTime are specified, it will use the BlockHeight. - // In the (likely) event that multiple operations are queued at the same - // block height, they will execute in a FIFO pattern. - FutureOperation struct { - BlockHeight int - BlockTime time.Time - Op Operation - } +type mockValidator struct { + val abci.ValidatorUpdate + livenessState int +} - // WeightedOperation is an operation with associated weight. - // This is used to bias the selection operation within the simulator. - WeightedOperation struct { - Weight int - Op Operation - } -) +// FutureOperation is an operation which will be ran at the +// beginning of the provided BlockHeight. +// If both a BlockHeight and BlockTime are specified, it will use the BlockHeight. +// In the (likely) event that multiple operations are queued at the same +// block height, they will execute in a FIFO pattern. +type FutureOperation struct { + BlockHeight int + BlockTime time.Time + Op Operation +} + +// WeightedOperation is an operation with associated weight. +// This is used to bias the selection operation within the simulator. +type WeightedOperation struct { + Weight int + Op Operation +} // TODO remove? not being called anywhere -// PeriodicInvariant returns an Invariant function closure that asserts -// a given invariant if the mock application's last block modulo the given -// period is congruent to the given offset. +// PeriodicInvariant returns an Invariant function closure that asserts a given +// invariant if the mock application's last block modulo the given period is +// congruent to the given offset. func PeriodicInvariant(invariant Invariant, period int, offset int) Invariant { return func(app *baseapp.BaseApp) error { if int(app.LastBlockHeight())%period == offset { @@ -80,8 +83,3 @@ func PeriodicInvariant(invariant Invariant, period int, offset int) Invariant { return nil } } - -// nolint -func (acc Account) Equals(acc2 Account) bool { - return acc.Address.Equals(acc2.Address) -} diff --git a/x/mock/simulation/util.go b/x/mock/simulation/util.go index 62b253a25801..bb974b65f1ac 100644 --- a/x/mock/simulation/util.go +++ b/x/mock/simulation/util.go @@ -21,8 +21,8 @@ import ( // shamelessly copied from https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang#31832326 // TODO we should probably move this to tendermint/libs/common/random.go -const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" const ( + letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" letterIdxBits = 6 // 6 bits to represent a letter index letterIdxMask = 1< 10 { - fileName := fmt.Sprintf("simulation_log_%s.txt", time.Now().Format("2006-01-02 15:04:05")) + + fileName := fmt.Sprintf("simulation_log_%s.txt", + time.Now().Format("2006-01-02 15:04:05")) + fmt.Printf("Too many logs to display, instead writing to %s\n", fileName) f, _ = os.Create(fileName) } From 6a7c4d1c868a8fdb47ddf42b2f607e9a5c3ce1bb Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 7 Nov 2018 10:37:45 -0500 Subject: [PATCH 04/18] rand utile ... --- x/mock/simulation/rand_util.go | 93 +++++++++++++++++++++ x/mock/simulation/random_simulate_blocks.go | 8 +- x/mock/simulation/types.go | 13 --- x/mock/simulation/util.go | 81 ------------------ 4 files changed, 94 insertions(+), 101 deletions(-) create mode 100644 x/mock/simulation/rand_util.go diff --git a/x/mock/simulation/rand_util.go b/x/mock/simulation/rand_util.go new file mode 100644 index 000000000000..076995d360ac --- /dev/null +++ b/x/mock/simulation/rand_util.go @@ -0,0 +1,93 @@ +package simulation + +import ( + "math/big" + "math/rand" + "time" + + "github.com/tendermint/tendermint/crypto/ed25519" + "github.com/tendermint/tendermint/crypto/secp256k1" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/mock" +) + +const ( + letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + letterIdxBits = 6 // 6 bits to represent a letter index + letterIdxMask = 1<= 0; { + if remain == 0 { + cache, remain = r.Int63(), letterIdxMax + } + if idx := int(cache & letterIdxMask); idx < len(letterBytes) { + b[i] = letterBytes[idx] + i-- + } + cache >>= letterIdxBits + remain-- + } + return string(b) +} + +// RandomAcc pick a random account from an array +func RandomAcc(r *rand.Rand, accs []Account) Account { + return accs[r.Intn( + len(accs), + )] +} + +// Generate a random amount +func RandomAmount(r *rand.Rand, max sdk.Int) sdk.Int { + return sdk.NewInt(int64(r.Intn(int(max.Int64())))) +} + +// RandomDecAmount generates a random decimal amount +func RandomDecAmount(r *rand.Rand, max sdk.Dec) sdk.Dec { + randInt := big.NewInt(0).Rand(r, max.Int) + return sdk.NewDecFromBigIntWithPrec(randInt, sdk.Precision) +} + +// RandomAccounts generates n random accounts +func RandomAccounts(r *rand.Rand, n int) []Account { + accs := make([]Account, n) + for i := 0; i < n; i++ { + // don't need that much entropy for simulation + privkeySeed := make([]byte, 15) + r.Read(privkeySeed) + useSecp := r.Int63()%2 == 0 + if useSecp { + accs[i].PrivKey = secp256k1.GenPrivKeySecp256k1(privkeySeed) + } else { + accs[i].PrivKey = ed25519.GenPrivKeyFromSecret(privkeySeed) + } + accs[i].PubKey = accs[i].PrivKey.PubKey() + accs[i].Address = sdk.AccAddress(accs[i].PubKey.Address()) + } + return accs +} + +// RandomSetGenesis wraps mock.RandomSetGenesis, but using simulation accounts +func RandomSetGenesis(r *rand.Rand, app *mock.App, accs []Account, denoms []string) { + addrs := make([]sdk.AccAddress, len(accs)) + for i := 0; i < len(accs); i++ { + addrs[i] = accs[i].Address + } + mock.RandomSetGenesis(r, app, addrs, denoms) +} + +// RandTimestamp generates a random timestamp +func RandTimestamp(r *rand.Rand) time.Time { + // json.Marshal breaks for timestamps greater with year greater than 9999 + unixTime := r.Int63n(253373529600) + return time.Unix(unixTime, 0) +} diff --git a/x/mock/simulation/random_simulate_blocks.go b/x/mock/simulation/random_simulate_blocks.go index 17a26f762647..9cfe27011fe0 100644 --- a/x/mock/simulation/random_simulate_blocks.go +++ b/x/mock/simulation/random_simulate_blocks.go @@ -50,12 +50,6 @@ func initChain(r *rand.Rand, params Params, return } -func randTimestamp(r *rand.Rand) time.Time { - // json.Marshal breaks for timestamps greater with year greater than 9999 - unixTime := r.Int63n(253373529600) - return time.Unix(unixTime, 0) -} - // SimulateFromSeed tests an application by running the provided // operations, testing the provided invariants, but using the provided seed. func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, @@ -70,7 +64,7 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, r := rand.New(rand.NewSource(seed)) params := RandomParams(r) // := DefaultParams() fmt.Printf("Randomized simulation params: %+v\n", params) - timestamp := randTimestamp(r) + timestamp := RandTimestamp(r) fmt.Printf("Starting the simulation from time %v, unixtime %v\n", timestamp.UTC().Format(time.UnixDate), timestamp.Unix()) timeDiff := maxTimePerBlock - minTimePerBlock diff --git a/x/mock/simulation/types.go b/x/mock/simulation/types.go index 198e2a4ff827..65118547f583 100644 --- a/x/mock/simulation/types.go +++ b/x/mock/simulation/types.go @@ -70,16 +70,3 @@ type WeightedOperation struct { Weight int Op Operation } - -// TODO remove? not being called anywhere -// PeriodicInvariant returns an Invariant function closure that asserts a given -// invariant if the mock application's last block modulo the given period is -// congruent to the given offset. -func PeriodicInvariant(invariant Invariant, period int, offset int) Invariant { - return func(app *baseapp.BaseApp) error { - if int(app.LastBlockHeight())%period == offset { - return invariant(app) - } - return nil - } -} diff --git a/x/mock/simulation/util.go b/x/mock/simulation/util.go index bb974b65f1ac..10ed6b3b0b98 100644 --- a/x/mock/simulation/util.go +++ b/x/mock/simulation/util.go @@ -2,50 +2,15 @@ package simulation import ( "fmt" - "math/big" - "math/rand" "os" "sort" "strings" "testing" "time" - "github.com/tendermint/tendermint/crypto/ed25519" - "github.com/tendermint/tendermint/crypto/secp256k1" - "github.com/cosmos/cosmos-sdk/baseapp" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/mock" -) - -// shamelessly copied from https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang#31832326 -// TODO we should probably move this to tendermint/libs/common/random.go - -const ( - letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" - letterIdxBits = 6 // 6 bits to represent a letter index - letterIdxMask = 1<= 0; { - if remain == 0 { - cache, remain = r.Int63(), letterIdxMax - } - if idx := int(cache & letterIdxMask); idx < len(letterBytes) { - b[i] = letterBytes[idx] - i-- - } - cache >>= letterIdxBits - remain-- - } - return string(b) -} - // Pretty-print events as a table func DisplayEvents(events map[string]uint) { var keys []string @@ -59,43 +24,6 @@ func DisplayEvents(events map[string]uint) { } } -// RandomAcc pick a random account from an array -func RandomAcc(r *rand.Rand, accs []Account) Account { - return accs[r.Intn( - len(accs), - )] -} - -// Generate a random amount -func RandomAmount(r *rand.Rand, max sdk.Int) sdk.Int { - return sdk.NewInt(int64(r.Intn(int(max.Int64())))) -} - -// RandomDecAmount generates a random decimal amount -func RandomDecAmount(r *rand.Rand, max sdk.Dec) sdk.Dec { - randInt := big.NewInt(0).Rand(r, max.Int) - return sdk.NewDecFromBigIntWithPrec(randInt, sdk.Precision) -} - -// RandomAccounts generates n random accounts -func RandomAccounts(r *rand.Rand, n int) []Account { - accs := make([]Account, n) - for i := 0; i < n; i++ { - // don't need that much entropy for simulation - privkeySeed := make([]byte, 15) - r.Read(privkeySeed) - useSecp := r.Int63()%2 == 0 - if useSecp { - accs[i].PrivKey = secp256k1.GenPrivKeySecp256k1(privkeySeed) - } else { - accs[i].PrivKey = ed25519.GenPrivKeyFromSecret(privkeySeed) - } - accs[i].PubKey = accs[i].PrivKey.PubKey() - accs[i].Address = sdk.AccAddress(accs[i].PubKey.Address()) - } - return accs -} - // Builds a function to add logs for this particular block func addLogMessage(testingmode bool, blockLogBuilders []*strings.Builder, height int) func(string) { if testingmode { @@ -124,15 +52,6 @@ func assertAllInvariants(t *testing.T, app *baseapp.BaseApp, } } -// RandomSetGenesis wraps mock.RandomSetGenesis, but using simulation accounts -func RandomSetGenesis(r *rand.Rand, app *mock.App, accs []Account, denoms []string) { - addrs := make([]sdk.AccAddress, len(accs)) - for i := 0; i < len(accs); i++ { - addrs[i] = accs[i].Address - } - mock.RandomSetGenesis(r, app, addrs, denoms) -} - // Creates a function to print out the logs func logPrinter(testingmode bool, logs []*strings.Builder) func() { if testingmode { From 78c3430bb34b1b2ea48072083676d2ffaad0829b Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 7 Nov 2018 11:00:24 -0500 Subject: [PATCH 05/18] moving stuff around a bit, trying to get rid of types --- x/mock/simulation/account.go | 51 ++++++++++++ x/mock/simulation/invariants.go | 30 +++++++ x/mock/simulation/rand_util.go | 29 ------- ...{random_simulate_blocks.go => simulate.go} | 82 ++++--------------- x/mock/simulation/types.go | 27 ------ x/mock/simulation/validator.go | 78 ++++++++++++++++++ 6 files changed, 175 insertions(+), 122 deletions(-) create mode 100644 x/mock/simulation/account.go create mode 100644 x/mock/simulation/invariants.go rename x/mock/simulation/{random_simulate_blocks.go => simulate.go} (88%) create mode 100644 x/mock/simulation/validator.go diff --git a/x/mock/simulation/account.go b/x/mock/simulation/account.go new file mode 100644 index 000000000000..37dfbb2cdb1a --- /dev/null +++ b/x/mock/simulation/account.go @@ -0,0 +1,51 @@ +package simulation + +import ( + "math/rand" + + "github.com/tendermint/tendermint/crypto" + "github.com/tendermint/tendermint/crypto/ed25519" + "github.com/tendermint/tendermint/crypto/secp256k1" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// Account contains a privkey, pubkey, address tuple +// eventually more useful data can be placed in here. +// (e.g. number of coins) +type Account struct { + PrivKey crypto.PrivKey + PubKey crypto.PubKey + Address sdk.AccAddress +} + +// are two accounts equal +func (acc Account) Equals(acc2 Account) bool { + return acc.Address.Equals(acc2.Address) +} + +// RandomAcc pick a random account from an array +func RandomAcc(r *rand.Rand, accs []Account) Account { + return accs[r.Intn( + len(accs), + )] +} + +// RandomAccounts generates n random accounts +func RandomAccounts(r *rand.Rand, n int) []Account { + accs := make([]Account, n) + for i := 0; i < n; i++ { + // don't need that much entropy for simulation + privkeySeed := make([]byte, 15) + r.Read(privkeySeed) + useSecp := r.Int63()%2 == 0 + if useSecp { + accs[i].PrivKey = secp256k1.GenPrivKeySecp256k1(privkeySeed) + } else { + accs[i].PrivKey = ed25519.GenPrivKeyFromSecret(privkeySeed) + } + accs[i].PubKey = accs[i].PrivKey.PubKey() + accs[i].Address = sdk.AccAddress(accs[i].PubKey.Address()) + } + return accs +} diff --git a/x/mock/simulation/invariants.go b/x/mock/simulation/invariants.go new file mode 100644 index 000000000000..b8f9aad21d00 --- /dev/null +++ b/x/mock/simulation/invariants.go @@ -0,0 +1,30 @@ +package simulation + +import ( + "fmt" + "testing" + + "github.com/cosmos/cosmos-sdk/baseapp" +) + +// An Invariant is a function which tests a particular invariant. +// If the invariant has been broken, it should return an error +// containing a descriptive message about what happened. +// The simulator will then halt and print the logs. +type Invariant func(app *baseapp.BaseApp) error + +// assertAllInvariants asserts a list of provided invariants against +// application state +func assertAllInvariants(t *testing.T, app *baseapp.BaseApp, + invariants []Invariant, where string, displayLogs func()) { + + for i := 0; i < len(invariants); i++ { + err := invariants[i](app) + if err != nil { + fmt.Printf("Invariants broken after %s\n", where) + fmt.Println(err.Error()) + displayLogs() + t.Fatal() + } + } +} diff --git a/x/mock/simulation/rand_util.go b/x/mock/simulation/rand_util.go index 076995d360ac..c40d2a65ca1d 100644 --- a/x/mock/simulation/rand_util.go +++ b/x/mock/simulation/rand_util.go @@ -5,9 +5,6 @@ import ( "math/rand" "time" - "github.com/tendermint/tendermint/crypto/ed25519" - "github.com/tendermint/tendermint/crypto/secp256k1" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/mock" ) @@ -39,13 +36,6 @@ func RandStringOfLength(r *rand.Rand, n int) string { return string(b) } -// RandomAcc pick a random account from an array -func RandomAcc(r *rand.Rand, accs []Account) Account { - return accs[r.Intn( - len(accs), - )] -} - // Generate a random amount func RandomAmount(r *rand.Rand, max sdk.Int) sdk.Int { return sdk.NewInt(int64(r.Intn(int(max.Int64())))) @@ -57,25 +47,6 @@ func RandomDecAmount(r *rand.Rand, max sdk.Dec) sdk.Dec { return sdk.NewDecFromBigIntWithPrec(randInt, sdk.Precision) } -// RandomAccounts generates n random accounts -func RandomAccounts(r *rand.Rand, n int) []Account { - accs := make([]Account, n) - for i := 0; i < n; i++ { - // don't need that much entropy for simulation - privkeySeed := make([]byte, 15) - r.Read(privkeySeed) - useSecp := r.Int63()%2 == 0 - if useSecp { - accs[i].PrivKey = secp256k1.GenPrivKeySecp256k1(privkeySeed) - } else { - accs[i].PrivKey = ed25519.GenPrivKeyFromSecret(privkeySeed) - } - accs[i].PubKey = accs[i].PrivKey.PubKey() - accs[i].Address = sdk.AccAddress(accs[i].PubKey.Address()) - } - return accs -} - // RandomSetGenesis wraps mock.RandomSetGenesis, but using simulation accounts func RandomSetGenesis(r *rand.Rand, app *mock.App, accs []Account, denoms []string) { addrs := make([]sdk.AccAddress, len(accs)) diff --git a/x/mock/simulation/random_simulate_blocks.go b/x/mock/simulation/simulate.go similarity index 88% rename from x/mock/simulation/random_simulate_blocks.go rename to x/mock/simulation/simulate.go index 9cfe27011fe0..7342a91c153a 100644 --- a/x/mock/simulation/random_simulate_blocks.go +++ b/x/mock/simulation/simulate.go @@ -14,7 +14,6 @@ import ( "time" abci "github.com/tendermint/tendermint/abci/types" - cmn "github.com/tendermint/tendermint/libs/common" tmtypes "github.com/tendermint/tendermint/types" "github.com/cosmos/cosmos-sdk/baseapp" @@ -188,7 +187,8 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, } if header.ProposerAddress == nil { - fmt.Printf("\nSimulation stopped early as all validators have been unbonded, there is nobody left propose a block!\n") + fmt.Printf("\nSimulation stopped early as all validators " + + "have been unbonded, there is nobody left propose a block!\n") stopEarly = true break } @@ -204,11 +204,16 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, DisplayEvents(events) return } - fmt.Printf("\nSimulation complete. Final height (blocks): %d, final time (seconds), : %v, operations ran %d\n", header.Height, header.Time, opCount) + fmt.Printf("\nSimulation complete. Final height (blocks): %d, "+ + "final time (seconds), : %v, operations ran %d\n", + header.Height, header.Time, opCount) + DisplayEvents(events) return nil } +//______________________________________________________________________________ + type blockSimFn func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []Account, header abci.Header, logWriter func(string)) (opCount int) @@ -302,7 +307,7 @@ func getBlockSize(r *rand.Rand, params Params, } else { blocksize = 0 } - return + return state, blocksize } // adds all future operations into the operation queue. @@ -313,6 +318,7 @@ func queueOperations(queuedOperations map[int][]Operation, if futureOperations == nil { return } + for _, futureOp := range futureOperations { if futureOp.BlockHeight != 0 { if val, ok := queuedOperations[futureOp.BlockHeight]; ok { @@ -322,7 +328,12 @@ func queueOperations(queuedOperations map[int][]Operation, } } else { // TODO: Replace with proper sorted data structure, so don't have the copy entire slice - index := sort.Search(len(queuedTimeOperations), func(i int) bool { return queuedTimeOperations[i].BlockTime.After(futureOp.BlockTime) }) + index := sort.Search( + len(queuedTimeOperations), + func(i int) bool { + return queuedTimeOperations[i].BlockTime.After(futureOp.BlockTime) + }, + ) queuedTimeOperations = append(queuedTimeOperations, FutureOperation{}) copy(queuedTimeOperations[index+1:], queuedTimeOperations[index:]) queuedTimeOperations[index] = futureOp @@ -377,32 +388,6 @@ func runQueuedTimeOperations(queueOperations []FutureOperation, return numOpsRan } -func getKeys(validators map[string]mockValidator) []string { - keys := make([]string, len(validators)) - i := 0 - for key := range validators { - keys[i] = key - i++ - } - sort.Strings(keys) - return keys -} - -// randomProposer picks a random proposer from the current validator set -func randomProposer(r *rand.Rand, validators map[string]mockValidator) cmn.HexBytes { - keys := getKeys(validators) - if len(keys) == 0 { - return nil - } - key := keys[r.Intn(len(keys))] - proposer := validators[key].val - pk, err := tmtypes.PB2TM.PubKey(proposer.PubKey) - if err != nil { - panic(err) - } - return pk.Address() -} - // RandomRequestBeginBlock generates a list of signing validators according to // the provided list of validators, signing fraction, and evidence fraction func RandomRequestBeginBlock(r *rand.Rand, params Params, @@ -483,38 +468,3 @@ func RandomRequestBeginBlock(r *rand.Rand, params Params, ByzantineValidators: evidence, } } - -// updateValidators mimicks Tendermint's update logic -// nolint: unparam -func updateValidators(tb testing.TB, r *rand.Rand, params Params, - current map[string]mockValidator, updates []abci.ValidatorUpdate, - event func(string)) map[string]mockValidator { - - for _, update := range updates { - str := fmt.Sprintf("%v", update.PubKey) - switch { - case update.Power == 0: - if _, ok := current[str]; !ok { - tb.Fatalf("tried to delete a nonexistent validator") - } - - event("endblock/validatorupdates/kicked") - delete(current, str) - default: - // Does validator already exist? - if mVal, ok := current[str]; ok { - mVal.val = update - event("endblock/validatorupdates/updated") - } else { - // Set this new validator - current[str] = mockValidator{ - update, - GetMemberOfInitialState(r, params.InitialLivenessWeightings), - } - event("endblock/validatorupdates/added") - } - } - } - - return current -} diff --git a/x/mock/simulation/types.go b/x/mock/simulation/types.go index 65118547f583..1014042a7148 100644 --- a/x/mock/simulation/types.go +++ b/x/mock/simulation/types.go @@ -6,8 +6,6 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" - abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/crypto" ) // Operation runs a state machine transition, @@ -28,31 +26,6 @@ type Operation func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, // RandSetup performs the random setup the mock module needs. type RandSetup func(r *rand.Rand, accounts []Account) -// An Invariant is a function which tests a particular invariant. -// If the invariant has been broken, it should return an error -// containing a descriptive message about what happened. -// The simulator will then halt and print the logs. -type Invariant func(app *baseapp.BaseApp) error - -// Account contains a privkey, pubkey, address tuple -// eventually more useful data can be placed in here. -// (e.g. number of coins) -type Account struct { - PrivKey crypto.PrivKey - PubKey crypto.PubKey - Address sdk.AccAddress -} - -// are two accounts equal -func (acc Account) Equals(acc2 Account) bool { - return acc.Address.Equals(acc2.Address) -} - -type mockValidator struct { - val abci.ValidatorUpdate - livenessState int -} - // FutureOperation is an operation which will be ran at the // beginning of the provided BlockHeight. // If both a BlockHeight and BlockTime are specified, it will use the BlockHeight. diff --git a/x/mock/simulation/validator.go b/x/mock/simulation/validator.go new file mode 100644 index 000000000000..e758f4da4c64 --- /dev/null +++ b/x/mock/simulation/validator.go @@ -0,0 +1,78 @@ +package simulation + +import ( + "fmt" + "math/rand" + "sort" + "testing" + + abci "github.com/tendermint/tendermint/abci/types" + cmn "github.com/tendermint/tendermint/libs/common" + tmtypes "github.com/tendermint/tendermint/types" +) + +type mockValidator struct { + val abci.ValidatorUpdate + livenessState int +} + +func getKeys(validators map[string]mockValidator) []string { + keys := make([]string, len(validators)) + i := 0 + for key := range validators { + keys[i] = key + i++ + } + sort.Strings(keys) + return keys +} + +// randomProposer picks a random proposer from the current validator set +func randomProposer(r *rand.Rand, validators map[string]mockValidator) cmn.HexBytes { + keys := getKeys(validators) + if len(keys) == 0 { + return nil + } + key := keys[r.Intn(len(keys))] + proposer := validators[key].val + pk, err := tmtypes.PB2TM.PubKey(proposer.PubKey) + if err != nil { + panic(err) + } + return pk.Address() +} + +// updateValidators mimicks Tendermint's update logic +// nolint: unparam +func updateValidators(tb testing.TB, r *rand.Rand, params Params, + current map[string]mockValidator, updates []abci.ValidatorUpdate, + event func(string)) map[string]mockValidator { + + for _, update := range updates { + str := fmt.Sprintf("%v", update.PubKey) + switch { + case update.Power == 0: + if _, ok := current[str]; !ok { + tb.Fatalf("tried to delete a nonexistent validator") + } + + event("endblock/validatorupdates/kicked") + delete(current, str) + default: + // Does validator already exist? + if mVal, ok := current[str]; ok { + mVal.val = update + event("endblock/validatorupdates/updated") + } else { + // Set this new validator + current[str] = mockValidator{ + update, + GetMemberOfInitialState(r, params.InitialLivenessWeightings), + } + event("endblock/validatorupdates/added") + } + } + } + + return current +} From ea7a5ea1a85e4c477d76184b8c3419fa567fcba1 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 7 Nov 2018 11:57:53 -0500 Subject: [PATCH 06/18] reorganize more --- x/mock/simulation/params.go | 21 +++++++++++++++++++++ x/mock/simulation/simulate.go | 11 ----------- x/mock/simulation/types.go | 25 +++++++++++-------------- x/mock/simulation/util.go | 29 +++++++++++------------------ 4 files changed, 43 insertions(+), 43 deletions(-) diff --git a/x/mock/simulation/params.go b/x/mock/simulation/params.go index 8499e6c1189b..9c4398c88c7d 100644 --- a/x/mock/simulation/params.go +++ b/x/mock/simulation/params.go @@ -44,6 +44,27 @@ type Params struct { BlockSizeTransitionMatrix TransitionMatrix } +// getBlockSize returns a block size as determined from the transition matrix. +// It targets making average block size the provided parameter. The three +// states it moves between are: +// - "over stuffed" blocks with average size of 2 * avgblocksize, +// - normal sized blocks, hitting avgBlocksize on average, +// - and empty blocks, with no txs / only txs scheduled from the past. +func getBlockSize(r *rand.Rand, params Params, + lastBlockSizeState, avgBlockSize int) (state, blocksize int) { + // TODO: Make default blocksize transition matrix actually make the average + // blocksize equal to avgBlockSize. + state = params.BlockSizeTransitionMatrix.NextState(r, lastBlockSizeState) + if state == 0 { + blocksize = r.Intn(avgBlockSize * 4) + } else if state == 1 { + blocksize = r.Intn(avgBlockSize * 2) + } else { + blocksize = 0 + } + return state, blocksize +} + // Return default simulation parameters func DefaultParams() Params { return Params{ diff --git a/x/mock/simulation/simulate.go b/x/mock/simulation/simulate.go index 7342a91c153a..68e4f8b7b2fd 100644 --- a/x/mock/simulation/simulate.go +++ b/x/mock/simulation/simulate.go @@ -278,17 +278,6 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params } } -func getTestingMode(tb testing.TB) (testingMode bool, t *testing.T, b *testing.B) { - testingMode = false - if _t, ok := tb.(*testing.T); ok { - t = _t - testingMode = true - } else { - b = tb.(*testing.B) - } - return -} - // getBlockSize returns a block size as determined from the transition matrix. // It targets making average block size the provided parameter. The three // states it moves between are: diff --git a/x/mock/simulation/types.go b/x/mock/simulation/types.go index 1014042a7148..2789ca69942e 100644 --- a/x/mock/simulation/types.go +++ b/x/mock/simulation/types.go @@ -8,13 +8,14 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -// Operation runs a state machine transition, -// and ensures the transition happened as expected. -// The operation could be running and testing a fuzzed transaction, -// or doing the same for a message. +// RandSetup performs the random setup the mock module needs. +type RandSetup func(r *rand.Rand, accounts []Account) + +// Operation runs a state machine transition, and ensures the transition +// happened as expected. The operation could be running and testing a fuzzed +// transaction, or doing the same for a message. // -// For ease of debugging, -// an operation returns a descriptive message "action", +// For ease of debugging, an operation returns a descriptive message "action", // which details what this fuzzed state machine transition actually did. // // Operations can optionally provide a list of "FutureOperations" to run later @@ -23,14 +24,10 @@ type Operation func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []Account, event func(string)) ( action string, futureOperations []FutureOperation, err error) -// RandSetup performs the random setup the mock module needs. -type RandSetup func(r *rand.Rand, accounts []Account) - -// FutureOperation is an operation which will be ran at the -// beginning of the provided BlockHeight. -// If both a BlockHeight and BlockTime are specified, it will use the BlockHeight. -// In the (likely) event that multiple operations are queued at the same -// block height, they will execute in a FIFO pattern. +// FutureOperation is an operation which will be ran at the beginning of the +// provided BlockHeight. If both a BlockHeight and BlockTime are specified, it +// will use the BlockHeight. In the (likely) event that multiple operations +// are queued at the same block height, they will execute in a FIFO pattern. type FutureOperation struct { BlockHeight int BlockTime time.Time diff --git a/x/mock/simulation/util.go b/x/mock/simulation/util.go index 10ed6b3b0b98..e115cde2492c 100644 --- a/x/mock/simulation/util.go +++ b/x/mock/simulation/util.go @@ -7,10 +7,19 @@ import ( "strings" "testing" "time" - - "github.com/cosmos/cosmos-sdk/baseapp" ) +func getTestingMode(tb testing.TB) (testingMode bool, t *testing.T, b *testing.B) { + testingMode = false + if _t, ok := tb.(*testing.T); ok { + t = _t + testingMode = true + } else { + b = tb.(*testing.B) + } + return +} + // Pretty-print events as a table func DisplayEvents(events map[string]uint) { var keys []string @@ -36,22 +45,6 @@ func addLogMessage(testingmode bool, blockLogBuilders []*strings.Builder, height return func(x string) {} } -// assertAllInvariants asserts a list of provided invariants against -// application state -func assertAllInvariants(t *testing.T, app *baseapp.BaseApp, - invariants []Invariant, where string, displayLogs func()) { - - for i := 0; i < len(invariants); i++ { - err := invariants[i](app) - if err != nil { - fmt.Printf("Invariants broken after %s\n", where) - fmt.Println(err.Error()) - displayLogs() - t.Fatal() - } - } -} - // Creates a function to print out the logs func logPrinter(testingmode bool, logs []*strings.Builder) func() { if testingmode { From 980dd78c624d751a429ba41de6d9bc012dddb717 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 7 Nov 2018 12:00:13 -0500 Subject: [PATCH 07/18] rename ambig naming of queueOperations --- x/mock/simulation/simulate.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/x/mock/simulation/simulate.go b/x/mock/simulation/simulate.go index 68e4f8b7b2fd..85eadcaa60f7 100644 --- a/x/mock/simulation/simulate.go +++ b/x/mock/simulation/simulate.go @@ -331,47 +331,47 @@ func queueOperations(queuedOperations map[int][]Operation, } // nolint: errcheck -func runQueuedOperations(queueOperations map[int][]Operation, +func runQueuedOperations(queueOps map[int][]Operation, height int, tb testing.TB, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []Account, logWriter func(string), displayLogs func(), event func(string)) (numOpsRan int) { - if queuedOps, ok := queueOperations[height]; ok { - numOps := len(queuedOps) + if queuedOp, ok := queueOps[height]; ok { + numOps := len(queuedOp) for i := 0; i < numOps; i++ { // For now, queued operations cannot queue more operations. // If a need arises for us to support queued messages to queue more messages, this can // be changed. - logUpdate, _, err := queuedOps[i](r, app, ctx, accounts, event) + logUpdate, _, err := queuedOp[i](r, app, ctx, accounts, event) logWriter(logUpdate) if err != nil { displayLogs() tb.FailNow() } } - delete(queueOperations, height) + delete(queueOps, height) return numOps } return 0 } -func runQueuedTimeOperations(queueOperations []FutureOperation, +func runQueuedTimeOperations(queueOps []FutureOperation, currentTime time.Time, tb testing.TB, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []Account, logWriter func(string), displayLogs func(), event func(string)) (numOpsRan int) { numOpsRan = 0 - for len(queueOperations) > 0 && currentTime.After(queueOperations[0].BlockTime) { + for len(queueOps) > 0 && currentTime.After(queueOps[0].BlockTime) { // For now, queued operations cannot queue more operations. // If a need arises for us to support queued messages to queue more messages, this can // be changed. - logUpdate, _, err := queueOperations[0].Op(r, app, ctx, accounts, event) + logUpdate, _, err := queueOps[0].Op(r, app, ctx, accounts, event) logWriter(logUpdate) if err != nil { displayLogs() tb.FailNow() } - queueOperations = queueOperations[1:] + queueOps = queueOps[1:] numOpsRan++ } return numOpsRan From b5a205eb15a047f384e677724195dab69e72af10 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 7 Nov 2018 12:19:58 -0500 Subject: [PATCH 08/18] minimizing indentation --- x/mock/simulation/simulate.go | 145 ++++++++++++++++++++------------- x/mock/simulation/validator.go | 28 +++---- 2 files changed, 100 insertions(+), 73 deletions(-) diff --git a/x/mock/simulation/simulate.go b/x/mock/simulation/simulate.go index 85eadcaa60f7..2a199ad3cba4 100644 --- a/x/mock/simulation/simulate.go +++ b/x/mock/simulation/simulate.go @@ -64,7 +64,8 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, params := RandomParams(r) // := DefaultParams() fmt.Printf("Randomized simulation params: %+v\n", params) timestamp := RandTimestamp(r) - fmt.Printf("Starting the simulation from time %v, unixtime %v\n", timestamp.UTC().Format(time.UnixDate), timestamp.Unix()) + fmt.Printf("Starting the simulation from time %v, unixtime %v\n", + timestamp.UTC().Format(time.UnixDate), timestamp.Unix()) timeDiff := maxTimePerBlock - minTimePerBlock accs := RandomAccounts(r, params.NumKeys) @@ -76,11 +77,16 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, } validators := initChain(r, params, accs, setups, app, appStateFn) + // Second variable to keep pending validator set (delayed one block since TM 0.24) // Initially this is the same as the initial validator set nextValidators := validators - header := abci.Header{Height: 1, Time: timestamp, ProposerAddress: randomProposer(r, validators)} + header := abci.Header{ + Height: 1, + Time: timestamp, + ProposerAddress: randomProposer(r, validators), + } opCount := 0 // Setup code to catch SIGTERM's @@ -88,7 +94,8 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT) go func() { receivedSignal := <-c - fmt.Printf("\nExiting early due to %s, on block %d, operation %d\n", receivedSignal, header.Height, opCount) + fmt.Printf("\nExiting early due to %s, on block %d, operation %d\n", + receivedSignal, header.Height, opCount) simError = fmt.Errorf("Exited due to %s", receivedSignal) stopEarly = true }() @@ -96,7 +103,8 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, var pastTimes []time.Time var pastVoteInfos [][]abci.VoteInfo - request := RandomRequestBeginBlock(r, params, validators, pastTimes, pastVoteInfos, event, header) + request := RandomRequestBeginBlock(r, params, + validators, pastTimes, pastVoteInfos, event, header) // These are operations which have been queued by previous operations operationQueue := make(map[int][]Operation) @@ -122,11 +130,14 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, stackTrace := string(debug.Stack()) fmt.Println(stackTrace) displayLogs() - simError = fmt.Errorf("Simulation halted due to panic on block %d", header.Height) + simError = fmt.Errorf( + "Simulation halted due to panic on block %d", + header.Height) } }() } + // TODO split up the contents of this for loop into new functions for i := 0; i < numBlocks && !stopEarly; i++ { // Log the header time for future lookup pastTimes = append(pastTimes, header.Time) @@ -194,11 +205,13 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, } // Generate a random RequestBeginBlock with the current validator set for the next block - request = RandomRequestBeginBlock(r, params, validators, pastTimes, pastVoteInfos, event, header) + request = RandomRequestBeginBlock(r, params, validators, + pastTimes, pastVoteInfos, event, header) // Update the validator set, which will be reflected in the application on the next block validators = nextValidators - nextValidators = updateValidators(tb, r, params, validators, res.ValidatorUpdates, event) + nextValidators = updateValidators(tb, r, params, + validators, res.ValidatorUpdates, event) } if stopEarly { DisplayEvents(events) @@ -224,11 +237,9 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params operationQueue map[int][]Operation, timeOperationQueue []FutureOperation, totalNumBlocks int, avgBlockSize int, displayLogs func()) blockSimFn { - var ( - lastBlocksizeState = 0 // state for [4 * uniform distribution] - totalOpWeight = 0 - blocksize int - ) + var lastBlocksizeState = 0 // state for [4 * uniform distribution] + var totalOpWeight = 0 + var blocksize int for i := 0; i < len(ops); i++ { totalOpWeight += ops[i].Weight @@ -286,14 +297,16 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params // - and empty blocks, with no txs / only txs scheduled from the past. func getBlockSize(r *rand.Rand, params Params, lastBlockSizeState, avgBlockSize int) (state, blocksize int) { + // TODO: Make default blocksize transition matrix actually make the average // blocksize equal to avgBlockSize. state = params.BlockSizeTransitionMatrix.NextState(r, lastBlockSizeState) - if state == 0 { + switch state { + case 0: blocksize = r.Intn(avgBlockSize * 4) - } else if state == 1 { + case 1: blocksize = r.Intn(avgBlockSize * 2) - } else { + default: blocksize = 0 } return state, blocksize @@ -336,23 +349,25 @@ func runQueuedOperations(queueOps map[int][]Operation, accounts []Account, logWriter func(string), displayLogs func(), event func(string)) (numOpsRan int) { - if queuedOp, ok := queueOps[height]; ok { - numOps := len(queuedOp) - for i := 0; i < numOps; i++ { - // For now, queued operations cannot queue more operations. - // If a need arises for us to support queued messages to queue more messages, this can - // be changed. - logUpdate, _, err := queuedOp[i](r, app, ctx, accounts, event) - logWriter(logUpdate) - if err != nil { - displayLogs() - tb.FailNow() - } + queuedOp, ok := queueOps[height] + if !ok { + return 0 + } + + numOps := len(queuedOp) + for i := 0; i < numOps; i++ { + // For now, queued operations cannot queue more operations. + // If a need arises for us to support queued messages to queue more messages, this can + // be changed. + logUpdate, _, err := queuedOp[i](r, app, ctx, accounts, event) + logWriter(logUpdate) + if err != nil { + displayLogs() + tb.FailNow() } - delete(queueOps, height) - return numOps } - return 0 + delete(queueOps, height) + return numOps } func runQueuedTimeOperations(queueOps []FutureOperation, @@ -385,11 +400,13 @@ func RandomRequestBeginBlock(r *rand.Rand, params Params, event func(string), header abci.Header) abci.RequestBeginBlock { if len(validators) == 0 { - return abci.RequestBeginBlock{Header: header} + return abci.RequestBeginBlock{ + Header: header, + } } voteInfos := make([]abci.VoteInfo, len(validators)) - i := 0 - for _, key := range getKeys(validators) { + + for i, key := range getKeys(validators) { mVal := validators[key] mVal.livenessState = params.LivenessTransitionMatrix.NextState(r, mVal.livenessState) signed := true @@ -403,11 +420,13 @@ func RandomRequestBeginBlock(r *rand.Rand, params Params, // offline signed = false } + if signed { event("beginblock/signing/signed") } else { event("beginblock/signing/missed") } + pubkey, err := tmtypes.PB2TM.PubKey(mVal.val.PubKey) if err != nil { panic(err) @@ -419,36 +438,46 @@ func RandomRequestBeginBlock(r *rand.Rand, params Params, }, SignedLastBlock: signed, } - i++ } + + // return if no past times + if len(pastTimes) <= 0 { + return abci.RequestBeginBlock{ + Header: header, + LastCommitInfo: abci.LastCommitInfo{ + Votes: voteInfos, + }, + } + } + // TODO: Determine capacity before allocation evidence := make([]abci.Evidence, 0) - // Anything but the first block - if len(pastTimes) > 0 { - for r.Float64() < params.EvidenceFraction { - height := header.Height - time := header.Time - vals := voteInfos - if r.Float64() < params.PastEvidenceFraction { - height = int64(r.Intn(int(header.Height) - 1)) - time = pastTimes[height] - vals = pastVoteInfos[height] - } - validator := vals[r.Intn(len(vals))].Validator - var totalVotingPower int64 - for _, val := range vals { - totalVotingPower += val.Validator.Power - } - evidence = append(evidence, abci.Evidence{ - Type: tmtypes.ABCIEvidenceTypeDuplicateVote, - Validator: validator, - Height: height, - Time: time, - TotalVotingPower: totalVotingPower, - }) - event("beginblock/evidence") + for r.Float64() < params.EvidenceFraction { + height := header.Height + time := header.Time + vals := voteInfos + if r.Float64() < params.PastEvidenceFraction { + height = int64(r.Intn(int(header.Height) - 1)) + time = pastTimes[height] + vals = pastVoteInfos[height] } + validator := vals[r.Intn(len(vals))].Validator + var totalVotingPower int64 + + for _, val := range vals { + totalVotingPower += val.Validator.Power + } + + evidence = append(evidence, abci.Evidence{ + Type: tmtypes.ABCIEvidenceTypeDuplicateVote, + Validator: validator, + Height: height, + Time: time, + TotalVotingPower: totalVotingPower, + }) + event("beginblock/evidence") } + return abci.RequestBeginBlock{ Header: header, LastCommitInfo: abci.LastCommitInfo{ diff --git a/x/mock/simulation/validator.go b/x/mock/simulation/validator.go index e758f4da4c64..3bbe1050cc91 100644 --- a/x/mock/simulation/validator.go +++ b/x/mock/simulation/validator.go @@ -16,6 +16,7 @@ type mockValidator struct { livenessState int } +// TODO describe usage func getKeys(validators map[string]mockValidator) []string { keys := make([]string, len(validators)) i := 0 @@ -50,27 +51,24 @@ func updateValidators(tb testing.TB, r *rand.Rand, params Params, for _, update := range updates { str := fmt.Sprintf("%v", update.PubKey) - switch { - case update.Power == 0: + if update.Power == 0 { if _, ok := current[str]; !ok { tb.Fatalf("tried to delete a nonexistent validator") } - event("endblock/validatorupdates/kicked") delete(current, str) - default: - // Does validator already exist? - if mVal, ok := current[str]; ok { - mVal.val = update - event("endblock/validatorupdates/updated") - } else { - // Set this new validator - current[str] = mockValidator{ - update, - GetMemberOfInitialState(r, params.InitialLivenessWeightings), - } - event("endblock/validatorupdates/added") + + } else if mVal, ok := current[str]; ok { + // validator already exists + mVal.val = update + event("endblock/validatorupdates/updated") + } else { + // Set this new validator + current[str] = mockValidator{ + update, + GetMemberOfInitialState(r, params.InitialLivenessWeightings), } + event("endblock/validatorupdates/added") } } From 49707543385d253817b12c1b2a6086d853187177 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 7 Nov 2018 14:42:00 -0500 Subject: [PATCH 09/18] fix some duplicate to get passing --- x/mock/simulation/params.go | 8 +++++--- x/mock/simulation/simulate.go | 23 ----------------------- 2 files changed, 5 insertions(+), 26 deletions(-) diff --git a/x/mock/simulation/params.go b/x/mock/simulation/params.go index 9c4398c88c7d..c46ab236bf27 100644 --- a/x/mock/simulation/params.go +++ b/x/mock/simulation/params.go @@ -52,14 +52,16 @@ type Params struct { // - and empty blocks, with no txs / only txs scheduled from the past. func getBlockSize(r *rand.Rand, params Params, lastBlockSizeState, avgBlockSize int) (state, blocksize int) { + // TODO: Make default blocksize transition matrix actually make the average // blocksize equal to avgBlockSize. state = params.BlockSizeTransitionMatrix.NextState(r, lastBlockSizeState) - if state == 0 { + switch state { + case 0: blocksize = r.Intn(avgBlockSize * 4) - } else if state == 1 { + case 1: blocksize = r.Intn(avgBlockSize * 2) - } else { + default: blocksize = 0 } return state, blocksize diff --git a/x/mock/simulation/simulate.go b/x/mock/simulation/simulate.go index 2a199ad3cba4..f963e949193f 100644 --- a/x/mock/simulation/simulate.go +++ b/x/mock/simulation/simulate.go @@ -289,29 +289,6 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params } } -// getBlockSize returns a block size as determined from the transition matrix. -// It targets making average block size the provided parameter. The three -// states it moves between are: -// - "over stuffed" blocks with average size of 2 * avgblocksize, -// - normal sized blocks, hitting avgBlocksize on average, -// - and empty blocks, with no txs / only txs scheduled from the past. -func getBlockSize(r *rand.Rand, params Params, - lastBlockSizeState, avgBlockSize int) (state, blocksize int) { - - // TODO: Make default blocksize transition matrix actually make the average - // blocksize equal to avgBlockSize. - state = params.BlockSizeTransitionMatrix.NextState(r, lastBlockSizeState) - switch state { - case 0: - blocksize = r.Intn(avgBlockSize * 4) - case 1: - blocksize = r.Intn(avgBlockSize * 2) - default: - blocksize = 0 - } - return state, blocksize -} - // adds all future operations into the operation queue. func queueOperations(queuedOperations map[int][]Operation, queuedTimeOperations []FutureOperation, From 1ee6c3295d7c75d403231a3655f32f1f72d90f4c Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 7 Nov 2018 23:26:02 -0500 Subject: [PATCH 10/18] operations functions --- x/mock/simulation/{types.go => operation.go} | 34 ++++++++++++-- x/mock/simulation/simulate.go | 47 +++++++------------- x/mock/simulation/validator.go | 20 +++++++++ 3 files changed, 68 insertions(+), 33 deletions(-) rename x/mock/simulation/{types.go => operation.go} (66%) diff --git a/x/mock/simulation/types.go b/x/mock/simulation/operation.go similarity index 66% rename from x/mock/simulation/types.go rename to x/mock/simulation/operation.go index 2789ca69942e..21b606b37359 100644 --- a/x/mock/simulation/types.go +++ b/x/mock/simulation/operation.go @@ -8,9 +8,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -// RandSetup performs the random setup the mock module needs. -type RandSetup func(r *rand.Rand, accounts []Account) - // Operation runs a state machine transition, and ensures the transition // happened as expected. The operation could be running and testing a fuzzed // transaction, or doing the same for a message. @@ -24,6 +21,9 @@ type Operation func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []Account, event func(string)) ( action string, futureOperations []FutureOperation, err error) +// queue of operations +type OperationQueue map[int][]Operation + // FutureOperation is an operation which will be ran at the beginning of the // provided BlockHeight. If both a BlockHeight and BlockTime are specified, it // will use the BlockHeight. In the (likely) event that multiple operations @@ -40,3 +40,31 @@ type WeightedOperation struct { Weight int Op Operation } + +// WeightedOperations is the group of all weighted operations to simulate. +type WeightedOperations []WeightedOperation + +func (w WeightedOperations) totalWeight() int { + totalOpWeight := 0 + for i := 0; i < len(w); i++ { + totalOpWeight += w[i].Weight + } + return totalOpWeight +} + +type selectOpFn func(r *rand.Rand) Operation + +func (w WeightedOperations) getSelectOpFn() selectOpFn { + totalOpWeight := ops.totalWeight() + return func(r *rand.Rand) Operation { + x := r.Intn(totalOpWeight) + for i := 0; i < len(ops); i++ { + if x <= ops[i].Weight { + return ops[i].Op + } + x -= ops[i].Weight + } + // shouldn't happen + return ops[0].Op + } +} diff --git a/x/mock/simulation/simulate.go b/x/mock/simulation/simulate.go index f963e949193f..849f4eeaefec 100644 --- a/x/mock/simulation/simulate.go +++ b/x/mock/simulation/simulate.go @@ -20,6 +20,9 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +// RandSetup performs the random setup the mock module needs. +type RandSetup func(r *rand.Rand, accounts []Account) + // Simulate tests application by sending random messages. func Simulate(t *testing.T, app *baseapp.BaseApp, appStateFn func(r *rand.Rand, accs []Account) json.RawMessage, @@ -27,26 +30,25 @@ func Simulate(t *testing.T, app *baseapp.BaseApp, invariants []Invariant, numBlocks int, blockSize int, commit bool) error { time := time.Now().UnixNano() - return SimulateFromSeed(t, app, appStateFn, time, ops, setups, invariants, numBlocks, blockSize, commit) + return SimulateFromSeed(t, app, appStateFn, time, ops, + setups, invariants, numBlocks, blockSize, commit) } +// initialize the chain for the simulation func initChain(r *rand.Rand, params Params, accounts []Account, setups []RandSetup, app *baseapp.BaseApp, - appStateFn func(r *rand.Rand, accounts []Account) json.RawMessage) ( - validators map[string]mockValidator) { - - res := app.InitChain(abci.RequestInitChain{AppStateBytes: appStateFn(r, accounts)}) - validators = make(map[string]mockValidator) - for _, validator := range res.Validators { - str := fmt.Sprintf("%v", validator.PubKey) - validators[str] = mockValidator{validator, GetMemberOfInitialState(r, params.InitialLivenessWeightings)} + appStateFn func(r *rand.Rand, accounts []Account) json.RawMessage) mockValidators { + + req := abci.RequestInitChain{ + AppStateBytes: appStateFn(r, accounts), } + res := app.InitChain(req) + validators = newMockValidators(res.Validators) for i := 0; i < len(setups); i++ { setups[i](r, accounts) } - - return + return validators } // SimulateFromSeed tests an application by running the provided @@ -230,31 +232,16 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, type blockSimFn func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []Account, header abci.Header, logWriter func(string)) (opCount int) -// Returns a function to simulate blocks. Written like this to avoid constant parameters being passed everytime, to minimize -// memory overhead +// Returns a function to simulate blocks. Written like this to avoid constant +// parameters being passed everytime, to minimize memory overhead func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params Params, - event func(string), invariants []Invariant, ops []WeightedOperation, + event func(string), invariants []Invariant, ops WeightedOperations, operationQueue map[int][]Operation, timeOperationQueue []FutureOperation, totalNumBlocks int, avgBlockSize int, displayLogs func()) blockSimFn { var lastBlocksizeState = 0 // state for [4 * uniform distribution] - var totalOpWeight = 0 var blocksize int - - for i := 0; i < len(ops); i++ { - totalOpWeight += ops[i].Weight - } - selectOp := func(r *rand.Rand) Operation { - x := r.Intn(totalOpWeight) - for i := 0; i < len(ops); i++ { - if x <= ops[i].Weight { - return ops[i].Op - } - x -= ops[i].Weight - } - // shouldn't happen - return ops[0].Op - } + selectOp := ops.getSelectOpFn() return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []Account, header abci.Header, logWriter func(string)) (opCount int) { diff --git a/x/mock/simulation/validator.go b/x/mock/simulation/validator.go index 3bbe1050cc91..7a2213bdb727 100644 --- a/x/mock/simulation/validator.go +++ b/x/mock/simulation/validator.go @@ -16,6 +16,26 @@ type mockValidator struct { livenessState int } +type mockValidators map[string]mockValidator + +// get mockValidators from abci validators +func newMockValidators(abciVals abci.ValidatorUpdate) mockValidators { + + validators = make(mockValidators) + for _, validator := range abciVals { + str := fmt.Sprintf("%v", validator.PubKey) + liveliness := GetMemberOfInitialState(r, + params.InitialLivenessWeightings) + + validators[str] = mockValidator{ + val: validator, + livenessState: liveliness, + } + } + + return validators +} + // TODO describe usage func getKeys(validators map[string]mockValidator) []string { keys := make([]string, len(validators)) From ff327049ee3f32871d56e2d015c75d59887372f4 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 7 Nov 2018 23:55:48 -0500 Subject: [PATCH 11/18] assertAllInvarients changes, Operation reorg --- x/mock/simulation/invariants.go | 18 +++++----- x/mock/simulation/operation.go | 38 +++++++++++++++++++++ x/mock/simulation/simulate.go | 58 ++++++++------------------------- 3 files changed, 60 insertions(+), 54 deletions(-) diff --git a/x/mock/simulation/invariants.go b/x/mock/simulation/invariants.go index b8f9aad21d00..23686005d1cf 100644 --- a/x/mock/simulation/invariants.go +++ b/x/mock/simulation/invariants.go @@ -13,16 +13,16 @@ import ( // The simulator will then halt and print the logs. type Invariant func(app *baseapp.BaseApp) error -// assertAllInvariants asserts a list of provided invariants against -// application state -func assertAllInvariants(t *testing.T, app *baseapp.BaseApp, - invariants []Invariant, where string, displayLogs func()) { +// group of Invarient +type Invariants []Invariant - for i := 0; i < len(invariants); i++ { - err := invariants[i](app) - if err != nil { - fmt.Printf("Invariants broken after %s\n", where) - fmt.Println(err.Error()) +// assertAll asserts the all invariants against application state +func (invs Invariants) assertAll(t *testing.T, app *baseapp.BaseApp, + event string, displayLogs func()) { + + for i := 0; i < len(invs); i++ { + if err := invs[i](app); err != nil { + fmt.Printf("Invariants broken after %s\n%s\n", event, err.Error()) displayLogs() t.Fatal() } diff --git a/x/mock/simulation/operation.go b/x/mock/simulation/operation.go index 21b606b37359..27757df2d573 100644 --- a/x/mock/simulation/operation.go +++ b/x/mock/simulation/operation.go @@ -2,6 +2,7 @@ package simulation import ( "math/rand" + "sort" "time" "github.com/cosmos/cosmos-sdk/baseapp" @@ -24,6 +25,41 @@ type Operation func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, // queue of operations type OperationQueue map[int][]Operation +// adds all future operations into the operation queue. +func queueOperations(queuedOps OperationQueue, + queuedTimeOps []FutureOperation, + futureOps []FutureOperation) { + + if futureOps == nil { + return + } + + for _, futureOp := range futureOps { + if futureOp.BlockHeight != 0 { + if val, ok := queuedOps[futureOp.BlockHeight]; ok { + queuedOps[futureOp.BlockHeight] = append(val, futureOp.Op) + } else { + queuedOps[futureOp.BlockHeight] = []Operation{futureOp.Op} + } + continue + } + + // TODO: Replace with proper sorted data structure, so don't have the + // copy entire slice + index := sort.Search( + len(queuedTimeOps), + func(i int) bool { + return queuedTimeOps[i].BlockTime.After(futureOp.BlockTime) + }, + ) + queuedTimeOps = append(queuedTimeOps, FutureOperation{}) + copy(queuedTimeOps[index+1:], queuedTimeOps[index:]) + queuedTimeOps[index] = futureOp + } +} + +//________________________________________________________________________ + // FutureOperation is an operation which will be ran at the beginning of the // provided BlockHeight. If both a BlockHeight and BlockTime are specified, it // will use the BlockHeight. In the (likely) event that multiple operations @@ -34,6 +70,8 @@ type FutureOperation struct { Op Operation } +//________________________________________________________________________ + // WeightedOperation is an operation with associated weight. // This is used to bias the selection operation within the simulator. type WeightedOperation struct { diff --git a/x/mock/simulation/simulate.go b/x/mock/simulation/simulate.go index 849f4eeaefec..d9b5176f8cd2 100644 --- a/x/mock/simulation/simulate.go +++ b/x/mock/simulation/simulate.go @@ -7,7 +7,6 @@ import ( "os" "os/signal" "runtime/debug" - "sort" "strings" "syscall" "testing" @@ -27,7 +26,7 @@ type RandSetup func(r *rand.Rand, accounts []Account) func Simulate(t *testing.T, app *baseapp.BaseApp, appStateFn func(r *rand.Rand, accs []Account) json.RawMessage, ops []WeightedOperation, setups []RandSetup, - invariants []Invariant, numBlocks int, blockSize int, commit bool) error { + invariants Invariants, numBlocks int, blockSize int, commit bool) error { time := time.Now().UnixNano() return SimulateFromSeed(t, app, appStateFn, time, ops, @@ -55,7 +54,7 @@ func initChain(r *rand.Rand, params Params, // operations, testing the provided invariants, but using the provided seed. func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, appStateFn func(r *rand.Rand, accs []Account) json.RawMessage, - seed int64, ops []WeightedOperation, setups []RandSetup, invariants []Invariant, + seed int64, ops WeightedOperations, setups []RandSetup, invariants Invariants, numBlocks int, blockSize int, commit bool) (simError error) { // in case we have to end early, don't os.Exit so that we can run cleanup code. @@ -154,7 +153,7 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, if testingMode { // Make sure invariants hold at beginning of block - assertAllInvariants(t, app, invariants, "BeginBlock", displayLogs) + invariants.assertAll(t, app, "BeginBlock", displayLogs) } ctx := app.NewContext(false, header) @@ -171,15 +170,15 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, logWriter, displayLogs, event) if testingMode && onOperation { // Make sure invariants hold at end of queued operations - assertAllInvariants(t, app, invariants, "QueuedOperations", displayLogs) + invariants.assertAll(t, app, "QueuedOperations", displayLogs) } logWriter("Standard operations") operations := blockSimulator(r, app, ctx, accs, header, logWriter) opCount += operations + numQueuedOpsRan + numQueuedTimeOpsRan if testingMode { - // Make sure invariants hold at end of block - assertAllInvariants(t, app, invariants, "StandardOperations", displayLogs) + // Make sure invariants hold at the operation + invariants.assertAll(t, app, "StandardOperations", displayLogs) } res := app.EndBlock(abci.RequestEndBlock{}) @@ -193,7 +192,7 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, if testingMode { // Make sure invariants hold at end of block - assertAllInvariants(t, app, invariants, "EndBlock", displayLogs) + invariants.assertAll(t, app, "EndBlock", displayLogs) } if commit { app.Commit() @@ -235,7 +234,7 @@ type blockSimFn func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, // Returns a function to simulate blocks. Written like this to avoid constant // parameters being passed everytime, to minimize memory overhead func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params Params, - event func(string), invariants []Invariant, ops WeightedOperations, + event func(string), invariants Invariants, ops WeightedOperations, operationQueue map[int][]Operation, timeOperationQueue []FutureOperation, totalNumBlocks int, avgBlockSize int, displayLogs func()) blockSimFn { @@ -262,8 +261,8 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params queueOperations(operationQueue, timeOperationQueue, futureOps) if testingMode { if onOperation { - assertAllInvariants(t, app, invariants, - fmt.Sprintf("operation: %v", logUpdate), displayLogs) + eventStr := fmt.Sprintf("operation: %v", logUpdate) + invariants.assertAll(t, app, eventStr, displayLogs) } if opCount%50 == 0 { fmt.Printf("\rSimulating... block %d/%d, operation %d/%d. ", @@ -276,37 +275,6 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params } } -// adds all future operations into the operation queue. -func queueOperations(queuedOperations map[int][]Operation, - queuedTimeOperations []FutureOperation, - futureOperations []FutureOperation) { - - if futureOperations == nil { - return - } - - for _, futureOp := range futureOperations { - if futureOp.BlockHeight != 0 { - if val, ok := queuedOperations[futureOp.BlockHeight]; ok { - queuedOperations[futureOp.BlockHeight] = append(val, futureOp.Op) - } else { - queuedOperations[futureOp.BlockHeight] = []Operation{futureOp.Op} - } - } else { - // TODO: Replace with proper sorted data structure, so don't have the copy entire slice - index := sort.Search( - len(queuedTimeOperations), - func(i int) bool { - return queuedTimeOperations[i].BlockTime.After(futureOp.BlockTime) - }, - ) - queuedTimeOperations = append(queuedTimeOperations, FutureOperation{}) - copy(queuedTimeOperations[index+1:], queuedTimeOperations[index:]) - queuedTimeOperations[index] = futureOp - } - } -} - // nolint: errcheck func runQueuedOperations(queueOps map[int][]Operation, height int, tb testing.TB, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, @@ -318,8 +286,8 @@ func runQueuedOperations(queueOps map[int][]Operation, return 0 } - numOps := len(queuedOp) - for i := 0; i < numOps; i++ { + numOpsRan = len(queuedOp) + for i := 0; i < numOpsRan; i++ { // For now, queued operations cannot queue more operations. // If a need arises for us to support queued messages to queue more messages, this can // be changed. @@ -331,7 +299,7 @@ func runQueuedOperations(queueOps map[int][]Operation, } } delete(queueOps, height) - return numOps + return numOpsRan } func runQueuedTimeOperations(queueOps []FutureOperation, From fee0763b5ef4bc609bcec2a1eecbde3c57a0a4ff Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 8 Nov 2018 00:12:51 -0500 Subject: [PATCH 12/18] mock tendermint --- x/mock/simulation/mock_tendermint.go | 198 +++++++++++++++++++++++++++ x/mock/simulation/simulate.go | 108 ++------------- x/mock/simulation/validator.go | 96 ------------- 3 files changed, 206 insertions(+), 196 deletions(-) create mode 100644 x/mock/simulation/mock_tendermint.go delete mode 100644 x/mock/simulation/validator.go diff --git a/x/mock/simulation/mock_tendermint.go b/x/mock/simulation/mock_tendermint.go new file mode 100644 index 000000000000..ee381cbb6184 --- /dev/null +++ b/x/mock/simulation/mock_tendermint.go @@ -0,0 +1,198 @@ +package simulation + +import ( + "fmt" + "math/rand" + "sort" + "testing" + "time" + + abci "github.com/tendermint/tendermint/abci/types" + cmn "github.com/tendermint/tendermint/libs/common" + tmtypes "github.com/tendermint/tendermint/types" +) + +type mockValidator struct { + val abci.ValidatorUpdate + livenessState int +} + +type mockValidators map[string]mockValidator + +// get mockValidators from abci validators +func newMockValidators(abciVals abci.ValidatorUpdate) mockValidators { + + validators = make(mockValidators) + for _, validator := range abciVals { + str := fmt.Sprintf("%v", validator.PubKey) + liveliness := GetMemberOfInitialState(r, + params.InitialLivenessWeightings) + + validators[str] = mockValidator{ + val: validator, + livenessState: liveliness, + } + } + + return validators +} + +// TODO describe usage +func (vals mockValidators) getKeys() []string { + keys := make([]string, len(validators)) + i := 0 + for key := range validators { + keys[i] = key + i++ + } + sort.Strings(keys) + return keys +} + +//_________________________________________________________________________________ + +// randomProposer picks a random proposer from the current validator set +func randomProposer(r *rand.Rand, validators map[string]mockValidator) cmn.HexBytes { + keys := validators.getKeys() + if len(keys) == 0 { + return nil + } + key := keys[r.Intn(len(keys))] + proposer := validators[key].val + pk, err := tmtypes.PB2TM.PubKey(proposer.PubKey) + if err != nil { + panic(err) + } + return pk.Address() +} + +// updateValidators mimicks Tendermint's update logic +// nolint: unparam +func updateValidators(tb testing.TB, r *rand.Rand, params Params, + current map[string]mockValidator, updates []abci.ValidatorUpdate, + event func(string)) map[string]mockValidator { + + for _, update := range updates { + str := fmt.Sprintf("%v", update.PubKey) + if update.Power == 0 { + if _, ok := current[str]; !ok { + tb.Fatalf("tried to delete a nonexistent validator") + } + event("endblock/validatorupdates/kicked") + delete(current, str) + + } else if mVal, ok := current[str]; ok { + // validator already exists + mVal.val = update + event("endblock/validatorupdates/updated") + } else { + // Set this new validator + current[str] = mockValidator{ + update, + GetMemberOfInitialState(r, params.InitialLivenessWeightings), + } + event("endblock/validatorupdates/added") + } + } + + return current +} + +// RandomRequestBeginBlock generates a list of signing validators according to +// the provided list of validators, signing fraction, and evidence fraction +func RandomRequestBeginBlock(r *rand.Rand, params Params, + validators mockValidators, pastTimes []time.Time, + pastVoteInfos [][]abci.VoteInfo, + event func(string), header abci.Header) abci.RequestBeginBlock { + + if len(validators) == 0 { + return abci.RequestBeginBlock{ + Header: header, + } + } + + voteInfos := make([]abci.VoteInfo, len(validators)) + for i, key := range validators.getKeys() { + mVal := validators[key] + mVal.livenessState = params.LivenessTransitionMatrix.NextState(r, mVal.livenessState) + signed := true + + if mVal.livenessState == 1 { + // spotty connection, 50% probability of success + // See https://github.com/golang/go/issues/23804#issuecomment-365370418 + // for reasoning behind computing like this + signed = r.Int63()%2 == 0 + } else if mVal.livenessState == 2 { + // offline + signed = false + } + + if signed { + event("beginblock/signing/signed") + } else { + event("beginblock/signing/missed") + } + + pubkey, err := tmtypes.PB2TM.PubKey(mVal.val.PubKey) + if err != nil { + panic(err) + } + voteInfos[i] = abci.VoteInfo{ + Validator: abci.Validator{ + Address: pubkey.Address(), + Power: mVal.val.Power, + }, + SignedLastBlock: signed, + } + } + + // return if no past times + if len(pastTimes) <= 0 { + return abci.RequestBeginBlock{ + Header: header, + LastCommitInfo: abci.LastCommitInfo{ + Votes: voteInfos, + }, + } + } + + // TODO: Determine capacity before allocation + evidence := make([]abci.Evidence, 0) + for r.Float64() < params.EvidenceFraction { + + height := header.Height + time := header.Time + vals := voteInfos + + if r.Float64() < params.PastEvidenceFraction { + height = int64(r.Intn(int(header.Height) - 1)) + time = pastTimes[height] + vals = pastVoteInfos[height] + } + validator := vals[r.Intn(len(vals))].Validator + + var totalVotingPower int64 + for _, val := range vals { + totalVotingPower += val.Validator.Power + } + + evidence = append(evidence, + abci.Evidence{ + Type: tmtypes.ABCIEvidenceTypeDuplicateVote, + Validator: validator, + Height: height, + Time: time, + TotalVotingPower: totalVotingPower, + }, + ) + event("beginblock/evidence") + } + + return abci.RequestBeginBlock{ + Header: header, + LastCommitInfo: abci.LastCommitInfo{ + Votes: voteInfos, + }, + ByzantineValidators: evidence, + } +} diff --git a/x/mock/simulation/simulate.go b/x/mock/simulation/simulate.go index d9b5176f8cd2..dc7c12bfe9f4 100644 --- a/x/mock/simulation/simulate.go +++ b/x/mock/simulation/simulate.go @@ -13,7 +13,6 @@ import ( "time" abci "github.com/tendermint/tendermint/abci/types" - tmtypes "github.com/tendermint/tendermint/types" "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" @@ -25,7 +24,7 @@ type RandSetup func(r *rand.Rand, accounts []Account) // Simulate tests application by sending random messages. func Simulate(t *testing.T, app *baseapp.BaseApp, appStateFn func(r *rand.Rand, accs []Account) json.RawMessage, - ops []WeightedOperation, setups []RandSetup, + ops WeightedOperations, setups []RandSetup, invariants Invariants, numBlocks int, blockSize int, commit bool) error { time := time.Now().UnixNano() @@ -60,13 +59,17 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, // in case we have to end early, don't os.Exit so that we can run cleanup code. stopEarly := false testingMode, t, b := getTestingMode(tb) - fmt.Printf("Starting SimulateFromSeed with randomness created with seed %d\n", int(seed)) + fmt.Printf("Starting SimulateFromSeed with randomness "+ + "created with seed %d\n", int(seed)) + r := rand.New(rand.NewSource(seed)) params := RandomParams(r) // := DefaultParams() fmt.Printf("Randomized simulation params: %+v\n", params) + timestamp := RandTimestamp(r) fmt.Printf("Starting the simulation from time %v, unixtime %v\n", timestamp.UTC().Format(time.UnixDate), timestamp.Unix()) + timeDiff := maxTimePerBlock - minTimePerBlock accs := RandomAccounts(r, params.NumKeys) @@ -232,10 +235,10 @@ type blockSimFn func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []Account, header abci.Header, logWriter func(string)) (opCount int) // Returns a function to simulate blocks. Written like this to avoid constant -// parameters being passed everytime, to minimize memory overhead +// parameters being passed everytime, to minimize memory overhead. func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params Params, event func(string), invariants Invariants, ops WeightedOperations, - operationQueue map[int][]Operation, timeOperationQueue []FutureOperation, + operationQueue OperationQueue, timeOperationQueue []FutureOperation, totalNumBlocks int, avgBlockSize int, displayLogs func()) blockSimFn { var lastBlocksizeState = 0 // state for [4 * uniform distribution] @@ -323,98 +326,3 @@ func runQueuedTimeOperations(queueOps []FutureOperation, } return numOpsRan } - -// RandomRequestBeginBlock generates a list of signing validators according to -// the provided list of validators, signing fraction, and evidence fraction -func RandomRequestBeginBlock(r *rand.Rand, params Params, - validators map[string]mockValidator, pastTimes []time.Time, - pastVoteInfos [][]abci.VoteInfo, - event func(string), header abci.Header) abci.RequestBeginBlock { - - if len(validators) == 0 { - return abci.RequestBeginBlock{ - Header: header, - } - } - voteInfos := make([]abci.VoteInfo, len(validators)) - - for i, key := range getKeys(validators) { - mVal := validators[key] - mVal.livenessState = params.LivenessTransitionMatrix.NextState(r, mVal.livenessState) - signed := true - - if mVal.livenessState == 1 { - // spotty connection, 50% probability of success - // See https://github.com/golang/go/issues/23804#issuecomment-365370418 - // for reasoning behind computing like this - signed = r.Int63()%2 == 0 - } else if mVal.livenessState == 2 { - // offline - signed = false - } - - if signed { - event("beginblock/signing/signed") - } else { - event("beginblock/signing/missed") - } - - pubkey, err := tmtypes.PB2TM.PubKey(mVal.val.PubKey) - if err != nil { - panic(err) - } - voteInfos[i] = abci.VoteInfo{ - Validator: abci.Validator{ - Address: pubkey.Address(), - Power: mVal.val.Power, - }, - SignedLastBlock: signed, - } - } - - // return if no past times - if len(pastTimes) <= 0 { - return abci.RequestBeginBlock{ - Header: header, - LastCommitInfo: abci.LastCommitInfo{ - Votes: voteInfos, - }, - } - } - - // TODO: Determine capacity before allocation - evidence := make([]abci.Evidence, 0) - for r.Float64() < params.EvidenceFraction { - height := header.Height - time := header.Time - vals := voteInfos - if r.Float64() < params.PastEvidenceFraction { - height = int64(r.Intn(int(header.Height) - 1)) - time = pastTimes[height] - vals = pastVoteInfos[height] - } - validator := vals[r.Intn(len(vals))].Validator - var totalVotingPower int64 - - for _, val := range vals { - totalVotingPower += val.Validator.Power - } - - evidence = append(evidence, abci.Evidence{ - Type: tmtypes.ABCIEvidenceTypeDuplicateVote, - Validator: validator, - Height: height, - Time: time, - TotalVotingPower: totalVotingPower, - }) - event("beginblock/evidence") - } - - return abci.RequestBeginBlock{ - Header: header, - LastCommitInfo: abci.LastCommitInfo{ - Votes: voteInfos, - }, - ByzantineValidators: evidence, - } -} diff --git a/x/mock/simulation/validator.go b/x/mock/simulation/validator.go deleted file mode 100644 index 7a2213bdb727..000000000000 --- a/x/mock/simulation/validator.go +++ /dev/null @@ -1,96 +0,0 @@ -package simulation - -import ( - "fmt" - "math/rand" - "sort" - "testing" - - abci "github.com/tendermint/tendermint/abci/types" - cmn "github.com/tendermint/tendermint/libs/common" - tmtypes "github.com/tendermint/tendermint/types" -) - -type mockValidator struct { - val abci.ValidatorUpdate - livenessState int -} - -type mockValidators map[string]mockValidator - -// get mockValidators from abci validators -func newMockValidators(abciVals abci.ValidatorUpdate) mockValidators { - - validators = make(mockValidators) - for _, validator := range abciVals { - str := fmt.Sprintf("%v", validator.PubKey) - liveliness := GetMemberOfInitialState(r, - params.InitialLivenessWeightings) - - validators[str] = mockValidator{ - val: validator, - livenessState: liveliness, - } - } - - return validators -} - -// TODO describe usage -func getKeys(validators map[string]mockValidator) []string { - keys := make([]string, len(validators)) - i := 0 - for key := range validators { - keys[i] = key - i++ - } - sort.Strings(keys) - return keys -} - -// randomProposer picks a random proposer from the current validator set -func randomProposer(r *rand.Rand, validators map[string]mockValidator) cmn.HexBytes { - keys := getKeys(validators) - if len(keys) == 0 { - return nil - } - key := keys[r.Intn(len(keys))] - proposer := validators[key].val - pk, err := tmtypes.PB2TM.PubKey(proposer.PubKey) - if err != nil { - panic(err) - } - return pk.Address() -} - -// updateValidators mimicks Tendermint's update logic -// nolint: unparam -func updateValidators(tb testing.TB, r *rand.Rand, params Params, - current map[string]mockValidator, updates []abci.ValidatorUpdate, - event func(string)) map[string]mockValidator { - - for _, update := range updates { - str := fmt.Sprintf("%v", update.PubKey) - if update.Power == 0 { - if _, ok := current[str]; !ok { - tb.Fatalf("tried to delete a nonexistent validator") - } - event("endblock/validatorupdates/kicked") - delete(current, str) - - } else if mVal, ok := current[str]; ok { - // validator already exists - mVal.val = update - event("endblock/validatorupdates/updated") - } else { - // Set this new validator - current[str] = mockValidator{ - update, - GetMemberOfInitialState(r, params.InitialLivenessWeightings), - } - event("endblock/validatorupdates/added") - } - } - - return current -} From eb16a01666e79f38c73c3fdde3d16370539aa0b9 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 8 Nov 2018 00:22:16 -0500 Subject: [PATCH 13/18] util cleanup --- x/mock/simulation/mock_tendermint.go | 2 + x/mock/simulation/util.go | 83 +++++++++++++++------------- 2 files changed, 47 insertions(+), 38 deletions(-) diff --git a/x/mock/simulation/mock_tendermint.go b/x/mock/simulation/mock_tendermint.go index ee381cbb6184..a98e8961414d 100644 --- a/x/mock/simulation/mock_tendermint.go +++ b/x/mock/simulation/mock_tendermint.go @@ -74,6 +74,7 @@ func updateValidators(tb testing.TB, r *rand.Rand, params Params, for _, update := range updates { str := fmt.Sprintf("%v", update.PubKey) + if update.Power == 0 { if _, ok := current[str]; !ok { tb.Fatalf("tried to delete a nonexistent validator") @@ -85,6 +86,7 @@ func updateValidators(tb testing.TB, r *rand.Rand, params Params, // validator already exists mVal.val = update event("endblock/validatorupdates/updated") + } else { // Set this new validator current[str] = mockValidator{ diff --git a/x/mock/simulation/util.go b/x/mock/simulation/util.go index e115cde2492c..6b2c01052d84 100644 --- a/x/mock/simulation/util.go +++ b/x/mock/simulation/util.go @@ -34,54 +34,61 @@ func DisplayEvents(events map[string]uint) { } // Builds a function to add logs for this particular block -func addLogMessage(testingmode bool, blockLogBuilders []*strings.Builder, height int) func(string) { - if testingmode { - blockLogBuilders[height] = &strings.Builder{} - return func(x string) { - (*blockLogBuilders[height]).WriteString(x) - (*blockLogBuilders[height]).WriteString("\n") - } +func addLogMessage(testingmode bool, + blockLogBuilders []*strings.Builder, height int) func(string) { + + if !testingmode { + return func(_ string) {} + } + + blockLogBuilders[height] = &strings.Builder{} + return func(x string) { + (*blockLogBuilders[height]).WriteString(x) + (*blockLogBuilders[height]).WriteString("\n") } - return func(x string) {} } // Creates a function to print out the logs func logPrinter(testingmode bool, logs []*strings.Builder) func() { - if testingmode { - return func() { - numLoggers := 0 - for i := 0; i < len(logs); i++ { - // We're passed the last created block - if logs[i] == nil { - numLoggers = i - break - } + if !testingmode { + return func() {} + } + + return func() { + numLoggers := 0 + for i := 0; i < len(logs); i++ { + // We're passed the last created block + if logs[i] == nil { + numLoggers = i + break } - var f *os.File - if numLoggers > 10 { + } - fileName := fmt.Sprintf("simulation_log_%s.txt", - time.Now().Format("2006-01-02 15:04:05")) + var f *os.File + if numLoggers > 10 { + fileName := fmt.Sprintf("simulation_log_%s.txt", + time.Now().Format("2006-01-02 15:04:05")) + fmt.Printf("Too many logs to display, instead writing to %s\n", + fileName) + f, _ = os.Create(fileName) + } - fmt.Printf("Too many logs to display, instead writing to %s\n", fileName) - f, _ = os.Create(fileName) + for i := 0; i < numLoggers; i++ { + if f == nil { + fmt.Printf("Begin block %d\n", i+1) + fmt.Println((*logs[i]).String()) + continue } - for i := 0; i < numLoggers; i++ { - if f != nil { - _, err := f.WriteString(fmt.Sprintf("Begin block %d\n", i+1)) - if err != nil { - panic("Failed to write logs to file") - } - _, err = f.WriteString((*logs[i]).String()) - if err != nil { - panic("Failed to write logs to file") - } - } else { - fmt.Printf("Begin block %d\n", i+1) - fmt.Println((*logs[i]).String()) - } + + _, err := f.WriteString(fmt.Sprintf("Begin block %d\n", i+1)) + if err != nil { + panic("Failed to write logs to file") + } + + _, err = f.WriteString((*logs[i]).String()) + if err != nil { + panic("Failed to write logs to file") } } } - return func() {} } From 5f289e5fdd7fa80370e94215c7793909b4f936bc Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 8 Nov 2018 00:54:12 -0500 Subject: [PATCH 14/18] event stats object, more general cleanup --- x/mock/simulation/event_stats.go | 33 +++++++++++++ x/mock/simulation/operation.go | 14 ++++-- x/mock/simulation/simulate.go | 84 ++++++++++++++++---------------- x/mock/simulation/util.go | 14 ------ 4 files changed, 85 insertions(+), 60 deletions(-) create mode 100644 x/mock/simulation/event_stats.go diff --git a/x/mock/simulation/event_stats.go b/x/mock/simulation/event_stats.go new file mode 100644 index 000000000000..5276c533bc5d --- /dev/null +++ b/x/mock/simulation/event_stats.go @@ -0,0 +1,33 @@ +package simulation + +import ( + "fmt" + "sort" +) + +type eventStats map[string]uint + +func newEventStats() eventStats { + events := make(map[string]uint) + return events + event := func(what string) { + events[what]++ + } +} + +func (es *eventStats) tally(eventDesc string) { + es[eventDesc]++ +} + +// Pretty-print events as a table +func (es eventStats) Print() { + var keys []string + for key := range es { + keys = append(keys, key) + } + sort.Strings(keys) + fmt.Printf("Event statistics: \n") + for _, key := range keys { + fmt.Printf(" % 60s => %d\n", key, es[key]) + } +} diff --git a/x/mock/simulation/operation.go b/x/mock/simulation/operation.go index 27757df2d573..7710cf5c9152 100644 --- a/x/mock/simulation/operation.go +++ b/x/mock/simulation/operation.go @@ -18,17 +18,21 @@ import ( // // Operations can optionally provide a list of "FutureOperations" to run later // These will be ran at the beginning of the corresponding block. -type Operation func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, - accounts []Account, event func(string)) ( - action string, futureOperations []FutureOperation, err error) +type Operation func(r *rand.Rand, app *baseapp.BaseApp, + ctx sdk.Context, accounts []Account, event func(string)) ( + action string, futureOps []FutureOperation, err error) // queue of operations type OperationQueue map[int][]Operation +func newOperationQueue() OperationQueue { + operationQueue := make(OperationQueue) + return operationQueue +} + // adds all future operations into the operation queue. func queueOperations(queuedOps OperationQueue, - queuedTimeOps []FutureOperation, - futureOps []FutureOperation) { + queuedTimeOps []FutureOperation, futureOps []FutureOperation) { if futureOps == nil { return diff --git a/x/mock/simulation/simulate.go b/x/mock/simulation/simulate.go index dc7c12bfe9f4..e6749f4e196c 100644 --- a/x/mock/simulation/simulate.go +++ b/x/mock/simulation/simulate.go @@ -21,10 +21,12 @@ import ( // RandSetup performs the random setup the mock module needs. type RandSetup func(r *rand.Rand, accounts []Account) +// AppStateFn returns the app state json bytes +type AppStateFn func(r *rand.Rand, accs []Account) json.RawMessage + // Simulate tests application by sending random messages. func Simulate(t *testing.T, app *baseapp.BaseApp, - appStateFn func(r *rand.Rand, accs []Account) json.RawMessage, - ops WeightedOperations, setups []RandSetup, + appStateFn AppStateFn, ops WeightedOperations, setups []RandSetup, invariants Invariants, numBlocks int, blockSize int, commit bool) error { time := time.Now().UnixNano() @@ -33,9 +35,9 @@ func Simulate(t *testing.T, app *baseapp.BaseApp, } // initialize the chain for the simulation -func initChain(r *rand.Rand, params Params, - accounts []Account, setups []RandSetup, app *baseapp.BaseApp, - appStateFn func(r *rand.Rand, accounts []Account) json.RawMessage) mockValidators { +func initChain(r *rand.Rand, params Params, accounts []Account, + setups []RandSetup, app *baseapp.BaseApp, + appStateFn AppStateFn) mockValidators { req := abci.RequestInitChain{ AppStateBytes: appStateFn(r, accounts), @@ -51,9 +53,10 @@ func initChain(r *rand.Rand, params Params, // SimulateFromSeed tests an application by running the provided // operations, testing the provided invariants, but using the provided seed. +// TODO split this monster function up func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, - appStateFn func(r *rand.Rand, accs []Account) json.RawMessage, - seed int64, ops WeightedOperations, setups []RandSetup, invariants Invariants, + appStateFn AppStateFn, seed int64, ops WeightedOperations, + setups []RandSetup, invariants Invariants, numBlocks int, blockSize int, commit bool) (simError error) { // in case we have to end early, don't os.Exit so that we can run cleanup code. @@ -71,20 +74,13 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, timestamp.UTC().Format(time.UnixDate), timestamp.Unix()) timeDiff := maxTimePerBlock - minTimePerBlock - accs := RandomAccounts(r, params.NumKeys) - - // Setup event stats - events := make(map[string]uint) - event := func(what string) { - events[what]++ - } - + eventStats := newEventStats() validators := initChain(r, params, accs, setups, app, appStateFn) - // Second variable to keep pending validator set (delayed one block since TM 0.24) - // Initially this is the same as the initial validator set - nextValidators := validators + // Second variable to keep pending validator set (delayed one block since + // TM 0.24) Initially this is the same as the initial validator set + nextValidators := validators() header := abci.Header{ Height: 1, @@ -108,10 +104,10 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, var pastVoteInfos [][]abci.VoteInfo request := RandomRequestBeginBlock(r, params, - validators, pastTimes, pastVoteInfos, event, header) + validators, pastTimes, pastVoteInfos, eventStats.tally, header) // These are operations which have been queued by previous operations - operationQueue := make(map[int][]Operation) + operationQueue := newOperationQueue() timeOperationQueue := []FutureOperation{} var blockLogBuilders []*strings.Builder @@ -120,7 +116,7 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, } displayLogs := logPrinter(testingMode, blockLogBuilders) blockSimulator := createBlockSimulator( - testingMode, tb, t, params, event, invariants, + testingMode, tb, t, params, eventStats.tally, invariants, ops, operationQueue, timeOperationQueue, numBlocks, blockSize, displayLogs) @@ -143,6 +139,7 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, // TODO split up the contents of this for loop into new functions for i := 0; i < numBlocks && !stopEarly; i++ { + // Log the header time for future lookup pastTimes = append(pastTimes, header.Time) pastVoteInfos = append(pastVoteInfos, request.LastCommitInfo.Votes) @@ -155,7 +152,6 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, app.BeginBlock(request) if testingMode { - // Make sure invariants hold at beginning of block invariants.assertAll(t, app, "BeginBlock", displayLogs) } @@ -166,13 +162,14 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, numQueuedOpsRan := runQueuedOperations( operationQueue, int(header.Height), tb, r, app, ctx, accs, logWriter, - displayLogs, event) + displayLogs, eventStats.tally) + numQueuedTimeOpsRan := runQueuedTimeOperations( timeOperationQueue, header.Time, tb, r, app, ctx, accs, - logWriter, displayLogs, event) + logWriter, displayLogs, eventStats.tally) + if testingMode && onOperation { - // Make sure invariants hold at end of queued operations invariants.assertAll(t, app, "QueuedOperations", displayLogs) } @@ -180,7 +177,6 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, operations := blockSimulator(r, app, ctx, accs, header, logWriter) opCount += operations + numQueuedOpsRan + numQueuedTimeOpsRan if testingMode { - // Make sure invariants hold at the operation invariants.assertAll(t, app, "StandardOperations", displayLogs) } @@ -194,7 +190,6 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, logWriter("EndBlock") if testingMode { - // Make sure invariants hold at end of block invariants.assertAll(t, app, "EndBlock", displayLogs) } if commit { @@ -208,24 +203,27 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, break } - // Generate a random RequestBeginBlock with the current validator set for the next block + // Generate a random RequestBeginBlock with the current validator set + // for the next block request = RandomRequestBeginBlock(r, params, validators, - pastTimes, pastVoteInfos, event, header) + pastTimes, pastVoteInfos, eventStats.tally, header) - // Update the validator set, which will be reflected in the application on the next block + // Update the validator set, which will be reflected in the application + // on the next block validators = nextValidators nextValidators = updateValidators(tb, r, params, - validators, res.ValidatorUpdates, event) + validators, res.ValidatorUpdates, eventStats.tally) } + if stopEarly { - DisplayEvents(events) - return + eventStats.Print() + return simError } fmt.Printf("\nSimulation complete. Final height (blocks): %d, "+ "final time (seconds), : %v, operations ran %d\n", header.Height, header.Time, opCount) - DisplayEvents(events) + eventStats.Print() return nil } @@ -252,7 +250,8 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params header.Height, totalNumBlocks, opCount, blocksize) lastBlocksizeState, blocksize = getBlockSize(r, params, lastBlocksizeState, avgBlockSize) - for j := 0; j < blocksize; j++ { + for i := 0; i < blocksize; i++ { + logUpdate, futureOps, err := selectOp(r)(r, app, ctx, accounts, event) logWriter(logUpdate) if err != nil { @@ -280,9 +279,9 @@ func createBlockSimulator(testingMode bool, tb testing.TB, t *testing.T, params // nolint: errcheck func runQueuedOperations(queueOps map[int][]Operation, - height int, tb testing.TB, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, - accounts []Account, logWriter func(string), - displayLogs func(), event func(string)) (numOpsRan int) { + height int, tb testing.TB, r *rand.Rand, app *baseapp.BaseApp, + ctx sdk.Context, accounts []Account, logWriter func(string), + displayLogs func(), tallyEvent func(string)) (numOpsRan int) { queuedOp, ok := queueOps[height] if !ok { @@ -291,10 +290,11 @@ func runQueuedOperations(queueOps map[int][]Operation, numOpsRan = len(queuedOp) for i := 0; i < numOpsRan; i++ { + // For now, queued operations cannot queue more operations. // If a need arises for us to support queued messages to queue more messages, this can // be changed. - logUpdate, _, err := queuedOp[i](r, app, ctx, accounts, event) + logUpdate, _, err := queuedOp[i](r, app, ctx, accounts, tallyEvent) logWriter(logUpdate) if err != nil { displayLogs() @@ -308,19 +308,21 @@ func runQueuedOperations(queueOps map[int][]Operation, func runQueuedTimeOperations(queueOps []FutureOperation, currentTime time.Time, tb testing.TB, r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []Account, - logWriter func(string), displayLogs func(), event func(string)) (numOpsRan int) { + logWriter func(string), displayLogs func(), tallyEvent func(string)) (numOpsRan int) { numOpsRan = 0 for len(queueOps) > 0 && currentTime.After(queueOps[0].BlockTime) { + // For now, queued operations cannot queue more operations. // If a need arises for us to support queued messages to queue more messages, this can // be changed. - logUpdate, _, err := queueOps[0].Op(r, app, ctx, accounts, event) + logUpdate, _, err := queueOps[0].Op(r, app, ctx, accounts, tallyEvent) logWriter(logUpdate) if err != nil { displayLogs() tb.FailNow() } + queueOps = queueOps[1:] numOpsRan++ } diff --git a/x/mock/simulation/util.go b/x/mock/simulation/util.go index 6b2c01052d84..02ef56d2e703 100644 --- a/x/mock/simulation/util.go +++ b/x/mock/simulation/util.go @@ -3,7 +3,6 @@ package simulation import ( "fmt" "os" - "sort" "strings" "testing" "time" @@ -20,19 +19,6 @@ func getTestingMode(tb testing.TB) (testingMode bool, t *testing.T, b *testing.B return } -// Pretty-print events as a table -func DisplayEvents(events map[string]uint) { - var keys []string - for key := range events { - keys = append(keys, key) - } - sort.Strings(keys) - fmt.Printf("Event statistics: \n") - for _, key := range keys { - fmt.Printf(" % 60s => %d\n", key, events[key]) - } -} - // Builds a function to add logs for this particular block func addLogMessage(testingmode bool, blockLogBuilders []*strings.Builder, height int) func(string) { From a9ef83bc4a0de7b1672ae3ab513007d613f63a72 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 8 Nov 2018 01:57:37 -0500 Subject: [PATCH 15/18] compiling --- x/mock/simulation/event_stats.go | 5 +---- x/mock/simulation/mock_tendermint.go | 15 ++++++++------- x/mock/simulation/operation.go | 8 ++++---- x/mock/simulation/simulate.go | 10 +++++----- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/x/mock/simulation/event_stats.go b/x/mock/simulation/event_stats.go index 5276c533bc5d..f54eef4cc99a 100644 --- a/x/mock/simulation/event_stats.go +++ b/x/mock/simulation/event_stats.go @@ -10,12 +10,9 @@ type eventStats map[string]uint func newEventStats() eventStats { events := make(map[string]uint) return events - event := func(what string) { - events[what]++ - } } -func (es *eventStats) tally(eventDesc string) { +func (es eventStats) tally(eventDesc string) { es[eventDesc]++ } diff --git a/x/mock/simulation/mock_tendermint.go b/x/mock/simulation/mock_tendermint.go index a98e8961414d..2ddac2e79421 100644 --- a/x/mock/simulation/mock_tendermint.go +++ b/x/mock/simulation/mock_tendermint.go @@ -20,9 +20,10 @@ type mockValidator struct { type mockValidators map[string]mockValidator // get mockValidators from abci validators -func newMockValidators(abciVals abci.ValidatorUpdate) mockValidators { +func newMockValidators(r *rand.Rand, abciVals []abci.ValidatorUpdate, + params Params) mockValidators { - validators = make(mockValidators) + validators := make(mockValidators) for _, validator := range abciVals { str := fmt.Sprintf("%v", validator.PubKey) liveliness := GetMemberOfInitialState(r, @@ -39,9 +40,9 @@ func newMockValidators(abciVals abci.ValidatorUpdate) mockValidators { // TODO describe usage func (vals mockValidators) getKeys() []string { - keys := make([]string, len(validators)) + keys := make([]string, len(vals)) i := 0 - for key := range validators { + for key := range vals { keys[i] = key i++ } @@ -52,13 +53,13 @@ func (vals mockValidators) getKeys() []string { //_________________________________________________________________________________ // randomProposer picks a random proposer from the current validator set -func randomProposer(r *rand.Rand, validators map[string]mockValidator) cmn.HexBytes { - keys := validators.getKeys() +func (vals mockValidators) randomProposer(r *rand.Rand) cmn.HexBytes { + keys := vals.getKeys() if len(keys) == 0 { return nil } key := keys[r.Intn(len(keys))] - proposer := validators[key].val + proposer := vals[key].val pk, err := tmtypes.PB2TM.PubKey(proposer.PubKey) if err != nil { panic(err) diff --git a/x/mock/simulation/operation.go b/x/mock/simulation/operation.go index 7710cf5c9152..00237e03ee31 100644 --- a/x/mock/simulation/operation.go +++ b/x/mock/simulation/operation.go @@ -86,17 +86,17 @@ type WeightedOperation struct { // WeightedOperations is the group of all weighted operations to simulate. type WeightedOperations []WeightedOperation -func (w WeightedOperations) totalWeight() int { +func (ops WeightedOperations) totalWeight() int { totalOpWeight := 0 - for i := 0; i < len(w); i++ { - totalOpWeight += w[i].Weight + for _, op := range ops { + totalOpWeight += op.Weight } return totalOpWeight } type selectOpFn func(r *rand.Rand) Operation -func (w WeightedOperations) getSelectOpFn() selectOpFn { +func (ops WeightedOperations) getSelectOpFn() selectOpFn { totalOpWeight := ops.totalWeight() return func(r *rand.Rand) Operation { x := r.Intn(totalOpWeight) diff --git a/x/mock/simulation/simulate.go b/x/mock/simulation/simulate.go index e6749f4e196c..ac0e5d3f7f11 100644 --- a/x/mock/simulation/simulate.go +++ b/x/mock/simulation/simulate.go @@ -43,7 +43,7 @@ func initChain(r *rand.Rand, params Params, accounts []Account, AppStateBytes: appStateFn(r, accounts), } res := app.InitChain(req) - validators = newMockValidators(res.Validators) + validators := newMockValidators(r, res.Validators, params) for i := 0; i < len(setups); i++ { setups[i](r, accounts) @@ -76,16 +76,16 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, timeDiff := maxTimePerBlock - minTimePerBlock accs := RandomAccounts(r, params.NumKeys) eventStats := newEventStats() - validators := initChain(r, params, accs, setups, app, appStateFn) // Second variable to keep pending validator set (delayed one block since // TM 0.24) Initially this is the same as the initial validator set - nextValidators := validators() + validators := initChain(r, params, accs, setups, app, appStateFn) + nextValidators := validators header := abci.Header{ Height: 1, Time: timestamp, - ProposerAddress: randomProposer(r, validators), + ProposerAddress: validators.randomProposer(r), } opCount := 0 @@ -186,7 +186,7 @@ func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, time.Duration(minTimePerBlock) * time.Second) header.Time = header.Time.Add( time.Duration(int64(r.Intn(int(timeDiff)))) * time.Second) - header.ProposerAddress = randomProposer(r, validators) + header.ProposerAddress = validators.randomProposer(r) logWriter("EndBlock") if testingMode { From bbf574cedbcb5a2cf0e734c68ff40b96ae624fbf Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 8 Nov 2018 02:19:08 -0500 Subject: [PATCH 16/18] pending --- PENDING.md | 1 + 1 file changed, 1 insertion(+) diff --git a/PENDING.md b/PENDING.md index 4d87301f0d5b..3f632a8505f4 100644 --- a/PENDING.md +++ b/PENDING.md @@ -35,6 +35,7 @@ IMPROVEMENTS * Gaia * SDK + - [mock/simulation] [\#2720] major cleanup, introduction of helper objects, reorganization * Tendermint From bfc7944b55382c0749fecf294289e3b65792c4dc Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 8 Nov 2018 10:32:53 -0500 Subject: [PATCH 17/18] val comments --- x/mock/simulation/doc.go | 11 +++++------ x/mock/simulation/params.go | 23 ----------------------- x/mock/simulation/transition_matrix.go | 2 +- x/mock/simulation/util.go | 24 ++++++++++++++++++++++++ 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/x/mock/simulation/doc.go b/x/mock/simulation/doc.go index 2febd9e4719d..ff292bd388fc 100644 --- a/x/mock/simulation/doc.go +++ b/x/mock/simulation/doc.go @@ -2,12 +2,11 @@ Package simulation implements a simulation framework for any state machine built on the SDK which utilizes auth. -It is primarily intended for fuzz testing the integration of modules. It will +It is primarily intended for fuzz testing the integration of modules. It will test that the provided operations are interoperable, and that the desired -invariants hold. It can additionally be used to detect what the performance +invariants hold. It can additionally be used to detect what the performance benchmarks in the system are, by using benchmarking mode and cpu / mem -profiling. If it detects a failure, it provides the entire log of what was -ran, +profiling. If it detects a failure, it provides the entire log of what was ran. The simulator takes as input: a random seed, the set of operations to run, the invariants to test, and additional parameters to configure how long to run, and @@ -15,12 +14,12 @@ misc. parameters that affect simulation speed. It is intended that every module provides a list of Operations which will randomly create and run a message / tx in a manner that is interesting to fuzz, -and verify that the state transition was executed as expected. Each module +and verify that the state transition was executed as expected. Each module should additionally provide methods to assert that the desired invariants hold. Then to perform a randomized simulation, select the set of desired operations, the weightings for each, the invariants you want to test, and how long to run -it for. Then run simulation.Simulate! The simulator will handle things like +it for. Then run simulation.Simulate! The simulator will handle things like ensuring that validators periodically double signing, or go offline. */ package simulation diff --git a/x/mock/simulation/params.go b/x/mock/simulation/params.go index c46ab236bf27..8499e6c1189b 100644 --- a/x/mock/simulation/params.go +++ b/x/mock/simulation/params.go @@ -44,29 +44,6 @@ type Params struct { BlockSizeTransitionMatrix TransitionMatrix } -// getBlockSize returns a block size as determined from the transition matrix. -// It targets making average block size the provided parameter. The three -// states it moves between are: -// - "over stuffed" blocks with average size of 2 * avgblocksize, -// - normal sized blocks, hitting avgBlocksize on average, -// - and empty blocks, with no txs / only txs scheduled from the past. -func getBlockSize(r *rand.Rand, params Params, - lastBlockSizeState, avgBlockSize int) (state, blocksize int) { - - // TODO: Make default blocksize transition matrix actually make the average - // blocksize equal to avgBlockSize. - state = params.BlockSizeTransitionMatrix.NextState(r, lastBlockSizeState) - switch state { - case 0: - blocksize = r.Intn(avgBlockSize * 4) - case 1: - blocksize = r.Intn(avgBlockSize * 2) - default: - blocksize = 0 - } - return state, blocksize -} - // Return default simulation parameters func DefaultParams() Params { return Params{ diff --git a/x/mock/simulation/transition_matrix.go b/x/mock/simulation/transition_matrix.go index 97cd307e56cd..f7d713775d38 100644 --- a/x/mock/simulation/transition_matrix.go +++ b/x/mock/simulation/transition_matrix.go @@ -51,7 +51,7 @@ func (t TransitionMatrix) NextState(r *rand.Rand, i int) int { } // GetMemberOfInitialState takes an initial array of weights, of size n. -// It returns a weighted random number in [0,n]. +// It returns a weighted random number in [0,n). func GetMemberOfInitialState(r *rand.Rand, weights []int) int { n := len(weights) total := 0 diff --git a/x/mock/simulation/util.go b/x/mock/simulation/util.go index 02ef56d2e703..f10364aaaa38 100644 --- a/x/mock/simulation/util.go +++ b/x/mock/simulation/util.go @@ -2,6 +2,7 @@ package simulation import ( "fmt" + "math/rand" "os" "strings" "testing" @@ -78,3 +79,26 @@ func logPrinter(testingmode bool, logs []*strings.Builder) func() { } } } + +// getBlockSize returns a block size as determined from the transition matrix. +// It targets making average block size the provided parameter. The three +// states it moves between are: +// - "over stuffed" blocks with average size of 2 * avgblocksize, +// - normal sized blocks, hitting avgBlocksize on average, +// - and empty blocks, with no txs / only txs scheduled from the past. +func getBlockSize(r *rand.Rand, params Params, + lastBlockSizeState, avgBlockSize int) (state, blocksize int) { + + // TODO: Make default blocksize transition matrix actually make the average + // blocksize equal to avgBlockSize. + state = params.BlockSizeTransitionMatrix.NextState(r, lastBlockSizeState) + switch state { + case 0: + blocksize = r.Intn(avgBlockSize * 4) + case 1: + blocksize = r.Intn(avgBlockSize * 2) + default: + blocksize = 0 + } + return state, blocksize +} From b1ba6a44552273605fa77edb84a4ee033751de97 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Fri, 9 Nov 2018 10:31:56 -0500 Subject: [PATCH 18/18] add back in PeriodicInvariant --- x/mock/simulation/util.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/x/mock/simulation/util.go b/x/mock/simulation/util.go index f10364aaaa38..df2b6dae44b8 100644 --- a/x/mock/simulation/util.go +++ b/x/mock/simulation/util.go @@ -7,6 +7,8 @@ import ( "strings" "testing" "time" + + "github.com/cosmos/cosmos-sdk/baseapp" ) func getTestingMode(tb testing.TB) (testingMode bool, t *testing.T, b *testing.B) { @@ -102,3 +104,19 @@ func getBlockSize(r *rand.Rand, params Params, } return state, blocksize } + +// PeriodicInvariant returns an Invariant function closure that asserts a given +// invariant if the mock application's last block modulo the given period is +// congruent to the given offset. +// +// NOTE this function is intended to be used manually used while running +// computationally heavy simulations. +// TODO reference this function in the codebase probably through use of a switch +func PeriodicInvariant(invariant Invariant, period int, offset int) Invariant { + return func(app *baseapp.BaseApp) error { + if int(app.LastBlockHeight())%period == offset { + return invariant(app) + } + return nil + } +}