Skip to content

Commit

Permalink
perf(core-api): transaction transformer (#2327)
Browse files Browse the repository at this point in the history
  • Loading branch information
spkjp authored and faustbrian committed Mar 28, 2019
1 parent 99b5cff commit d512dac
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 26 deletions.
12 changes: 6 additions & 6 deletions packages/core-api/src/repositories/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class TransactionsRepository extends Repository implements IRepository {
*/
public async findAllLegacy(parameters: any = {}): Promise<any> {
const selectQuery = this.query
.select(this.query.block_id, this.query.serialized, this.query.timestamp)
.select(this.query.id, this.query.block_id, this.query.serialized, this.query.timestamp)
.from(this.query);

if (parameters.senderId) {
Expand Down Expand Up @@ -115,7 +115,7 @@ export class TransactionsRepository extends Repository implements IRepository {
*/
public async findAllByWallet(wallet, parameters: any = {}): Promise<any> {
const selectQuery = this.query
.select(this.query.block_id, this.query.serialized, this.query.timestamp)
.select(this.query.id, this.query.block_id, this.query.serialized, this.query.timestamp)
.from(this.query);

const applyConditions = queries => {
Expand Down Expand Up @@ -202,7 +202,7 @@ export class TransactionsRepository extends Repository implements IRepository {
*/
public async findById(id): Promise<any> {
const query = this.query
.select(this.query.block_id, this.query.serialized, this.query.timestamp)
.select(this.query.id, this.query.block_id, this.query.serialized, this.query.timestamp)
.from(this.query)
.where(this.query.id.equals(id));

Expand All @@ -219,7 +219,7 @@ export class TransactionsRepository extends Repository implements IRepository {
*/
public async findByTypeAndId(type, id): Promise<any> {
const query = this.query
.select(this.query.block_id, this.query.serialized, this.query.timestamp)
.select(this.query.id, this.query.block_id, this.query.serialized, this.query.timestamp)
.from(this.query)
.where(this.query.id.equals(id).and(this.query.type.equals(type)));

Expand All @@ -235,7 +235,7 @@ export class TransactionsRepository extends Repository implements IRepository {
*/
public async findByIds(ids): Promise<any> {
const query = this.query
.select(this.query.block_id, this.query.serialized, this.query.timestamp)
.select(this.query.id, this.query.block_id, this.query.serialized, this.query.timestamp)
.from(this.query)
.where(this.query.id.in(ids));

Expand All @@ -248,7 +248,7 @@ export class TransactionsRepository extends Repository implements IRepository {
*/
public async findWithVendorField(): Promise<any> {
const query = this.query
.select(this.query.block_id, this.query.serialized, this.query.timestamp)
.select(this.query.id, this.query.block_id, this.query.serialized, this.query.timestamp)
.from(this.query)
.where(this.query.vendor_field_hex.isNotNull());

Expand Down
16 changes: 8 additions & 8 deletions packages/core-api/src/versions/1/transactions/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { app } from "@arkecosystem/core-container";
import { Blockchain } from "@arkecosystem/core-interfaces";
import { bignumify } from "@arkecosystem/core-utils";
import { crypto, Transaction } from "@arkecosystem/crypto";
import { Blockchain, Database } from "@arkecosystem/core-interfaces";
import { Transaction } from "@arkecosystem/crypto";

export function transformTransactionLegacy(model) {
const config = app.getConfig();
const blockchain = app.resolvePlugin<Blockchain.IBlockchain>("blockchain");
const databaseService = app.resolvePlugin<Database.IDatabaseService>("database");

const { data } = Transaction.fromBytes(model.serialized);
const { data } = Transaction.fromBytesUnsafe(model.serialized, model.id);
const senderId = databaseService.walletManager.findByPublicKey(data.senderPublicKey).address;

return {
id: data.id,
blockid: model.blockId,
type: data.type,
timestamp: model.timestamp || data.timestamp,
amount: +bignumify(data.amount).toFixed(),
fee: +bignumify(data.fee).toFixed(),
amount: +data.amount,
fee: +data.fee,
recipientId: data.recipientId,
senderId: crypto.getAddress(data.senderPublicKey, config.get("network.pubKeyHash")),
senderId,
senderPublicKey: data.senderPublicKey,
vendorField: data.vendorField,
signature: data.signature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class TransactionsController extends Controller {
return Boom.notFound("Transaction not found");
}

const data = { serialized: transaction.serialized.toString("hex") };
const data = { id: transaction.id, serialized: transaction.serialized.toString("hex") };

return super.respondWithResource(request, data, "transaction");
} catch (error) {
Expand Down
18 changes: 10 additions & 8 deletions packages/core-api/src/versions/2/transactions/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { app } from "@arkecosystem/core-container";
import { Blockchain } from "@arkecosystem/core-interfaces";
import { bignumify, formatTimestamp } from "@arkecosystem/core-utils";
import { crypto, Transaction } from "@arkecosystem/crypto";
import { Blockchain, Database } from "@arkecosystem/core-interfaces";
import { formatTimestamp } from "@arkecosystem/core-utils";
import { Transaction } from "@arkecosystem/crypto";

export function transformTransaction(model) {
const config = app.getConfig();
const blockchain = app.resolvePlugin<Blockchain.IBlockchain>("blockchain");
const databaseService = app.resolvePlugin<Database.IDatabaseService>("database");

const { data } = Transaction.fromBytesUnsafe(model.serialized, model.id);
const sender = databaseService.walletManager.findByPublicKey(data.senderPublicKey).address;

const { data } = Transaction.fromBytes(model.serialized);
const lastBlock = blockchain.getLastBlock();

return {
id: data.id,
blockId: model.blockId,
version: data.version,
type: data.type,
amount: +bignumify(data.amount).toFixed(),
fee: +bignumify(data.fee).toFixed(),
sender: crypto.getAddress(data.senderPublicKey, config.get("network.pubKeyHash")),
amount: +data.amount,
fee: +data.fee,
sender,
recipient: data.recipientId,
signature: data.signature,
signSignature: data.signSignature,
Expand Down
1 change: 0 additions & 1 deletion packages/core-api/src/versions/2/wallets/controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { app } from "@arkecosystem/core-container";
import Boom from "boom";
import Hapi from "hapi";
import { Controller } from "../shared/controller";
Expand Down
4 changes: 2 additions & 2 deletions packages/crypto/src/transactions/types/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export abstract class Transaction {
* NOTE: Only use this internally when it is safe to assume the buffer has already been
* verified.
*/
public static fromBytesUnsafe(buffer: Buffer, id: string): Transaction {
public static fromBytesUnsafe(buffer: Buffer, id?: string): Transaction {
try {
const transaction = TransactionDeserializer.deserialize(buffer);
transaction.data.id = id;
transaction.data.id = id || crypto.getId(transaction.data);
transaction.isVerified = true;

return transaction;
Expand Down

0 comments on commit d512dac

Please sign in to comment.