Skip to content

Commit

Permalink
Port engine with placeholder types and interfaces (#3)
Browse files Browse the repository at this point in the history
* Add empty types for engine and client

* Add engine components with empty types

* Fix lint errors

* Add typed placeholders to channel fields in Engine class

* Add methods to ChainService interface

* Add methods to MessageService interface

* Update test method import
  • Loading branch information
prathamesh0 authored May 22, 2023
1 parent 4c4f820 commit f120c7f
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/example-web-app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from 'react';

import test from '@cerc-io/nitro-client';
import { test } from '@cerc-io/nitro-client';

import logo from './logo.svg';
import './App.css';
Expand Down
4 changes: 3 additions & 1 deletion packages/nitro-client/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
{
"devDependencies": ["webpack.*.ts", "test/**/*.test.ts"]
}
]
],
"max-classes-per-file": "off",
"import/prefer-default-export": "off"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Proposal is a proposal either to add or to remove a guarantee.
//
// Exactly one of {toAdd, toRemove} should be non nil.
// TODO Implement
export class Proposal {}
1 change: 1 addition & 0 deletions packages/nitro-client/src/client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Client
18 changes: 18 additions & 0 deletions packages/nitro-client/src/client/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { MessageService } from './engine/messageservice/messageservice';
import { ChainService } from './engine/chainservice/chainservice';
import { Store } from './engine/store/store';
import { PolicyMaker } from './engine/policy-maker';
import { VoucherManager } from '../payments/voucher-manager';
import { Engine } from './engine/engine';

export class Client {
// The core business logic of the client
private engine: Engine;

private vm: VoucherManager;

constructor(msg: MessageService, chain: ChainService, store: Store, policymaker: PolicyMaker) {
this.vm = new VoucherManager();
this.engine = new Engine(this.vm, msg, chain, store, policymaker);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { GoReceivingChannelPlaceholder } from '../../../go-channel';
import { ChainTransaction } from '../../../protocols/interfaces';

// Event dictates which methods all chain events must implement
// TODO: Add methods
export interface Event {}

// TODO: Add eth chainservice implementation
export interface ChainService {
eventFeed (): GoReceivingChannelPlaceholder<Event>;

// TODO: Use protocols chain transaction type
// TODO: Can throw an error
sendTransaction (tx: ChainTransaction): void;

// TODO: Use Address type
getConsensusAppAddress (): string;

getVirtualPaymentAppAddress (): string;

// TODO: Can throw an error
getChainId (): number;

// TODO: Can throw an error
close (): void;
}
66 changes: 66 additions & 0 deletions packages/nitro-client/src/client/engine/engine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { GoChannelPlaceholder, GoReceivingChannelPlaceholder } from '../../go-channel';
import { MessageService } from './messageservice/messageservice';
import { ChainService, Event as ChainEvent } from './chainservice/chainservice';
import { Store } from './store/store';
import { PolicyMaker } from './policy-maker';
import { MetricsRecorder } from './metrics';
import { VoucherManager } from '../../payments/voucher-manager';
import { ObjectiveRequest } from '../../protocols/interfaces';
import { Message } from '../../protocols/messages';
import { Proposal } from '../../channel/consensus_channel/consensus_channel';

export class Engine {
objectiveRequestsFromAPI?: GoChannelPlaceholder<ObjectiveRequest>;

paymentRequestsFromAPI?: GoChannelPlaceholder<PaymentRequest>;

private fromChain?: GoReceivingChannelPlaceholder<ChainEvent>;

private fromMsg?: GoReceivingChannelPlaceholder<Message>;

private fromLedger?: GoChannelPlaceholder<Proposal>;

private toApi?: GoChannelPlaceholder<EngineEvent>;

private stop?: GoChannelPlaceholder<void>;

private msg: MessageService;

private chain: ChainService;

// A Store for persisting and restoring important data
private store: Store;

// A PolicyMaker decides whether to approve or reject objectives
private policymaker: PolicyMaker;

// logger zerolog.Logger

private metrics?: MetricsRecorder;

private vm: VoucherManager;

constructor(
vm: VoucherManager,
msg: MessageService,
chain: ChainService,
store: Store,
policymaker: PolicyMaker,
) {
this.vm = vm;

this.msg = msg;
this.chain = chain;
this.store = store;

this.policymaker = policymaker;
}
}

export type PaymentRequest = {
channelId: string
amount: number
};

// TODO Implement
export class EngineEvent {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { GoReceivingChannelPlaceholder } from '../../../go-channel';
import { Message } from '../../../protocols/messages';

// TODO: Add p2p implementation
// TODO: Add tests
export interface MessageService {
// TODO: Update comments

// Out returns a chan for receiving messages from the message service
out (): GoReceivingChannelPlaceholder<Message>;

// Send is for sending messages with the message service
// TODO: Use protocols message type
send (msg: Message): void;

// Close closes the message service
// TODO: Can throw an error
close (): void;
}
2 changes: 2 additions & 0 deletions packages/nitro-client/src/client/engine/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// TODO: Implement
export class MetricsRecorder {}
2 changes: 2 additions & 0 deletions packages/nitro-client/src/client/engine/policy-maker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// TODO: Add methods
export interface PolicyMaker {}
2 changes: 2 additions & 0 deletions packages/nitro-client/src/client/engine/store/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// TODO: Add methods
export interface Store {}
6 changes: 6 additions & 0 deletions packages/nitro-client/src/go-channel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* eslint max-classes-per-file: 0 */
/* eslint @typescript-eslint/no-unused-vars: 0 */

// TODO: Replace
export class GoChannelPlaceholder<T> {}
export class GoReceivingChannelPlaceholder<T> {}
4 changes: 2 additions & 2 deletions packages/nitro-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const test = (): string => {
export const test = (): string => {
// eslint-disable-next-line no-console
console.log('Test from nitro-client');

return 'test output';
};

export default test;
export { Client } from './client/client';
2 changes: 2 additions & 0 deletions packages/nitro-client/src/payments/voucher-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// TODO: Implement
export class VoucherManager {}
7 changes: 7 additions & 0 deletions packages/nitro-client/src/protocols/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// ChainTransaction defines the interface that every transaction must implement
// TODO: Add methods
export interface ChainTransaction {}

// ObjectiveRequest is a request to create a new objective.
// TODO: Add methods
export interface ObjectiveRequest {}
2 changes: 2 additions & 0 deletions packages/nitro-client/src/protocols/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// TODO Implement
export class Message {}
2 changes: 1 addition & 1 deletion packages/nitro-client/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import test from '../src/index';
import { test } from '../src/index';

describe('testFunction', () => {
it('should return the expected result', () => {
Expand Down

0 comments on commit f120c7f

Please sign in to comment.