Skip to content

Commit

Permalink
poc
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcos20 committed Oct 12, 2023
1 parent 4a22929 commit 30b99df
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 19 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,14 @@ Now, you should see the nodes discovery/connecting/disconnecting

Load postman collection from docs and play




## Structure:
- Everything hovers around components:
- database: will have connection to typesense/es and will implement basic operations. This is used by all other components
- indexer: upcoming indexer feature
- provider: will have core provider functionality
- httpRoutes: exposes http endpoints
- P2P: has P2P functionality. will have to extend handleBroadcasts and handleProtocolCommands, rest is pretty much done

22 changes: 22 additions & 0 deletions src/@types/OceanNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,35 @@ import { OceanProvider } from "../components/Provider/index"
import { OceanIndexer } from "../components/Indexer/index"
import {Stream} from "stream"



export interface OceanNodeDBConfig {
host:string,
user:string,
pwd:string,
dbname:string
}

export interface OceanNodeConfig {
hasP2P:boolean,
hasIndexer: boolean,
hasProvider: boolean,
pk:string,
dbConfig: OceanNodeDBConfig,
httpPort:number
}


export interface OceanNode {
node:OceanP2P | null,
indexer: OceanIndexer| null,
provider: OceanProvider| null
}





export interface P2PCommandResponse {
status: any,
stream: Stream | null
Expand Down
9 changes: 8 additions & 1 deletion src/components/Indexer/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
export class OceanIndexer{}
import { Database } from "../database"

export class OceanIndexer{
private db:Database
constructor (db:Database) {
this.db=db
}
}
8 changes: 7 additions & 1 deletion src/components/P2P/handleProtocolCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ export async function handleProtocolCommands ( connection:any){
case 'download':
sendStream=fs.createReadStream("/var/log/syslog")
//sendStream=fs.createReadStream("/etc/hostname")
status={httpStatus:200,filename: "hostname"}
status={
httpStatus:200,
headers:{
"Content-Disposition": "attachment; filename='syslog'",
"Content-Type":"application/text"
}
}
break;
default:
status={httpStatus:501,error:"Unknown command"}
Expand Down
5 changes: 4 additions & 1 deletion src/components/P2P/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {getPeerIdFromPrivateKey} from './peer-id'

import {cidFromRawString} from '../../utils'
import { Stream,Transform } from 'stream'
import { Database } from '../database'

const DEFAULT_OPTIONS = {
pollInterval: 1000
Expand All @@ -59,8 +60,10 @@ export class OceanP2P extends EventEmitter {
private _handleMessage: any
private _interval: NodeJS.Timeout
private _idx: number
constructor () {
private db:Database
constructor (db:Database) {
super()
this.db=db
}
async start(options:any=null){
const topic = 'oceanprotocol'
Expand Down
7 changes: 6 additions & 1 deletion src/components/Provider/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export class OceanProvider{
import { Database } from "../database"

export class OceanProvider{
private db:Database
constructor (db:Database) {
this.db=db
}
}
8 changes: 8 additions & 0 deletions src/components/database/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {OceanNodeDBConfig} from '../../@types/OceanNode'


export class Database{
constructor (config:OceanNodeDBConfig) {

}
}
5 changes: 3 additions & 2 deletions src/components/httpRoutes/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ directCommandRoute.post('/directCommand', express.json(),async (req: Request, re
const str=uint8ArrayToString(chunk.subarray())
const decoded=JSON.parse(str)
res.status(decoded.httpStatus)
res.header({'Content-Type': 'text/plain'})
res.header({'sdfdsf': 'sdfdsf'})
if ("headers" in decoded){
res.header(decoded.headers)
}
if(decoded.httpStatus!=200){
res.write(decoded.error)
res.end()
Expand Down
33 changes: 21 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { OceanP2P } from "./components/P2P/index"
import { OceanProvider } from "./components/Provider/index"
import { OceanIndexer } from "./components/Indexer/index"
import { getRandomInt} from './utils'
import { Database } from "./components/database/index"
import express, { Express, Request, Response } from 'express';
import { OceanNode} from './@types/index'
import swaggerUi from "swagger-ui-express";
import {httpRoutes} from './components/httpRoutes'
import {getConfig} from './utils'

let node:any
let oceanNode:OceanNode

const app: Express = express();
//const port = getRandomInt(6000,6500)
let port = parseInt(process.env.PORT)
if(isNaN(port))
port=8000


declare global {
namespace Express {
Expand All @@ -26,12 +25,22 @@ declare global {


async function main(){
const node=new OceanP2P()
await node.start()
await node.startListners()
const indexer = new OceanIndexer()
const provider = new OceanProvider()
oceanNode = {
const config = await getConfig()
let node=null
let indexer=null
let provider=null
let dbconn=new Database(config.dbConfig)
if (config.hasP2P){
node=new OceanP2P(dbconn)
await node.start()
await node.startListners()
}
if(config.hasIndexer)
indexer = new OceanIndexer(dbconn)
if(config.hasProvider)
provider = new OceanProvider(dbconn)

const oceanNode = {
node: node,
indexer: indexer,
provider: provider
Expand All @@ -53,8 +62,8 @@ async function main(){
);
app.use('/', httpRoutes);

app.listen(port, () => {
console.log(`HTTP port: ${port}`)
app.listen(config.httpPort, () => {
console.log(`HTTP port: ${config.httpPort}`)
})


Expand Down
23 changes: 23 additions & 0 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {OceanNodeConfig} from '../@types/OceanNode'


export async function getConfig():Promise<OceanNodeConfig>{
let port = parseInt(process.env.PORT)
if(isNaN(port))
port=8000

const config:OceanNodeConfig = {
hasIndexer:true,
hasP2P:true,
hasProvider:true,
httpPort: port,
dbConfig: {
dbname:'oceannode',
host:'127.0.0.1',
user: 'oceannode',
pwd: 'oceannode'
},
pk:process.env.PRIVATE_KEY
}
return(config)
}
3 changes: 2 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './conversions'
export * from './conversions'
export * from './config'

0 comments on commit 30b99df

Please sign in to comment.