forked from cardano-foundation/cardano-rosetta
-
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.
This commit includes the before mentioned api endpoint but also neededed to introduce DB connection and environment configuration. TODO: - Returning transactions data is still not implemented. - Setup and launch a test DB with mocked data re: cardano-foundation#13
- Loading branch information
1 parent
f99859a
commit e1de850
Showing
39 changed files
with
509 additions
and
127 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
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 |
---|---|---|
@@ -1,8 +1,11 @@ | ||
module.exports = { | ||
roots: ['<rootDir>/test'], | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.ts?$': 'ts-jest', | ||
'^.+\\.tsx?$': 'ts-jest' | ||
}, | ||
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$', | ||
testPathIgnorePatterns: ['/lib/', '/node_modules/'], | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], | ||
setupFiles: ['./test/e2e/setup-environment.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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,23 @@ | ||
/** | ||
* Custom error class to implement Rosetta Error Schema | ||
*/ | ||
class ApiError extends Error implements Components.Schemas.Error { | ||
code: number; | ||
message: string; | ||
retriable: boolean; | ||
details?: string; | ||
|
||
constructor(code: number, message: string, retriable: boolean, details?: string) { | ||
super(message); | ||
|
||
// Set the prototype explicitly. | ||
Object.setPrototypeOf(this, ApiError.prototype); | ||
|
||
this.code = code; | ||
this.message = message; | ||
this.retriable = retriable; | ||
this.details = details; | ||
} | ||
} | ||
|
||
export default ApiError; |
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,66 @@ | ||
import { hashFormatter } from '../utils/formatters'; | ||
import { Pool } from 'pg'; | ||
|
||
export interface Block { | ||
hash: string; | ||
number: number; | ||
time: number; | ||
parent: { | ||
hash: string; | ||
number: number; | ||
}; | ||
// transactions: Transaction[]; | ||
} | ||
|
||
export interface BlockchainRepository { | ||
/** | ||
* Finds a block based on the given block number or hash. If non sent, latest (tip) block information | ||
* is retrieved | ||
* | ||
* @param blockNumber | ||
* @param blockHash | ||
*/ | ||
findBlock(number?: number, blockHash?: string): Promise<Block | null>; | ||
} | ||
|
||
const findBlockQuery = (blockNumber?: number, blockHash?: string) => ` | ||
SELECT | ||
b1.block_no, | ||
b1.hash, | ||
b1.time, | ||
b2.block_no as parent_block_no, | ||
b2.hash as parent_hash | ||
FROM | ||
BLOCK b1 | ||
LEFT JOIN block b2 on (b1.block_no - 1) = b2.block_no | ||
WHERE | ||
${blockNumber ? 'b1.block_no = $1' : '$1 = $1'} AND | ||
${blockHash ? 'b1.hash = $2' : '$2 = $2'} | ||
LIMIT 1 | ||
`; | ||
|
||
export const configure = (databaseInstance: Pool): BlockchainRepository => ({ | ||
async findBlock(blockNumber?: number, blockHash?: string): Promise<Block | null> { | ||
const query = findBlockQuery(blockNumber, blockHash); | ||
// Add paramter or short-circuit it | ||
const parameters = [ | ||
blockNumber ? blockNumber : true, | ||
blockHash ? Buffer.from(blockHash.replace('0x', ''), 'hex') : true | ||
]; | ||
const result = await databaseInstance.query(query, parameters); | ||
/* eslint-disable camelcase */ | ||
if (result.rows.length === 1) { | ||
const { block_no, hash, time, parent_block_no, parent_hash } = result.rows[0]; | ||
return { | ||
number: block_no, | ||
hash: hashFormatter(hash), | ||
time, | ||
parent: { | ||
number: parent_block_no, | ||
hash: hashFormatter(parent_hash) | ||
} | ||
}; | ||
} | ||
return null; | ||
} | ||
}); |
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,10 @@ | ||
import { Pool } from 'pg'; | ||
|
||
/** | ||
* Creates a database pool to be used to run queries. No connection will be established. | ||
* | ||
* @param connectionString `postgresql://dbuser:secretpassword@database.server.com:3211/mydb` | ||
*/ | ||
const createPool = (connectionString: string): Pool => new Pool({ connectionString }); | ||
|
||
export default createPool; |
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,16 @@ | ||
import { Pool } from 'pg'; | ||
import * as BlockchainRepository from './blockchain-repository'; | ||
|
||
export interface Repositories { | ||
blockchainRepository: BlockchainRepository.BlockchainRepository; | ||
} | ||
|
||
/** | ||
* Configures the repositories with the given DB connection to make them ready | ||
* to be used | ||
* | ||
* @param database connection to be used to run queries | ||
*/ | ||
export const configure = (database: Pool): Repositories => ({ | ||
blockchainRepository: BlockchainRepository.configure(database) | ||
}); |
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,44 @@ | ||
import fastify from 'fastify'; | ||
import fastifyBlipp from 'fastify-blipp'; | ||
import openapiGlue from 'fastify-openapi-glue'; | ||
import { wrap } from './controllers/generic-controller'; | ||
import ApiError from './api-error'; | ||
import { Services } from './services/services'; | ||
import { Server, IncomingMessage, ServerResponse } from 'http'; | ||
|
||
/** | ||
* This function builds a Fastify instance connecting the services with the | ||
* corresponding fastify route handlers. | ||
* | ||
* @param services to be used to handle the requests | ||
* @param logger true if logger should be enabled, false otherwise | ||
*/ | ||
const buildServer = ( | ||
services: Services, | ||
logger = true | ||
): fastify.FastifyInstance<Server, IncomingMessage, ServerResponse> => { | ||
const server = fastify({ logger }); | ||
|
||
server.register(fastifyBlipp); | ||
server.register(openapiGlue, { | ||
specification: `${__dirname}/openApi.json`, | ||
service: wrap(services), | ||
noAdditional: true | ||
}); | ||
|
||
// Custom error handling is needed as the specified by Rosetta API doesn't match | ||
// the fastify default one | ||
server.setErrorHandler((error: Error, request, reply) => { | ||
if (error instanceof ApiError) { | ||
// eslint-disable-next-line no-magic-numbers | ||
reply.status(500).send({ | ||
...error, | ||
message: error.message | ||
}); | ||
} else reply.send(error); | ||
}); | ||
|
||
return server; | ||
}; | ||
|
||
export default buildServer; |
Oops, something went wrong.