Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Simulation: Params from File #4435

Merged
merged 21 commits into from
Jun 8, 2019
Merged

Simulation: Params from File #4435

merged 21 commits into from
Jun 8, 2019

Conversation

alexanderbez
Copy link
Contributor

@alexanderbez alexanderbez commented May 29, 2019

  • Cleanup simulation parameter output (uses JSON now)
  • Move distribution parameter generation to simulation package
  • Add simulation godoc (doc.go)

closes: #4428

/cc @npinto


  • Targeted PR against correct branch (see CONTRIBUTING.md)

  • Linked to github-issue with discussion and accepted design OR link to spec that describes this work.

  • Wrote tests

  • Updated relevant documentation (docs/)

  • Added a relevant changelog entry: clog add [section] [stanza] [message]

  • rereviewed Files changed in the github PR explorer


For Admin Use:

  • Added appropriate labels to PR (ex. wip, ready-for-review, docs)
  • Reviewers Assigned
  • Squashed all commits, uses message "Merge pull request #XYZ: [title]" (coding standards)

@codecov
Copy link

codecov bot commented May 29, 2019

Codecov Report

Merging #4435 into master will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master    #4435   +/-   ##
=======================================
  Coverage   54.58%   54.58%           
=======================================
  Files         248      248           
  Lines       15967    15967           
=======================================
  Hits         8716     8716           
  Misses       6617     6617           
  Partials      634      634

@npinto
Copy link
Contributor

npinto commented May 30, 2019

Should we also consider adding new parameters to control some hardcoded values like e.g.:

{100, banksim.SimulateMsgSend(app.accountKeeper, app.bankKeeper)},

What do you think?

@alexanderbez
Copy link
Contributor Author

Good idea @npinto! Yes, I'll include this too.

simapp/doc.go Outdated Show resolved Hide resolved
simapp/test_util.go Outdated Show resolved Hide resolved
Copy link
Contributor

@alessio alessio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few comments - changes are good, docs is great, as such deserves better go doc compliant formatting

Copy link
Collaborator

@fedekunze fedekunze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor comments only

simapp/sim_test.go Outdated Show resolved Hide resolved
simapp/test_util.go Outdated Show resolved Hide resolved
x/simulation/util.go Show resolved Hide resolved
simapp/sim_test.go Show resolved Hide resolved
Copy link
Collaborator

@fedekunze fedekunze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK

@alexanderbez
Copy link
Contributor Author

@alessio I fixed the godoc. It's pretty now. Ty

@alexanderbez alexanderbez requested a review from alessio June 5, 2019 17:33
simapp/doc.go Outdated Show resolved Hide resolved
simapp/doc.go Outdated Show resolved Hide resolved
simapp/doc.go Outdated Show resolved Hide resolved
simapp/doc.go Outdated Show resolved Hide resolved
Copy link
Contributor

@alessio alessio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pending tiny corrections - else this looks good

alexanderbez and others added 3 commits June 5, 2019 13:43
Co-Authored-By: Alessio Treglia <quadrispro@ubuntu.com>
Co-Authored-By: Alessio Treglia <quadrispro@ubuntu.com>
@npinto
Copy link
Contributor

npinto commented Jun 5, 2019

What do you think about allowing pausing/resuming of these simulations (w.r.t. eg blocks) ? That is: (1) having a way to dump the full state of the simulator at the end of a given number of blocks, and (2) taking this state and resuming the simulation for another given number of blocks.

This could allow "in-flight" fine tuning of parameters and also much better optimizations of simulations (e.g. "early stopping" of non promising simulations).

@alexanderbez
Copy link
Contributor Author

@npinto good idea but out of scope of this PR! Let's open up another issue for that. I'd like to keep the PRs as minimal and focused as possible 👍

@npinto
Copy link
Contributor

npinto commented Jun 5, 2019

@alexanderbez sounds good, I'll open another feature request for this. Thanks!

@npinto
Copy link
Contributor

npinto commented Jun 5, 2019

Should we consider printing out a json representation of the event statistics? like e.g.:

Simulation stopped early as all validators have been unbonded; nobody left to propose a block!
Event statistics:
                                         /no-operation/failure => 1582
                                       auth/deduct_fee/failure => 15
                                            auth/deduct_fee/ok => 159
                                        bank/multisend/failure => 22
                                             bank/multisend/ok => 217
                                             bank/send/failure => 242
                                                  bank/send/ok => 2379
                                           beginblock/evidence => 151
                                     beginblock/signing/missed => 565
                                     beginblock/signing/signed => 2768
                            distr/set_withdraw_address/failure => 1312
                       distr/withdraw_delegator_reward/failure => 1190
                            distr/withdraw_delegator_reward/ok => 140
                   distr/withdraw_validator_commission/failure => 299
                        distr/withdraw_validator_commission/ok => 994
                               endblock/validatorupdates/added => 106
                              endblock/validatorupdates/kicked => 113
                             endblock/validatorupdates/updated => 842
                                           gov/deposit/failure => 802
                                                gov/deposit/ok => 1741
                                   gov/submit_proposal/failure => 60
                                        gov/submit_proposal/ok => 300
                                       slashing/unjail/failure => 2483
                              staking/begin_redelegate/failure => 1944
                                   staking/begin_redelegate/ok => 148
                              staking/create_validator/failure => 2018
                                   staking/create_validator/ok => 106
                                      staking/delegate/failure => 1479
                                           staking/delegate/ok => 2910
                                staking/edit_validator/failure => 60
                                     staking/edit_validator/ok => 63
GoLevelDB Stats
Compactions
 Level |   Tables   |    Size(MB)   |    Time(sec)  |    Read(MB)   |   Write(MB)
-------+------------+---------------+---------------+---------------+---------------
   0   |          0 |       0.00000 |       0.06395 |       0.00000 |       5.99821
   1   |          1 |       0.84860 |       0.02618 |       5.99821 |       0.84860

GoLevelDB cached block size 1273211
--- PASS: TestFullAppSimulation (30.89s)
PASS
ok  	github.com/cosmos/cosmos-sdk/simapp	30.936s

It might then be easier to parse them out if they need to be analyzed/optimized.

@fedekunze
Copy link
Collaborator

Should we consider printing out a json representation of the event statistics?

That would be nice but maybe not for the scope of this PR. I'd merge this and then improve it afterwards with the comments you suggested @npinto

@alexanderbez
Copy link
Contributor Author

Yeah sure @npinto, mind opening up another issue for this?

Copy link
Contributor

@rigelrozanski rigelrozanski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, great documentation section.

Few comments throughout, biggest comment is the code structure of using immediately executed function variables while generating module genesis information (aka genXXXXGenesisState)

x/simulation/params.go Outdated Show resolved Hide resolved
x/simulation/util.go Show resolved Hide resolved
simapp/doc.go Show resolved Hide resolved
simapp/sim_test.go Outdated Show resolved Hide resolved
simapp/sim_test.go Outdated Show resolved Hide resolved
simapp/sim_test.go Show resolved Hide resolved
var v uint64
ap.GetOrGenerate(cdc, "max_memo_characters", &v, r, func(r *rand.Rand) { v = simulation.ModuleParamSimulator["max_memo_characters"](r).(uint64) })
return v
}(r),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code structure is quite obfuscated, I understand the thinking to compress all the code required to getOrGen a param into an immediately executed function as is done here. But I think a more intuitive/digestable structure would be to just individually get all the parameters at the top of this function (before defining authGenesis). Something like:

func genAuthGenesisState(cdc *codec.Codec, r *rand.Rand, ap simulation.AppParams, genesisState map[string]json.RawMessage) {

     var maxMemoChars uint64
     ap.GetOrGenerate(cdc, "max_memo_characters", &maxMemoChars, r, func(r *rand.Rand) { v = simulation.ModuleParamSimulator["max_memo_characters"](r).(uint64) })
     ....

     authGenesis := auth.NewGenesisState(
		nil,
		auth.NewParams(
			maxMemoChars,
                         ...

This also applies too all modules aka genXXXXGenesisState

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah so I understand your concern upon first reading the code. I think the additional verbosity that results from such a suggested change isn't worth it because now that we have constants that are directly mapped to the meaning of the arguments, it's very clear what it's doing and what value it provides.

I'd rather not have to go through each of these for what will probably be a tiny (if any) gain in brevity.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the code would actually become less verbose under the suggested change, however we would have to come up with variable names - that's pretty much the only downside. I don't mind quickly making these changes if it's just a matter of the work

var v int
ap.GetOrGenerate(cdc, "op_weight_deduct_fee", &v, nil, func(_ *rand.Rand) { v = 5 })
return v
}(nil),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto on the defining contents of WeightedOperation before this return statement (not in immediately executed function variables) as per my previous comment on this structure for all the modules (genXXXXGenesisState)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See response :)

Copy link
Contributor

@rigelrozanski rigelrozanski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After implementing my suggestion, I've changed my position, I think it makes things even more obfuscated unfortunately. I've opted to keep it, but I expanded up the 2nd layer function variables what were really hidden in there (and also made the lines very long)

@rigelrozanski rigelrozanski merged commit 6606007 into master Jun 8, 2019
@rigelrozanski rigelrozanski deleted the bez/4428-sim-params-file branch June 8, 2019 20:55
@npinto
Copy link
Contributor

npinto commented Jul 2, 2019

Yeah sure @npinto, mind opening up another issue for this?

Sure! See #4670

larry0x pushed a commit to larry0x/cosmos-sdk that referenced this pull request May 22, 2023
* Move distr params to simulation param generator

* Cleanup sim params output

* Print params when possible instead of entire genesis struct

* Update module param simulator keys

* Rename module params simulator keys

* Add/fix missing module params and implement SimParams

* UPdate sim test to accept params file

* Allow weights to be provided from file

* Create doc.go

* Remove TODO

* Implement MustMarshalJSONIndent

* Use mustMarshalJSONIndent for non-codec

* Remove TODO

* Fix doc.go

* Update points

* Update simapp/doc.go

Co-Authored-By: Alessio Treglia <quadrispro@ubuntu.com>

* Update simapp/doc.go

Co-Authored-By: Alessio Treglia <quadrispro@ubuntu.com>

* Cleanup appStateFn

* Use constants for simulation parameter keys

* Update msgs.go

* minor formatting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Simulation: Params from File
5 participants