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

Implement DocumentsBatchTransition #62

Merged
merged 13 commits into from
Oct 10, 2023
2 changes: 0 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ name: Build and push packages
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

env:
REGISTRY: ghcr.io
Expand Down
12 changes: 7 additions & 5 deletions packages/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ const Routes = require('./src/routes')

const ServiceNotAvailableError = require("./src/errors/ServiceNotAvailableError");
const MainController = require("./src/controllers/MainController");
const TransactionController = require("./src/controllers/TransactionController");
const BlockController = require("./src/controllers/BlockController");
const TransactionsController = require("./src/controllers/TransactionsController");
const BlocksController = require("./src/controllers/BlocksController");
const DocumentsController = require("./src/controllers/DocumentsController");
const packageVersion = require('./package.json').version
const Worker = require('./src/worker/index')
const {BLOCK_TIME} = require("./src/constants");
Expand Down Expand Up @@ -69,11 +70,12 @@ const init = async () => {
await knex.raw('select 1+1');

const mainController = new MainController(knex)
const blockController = new BlockController(knex)
const transactionController = new TransactionController(client, knex)
const blocksController = new BlocksController(knex)
const transactionsController = new TransactionsController(client, knex)
const dataContractsController = new DataContractsController(knex)
const documentsController = new DocumentsController(knex)

Routes({fastify, mainController, blockController, transactionController, dataContractsController})
Routes({fastify, mainController, blocksController, transactionsController, dataContractsController, documentsController})

fastify.setErrorHandler(errorHandler)
fastify.listen({ host: "0.0.0.0", port: 3005, listenTextResolver: (address) => console.log(`Platform indexer API has started on the ${address}`)});
Expand Down
82 changes: 0 additions & 82 deletions packages/api/src/controllers/BlockController.js

This file was deleted.

30 changes: 30 additions & 0 deletions packages/api/src/controllers/BlocksController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const Block = require('../models/Block');
const BlocksDAO = require('../dao/BlocksDAO');

class BlocksController {
constructor(knex) {
this.blocksDAO = new BlocksDAO(knex)
}

getBlockByHash = async (request, response) => {
const {hash} = request.params

const block = await this.blocksDAO.getBlockByHash(hash)

if (!block) {
return response.status(404).send({message: 'not found'})
}

response.send(block);
}

getBlocks = async (request, response) => {
const {from, to, order = 'desc'} = request.query

const blocks = await this.blocksDAO.getBlocksPaginated(from, to, order)

response.send(blocks);
}
}

module.exports = BlocksController
25 changes: 7 additions & 18 deletions packages/api/src/controllers/DataContractsController.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
const DataContract = require("../models/DataContract");
const DataContractsDAO = require("../dao/DataContractsDAO");

class DataContractsController {
constructor(knex) {
this.knex = knex
this.dataContractsDAO = new DataContractsDAO(knex)
}

getDataContracts = async (request, response) => {
const subquery = this.knex('data_contracts')
.select(this.knex.raw('data_contracts.id as id, data_contracts.identifier as identifier, data_contracts.version as version, rank() over (partition by identifier order by version desc) rank'))
.as('data_contracts')
const dataContracts = await this.dataContractsDAO.getDataContracts();

const rows = await this.knex(subquery)
.select('id', 'identifier', 'version', 'rank')
.where('rank', '=', 1)

response.send(rows.map(dataContract => DataContract.fromJSON(dataContract)));
response.send(dataContracts)
}

getDataContractByIdentifier = async (request, response) => {
const {identifier} = request.params

const rows = await this.knex('data_contracts')
.select('data_contracts.identifier as identifier', 'data_contracts.schema as schema', 'data_contracts.version as version')
.where('data_contracts.identifier', identifier)
.orderBy('id', 'desc')
.limit(1);

const [row] = rows
const dataContract = await this.dataContractsDAO.getDataContractByIdentifier(identifier)

if (!row) {
if (!dataContract) {
response.status(404).send({message: 'not found'})
}

response.send({identifier: row.identifier, schema: row.schema, version: row.version});
response.send(dataContract);
}
}

Expand Down
30 changes: 30 additions & 0 deletions packages/api/src/controllers/DocumentsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const Document = require('../models/Document');
const DocumentsDAO = require('../dao/DocumentsDAO');

class DocumentsController {
constructor(knex) {
this.documentsDAO = new DocumentsDAO(knex)
}

getDocumentByIdentifier = async (request, response) => {
const {identifier} = request.params

const document = await this.documentsDAO.getDocumentByIdentifier(identifier)

if (!document) {
response.status(404).send({message: 'not found'})
}

response.send(Document.fromRow(document));
}

getDocumentsByDataContract = async (request, response) => {
const {identifier} = request.params

const documents = await this.documentsDAO.getDocumentsByDataContract(identifier)

response.send(documents);
}
}

module.exports = DocumentsController
78 changes: 39 additions & 39 deletions packages/api/src/controllers/MainController.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
const BlocksDAO = require('../dao/BlocksDAO')
const DataContractsDAO = require('../dao/DataContractsDAO')
const TransactionsDAO = require('../dao/TransactionsDAO')

class MainController {
constructor(knex) {
this.knex = knex;
this.blocksDAO = new BlocksDAO(knex)
this.dataContractsDAO = new DataContractsDAO(knex)
this.transactionsDAO = new TransactionsDAO(knex)
}

getStatus = async (request, response) => {
const [result] = await this.knex('blocks').max('height')

const {max} = result

response.send({
network: "dash-testnet-25",
appVersion: "1",
p2pVersion: "8",
blockVersion: "13",
blocksCount: max,
tenderdashVersion: "0.13.1"
});
getStatus = async (request, response) => {
const max = await this.blocksDAO.getMaxHeight()

response.send({
network: "dash-testnet-25",
appVersion: "1",
p2pVersion: "8",
blockVersion: "13",
blocksCount: max,
tenderdashVersion: "0.13.1"
});
}

search = async (request, response) => {
Expand All @@ -27,42 +31,38 @@ class MainController {
}

if (/^[0-9]$/.test(query)) {
// search blocks by height
const [row] = await this.knex('blocks').select('hash', 'block_height').where('block_height', query)

if (row) {
const {hash, block_height} = row
// search block by height
const block = await this.blocksDAO.getBlockByHeight(query)

return response.send({hash: hash, height: block_height})
if (block) {
return response.send({block})
}
}

// check if base64 and 44 length for Identity ids
if (/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(query) && query.length === 44) {
// search blocks by height
const [row] = await this.knex('data_contracts').select('identifier').where('identifier', query)
if (/^[0-9A-F]{64,64}$/.test(query)) {
// search block by hash
const block = await this.blocksDAO.getBlockByHash(query)

if (row) {
return response.send({identifier: row.identifier})
if (block) {
return response.send({block})
}
}

// search blocks
const [row] = await this.knex('blocks').select('hash', 'block_height').where('hash', query)
// search transactions
const transaction = await this.transactionsDAO.getTransactionByHash(query)

if (row) {
const {hash, block_height} = row

return response.send({hash: hash, height: block_height})
if (transaction) {
return response.send({transaction})
}
}

// search transactions
const [stRow] = await this.knex('state_transitions').select('hash').where('hash', query)

if (stRow) {
const {hash} = stRow
// check if base64 and 44 length for Identity ids
if (/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(query) && query.length === 44) {
// search block by height
const dataContract = await this.dataContractsDAO.getDataContractByIdentifier(query)

return response.send({hash})
if (dataContract) {
return response.send({dataContract})
}
}

response.status(404).send({message: 'not found'})
Expand Down
48 changes: 48 additions & 0 deletions packages/api/src/controllers/TransactionsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const cache = require("../cache");
const Transaction = require("../models/Transaction");
const TransactionsDAO = require("../dao/TransactionsDAO");

class TransactionsController {
constructor(client, knex) {
this.client = client
this.transactionsDAO = new TransactionsDAO(knex)
}

getTransactions = async (request, response) => {
const {from, to} = request.query

const transactions = await this.transactionsDAO.getTransactions(from, to)

response.send(transactions);
}

getTransactionByHash = async (request, reply) => {
const {txHash} = request.params;

const transaction = await this.transactionsDAO.getTransactionByHash(txHash)

if (!transaction) {
return reply.status(404).send({message: 'not found'})
}

reply.send(transaction)
}

decode = async (request, reply) => {
const {base64} = request.body;

const cached = cache.get('decoded_' + base64)

if (cached) {
return reply.send(cached)
}

const stateTransition = await this.client.platform.dpp.stateTransition.createFromBuffer(Buffer.from(base64, 'base64'));

cache.set('decoded_' + base64, stateTransition)

reply.send(stateTransition)
}
}

module.exports = TransactionsController
Loading