fix: removed empty events array from response #668

This commit is contained in:
M Hassan Tariq
2021-11-24 14:49:08 +05:00
committed by GitHub
parent 67c453cb6c
commit 172e6a2123
7 changed files with 66 additions and 41 deletions

View File

@@ -19,7 +19,6 @@
"canonical",
"tx_status",
"tx_result",
"events",
"event_count",
"parent_block_hash",
"is_unanchored",
@@ -129,13 +128,6 @@
"execution_cost_write_length": {
"type": "integer",
"description": "Execution cost write length."
},
"events" : {
"type": "array",
"description": "List of transaction events",
"items": {
"$ref": "../transaction-events/transaction-event.schema.json"
}
}
}
}

View File

@@ -0,0 +1,24 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "TransactionWithEvents",
"description": "Describes all transaction types on Stacks 2.0 blockchain",
"allOf": [
{
"$ref": "./transaction.schema.json"
},
{
"additionalProperties": false,
"required": ["events"],
"properties": {
"events": {
"type": "array",
"description": "List of transaction events",
"items": {
"$ref": "../transaction-events/transaction-event.schema.json"
}
}
}
}
]
}

14
docs/generated.d.ts vendored
View File

@@ -233,6 +233,7 @@ export type SchemaMergeRootStub =
| TransactionNotFound
| TransactionStatus1
| TransactionType
| TransactionWithEvents
| Transaction
| InboundStxTransfer
| RpcAddressBalanceNotificationParams
@@ -484,10 +485,6 @@ export type AbstractTransaction = BaseTransaction & {
* Execution cost write length.
*/
execution_cost_write_length: number;
/**
* List of transaction events
*/
events: TransactionEvent[];
};
export type PostConditionMode = "allow" | "deny";
/**
@@ -768,6 +765,15 @@ export type TransactionStatus1 = "success" | "abort_by_response" | "abort_by_pos
* String literal of all Stacks 2.0 transaction types
*/
export type TransactionType = "token_transfer" | "smart_contract" | "contract_call" | "poison_microblock" | "coinbase";
/**
* Describes all transaction types on Stacks 2.0 blockchain
*/
export type TransactionWithEvents = Transaction & {
/**
* List of transaction events
*/
events: TransactionEvent[];
};
export type RpcSubscriptionType =
| "tx_update"
| "address_tx_update"

View File

@@ -305,7 +305,7 @@ paths:
content:
application/json:
schema:
$ref: ./entities/transactions/transaction.schema.json
$ref: ./entities/transactions/transaction-with-events.schema.json
example:
$ref: ./entities/transactions/transaction-4-coinbase.example.json
404:

View File

@@ -27,6 +27,7 @@ import {
SmartContractTransaction,
SmartContractTransactionMetadata,
TokenTransferTransactionMetadata,
TransactionWithEvents,
Transaction,
TransactionAnchorModeType,
TransactionEvent,
@@ -168,11 +169,14 @@ export function getTxStatus(txStatus: DbTxStatus | string): string {
}
}
type HasEventTransaction = SmartContractTransaction | ContractCallTransaction;
type EventTypeString =
| 'smart_contract_log'
| 'stx_asset'
| 'fungible_token_asset'
| 'non_fungible_token_asset'
| 'stx_lock';
export function getEventTypeString(
eventTypeId: DbEventTypeId
): ElementType<Exclude<HasEventTransaction['events'], undefined>>['event_type'] {
export function getEventTypeString(eventTypeId: DbEventTypeId): EventTypeString {
switch (eventTypeId) {
case DbEventTypeId.SmartContractLog:
return 'smart_contract_log';
@@ -734,7 +738,6 @@ function parseDbAbstractTx(dbTx: DbTx, baseTx: BaseTransaction): AbstractTransac
microblock_sequence: dbTx.microblock_sequence,
microblock_canonical: dbTx.microblock_canonical,
event_count: dbTx.event_count,
events: [],
execution_cost_read_count: dbTx.execution_cost_read_count,
execution_cost_read_length: dbTx.execution_cost_read_length,
execution_cost_runtime: dbTx.execution_cost_runtime,
@@ -813,7 +816,7 @@ export async function getMempoolTxsFromDataStore(
export async function getTxsFromDataStore(
db: DataStore,
args: GetTxsArgs | GetTxsWithEventsArgs
): Promise<Transaction[]> {
): Promise<Transaction[] | TransactionWithEvents[]> {
// fetching all requested transactions from db
const txQuery = await db.getTxListDetails({
txIds: args.txIds,
@@ -861,12 +864,16 @@ export async function getTxsFromDataStore(
// incase transaction events are requested
if ('eventLimit' in args) {
// this will insert all events in a single parsedTransaction. Only specific ones are to be added.
parsedTxs.forEach(
const txsWithEvents: TransactionWithEvents[] = parsedTxs.map(ptx => {
return { ...ptx, events: [] };
});
txsWithEvents.forEach(
ptx =>
(ptx.events = events
.filter(event => event.tx_id === ptx.tx_id)
.map(event => parseDbEvent(event)))
);
return txsWithEvents;
}
return parsedTxs;
}
@@ -874,7 +881,7 @@ export async function getTxsFromDataStore(
export async function getTxFromDataStore(
db: DataStore,
args: GetTxArgs | GetTxWithEventsArgs | GetTxFromDbTxArgs
): Promise<FoundOrNot<Transaction>> {
): Promise<FoundOrNot<TransactionWithEvents | Transaction>> {
let dbTx: DbTx;
if ('dbTx' in args) {
dbTx = args.dbTx;
@@ -902,15 +909,16 @@ export async function getTxFromDataStore(
// If tx events are requested
if ('eventLimit' in args) {
const txWithEvents: TransactionWithEvents = { ...parsedTx, events: [] };
const eventsQuery = await db.getTxEvents({
txId: args.txId,
indexBlockHash: dbTx.index_block_hash,
limit: args.eventLimit,
offset: args.eventOffset,
});
parsedTx.events = eventsQuery.results.map(event => parseDbEvent(event));
txWithEvents.events = eventsQuery.results.map(event => parseDbEvent(event));
return { found: true, result: txWithEvents };
}
return {
found: true,
result: parsedTx,
@@ -1079,7 +1087,7 @@ export async function searchTxs(
export async function searchTx(
db: DataStore,
args: GetTxArgs | GetTxWithEventsArgs
): Promise<FoundOrNot<Transaction | MempoolTransaction>> {
): Promise<FoundOrNot<Transaction | TransactionWithEvents | MempoolTransaction>> {
// First, check the happy path: the tx is mined and in the canonical chain.
const minedTxs = await getTxsFromDataStore(db, { ...args, txIds: [args.txId] });
const minedTx = minedTxs[0] ?? undefined;

View File

@@ -2484,7 +2484,6 @@ describe('api tests', () => {
amount: '35',
memo: '0x6869',
},
events: [],
event_count: 0,
execution_cost_read_count: 1,
execution_cost_read_length: 2,
@@ -2565,7 +2564,6 @@ describe('api tests', () => {
amount: '250',
memo: '0x6869',
},
events: [],
event_count: 0,
execution_cost_read_count: 1,
execution_cost_read_length: 2,
@@ -2623,7 +2621,6 @@ describe('api tests', () => {
amount: '100',
memo: '0x6869',
},
events: [],
event_count: 0,
execution_cost_read_count: 1,
execution_cost_read_length: 2,
@@ -2704,7 +2701,6 @@ describe('api tests', () => {
amount: '35',
memo: '0x6869',
},
events: [],
event_count: 0,
execution_cost_read_count: 1,
execution_cost_read_length: 2,
@@ -2776,7 +2772,6 @@ describe('api tests', () => {
amount: '35',
memo: '0x6869',
},
events: [],
event_count: 0,
execution_cost_read_count: 1,
execution_cost_read_length: 2,
@@ -2857,7 +2852,6 @@ describe('api tests', () => {
amount: '15',
memo: '0x6869',
},
events: [],
event_count: 0,
execution_cost_read_count: 1,
execution_cost_read_length: 2,
@@ -3414,7 +3408,6 @@ describe('api tests', () => {
memo: '0x6869',
},
event_count: 0,
events: [],
execution_cost_read_count: 0,
execution_cost_read_length: 0,
execution_cost_runtime: 0,
@@ -3455,7 +3448,6 @@ describe('api tests', () => {
memo: '0x6869',
},
event_count: 0,
events: [],
execution_cost_read_count: 0,
execution_cost_read_length: 0,
execution_cost_runtime: 0,
@@ -3496,7 +3488,6 @@ describe('api tests', () => {
memo: '0x6869',
},
event_count: 0,
events: [],
execution_cost_read_count: 0,
execution_cost_read_length: 0,
execution_cost_runtime: 0,
@@ -3959,12 +3950,12 @@ describe('api tests', () => {
execution_cost_write_count: 0,
execution_cost_write_length: 0,
};
expect(txQuery.result).toEqual(expectedResp);
const { events, ...excludedEvents } = expectedResp;
const fetchTx = await supertest(api.server).get(`/extended/v1/tx/${dbTx.tx_id}`);
expect(fetchTx.status).toBe(200);
expect(fetchTx.type).toBe('application/json');
expect(JSON.parse(fetchTx.text)).toEqual(expectedResp);
expect(txQuery.result).toEqual(excludedEvents);
});
test('tx store and processing', async () => {
@@ -4172,7 +4163,8 @@ describe('api tests', () => {
execution_cost_write_count: 0,
execution_cost_write_length: 0,
};
expect(txQuery.result).toEqual(expectedResp);
const { events, ...excludedEvents } = expectedResp;
expect(txQuery.result).toEqual(excludedEvents);
const fetchTx = await supertest(api.server).get(`/extended/v1/tx/${dbTx.tx_id}`);
expect(fetchTx.status).toBe(200);
@@ -4296,7 +4288,8 @@ describe('api tests', () => {
execution_cost_write_count: 0,
execution_cost_write_length: 0,
};
expect(txQuery.result).toEqual(expectedResp);
const { events, ...excludedEvents } = expectedResp;
expect(txQuery.result).toEqual(excludedEvents);
const fetchTx = await supertest(api.server).get(`/extended/v1/tx/${dbTx.tx_id}`);
expect(fetchTx.status).toBe(200);
@@ -4420,7 +4413,8 @@ describe('api tests', () => {
execution_cost_write_count: 0,
execution_cost_write_length: 0,
};
expect(txQuery.result).toEqual(expectedResp);
const { events, ...excludedEvents } = expectedResp;
expect(txQuery.result).toEqual(excludedEvents);
const fetchTx = await supertest(api.server).get(`/extended/v1/tx/${dbTx.tx_id}`);
expect(fetchTx.status).toBe(200);

View File

@@ -25,6 +25,7 @@ import {
MicroblockListResponse,
Transaction,
TransactionResults,
TransactionWithEvents,
} from '@stacks/stacks-blockchain-api-types';
import { useWithCleanup } from './test-helpers';
import { startEventServer } from '../event-stream/event-server';
@@ -149,7 +150,7 @@ describe('microblock tests', () => {
}
}
const txResult2 = await supertest(api.server).get(`/extended/v1/tx/${lostTx}`);
const { body: txBody }: { body: Transaction } = txResult2;
const { body: txBody }: { body: TransactionWithEvents } = txResult2;
expect(txBody.canonical).toBe(true);
expect(txBody.microblock_canonical).toBe(true);
expect(txBody.tx_id).toBe(lostTx);
@@ -215,7 +216,7 @@ describe('microblock tests', () => {
}
}
const txResult2 = await supertest(api.server).get(`/extended/v1/tx/${lostTx}`);
const { body: txBody }: { body: Transaction } = txResult2;
const { body: txBody }: { body: TransactionWithEvents } = txResult2;
expect(txBody.canonical).toBe(true);
expect(txBody.microblock_canonical).toBe(true);
expect(txBody.tx_id).toBe(lostTx);
@@ -440,7 +441,7 @@ describe('microblock tests', () => {
const txResult2 = await supertest(api.server).get(
`/extended/v1/tx/${mbTx1.tx_id}?unanchored`
);
const { body: txBody2 }: { body: Transaction } = txResult2;
const { body: txBody2 }: { body: TransactionWithEvents } = txResult2;
expect(txBody2.tx_id).toBe(mbTx1.tx_id);
expect(txBody2.tx_status).toBe('success');
expect(txBody2.events).toHaveLength(1);