Refactor part of forum-node into network-node

This commit is contained in:
Ladd Hoffman 2023-01-30 20:59:16 -06:00
parent ab63ad1868
commit 5fc5bbe0b5
4 changed files with 63 additions and 47 deletions

View File

@ -1,62 +1,21 @@
import { Actor } from './actor.js';
import { Action } from './action.js'; import { Action } from './action.js';
import { import {
Message, PostMessage, PeerMessage, messageFromJSON, Message, PostMessage, PeerMessage, messageFromJSON,
} from './message.js'; } from './message.js';
import { CryptoUtil } from './crypto.js'; import { CryptoUtil } from './crypto.js';
import { ForumView } from './forum-view.js'; import { ForumView } from './forum-view.js';
import { PrioritizedQueue } from './prioritized-queue.js'; import { NetworkNode } from './network-node.js';
export class ForumNode extends Actor { export class ForumNode extends NetworkNode {
constructor(name, scene) { constructor(name, scene) {
super(name, scene); super(name, scene);
this.forumView = new ForumView(); this.forumView = new ForumView();
this.queue = new PrioritizedQueue();
this.actions = { this.actions = {
...this.actions,
storePost: new Action('store post', scene), storePost: new Action('store post', scene),
peerMessage: new Action('peer message', scene),
}; };
} }
// Generate a signing key pair and connect to the network
async initialize(forumNetwork) {
this.keyPair = await CryptoUtil.generateAsymmetricKey();
this.forumNetwork = forumNetwork.addNode(this);
this.status.set('Initialized');
return this;
}
// Send a message to all other nodes in the network
async broadcast(message) {
await message.sign(this.keyPair);
const otherForumNodes = this.forumNetwork
.listNodes()
.filter((forumNode) => forumNode.keyPair.publicKey !== this.keyPair.publicKey);
for (const forumNode of otherForumNodes) {
// For now just call receiveMessage on the target node
// await this.actions.peerMessage.log(this, forumNode, null, message.content);
await this.actions.peerMessage.log(this, forumNode);
await forumNode.receiveMessage(JSON.stringify(message.toJSON()));
}
}
// Perform minimal processing to ingest a message.
// Enqueue it for further processing.
async receiveMessage(messageStr) {
const messageJson = JSON.parse(messageStr);
const senderReputation = this.forumView.getReputation(messageJson.publicKey) || 0;
this.queue.add(messageJson, senderReputation);
}
// Process next highest priority message in the queue
async processNextMessage() {
const messageJson = this.queue.pop();
if (!messageJson) {
return null;
}
return this.processMessage(messageJson);
}
// Process a message from the queue // Process a message from the queue
async processMessage(messageJson) { async processMessage(messageJson) {
try { try {

View File

@ -0,0 +1,57 @@
import { Actor } from './actor.js';
import { Action } from './action.js';
import { CryptoUtil } from './crypto.js';
import { PrioritizedQueue } from './prioritized-queue.js';
export class NetworkNode extends Actor {
constructor(name, scene) {
super(name, scene);
this.queue = new PrioritizedQueue();
this.actions = {
peerMessage: new Action('peer message', scene),
};
}
// Generate a signing key pair and connect to the network
async initialize(forumNetwork) {
this.keyPair = await CryptoUtil.generateAsymmetricKey();
this.forumNetwork = forumNetwork.addNode(this);
this.status.set('Initialized');
return this;
}
// Send a message to all other nodes in the network
async broadcast(message) {
await message.sign(this.keyPair);
const otherForumNodes = this.forumNetwork
.listNodes()
.filter((forumNode) => forumNode.keyPair.publicKey !== this.keyPair.publicKey);
for (const forumNode of otherForumNodes) {
// For now just call receiveMessage on the target node
// await this.actions.peerMessage.log(this, forumNode, null, message.content);
await this.actions.peerMessage.log(this, forumNode);
await forumNode.receiveMessage(JSON.stringify(message.toJSON()));
}
}
// Perform minimal processing to ingest a message.
// Enqueue it for further processing.
async receiveMessage(messageStr) {
const messageJson = JSON.parse(messageStr);
const senderReputation = this.forumView.getReputation(messageJson.publicKey) || 0;
this.queue.add(messageJson, senderReputation);
}
// Process next highest priority message in the queue
async processNextMessage() {
const messageJson = this.queue.pop();
if (!messageJson) {
return null;
}
return this.processMessage(messageJson);
}
// Process a message from the queue
// async processMessage(messageJson) {
// }
}

View File

@ -1,4 +1,4 @@
export class ForumNetwork { export class Network {
constructor() { constructor() {
this.nodes = new Map(); this.nodes = new Map();
} }

View File

@ -3,7 +3,7 @@ import { Scene } from '../../classes/scene.js';
import { PostContent } from '../../classes/post-content.js'; import { PostContent } from '../../classes/post-content.js';
import { Expert } from '../../classes/expert.js'; import { Expert } from '../../classes/expert.js';
import { ForumNode } from '../../classes/forum-node.js'; import { ForumNode } from '../../classes/forum-node.js';
import { ForumNetwork } from '../../classes/forum-network.js'; import { Network } from '../../classes/network.js';
import { CryptoUtil } from '../../classes/crypto.js'; import { CryptoUtil } from '../../classes/crypto.js';
import { delay } from '../../util.js'; import { delay } from '../../util.js';
@ -15,7 +15,7 @@ window.scene = new Scene('Forum Network test', rootBox).withSequenceDiagram();
window.author1 = await new Expert('author1', window.scene).initialize(); window.author1 = await new Expert('author1', window.scene).initialize();
window.author2 = await new Expert('author2', window.scene).initialize(); window.author2 = await new Expert('author2', window.scene).initialize();
window.forumNetwork = new ForumNetwork(); window.forumNetwork = new Network();
window.forumNode1 = await new ForumNode('node1', window.scene).initialize( window.forumNode1 = await new ForumNode('node1', window.scene).initialize(
window.forumNetwork, window.forumNetwork,