add comments

This commit is contained in:
Michael Zhu
2019-12-18 11:07:26 -08:00
parent bb3ec970a9
commit 701ba3902c
6 changed files with 28 additions and 7 deletions

View File

@@ -120,7 +120,7 @@ export function StakerMixin<TBase extends Constructor>(Base: TBase): TBase & Con
? StakeStatus.Undelegated
: (Pseudorandom.sample(
[StakeStatus.Undelegated, StakeStatus.Delegated],
[0.2, 0.8],
[0.2, 0.8], // 20% chance of `Undelegated`, 80% chance of `Delegated`
) as StakeStatus);
const from = new StakeInfo(fromStatus, fromPoolId);
@@ -133,7 +133,7 @@ export function StakerMixin<TBase extends Constructor>(Base: TBase): TBase & Con
? StakeStatus.Undelegated
: (Pseudorandom.sample(
[StakeStatus.Undelegated, StakeStatus.Delegated],
[0.2, 0.8],
[0.2, 0.8], // 20% chance of `Undelegated`, 80% chance of `Delegated`
) as StakeStatus);
const to = new StakeInfo(toStatus, toPoolId);

View File

@@ -8,8 +8,10 @@ class PRNGWrapper {
public readonly rng = seedrandom(this.seed);
/*
* Pseudorandom version of _.sample. Picks an element of the given array with uniform probability.
* Return undefined if the array is empty.
* Pseudorandom version of _.sample. Picks an element of the given array. If an array of weights
* is provided, elements of `arr` are weighted according to the value in the corresponding index
* of `weights`. Otherwise, the samples are chosen uniformly at random. Return undefined if the
* array is empty.
*/
public sample<T>(arr: T[], weights?: number[]): T | undefined {
if (arr.length === 0) {
@@ -29,7 +31,9 @@ class PRNGWrapper {
/*
* Pseudorandom version of _.sampleSize. Returns an array of `n` samples from the given array
* (with replacement), chosen with uniform probability. Return undefined if the array is empty.
* (with replacement). If an array of weights is provided, elements of `arr` are weighted
* according to the value in the corresponding index of `weights`. Otherwise, the samples are
* chosen uniformly at random. Return undefined if the array is empty.
*/
public sampleSize<T>(arr: T[], n: number, weights?: number[]): T[] | undefined {
if (arr.length === 0) {
@@ -59,11 +63,14 @@ class PRNGWrapper {
/*
* Returns a function that produces samples from the Kumaraswamy distribution parameterized by
* the given alpha and beta. The Kumaraswamy distribution is like the beta distribution, but
* with a nice closed form.
* with a nice closed form. More info:
* https://en.wikipedia.org/wiki/Kumaraswamy_distribution
* https://www.johndcook.com/blog/2009/11/24/kumaraswamy-distribution/
* Alpha and beta default to 0.2, so that the distribution favors the extremes of the domain.
* The PDF for alpha=0.2, beta=0.2:
* https://www.wolframalpha.com/input/?i=0.2*0.2*x%5E%280.2-1%29*%281-x%5E0.2%29%5E%280.2-1%29+from+0+to+1
*/
public kumaraswamy(this: PRNGWrapper, alpha: Numberish, beta: Numberish): () => BigNumber {
public kumaraswamy(this: PRNGWrapper, alpha: Numberish = 0.2, beta: Numberish = 0.2): () => BigNumber {
const ONE = new BigNumber(1);
return () => {
const u = new BigNumber(this.rng()).modulo(ONE); // u ~ Uniform(0, 1)

View File

@@ -16,7 +16,9 @@ export class PoolManagementSimulation extends Simulation {
const operators = filterActorsByRole(actors, PoolOperator);
const [actions, weights] = _.unzip([
// 40% chance of executing validCreateStakingPool assertion for a random operator
...operators.map(operator => [operator.simulationActions.validCreateStakingPool, 0.4]),
// 60% chance of executing validDecreaseStakingPoolOperatorShare for a random operator
...operators.map(operator => [operator.simulationActions.validDecreaseStakingPoolOperatorShare, 0.6]),
]) as [Array<AsyncIterableIterator<AssertionResult | void>>, number[]];
while (true) {

View File

@@ -24,8 +24,11 @@ export class PoolMembershipSimulation extends Simulation {
const poolManagement = new PoolManagementSimulation(this.environment);
const [actions, weights] = _.unzip([
// 20% chance of executing validJoinStakingPool for a random maker
...makers.map(maker => [maker.simulationActions.validJoinStakingPool, 0.2 / makers.length]),
// 60% chance of executing validFillOrder for a random taker
...takers.map(taker => [taker.simulationActions.validFillOrder, 0.6 / takers.length]),
// 20% chance of executing an assertion generated from the pool management simulation
[poolManagement.generator, 0.2],
]) as [Array<AsyncIterableIterator<AssertionResult | void>>, number[]];

View File

@@ -22,9 +22,13 @@ export class StakeManagementSimulation extends Simulation {
const poolManagement = new PoolManagementSimulation(this.environment);
const [actions, weights] = _.unzip([
// 30% chance of executing validStake for a random staker
...stakers.map(staker => [staker.simulationActions.validStake, 0.3 / stakers.length]),
// 20% chance of executing validUnstake for a random staker
...stakers.map(staker => [staker.simulationActions.validUnstake, 0.2 / stakers.length]),
// 30% chance of executing validMoveStake for a random staker
...stakers.map(staker => [staker.simulationActions.validMoveStake, 0.3 / stakers.length]),
// 20% chance of executing an assertion generated from the pool management simulation
[poolManagement.generator, 0.2],
]) as [Array<AsyncIterableIterator<AssertionResult | void>>, number[]];

View File

@@ -34,10 +34,15 @@ export class StakingRewardsSimulation extends Simulation {
const stakeManagement = new StakeManagementSimulation(this.environment);
const [actions, weights] = _.unzip([
// 10% chance of executing validWithdrawDelegatorRewards for a random staker
...stakers.map(staker => [staker.simulationActions.validWithdrawDelegatorRewards, 0.1 / stakers.length]),
// 10% chance of executing validFinalizePool for a random keeper
...keepers.map(keeper => [keeper.simulationActions.validFinalizePool, 0.1 / keepers.length]),
// 10% chance of executing validEndEpoch for a random keeper
...keepers.map(keeper => [keeper.simulationActions.validEndEpoch, 0.1 / keepers.length]),
// 50% chance of executing an assertion generated from the pool membership simulation
[poolMembership.generator, 0.5],
// 20% chance of executing an assertion generated from the stake management simulation
[stakeManagement.generator, 0.2],
]) as [Array<AsyncIterableIterator<AssertionResult | void>>, number[]];
while (true) {