Skip to content
This repository has been archived by the owner on Jul 18, 2020. It is now read-only.

Commit

Permalink
added getStatus for cardano-explorer data provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
crchemist-ip committed Feb 21, 2020
1 parent 5901300 commit 0f9e2b7
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 10 deletions.
1 change: 1 addition & 0 deletions config/develop.json
Expand Up @@ -15,6 +15,7 @@
"user": "postgres",
"host": "localhost",
"database": "cexplorer",
"password": "mysecretpassword",
"port": 5432,
"min": 4,
"max": 20,
Expand Down
2 changes: 2 additions & 0 deletions src/blockchain/shelley/block.js
Expand Up @@ -4,6 +4,8 @@ import { Block } from '../common'
import type { EpochIdType, SlotIdType, TxType } from '../common'
import shelleyUtils from './utils'

export const GENESIS_PARENT = '0000000000000000000000000000000000000000000000000000000000000000'

export type HeaderType = Array<any>

export default class ShelleyBlock implements Block {
Expand Down
2 changes: 1 addition & 1 deletion src/entities/postgres-storage/database.js
Expand Up @@ -720,7 +720,7 @@ class DB<TxType: ByronTxType | ShelleyTxType> {


helpers.annotate(DB, [
SERVICE_IDENTIFIER.DB_CONNECTION,
{ type: SERVICE_IDENTIFIER.DB_CONNECTION, named: 'dbConnection' },
SERVICE_IDENTIFIER.LOGGER,
'pendingTxsTimeoutMinutes',
])
Expand Down
@@ -1,25 +1,94 @@
// @flow

import { helpers } from 'inversify-vanillajs-helpers'
import type { Logger } from 'bunyan'

import type { RawDataProvider, RawDataParser, NetworkConfig } from '../../../interfaces'
import type {
RawDataProvider,
NetworkConfig,
DBConnection,
} from '../../../interfaces'
import type { NodeStatusType } from '../../../interfaces/raw-data-provider'
import SERVICE_IDENTIFIER from '../../../constants/identifiers'
import { GENESIS_PARENT } from '../../../blockchain/shelley/block'

import {
GET_BEST_BLOCK_NUM,
} from './db-queries'

class CardanoExplorerApi implements RawDataProvider {
parser: RawDataParser
logger: Logger

conn: DBConnection

networkConfig: NetworkConfig

constructor(
networkConfig: NetworkConfig,
logger: Logger,
conn: DBConnection,
) {
this.logger = logger
this.networkConfig = networkConfig
this.conn = conn
}

getStatus() {
console.log('Get status called.')
async getGenesis(hash: string): Promise<Object> {
this.logger.debug(`getGenesis: ${hash}`)
// const resp = await this.getJson(`/genesis/${hash}`)
// const { data } = resp
// return data
// not supported right now in jormungandr, so we're hardcoding this for now
// as something empty to not cause any issues.
return {
protocolConsts: {
protocolMagic: null,
},
nonAvvmBalances: [],
avvmDistr: [],
}
}

async getStatus(): Promise<NodeStatusType> {
const resp = await this.conn.query(GET_BEST_BLOCK_NUM)
this.logger.debug('[cardano-explorer-api].getStatus', resp)
if (resp.rows.length === 1) {
const {
epoch, slotNo, height, hash,
} = resp.rows[0]
const tipStatus = {
height,
hash,
slot: [epoch, slotNo],
}
return {
packedEpochs: epoch,
tip: {
local: tipStatus,
remote: tipStatus,
},
}
}

const emptyTip = {
height: 0,
slot: [0, 0],
hash: GENESIS_PARENT,
}
return {
packedEpochs: 0,
tip: {
local: emptyTip,
remote: emptyTip,
},
}
}
}

helpers.annotate(CardanoExplorerApi, [
SERVICE_IDENTIFIER.NETWORK_CONFIG,
SERVICE_IDENTIFIER.LOGGER,
{ type: SERVICE_IDENTIFIER.DB_CONNECTION, named: 'cardanoExplorerDbConnection' },
])

export default CardanoExplorerApi
16 changes: 16 additions & 0 deletions src/entities/raw-data-providers/cardano-explorer-api/db-queries.js
@@ -0,0 +1,16 @@
// @flow

import squel from 'squel'

export const sql = squel.useFlavour('postgres')

export const GET_BEST_BLOCK_NUM = sql.select()
.from('"Block"')
.field('id', 'hash')
.field('number', 'height')
.field('"epochNo"', 'epoch')
.field('"slotNo"')
.where('number IS NOT NULL')
.order('number', false)
.limit(1)
.toString()
4 changes: 2 additions & 2 deletions src/entities/raw-data-providers/jormungandr-api.js
Expand Up @@ -10,11 +10,11 @@ import { RawDataProvider, RawDataParser } from '../../interfaces'
import SERVICE_IDENTIFIER from '../../constants/identifiers'
import type { NetworkConfig } from '../../interfaces'

import { GENESIS_PARENT } from '../../blockchain/shelley/block'

// these two are for getting the network instead of using NetworkConfig
import { getNetworkConfig } from '../../utils'

const GENESIS_PARENT = '0000000000000000000000000000000000000000000000000000000000000000'

class JormungandrApi implements RawDataProvider {
#networkBaseUrl: string

Expand Down
16 changes: 15 additions & 1 deletion src/interfaces/raw-data-provider.js
@@ -1,9 +1,23 @@
// @flow

export type NodeTipStatusType = {
height: number,
slot: [number, number],
hash: string,
}

export type NodeStatusType = {
packedEpochs: number,
tip: {
local: NodeTipStatusType,
remote: NodeTipStatusType
}
}

export interface RawDataProvider {
postSignedTx(txPayload: string): Promise<any>;
getBlock(id: string): Promise<string>;
getEpoch(id: number): Promise<string>;
getGenesis(hash: string): Promise<Object>;
getStatus(): Promise<any>;
getStatus(): Promise<NodeStatusType>;
}
3 changes: 2 additions & 1 deletion src/ioc-config/cardano-explorer-db.js
Expand Up @@ -13,7 +13,8 @@ const createDb = async (dbSettings: PgPoolConfig): Promise<Pool> => (new pg.Pool

const cardanoExplorerDbModule = new AsyncContainerModule(async (bind: interfaces.Bind) => {
const dbConn = await createDb(config.get('cardanoExplorerDb'))
bind<DBConnection>(SERVICE_IDENTIFIER.DB_CONNECTION).toConstantValue(dbConn).whenTargetNamed('cardanoExplorerDb')
bind<DBConnection>(SERVICE_IDENTIFIER.DB_CONNECTION)
.toConstantValue(dbConn).whenTargetNamed('cardanoExplorerDbConnection')
})

export default cardanoExplorerDbModule
3 changes: 2 additions & 1 deletion src/ioc-config/db.js
Expand Up @@ -13,7 +13,8 @@ const createDb = async (dbSettings: PgPoolConfig): Promise<Pool> => (new pg.Pool

const dbModule = new AsyncContainerModule(async (bind: interfaces.Bind) => {
const dbConn = await createDb(config.get('db'))
bind<DBConnection>(SERVICE_IDENTIFIER.DB_CONNECTION).toConstantValue(dbConn)
bind<DBConnection>(SERVICE_IDENTIFIER.DB_CONNECTION).
toConstantValue(dbConn).whenTargetNamed('dbConnection')
})

export default dbModule

0 comments on commit 0f9e2b7

Please sign in to comment.