Skip to content

Commit

Permalink
feat: add typeHash and daoData when sync
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Nov 4, 2019
1 parent 5efe225 commit 1b6b062
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/neuron-wallet/src/database/chain/meta-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface MetaInfo {
genesisBlockHash: string
systemScriptInfo: SystemScript
chain: string // ckb | ckb_testnet | ckb_dev
daoScriptInfo: SystemScript
}

export const updateMetaInfo = (metaInfo: MetaInfo) => {
Expand Down
52 changes: 52 additions & 0 deletions packages/neuron-wallet/src/models/dao-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import NodeService from 'services/node'
import { ScriptHashType } from 'types/cell-types'
import Core from '@nervosnetwork/ckb-sdk-core'
import { SystemScript } from './lock-utils'
import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils'

export default class DaoUtils {
daoScript: SystemScript

constructor(daoScript: SystemScript) {
this.daoScript = daoScript
}

static daoScriptInfo: SystemScript | undefined

static previousURL: string | undefined

static async loadDaoScript(nodeURL: string): Promise<SystemScript> {
const core = new Core(nodeURL)

const genesisBlock = await core.rpc.getBlockByNumber(BigInt(0))
const systemCellTransaction = genesisBlock.transactions[0]
const daoOutPoint = {
txHash: systemCellTransaction.hash,
index: '2'
}

const daoTypeHash = scriptToHash(systemCellTransaction.outputs[2].type!)

return {
codeHash: daoTypeHash,
outPoint: daoOutPoint,
hashType: ScriptHashType.Type
}
}

static async daoScript(nodeURL: string = NodeService.getInstance().core.rpc.node.url): Promise<SystemScript> {
if (DaoUtils.daoScriptInfo && nodeURL === DaoUtils.previousURL) {
return DaoUtils.daoScriptInfo
}

DaoUtils.daoScriptInfo = await DaoUtils.loadDaoScript(nodeURL)
DaoUtils.previousURL = nodeURL

return DaoUtils.daoScriptInfo
}

static setDaoScript(info: SystemScript) {
DaoUtils.daoScriptInfo = info
DaoUtils.previousURL = NodeService.getInstance().core.rpc.node.url
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ export default class CheckAndSave {
private block: Block
private lockHashes: string[]
private url: string
private daoScriptHash: string

constructor(block: Block, lockHashes: string[], url: string) {
constructor(block: Block, lockHashes: string[], url: string, daoScriptHash: string) {
this.block = block
this.lockHashes = lockHashes
this.url = url
this.daoScriptHash = daoScriptHash
}

public process = async (): Promise<boolean[]> => {
const txs = this.block.transactions
let result: boolean[] = []
for (const tx of txs) {
const checkTx = new CheckTx(tx, this.url)
const checkTx = new CheckTx(tx, this.url, this.daoScriptHash)
const checkResult = await checkTx.checkAndSave(this.lockHashes)
result.push(checkResult)
}
Expand Down
11 changes: 10 additions & 1 deletion packages/neuron-wallet/src/services/sync/check-and-save/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ export default class CheckTx {
private tx: Transaction
private addressesUsedSubject: Subject<AddressesWithURL>
private url: string
private daoTypeHash: string

constructor(
tx: Transaction,
url: string,
daoTypeHash: string,
addressesUsedSubject: Subject<AddressesWithURL> = addressesUsedSubjectParam,
) {
this.tx = tx
this.addressesUsedSubject = addressesUsedSubject

this.url = url
this.daoTypeHash = daoTypeHash
}

public check = async (lockHashes: string[]): Promise<string[]> => {
Expand Down Expand Up @@ -51,10 +54,16 @@ export default class CheckTx {
}

public filterOutputs = (lockHashes: string[]) => {
const cells: Cell[] = this.tx.outputs!.map(output => {
const cells: Cell[] = this.tx.outputs!.map((output, index) => {
const checkOutput = new CheckOutput(output)
const result = checkOutput.checkLockHash(lockHashes)
if (result) {
if (output.type) {
this.tx.outputs![index].typeHash = LockUtils.computeScriptHash(output.type)
if (output.typeHash === this.daoTypeHash) {
this.tx.outputs![index].daoData = this.tx.outputsData![index]
}
}
return output
}
return false
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-wallet/src/services/sync/get-blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ export default class GetBlocks {
return this.core.rpc.getTipBlockNumber()
}

public checkAndSave = async (blocks: Block[], lockHashes: string[]): Promise<void> => {
public checkAndSave = async (blocks: Block[], lockHashes: string[], daoScriptHash: string): Promise<void> => {
const cachedPreviousTxs = new Map()
for (const block of blocks) {
logger.debug(`checking block #${block.header.number}, ${block.transactions.length} txs`)
for (const tx of block.transactions) {
const checkTx = new CheckTx(tx, this.url)
const checkTx = new CheckTx(tx, this.url, daoScriptHash)
const addresses = await checkTx.check(lockHashes)
if (addresses.length > 0) {
for (const input of tx.inputs!) {
Expand Down
14 changes: 13 additions & 1 deletion packages/neuron-wallet/src/services/sync/queue.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Block, BlockHeader } from 'types/cell-types'
import { TransactionPersistor } from 'services/tx'
import logger from 'utils/logger'
import LockUtils from 'models/lock-utils'
import DaoUtils from 'models/dao-utils'

import GetBlocks from './get-blocks'
import RangeForCheck, { CheckResultType } from './range-for-check'
Expand All @@ -22,6 +24,8 @@ export default class Queue {

private yieldTime = 1

private url: string

constructor(
url: string,
lockHashes: string[],
Expand All @@ -32,6 +36,7 @@ export default class Queue {
start: boolean = true
) {
this.lockHashes = lockHashes
this.url = url
this.getBlocksService = new GetBlocks(url)
this.startBlockNumber = BigInt(startBlockNumber)
this.endBlockNumber = BigInt(endBlockNumber)
Expand Down Expand Up @@ -117,8 +122,15 @@ export default class Queue {
return
}

const daoScriptInfo = await DaoUtils.daoScript(this.url)
const daoScriptHash = LockUtils.computeScriptHash({
codeHash: daoScriptInfo.codeHash,
args: "0x",
hashType: daoScriptInfo.hashType,
})

// 3. check and save
await this.getBlocksService.checkAndSave(blocks, this.lockHashes)
await this.getBlocksService.checkAndSave(blocks, this.lockHashes, daoScriptHash)

// 4. update currentBlockNumber
const lastBlock = blocks[blocks.length - 1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,17 @@ export class TransactionPersistor {
output.status = outputStatus
if (o.type) {
output.typeScript = o.type
output.typeHash = o.typeHash ? o.typeHash : null
}
const data = outputsData[index]
if (data && data !== '0x') {
output.hasData = true
} else {
output.hasData = false
}
if (o.daoData) {
output.daoData = o.daoData
}
return output
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import LockUtils from 'models/lock-utils'
import logger from 'utils/logger'
import genesisBlockHash, { getChain } from './genesis'
import ChainInfo from 'models/chain-info'
import DaoUtils from '../../models/dao-utils';

// only used by main process
export class InitDatabase {
Expand Down Expand Up @@ -47,7 +48,8 @@ export class InitDatabase {

try {
const systemScriptInfo = await LockUtils.systemScript(url)
updateMetaInfo({ genesisBlockHash: hash, systemScriptInfo, chain })
const daoScriptInfo = await DaoUtils.daoScript(url)
updateMetaInfo({ genesisBlockHash: hash, systemScriptInfo, chain, daoScriptInfo })
} catch (err) {
logger.error('update systemScriptInfo failed:', err)
}
Expand All @@ -61,6 +63,7 @@ export class InitDatabase {
chain = metaInfo.chain
ChainInfo.getInstance().setChain(chain)
LockUtils.setSystemScript(metaInfo.systemScriptInfo)
DaoUtils.setDaoScript(metaInfo.daoScriptInfo)
hash = metaInfo.genesisBlockHash
this.success = true
} catch (error) {
Expand Down
2 changes: 2 additions & 0 deletions packages/neuron-wallet/src/types/cell-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ export interface Cell {
outPoint?: OutPoint
status?: string
lockHash?: string
typeHash?: string | null
daoData?: string | null
}

export interface OutPoint {
Expand Down

0 comments on commit 1b6b062

Please sign in to comment.