Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/init-did-support' into init-did-…
Browse files Browse the repository at this point in the history
…support

# Conflicts:
#	src/rpc/index.ts
  • Loading branch information
Igx22 committed Oct 18, 2024
2 parents d043d5c + 6ec1646 commit 6b09e3b
Show file tree
Hide file tree
Showing 17 changed files with 575 additions and 382 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ node_modules
public
build
dist
*.ts
# *.ts
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ node_modules
public
build
dist
*.ts
# *.ts
35 changes: 19 additions & 16 deletions src/helpers/dbHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import pgPromise from 'pg-promise'
import { IClient } from 'pg-promise/typescript/pg-subset'
import { DateTime } from 'ts-luxon'

import { InitDid } from '../generated/push/block_pb'
import { InitDid, Transaction } from '../generated/push/block_pb'
import log from '../loaders/logger'
import { PushKeys } from '../services/transactions/pushKeys'
import { PushWallets } from '../services/transactions/pushWallets'
Expand All @@ -20,7 +20,7 @@ import { WinstonUtil } from '../utilz/winstonUtil'
// todo move everything into PgUtil including connection management
// todo use PgUtil
// todo use placeholders (?)

// todo: move the query to specific service files
const logger = WinstonUtil.newLog('pg')
const options = {
query: function (e) {
Expand Down Expand Up @@ -354,20 +354,21 @@ END $$ LANGUAGE plpgsql;
nsIndex: string,
ts: string,
skey: string,
body: string
body: Transaction
) {
const transactionObj = body.toObject()
log.debug(`putValueInStorageTable() namespace=${ns}, namespaceShardId=${shardId}
, skey=${skey}, jsonValue=${body}`)
, skey=${skey}, jsonValue=${transactionObj}`)
const sql = `INSERT INTO storage_node (namespace, namespace_shard_id, namespace_id, ts, skey, dataschema, payload)
values (\${ns}, \${shardId}, \${nsIndex}, to_timestamp(\${ts}), \${skey}, 'v1', \${body})
ON CONFLICT (namespace, namespace_shard_id, namespace_id, skey) DO UPDATE SET payload = \${body}`
values (\${ns}, \${shardId}, \${nsIndex}, to_timestamp(\${ts}), \${skey}, 'v1', \${transactionObj})
ON CONFLICT (namespace, namespace_shard_id, namespace_id, skey) DO UPDATE SET payload = \${transactionObj}`
const params = {
ns,
shardId,
nsIndex,
ts,
skey,
body
transactionObj
}
return pgPool
.none(sql, params)
Expand All @@ -382,18 +383,20 @@ END $$ LANGUAGE plpgsql;
})
}

static async indexTransactionCategory(ns: string, body: string) {
const parsedBody = typeof body == 'string' ? JSON.parse(body) : body
static async indexTransactionCategory(ns: string, body: Transaction) {
// parent function that calls functions to store and index trxs based on category
if (ns == 'INIT_DID') {
const deserializedData = InitDid.deserializeBinary(
BitUtil.base64ToBytes(parsedBody.data)
).toObject()
const masterPubKey = deserializedData.masterpubkey
const did = parsedBody['sender']
const derivedKeyIndex = deserializedData.derivedkeyindex
const derivedPublicKey = deserializedData.derivedpubkey
const walletToEncodedDerivedKeyMap = arrayToMap(deserializedData.wallettoencderivedkeyMap)
BitUtil.base64ToBytes(body.getData_asB64())
)
const masterPubKey = deserializedData.getMasterpubkey()
const did = body.getSender()
const derivedKeyIndex = deserializedData.getDerivedkeyindex()
const derivedPublicKey = deserializedData.getDerivedpubkey()
// was not able to use inbuilt proto methods to get the data
const walletToEncodedDerivedKeyMap = arrayToMap(
deserializedData.toObject().wallettoencderivedkeyMap
)
await DbHelper.putInitDidTransaction({
masterPublicKey: masterPubKey,
did: did,
Expand Down
10 changes: 8 additions & 2 deletions src/rpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@
// TODO convert this to a single rpc object???

import { GetAccountInfo } from './get_accountInfo'
import { PushPutBlock } from './push_putBlock'
import { PushPutBlockHash } from './push_putBlockHash'
import { StorageGetTransaction } from './storage_getTransaction'
import { StorageGetTransactions } from './storage_getTransactions'
// put the controllers here
const controllers = {
push_getTransactions: StorageGetTransactions.storageGetTransactions,
storage_getTransaction: StorageGetTransaction.storageGetTransaction,
push_accountInfo: GetAccountInfo.getAccountInfo
push_accountInfo: GetAccountInfo.getAccountInfo,
push_putBlockHash: PushPutBlockHash.pushPutBlockHash,
push_putBlock: PushPutBlock.pushPutBlock
}

// put the after test controllers here, keep in mind to keep the function name same as controller
const afterController = {
push_getTransactions: StorageGetTransactions.afterStorageGetTransactions,
storage_getTransaction: StorageGetTransaction.afterStorageGetTransaction,
push_accountInfo: GetAccountInfo.afterGetAccountInfo
push_accountInfo: GetAccountInfo.afterGetAccountInfo,
push_putBlockHash: PushPutBlockHash.afterPushPutBlockHash,
push_putBlock: PushPutBlock.afterPushPutBlock
}

export const rpcControllerConfigs = {
Expand Down
64 changes: 64 additions & 0 deletions src/rpc/push_putBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import Container from 'typedi'
import { z } from 'zod'

import LoggerInstance from '../loaders/logger'
import StorageNode from '../services/messaging/storageNode'

const BlockItem = z.object({
id: z.number().optional(),
object_hash: z.string().optional(),
object: z.string()
})
const PushPutBlockParamsSchema = z.object({
blocks: z.array(BlockItem),
signature: z.string()
})

type PushPutBlockParams = z.infer<typeof PushPutBlockParamsSchema>
type BlockItemType = { id?: number; object_hash?: string; object: string }
export class PushPutBlock {
constructor() {}

public static contructErrorMessage(errorMessage: string) {
const error = new Error(errorMessage) as any
error.data = { error: errorMessage }
return error
}

public static async pushPutBlock(params: PushPutBlockParams) {
const { blocks, signature } = params

// Validate the transaction parameters
const validationRes = PushPutBlock.validatePushPutBlock(params)
if (!validationRes) {
return new Error('Invalid params')
}
const st: StorageNode = await Container.get(StorageNode)
const result = await Promise.all(
blocks.map(async (block) => {
try {
if (!block.object) {
throw new Error('Block object is missing')
}
await st.accept(block as BlockItemType)
return 'SUCCESS'
} catch (error) {
return { block, status: 'FAIL', error: error.message }
}
})
)
return result
}

public static validatePushPutBlock(params: PushPutBlockParams) {
try {
// TODO: add validation for signature
return true
} catch (e) {
LoggerInstance.error('Error validating get transaction:', e)
return false
}
}

public static afterPushPutBlock = [(params, result, raw) => console.log('Block result:', result)]
}
44 changes: 44 additions & 0 deletions src/rpc/push_putBlockHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { z } from 'zod'

import LoggerInstance from '../loaders/logger'
import { Block } from '../services/block/block'

const PushPutBlockHashParamsSchema = z.object({
hashes: z.array(z.string()),
signature: z.string()
})

type PushPutBlockHashParams = z.infer<typeof PushPutBlockHashParamsSchema>
export class PushPutBlockHash {
constructor() {}

public static contructErrorMessage(errorMessage: string) {
const error = new Error(errorMessage) as any
error.data = { error: errorMessage }
return error
}

public static async pushPutBlockHash(params: PushPutBlockHashParams) {
const { hashes, signature } = params
const validationRes = PushPutBlockHash.validatePushPutBlockHash(params)
if (!validationRes) {
return new Error('Invalid params')
}
const res = Block.getBulkBlocksByHash(hashes)
return res
}

public static validatePushPutBlockHash(params: PushPutBlockHashParams) {
try {
// TODO: add validation for signature
return true
} catch (e) {
LoggerInstance.error('Error validating get transaction:', e)
return false
}
}

public static afterPushPutBlockHash = [
(params, result, raw) => console.log('Block result:', result)
]
}
2 changes: 1 addition & 1 deletion src/rpc/storage_getTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type StorageGetTransactionsParams = z.infer<typeof StorageGetTransactionsParamsS

export class StorageGetTransaction {
constructor() {}

// TODO: Make it a util function or part of base class
public static contructErrorMessage(errorMessage: string) {
const error = new Error(errorMessage) as any
error.data = { error: errorMessage }
Expand Down
29 changes: 29 additions & 0 deletions src/services/block/block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Service } from 'typedi'

import { PgUtil } from '../../utilz/pgUtil'

@Service()
export class Block {
constructor() {}

static async getBlockByHash(blockHash: string) {
const query = `SELECT * FROM blocks WHERE block_hash = $1`
const result = await PgUtil.queryOneRow(query, [blockHash])
return { result: result }
}

static async getBulkBlocksByHash(blockHashes: string[]) {
const query = `SELECT object_hash FROM blocks WHERE object_hash = ANY($1::text[])`
const result = await PgUtil.queryArr<{ id: number; object: string; object_hash: string }>(
query,
[blockHashes]
)
console.log('result:', result)
const foundHashes = result.map((row) => row.object_hash)

const statusArray = blockHashes.map((hash) =>
foundHashes.includes(hash) ? 'SEND' : 'NOT_SEND'
)
return { result: statusArray }
}
}
Loading

0 comments on commit 6b09e3b

Please sign in to comment.