Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/vsc-eco/vsc-node into main
Browse files Browse the repository at this point in the history
  • Loading branch information
vaultec81 committed Apr 3, 2024
2 parents c8a898b + 818a1ad commit 2152b87
Show file tree
Hide file tree
Showing 20 changed files with 778 additions and 179 deletions.
67 changes: 67 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
version: "3.3"

services:
vsc-node: # name of the service
build: .
image: vsc-node # the image to use
container_name: vsc-node_dev # what to label the container for docker ps
restart: always # restart if failed, until we stop it ourselves
#external_links:
#- mongo
depends_on:
- ipfs
- mongo
networks:
- vsc-node
env_file:
- .env
ports:
- 1338:1337
environment:
DEV: true
IPFS_HOST: http://ipfs:5201
MONGO_HOST: mongo:27017
volumes:
- node-modules-store:/home/github/app/node_modules
- ./data/vsc-node:/root/.vsc-node
- ./seed-backup.json:/root/.vsc-seed-backup.json
- ./.git/refs/heads/main:/root/git_commit
- ./src:/home/github/app/src

mongo:
container_name: mongo_vsc_dev
image: mongo:4.4.18
restart: always
ports:
- 127.0.0.1:27022:27017
networks:
- vsc-node
volumes:
- ./data/vsc-db:/data/db

ipfs:
container_name: ipfs-vsc_dev
image: ipfs/kubo:v0.18.1
restart: always
command:
- daemon
- --enable-pubsub-experiment
- --init-profile
- server
networks:
- vsc-node
ports:
- "4002:4002"
- "127.0.0.1:5201:5201"
environment:
IPFS_PATH: /etc/ipfs
volumes:
- ./data/ipfs:/etc/ipfs

volumes:
node-modules-store: {}
mongodb: {}

networks:
vsc-node:
driver: bridge
65 changes: 57 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@nestjs/platform-express": "^8.4.7",
"@summa-tx/bitcoin-spv-js": "^4.0.2",
"@transmute/did-key-bls12381": "^0.3.0-unstable.10",
"@types/node": "^20.12.2",
"@vsc.eco/client": "^0.0.4",
"@vsc.eco/sdk": "^0.1.3",
"ajv": "^6.12.6",
Expand Down
11 changes: 11 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ async function startup(): Promise<void> {

const api = new ApiModule(1337, core)
await api.listen()


const cleanup = async (code: number) => {
await core.stop()
await coreNew.stop()
await api.stop()
};

process.on("SIGINT", () => cleanup(0));
process.on("SIGTERM", () => cleanup(0));
process.on("beforeExit", cleanup);
}

void startup()
Expand Down
95 changes: 0 additions & 95 deletions src/modules/api/graphql/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,6 @@ export const DebugResolvers = {
}
}

// called recursively, as the object might consist of a nested JWS structure
const getDagCborIpfsHashContent = async (cid: CID) => {
let content = await appContainer.self.ipfs.dag.get(cid) as any;

if (typeof content === 'object' && content) {
// check if ipfs object is in JWS format, if so we need to go one layer below
const data = content.value;
if ('payload' in data && 'signatures' in data) {
const nestedCid: CID = (data as any).link as CID;
if (nestedCid.toString() === cid.toString()) {
return 'the ipfs object is in JWS format, but the link points to itself, this is not allowed!';
}

content = await getDagCborIpfsHashContent(nestedCid);
if (typeof content === 'object') {
content.link = data.link.toString()
content.payload = data.payload
content.signatures = data.signatures
} else {
content = {
data: content,
link: data.link.toString(),
payload: data.payload,
signatures: data.signatures
}
}
}
}

return content;
}

export const Resolvers = {
contractState: async (_, args) => {
// const data = await appContainer.self.contractEngine.contractDb.findOne({
Expand Down Expand Up @@ -349,69 +317,6 @@ export const Resolvers = {
})
return nextSlot;
},

// finds and tags vsc-tx/ vsc-blocks via ipfs CID's, unidentifiable CID's are tagged with the type 'null'
findCID: async (_, args) => {
if (appContainer.self.config.get('ipfs.pinEverything')) {
const ipfsHash = args.id;

let cid = null;
try {
cid = CID.parse(ipfsHash)
} catch {
throw new GraphQLError('Invalid CID format!')
}

// get ipfs content for cid
let content = null;
if (cid.code === 0x70) {
// dag-pd
const stream = appContainer.self.ipfs.cat(cid);
const chunks = [];

for await (const chunk of stream) {
chunks.push(chunk);
}

const buffer = Buffer.concat(chunks);
let dataRaw = buffer.toString();

try {
content = JSON.parse(dataRaw);
} catch (e) {
content = dataRaw;
}
} else if (cid.code === 0x71) {
// dag-cbor
content = await getDagCborIpfsHashContent(cid);
}

// determine the type of vsc data structure that was found e.g.: vsc-tx/ blocks
let type = null;
if (content && typeof content.value === 'object' && content.value.__t && ['vsc-tx', 'vsc-block'].includes(content.value.__t)) {
type = content.value.__t
}

let result: {
type: string,
data: any,
link?: string,
payload?: string,
signatures?: {
protected: string,
signature: string
}[]
} = { type: type, data: content.value }
if (content.payload && content.signatures && content.link) {
result.payload = content.payload
result.signatures = content.signatures
result.link = content.link
}
return result
} else {
throw new GraphQLError("Current node configuration does not allow for this endpoint to be used.")
}
},
submitTransactionV1: async (_, args) => {
const {id} = await appContainer.self.newService.transactionPool.ingestTx({
tx: args.tx,
Expand Down
8 changes: 0 additions & 8 deletions src/modules/api/graphql/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,6 @@ export const schema = `
enabled_at: Int
trusted: Boolean
}
type findCIDResult {
type: String
data: JSON
link: String
payload: String
signatures: JSON
}
interface BalanceController {
type: BalanceControllerType
authority: String
Expand Down Expand Up @@ -161,7 +154,6 @@ export const schema = `
# Need Revision
findContract(id: String): FindContractResult
findCID(id: String): findCIDResult
# End Need Revision
Expand Down
10 changes: 8 additions & 2 deletions src/modules/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import { Module } from '@nestjs/common'
import { INestApplication, Module } from '@nestjs/common'
import { NestFactory } from '@nestjs/core'
import { createSchema, createYoga } from 'graphql-yoga'
import { IPFSHTTPClient } from 'kubo-rpc-client'
Expand All @@ -24,6 +24,7 @@ class ControllerModule {}
* see api requirements here https://github.com/3speaknetwork/research/discussions/3
*/
export class ApiModule {
app: INestApplication
constructor(
private readonly listenPort: number,
private readonly self: CoreService
Expand All @@ -32,7 +33,8 @@ export class ApiModule {
}

public async listen() {
const app = await NestFactory.create(ControllerModule)
this.app = await NestFactory.create(ControllerModule)
const app = this.app

// Bring back API docs when needed. Mostly use already documented graphql
// const swaggerconfig = new DocumentBuilder().setTitle('VSC API').build()
Expand Down Expand Up @@ -78,4 +80,8 @@ export class ApiModule {

await app.listen(this.listenPort)
}

async stop() {
await this.app.close()
}
}
Loading

0 comments on commit 2152b87

Please sign in to comment.