Browse Source

optimize /wallet endpoint

umbrel
kenshin-samourai 4 years ago
parent
commit
37bf6effd5
  1. 9
      lib/wallet/address-info.js
  2. 66
      lib/wallet/hd-account-info.js
  3. 8
      lib/wallet/wallet-info.js
  4. 77
      lib/wallet/wallet-service.js

9
lib/wallet/address-info.js

@ -38,10 +38,19 @@ class AddressInfo {
* @returns {Promise} * @returns {Promise}
*/ */
async loadInfo() { async loadInfo() {
return Promise.all([
this._loadBalance(),
this._loadNbTransactions()
])
}
async _loadBalance() {
const balance = await db.getAddressBalance(this.address) const balance = await db.getAddressBalance(this.address)
if (balance !== null) if (balance !== null)
this.finalBalance = balance this.finalBalance = balance
}
async _loadNbTransactions() {
const nbTxs = await db.getAddressNbTransactions(this.address) const nbTxs = await db.getAddressNbTransactions(this.address)
if (nbTxs !== null) if (nbTxs !== null)
this.nTx = nbTxs this.nTx = nbTxs

66
lib/wallet/hd-account-info.js

@ -67,38 +67,50 @@ class HdAccountInfo {
*/ */
async loadInfo() { async loadInfo() {
try { try {
const id = await db.getHDAccountId(this.xpub) await Promise.all([
//if (id == null) return false this._loadDerivationInfo(),
this._loadBalance(),
const account = await db.getHDAccount(this.xpub) this._loadUnusedIndices(),
this.created = account.hdCreated this._loadDerivedIndices(),
this.derivation = hdaHelper.typeString(account.hdType) this._loadNbTransactions(),
this.tracked = true ])
return true
this.finalBalance = await db.getHDAccountBalance(this.xpub) } catch(e) {
return false
const unusedIdx = await db.getHDAccountNextUnusedIndices(this.xpub) }
this.accountIndex = unusedIdx[0] }
this.changeIndex = unusedIdx[1]
const derivedIdx = await db.getHDAccountDerivedIndices(this.xpub) async _loadDerivationInfo() {
this.accountDerivedIndex = derivedIdx[0] const account = await db.getHDAccount(this.xpub)
this.changeDerivedIndex = derivedIdx[1] this.created = account.hdCreated
this.derivation = hdaHelper.typeString(account.hdType)
this.tracked = true
const node = hdaHelper.getNode(this.xpub)
const index = node[2].index
const threshold = Math.pow(2,31)
const hardened = (index >= threshold)
this.account = hardened ? (index - threshold) : index
this.depth = node[2].depth
}
this.nTx = await db.getHDAccountNbTransactions(this.xpub) async _loadBalance() {
this.finalBalance = await db.getHDAccountBalance(this.xpub)
}
const node = hdaHelper.getNode(this.xpub) async _loadUnusedIndices() {
const index = node[2].index const unusedIdx = await db.getHDAccountNextUnusedIndices(this.xpub)
const threshold = Math.pow(2,31) this.accountIndex = unusedIdx[0]
const hardened = (index >= threshold) this.changeIndex = unusedIdx[1]
this.account = hardened ? (index - threshold) : index }
this.depth = node[2].depth
return true async _loadDerivedIndices() {
const derivedIdx = await db.getHDAccountDerivedIndices(this.xpub)
this.accountDerivedIndex = derivedIdx[0]
this.changeDerivedIndex = derivedIdx[1]
}
} catch(e) { async _loadNbTransactions() {
return false this.nTx = await db.getHDAccountNbTransactions(this.xpub)
}
} }
/** /**

8
lib/wallet/wallet-info.js

@ -52,7 +52,7 @@ class WalletInfo {
* @returns {Promise} * @returns {Promise}
*/ */
async ensureHdAccounts() { async ensureHdAccounts() {
return util.seriesCall(this.entities.xpubs, async xpub => { return util.parallelCall(this.entities.xpubs, async xpub => {
const hdaInfo = new HdAccountInfo(xpub) const hdaInfo = new HdAccountInfo(xpub)
return hdaInfo.ensureHdAccount() return hdaInfo.ensureHdAccount()
}) })
@ -63,7 +63,7 @@ class WalletInfo {
* @returns {Promise} * @returns {Promise}
*/ */
async loadHdAccountsInfo() { async loadHdAccountsInfo() {
return util.seriesCall(this.entities.xpubs, async xpub => { return util.parallelCall(this.entities.xpubs, async xpub => {
const hdaInfo = new HdAccountInfo(xpub) const hdaInfo = new HdAccountInfo(xpub)
await hdaInfo.loadInfo() await hdaInfo.loadInfo()
this.wallet.finalBalance += hdaInfo.finalBalance this.wallet.finalBalance += hdaInfo.finalBalance
@ -113,7 +113,7 @@ class WalletInfo {
* @returns {Promise} * @returns {Promise}
*/ */
async loadAddressesInfo() { async loadAddressesInfo() {
return util.seriesCall(this.entities.addrs, async address => { return util.parallelCall(this.entities.addrs, async address => {
const addrInfo = new AddressInfo(address) const addrInfo = new AddressInfo(address)
await addrInfo.loadInfo() await addrInfo.loadInfo()
this.wallet.finalBalance += addrInfo.finalBalance this.wallet.finalBalance += addrInfo.finalBalance
@ -175,7 +175,7 @@ class WalletInfo {
*/ */
async loadUtxos() { async loadUtxos() {
// Load the utxos for the hd accounts // Load the utxos for the hd accounts
await util.seriesCall(this.entities.xpubs, async xpub => { await util.parallelCall(this.entities.xpubs, async xpub => {
const hdaInfo = new HdAccountInfo(xpub) const hdaInfo = new HdAccountInfo(xpub)
const utxos = await hdaInfo.loadUtxos() const utxos = await hdaInfo.loadUtxos()
for (let utxo of utxos) for (let utxo of utxos)

77
lib/wallet/wallet-service.js

@ -51,37 +51,53 @@ class WalletService {
const walletInfo = new WalletInfo(active) const walletInfo = new WalletInfo(active)
try { try {
// Add the new xpubs await Promise.all([
await util.seriesCall(legacy.xpubs, this._newBIP44)
await util.seriesCall(bip49.xpubs, this._newBIP49) // Add the new xpubs
await util.seriesCall(bip84.xpubs, this._newBIP84) util.parallelCall(legacy.xpubs, this._newBIP44),
// Load hd accounts info util.parallelCall(bip49.xpubs, this._newBIP49),
await walletInfo.ensureHdAccounts() util.parallelCall(bip84.xpubs, this._newBIP84),
await walletInfo.loadHdAccountsInfo() // Add the new addresses
// Add the new addresses db.addAddresses(legacy.addrs),
await db.addAddresses(legacy.addrs) db.addAddresses(bip49.addrs),
await db.addAddresses(bip49.addrs) db.addAddresses(bip84.addrs),
await db.addAddresses(bip84.addrs) db.addAddresses(pubkeys.addrs),
await db.addAddresses(pubkeys.addrs) ])
// Ensure addresses exist
await walletInfo.ensureAddresses() // Ensure hd accounts and addresses exist
await Promise.all([
walletInfo.ensureHdAccounts(),
walletInfo.ensureAddresses(),
])
// Force import of addresses associated to paynyms // Force import of addresses associated to paynyms
// if dojo relies on a local index // if dojo relies on a local index
if (keys.indexer.active != 'third_party_explorer') if (keys.indexer.active != 'third_party_explorer')
await this._forceEnsureAddressesForActivePubkeys(active) await this._forceEnsureAddressesForActivePubkeys(active)
// Filter the addresses // Filter the addresses
await walletInfo.filterAddresses() await walletInfo.filterAddresses()
// Load the utxos
await walletInfo.loadUtxos() // Load wallet information
// Load the addresses await Promise.all([
await walletInfo.loadAddressesInfo() // Load the hd accounts,
// Load the most recent transactions walletInfo.loadHdAccountsInfo(),
await walletInfo.loadTransactions(0, null, true) // Load the utxos
// Load feerates walletInfo.loadUtxos(),
await walletInfo.loadFeesInfo() // Load the addresses
walletInfo.loadAddressesInfo(),
// Load the most recent transactions
walletInfo.loadTransactions(0, null, true),
// Load feerates
walletInfo.loadFeesInfo(),
])
// Postprocessing // Postprocessing
await walletInfo.postProcessAddresses() await Promise.all([
await walletInfo.postProcessHdAccounts() walletInfo.postProcessAddresses(),
walletInfo.postProcessHdAccounts(),
])
// Format the result // Format the result
return this._formatGetFullWalletInfoResult(walletInfo) return this._formatGetFullWalletInfoResult(walletInfo)
@ -280,13 +296,18 @@ class WalletService {
try { try {
// Filter the addresses // Filter the addresses
await walletInfo.filterAddresses() await walletInfo.filterAddresses()
// Load the number of transactions
await walletInfo.loadNbTransactions() await Promise.all([
// Load the requested page of transactions // Load the number of transactions
await walletInfo.loadTransactions(page, count, false) walletInfo.loadNbTransactions(),
// Load the requested page of transactions
walletInfo.loadTransactions(page, count, false),
])
// Postprocessing // Postprocessing
await walletInfo.postProcessAddresses() await walletInfo.postProcessAddresses()
await walletInfo.postProcessHdAccounts() await walletInfo.postProcessHdAccounts()
// Format the result // Format the result
ret.n_tx = walletInfo.nTx ret.n_tx = walletInfo.nTx
ret.txs = walletInfo.txs ret.txs = walletInfo.txs

Loading…
Cancel
Save