Skip to content

Commit

Permalink
fix: create tx in several SQLs
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Sep 19, 2019
1 parent bba6acc commit d012cc5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/neuron-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"rxjs": "6.5.3",
"sha3": "2.0.7",
"sqlite3": "4.1.0",
"typeorm": "0.2.18",
"typeorm": "0.2.19",
"uuid": "3.3.3"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {MigrationInterface, QueryRunner, TableColumn} from "typeorm";

export class ChangeHasDataDefault1568621556467 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.changeColumn('output', 'hasData', new TableColumn({
name: 'hasData',
type: 'boolean',
default: 0,
}))
}

public async down(_queryRunner: QueryRunner): Promise<any> {
}

}
39 changes: 37 additions & 2 deletions packages/neuron-wallet/src/services/tx/transaction-persistor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { getConnection } from 'typeorm'
import { getConnection, QueryRunner } from 'typeorm';
import { OutPoint, Transaction, TransactionWithoutHash, Input, TransactionStatus } from 'types/cell-types'
import InputEntity from 'database/chain/entities/input'
import OutputEntity from 'database/chain/entities/output'
import TransactionEntity from 'database/chain/entities/transaction'
import LockUtils from 'models/lock-utils'
import { OutputStatus, TxSaveType } from './params'
import Utils from 'services/sync/utils'

/* eslint @typescript-eslint/no-unused-vars: "warn" */
/* eslint no-await-in-loop: "off" */
Expand Down Expand Up @@ -157,10 +158,44 @@ export class TransactionPersistor {
})
)

await connection.manager.save([tx, ...inputs, ...previousOutputs, ...outputs])
const sliceSize = 100
const sleepTime = 10
const queryRunner = connection.createQueryRunner()
await TransactionPersistor.waitTransaction(queryRunner)
await queryRunner.startTransaction()
try {
await queryRunner.manager.save(tx)
for (const slice of Utils.eachSlice(inputs, sliceSize)) {
await queryRunner.manager.save(slice)
await Utils.sleep(sleepTime)
}
for (const slice of Utils.eachSlice(previousOutputs, sliceSize)) {
await queryRunner.manager.save(slice)
await Utils.sleep(sleepTime)
}
for (const slice of Utils.eachSlice(outputs, sliceSize)) {
await queryRunner.manager.save(slice)
await Utils.sleep(sleepTime)
}
await queryRunner.commitTransaction();
} catch (err) {
await queryRunner.rollbackTransaction()
} finally {
await queryRunner.release()
}
return tx
}

private static waitTransaction = async(queryRunner: QueryRunner, timeout: number = 5000) => {
const startAt: number = +new Date()
while (queryRunner.isTransactionActive) {
const now: number = +new Date()
if (now - startAt < timeout) {
await Utils.sleep(50)
}
}
}

public static deleteWhenFork = async (blockNumber: string) => {
const txs = await getConnection()
.getRepository(TransactionEntity)
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16454,10 +16454,10 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typeorm@0.2.18:
version "0.2.18"
resolved "https://registry.yarnpkg.com/typeorm/-/typeorm-0.2.18.tgz#8ae1d21104117724af41ddc11035c40a705e1de8"
integrity sha512-S553GwtG5ab268+VmaLCN7gKDqFPIzUw0eGMTobJ9yr0Np62Ojfx8j1Oa9bIeh5p7Pz1/kmGabAHoP1MYK05pA==
typeorm@0.2.19:
version "0.2.19"
resolved "https://registry.yarnpkg.com/typeorm/-/typeorm-0.2.19.tgz#a0cff0714180e5720df157df02c5759a1a646dc3"
integrity sha512-xKVx/W41zckQ7v8WYcpRhSKpjXDKG/Jgjy0RWvYelR8ZnfyblNRL12jF4P8tIhwXv6l5t01s7HEc9lR+zb6Gtg==
dependencies:
app-root-path "^2.0.1"
buffer "^5.1.0"
Expand Down

0 comments on commit d012cc5

Please sign in to comment.