-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/init-did-support' into init-did-…
…support # Conflicts: # src/rpc/index.ts
- Loading branch information
Showing
17 changed files
with
575 additions
and
382 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ node_modules | |
public | ||
build | ||
dist | ||
*.ts | ||
# *.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ node_modules | |
public | ||
build | ||
dist | ||
*.ts | ||
# *.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
} |
Oops, something went wrong.