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: onSlotFinalized include all previous slots. #1335

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 58 additions & 5 deletions client/src/features/visualizer-vivagraph/hooks/useFeed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useRef, useState, useContext } from "react";
import { type BlockMetadataResponse } from "@iota/sdk-wasm-nova/web";
import { type BlockMetadataResponse, Utils, BlockState, SlotIndex } from "@iota/sdk-wasm-nova/web";
import { ServiceFactory } from "~factories/serviceFactory";
import { IFeedBlockData } from "~/models/api/nova/feed/IFeedBlockData";
import Viva from "vivagraphjs";
Expand Down Expand Up @@ -35,6 +35,9 @@ export const useFeed = (network: string) => {
const deleteBlockIdToMetadata = useTangleStore((state) => state.deleteBlockIdToMetadata);
const updateBlockIdToMetadata = useTangleStore((state) => state.updateBlockIdToMetadata);
const search = useTangleStore((state) => state.search);
const addToConfirmedBlocksBySlot = useTangleStore((state) => state.addToConfirmedBlocksBySlot);
const removeConfirmedBlocksSlot = useTangleStore((state) => state.removeConfirmedBlocksSlot);
const confirmedBlocksBySlot = useTangleStore((state) => state.confirmedBlocksBySlot);
const themeMode = useGetThemeMode();

const graphContext = useContext(GraphContext);
Expand Down Expand Up @@ -209,8 +212,9 @@ export const useFeed = (network: string) => {
const blockMetadata = getBlockIdToMetadata(blockId);

if (!blockMetadata) {
const color = getBlockColorByState(themeMode, "pending");
createBlock(blockId, { ...newBlock, color: color }, now);
const initState = "pending";
const color = getBlockColorByState(themeMode, initState);
createBlock(blockId, { ...newBlock, color: color, state: initState }, now);

const parentIds = getBlockParents(newBlock);
const existingBlockIds = getBlockMetadataKeys();
Expand All @@ -232,12 +236,61 @@ export const useFeed = (network: string) => {
const selectedColor = getBlockColorByState(themeMode, metadataUpdate.blockState);

updateBlockIdToMetadata(metadataUpdate.blockId, { color: selectedColor });
updateBlockColor(metadataUpdate.blockId, selectedColor);

const blockMetadata = getBlockIdToMetadata(metadataUpdate.blockId);
if (blockMetadata) {
const previousBlockState = blockMetadata.state;
const wasConfirmedBeforeAccepted = previousBlockState === "accepted" && metadataUpdate.blockState === "confirmed";

if (!wasConfirmedBeforeAccepted) {
updateBlockIdToMetadata(metadataUpdate.blockId, {
state: metadataUpdate.blockState,
});
}

updateBlockColor(metadataUpdate.blockId, selectedColor);
const acceptedStates: BlockState[] = ["confirmed", "accepted"];
if (acceptedStates.includes(metadataUpdate.blockState)) {
const slot = Utils.computeSlotIndex(metadataUpdate.blockId);
addToConfirmedBlocksBySlot(metadataUpdate.blockId, slot);
}
}
}
}

function onSlotFinalized(slotFinalized: SlotIndex): void {
const slotsBefore = Array.from(confirmedBlocksBySlot.keys());

const slots = [...slotsBefore, slotFinalized];

const blocks = [];
for (const slot of slots) {
const blockIds = confirmedBlocksBySlot.get(slot);
if (blockIds) {
blocks.push(...blockIds);
}
}

if (blocks?.length) {
blocks.forEach((blockId) => {
const selectedColor = getBlockColorByState(themeMode, "finalized");
if (selectedColor) {
updateBlockIdToMetadata(blockId, {
state: "finalized",
color: selectedColor,
});
updateBlockColor(blockId, selectedColor);
}
});
}

for (const slot of slots) {
removeConfirmedBlocksSlot(slot);
Comment on lines +262 to +288
Copy link
Member

Choose a reason for hiding this comment

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

It looks nice, I suggest renaming the variables a bit so it is more easy to understand :)

        const previousFinalizedSlots = Array.from(confirmedBlocksBySlot.keys());
        const allFinalizedSlots = [...previousFinalizedSlots, slotFinalized];
        const confirmedBlocks = [];

        for (const slot of allFinalizedSlots) {
            const confirmedBlockIds = confirmedBlocksBySlot.get(slot);

            if (confirmedBlockIds) {
                confirmedBlocks.push(...confirmedBlockIds);
            }
        }

        if (confirmedBlocks?.length) {
            confirmedBlocks.forEach((blockId) => {
                const finalizedBlockColor = getBlockColorByState(themeMode, "finalized");
                if (finalizedBlockColor) {
                    updateBlockIdToMetadata(blockId, {
                        state: "finalized",
                        color: finalizedBlockColor,
                    });
                    updateBlockColor(blockId, finalizedBlockColor);
                }
            });
        }

        for (const slot of allFinalizedSlots) {
            removeConfirmedBlocksSlot(slot);

}
}

const feedSubscriptionStart = () => {
feedService.subscribeBlocks(onNewBlock, onBlockMetadataUpdate, () => {});
feedService.subscribeBlocks(onNewBlock, onBlockMetadataUpdate, onSlotFinalized);
};

useEffect(() => {
Expand Down
40 changes: 40 additions & 0 deletions client/src/features/visualizer-vivagraph/store/tangle.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { create } from "zustand";
import { devtools } from "zustand/middleware";
import { IFeedBlockData } from "~models/api/nova/feed/IFeedBlockData";
import { BlockId, BlockState, SlotIndex } from "@iota/sdk-wasm-nova/web";

export interface VivagraphParams {
color: string;
state: BlockState;
}

interface TangleState {
Expand All @@ -24,13 +26,21 @@ interface TangleState {

search: string;
setSearch: (search: string) => void;

resetTangleStore: () => void;

// Confirmed/accepted blocks by slot
confirmedBlocksBySlot: Map<number, string[]>;
addToConfirmedBlocksBySlot: (blockId: BlockId, slot: SlotIndex) => void;
removeConfirmedBlocksSlot: (slot: SlotIndex) => void;
}

const INITIAL_STATE = {
blockIdToMetadata: new Map(),
visibleBlocks: [],
selectedNode: null,
search: "",
confirmedBlocksBySlot: new Map(),
};

export const useTangleStore = create<TangleState>()(
Expand Down Expand Up @@ -85,5 +95,35 @@ export const useTangleStore = create<TangleState>()(
getBlockMetadataValues: () => {
return Array.from(get().blockIdToMetadata.values());
},

addToConfirmedBlocksBySlot: (blockId, slot) => {
set((state) => {
state.confirmedBlocksBySlot.has(slot)
? state.confirmedBlocksBySlot.get(slot)?.push(blockId)
: state.confirmedBlocksBySlot.set(slot, [blockId]);
return {
...state,
confirmedBlocksBySlot: state.confirmedBlocksBySlot,
};
});
},

removeConfirmedBlocksSlot: (slot) => {
set((state) => {
state.confirmedBlocksBySlot.delete(slot);

// Cleanup all slots that are lower than the current slot
for (const existingSlot of state.confirmedBlocksBySlot.keys()) {
if (existingSlot < slot) {
state.confirmedBlocksBySlot.delete(existingSlot);
}
}

return {
...state,
confirmedBlocksBySlot: state.confirmedBlocksBySlot,
};
});
},
})),
);
Loading