Skip to content

Commit 436b388

Browse files
committed
feat: add lock to input
1 parent e7e9e83 commit 436b388

7 files changed

Lines changed: 83 additions & 17 deletions

File tree

packages/neuron-wallet/src/database/chain/entities/input.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Entity, BaseEntity, Column, ManyToOne, PrimaryGeneratedColumn } from 'typeorm'
2-
import { OutPoint, Input as InputInterface } from 'types/cell-types'
2+
import { OutPoint, Input as InputInterface, Script } from 'types/cell-types'
33
import Transaction from './transaction'
44

55
/* eslint @typescript-eslint/no-unused-vars: "warn" */
@@ -33,6 +33,13 @@ export default class Input extends BaseEntity {
3333
})
3434
lockHash: string | null = null
3535

36+
// cellbase input has no previous output lock script
37+
@Column({
38+
type: 'simple-json',
39+
nullable: true,
40+
})
41+
lock: Script | null = null
42+
3643
@ManyToOne(_type => Transaction, transaction => transaction.inputs, { onDelete: 'CASCADE' })
3744
transaction!: Transaction
3845

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {MigrationInterface, QueryRunner, TableColumn} from "typeorm";
2+
3+
export class AddLockToInput1570522869590 implements MigrationInterface {
4+
5+
public async up(queryRunner: QueryRunner): Promise<any> {
6+
await queryRunner.addColumn('input', new TableColumn({
7+
name: 'lock',
8+
type: 'text',
9+
isNullable: true,
10+
}))
11+
}
12+
13+
public async down(queryRunner: QueryRunner): Promise<any> {
14+
await queryRunner.dropColumn('input', 'lock')
15+
}
16+
17+
}

packages/neuron-wallet/src/database/chain/ormconfig.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import SyncInfo from './entities/sync-info'
1212
import { InitMigration1566959757554 } from './migrations/1566959757554-InitMigration'
1313
import { AddTypeAndHasData1567144517514 } from './migrations/1567144517514-AddTypeAndHasData'
1414
import { ChangeHasDataDefault1568621556467 } from './migrations/1568621556467-ChangeHasDataDefault'
15+
import { AddLockToInput1570522869590 } from './migrations/1570522869590-AddLockToInput'
1516

1617
export const CONNECTION_NOT_FOUND_NAME = 'ConnectionNotFoundError'
1718

@@ -32,7 +33,12 @@ const connectOptions = async (genesisBlockHash: string): Promise<SqliteConnectio
3233
type: 'sqlite',
3334
database,
3435
entities: [Transaction, Input, Output, SyncInfo],
35-
migrations: [InitMigration1566959757554, AddTypeAndHasData1567144517514, ChangeHasDataDefault1568621556467],
36+
migrations: [
37+
InitMigration1566959757554,
38+
AddTypeAndHasData1567144517514,
39+
ChangeHasDataDefault1568621556467,
40+
AddLockToInput1570522869590,
41+
],
3642
logging,
3743
}
3844
}

packages/neuron-wallet/src/services/cells.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ export default class CellsService {
118118
previousOutput: cell.outPoint(),
119119
since: '0',
120120
lock: cell.lock,
121+
lockHash: cell.lockHash,
122+
capacity: cell.capacity,
121123
}
122124
inputs.push(input)
123125
inputCapacities += BigInt(cell.capacity)

packages/neuron-wallet/src/services/indexer/queue.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import IndexerTransaction from 'services/tx/indexer-transaction'
1313
import IndexerRPC from './indexer-rpc'
1414
import HexUtils from 'utils/hex'
1515
import { TxUniqueFlagCache } from './tx-unique-flag'
16+
import TransactionEntity from 'database/chain/entities/transaction';
1617

1718
export interface LockHashInfo {
1819
lockHash: string
@@ -196,15 +197,25 @@ export default class IndexerQueue {
196197
logger.debug('indexer fetched tx:', type, txPoint.txHash)
197198

198199
// tx timestamp / blockNumber / blockHash
199-
const { blockHash } = transactionWithStatus.txStatus
200-
if (blockHash) {
201-
const blockHeader = await this.getBlocksService.getHeader(blockHash)
202-
transaction.blockHash = blockHash
203-
transaction.blockNumber = blockHeader.number
204-
transaction.timestamp = blockHeader.timestamp
200+
let txEntity: TransactionEntity | undefined = await TransactionPersistor.get(transaction.hash)
201+
if (!txEntity || !txEntity.blockHash) {
202+
for (const input of transaction.inputs!) {
203+
const previousTxWithStatus = await this.getBlocksService.getTransaction(input.previousOutput!.txHash)
204+
const previousTx = TypeConvert.toTransaction(previousTxWithStatus.transaction)
205+
const previousOutput = previousTx.outputs![+input.previousOutput!.index]
206+
input.lock = previousOutput.lock
207+
input.lockHash = LockUtils.lockScriptToHash(input.lock)
208+
input.capacity = previousOutput.capacity
209+
}
210+
const { blockHash } = transactionWithStatus.txStatus
211+
if (blockHash) {
212+
const blockHeader = await this.getBlocksService.getHeader(blockHash)
213+
transaction.blockHash = blockHash
214+
transaction.blockNumber = blockHeader.number
215+
transaction.timestamp = blockHeader.timestamp
216+
}
217+
txEntity = await TransactionPersistor.saveFetchTx(transaction)
205218
}
206-
// broadcast address used
207-
const txEntity = await TransactionPersistor.saveFetchTx(transaction)
208219

209220
let address: string | undefined
210221
if (type === TxPointType.CreatedBy) {

packages/neuron-wallet/src/services/sync/get-blocks.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import { generateCore } from 'services/sdk-core'
33

44
import { Block, BlockHeader } from 'types/cell-types'
55
import TypeConvert from 'types/type-convert'
6-
import CheckAndSave from './check-and-save'
76
import Utils from './utils'
87
import HexUtils from 'utils/hex'
8+
import CheckTx from 'services/sync/check-and-save/tx'
9+
import { TransactionPersistor } from 'services/tx'
10+
import LockUtils from 'models/lock-utils'
11+
import { addressesUsedSubject } from './renderer-params'
912

1013
export default class GetBlocks {
1114
private retryTime: number
@@ -33,14 +36,25 @@ export default class GetBlocks {
3336
return tip
3437
}
3538

36-
public checkAndSave = async (blocks: Block[], lockHashes: string[]) => {
37-
let checkResult: boolean[][] = []
39+
public checkAndSave = async (blocks: Block[], lockHashes: string[]): Promise<void> => {
3840
for (const block of blocks) {
39-
const checkAndSave = new CheckAndSave(block, lockHashes)
40-
const result = await checkAndSave.process()
41-
checkResult.push(result)
41+
for (const tx of block.transactions) {
42+
const checkTx = new CheckTx(tx)
43+
const addresses = await checkTx.check(lockHashes)
44+
if (addresses.length > 0) {
45+
for (const input of tx.inputs!) {
46+
const previousTxWithStatus = await this.getTransaction(input.previousOutput!.txHash)
47+
const previousTx = TypeConvert.toTransaction(previousTxWithStatus.transaction)
48+
const previousOutput = previousTx.outputs![+input.previousOutput!.index]
49+
input.lock = previousOutput.lock
50+
input.lockHash = LockUtils.lockScriptToHash(input.lock)
51+
input.capacity = previousOutput.capacity
52+
}
53+
await TransactionPersistor.saveFetchTx(tx)
54+
addressesUsedSubject.next(addresses)
55+
}
56+
}
4257
}
43-
return checkResult
4458
}
4559

4660
public retryGetBlock = async (num: string): Promise<Block> => {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export class TransactionPersistor {
117117
input.transaction = tx
118118
input.capacity = i.capacity || null
119119
input.lockHash = i.lockHash || null
120+
input.lock = i.lock || null
120121
input.since = i.since!
121122
inputs.push(input)
122123

@@ -263,6 +264,14 @@ export class TransactionPersistor {
263264
return txEntity
264265
}
265266

267+
public static get = async (txHash: string) => {
268+
const txEntity: TransactionEntity | undefined = await getConnection()
269+
.getRepository(TransactionEntity)
270+
.findOne(txHash, { relations: ['inputs', 'outputs'] })
271+
272+
return txEntity
273+
}
274+
266275
public static saveSentTx = async (
267276
transaction: TransactionWithoutHash,
268277
txHash: string

0 commit comments

Comments
 (0)