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

Transaction owner field #306

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/api/.env
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ DASHCORE_PASS=password
EPOCH_CHANGE_TIME=3600000
DAPI_URL=127.0.0.1:1443:self-signed
TCP_CONNECT_TIMEOUT=400
DPNS_CONTRACT=5mjGWa9mruHnLBht3ntbfgodcSoJxA1XIfYiv1PFMVU=
15 changes: 12 additions & 3 deletions packages/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,11 @@ GET /transaction/DEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEE
type: 0,
gasUsed: 1337000,
status: "SUCCESS",
error: null
error: null,
owner: {
identifier: "6q9RFbeea73tE31LGMBLFZhtBUX3wZL3TcNynqE18Zgs",
aliases: []
}
}
```

Expand Down Expand Up @@ -450,7 +454,11 @@ GET /transactions?=1&limit=10&order=asc
type: 0,
gasUsed: 1337000,
status: "SUCCESS",
error: null
error: null,
owner: {
identifier: "6q9RFbeea73tE31LGMBLFZhtBUX3wZL3TcNynqE18Zgs",
aliases: []
}
}, ...
]
}
Expand Down Expand Up @@ -768,7 +776,8 @@ GET /identities/GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec/transactions?page=1
timestamp: "2024-03-18T10:13:54.150Z",
gasUsed: 1337000,
status: "SUCCESS",
error: null
error: null,
owner: "GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec"
}, ...
]
}
Expand Down
6 changes: 3 additions & 3 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
"lint": "standard ."
},
"dependencies": {
"@dashevo/dapi-client": "1.5.1",
"@dashevo/dashd-rpc": "19.0.0",
"@fastify/cors": "^8.3.0",
"@scure/base": "^1.1.5",
"bs58": "^6.0.0",
"cbor": "^9.0.2",
"dash": "4.5.1",
"dash": "4.4.1",
"dotenv": "^16.3.1",
"fastify": "^4.21.0",
"fastify-metrics": "^11.0.0",
"knex": "^2.5.1",
"node-fetch": "^2.6.11",
"pg": "^8.11.3"
"pg": "^8.11.3",
"@dashevo/dapi-client": "github:owl352/dapi-client"
},
"devDependencies": {
"standard": "^17.1.0",
Expand Down
21 changes: 21 additions & 0 deletions packages/api/src/DAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ class DAPI {
const { epochsInfo } = await this.dapi.platform.getEpochsInfo(start, count, { ascending })
return epochsInfo
}

async getContestedState (contractId,
documentTypeName,
indexName = 'parentNameAndLabel',
resultType = 2,
indexValuesList, startAtIdentifierInfo,
allowIncludeLockedAndAbstainingVoteTally,
count
) {
const { contestedResourceContenders } = await this.dapi.platform.getContestedResourceVoteState(
Buffer.from(contractId, 'base64'),
documentTypeName,
indexName,
resultType,
indexValuesList,
startAtIdentifierInfo,
allowIncludeLockedAndAbstainingVoteTally,
count
)
return contestedResourceContenders
}
}

module.exports = DAPI
1 change: 1 addition & 0 deletions packages/api/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let genesisTime
module.exports = {
EPOCH_CHANGE_TIME: Number(process.env.EPOCH_CHANGE_TIME),
TCP_CONNECT_TIMEOUT: Number(process.env.TCP_CONNECT_TIMEOUT),
DPNS_CONTRACT: process.env.DPNS_CONTRACT ?? '5mjGWa9mruHnLBht3ntbfgodcSoJxA1XIfYiv1PFMVU=',
get genesisTime () {
if (!genesisTime || isNaN(genesisTime)) {
return TenderdashRPC.getBlockByHeight(1).then((blockInfo) => {
Expand Down
45 changes: 42 additions & 3 deletions packages/api/src/controllers/IdentitiesController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const IdentitiesDAO = require('../dao/IdentitiesDAO')
const { IDENTITY_CREDIT_WITHDRAWAL } = require('../enums/StateTransitionEnum')
const { validateAliases } = require('../utils')
const Identity = require('../models/Identity')
const { base58 } = require('@scure/base')

class IdentitiesController {
constructor (knex, dapi) {
Expand All @@ -16,22 +19,55 @@ class IdentitiesController {
return response.status(404).send({ message: 'not found' })
}

const validatedAliases = await validateAliases(identity.aliases, identity.identifier, this.dapi)

const balance = await this.dapi.getIdentityBalance(identifier)

response.send({ ...identity, balance })
response.send(Identity.fromObject({ ...identity, aliases: validatedAliases, balance }))
}

getIdentityByDPNS = async (request, response) => {
const { dpns } = request.query

const identity = await this.identitiesDAO.getIdentityByDPNS(dpns)
let preIdentity
let identity

if (!dpns.includes('.')) {
preIdentity = await this.identitiesDAO.getIdentityByDPNS(dpns)

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

const [{ contestedState }] = await validateAliases(
[preIdentity ? preIdentity.aliases.find(v => v.includes(`${dpns}.`)) : dpns],
null,
this.dapi
)

if (contestedState) {
if (typeof contestedState.finishedVoteInfo?.wonByIdentityId === 'string') {
const identifier = base58.encode(Buffer.from(contestedState.finishedVoteInfo?.wonByIdentityId, 'base64'))

identity = await this.identitiesDAO.getIdentityByIdentifier(identifier)
}
}

if (!contestedState) {
identity = preIdentity ?? await this.identitiesDAO.getIdentityByDPNS(dpns)
}

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

const balance = await this.dapi.getIdentityBalance(identity.identifier)

const validatedAliases = await validateAliases(identity.aliases, identity.identifier, this.dapi)

identity = Identity.fromObject({ ...identity, aliases: validatedAliases })

response.send({ ...identity, balance })
}

Expand All @@ -42,7 +78,10 @@ class IdentitiesController {

const identitiesWithBalance = await Promise.all(identities.resultSet.map(async identity => {
const balance = await this.dapi.getIdentityBalance(identity.identifier)
return { ...identity, balance }

const validatedAliases = await validateAliases(identity.aliases, identity.identifier, this.dapi)

return { ...identity, aliases: validatedAliases, balance }
}))

response.send({ ...identities, resultSet: identitiesWithBalance })
Expand Down
43 changes: 36 additions & 7 deletions packages/api/src/controllers/MainController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const IdentitiesDAO = require('../dao/IdentitiesDAO')
const ValidatorsDAO = require('../dao/ValidatorsDAO')
const TenderdashRPC = require('../tenderdashRpc')
const Epoch = require('../models/Epoch')
const { validateAliases } = require('../utils')
const { base58 } = require('@scure/base')

const API_VERSION = require('../../package.json').version
const PLATFORM_VERSION = '1' + require('../../package.json').dependencies.dash.substring(1)
Expand Down Expand Up @@ -115,9 +117,9 @@ class MainController {
const identity = await this.identitiesDAO.getIdentityByIdentifier(query)

if (identity) {
const balance = await this.dapi.getIdentityBalance(identity.identifier)

return response.send({ identity: { ...identity, balance } })
// Sending without actual balance and aliases, because on frontend we were making
// request /identity/:identifier for actual data
return response.send({ identity })
}

// search data contracts
Expand All @@ -136,12 +138,39 @@ class MainController {
}

if (/^[^\s.]+(\.[^\s.]+)*$/.test(query)) {
const identity = await this.identitiesDAO.getIdentityByDPNS(query)
let preIdentity
let identity

if (identity) {
const balance = await this.dapi.getIdentityBalance(identity.identifier)
if (!query.includes('.')) {
preIdentity = await this.identitiesDAO.getIdentityByDPNS(query)

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

const [{ contestedState }] = await validateAliases(
[preIdentity ? preIdentity.aliases.find(v => v.includes(`${query}.`)) : query],
null,
this.dapi
)

if (contestedState) {
if (typeof contestedState.finishedVoteInfo?.wonByIdentityId === 'string') {
const identifier = base58.encode(Buffer.from(contestedState.finishedVoteInfo?.wonByIdentityId, 'base64'))

return response.send({ identity: { ...identity, balance } })
identity = await this.identitiesDAO.getIdentityByIdentifier(identifier)
}
}

if (!contestedState) {
identity = preIdentity ?? await this.identitiesDAO.getIdentityByDPNS(query)
}

if (identity) {
// Sending without actual balance and aliases, because on frontend we were making
// request /identity/:identifier for actual data
return response.send({ identity })
}
}

Expand Down
31 changes: 27 additions & 4 deletions packages/api/src/controllers/TransactionsController.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const TransactionsDAO = require('../dao/TransactionsDAO')
const utils = require('../utils')
const { calculateInterval } = require('../utils')
const { calculateInterval, validateAliases } = require('../utils')

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

getTransactionByHash = async (request, reply) => {
Expand All @@ -17,7 +18,17 @@ class TransactionsController {
return reply.status(404).send({ message: 'not found' })
}

reply.send(transaction)
const validatedAliases = transaction.owner.aliases?.length > 0
? await validateAliases(transaction.owner.aliases ?? [], transaction.owner?.identifier, this.dapi)
: []

reply.send({
...transaction,
owner: {
...transaction.owner,
aliases: validatedAliases
}
})
}

getTransactions = async (request, response) => {
Expand All @@ -29,7 +40,19 @@ class TransactionsController {

const transactions = await this.transactionsDAO.getTransactions(Number(page ?? 1), Number(limit ?? 10), order)

response.send(transactions)
const transactionsWithCorrectAliases = await Promise.all(transactions.resultSet.map(async transaction =>
({
...transaction,
owner: {
...transaction.owner,
aliases: transaction.owner.aliases?.length > 0
? await validateAliases(transaction.owner.aliases ?? [], transaction.owner?.identifier, this.dapi)
: []
}
})
))

response.send({ ...transactions, resultSet: transactionsWithCorrectAliases })
}

getTransactionHistory = async (request, response) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/api/src/dao/IdentitiesDAO.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,15 @@ module.exports = class IdentitiesDAO {
const subquery = this.knex('state_transitions')
.select('state_transitions.id as state_transition_id', 'state_transitions.hash as tx_hash',
'state_transitions.index as index', 'state_transitions.type as type', 'state_transitions.block_hash as block_hash',
'state_transitions.gas_used as gas_used', 'state_transitions.status as status', 'state_transitions.error as error'
'state_transitions.gas_used as gas_used', 'state_transitions.status as status', 'state_transitions.error as error',
'state_transitions.owner as owner'
)
.select(this.knex.raw(`rank() over (order by state_transitions.id ${order}) rank`))
.where('state_transitions.owner', '=', identifier)

const rows = await this.knex.with('with_alias', subquery)
.select('state_transition_id', 'tx_hash', 'index', 'block_hash', 'type', 'rank',
'gas_used', 'status', 'gas_used',
'gas_used', 'status', 'gas_used', 'owner',
'blocks.timestamp as timestamp', 'blocks.height as block_height')
.select(this.knex('with_alias').count('*').as('total_count'))
.leftJoin('blocks', 'blocks.hash', 'block_hash')
Expand Down
29 changes: 23 additions & 6 deletions packages/api/src/dao/TransactionsDAO.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@ module.exports = class TransactionsDAO {
}

getTransactionByHash = async (hash) => {
const aliasesSubquery = this.knex('identity_aliases')
.select('identity_identifier', this.knex.raw('array_agg(alias) as aliases'))
.groupBy('identity_identifier')
.as('aliases')

const [row] = await this.knex('state_transitions')
.select('state_transitions.hash as tx_hash', 'state_transitions.data as data',
'state_transitions.gas_used as gas_used', 'state_transitions.status as status', 'state_transitions.error as error',
'state_transitions.type as type', 'state_transitions.index as index', 'blocks.height as block_height',
'blocks.hash as block_hash', 'blocks.timestamp as timestamp')
.select(
'state_transitions.hash as tx_hash', 'state_transitions.data as data',
'state_transitions.gas_used as gas_used', 'state_transitions.status as status',
'state_transitions.error as error', 'state_transitions.type as type',
'state_transitions.index as index', 'blocks.height as block_height',
'blocks.hash as block_hash', 'blocks.timestamp as timestamp', 'state_transitions.owner as owner',
'aliases.aliases as aliases'
)
.whereILike('state_transitions.hash', hash)
.leftJoin('blocks', 'blocks.hash', 'state_transitions.block_hash')
.leftJoin(aliasesSubquery, 'aliases.identity_identifier', 'owner')

if (!row) {
return null
Expand All @@ -27,17 +37,24 @@ module.exports = class TransactionsDAO {
const fromRank = ((page - 1) * limit) + 1
const toRank = fromRank + limit - 1

const aliasesSubquery = this.knex('identity_aliases')
.select('identity_identifier', this.knex.raw('array_agg(alias) as aliases'))
.groupBy('identity_identifier')
.as('aliases')

const subquery = this.knex('state_transitions')
.select(this.knex('state_transitions').count('hash').as('total_count'), 'state_transitions.hash as tx_hash',
'state_transitions.data as data', 'state_transitions.type as type', 'state_transitions.index as index',
'state_transitions.gas_used as gas_used', 'state_transitions.status as status', 'state_transitions.error as error',
'state_transitions.block_hash as block_hash', 'state_transitions.id as id')
'state_transitions.block_hash as block_hash', 'state_transitions.id as id', 'state_transitions.owner as owner')
.select(this.knex.raw(`rank() over (order by state_transitions.id ${order}) rank`))
.select('aliases')
.leftJoin(aliasesSubquery, 'aliases.identity_identifier', 'state_transitions.owner')
.as('state_transitions')

const rows = await this.knex(subquery)
.select('total_count', 'data', 'type', 'index', 'rank', 'block_hash', 'state_transitions.tx_hash as tx_hash',
'gas_used', 'status', 'error', 'blocks.height as block_height', 'blocks.timestamp as timestamp')
'gas_used', 'status', 'error', 'blocks.height as block_height', 'blocks.timestamp as timestamp', 'owner', 'aliases')
.leftJoin('blocks', 'blocks.hash', 'block_hash')
.whereBetween('rank', [fromRank, toRank])
.orderBy('state_transitions.id', order)
Expand Down
4 changes: 4 additions & 0 deletions packages/api/src/models/Identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ module.exports = class Identity {
static fromRow ({ identifier, owner, revision, balance, timestamp, total_txs, total_data_contracts, total_documents, total_transfers, tx_hash, is_system, aliases }) {
return new Identity(identifier, owner, revision, Number(balance), timestamp, Number(total_txs), Number(total_data_contracts), Number(total_documents), Number(total_transfers), tx_hash, is_system, aliases)
}

static fromObject ({ identifier, owner, revision, balance, timestamp, totalTxs, totalDataContracts, totalDocuments, totalTransfers, txHash, isSystem, aliases }) {
return new Identity(identifier, owner, revision, balance, timestamp, totalTxs, totalDataContracts, totalDocuments, totalTransfers, txHash, isSystem, aliases)
}
}
Loading
Loading