Skip to content

Commit

Permalink
fix: added find method for Block and fixed the style for others
Browse files Browse the repository at this point in the history
  • Loading branch information
IOVgomezdn committed Mar 29, 2023
1 parent cf942f8 commit fce6b6b
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 94 deletions.
141 changes: 106 additions & 35 deletions src/converters/block.converters.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,113 @@
function rawBlockToEntity (data) {
function rawBlockToEntity ({
number,
hash,
miner,
parentHash,
sha3Uncles,
logsBloom,
transactionsRoot,
stateRoot,
receiptsRoot,
difficulty,
totalDifficulty,
extraData,
size,
gasLimit,
gasUsed,
timestamp,
minimumGasPrice,
bitcoinMergedMiningHeader,
bitcoinMergedMiningCoinbaseTransaction,
bitcoinMergedMiningMerkleProof,
hashForMergedMining,
paidFees,
cumulativeDifficulty,
received
}) {
return {
number: data.number,
hash: data.hash,
miner: data.miner,
parentHash: data.parentHash,
sha3Uncles: data.sha3Uncles,
logsBloom: data.logsBloom,
transactionsRoot: data.transactionsRoot,
stateRoot: data.stateRoot,
receiptsRoot: data.receiptsRoot,
difficulty: data.difficulty,
totalDifficulty: data.totalDifficulty,
extraData: data.extraData,
size: data.size,
gasLimit: data.gasLimit,
gasUsed: data.gasUsed,
timestamp: String(data.timestamp),
minimumGasPrice: data.minimumGasPrice,
bitcoinMergedMiningHeader: data.bitcoinMergedMiningHeader,
bitcoinMergedMiningCoinbaseTransaction: data.bitcoinMergedMiningCoinbaseTransaction,
bitcoinMergedMiningMerkleProof: data.bitcoinMergedMiningMerkleProof,
hashForMergedMining: data.hashForMergedMining,
paidFees: data.paidFees,
cumulativeDifficulty: data.cumulativeDifficulty,
received: String(data._received)
number,
hash,
miner,
parentHash,
sha3Uncles,
logsBloom,
transactionsRoot,
stateRoot,
receiptsRoot,
difficulty,
totalDifficulty,
extraData,
size,
gasLimit,
gasUsed,
timestamp: String(timestamp),
minimumGasPrice,
bitcoinMergedMiningHeader,
bitcoinMergedMiningCoinbaseTransaction,
bitcoinMergedMiningMerkleProof,
hashForMergedMining,
paidFees,
cumulativeDifficulty,
received: String(received)
}
}

function blockEntityToRaw (blockEntity) {
blockEntity['_received'] = Number(blockEntity.received)
delete blockEntity['received']

blockEntity['timestamp'] = Number(blockEntity['timestamp'])

blockEntity.uncles = blockEntity.uncle.map(uncleObj => uncleObj.hash)
delete blockEntity['uncle']

return blockEntity
function blockEntityToRaw ({
number,
hash,
miner,
parentHash,
sha3Uncles,
logsBloom,
transactionsRoot,
stateRoot,
receiptsRoot,
difficulty,
totalDifficulty,
extraData,
size,
gasLimit,
gasUsed,
timestamp,
minimumGasPrice,
bitcoinMergedMiningHeader,
bitcoinMergedMiningCoinbaseTransaction,
bitcoinMergedMiningMerkleProof,
hashForMergedMining,
paidFees,
cumulativeDifficulty,
received,
uncle,
transaction_transaction_block_numberToblock: transactions
}) {
return {
number,
hash,
miner,
parentHash,
sha3Uncles,
logsBloom,
transactionsRoot,
stateRoot,
receiptsRoot,
difficulty,
totalDifficulty,
extraData,
size,
gasLimit,
gasUsed,
timestamp: Number(timestamp),
minimumGasPrice,
bitcoinMergedMiningHeader,
bitcoinMergedMiningCoinbaseTransaction,
bitcoinMergedMiningMerkleProof,
hashForMergedMining,
paidFees,
cumulativeDifficulty,
_received: Number(received),
uncles: uncle.map(({hash}) => hash),
transactions: transactions.map(({hash}) => hash)
}
}

export {rawBlockToEntity, blockEntityToRaw}
71 changes: 24 additions & 47 deletions src/repositories/block.repository.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,36 @@
import {prismaClient} from '../lib/Setup'
import {rawBlockToEntity, blockEntityToRaw} from '../converters/block.converters'
import {mongoSortToPrisma} from './utils'
import {createPrismaOrderBy, mongoQueryToPrisma} from './utils'

const blockRelatedTables = {
uncle: {select: {hash: true}},
transaction_transaction_block_numberToblock: {select: {hash: true}}
}

export const blockRepository = {
async findOne (query = {}, project = {}, collection) {
const orderBy = []

if (project.sort) {
const sort = Object.entries(project.sort)[0]
let param = sort[0]
const value = mongoSortToPrisma(sort[1])

if (param === '_received') {
param = 'received'
}

const prismaSort = {
[param]: value
}

orderBy.push(prismaSort)
}

const requestedBlock = await prismaClient.block.findFirst({
where: query,
orderBy: orderBy,
include: {
uncle: {
select: {
hash: true
}
}
}
const blockToReturn = await prismaClient.block.findFirst({
where: mongoQueryToPrisma(query),
orderBy: createPrismaOrderBy(project),
include: blockRelatedTables
})

return requestedBlock ? blockEntityToRaw(requestedBlock) : null
},
find (query = {}, project = {}, collection, sort = {}, limit = 0, isArray = true) {
if (isArray) {
return collection
.find(query, project)
.sort(sort)
.limit(limit)
.toArray()
} else {
return collection
.find(query, project)
.sort(sort)
.limit(limit)
}
return blockToReturn ? blockEntityToRaw(blockToReturn) : null
},
countDocuments (query = {}, collection) {
return prismaClient.block.count({
where: query
async find (query = {}, project = {}, collection, sort = {}, limit = 0, isArray = true) {
const blocks = await prismaClient.block.findMany({
where: mongoQueryToPrisma(query),
orderBy: createPrismaOrderBy(project),
include: blockRelatedTables,
take: limit
})

return blocks.map(blockEntityToRaw)
},
async countDocuments (query = {}, collection) {
const count = await prismaClient.block.count({where: mongoQueryToPrisma(query)})

return count
},
async insertOne (data, collection) {
await prismaClient.block.create({data: rawBlockToEntity(data)})
Expand Down
25 changes: 13 additions & 12 deletions src/services/classes/CheckBlocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,19 +209,20 @@ export class CheckBlocks extends BlocksBase {

export const checkBlocksCongruence = async (blocksCollection, lastBlock) => {
try {
let blocks = {}
let query = (lastBlock) ? { number: { $lt: lastBlock } } : {}
await blockRepository.find(query, { _id: 0, number: 1, hash: 1, parentHash: 1 }, blocksCollection, { number: -1 }, 0, false)
.forEach(block => {
blocks[block.number] = block
})
let missing = []
let invalid = []
for (let number in blocks) {
const query = (lastBlock) ? { number: { $lt: lastBlock } } : {}
const blocks = await blockRepository.find(query, { _id: 0, number: 1, hash: 1, parentHash: 1 }, blocksCollection, { number: -1 }, 0, false)

for (const block of blocks) {
blocks[block.number] = block
}

const missing = []
const invalid = []
for (const number in blocks) {
if (number > 0) {
let block = blocks[number]
let parentNumber = number - 1
let parent = blocks[parentNumber]
const block = blocks[number]
const parentNumber = number - 1
const parent = blocks[parentNumber]
if (!parent) {
missing.push(parentNumber)
} else {
Expand Down

0 comments on commit fce6b6b

Please sign in to comment.