Skip to content
Open
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
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ services:
RECEIVER: atone1uq6zjslvsa29cy6uu75y8txnl52mw06j6fzlep
API_ROOT: 'http://api-main:3000/v1'
AUTH: dev
COMMUNITY_ACCOUNT_V1: atone1uq6zjslvsa29cy6uu75y8txnl52mw06j6fzlep

# Uncomment to enable fast sync
# ECLESIA_GRAPHQL_ENDPOINT: "https://graphql-atomone-testnet-1.allinbits.services/v1/graphql"
# ECLESIA_GRAPHQL_SECRET: ""
Expand Down
3 changes: 2 additions & 1 deletion packages/frontend-main/public/csp.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"https://cloud.umami.is",
"https://api-gateway.umami.dev",
"https://*.dither.chat",
"https://*.allinbits.com"
"https://*.allinbits.com",
"https://*.allinbits.services"
]
}
8 changes: 7 additions & 1 deletion packages/reader-main/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ services:
restart: always
container_name: chronosync
environment:
API_URLS: 'https://atomone-api.allinbits.com,https://atomone-rest.publicnode.com'
# Mainnet
# API_URLS: 'https://atomone-rest.publicnode.com'
#
# Testnet
API_URLS: 'https://atomone-testnet-1-api.allinbits.services'

START_BLOCK: '2605764'
BATCH_SIZE: 50
MEMO_PREFIX: dither.
RECEIVER: atone1uq6zjslvsa29cy6uu75y8txnl52mw06j6fzlep
AUTH: dev
COMMUNITY_ACCOUNT_V1: atone1uq6zjslvsa29cy6uu75y8txnl52mw06j6fzlep
# LOG: process.env.LOG === 'true' ? true : false,
command: [bun, run, start]
7 changes: 7 additions & 0 deletions packages/reader-main/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ let config: Config & {
AUTH: string;
ECLESIA_GRAPHQL_ENDPOINT?: string;
ECLESIA_GRAPHQL_SECRET?: string;
COMMUNITY_ACCOUNT_V1: string;
};

export function useConfig(): typeof config {
Expand All @@ -18,6 +19,10 @@ export function useConfig(): typeof config {
throw new Error(`AUTH must be set to a strong secret`);
}

if (!process.env.COMMUNITY_ACCOUNT_V1) {
throw new Error(`COMMUNITY_ACCOUNT_V1 value is required`);
}

config = {
API_URLS: process.env.API_URLS ? process.env.API_URLS.split(',') : [],
MEMO_PREFIX: process.env.MEMO_PREFIX,
Expand All @@ -30,6 +35,8 @@ export function useConfig(): typeof config {

ECLESIA_GRAPHQL_ENDPOINT: process.env.ECLESIA_GRAPHQL_ENDPOINT,
ECLESIA_GRAPHQL_SECRET: process.env.ECLESIA_GRAPHQL_SECRET,

COMMUNITY_ACCOUNT_V1: process.env.COMMUNITY_ACCOUNT_V1,
};

return config;
Expand Down
19 changes: 9 additions & 10 deletions packages/reader-main/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ChronoState } from '@atomone/chronostate';

import { useConfig } from './config/index';
import { EclesiaClient } from './eclesia/client';
import { MessageHandlers } from './messages/index';
import { messages } from './messages';
import { useQueue } from './queue';

const config = useConfig();
Expand Down Expand Up @@ -97,21 +97,20 @@ async function processAction(action: Action): Promise<ResponseStatus> {
}

const actionType = match[1];

const actionTypeKey = actionType as keyof typeof MessageHandlers;
if (!MessageHandlers[actionTypeKey]) {
console.warn(`Skipped ${action.hash}, unknown action type: ${actionType}`);
return 'SKIP';
}

const transfer = getTransferMessage(action.messages as unknown as Array<MsgGeneric>);
const quantity = getTransferQuantities(action.messages as unknown as Array<MsgGeneric>);
if (!transfer) {
console.warn(`No transfer provided, skipping. ${actionType}`);
return 'SKIP';
}

return await MessageHandlers[actionTypeKey]({ ...action, sender: transfer.from_address, quantity });
const handler = messages.get(transfer.to_address, actionType);
if (!handler) {
console.warn(`Skipped ${action.hash}, unknown action type: ${actionType}`);
return 'SKIP';
}

return await handler({ ...action, sender: transfer.from_address, quantity });
}

async function updateLastBlock(height: string, attempt = 0) {
Expand Down Expand Up @@ -216,7 +215,7 @@ export async function start() {

if (Number.parseInt(config.START_BLOCK) > lastBlockStored) {
console.info(`START_BLOCK is higher than last block stored, starting from START_BLOCK=${config.START_BLOCK}`);
config.START_BLOCK = lastBlockStored.toString();
startBlock = Number.parseInt(config.START_BLOCK);
} else {
startBlock = lastBlockStored;
}
Expand Down
41 changes: 23 additions & 18 deletions packages/reader-main/src/messages/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { Dislike } from './dislike';
import { Flag } from './flag';
import { Follow } from './follow';
import { Like } from './like';
import { Post } from './post';
import { Remove } from './remove';
import { Reply } from './reply';
import { Unfollow } from './unfollow';
import { useConfig } from '../config';
import { Messages } from './messages';
import { v1 } from './v1';

export const MessageHandlers = {
Dislike,
Flag,
Follow,
Like,
Post,
Remove,
Reply,
Unfollow,
};
const config = useConfig();

// Messages contains message handlers for different Dither protocol versions.
//
// Versions are defined by the address of the destination account used in the
// send messages.
//
// New versions must be created when the protocol changes because new functions
// are added, or because existing function parameters change. A new version means
// that the destination account for the send transactions must be changed to a
// different one.
//
// Dither protocol is open, it uses a memo field and a send message, so anyone
// can "pollute" Dither namespace sending multiple transactions using different
// non existent function names, which might be expected to be supported in the
// future. Apart from polluting the namespace they could also generate issues
// when replaying historical transactions to restore the data.
export const messages = new Messages({
[config.COMMUNITY_ACCOUNT_V1]: v1,
});
28 changes: 28 additions & 0 deletions packages/reader-main/src/messages/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { ActionWithData, ResponseStatus } from '../types';

export type MessageHandler = (action: ActionWithData) => Promise<ResponseStatus>;
export type Handlers = Record<string, Record<string, MessageHandler>>;

// Messages keeps track of message handlers for different versions.
export class Messages {
private handlers: Handlers;

constructor(handlers: Handlers) {
this.handlers = handlers;
}

// Registers messages for a specific version.
register(version: string, handlers: Record<string, MessageHandler>) {
this.handlers[version] = handlers;
}

// Gets a message handler by name for a specific version.
get(version: string, name: string): MessageHandler | undefined {
return this.handlers[version]?.[name];
}

// Lists registered versions.
versions(): string[] {
return Object.keys(this.handlers);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable ts/no-namespace */
import type { Posts } from '@atomone/dither-api-types';

import type { ActionWithData, ResponseStatus } from '../types/index';
import type { ActionWithData, ResponseStatus } from '../../types/index';

import process from 'node:process';

import { extractMemoContent } from '@atomone/chronostate';

import { useConfig } from '../config';
import { useConfig } from '../../config';

declare module '@atomone/chronostate' {
export namespace MemoExtractor {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable ts/no-namespace */
import type { Posts } from '@atomone/dither-api-types';

import type { ActionWithData, ResponseStatus } from '../types/index';
import type { ActionWithData, ResponseStatus } from '../../types/index';

import process from 'node:process';

import { extractMemoContent } from '@atomone/chronostate';

import { useConfig } from '../config';
import { useConfig } from '../../config';

declare module '@atomone/chronostate' {
export namespace MemoExtractor {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable ts/no-namespace */
import type { Posts } from '@atomone/dither-api-types';

import type { ActionWithData, ResponseStatus } from '../types/index';
import type { ActionWithData, ResponseStatus } from '../../types/index';

import process from 'node:process';

import { extractMemoContent } from '@atomone/chronostate';

import { useConfig } from '../config/index';
import { useConfig } from '../../config/index';

declare module '@atomone/chronostate' {
export namespace MemoExtractor {
Expand Down
19 changes: 19 additions & 0 deletions packages/reader-main/src/messages/v1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Dislike } from './dislike';
import { Flag } from './flag';
import { Follow } from './follow';
import { Like } from './like';
import { Post } from './post';
import { Remove } from './remove';
import { Reply } from './reply';
import { Unfollow } from './unfollow';

export const v1 = {
Dislike,
Flag,
Follow,
Like,
Post,
Remove,
Reply,
Unfollow,
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable ts/no-namespace */
import type { Posts } from '@atomone/dither-api-types';

import type { ActionWithData, ResponseStatus } from '../types/index';
import type { ActionWithData, ResponseStatus } from '../../types/index';

import process from 'node:process';

import { extractMemoContent } from '@atomone/chronostate';

import { useConfig } from '../config/index';
import { useConfig } from '../../config/index';

declare module '@atomone/chronostate' {
export namespace MemoExtractor {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable ts/no-namespace */
import type { Posts } from '@atomone/dither-api-types';

import type { ActionWithData, ResponseStatus } from '../types/index';
import type { ActionWithData, ResponseStatus } from '../../types/index';

import process from 'node:process';

import { extractMemoContent } from '@atomone/chronostate';

import { useConfig } from '../config/index';
import { useConfig } from '../../config/index';

declare module '@atomone/chronostate' {
export namespace MemoExtractor {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable ts/no-namespace */
import type { Posts } from '@atomone/dither-api-types';

import type { ActionWithData, ResponseStatus } from '../types/index';
import type { ActionWithData, ResponseStatus } from '../../types/index';

import process from 'node:process';

import { extractMemoContent } from '@atomone/chronostate';

import { useConfig } from '../config/index';
import { useConfig } from '../../config/index';

declare module '@atomone/chronostate' {
export namespace MemoExtractor {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable ts/no-namespace */
import type { Posts } from '@atomone/dither-api-types';

import type { ActionWithData, ResponseStatus } from '../types/index';
import type { ActionWithData, ResponseStatus } from '../../types/index';

import process from 'node:process';

import { extractMemoContent } from '@atomone/chronostate';

import { useConfig } from '../config/index';
import { useConfig } from '../../config/index';

declare module '@atomone/chronostate' {
export namespace MemoExtractor {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable ts/no-namespace */
import type { Posts } from '@atomone/dither-api-types';

import type { ActionWithData, ResponseStatus } from '../types/index';
import type { ActionWithData, ResponseStatus } from '../../types/index';

import process from 'node:process';

import { extractMemoContent } from '@atomone/chronostate';

import { useConfig } from '../config/index';
import { useConfig } from '../../config/index';

declare module '@atomone/chronostate' {
export namespace MemoExtractor {
Expand Down
Loading