Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/eth get logs #19

Merged
merged 3 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 49 additions & 1 deletion src/api/controllers/log.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {storesAPI as stores} from 'src/store'
import {InternalTransaction, ShardID, Transaction} from 'src/types/blockchain'
import {validator} from 'src/utils/validators/validators'
import {isHexString, validator} from 'src/utils/validators/validators'
import {
is64CharHexHash,
isBlockNumber,
Expand All @@ -13,8 +13,10 @@ import {
isFilters,
Void,
isAddress,
isTransactionHash,
} from 'src/utils/validators'
import {
EthGetLogParams,
Filter,
InternalTransactionQueryField,
TransactionQueryField,
Expand Down Expand Up @@ -85,3 +87,49 @@ export async function getDetailedLogsByField(
stores[shardID].log.getDetailedLogsByField(field, value, limit, offset)
)
}

export async function ethGetLogs(shardID: ShardID, params: EthGetLogParams): Promise<any> {
const {fromBlock, toBlock, blockhash, topics, address} = params
if (blockhash) {
validator({
blockhash: is64CharHexHash(blockhash),
})
} else {
if (fromBlock) {
validator({
fromBlock: () => [isHexString(fromBlock)],
})
}
if (toBlock) {
validator({
toBlock: () => [isHexString(toBlock)],
})
}
}

if (address) {
if (Array.isArray(address)) {
address.forEach((a) => {
validator({
address: isAddress(a),
})
})
} else {
validator({
address: isAddress(address),
})
}
}

if (topics) {
if (Array.isArray(topics)) {
topics.forEach((topic) => {
validator({
topics: isTransactionHash(topic),
})
})
}
}

return stores[shardID].log.ethGetLogs(params)
}
70 changes: 70 additions & 0 deletions src/api/rest/routes/rpcRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {Response, Request, Router, NextFunction} from 'express'
import * as controllers from 'src/api/controllers'
import {catchAsync} from 'src/api/rest/utils'
import {EthGetLogParams} from 'src/types'

export const rpcRouter = Router({mergeParams: true})

enum RPCMethod {
ethGetLogs = 'eth_getLogs',
hmyGetLogs = 'hmy_getLogs',
}

enum RPCErrorCode {
unknownMethod = -32601,
paramsNotDefined = -32700,
}

rpcRouter.post('/', catchAsync(postRpcRequest))

const wrapRpcResponse = (key: string, value: any) => {
return {
jsonrpc: '2.0',
id: 1,
[key]: value,
}
}

export async function postRpcRequest(req: Request, res: Response, next: NextFunction) {
const {method, params} = req.body

if (!params || !Array.isArray(params)) {
return res.send(
wrapRpcResponse('error', {
code: RPCErrorCode.paramsNotDefined,
message: `missing value for required argument 0`,
})
)
}

try {
switch (method) {
case RPCMethod.ethGetLogs:
case RPCMethod.hmyGetLogs: {
const data = await ethGetLogs(params[0])
res.send(wrapRpcResponse('result', data))
break
}
default: {
res.send(
wrapRpcResponse('error', {
code: RPCErrorCode.unknownMethod,
message: `the method ${method} does not exist/is not available`,
})
)
}
}
} catch (e) {
res.send(wrapRpcResponse('error', {message: e.message || 'Unknown error'}))
}
}

function ethGetLogs(params: EthGetLogParams) {
if (params.fromBlock) {
params.fromBlock = params.fromBlock.toLowerCase()
}
if (params.toBlock) {
params.toBlock = params.toBlock.toLowerCase()
}
return controllers.ethGetLogs(0, params)
}
2 changes: 2 additions & 0 deletions src/api/rest/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {erc721Router} from 'src/api/rest/routes/ERC721'
import {erc1155Router} from 'src/api/rest/routes/ERC1155'
import {oneWalletMetricsRouter} from 'src/api/rest/routes/oneWalletMetrics'
import {warmUpCache} from 'src/api/controllers/cache/warmUpCache'
import {rpcRouter} from 'src/api/rest/routes/rpcRouter'

import {transport} from 'src/api/rest/transport'
const l = logger(module)
Expand Down Expand Up @@ -50,6 +51,7 @@ export const RESTServer = async () => {
routerWithShards0.use('/erc721', erc721Router, transport)
routerWithShards0.use('/erc1155', erc1155Router, transport)
routerWithShards0.use('/1wallet', oneWalletMetricsRouter, transport)
routerWithShards0.use('/rpc', rpcRouter, transport)

api.use('/v0', routerWithShards0)

Expand Down
67 changes: 65 additions & 2 deletions src/store/postgres/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@ import {IStorageLog} from 'src/store/interface'
import {BlockHash, BlockNumber, Log, LogDetailed} from 'src/types/blockchain'

import {Query} from 'src/store/postgres/types'
import {Filter, InternalTransactionQueryField, TransactionQueryValue} from 'src/types'
import {
EthGetLogParams,
Filter,
InternalTransactionQueryField,
TransactionQueryValue,
} from 'src/types'
import {buildSQLQuery} from 'src/store/postgres/filters'
import {fromSnakeToCamelResponse} from 'src/store/postgres/queryMapper'
import {
fromSnakeToCamelResponse,
mapLogToEthLog,
queryParamsConvert,
} from 'src/store/postgres/queryMapper'

export class PostgresStorageLog implements IStorageLog {
query: Query
Expand Down Expand Up @@ -92,4 +101,58 @@ export class PostgresStorageLog implements IStorageLog {
)
return res.map(fromSnakeToCamelResponse)
}

ethGetLogs = async (params: EthGetLogParams) => {
const {fromBlock, toBlock, address, topics, blockhash} = params
const whereClause = []

if (blockhash) {
whereClause.push('block_hash = :blockhash')
} else {
if (fromBlock) {
whereClause.push('block_number >= :fromBlockInteger')
}
if (toBlock) {
whereClause.push('block_number <= :toBlockInteger')
}
}

if (address) {
if (Array.isArray(address)) {
whereClause.push('address = any (:address)')
} else {
whereClause.push('address = :address')
}
}

if (topics) {
whereClause.push('topics @> :topics')
}

const queryParams = {
fromBlockInteger: fromBlock ? parseInt(fromBlock, 16) : undefined,
toBlockInteger: toBlock ? parseInt(toBlock, 16) : undefined,
address,
topics: topics ? `{${topics?.join(',')}}` : undefined, // convert topics array to SQL array
blockhash,
}

const filteredParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
if (value !== undefined) acc[key] = value
return acc
}, {} as any)

const preparedQuery = queryParamsConvert(
`
select * from logs
where ${whereClause.length > 0 ? whereClause.join(' and ') : ''}
limit 1000
`,
filteredParams
)

const res = await this.query(preparedQuery.text, preparedQuery.values)

return res.map(fromSnakeToCamelResponse).map(mapLogToEthLog)
}
}
24 changes: 24 additions & 0 deletions src/store/postgres/queryMapper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {Log} from 'src/types'

export const mapNaming: Record<string, string> = {
extra_data: 'extraData',
gas_limit: 'gasLimit',
Expand Down Expand Up @@ -86,3 +88,25 @@ export const generateQuery = (o: Record<any, any>) => {
params,
}
}

const intToHex = (value: string | number) => '0x' + (+value).toString(16)

export const mapLogToEthLog = (log: Log) => {
return {
...log,
blockNumber: intToHex(log.blockNumber),
transactionIndex: intToHex(log.transactionIndex),
logIndex: intToHex(log.logIndex),
}
}

type QueryReducerArray = [string, any[], number]

export function queryParamsConvert(parameterizedSql: string, params: Record<string, any>) {
const [text, values] = Object.entries(params).reduce(
([sql, array, index], [key, value]) =>
[sql.replace(`:${key}`, `$${index}`), [...array, value], index + 1] as QueryReducerArray,
[parameterizedSql, [], 1] as QueryReducerArray
)
return {text, values}
}
3 changes: 2 additions & 1 deletion src/store/postgres/sql/scheme.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ create index if not exists idx_logs_block_hash on logs using hash (block_hash);
create index if not exists idx_logs_block_number on logs (block_number desc);
create index if not exists idx_logs_block_number_asc on logs (block_number);
create index if not exists idx_logs_block_number_address on logs (block_number desc, address);
create index if not exists idx_gin_logs_topics on logs using GIN (topics);

create table if not exists transactions
(
Expand Down Expand Up @@ -366,4 +367,4 @@ create table if not exists wallets_count
date timestamp default now(),
date_string varchar unique not null,
count bigint not null
);
);
8 changes: 8 additions & 0 deletions src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,11 @@ export type Filter = {
orderBy?: FilterOrderBy
filters: FilterEntry[]
}

export interface EthGetLogParams {
fromBlock: string
toBlock: string
address?: string | string[]
topics?: string[]
blockhash?: string
}