DataFinder.ts 5.77 KB
Newer Older
Cédric Moreau's avatar
Cédric Moreau committed
1
import {Server} from 'duniter/server'
Cédric Moreau's avatar
Cédric Moreau committed
2
import {DBBlock} from 'duniter/app/lib/db/DBBlock'
3
import {MonitorExecutionTime} from './MonitorExecutionTime'
Cédric Moreau's avatar
Cédric Moreau committed
4
5
6
7
8
9
10
11
12
13
14
15

export class DataFinder {

  private memCache: {
    [cacheName: string]: {
      [k: string]: any
    }
  } = {}

  constructor(protected duniterServer: Server) {
  }

16
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
17
18
19
20
  findPendingMembers() {
    return this.query('SELECT `buid`,`pubkey`,`uid`,`hash`,`expires_on`,`revocation_sig` FROM identities_pending WHERE `member`=0')
  }

21
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
22
23
24
25
26
  findPendingCertsToTarget(toPubkey: string, hash: string) {
    return this.getFromCacheOrDB('findPendingCertsToTarget', [toPubkey, hash].join('-'), () => this.query(
      'SELECT `from`,`block_number`,`block_hash`,`expires_on` FROM certifications_pending WHERE `to`=\''+toPubkey+'\' AND `target`=\''+hash+'\' ORDER BY `expires_on` DESC'))
  }

27
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
28
29
30
31
32
33
  getWotexInfos(uid: string) {
    return this.duniterServer.dal.idtyDAL.query('' +
      'SELECT hash, uid, pub, wotb_id FROM i_index WHERE uid = ? ' +
      'UNION ALL ' + 'SELECT hash, uid, pubkey as pub, (SELECT NULL) AS wotb_id FROM idty WHERE uid = ?', [uid, uid])
  }

34
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
35
36
  async getBlock(block_number: number): Promise<DBBlock|undefined> {
    return (await this.getFromCacheOrDB('getBlock', String(block_number),() => this.duniterServer.dal.getBlock(block_number))) || undefined
Cédric Moreau's avatar
Cédric Moreau committed
37
38
  }

39
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
40
41
42
43
  getUidOfPub(pub: string): Promise<{ uid: string }[]> {
    return this.getFromCacheOrDB('getUidOfPub', pub, () => this.query('SELECT `uid` FROM i_index WHERE `pub`=\''+pub+'\' LIMIT 1'))
  }

44
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
45
46
47
48
  async getWotbIdByIssuerPubkey(issuerPubkey: string) {
    return this.getFromCacheOrDB('getWotbIdByIssuerPubkey', issuerPubkey, async () => (await this.duniterServer.dal.iindexDAL.query('SELECT wotb_id FROM i_index WHERE pub = ? AND wotb_id IS NOT NULL', [issuerPubkey]))[0].wotb_id)
  }

49
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
50
51
52
53
  getChainableOnByIssuerPubkey(issuerPubkey: string) {
    return this.query('SELECT `chainable_on` FROM c_index WHERE `issuer`=\''+issuerPubkey+'\' ORDER BY `chainable_on` DESC LIMIT 1')
  }

54
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
55
56
57
58
  getChainableOnByIssuerPubkeyByExpOn(from: string) {
    return this.getFromCacheOrDB('getChainableOnByIssuerPubkeyByExpOn', from, () => this.query('SELECT `chainable_on` FROM c_index WHERE `issuer`=\''+from+'\' ORDER BY `expires_on` DESC LIMIT 1'))
  }

59
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
60
61
62
63
  getCurrentBlockOrNull() {
    return this.duniterServer.dal.getCurrentBlockOrNull()
  }

64
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
65
66
67
68
69
  findCertsOfIssuer(pub: string, tmpOrder: string) {
    return this.getFromCacheOrDB('findCertsOfIssuer', [pub, tmpOrder].join('-'), () => this.query(
      'SELECT `receiver`,`written_on`,`expires_on` FROM c_index WHERE `issuer`=\''+pub+'\' ORDER BY `expires_on` '+tmpOrder))
  }

70
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
71
72
73
74
75
  findCertsOfReceiver(pub: any, tmpOrder: string) {
    return this.getFromCacheOrDB('findCertsOfReceiver', [pub, tmpOrder].join('-'), () => this.query(
      'SELECT `issuer`,`written_on`,`expires_on` FROM c_index WHERE `receiver`=\''+pub+'\' ORDER BY `expires_on` '+tmpOrder))
  }

76
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
77
78
79
80
  getProtagonist(pub: string) {
    return this.getFromCacheOrDB('getProtagonist', pub, () => this.query('SELECT `uid`,`wotb_id` FROM i_index WHERE `pub`=\''+pub+'\' LIMIT 1'))
  }

81
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
82
83
84
85
86
  getCertsPending(pub: string, tmpOrder: string) {
    return this.getFromCacheOrDB('getCertsPending', [pub, tmpOrder].join('-'), () => this.query(
      'SELECT `from`,`to`,`block_number`,`expires_on` FROM certifications_pending WHERE `from`=\''+pub+'\' ORDER BY `expires_on` '+tmpOrder))
  }

87
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
88
89
90
91
92
  getCertsPendingFromTo(pub: any, tmpOrder: string) {
    return this.getFromCacheOrDB('getCertsPendingFromTo', [pub, tmpOrder].join('-'), () => this.query(
      'SELECT `from`,`block_number`,`block_hash`,`expires_on` FROM certifications_pending WHERE `to`=\''+pub+'\' ORDER BY `expires_on` '+tmpOrder))
  }

93
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
94
95
96
97
  getMembers() {
    return this.getFromCacheOrDB('getMembers', 'members', () => this.query('SELECT `uid`,`pub`,`member`,`written_on`,`wotb_id` FROM i_index WHERE `member`=1'))
  }

98
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
99
100
101
102
103
  membershipWrittenOnExpiresOn(pub: string) {
    return this.getFromCacheOrDB('membershipWrittenOnExpiresOn', pub, () => this.query(
      'SELECT `written_on`,`expires_on` FROM m_index WHERE `pub`=\''+pub+'\' ORDER BY `expires_on` DESC LIMIT 1'))
  }

104
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
105
106
107
108
  query(sql: string, params?: any[]) {
    return this.duniterServer.dal.peerDAL.query(sql, params || [])
  }

109
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
110
111
112
113
114
115
116
117
  async getFromCacheOrDB<T>(cacheName: string, key: string, fetcher: () => Promise<T>) {
    const cache = this.memCache[cacheName] || (this.memCache[cacheName] = {})
    if (cache[key]) {
      return cache[key]
    }
    return cache[key] = await fetcher()
  }

118
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
119
120
121
  invalidateCache() {
    this.memCache = {}
  }
Cédric Moreau's avatar
Cédric Moreau committed
122

123
  @MonitorExecutionTime()
Cédric Moreau's avatar
Cédric Moreau committed
124
125
126
127
128
  getBlockWhereMedianTimeGt(previousBlockchainTime: number) {
    return this.getFromCacheOrDB('getBlockWhereMedianTimeGt', String(previousBlockchainTime),
      () => this.query('SELECT `issuer`,`membersCount`,`medianTime`,`dividend`,`number`,`nonce` FROM block WHERE `fork`=0 AND `medianTime` > '+previousBlockchainTime+' ORDER BY `medianTime` ASC'))
  }

129
  @MonitorExecutionTime()
130
131
  getBlockWhereMedianTimeLteAndGt(medianTime: number, previousBlockchainTime: number) {
    return this.getFromCacheOrDB('getBlockWhereMedianTimeLteAndGt', [medianTime, previousBlockchainTime].join('-'),
Cédric Moreau's avatar
Cédric Moreau committed
132
133
      () => this.query('SELECT `issuer`,`membersCount`,`medianTime`,`dividend`,`number`,`nonce` FROM block WHERE `fork`=0 AND `medianTime` <= '+medianTime+' AND `medianTime` > '+previousBlockchainTime+' ORDER BY `medianTime` ASC'))
  }
Cédric Moreau's avatar
Cédric Moreau committed
134
}