{"action":"created","issue":{"url":"https://api.github.com/repos/omisego/ewallet/issues/224","repository_url":"https://api.github.com/repos/omisego/ewallet","labels_url":"https://api.github.com/repos/omisego/ewallet/issues/224/labels{/name}","comments_url":"https://api.github.com/repos/omisego/ewallet/issues/224/comments","events_url":"https://api.github.com/repos/omisego/ewallet/issues/224/events","html_url":"https://github.com/omisego/ewallet/issues/224","id":329370979,"node_id":"MDU6SXNzdWUzMjkzNzA5Nzk=","number":224,"title":"Randomly Failing to retrieve a transfer after insert","user":{"login":"T-Dnzt","id":2647689,"node_id":"MDQ6VXNlcjI2NDc2ODk=","avatar_url":"https://avatars2.githubusercontent.com/u/2647689?v=4","gravatar_id":"","url":"https://api.github.com/users/T-Dnzt","html_url":"https://github.com/T-Dnzt","followers_url":"https://api.github.com/users/T-Dnzt/followers","following_url":"https://api.github.com/users/T-Dnzt/following{/other_user}","gists_url":"https://api.github.com/users/T-Dnzt/gists{/gist_id}","starred_url":"https://api.github.com/users/T-Dnzt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/T-Dnzt/subscriptions","organizations_url":"https://api.github.com/users/T-Dnzt/orgs","repos_url":"https://api.github.com/users/T-Dnzt/repos","events_url":"https://api.github.com/users/T-Dnzt/events{/privacy}","received_events_url":"https://api.github.com/users/T-Dnzt/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-06-05T09:22:05Z","updated_at":"2018-06-05T09:58:21Z","closed_at":null,"author_association":"MEMBER","body":"# Subject of the issue\r\n\r\nRandomly Failing to retrieve a transfer after insert in:\r\n\r\n```elixir\r\n  def insert(attrs) do\r\n    changeset = changeset(%Transfer{}, attrs)\r\n    opts = [on_conflict: :nothing, conflict_target: :idempotency_token]\r\n\r\n    case Repo.insert(changeset, opts) do\r\n      {:ok, transfer} ->\r\n        {:ok, get_by_idempotency_token(transfer.idempotency_token)}\r\n\r\n      changeset ->\r\n        changeset\r\n    end\r\n  end\r\n```\r\n\r\n# Your environment\r\n\r\nStaging environment, using Ruby SDK or iOS SDK.\r\n\r\n# Steps to reproduce\r\n\r\n\r\n1. Get Ruby SDK\r\n2. Replace the config in `env.rb` with real values\r\n3. Run \r\n\r\n```\r\n~/src/ruby-sdk on  T305-renaming! ⌚ 16:05:55\r\n$ rspec ./spec/omisego/wallet_spec.rb                                                                                                                                                                                            ‹ruby-2.3.3›\r\n\r\nOmiseGO::Wallet\r\n  .all\r\n    retrieves the list of wallets\r\n  .credit\r\n    with valid params\r\n      credits the user's wallet\r\n    with params account_id and account_address\r\n      credits the user's wallet (FAILED - 1)\r\n  .debit\r\n    with valid params\r\n      debits the user's wallet (FAILED - 2)\r\n    with params account_id and account_address\r\n      debit/s the user's wallet\r\n\r\nFailures:\r\n\r\n  1) OmiseGO::Wallet.credit with params account_id and account_address credits the user's wallet\r\n     Failure/Error: expect(wallets).to be_kind_of OmiseGO::List\r\n       expected #<OmiseGO::Error:70175132363600 code: server:internal_server_error, description: Something went wrong on the server, messages: > to be a kind of OmiseGO::List\r\n     # ./spec/omisego/wallet_spec.rb:67:in `block (5 levels) in <module:OmiseGO>'\r\n     # /Users/thibault/.rvm/gems/ruby-2.3.3/gems/vcr-3.0.3/lib/vcr/util/variable_args_block_caller.rb:9:in `call_block'\r\n     # /Users/thibault/.rvm/gems/ruby-2.3.3/gems/vcr-3.0.3/lib/vcr.rb:189:in `use_cassette'\r\n     # ./spec/omisego/wallet_spec.rb:55:in `block (4 levels) in <module:OmiseGO>'\r\n\r\n  2) OmiseGO::Wallet.debit with valid params debits the user's wallet\r\n     Failure/Error: expect(wallets).to be_kind_of OmiseGO::List\r\n       expected #<OmiseGO::Error:70175123941460 code: server:internal_server_error, description: Something went wrong on the server, messages: > to be a kind of OmiseGO::List\r\n     # ./spec/omisego/wallet_spec.rb:87:in `block (5 levels) in <module:OmiseGO>'\r\n     # /Users/thibault/.rvm/gems/ruby-2.3.3/gems/vcr-3.0.3/lib/vcr/util/variable_args_block_caller.rb:9:in `call_block'\r\n     # /Users/thibault/.rvm/gems/ruby-2.3.3/gems/vcr-3.0.3/lib/vcr.rb:189:in `use_cassette'\r\n     # ./spec/omisego/wallet_spec.rb:76:in `block (4 levels) in <module:OmiseGO>'\r\n\r\nFinished in 3.04 seconds (files took 0.46432 seconds to load)\r\n5 examples, 2 failures\r\n\r\nFailed examples:\r\n\r\nrspec ./spec/omisego/wallet_spec.rb:54 # OmiseGO::Wallet.credit with params account_id and account_address credits the user's wallet\r\nrspec ./spec/omisego/wallet_spec.rb:75 # OmiseGO::Wallet.debit with valid params debits the user's wallet\r\n```\r\n\r\n# Expected behavior\r\n\r\nThe response should be successful instead of `500`. \r\n\r\n# Actual behavior\r\n\r\nThe server returns `500`.\r\n\r\n# Research\r\n\r\n\r\nThe server crashes because `nil` is passed as the first argument in those functions:\r\n\r\n```elixir\r\n  defp process_with_transfer(%Transfer{status: \"pending\"} = transfer, wallets, token) do\r\n    transfer\r\n    |> TransferGate.process()\r\n    |> process_with_transfer(wallets, token)\r\n  end\r\n\r\n  defp process_with_transfer(%Transfer{status: \"confirmed\"} = transfer, wallets, token) do\r\n    {:ok, transfer, wallets, token}\r\n  end\r\n\r\n  defp process_with_transfer(%Transfer{status: \"failed\"} = transfer, _wallets, _token) do\r\n    resp = transfer.ledger_response\r\n    {:error, transfer, resp[\"code\"], resp[\"description\"]}\r\n  end\r\n```\r\n\r\nWhich happens because of `get_by_idempotency_token` is returning `nil` in the following code, which is not supposed to happen becasue either a record has just been inserted or has been inserted in another process.\r\n\r\n```elixir\r\n  def insert(attrs) do\r\n    changeset = changeset(%Transfer{}, attrs)\r\n    opts = [on_conflict: :nothing, conflict_target: :idempotency_token]\r\n\r\n    case Repo.insert(changeset, opts) do\r\n      {:ok, transfer} ->\r\n        {:ok, get_by_idempotency_token(transfer.idempotency_token)}\r\n\r\n      changeset ->\r\n        changeset\r\n    end\r\n  end\r\n```\r\n\r\n# Attempts\r\n\r\n- Surround with Transaction PR #219 \r\n- Retries #222 \r\n\r\nThoughts? I ran out of ideas except for the retries with delay to solve the fact that the DB state does not seem up to date.\r\n"},"comment":{"url":"https://api.github.com/repos/omisego/ewallet/issues/comments/394652522","html_url":"https://github.com/omisego/ewallet/issues/224#issuecomment-394652522","issue_url":"https://api.github.com/repos/omisego/ewallet/issues/224","id":394652522,"node_id":"MDEyOklzc3VlQ29tbWVudDM5NDY1MjUyMg==","user":{"login":"T-Dnzt","id":2647689,"node_id":"MDQ6VXNlcjI2NDc2ODk=","avatar_url":"https://avatars2.githubusercontent.com/u/2647689?v=4","gravatar_id":"","url":"https://api.github.com/users/T-Dnzt","html_url":"https://github.com/T-Dnzt","followers_url":"https://api.github.com/users/T-Dnzt/followers","following_url":"https://api.github.com/users/T-Dnzt/following{/other_user}","gists_url":"https://api.github.com/users/T-Dnzt/gists{/gist_id}","starred_url":"https://api.github.com/users/T-Dnzt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/T-Dnzt/subscriptions","organizations_url":"https://api.github.com/users/T-Dnzt/orgs","repos_url":"https://api.github.com/users/T-Dnzt/repos","events_url":"https://api.github.com/users/T-Dnzt/events{/privacy}","received_events_url":"https://api.github.com/users/T-Dnzt/received_events","type":"User","site_admin":false},"created_at":"2018-06-05T09:58:21Z","updated_at":"2018-06-05T09:58:21Z","author_association":"MEMBER","body":"However, with this error, it's now possible to send the transaction again (with same idempotency token) and the second request always work as expected (since it's getting the record from the DB correctly)."}}
{"actor":{"display_login":"T-Dnzt"}}