[wallet] Avoid unnecessary balance checks

This commit is contained in:
Roman Volosovskyi 2021-01-25 17:49:32 +02:00
parent c2f8f012a8
commit 9bf4da1edd
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
1 changed files with 20 additions and 4 deletions

View File

@ -145,6 +145,9 @@
(defn get-min-known-block [db address] (defn get-min-known-block [db address]
(get-in db [:wallet :accounts (eip55/address->checksum address) :min-block])) (get-in db [:wallet :accounts (eip55/address->checksum address) :min-block]))
(defn get-max-block-with-transfers [db address]
(get-in db [:wallet :accounts (eip55/address->checksum address) :max-block]))
(defn min-block-transfers-count [db address] (defn min-block-transfers-count [db address]
(get-in db [:wallet :accounts (get-in db [:wallet :accounts
(eip55/address->checksum address) (eip55/address->checksum address)
@ -182,6 +185,17 @@
:min-block min-block :min-block min-block
:min-block-transfers-count min-block-transfers-count)})) :min-block-transfers-count min-block-transfers-count)}))
(fx/defn set-max-block-with-transfers
[{:keys [db]} address transfers]
(let [max-block (reduce
(fn [max-block {:keys [block]}]
(if (> block max-block)
block
max-block))
(get-in db [:wallet :accounts address :max-block] 0)
transfers)]
{:db (assoc-in db [:wallet :accounts address :max-block] max-block)}))
(defn update-fetching-status (defn update-fetching-status
[db addresses fetching-type state] [db addresses fetching-type state]
(update-in (update-in
@ -214,10 +228,12 @@
"count" (count transfers) "count" (count transfers)
"limit" limit) "limit" limit)
(let [checksum (eip55/address->checksum address) (let [checksum (eip55/address->checksum address)
min-known-block (or (get-min-known-block db address) max-known-block (or (get-max-block-with-transfers db address)
(:ethereum/current-block db)) 0)
effects (cond-> [(when (seq transfers) effects (cond-> [(when (seq transfers)
(set-lowest-fetched-block checksum transfers))] (set-lowest-fetched-block checksum transfers))
(when (seq transfers)
(set-max-block-with-transfers checksum transfers))]
(seq transfers) (seq transfers)
(concat (mapv add-transfer transfers)) (concat (mapv add-transfer transfers))
@ -231,7 +247,7 @@
true true
(conj (wallet/update-balances (conj (wallet/update-balances
(into [] (reduce (fn [acc {:keys [address block]}] (into [] (reduce (fn [acc {:keys [address block]}]
(if (>= block min-known-block) (if (> block max-known-block)
(conj acc address) (conj acc address)
acc)) acc))
#{} #{}