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

feat: remove operations array from the DRP #292

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

JanLewDev
Copy link
Contributor

@JanLewDev JanLewDev commented Dec 22, 2024

Description

This PR closes #257 and closes #310, removing the need to define the operations array by the DRP devs.

Key features:

  • State changing operations are detected and acted upon, only operations that mutate the state are added to the hashgraph (e.g. if the AddWinsSet holds {1}, add(1) is not creating a new vertex
  • Tests are adapted
  • The check can be bypassed by naming a drp function _*, get*, is* or query*. By convention such functions are not creating operations

@JanLewDev JanLewDev marked this pull request as ready for review December 30, 2024 16:43
Comment on lines +135 to +143
const drp = cloneDeep(this.originalDRP);
const state = cloneDeep(preOperationState);

for (const [key, value] of state.entries()) {
drp[key] = value;
}

const normalizedArgs = Array.isArray(args) ? args : [args];
drp[fn](...normalizedArgs);
Copy link
Contributor

Choose a reason for hiding this comment

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

you can seperate _computeState to _computeDRP(dependencies, op) and getDRPState(drp) to avoid duplicate these code.

Comment on lines +145 to +153
let stateChanged = false;
for (const [key, value] of preOperationState.entries()) {
try {
deepStrictEqual(value, drp[key]);
} catch (e) {
stateChanged = true;
break;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

create a new function to keep this callFn not too long

Comment on lines +161 to +166
const varNames: string[] = Object.keys(drp);
// biome-ignore lint: values can be anything
const newState: Map<string, any> = new Map();
for (const varName of varNames) {
newState.set(varName, drp[varName]);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

separate _computeState can avoid this

Comment on lines +64 to +65
["grant", "revoke"].includes(vertices[0].operation.type) &&
["grant", "revoke"].includes(vertices[1].operation.type)
Copy link
Contributor

Choose a reason for hiding this comment

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

it's like the operations array, let's try other solutions

Comment on lines +112 to +117
if (
!(propKey as string).startsWith("_") &&
!(propKey as string).startsWith("is") &&
!(propKey as string).startsWith("get") &&
!(propKey as string).startsWith("query")
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (
!(propKey as string).startsWith("_") &&
!(propKey as string).startsWith("is") &&
!(propKey as string).startsWith("get") &&
!(propKey as string).startsWith("query")
)
const excludedPrefixes = ["_", "is", "get", "query"]; // make this a constant, change this when adding a new prefix
if (!excludedPrefixes.some(prefix => (propKey as string).startsWith(prefix)))

refactor code

Comment on lines +278 to +286
if (state) {
this.states.set(vertex.hash, { state });
} else {
const newState = this._computeState(
vertex.dependencies,
vertex.operation,
);
this.states.set(vertex.hash, { state: newState });
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (state) {
this.states.set(vertex.hash, { state });
} else {
const newState = this._computeState(
vertex.dependencies,
vertex.operation,
);
this.states.set(vertex.hash, { state: newState });
}
if (state) {
this.states.set(vertex.hash, { state });
return;
}
const newState = this._computeState(
vertex.dependencies,
vertex.operation,
);
this.states.set(vertex.hash, { state: newState });

Reduced Nesting, Readability

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.

Do not register calls to the DRP that do not change the state as operations Drop Operations[]
3 participants