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

Bsodenkamp allocation identities refactor #3115

Merged
merged 13 commits into from
Aug 22, 2021

Conversation

bensodenkamp
Copy link
Contributor

Test Plan

bsodenkamp@Benjamins-MacBook-Pro cred % cat config/grain.json
{
"immediatePerWeek": 1000,
"balancedPerWeek": 5000,
"recentPerWeek": 19000,
"recentWeeklyDecayRate": 0.16,
"maxSimultaneousDistributions": 100
}
bsodenkamp@Benjamins-MacBook-Pro cred % pwd
/Users/bsodenkamp/projects/sourcecred.com/cred
bsodenkamp@Benjamins-MacBook-Pro cred % scdev grain -s -f > /tmp/scdevgrain
bsodenkamp@Benjamins-MacBook-Pro cred % scmain grain -s -f > /tmp/scmaingrain
bsodenkamp@Benjamins-MacBook-Pro cred % diff /tmp/scdevgrain /tmp/scmaingrain
bsodenkamp@Benjamins-MacBook-Pro cred %

Comment on lines -71 to -88
it("errors if there are no identities", () => {
const thunk = () =>
computeAllocation(immediate(5), [], credGrainView, 0);
expect(thunk).toThrowError("must have at least one identity");
});
it("errors if the total cred is zero", () => {
const thunk = () =>
computeAllocation(immediate(5), [aid(0, [0])], credGrainView, 0);
expect(thunk).toThrowError("cred is zero");
});
it("errors if there's NaN or Infinity in Cred", () => {
const thunk = () =>
computeAllocation(immediate(5), [aid(0, [NaN])], credGrainView, 0);
expect(thunk).toThrowError("invalid cred");
});
it("errors if there's inconsistent Cred lengths", () => {
const i1 = aid(0, [1]);
const i2 = aid(0, [1, 2]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I removed these checks, and added a validation routine in the CredGrainView to do some of the same checks, however those checks are not tested. To test some of we would have to create a a credGrainView with no cred, or with mismatched interval data for participants, and I'm not sure how to go about that. Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

You can use the CredGrainView.fromJSON function to construct it manually with desired test data.

blueridger
blueridger previously approved these changes Aug 12, 2021
@blueridger blueridger dismissed their stale review August 12, 2021 17:09

probably would be good to not have test coverage decline.

@bensodenkamp
Copy link
Contributor Author

Added a bunch of tests for malformed credGrainView objects. Committed and manual tested.

Comment on lines 134 to 135
this.activeParticipants().map((p) => {
p.credPerInterval.map((c) => {
Copy link
Member

Choose a reason for hiding this comment

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

since you aren't building a data structure, .forEach would be a better fit than .map

here and below

Comment on lines 146 to 156
this.activeParticipants().map((p) => {
if (p.credPerInterval.length !== this.intervals().length) {
throw new Error(`participant cred per interval length mismatch`);
}
});

this.activeParticipants().map((p) => {
if (p.grainEarnedPerInterval.length !== this.intervals().length) {
throw new Error(`participant grain per interval length mismatch`);
}
});
Copy link
Member

Choose a reason for hiding this comment

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

It'd be more efficient and concise if you placed all checks into the same loops. Not sure why the same loops are being duplicated throughout this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

describe("validation tests", () => {
const credGrainViewNoCred = CredGrainView.fromJSON(
JSON.parse(
'{"participants":[{"active":true,"identity":{"id":"YVZhbGlkVXVpZEF0TGFzdA", "subtype":"USER",\
Copy link
Member

@blueridger blueridger Aug 20, 2021

Choose a reason for hiding this comment

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

Non-blocking:
This works. An alternative strategy could've been to initialize a valid data structure in a beforeEach, and then modify it to be invalid for each test case. Parsing from string is kinda beneficial to smush it into fewer lines, but you could also have just initialized the "JSON" as a javascript hash and that would keep Flow typing stricter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Noted. I think my motivation was just to be able to see all the data at once.

Copy link
Member

@blueridger blueridger left a comment

Choose a reason for hiding this comment

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

so close! just missed 2
you can merge on this approval once fixed


this.activeParticipants().map((p) => {
p.grainEarnedPerInterval.map((g) => {
Copy link
Member

Choose a reason for hiding this comment

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

map -> forEach


this.activeParticipants().map((p) => {
p.credPerInterval.map((c) => {
Copy link
Member

Choose a reason for hiding this comment

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

map -> forEach
also can be deduped

@bensodenkamp bensodenkamp merged commit 4d468ac into main Aug 22, 2021
@bensodenkamp bensodenkamp deleted the bsodenkamp-allocation-identities-refactor branch August 22, 2021 00:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants