Skip to content

Commit

Permalink
problem: invalid problems make it into state
Browse files Browse the repository at this point in the history
  • Loading branch information
gsovereignty committed Nov 12, 2023
1 parent 1813658 commit ed45024
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
8 changes: 5 additions & 3 deletions src/lib/stores/nostrocket_state/master_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,23 @@ function processSoftStateChangeReqeustsFromMempool(
//let newState:Nostrocket = clone(currentState)
let currentList = [...get(eligible)];
currentList.forEach((e) => {
let copyOfState = currentState.Copy()
//todo clone not ref
switch (e.kind) {
case 31009: {
let [n, success] = handleIdentityEvent(e, currentState);
let [n, success] = handleIdentityEvent(e, copyOfState);
if (success) {
currentState = n;
currentState = copyOfState;
handled.push(e);
}
}
case 1972:
case 1971:
let err = HandleProblemEvent(e, currentState)
let err = HandleProblemEvent(e, copyOfState)
if (err != undefined) {
//console.log(err, e.id)
} else {
currentState = copyOfState
handled.push(e);
}
}
Expand Down
44 changes: 21 additions & 23 deletions src/lib/stores/nostrocket_state/soft_state/simplifiedProblems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,14 @@ function handleProblemStatusChangeEvent(
}

if (newStatus == "closed") {
let err:string|undefined = undefined
problem.Children.forEach((p) => {
let child = state.Problems.get(p)
if (!child) {err = "could not find child problem " + p + ". To prevent catastrophe, you can't close this."}
if (state.Problems.get(p)?.Status != "closed") {
err = "you must close the sub-problem " +
p + " before you can close this problem"
;
for (let c of problem.Children) {
let child = state.Problems.get(c)
if (!child) {return "could not find child problem " + c + ". To prevent catastrophe, you can't close this."}
if (child?.Status != "closed") {
return "you must close the sub-problem " +
c + " before you can close this problem"
}
})
if (err != undefined) {return err}
}
}
if (
newStatus == "patched" &&
Expand Down Expand Up @@ -114,6 +111,11 @@ function handleProblemCreation(
if (err != undefined) {
return err;
}
for (let id of p.Parents) {
if (state.Problems.get(id)?.Status != "open") {
return "cant create a problem on a parent that isn't open"
}
}
p.Events.push(ev.rawEvent());
state.Problems.set(p.UID, p);
populateChildren(p, state);
Expand Down Expand Up @@ -184,7 +186,6 @@ function eventToProblemData(
if (existing.Parents.size == 0 && existing.UID != rootProblem) {
return "problem does not have a parent";
}

let status = labelledTag(ev, "", "status")!;
if (!status) {
return "no status tag found";
Expand All @@ -211,33 +212,30 @@ function eventToProblemData(

function parentTagsToProblemData(ev: NDKEvent, existing: Problem) {
existing.Parents = new Set<string>();
ev.getMatchingTags("e").forEach((tag) => {
for (let tag of ev.getMatchingTags("e")) {
if (tag[tag.length - 1] == "parent") {
if (tag[1].length == 64) {
existing!.Parents.add(tag[1]);
existing.Parents.add(tag[1]);
}
}
});
}
}

function populateChildren(problem: Problem, state: Nostrocket) {
problem.Parents.forEach((parent) => {
for (let parent of problem.Parents) {
let parentProblem = state.Problems.get(parent);
if (parentProblem) {
parentProblem.Children.add(problem.UID);
}
});
}
}

export function hasOpenChildren(problem:Problem, state:Nostrocket):boolean {
let has = false
if (!state) {state = get(consensusTipState)}
problem?.Children.forEach(p=>{
if (state.Problems.get(p)?.Status != "closed") {
has = true
}
})
return has
for (let child of problem.Children) {
if (state.Problems.get(child)?.Status != "closed") {return true}
}
return false
}
//// Legacy stuff for reference, leave here for G to delete:
// function updateProblemWithNewHead(
Expand Down

0 comments on commit ed45024

Please sign in to comment.