Skip to content

Commit

Permalink
feat: add lock to input
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Oct 9, 2019
1 parent e7e9e83 commit 436b388
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 17 deletions.
9 changes: 8 additions & 1 deletion packages/neuron-wallet/src/database/chain/entities/input.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Entity, BaseEntity, Column, ManyToOne, PrimaryGeneratedColumn } from 'typeorm'
import { OutPoint, Input as InputInterface } from 'types/cell-types'
import { OutPoint, Input as InputInterface, Script } from 'types/cell-types'
import Transaction from './transaction'

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

// cellbase input has no previous output lock script
@Column({
type: 'simple-json',
nullable: true,
})
lock: Script | null = null

@ManyToOne(_type => Transaction, transaction => transaction.inputs, { onDelete: 'CASCADE' })
transaction!: Transaction

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {MigrationInterface, QueryRunner, TableColumn} from "typeorm";

export class AddLockToInput1570522869590 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.addColumn('input', new TableColumn({
name: 'lock',
type: 'text',
isNullable: true,
}))
}

public async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.dropColumn('input', 'lock')
}

}
8 changes: 7 additions & 1 deletion packages/neuron-wallet/src/database/chain/ormconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import SyncInfo from './entities/sync-info'
import { InitMigration1566959757554 } from './migrations/1566959757554-InitMigration'
import { AddTypeAndHasData1567144517514 } from './migrations/1567144517514-AddTypeAndHasData'
import { ChangeHasDataDefault1568621556467 } from './migrations/1568621556467-ChangeHasDataDefault'
import { AddLockToInput1570522869590 } from './migrations/1570522869590-AddLockToInput'

export const CONNECTION_NOT_FOUND_NAME = 'ConnectionNotFoundError'

Expand All @@ -32,7 +33,12 @@ const connectOptions = async (genesisBlockHash: string): Promise<SqliteConnectio
type: 'sqlite',
database,
entities: [Transaction, Input, Output, SyncInfo],
migrations: [InitMigration1566959757554, AddTypeAndHasData1567144517514, ChangeHasDataDefault1568621556467],
migrations: [
InitMigration1566959757554,
AddTypeAndHasData1567144517514,
ChangeHasDataDefault1568621556467,
AddLockToInput1570522869590,
],
logging,
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/neuron-wallet/src/services/cells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ export default class CellsService {
previousOutput: cell.outPoint(),
since: '0',
lock: cell.lock,
lockHash: cell.lockHash,
capacity: cell.capacity,
}
inputs.push(input)
inputCapacities += BigInt(cell.capacity)
Expand Down
27 changes: 19 additions & 8 deletions packages/neuron-wallet/src/services/indexer/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import IndexerTransaction from 'services/tx/indexer-transaction'
import IndexerRPC from './indexer-rpc'
import HexUtils from 'utils/hex'
import { TxUniqueFlagCache } from './tx-unique-flag'
import TransactionEntity from 'database/chain/entities/transaction';

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

// tx timestamp / blockNumber / blockHash
const { blockHash } = transactionWithStatus.txStatus
if (blockHash) {
const blockHeader = await this.getBlocksService.getHeader(blockHash)
transaction.blockHash = blockHash
transaction.blockNumber = blockHeader.number
transaction.timestamp = blockHeader.timestamp
let txEntity: TransactionEntity | undefined = await TransactionPersistor.get(transaction.hash)
if (!txEntity || !txEntity.blockHash) {
for (const input of transaction.inputs!) {
const previousTxWithStatus = await this.getBlocksService.getTransaction(input.previousOutput!.txHash)
const previousTx = TypeConvert.toTransaction(previousTxWithStatus.transaction)
const previousOutput = previousTx.outputs![+input.previousOutput!.index]
input.lock = previousOutput.lock
input.lockHash = LockUtils.lockScriptToHash(input.lock)
input.capacity = previousOutput.capacity
}
const { blockHash } = transactionWithStatus.txStatus
if (blockHash) {
const blockHeader = await this.getBlocksService.getHeader(blockHash)
transaction.blockHash = blockHash
transaction.blockNumber = blockHeader.number
transaction.timestamp = blockHeader.timestamp
}
txEntity = await TransactionPersistor.saveFetchTx(transaction)
}
// broadcast address used
const txEntity = await TransactionPersistor.saveFetchTx(transaction)

let address: string | undefined
if (type === TxPointType.CreatedBy) {
Expand Down
28 changes: 21 additions & 7 deletions packages/neuron-wallet/src/services/sync/get-blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { generateCore } from 'services/sdk-core'

import { Block, BlockHeader } from 'types/cell-types'
import TypeConvert from 'types/type-convert'
import CheckAndSave from './check-and-save'
import Utils from './utils'
import HexUtils from 'utils/hex'
import CheckTx from 'services/sync/check-and-save/tx'
import { TransactionPersistor } from 'services/tx'
import LockUtils from 'models/lock-utils'
import { addressesUsedSubject } from './renderer-params'

export default class GetBlocks {
private retryTime: number
Expand Down Expand Up @@ -33,14 +36,25 @@ export default class GetBlocks {
return tip
}

public checkAndSave = async (blocks: Block[], lockHashes: string[]) => {
let checkResult: boolean[][] = []
public checkAndSave = async (blocks: Block[], lockHashes: string[]): Promise<void> => {
for (const block of blocks) {
const checkAndSave = new CheckAndSave(block, lockHashes)
const result = await checkAndSave.process()
checkResult.push(result)
for (const tx of block.transactions) {
const checkTx = new CheckTx(tx)
const addresses = await checkTx.check(lockHashes)
if (addresses.length > 0) {
for (const input of tx.inputs!) {
const previousTxWithStatus = await this.getTransaction(input.previousOutput!.txHash)
const previousTx = TypeConvert.toTransaction(previousTxWithStatus.transaction)
const previousOutput = previousTx.outputs![+input.previousOutput!.index]
input.lock = previousOutput.lock
input.lockHash = LockUtils.lockScriptToHash(input.lock)
input.capacity = previousOutput.capacity
}
await TransactionPersistor.saveFetchTx(tx)
addressesUsedSubject.next(addresses)
}
}
}
return checkResult
}

public retryGetBlock = async (num: string): Promise<Block> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export class TransactionPersistor {
input.transaction = tx
input.capacity = i.capacity || null
input.lockHash = i.lockHash || null
input.lock = i.lock || null
input.since = i.since!
inputs.push(input)

Expand Down Expand Up @@ -263,6 +264,14 @@ export class TransactionPersistor {
return txEntity
}

public static get = async (txHash: string) => {
const txEntity: TransactionEntity | undefined = await getConnection()
.getRepository(TransactionEntity)
.findOne(txHash, { relations: ['inputs', 'outputs'] })

return txEntity
}

public static saveSentTx = async (
transaction: TransactionWithoutHash,
txHash: string
Expand Down

0 comments on commit 436b388

Please sign in to comment.