Skip to content

Commit

Permalink
feature: update shape of tangle.
Browse files Browse the repository at this point in the history
Signed-off-by: Eugene Panteleymonchuk <[email protected]>
  • Loading branch information
panteleymonchuk committed Dec 13, 2023
1 parent a345a5a commit ec35d92
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 192 deletions.
1 change: 0 additions & 1 deletion api/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
.eslintrc.js
*.d.ts
novaFeed.ts
18 changes: 9 additions & 9 deletions api/src/initServices.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MqttClient as ChrysalisMqttClient } from "@iota/mqtt.js";
import { Client as StardustClient } from "@iota/sdk";
// import { initLogger, Client as NovaMqttClient } from "@iota/sdk-nova";
import { initLogger, Client as NovaMqttClient } from "@iota/sdk-nova";
import { ServiceFactory } from "./factories/serviceFactory";
import logger from "./logger";
import { IConfiguration } from "./models/configuration/IConfiguration";
Expand Down Expand Up @@ -29,7 +29,7 @@ import { NodeInfoService } from "./services/stardust/nodeInfoService";
import { StardustStatsService } from "./services/stardust/stats/stardustStatsService";

// iota-sdk debug
// initLogger();
initLogger();

const CURRENCY_UPDATE_INTERVAL_MS = 5 * 60000;

Expand Down Expand Up @@ -241,13 +241,13 @@ function initStardustServices(networkConfig: INetwork): void {
function initNovaServices(networkConfig: INetwork): void {
logger.verbose(`Initializing Nova services for ${networkConfig.network}`);

// const mqttInstance = new NovaMqttClient(
// { nodes: [networkConfig.feedEndpoint], brokerOptions: { useWs: true }, ignoreNodeHealth: true }
// );
// ServiceFactory.register(
// `mqtt-${networkConfig.network}`,
// () => mqttInstance
// );
const mqttInstance = new NovaMqttClient(
{ nodes: [networkConfig.feedEndpoint], brokerOptions: { useWs: true }, ignoreNodeHealth: true }
);
ServiceFactory.register(
`mqtt-${networkConfig.network}`,
() => mqttInstance
);

const feedInstance = new NovaFeed(networkConfig.network);
ServiceFactory.register(
Expand Down
13 changes: 5 additions & 8 deletions api/src/services/nova/feed/novaFeed.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { BasicBlock, Client, IBlockMetadata } from "@iota/sdk-nova";
import { ClassConstructor, plainToInstance } from "class-transformer";
import { ServiceFactory } from "../../../factories/serviceFactory";
import logger from "../../../logger";

class Mock {

}

/**
* Wrapper class around Nova MqttClient.
* Streaming blocks from mqtt (upstream) to explorer-client connections (downstream).
Expand All @@ -21,7 +18,7 @@ export class NovaFeed {
/**
* Mqtt service for data (upstream).
*/
private readonly _mqttClient: any;
private readonly _mqttClient: Client;

/**
* Creates a new instance of NovaFeed.
Expand All @@ -30,7 +27,7 @@ export class NovaFeed {
constructor(networkId: string) {
logger.debug("[NovaFeed] Constructing a Nova Feed");
this.blockSubscribers = {};
this._mqttClient = ServiceFactory.get<any>(`mqtt-${networkId}`);
this._mqttClient = ServiceFactory.get<Client>(`mqtt-${networkId}`);

logger.debug(`[NovaFeed] Mqtt is ${JSON.stringify(this._mqttClient)}`);

Expand All @@ -49,7 +46,7 @@ export class NovaFeed {
// eslint-disable-next-line no-void
void this._mqttClient.listenMqtt(["blocks"], (_, message) => {
try {
const block: any = this.parseMqttPayloadMessage(Mock, message);
const block: BasicBlock = this.parseMqttPayloadMessage(BasicBlock, message);
const update: Partial<Record<string, unknown>> = {
block
};
Expand All @@ -66,7 +63,7 @@ export class NovaFeed {
// eslint-disable-next-line no-void
void this._mqttClient.listenMqtt(["block-metadata/referenced"], (_, message) => {
const parsed: { topic: string; payload: string } = JSON.parse(message);
const metadata: any = JSON.parse(parsed.payload);
const metadata: IBlockMetadata = JSON.parse(parsed.payload);

logger.debug(`New metadata ${JSON.stringify(metadata)}`);

Expand Down
2 changes: 1 addition & 1 deletion client/src/features/visualizer-threejs/Emitter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const Emitter: React.FC<EmitterProps> = ({
});

// The Tangle rendering hook
useRenderTangle(emitterRef);
useRenderTangle();

return (
<mesh
Expand Down
20 changes: 12 additions & 8 deletions client/src/features/visualizer-threejs/VisualizerInstance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const VisualizerInstance: React.FC<RouteComponentProps<VisualizerRouteProps>> =
const blockIdToPosition = useTangleStore(s => s.blockIdToPosition);
const blockMetadata = useTangleStore(s => s.blockMetadata);
const indexToBlockId = useTangleStore(s => s.indexToBlockId);
const addBlockAnimation = useTangleStore(s => s.addBlockAnimation);
// const addBlockAnimation = useTangleStore(s => s.addBlockAnimation);

const emitterRef = useRef<THREE.Mesh>(null);
const feedServiceRef = useRef<StardustFeedClient | null>(null);
Expand Down Expand Up @@ -146,18 +146,22 @@ const VisualizerInstance: React.FC<RouteComponentProps<VisualizerRouteProps>> =
bpsCounter.start();
}

addBlock({
id: blockData.blockId,
position,
color: PENDING_BLOCK_COLOR
});
addBlockAnimation(blockData.blockId);

blockIdToPosition.set(blockData.blockId, position);
blockMetadata.set(blockData.blockId, blockData);

addToEdgeQueue(blockData.blockId, blockData.parents ?? []);
addYPosition(Y);

addBlock({
id: blockData.blockId,
position,
color: PENDING_BLOCK_COLOR,
initPosition: {
x: randomIntFromInterval(emitterBox.min.x, emitterBox.max.x),
y: 0,
z: randomIntFromInterval(emitterBox.min.z, emitterBox.max.z)
}
});
}
};

Expand Down
1 change: 1 addition & 0 deletions client/src/features/visualizer-threejs/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const ZOOM_DEFAULT = 2;
export const TIME_DIFF_COUNTER = 250;
export const SECOND = 1000;
export const DATA_SENDER_TIME_INTERVAL = 500;
export const ANIMATION_TIME_SECONDS = 3;

// colors
export const PENDING_BLOCK_COLOR = new Color('#A6C3FC')
Expand Down
58 changes: 37 additions & 21 deletions client/src/features/visualizer-threejs/store/tangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@ import { devtools } from "zustand/middleware";
import { ZOOM_DEFAULT } from "../constants";
import {IFeedBlockData} from "../../../models/api/stardust/feed/IFeedBlockData";

interface BlockState {
interface IPosition {
x: number;
y: number;
z: number;
}

export interface IBlockAnimation {
[blockId: string]: IPosition & {
duration: number;
};
}

export interface BlockState {
id: string;
position: [x: number, y: number, z: number];
color: Color;
Expand All @@ -23,7 +35,7 @@ interface EdgeEntry {
interface TangleState {
// Queue for "add block" operation to the canvas
blockQueue: BlockState[];
addToBlockQueue: (newBlock: BlockState) => void;
addToBlockQueue: (newBlock: BlockState & { initPosition: IPosition; }) => void;
removeFromBlockQueue: (blockIds: string[]) => void;

edgeQueue: Edge[];
Expand Down Expand Up @@ -56,11 +68,8 @@ interface TangleState {
clickedInstanceId: string | null;
setClickedInstanceId: (instanceId: string | null) => void;

// blockAnimation: Map<string, number>; // string - block id, number - duration from start
blockAnimation: {[blockId: string]: number}; // string - block id, number - duration from start
addBlockAnimation: (blockId: string) => void;
blockAnimationUpdate: (blockIds: {[blockId: string]: number}) => void;

blockAnimation: IBlockAnimation;
blockAnimationUpdate: (updated: IBlockAnimation) => void;
}

export const useTangleStore = create<TangleState>()(devtools(set => ({
Expand All @@ -77,30 +86,37 @@ export const useTangleStore = create<TangleState>()(devtools(set => ({
bps: 0,
clickedInstanceId: null,
blockAnimation: {},
blockAnimationUpdate: (blockIds) => {
blockAnimationUpdate: (updated) => {
set(state => {
const nextBlockAnimation = {
...state.blockAnimation,
...updated,
};
for (const key in nextBlockAnimation) {
if (nextBlockAnimation[key].duration > 1) {
delete nextBlockAnimation[key];
}
}
return {
blockAnimation: blockIds,
blockAnimation: nextBlockAnimation,
};
});
},
addBlockAnimation: (blockId: string) => {
addToBlockQueue: newBlockData => {
set(state => {
const next = {
...state.blockAnimation,
[blockId]: 0,
}
const { initPosition, ...blockDataRest } = newBlockData;
return {
blockAnimation: next,
blockQueue: [...state.blockQueue, blockDataRest],
blockAnimation: {
...state.blockAnimation,
[newBlockData.id]: {
...initPosition,
duration: 0,
},
}
};
});
},

addToBlockQueue: newBlockData => {
set(state => ({
blockQueue: [...state.blockQueue, newBlockData]
}));
},
removeFromBlockQueue: (blockIds: string[]) => {
set(state => ({
...state,
Expand Down
Loading

0 comments on commit ec35d92

Please sign in to comment.