Skip to content

Commit d012cc5

Browse files
committed
fix: create tx in several SQLs
1 parent bba6acc commit d012cc5

4 files changed

Lines changed: 58 additions & 7 deletions

File tree

packages/neuron-wallet/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"rxjs": "6.5.3",
4848
"sha3": "2.0.7",
4949
"sqlite3": "4.1.0",
50-
"typeorm": "0.2.18",
50+
"typeorm": "0.2.19",
5151
"uuid": "3.3.3"
5252
},
5353
"devDependencies": {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {MigrationInterface, QueryRunner, TableColumn} from "typeorm";
2+
3+
export class ChangeHasDataDefault1568621556467 implements MigrationInterface {
4+
5+
public async up(queryRunner: QueryRunner): Promise<any> {
6+
await queryRunner.changeColumn('output', 'hasData', new TableColumn({
7+
name: 'hasData',
8+
type: 'boolean',
9+
default: 0,
10+
}))
11+
}
12+
13+
public async down(_queryRunner: QueryRunner): Promise<any> {
14+
}
15+
16+
}

packages/neuron-wallet/src/services/tx/transaction-persistor.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { getConnection } from 'typeorm'
1+
import { getConnection, QueryRunner } from 'typeorm';
22
import { OutPoint, Transaction, TransactionWithoutHash, Input, TransactionStatus } from 'types/cell-types'
33
import InputEntity from 'database/chain/entities/input'
44
import OutputEntity from 'database/chain/entities/output'
55
import TransactionEntity from 'database/chain/entities/transaction'
66
import LockUtils from 'models/lock-utils'
77
import { OutputStatus, TxSaveType } from './params'
8+
import Utils from 'services/sync/utils'
89

910
/* eslint @typescript-eslint/no-unused-vars: "warn" */
1011
/* eslint no-await-in-loop: "off" */
@@ -157,10 +158,44 @@ export class TransactionPersistor {
157158
})
158159
)
159160

160-
await connection.manager.save([tx, ...inputs, ...previousOutputs, ...outputs])
161+
const sliceSize = 100
162+
const sleepTime = 10
163+
const queryRunner = connection.createQueryRunner()
164+
await TransactionPersistor.waitTransaction(queryRunner)
165+
await queryRunner.startTransaction()
166+
try {
167+
await queryRunner.manager.save(tx)
168+
for (const slice of Utils.eachSlice(inputs, sliceSize)) {
169+
await queryRunner.manager.save(slice)
170+
await Utils.sleep(sleepTime)
171+
}
172+
for (const slice of Utils.eachSlice(previousOutputs, sliceSize)) {
173+
await queryRunner.manager.save(slice)
174+
await Utils.sleep(sleepTime)
175+
}
176+
for (const slice of Utils.eachSlice(outputs, sliceSize)) {
177+
await queryRunner.manager.save(slice)
178+
await Utils.sleep(sleepTime)
179+
}
180+
await queryRunner.commitTransaction();
181+
} catch (err) {
182+
await queryRunner.rollbackTransaction()
183+
} finally {
184+
await queryRunner.release()
185+
}
161186
return tx
162187
}
163188

189+
private static waitTransaction = async(queryRunner: QueryRunner, timeout: number = 5000) => {
190+
const startAt: number = +new Date()
191+
while (queryRunner.isTransactionActive) {
192+
const now: number = +new Date()
193+
if (now - startAt < timeout) {
194+
await Utils.sleep(50)
195+
}
196+
}
197+
}
198+
164199
public static deleteWhenFork = async (blockNumber: string) => {
165200
const txs = await getConnection()
166201
.getRepository(TransactionEntity)

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16454,10 +16454,10 @@ typedarray@^0.0.6:
1645416454
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
1645516455
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
1645616456

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

0 commit comments

Comments
 (0)