Skip to content

Commit

Permalink
Merge pull request #176 from rsksmart/staging
Browse files Browse the repository at this point in the history
Merge Staging into Develop
  • Loading branch information
nicov-iov authored Dec 18, 2024
2 parents d7c7996 + 1b7958e commit c309925
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 29 deletions.
4 changes: 4 additions & 0 deletions prisma/rsk-explorer-database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

/*
V1.1.4 Notes:
- add index on table transaction_in_pool(pool_id)
V1.1.3 Notes:
- add index on table contract(symbol)
Expand Down Expand Up @@ -181,6 +184,7 @@ status VARCHAR NOT NULL,
CONSTRAINT pk_transaction_in_pool_hash_poolId PRIMARY KEY (hash, pool_id),
CONSTRAINT fk_transaction_in_pool_poolId FOREIGN KEY (pool_id) REFERENCES tx_pool(id)
);
CREATE INDEX idx_transaction_in_pool_pool_id ON transaction_in_pool(pool_id);

CREATE TABLE address (
id SERIAL,
Expand Down
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ model transaction_in_pool {
tx_pool tx_pool @relation(fields: [poolId], references: [id], onDelete: NoAction, onUpdate: NoAction, map: "fk_transaction_in_pool_pool_id")
@@id([hash, poolId], map: "pk_transaction_in_pool_hash_poolid")
@@index([poolId], map: "idx_transaction_in_pool_pool_id")
}

model transaction_pending {
Expand Down
8 changes: 7 additions & 1 deletion src/api/TxPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class TxPool extends DataCollector {
start () {
super.start()
this.log.info('Tx Pool started')
this.updatePool()
this.tick()
}

tick () {
Expand All @@ -22,12 +22,18 @@ export class TxPool extends DataCollector {

async updatePool () {
try {
// this.log.debug(`Checking for new tx pools... (checking every ${this.tickDelay} ms)`)
let pool = await this.getPool()
if (pool && pool.timestamp !== this.state.timestamp) {
// this.log.debug("New tx pool detected")
this.state = Object.assign({}, pool)
this.events.emit('newPool', this.getState())

// this.log.debug("Updating tx pool chart...")
await this.updatePoolChart()
this.events.emit('poolChart', this.getPoolChart())
} else {
// this.log.debug("No new tx pool detected")
}
} catch (err) {
this.log.error(err)
Expand Down
4 changes: 2 additions & 2 deletions src/api/modules/Address.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class Address extends DataCollectorItem {
getAddresses: params => {
let type = (params.query) ? params.query.type : null
let query = (type) ? { type } : {}
return this.getPageData(query, params, { deleteCodeAndInput: true })
return this.getPageData(query, params, { useV2: true, action: 'getAddresses' })
},
/**
* @swagger
Expand Down Expand Up @@ -163,7 +163,7 @@ export class Address extends DataCollectorItem {
}
}

return this.getPageData(query, params, { deleteCodeAndInput: true })
return this.getPageData(query, params, { useV2: true, action: 'getTokens' })
},
/**
* @swagger
Expand Down
3 changes: 3 additions & 0 deletions src/converters/address.converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function rawContractToEntity ({
}

function addressEntityToRaw ({
id,
address,
address_latest_balance_address_latest_balance_addressToaddress: latestBalance,
isNative,
Expand Down Expand Up @@ -113,6 +114,8 @@ function addressEntityToRaw ({
if (addressToReturn.createdByTx) delete addressToReturn.createdByTx.input
}

addressToReturn.id = id

return removeNullFields(addressToReturn, ['name'])
}

Expand Down
23 changes: 16 additions & 7 deletions src/lib/prismaClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@ const prismaClient = new PrismaClient({
url: `${protocol}${user}:${password}@${host}:${port}/${databaseName}?connection_limit=${connectionLimit}`
}
},
// log: ['query', 'info', 'warn', 'error'],
errorFormat: 'pretty'
})

// prismaClient.$on('query', (e) => {
// console.log('Query: ' + e.query)
// console.log('Params: ' + e.params)
// console.log('Duration: ' + e.duration + 'ms')
// })
const prismaClientWithLogging = new PrismaClient({
datasources: {
db: {
url: `${protocol}${user}:${password}@${host}:${port}/${databaseName}?connection_limit=${connectionLimit}`
}
},
log: ['query', 'info', 'warn', 'error'],
errorFormat: 'pretty'
})

prismaClientWithLogging.$on('query', (e) => {
console.log('Query: ' + e.query)
console.log('Params: ' + e.params)
console.log('Duration: ' + e.duration + 'ms')
})

export { prismaClient }
export { prismaClient, prismaClientWithLogging }
24 changes: 5 additions & 19 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,27 +154,13 @@ export const hash = (thing, alg = 'sha1', out = 'hex') => {

export const createHash = (v) => hash(v, 'sha1', 'hex')

export const measurePromiseTime = async (promise, { label = undefined, log = console, printMeasurement = false, msThreshold = 1000 } = {}) => {
let time = Date.now()
const enableAllMeasuresLogging = false

export const measurePromiseTime = async (promise, { name = 'Measurement', forceMeasureLogging = false, ...extraData }) => {
const time = Date.now()
const result = await promise
time = Date.now() - time
const printMetrics = () => {
if (label) {
log.info(`Time taken for ${label}: ${time} ms.`)
} else {
log.info(`Time taken: ${time} ms.`)
}
}

if (printMeasurement) {
if (msThreshold) {
if (time > msThreshold) {
printMetrics()
}
} else {
printMetrics()
}
}
if (enableAllMeasuresLogging || forceMeasureLogging) console.dir({ [name]: `${Date.now() - time}ms`, ...extraData }, { depth: null })

return result
}
42 changes: 42 additions & 0 deletions src/repositories/address.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { generateFindQuery } from './utils'
import { addressRelatedTables } from './includeRelatedTables'
import { addrTypes } from '../lib/types'

const relatedTables = {
latestBalance: 'address_latest_balance_address_latest_balance_addressToaddress'
}

export function getAddressRepository (prismaClient) {
return {
async findOne (query = {}, project = {}, endpointOptions) {
Expand All @@ -17,6 +21,29 @@ export function getAddressRepository (prismaClient) {
return address ? addressEntityToRaw(address, endpointOptions) : null
},
async find (query = {}, project = {}, sort = {}, limit = 0, endpointOptions) {
const { useV2, action } = endpointOptions
if (useV2) {
if (action === 'getAddresses') {
const projection = {}
const include = { [relatedTables.latestBalance]: true }

const prismaQuery = generateFindQuery(query, projection, include, sort, limit)
const rawAddresses = await prismaClient.address.findMany(prismaQuery)

return formatAddressesWithBalances(rawAddresses)
} else if (action === 'getTokens') {
const projection = {}
const include = {
[relatedTables.latestBalance]: true
}

const prismaQuery = generateFindQuery(query, projection, include, sort, limit)
const rawTokens = await prismaClient.address.findMany(prismaQuery)

return formatAddressesWithBalances(rawTokens)
}
}

if (endpointOptions.isForGetMiners) {
const miners = await prismaClient.miner_address.findMany(generateFindQuery(query, project, {}, sort, limit))

Expand Down Expand Up @@ -124,3 +151,18 @@ function generateSaveContractQueries (data, prismaClient, upserting) {

return transactionQueries
}

function formatAddressesWithBalances (rawAddresses) {
return rawAddresses.map(address => {
if (address[relatedTables.latestBalance]) {
const { balance, blockNumber } = address[relatedTables.latestBalance]
address.balance = balance
address.blockNumber = blockNumber
delete address[relatedTables.latestBalance]
} else {
address.balance = '0'
address.blockNumber = 0
}
return address
})
}

0 comments on commit c309925

Please sign in to comment.