More fixes for db field renaming

This commit is contained in:
Vitaliy Vlasov 2018-05-24 00:36:46 +03:00
parent 3d9fb55621
commit 8672376a98
No known key found for this signature in database
GPG Key ID: A7D57C347F2B2964
11 changed files with 72 additions and 49 deletions

View File

@ -447,6 +447,7 @@ SELECT
i.payout_receipt AS payout_receipt, i.payout_receipt AS payout_receipt,
i.commit_sha AS commit_sha, i.commit_sha AS commit_sha,
u.address AS owner_address, u.address AS owner_address,
u.login AS owner_login,
i.contract_address AS contract_address, i.contract_address AS contract_address,
i.confirm_hash AS confirm_hash, i.confirm_hash AS confirm_hash,
i.title AS title, i.title AS title,

View File

@ -22,12 +22,17 @@
(let [labels (:labels issue)] (let [labels (:labels issue)]
(some #(= label-name (:name %)) labels))) (some #(= label-name (:name %)) labels)))
(def last-states (atom {}))
(defn transition [{:keys [issue-id tx-info] :as bounty} state] (defn transition [{:keys [issue-id tx-info] :as bounty} state]
(let [bounty-not= (fn [current db] (let [bounty-not= (fn [current db]
(some #(not= (%1 current) (%1 db)) (some #(not= (%1 current) (%1 db))
(disj (set (keys current)) :tx-info))) (disj (set (keys current)) :tx-info)))
bounty-from-db (issues/get-issue-by-id issue-id) bounty-from-db (issues/get-issue-by-id issue-id)
bounty (and (bounty-not= bounty bounty-from-db) bounty (and (or
(and (= state :pending-contributor-address)
(not= state (get @last-states issue-id)))
(bounty-not= bounty bounty-from-db))
(merge bounty-from-db bounty))] (merge bounty-from-db bounty))]
(when bounty (when bounty
(case state (case state
@ -64,9 +69,10 @@
(:balance-eth bounty) (:balance-eth bounty)
(:tokens bounty) (:tokens bounty)
(:value-usd bounty))) (:value-usd bounty)))
nil)
(github/update-comment bounty state)))
(swap! last-states assoc issue-id state))
)
(github/update-comment bounty state))))
(defn deploy-contract [owner-address issue-id] (defn deploy-contract [owner-address issue-id]
(if (empty? owner-address) (if (empty? owner-address)
(log/errorf "issue %s: Unable to deploy bounty contract because repo owner has no Ethereum addres" issue-id) (log/errorf "issue %s: Unable to deploy bounty contract because repo owner has no Ethereum addres" issue-id)
@ -82,6 +88,20 @@
(log/errorf "issue %s Failed to deploy contract to %s" issue-id owner-address)) (log/errorf "issue %s Failed to deploy contract to %s" issue-id owner-address))
(catch Exception ex (log/errorf ex "issue %s: deploy-contract exception" issue-id))))) (catch Exception ex (log/errorf ex "issue %s: deploy-contract exception" issue-id)))))
(defn execute-payout [issue-id contract-address payout-address]
(if (empty? payout-address)
(do
(log/warn "issue %s: Cannot sign pending bounty - winner has no payout address" issue-id)
(transition {:issue-id issue-id} :pending-contributor-address))
(let [tx-info (multisig/send-all {:contract contract-address
:payout-address payout-address
:internal-tx-id [:execute issue-id]})]
(log/infof "issue %s: Payout self-signed, called sign-all(%s) tx: %s" issue-id contract-address payout-address (:tx-hash tx-info))
(transition {:execute-hash (:tx-hash tx-info)
:issue-id issue-id
:tx-info tx-info} :pending-sob-confirmation)
tx-info)))
(defn add-bounty-for-issue [repo repo-id issue] (defn add-bounty-for-issue [repo repo-id issue]
(let [{issue-id :id (let [{issue-id :id
issue-number :number issue-number :number

View File

@ -68,17 +68,17 @@
(db/reset-payout-hash con-db {:issue_id issue-id}))) (db/reset-payout-hash con-db {:issue_id issue-id})))
(def payout-receipt-keys (def payout-receipt-keys
[:issue_id [:issue-id
:payout_hash :payout-hash
:contract_address :contract-address
:repo :repo
:owner :owner
:comment_id :comment-id
:issue_number :issue-number
:balance_eth :balance-eth
:tokens :tokens
:confirm_hash :confirm-hash
:payee_login :payee-login
:updated]) :updated])
(defn update-payout-receipt (defn update-payout-receipt

View File

@ -333,7 +333,8 @@
[{:keys [issue-id owner repo comment-id issue-number contract-address [{:keys [issue-id owner repo comment-id issue-number contract-address
balance-eth tokens balance-eth tokens
payout-receipt payout-receipt
winner-login winner-address transaction-hash] :as issue} owner-login
winner-login transaction-hash] :as issue}
state] state]
(let [comment (case state (let [comment (case state
:deploying :deploying
@ -345,17 +346,23 @@
contract-address contract-address
balance-eth balance-eth
tokens) tokens)
(:pending-maintainer-confirmation :pending-contributor-address) :pending-sob-confirmation
(generate-merged-comment contract-address (generate-merged-comment contract-address
balance-eth balance-eth
tokens tokens
winner-login (or winner-login owner-login)
(str/blank? winner-address)) false)
:pending-contributor-address
(generate-merged-comment contract-address
balance-eth
tokens
(or winner-login owner-login)
true)
:paid :paid
(generate-paid-comment contract-address (generate-paid-comment contract-address
balance-eth balance-eth
tokens tokens
winner-login) (or winner-login owner-login))
nil)] nil)]
(log/info (str "Updating " owner "/" repo "/" issue-number (log/info (str "Updating " owner "/" repo "/" issue-number
" comment #" comment-id " with contents: " comment)) " comment #" comment-id " with contents: " comment))

View File

@ -27,10 +27,10 @@
[token] [token]
(let [user (github/get-user token) (let [user (github/get-user token)
{email :email {email :email
user-id :id} user] user-id :id} user
(log/debug "get-or-create-user" user) db-user (users/get-user user-id)]
(or (if (:id db-user)
(users/get-user user-id) db-user
(create-user token user)))) (create-user token user))))
(defroutes redirect-routes (defroutes redirect-routes

View File

@ -19,7 +19,8 @@
[clojure.tools.logging :as log] [clojure.tools.logging :as log]
[commiteth.config :refer [env]] [commiteth.config :refer [env]]
[commiteth.util.util :refer [usd-decimal->str [commiteth.util.util :refer [usd-decimal->str
eth-decimal->str]] eth-decimal->str
to-db-map]]
[crypto.random :as random] [crypto.random :as random]
[clojure.set :refer [rename-keys]] [clojure.set :refer [rename-keys]]
[clojure.string :as str] [clojure.string :as str]
@ -148,11 +149,8 @@
(defn execute-revocation [issue-id contract-address payout-address] (defn execute-revocation [issue-id contract-address payout-address]
(log/info (str "executing revocation for " issue-id "at" contract-address)) (log/info (str "executing revocation for " issue-id "at" contract-address))
(try (try
(let [tx-info (multisig/send-all {:contract contract-address (let [tx-info (bounties/execute-payout issue-id contract-address payout-address)]
:payout-address payout-address (:tx-hash tx-info))
:internal-tx-id [:execute issue-id]})]
(tracker/track-tx! tx-info)
{:execute-hash (:tx-hash tx-info)})
(catch Throwable ex (catch Throwable ex
(log/errorf ex "error revoking funds for %s" issue-id)))) (log/errorf ex "error revoking funds for %s" issue-id))))
@ -197,11 +195,11 @@
:auth-rules authenticated? :auth-rules authenticated?
:current-user user :current-user user
:body [body {:address s/Str :body [body {:address s/Str
:is_hidden_in_hunters s/Bool}] :is-hidden-in-hunters s/Bool}]
:summary "Updates user's fields." :summary "Updates user's fields."
(let [user-id (:id user) (let [user-id (:id user)
{:keys [address]} body] {:keys [address is-hidden-in-hunters]} body]
(when-not (eth/valid-address? address) (when-not (eth/valid-address? address)
(log/debugf "POST /user: Wrong address %s" address) (log/debugf "POST /user: Wrong address %s" address)
@ -210,7 +208,8 @@
(db/with-tx (db/with-tx
(when-not (db/user-exists? {:id user-id}) (when-not (db/user-exists? {:id user-id})
(not-found! "No such a user.")) (not-found! "No such a user."))
(db/update! :users body ["id = ?" user-id])) (db/update! :users (to-db-map address is-hidden-in-hunters)
["id = ?" user-id]))
(ok))) (ok)))
@ -248,7 +247,7 @@
:current-user user :current-user user
(let [{:keys [contract-address owner-address]} (issues/get-issue-by-id issue-id)] (let [{:keys [contract-address owner-address]} (issues/get-issue-by-id issue-id)]
(do (log/infof "calling revoke-initiate for %s with %s %s" issue-id contract-address owner-address) (do (log/infof "calling revoke-initiate for %s with %s %s" issue-id contract-address owner-address)
(if-let [{:keys [execute-hash]} (execute-revocation issue-id contract-address owner-address)] (if-let [execute-hash (execute-revocation issue-id contract-address owner-address)]
(ok {:issue-id issue-id (ok {:issue-id issue-id
:execute-hash execute-hash :execute-hash execute-hash
:contract-address contract-address}) :contract-address contract-address})

View File

@ -79,24 +79,14 @@
[] []
(log/info "In self-sign-bounty") (log/info "In self-sign-bounty")
(p :self-sign-bounty (p :self-sign-bounty
(doseq [{:keys [contract-address winner-address issue-id winner-login] :as issue} (doseq [{:keys [contract-address winner-address issue-id] :as issue}
(db-bounties/pending-bounties)] (db-bounties/pending-bounties)]
(try (try
;; TODO(martin) delete this shortly after org-dashboard deploy ;; TODO(martin) delete this shortly after org-dashboard deploy
;; as we're now setting `winner_login` when handling a new claims ;; as we're now setting `winner_login` when handling a new claims
;; coming in via webhooks (see `commiteth.routes.webhooks/handle-claim`) ;; coming in via webhooks (see `commiteth.routes.webhooks/handle-claim`)
;(db-bounties/update-winner-login issue-id winner-login) ;(db-bounties/update-winner-login issue-id winner-login)
(if (empty? winner-address) (bounties/execute-payout issue-id contract-address winner-address)
(do
(log/warn "issue %s: Cannot sign pending bounty - winner has no payout address" issue-id)
(bounties/transition {:issue-id issue-id} :pending-contributor-address))
(let [tx-info (multisig/send-all {:contract contract-address
:payout-address winner-address
:internal-tx-id [:execute issue-id]})]
(log/infof "issue %s: Payout self-signed, called sign-all(%s) tx: %s" issue-id contract-address winner-address (:tx-hash tx-info))
(bounties/transition {:execute-hash (:tx-hash tx-info)
:issue-id issue-id
:tx-info tx-info} :pending-sob-confirmation)))
(catch Throwable ex (catch Throwable ex
(log/error ex "issue %s: self-sign-bounty exception" issue-id))))) (log/error ex "issue %s: self-sign-bounty exception" issue-id)))))
(log/info "Exit self-sign-bounty")) (log/info "Exit self-sign-bounty"))
@ -164,7 +154,8 @@
(defn update-payout-receipt (defn update-payout-receipt
"Gets transaction receipt for each confirmed payout and updates payout_hash" "Gets transaction receipt for each confirmed payout and updates payout_hash"
[{:keys [payout-hash contract-address confirm-hash issue-id updated] :as issue}] [{:keys [payout-hash contract-address confirm-hash issue-id updated] :as bounty}]
{:pre [(util/contains-all-keys bounty db-bounties/payout-receipt-keys)]}
(log/info "In update-payout-receipt") (log/info "In update-payout-receipt")
(p :update-payout-receipt (p :update-payout-receipt
(try (try
@ -183,7 +174,7 @@
(log/infof "issue %s: execute tx: %s" issue-id execute-tx-hash)))) (log/infof "issue %s: execute tx: %s" issue-id execute-tx-hash))))
(do (do
(log/infof "issue %s: Payout has succeeded, payout receipt %s" issue-id receipt) (log/infof "issue %s: Payout has succeeded, payout receipt %s" issue-id receipt)
(bounties/transition (assoc issue :payout-receipt receipt) :paid)))) (bounties/transition (assoc bounty :payout-receipt receipt) :paid))))
(when (older-than-3h? updated) (when (older-than-3h? updated)
(log/warn "issue %s: Resetting payout hash for issue as it has not been mined in 3h" issue-id) (log/warn "issue %s: Resetting payout hash for issue as it has not been mined in 3h" issue-id)
(db-bounties/reset-payout-hash issue-id))) (db-bounties/reset-payout-hash issue-id)))

View File

@ -21,3 +21,8 @@
(defmacro to-db-map [& vars] (defmacro to-db-map [& vars]
(into {} (map #(vector (keyword (str/replace (name %1) "-" "_")) %1) vars))) (into {} (map #(vector (keyword (str/replace (name %1) "-" "_")) %1) vars)))
(defn contains-all-keys [m ks]
{:pre [(map? m) [(vector? ks)]]}
(every?
#(contains? m %) ks))

View File

@ -42,7 +42,7 @@
updated-bounties (get-in context [:effects :db :owner-bounties]) updated-bounties (get-in context [:effects :db :owner-bounties])
confirming-issue-id (get-confirming-issue-id :commiteth pending-revocations)] confirming-issue-id (get-confirming-issue-id :commiteth pending-revocations)]
(when-let [revoking-bounty (get updated-bounties confirming-issue-id)] (when-let [revoking-bounty (get updated-bounties confirming-issue-id)]
(if (:confirm_hash revoking-bounty) (if (:confirm-hash revoking-bounty)
(do (dispatch-confirm-payout revoking-bounty) (do (dispatch-confirm-payout revoking-bounty)
(dispatch-set-pending-revocation revoking-bounty)) (dispatch-set-pending-revocation revoking-bounty))
(println (str "currently revoking " confirming-issue-id " but confirm hash has not been set yet.")))) (println (str "currently revoking " confirming-issue-id " but confirm hash has not been set yet."))))
@ -69,7 +69,7 @@
updated-bounties (get-in context [:effects :db :owner-bounties]) updated-bounties (get-in context [:effects :db :owner-bounties])
confirming-issue-id (get-confirming-issue-id :owner pending-revocations)] confirming-issue-id (get-confirming-issue-id :owner pending-revocations)]
(when-let [revoking-bounty (get updated-bounties confirming-issue-id)] (when-let [revoking-bounty (get updated-bounties confirming-issue-id)]
(if (:payout_receipt revoking-bounty) (if (:payout-receipt revoking-bounty)
(dispatch-remove-pending-revocation revoking-bounty) (dispatch-remove-pending-revocation revoking-bounty)
(println (str "currently revoking " confirming-issue-id " but payout receipt has not been set yet.")))) (println (str "currently revoking " confirming-issue-id " but payout receipt has not been set yet."))))
;; interceptor must return context ;; interceptor must return context

View File

@ -10,7 +10,7 @@
(let [db (rf/subscribe [:db]) (let [db (rf/subscribe [:db])
updating-user (rf/subscribe [:get-in [:updating-user]]) updating-user (rf/subscribe [:get-in [:updating-user]])
address (r/atom @(rf/subscribe [:get-in [:user :address]])) address (r/atom @(rf/subscribe [:get-in [:user :address]]))
hidden (r/atom @(rf/subscribe [:get-in [:user :is_hidden_in_hunters]]))] hidden (r/atom @(rf/subscribe [:get-in [:user :is-hidden-in-hunters]]))]
(fn [] (fn []
(let [web3 (:web3 @db) (let [web3 (:web3 @db)
@ -64,7 +64,7 @@
[:button [:button
(merge {:on-click (merge {:on-click
#(rf/dispatch [:save-user-fields {:address @address #(rf/dispatch [:save-user-fields {:address @address
:is_hidden_in_hunters @hidden}]) :is-hidden-in-hunters @hidden}])
:disabled (str/blank? @address) :disabled (str/blank? @address)
:class (str "ui button small update-address-button" :class (str "ui button small update-address-button"
(when @updating-user (when @updating-user

View File

@ -36,5 +36,5 @@
:address "address" :address "address"
:created nil :created nil
:welcome_email_sent 0 :welcome_email_sent 0
:is_hidden_in_hunters false} :is-hidden-in-hunters false}
(db/get-user t-conn {:id 1}))))) (db/get-user t-conn {:id 1})))))