Skip to content

Commit

Permalink
problem: files are badly organized and it's confusing
Browse files Browse the repository at this point in the history
  • Loading branch information
gsovereignty committed Oct 17, 2023
1 parent cce9c69 commit 57084fb
Show file tree
Hide file tree
Showing 24 changed files with 104 additions and 218 deletions.
4 changes: 2 additions & 2 deletions src/components/LoginNIP07Button.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script lang="ts">
import { processMempool } from "$lib/consensus/producer";
import { processMempool } from "$lib/consensus/consensus_event_producer";
import { login } from "$lib/helpers/login";
import { currentUser } from "$lib/stores/current-user";
import ndk from "$lib/stores/ndk";
import ndk from "$lib/stores/ndk_events";
import { NDKNip07Signer } from "@nostr-dev-kit/ndk";
import { Button } from "carbon-components-svelte";
import { User } from "carbon-pictograms-svelte";
Expand Down
2 changes: 1 addition & 1 deletion src/components/Profile.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import ndk from "$lib/stores/ndk";
import ndk from "$lib/stores/ndk_events";
import {
AspectRatio,
Column,
Expand Down
6 changes: 3 additions & 3 deletions src/components/modals/AddIdentity.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { BitcoinHeightTag, BitcoinTipHeight } from "$lib/helpers/bitcoin";
import { BitcoinHeightTag } from "$lib/helpers/bitcoin";
import { unixTimeNow } from "$lib/helpers/mundane";
import {
hexPubkeyValidator,
Expand All @@ -9,8 +9,8 @@
simulate
} from "$lib/settings";
import { currentUser } from "$lib/stores/current-user";
import ndk, { ndk_profiles } from "$lib/stores/ndk";
import { FUCKYOUVITE, consensusTipState } from "$lib/stores/state";
import ndk, { ndk_profiles } from "$lib/stores/ndk_events";
import { FUCKYOUVITE, consensusTipState } from "$lib/consensus/state";
import { NDKEvent } from "@nostr-dev-kit/ndk";
import {
Button,
Expand Down
4 changes: 2 additions & 2 deletions src/components/modals/AddProblem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
simulate
} from "$lib/settings";
import { currentUser } from "$lib/stores/current-user";
import ndk from "$lib/stores/ndk";
import { FUCKYOUVITE } from "$lib/stores/state";
import ndk from "$lib/stores/ndk_events";
import { FUCKYOUVITE } from "$lib/consensus/state";
import { NDKEvent } from "@nostr-dev-kit/ndk";
import {
Button,
Expand Down
2 changes: 1 addition & 1 deletion src/components/modals/CreateRocket.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { unixTimeNow } from "$lib/helpers/mundane";
import { rocketNameValidator, rootTag, simulate } from "$lib/settings";
import { currentUser } from "$lib/stores/current-user";
import ndk from "$lib/stores/ndk";
import ndk from "$lib/stores/ndk_events";
import { NDKEvent } from "@nostr-dev-kit/ndk";
import {
Button,
Expand Down
2 changes: 1 addition & 1 deletion src/components/objects/Problem.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { consensusTipState } from "$lib/stores/state";
import { consensusTipState } from "$lib/consensus/state";
import type { Problem } from "$lib/types";
import { AccordionItem, InlineLoading } from "carbon-components-svelte";
import AddProblem from "../modals/AddProblem.svelte";
Expand Down
2 changes: 1 addition & 1 deletion src/components/views/Identity.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
import {
nostrocketParticipantProfiles,
} from "$lib/stores/state";
} from "$lib/consensus/state";
import {
AspectRatio,
Button,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { weHaveTheLead } from "$lib/consensus/current-votepower";
import { weHaveTheLead } from "$lib/consensus/votepower";
import { BitcoinTipHeight } from "$lib/helpers/bitcoin";
import { unixTimeNow } from "$lib/helpers/mundane";
import { validate } from "$lib/protocol_validators/rockets";
import { MAX_STATECHANGE_EVENT_AGE, rootEventID, rootTag, simulate } from "$lib/settings";
import ndk from "$lib/stores/ndk";
import { consensusTipState, eventsInState, labelledTag, mempool } from "$lib/stores/state";
import ndk from "$lib/stores/ndk_events";
import { consensusTipState, eventsInState, labelledTag, mempool } from "$lib/consensus/state";
import { NDKEvent } from "@nostr-dev-kit/ndk";
import { Mutex } from "async-mutex";
import {
Expand Down
24 changes: 0 additions & 24 deletions src/lib/consensus/current-state.ts

This file was deleted.

20 changes: 9 additions & 11 deletions src/lib/stores/state.ts → src/lib/consensus/state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//todo deprecate this file and use /consensus instead
//todo deprecate precomputed state
import createEventpool from "$lib/consensus/mempool";
import createEventpool from "$lib/stores/mempool";
import { validate } from "$lib/protocol_validators/rockets";
import type { NDKEvent, NDKFilter, NDKUser } from "@nostr-dev-kit/ndk";
import { Mutex } from "async-mutex";
Expand All @@ -12,15 +12,15 @@ import {
} from "svelte/store";
import { allNostrocketEventKinds } from "../kinds";
import {
changeStateMutex,
ignitionPubkey,
nostrocketIgnitionEvent,
rootEventID,
} from "../settings";
import { Nostrocket, Problem, type Account } from "../types";
import ndk, { ndk_profiles } from "./ndk";
import { fetchEventsAndUpsertStore, problemEvents } from "./problems";
import { profiles } from "./profiles";
import ndk, { ndk_profiles } from "../stores/ndk_events";
import { fetchEventsAndUpsertStore, problemEvents } from "../stores/problems";
import { profiles } from "../stores/profiles";
import { changeStateMutex } from "../stores/mutex";

export function FUCKYOUVITE(): NDKUser { //todo, vite issue fixed, update everywhere that uses this
return $ndk.getUser({});
Expand Down Expand Up @@ -205,8 +205,7 @@ validConsensusEvents.subscribe((x) => {
let request = labelledTag(x[0], "request", "e");
if (request) {
let requestEvent = mempool.fetch(request);
//todo add mutex
changeStateMutex.acquire().then(()=>{
changeStateMutex(request).then(release=>{
let current = get(consensusTipState);
if (!requestEvent) {
console.log("event: ", request, " for consensus event ", x[0].id, " is not in mempool")
Expand All @@ -226,8 +225,7 @@ validConsensusEvents.subscribe((x) => {
}
}
}

changeStateMutex.release()
release()
})
}
}
Expand All @@ -243,11 +241,11 @@ async function watchMempool() {
let last = 0
watchMempoolMutex.acquire().then(()=>{
mempool.subscribe(()=>{
changeStateMutex.acquire().then(()=>{
changeStateMutex("state:244").then((release)=>{
let current = get(consensusTipState);
let newstate = processMempool(current)
consensusTipState.set(newstate);
changeStateMutex.release()
release()
})
})
})
Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions src/lib/kinds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ export const kindsThatNeedConsensus = Object.keys(consensusKinds).map((k) =>
parseInt(k)
);

const problemKindRecord: Record<number, string> = {
15171971: "Problem ANCHOR",
15171972: "Problem COMMIT",
15171973: "Problem TEXT",
31971: "Problem HEAD",
};

export const problemKinds = Object.keys(problemKindRecord).map((k) =>
parseInt(k)
);

const kinds: Record<number, string> = {
10311: "Precomputed State",
15171031: "Rocket Ignition",
Expand Down
12 changes: 10 additions & 2 deletions src/lib/settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Mutex } from "async-mutex";

export const MAX_STATECHANGE_EVENT_AGE = 86_400; //seconds

Expand Down Expand Up @@ -35,4 +34,13 @@ export const rocketNameValidator = /^\w{5,20}$/;

export const hexPubkeyValidator = /^\w{64}$/;

export const changeStateMutex = new Mutex()
export const profileRelays = [
"wss://nostr.688.org",
"wss://relay.damus.io",
"wss://nos.lol",
];

export const defaultRelays = [
"wss://nostr.688.org",
// 'ws://localhost:8080',
];
13 changes: 11 additions & 2 deletions src/lib/consensus/mempool.ts → src/lib/stores/mempool.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { allNostrocketEventKinds, kindsThatNeedConsensus } from "$lib/kinds";
import { allNostrocketEventKinds, kindsThatNeedConsensus, problemKinds } from "$lib/kinds";
import { rootEventID } from "$lib/settings";
import { labelledTag } from "$lib/stores/state";
import { problemEvents } from "$lib/stores/problems";
import { labelledTag } from "$lib/consensus/state";
import type { NDKEvent } from "@nostr-dev-kit/ndk";
import { get, writable } from "svelte/store";

Expand All @@ -10,6 +11,14 @@ export default function createEventpool(notstrict:boolean|undefined) {
return {
subscribe,
push: (e: NDKEvent): void => {
if (problemKinds.includes(e.kind)) {
problemEvents.update(pe=>{
if (!pe.get(e.id)) {
pe.set(e.id, e)
}
return pe
})
}
if (!notstrict) {
if (labelledTag(e, "root", "e") == rootEventID && allNostrocketEventKinds.includes(e.kind? e.kind : 0)) {
update((m) => {
Expand Down
22 changes: 22 additions & 0 deletions src/lib/stores/mutex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Mutex } from "async-mutex"
import { writable } from "svelte/store"

const CHANGE_STATE_MUTEX = new Mutex()
export const MutexObserver = writable(false)

export async function changeStateMutex(id:string|undefined) {
let p = new Promise<()=>void>((resolve)=>{
CHANGE_STATE_MUTEX.acquire().then(()=>{
MutexObserver.set(true)
resolve(()=>{
CHANGE_STATE_MUTEX.release()
MutexObserver.set(false)
})
})
})
return p
}

MutexObserver.subscribe(state=>{
console.log("Mutext is locked? ", state)
})
35 changes: 2 additions & 33 deletions src/lib/stores/ndk.ts → src/lib/stores/ndk_events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { browser } from "$app/environment";
import NDK from "@nostr-dev-kit/ndk";
import { defaultRelays, profileRelays } from "$lib/settings";
import type { NDKCacheAdapter } from "@nostr-dev-kit/ndk";
import NDKDexieCacheAdapter from "@nostr-dev-kit/ndk-cache-dexie";
import NDKSvelte from "@nostr-dev-kit/ndk-svelte";
Expand All @@ -13,39 +13,8 @@ if (browser) {
});
}

// // get relays from localstorage
// let relays;

// try {
// // relays = localStorage.getItem('relays');
// } catch (e) {
// /* empty */
// }

// let relayList: string[] = [];

// if (relays) {
// relayList = JSON.parse(relays);
// }

export const defaultRelays = [
"wss://nostr.688.org",
// 'ws://localhost:8080',
];

export const profileRelays = [
"wss://nostr.688.org",
"wss://relay.damus.io",
"wss://nos.lol",
];

// if (!relayList || !Array.isArray(relayList) || relayList.length === 0) {
// relayList = defaultRelays;
// }

let relayList = defaultRelays;
const _ndk: NDKSvelte = new NDKSvelte({
explicitRelayUrls: relayList,
explicitRelayUrls: defaultRelays,
});

const ndk = writable(_ndk);
Expand Down
Loading

0 comments on commit 57084fb

Please sign in to comment.