Skip to content

Commit

Permalink
Receipt processing (#57)
Browse files Browse the repository at this point in the history
* add receipt processing

* add receipt decoding

* remove trash line
  • Loading branch information
n0cte authored Jun 16, 2021
1 parent 8059e93 commit 9da6ff2
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/migrations/1623406376710-AddFieldsToReceipt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {MigrationInterface, QueryRunner} from "typeorm";

export class AddFieldsToReceipt1623406376710 implements MigrationInterface {
name = 'AddFieldsToReceipt1623406376710'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "eth"."receipt_cids" ADD COLUMN "post_state" VARCHAR(66)`);
await queryRunner.query(`ALTER TABLE "eth"."receipt_cids" ADD COLUMN "post_status" INTEGER;`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "eth"."receipt_cids" DROP COLUMN "post_status"`);
await queryRunner.query(`ALTER TABLE "eth"."receipt_cids" DROP COLUMN "post_state"`);
}
}
6 changes: 6 additions & 0 deletions src/models/eth/receiptCids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ export default class ReceiptCids {
@Column("varchar", { name: "log_contracts", nullable: true, array: true })
logContracts: string[] | null;

@Column("character varying", { name: "post_state", nullable: true, length: 66 })
postState: string | null;

@Column("integer", { name: "post_status", unique: true })
postStatus: number | null;

@OneToOne(
() => TransactionCids,
(transactionCids) => transactionCids.receiptCids,
Expand Down
33 changes: 33 additions & 0 deletions src/repositories/eth/receiptCidsRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { DeepPartial, EntityRepository, Repository } from 'typeorm';
import ReceiptCids from '../../models/eth/receiptCids';
import TransactionCids from '../../models/eth/transactionCids';
import { EthReceiptCid } from "../../types";

@EntityRepository(ReceiptCids)
export default class ReceiptCidsRepository extends Repository<ReceiptCids> {

public async add(receipt: EthReceiptCid, tx: TransactionCids): Promise<ReceiptCids> {
const result = await this.createQueryBuilder()
.insert()
.values({
txId: tx.id,
cid: receipt.cid,
mhKey: receipt.mhKey,
contract: receipt.contract,
topic0s: receipt.topic0S,
topic1s: receipt.topic1S,
topic2s: receipt.topic2S,
topic3s: receipt.topic3S,
logContracts: receipt.logContracts,
postState: receipt.postState,
postStatus: receipt.postStatus,
})
.returning("*")
// eslint-disable-next-line @typescript-eslint/camelcase
.orUpdate({conflict_target: ["tx_id"], overwrite: ["cid", "mh_key", "contract", "topic0s", "topic1s", "topic2s", "topic3s", "log_contracts", "post_state", "post_status"]})
.execute();

return this.create(result.generatedMaps[0] as DeepPartial<ReceiptCids>)
}

}
16 changes: 16 additions & 0 deletions src/repositories/graphqlRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,22 @@ export default class GraphqlRepository {
blockByMhKey {
data
}
receiptCidByTxId {
blockByMhKey {
data
}
postStatus
cid
contract
contractHash
topic0S
topic1S
topic2S
topic3S
postState
logContracts
mhKey
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ process.on('unhandledRejection', (reason, p) => {
if (data?.ethTransactionCidsByHeaderId?.nodes && data?.ethTransactionCidsByHeaderId?.nodes.length > 0) {
const txs = data.ethTransactionCidsByHeaderId.nodes;
for (const tx of txs) {
await dataService.processTransaction(tx, header.id);
const txCid = await dataService.processTransaction(tx, header.id);
await dataService.processReceipt(tx.receiptCidByTxId, txCid);
}
}
},
Expand Down
16 changes: 16 additions & 0 deletions src/services/dataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import GraphqlService from './graphqlService';
import HeaderCids from '../models/eth/headerCids';
import TransactionCids from '../models/eth/transactionCids';
import TransactionCidsRepository from '../repositories/eth/transactionCidsRepository';
import ReceiptCidsRepository from '../repositories/eth/receiptCidsRepository';
import HeaderCidsRepository from '../repositories/eth/headerCidsRepository';
import StateCids from '../models/eth/stateCids';
import State from '../models/contract/state';
Expand All @@ -37,6 +38,7 @@ import {
import BackfillProgressRepository from '../repositories/data/backfillProgressRepository';
import BlockRepository from "../repositories/eth/blockRepository";
import {decodeStorageCid, increaseHexByOne} from "../utils";
import ReceiptCids from 'models/eth/receiptCids';

const LIMIT = 1000;

Expand Down Expand Up @@ -298,6 +300,20 @@ VALUES
});
}

public async processReceipt(receipt: EthReceiptCid, tx: TransactionCids): Promise<ReceiptCids> {
if (!receipt) {
return;
}

return getConnection().transaction(async (entityManager) => {
const receiptRepository: ReceiptCidsRepository = entityManager.getCustomRepository(ReceiptCidsRepository);
const blockRepository: BlockRepository = entityManager.getCustomRepository(BlockRepository);

await blockRepository.add(receipt.mhKey, receipt.blockByMhKey.data);
return receiptRepository.add(receipt, tx);
});
}

public async processHeader(relatedNode: EthHeaderCid): Promise<HeaderCids> {

if (!relatedNode) {
Expand Down
3 changes: 3 additions & 0 deletions src/types/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export type EthReceiptCid = {
contract: string;
blockByMhKey: BlockByMhKey;
ethTransactionCidByTxId: EthTransactionCid;
postState: string;
postStatus: number;
};

export type EthTransactionCid = {
Expand All @@ -24,6 +26,7 @@ export type EthTransactionCid = {
txHash: string;
ethHeaderCidByHeaderId: EthHeaderCid;
blockByMhKey: BlockByMhKey;
receiptCidByTxId: EthReceiptCid;
};

export type ethTransactionCids = {
Expand Down
11 changes: 11 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,14 @@ export const decodeTransactionData = (data: string) => { // eslint-disable-line
s: decoded[8].toString('hex'),
}
}

export const decodeReceiptData = (data: string) => { // eslint-disable-line
const buffer = Buffer.from(data.replace('\\x',''), 'hex');
const decoded: any = rlp.decode(buffer); // eslint-disable-line

return {
status: decoded[0].toString('hex'),
gasUsed: decoded[1].toString('hex'),
data: decoded[2].toString('hex'),
}
}

0 comments on commit 9da6ff2

Please sign in to comment.