├── .formatter.exs ├── .github └── workflows │ └── elixir.yml ├── .gitignore ├── LICENSE ├── README.md ├── config └── config.exs ├── lib ├── mix │ └── tasks │ │ └── bitbox.gen.migrations.ex ├── txbox.ex └── txbox │ ├── mapi │ ├── processor.ex │ └── queue.ex │ ├── transactions.ex │ └── transactions │ ├── mapi_response.ex │ ├── meta.ex │ └── tx.ex ├── media ├── poster.png └── state-machine.png ├── mix.exs ├── mix.lock ├── priv └── templates │ └── migrations │ ├── 1_setup_txbox.exs.eex │ └── 2_create_txbox_mapi_responses.exs.eex └── test ├── mix └── tasks │ └── txbox.gen.migrations_test.exs ├── mocks ├── mapi-push-failure.json ├── mapi-push-success.json ├── mapi-status-confirmed.json ├── mapi-status-mempool.json └── mapi-status-notfound.json ├── support ├── case_template.ex ├── file_helpers.ex └── repo.ex ├── test_helper.exs ├── txbox ├── mapi │ ├── processor_test.exs │ └── queue_test.exs └── transactions_test.exs └── txbox_test.exs /.formatter.exs: -------------------------------------------------------------------------------- 1 | # Used by "mix format" 2 | [ 3 | inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] 4 | ] 5 | -------------------------------------------------------------------------------- /.github/workflows/elixir.yml: -------------------------------------------------------------------------------- 1 | name: Elixir CI 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | test: 11 | 12 | runs-on: ubuntu-latest 13 | name: OTP ${{matrix.otp}} / Elixir ${{matrix.elixir}} 14 | 15 | services: 16 | db: 17 | image: postgres:11 18 | ports: ['5432:5432'] 19 | env: 20 | POSTGRES_PASSWORD: postgres 21 | options: >- 22 | --health-cmd pg_isready 23 | --health-interval 10s 24 | --health-timeout 5s 25 | --health-retries 5 26 | 27 | strategy: 28 | matrix: 29 | otp: ['22.2', '23.3', '24.0'] 30 | elixir: ['1.12.3'] 31 | steps: 32 | - uses: actions/checkout@v2 33 | - uses: erlef/setup-beam@v1 34 | with: 35 | otp-version: ${{matrix.otp}} 36 | elixir-version: ${{matrix.elixir}} 37 | - run: mix deps.get 38 | - run: mix test -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | # The directory Mix will write compiled artifacts to. 3 | /_build/ 4 | 5 | # If you run "mix test --cover", coverage assets end up here. 6 | /cover/ 7 | 8 | # The directory Mix downloads your dependencies sources to. 9 | /deps/ 10 | 11 | # Where third-party dependencies like ExDoc output generated docs. 12 | /doc/ 13 | 14 | # Ignore .fetch files in case you like to edit your project deps locally. 15 | /.fetch 16 | 17 | # If the VM crashes, it generates a dump, let's ignore it too. 18 | erl_crash.dump 19 | 20 | # Also ignore archive artifacts (built via "mix archive.build"). 21 | *.ez 22 | 23 | # Ignore package tarball (built via "mix hex.build"). 24 | txbox-*.tar 25 | 26 | # Dev tmp dir 27 | /priv/tmp -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Txbox 2 | 3 | ![Elixir Bitcoin Tx storage schema](https://github.com/libitx/txbox/raw/master/media/poster.png) 4 | 5 | ![Hex.pm](https://img.shields.io/hexpm/v/txbox?color=informational) 6 | ![License](https://img.shields.io/github/license/libitx/txbox?color=informational) 7 | ![Build Status](https://img.shields.io/github/workflow/status/libitx/txbox/Elixir%20CI) 8 | 9 | Txbox is a Bitcoin transaction storage schema. It lets you store Bitcoin transactions in your application's database with searchable and filterable semantic metadata. Txbox is inspired by [TXT](https://txt.network/) but adapted to slot into an Elixir developers toolset. 10 | 11 | * Built on Ecto! Store Bitcoin Transactions in your database and define associations with any other data from your app's domain. 12 | * Built in queue for pushing signed transactions to the Bitcoin network via the [Miner API](https://github.com/bitcoin-sv/merchantapi-reference). 13 | * Auto-syncs with the [Miner API](https://github.com/bitcoin-sv/merchantapi-reference) of your choice, and caches signed responses. 14 | * Aims to be compatible with TXT, with similar schema design and API for searching and filtering. 15 | * Unlike TXT, no web UI or HTTP API is exposed. Txbox is purely a database schema with query functions - the rest is up to you. 16 | * Coming soon (™) - Seamlessly import and export from other TXT-compatible platforms. 17 | 18 | ## Installation 19 | 20 | The package can be installed by adding `txbox` to your list of dependencies in `mix.exs`. 21 | 22 | 23 | ```elixir 24 | def deps do 25 | [ 26 | {:txbox, "~> 0.3"} 27 | ] 28 | end 29 | ``` 30 | 31 | Once installed, update your application's configuration, making sure Txbox knows which Repo to use. 32 | 33 | ```elixir 34 | # config/config.exs 35 | config :txbox, repo: MyApp.Repo 36 | ``` 37 | 38 | Next, `Txbox` to your application's supervision tree. 39 | 40 | ```elixir 41 | children = [ 42 | {Txbox, [ 43 | # Manic miner configuration (defaults to :taal) 44 | miner: {:taal, headers: [{"token", "MYTOKEN"}]}, 45 | # Maximum number of times to attempt polling the miner (default is 20) 46 | max_status_attempts: 20, 47 | # Interval (in seconds) between each mAPI request (default is 300 - 5 minutes) 48 | retry_status_after: 300 49 | ]} 50 | ] 51 | 52 | Supervisor.start_link(children, strategy: :one_for_one) 53 | ``` 54 | 55 | Finally, run the following tasks to generate and run the required database migrations. 56 | 57 | ```console 58 | mix txbox.gen.migrations 59 | mix ecto.migrate 60 | ``` 61 | 62 | ## Upgrading 63 | 64 | If upgrading from a previous version of `txbox`, make sure to run the migrations 65 | task to check if any new migrations are required. 66 | 67 | ```console 68 | mix txbox.gen.migrations 69 | # If needed 70 | mix ecto.migrate 71 | ``` 72 | 73 | ## Usage 74 | 75 | For detailed examples, refer to the [full documentation](https://hexdocs.pm/txbox). 76 | 77 | Once up an running, using Txbox is simple. The `Txbox` modules provides four CRUD-like functions for managing transactions: `create/2`, `update/2`, `find/2` and `all/2`. 78 | 79 | To add a transaction to Txbox, the minimum required is to give a `txid`. 80 | 81 | ```elixir 82 | Txbox.create(%{ 83 | txid: "6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110" 84 | }) 85 | ``` 86 | 87 | Once a transaction is added, Txbox automatically syncs with the Miner API of your choice, updating the transaction's status until it is confirmed in a block. 88 | 89 | When a channel name is ommitted, transactions are added to the `default_channel/0` (`"txbox"`), but by specifiying a channel name as the first argument, the transaction will be added to that channel. You can provide additional metadata about the transaction, as well as attach the raw transaction binary. 90 | 91 | ```elixir 92 | Txbox.create("photos", %{ 93 | txid: "6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110", 94 | rawtx: <<...>>, 95 | tags: ["hubble", "universe"], 96 | meta: %{ 97 | title: "Hubble Ultra-Deep Field" 98 | }, 99 | data: %{ 100 | bitfs: "https://x.bitfs.network/6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110.out.0.3" 101 | } 102 | }) 103 | ``` 104 | 105 | The transaction can be retrieved by the `txid`. 106 | 107 | ```elixir 108 | Txbox.find("6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110") 109 | ``` 110 | 111 | As before, omitting the channel scopes the query to the `default_channel/0` (`"txbox"`). Alterntively you can pass the channel name as the first argument, or use `"_"` which is the TXT syntax for global scope. 112 | 113 | ```elixir 114 | Txbox.find("_", "6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110") 115 | ``` 116 | 117 | A list of transactions can be returned using `all/2`. The second parameter must be a `t:map/0` of query parameters to filter and search by. 118 | 119 | ```elixir 120 | Txbox.all("photos", %{ 121 | from: 636400, 122 | tagged: "hubble", 123 | limit: 5 124 | }) 125 | ``` 126 | 127 | A full text search can be made by using the `:search` filter parameter. 128 | 129 | ```elixir 130 | Txbox.all("_", %{ 131 | search: "hubble deep field" 132 | }) 133 | ``` 134 | 135 | ### Filtering and searching 136 | 137 | Txbox adopts the same syntax and query modifiers [used by TXT](https://txt.network/#/?id=c-queries). Txbox automatically normalizes the query map, so keys can be specifiied either as atoms or strings. Here are a few examples: 138 | 139 | * `:search` - Full text search made on trasactions' tags and meta data 140 | * `%{search: "hubble deep field"}` 141 | * `:tagged` - Filter transactions by the given tag or tags 142 | * `%{tagged: "photos"}` - all transactions tagged with "photos" 143 | * `%{tagged: ["space", "hubble"]}` - all transactions tagged with *both* "space" and "hubble" 144 | * `%{tagged: "space, hubble"}` - as above, but given as a comma seperated string 145 | * `:from` - The block height from which to filter transactions by 146 | * `%{from: 636400}` - all transactions from and including block 636400 147 | * `:to` - The block height to which to filter transactions by 148 | * `%{to: 636800}` - all transactions upto and including block 636800 149 | * `%{from: 636400, to: 636800}` - all transactions in the range 636400 to 636800 150 | * `:at` - The block height at which to filter transactions by exactly 151 | * `%{at: 636500}` - all transactions at block 636500 152 | * `%{at: "null"}` - all transactions without a block height (unconfirmed) 153 | * `%{at: "!null"}` - all transactions with any block height (confirmed) 154 | * `:order` - The attribute to sort transactions by 155 | * `%{order: "i"}` - sort by block height in ascending order 156 | * `%{order: "-i"}` - sort by block height in descending order 157 | * `%{order: "created_at"}` - sort by insertion time in ascending order 158 | * `%{order: "-created_at"}` - sort by insertion time in descending order 159 | * `:limit` - The maximum number of transactions to return 160 | * `:offset` - The start offset from which to return transactions (for pagination) 161 | 162 | 163 | ## Transaction state machine and miner API integration 164 | 165 | Under the hood, Txbox is packed with a powerful state machine with automatic miner API integration. 166 | 167 | ![Txbox state machine](https://github.com/libitx/txbox/raw/master/media/state-machine.png) 168 | 169 | When creating a new transaction, you can set its state to one of the following values. 170 | 171 | * `"pending"` - If no state is specified, the default state is `"pending"`. Pending transactions can be considered draft or incomplete transactions. Draft transactions can be updated, and will not be pushed to miners unless the state changes. 172 | * `"queued"` - Under the `"queued"` state, a transaction will be asynchronously pushed to the configured miner API in the background. Depending on the miner response, the state will transition to `"pushed"` or `"failed"`. 173 | * `"pushed"` - If the state is specified as `"pushed"`, this tells Txbox the transaction is already accepted by miners. In the background, Txbox will poll the configured miner API until a response confirms the transaction is in a block. 174 | 175 | The miner API queue and processing occurs automatically in a background process, run under your application's supervision tree. For details refer to `Txbox.Mapi.Queue` and `Txbox.Mapi.Processor`. 176 | 177 | Each historic miner API response is saved associated to the transaction. The most recent response is always preloaded with the transaction. This allows you to inspect any messages or errors given by miners. 178 | 179 | ```elixir 180 | iex> {:ok, tx} = Txbox.find("6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110") 181 | iex> tx.status 182 | %Txbox.Transactions.MapiResponse{ 183 | type: "push", 184 | payload: %{ 185 | "return_result" => "failure", 186 | "return_description" => "Not enough fees", 187 | ... 188 | }, 189 | public_key: "03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270", 190 | signature: "3045022100c8e7f9369545b89c978afc13cc19fc6dd6e1cd139d363a6b808141e2c9fccd2e02202e12f4bf91d10bf7a45191e6fe77f50d7b5351dae7e0613fecc42f61a5736af8", 191 | verified: true 192 | } 193 | ``` 194 | 195 | For more examples, refer to the [full documentation](https://hexdocs.pm/txbox). 196 | 197 | ## License 198 | 199 | Txbox is open source and released under the [Apache-2 License](https://github.com/libitx/manic/blob/master/LICENSE.md). 200 | 201 | © Copyright 2020 libitx. -------------------------------------------------------------------------------- /config/config.exs: -------------------------------------------------------------------------------- 1 | use Mix.Config 2 | 3 | config :txbox, 4 | repo: Txbox.Test.Repo, 5 | ecto_repos: [Txbox.Test.Repo] 6 | 7 | config :txbox, Txbox.Test.Repo, 8 | database: "txbox_test", 9 | pool: Ecto.Adapters.SQL.Sandbox, 10 | priv: "priv/tmp/txbox_test" 11 | 12 | config :logger, level: :warn 13 | 14 | config :tesla, adapter: Tesla.Mock 15 | 16 | 17 | # Configure the database for GitHub Actions 18 | if System.get_env("GITHUB_ACTIONS") do 19 | config :txbox, Txbox.Test.Repo, 20 | username: "postgres", 21 | password: "postgres" 22 | end 23 | -------------------------------------------------------------------------------- /lib/mix/tasks/bitbox.gen.migrations.ex: -------------------------------------------------------------------------------- 1 | defmodule Mix.Tasks.Txbox.Gen.Migrations do 2 | @shortdoc "Generates Txbox's migrations" 3 | 4 | @moduledoc """ 5 | Generates the required Txbox database migrations. 6 | """ 7 | use Mix.Task 8 | import Mix.Generator 9 | 10 | @doc false 11 | def run(args) do 12 | Mix.Ecto.no_umbrella!("ecto.gen.migration") 13 | 14 | repos = Mix.Ecto.parse_repo(args) 15 | 16 | Enum.each(repos, fn repo -> 17 | Mix.Ecto.ensure_repo(repo, args) 18 | path = Ecto.Migrator.migrations_path(repo) 19 | create_directory(path) 20 | 21 | :txbox 22 | |> Application.app_dir() 23 | |> Path.join("priv/templates/migrations/*.exs.eex") 24 | |> Path.wildcard 25 | |> Enum.filter(& target_nonexistent?(&1, path)) 26 | |> Enum.with_index 27 | |> Enum.each(fn {m, n} -> copy_migration(m, path, n) end) 28 | end) 29 | end 30 | 31 | defp target_nonexistent?(source_path, target_path) do 32 | basename = source_path 33 | |> Path.basename(".exs.eex") 34 | |> String.replace(~r/^\d_/, "") 35 | 36 | target_path 37 | |> Path.join("*_#{basename}.exs") 38 | |> Path.wildcard 39 | |> Enum.empty? 40 | end 41 | 42 | defp copy_migration(source_path, target_path, n) do 43 | basename = source_path 44 | |> Path.basename(".exs.eex") 45 | |> String.replace(~r/^\d_/, "") 46 | 47 | target_file = Path.join(target_path, "#{timestamp(n)}_#{basename}.exs") 48 | generated_file = EEx.eval_file(source_path, module_prefix: app_module()) 49 | create_file(target_file, generated_file) 50 | end 51 | 52 | defp app_module do 53 | Mix.Project.config() 54 | |> Keyword.fetch!(:app) 55 | |> to_string() 56 | |> Macro.camelize() 57 | end 58 | 59 | defp timestamp(n) do 60 | {{y, m, d}, {hh, mm, ss}} = :calendar.universal_time() 61 | "#{y}#{pad(m)}#{pad(d)}#{pad(hh)}#{pad(mm)}#{pad(ss + n)}" 62 | end 63 | 64 | defp pad(i) when i < 10, do: <> 65 | defp pad(i), do: to_string(i) 66 | end 67 | -------------------------------------------------------------------------------- /lib/txbox.ex: -------------------------------------------------------------------------------- 1 | defmodule Txbox do 2 | @moduledoc """ 3 | ![Elixir Bitcoin Tx storage schema](https://github.com/libitx/txbox/raw/master/media/poster.png) 4 | 5 | ![License](https://img.shields.io/github/license/libitx/txbox?color=informational) 6 | 7 | Txbox is a Bitcoin transaction storage schema. It lets you store Bitcoin 8 | transactions in your application's database with searchable and filterable 9 | semantic metadata. Txbox is inspired by [TXT](https://txt.network/) but 10 | adapted to slot into an Elixir developers toolset. 11 | 12 | * Built on Ecto! Store Bitcoin Transactions in your database and define associations with any other data from your app's domain. 13 | * Built in queue for pushing signed transactions to the Bitcoin network via the [Miner API](https://github.com/bitcoin-sv/merchantapi-reference). 14 | * Auto-syncs with the [Miner API](https://github.com/bitcoin-sv/merchantapi-reference) of your choice, and caches signed responses. 15 | * Aims to be compatible with TXT, with similar schema design and API for searching and filtering. 16 | * Unlike TXT, no web UI or HTTP API is exposed. Txbox is purely a database schema with query functions - the rest is up to you. 17 | * Coming soon (™) - Seamlessly import and export from other TXT-compatible platforms. 18 | 19 | ## Installation 20 | 21 | The package can be installed by adding `txbox` to your list of dependencies in 22 | `mix.exs`. 23 | 24 | def deps do 25 | [ 26 | {:txbox, "~> 0.3"} 27 | ] 28 | end 29 | 30 | Once installed, update your application's configuration, making sure Txbox 31 | knows which Repo to use. 32 | 33 | ```elixir 34 | # config/config.exs 35 | config :txbox, repo: MyApp.Repo 36 | ``` 37 | 38 | Next, add `Txbox` to your application's supervision tree. 39 | 40 | children = [ 41 | {Txbox, [ 42 | # Manic miner configuration (defaults to :taal) 43 | miner: {:taal, headers: [{"token", "MYTOKEN"}]}, 44 | # Maximum number of times to attempt polling the miner (default is 20) 45 | max_status_attempts: 20, 46 | # Interval (in seconds) between each mAPI request (default is 300 - 5 minutes) 47 | retry_status_after: 300 48 | ]} 49 | ] 50 | 51 | Supervisor.start_link(children, strategy: :one_for_one) 52 | 53 | Finally, run the following tasks to generate and run the required database 54 | migrations. 55 | 56 | ```console 57 | mix txbox.gen.migrations 58 | mix ecto.migrate 59 | ``` 60 | 61 | ## Upgrading 62 | 63 | If upgrading from a previous version of `txbox`, make sure to run the migrations 64 | task to check if any new migrations are required. 65 | 66 | ```console 67 | mix txbox.gen.migrations 68 | # If needed 69 | mix ecto.migrate 70 | ``` 71 | 72 | ## Usage 73 | 74 | Once up an running, using Txbox is simple. The `Txbox` modules provides four 75 | CRUD-like functions for managing transactions: `create/2`, `update/2`, 76 | `find/2` and `all/2`. 77 | 78 | To add a transaction to Txbox, the minimum required is to give a `txid`. 79 | 80 | iex> Txbox.create(%{ 81 | ...> txid: "6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110" 82 | ...> }) 83 | {:ok, %Tx{}} 84 | 85 | When a channel name is ommitted, transactions are added to the `default_channel/0` 86 | (`"txbox"`), but by specifiying a channel name as the first argument, the 87 | transaction will be added to that channel. You can provide additional metadata 88 | about the transaction, as well as attach the raw transaction binary. 89 | 90 | iex> Txbox.create("photos", %{ 91 | ...> txid: "6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110", 92 | ...> rawtx: <<...>>, 93 | ...> tags: ["hubble", "universe"], 94 | ...> meta: %{ 95 | ...> title: "Hubble Ultra-Deep Field" 96 | ...> }, 97 | ...> data: %{ 98 | ...> bitfs: "https://x.bitfs.network/6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110.out.0.3" 99 | ...> } 100 | ...> }) 101 | {:ok, %Tx{}} 102 | 103 | The transaction can be retrieved by the `txid`. 104 | 105 | iex> Txbox.find("6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110") 106 | {:ok, %Tx{}} 107 | 108 | As before, omitting the channel scopes the query to the `default_channel/0` 109 | (`"txbox"`). Alterntively you can pass the channel name as the first argument, 110 | or use `"_"` which is the TXT syntax for global scope. 111 | 112 | iex> Txbox.find("_", "6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110") 113 | {:ok, %Tx{}} 114 | 115 | A list of transactions can be returned using `all/2`. The second parameter 116 | must be a `t:map/0` of query parameters to filter and search by. 117 | 118 | iex> Txbox.all("photos", %{ 119 | ...> from: 636400, 120 | ...> tagged: "hubble", 121 | ...> limit: 5 122 | ...> }) 123 | {:ok, [%Tx{}, ...]} 124 | 125 | A full text search can be made by using the `:search` filter parameter. 126 | 127 | iex> Txbox.all("_", %{ 128 | ...> search: "hubble deep field" 129 | ...> }) 130 | {:ok, [%Tx{}, ...]} 131 | 132 | ### Filtering and searching 133 | 134 | Txbox adopts the same syntax and query modifiers [used by TXT](https://txt.network/#/?id=c-queries). 135 | Txbox automatically normalizes the query map, so keys can be specifiied either 136 | as atoms or strings. Here are a few examples: 137 | 138 | * `:search` - Full text search made on trasactions' tags and meta data 139 | * `%{search: "hubble deep field"}` 140 | * `:tagged` - Filter transactions by the given tag or tags 141 | * `%{tagged: "photos"}` - all transactions tagged with "photos" 142 | * `%{tagged: ["space", "hubble"]}` - all transactions tagged with *both* "space" and "hubble" 143 | * `%{tagged: "space, hubble"}` - as above, but given as a comma seperated string 144 | * `:from` - The block height from which to filter transactions by 145 | * `%{from: 636400}` - all transactions from and including block 636400 146 | * `:to` - The block height to which to filter transactions by 147 | * `%{to: 636800}` - all transactions upto and including block 636800 148 | * `%{from: 636400, to: 636800}` - all transactions in the range 636400 to 636800 149 | * `:at` - The block height at which to filter transactions by exactly 150 | * `%{at: 636500}` - all transactions at block 636500 151 | * `%{at: "null"}` - all transactions without a block height (unconfirmed) 152 | * `%{at: "!null"}` - all transactions with any block height (confirmed) 153 | * `:order` - The attribute to sort transactions by 154 | * `%{order: "i"}` - sort by block height in ascending order 155 | * `%{order: "-i"}` - sort by block height in descending order 156 | * `%{order: "created_at"}` - sort by insertion time in ascending order 157 | * `%{order: "-created_at"}` - sort by insertion time in descending order 158 | * `:limit` - The maximum number of transactions to return 159 | * `:offset` - The start offset from which to return transactions (for pagination) 160 | 161 | ## Transaction state machine and miner API integration 162 | 163 | Under the hood, Txbox is packed with a powerful state machine with automatic 164 | miner API integration. 165 | 166 | ![Txbox state machine](https://github.com/libitx/txbox/raw/master/media/state-machine.png) 167 | 168 | When creating a new transaction, you can set its state to one of the 169 | following values. 170 | 171 | * `"pending"` - If no state is specified, the default state is `"pending"`. 172 | Pending transactions can be considered draft or incomplete transactions. Draft 173 | transactions can be updated, and will not be pushed to miners unless the state 174 | changes. 175 | * `"queued"` - Under the `"queued"` state, a transaction will be asynchronously 176 | pushed to the configured miner API in the background. Depending on the miner 177 | response, the state will transition to `"pushed"` or `"failed"`. 178 | * `"pushed"` - If the state is specified as `"pushed"`, this tells Txbox the 179 | transaction is already accepted by miners. In the background, Txbox will poll 180 | the configured miner API until a response confirms the transaction is in a 181 | block. 182 | 183 | The miner API queue and processing occurs automatically in a background 184 | process, run under your application's supervision tree. For details refer to 185 | `Txbox.Mapi.Queue` and `Txbox.Mapi.Processor`. 186 | 187 | Each historic miner API response is saved associated to the transaction. The 188 | most recent response is always preloaded with the transaction. This allows 189 | you to inspect any messages or errors given by miners. 190 | 191 | iex> {:ok, tx} = Txbox.find("6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110") 192 | iex> tx.status 193 | %Txbox.Transactions.MapiResponse{ 194 | type: "push", 195 | payload: %{ 196 | "return_result" => "failure", 197 | "return_description" => "Not enough fees", 198 | ... 199 | }, 200 | public_key: "03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270", 201 | signature: "3045022100c8e7f9369545b89c978afc13cc19fc6dd6e1cd139d363a6b808141e2c9fccd2e02202e12f4bf91d10bf7a45191e6fe77f50d7b5351dae7e0613fecc42f61a5736af8", 202 | verified: true 203 | } 204 | """ 205 | @doc false 206 | use Supervisor 207 | alias Txbox.Transactions 208 | alias Txbox.Transactions.Tx 209 | 210 | 211 | @default_channel "txbox" 212 | 213 | 214 | @doc """ 215 | Returns the default channel (`"txbox"`). 216 | """ 217 | @spec default_channel() :: String.t 218 | def default_channel(), do: @default_channel 219 | 220 | 221 | @doc """ 222 | Starts the Txbox process linked to the current process. 223 | """ 224 | @spec start_link(keyword) :: Supervisor.on_start 225 | def start_link(options) do 226 | Supervisor.start_link(__MODULE__, options, name: __MODULE__) 227 | end 228 | 229 | 230 | @impl true 231 | def init(opts) do 232 | children = [ 233 | {Txbox.Mapi.Queue, opts}, 234 | {Txbox.Mapi.Processor, opts} 235 | ] 236 | 237 | Supervisor.init(children, strategy: :one_for_one) 238 | end 239 | 240 | 241 | @doc false 242 | def all(query \\ %{}) 243 | def all(query) when is_map(query), do: all(@default_channel, query) 244 | def all(channel) when is_binary(channel), do: all(channel, %{}) 245 | 246 | @doc """ 247 | Finds a list of transactions, scoped by the specified channel and/or filtered 248 | by the map of query options. 249 | 250 | If the channel is omitted, it defaults to `default_channel/0`. Alernatively, 251 | the channel can be specified as `"_"` which is the TXT syntax for the global 252 | scope. 253 | 254 | ## Query options 255 | 256 | The accepted query options are: (keys can be atoms or strings) 257 | 258 | * `:search` - Full text search made on trasactions' tags and meta data 259 | * `:tagged` - Filter transactions by the given tag or tags 260 | * `:from` - The block height from which to filter transactions by 261 | * `:to` - The block height to which to filter transactions by 262 | * `:at` - The block height at which to filter transactions by exactly 263 | * `:order` - The attribute to sort transactions by 264 | * `:limit` - The maximum number of transactions to return 265 | * `:offset` - The start offset from which to return transactions (for pagination) 266 | * `:rawtx` - Include the full rawtx in the query response (defaults false) 267 | 268 | ## Examples 269 | 270 | Find all transactions from the specified block height in the default channel (`"txbox"`) 271 | 272 | iex> Txbox.all(%{from: 636400}) 273 | {:ok, [%Tx{}, ...]} 274 | 275 | Find all transactions in the specified channel with a combination of filters 276 | 277 | iex> Txbox.all("photos", %{from: 636400, tagged: "hubble", limit: 5}) 278 | {:ok, [%Tx{}, ...]} 279 | 280 | Find all transactions in any channel unfiltered 281 | 282 | iex> Txbox.all("_") 283 | {:ok, [%Tx{}, ...]} 284 | 285 | Make full text search against the transactions' meta data and tag names. 286 | 287 | iex> Txbox.all(%{search: "hubble deep field"}) 288 | {:ok, [%Tx{}, ...]} 289 | """ 290 | @spec all(String.t, map) :: {:ok, list(Tx.t)} 291 | def all(channel, %{} = query) do 292 | txns = Tx 293 | |> Transactions.channel(channel) 294 | |> Transactions.list_tx(query) 295 | 296 | {:ok, txns} 297 | end 298 | 299 | 300 | @doc false 301 | def find(txid) when is_binary(txid), 302 | do: find(@default_channel, txid) 303 | 304 | @doc false 305 | def find(txid, options) when is_binary(txid) and is_list(options), 306 | do: find(@default_channel, txid, options) 307 | 308 | @doc """ 309 | Finds a transaction by it's txid, scoped by the specified channel. 310 | 311 | If the channel is omitted, it defaults to `default_channel/0`. Alernatively, 312 | the channel can be specified as `"_"` which is the TXT syntax for the global 313 | scope. 314 | 315 | ## Options 316 | 317 | The accepted options are: 318 | 319 | * `:rawtx` - Include the full rawtx in the query response (defaults false) 320 | 321 | ## Examples 322 | 323 | Find within the default channel (`"txbox"`) 324 | 325 | iex> Txbox.find("6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110") 326 | {:ok, %Tx{}} 327 | 328 | Find within the specified channel 329 | 330 | iex> Txbox.find("photos", "6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110") 331 | {:ok, %Tx{}} 332 | 333 | Find within the global scope 334 | 335 | iex> Txbox.find("_", "6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110") 336 | {:ok, %Tx{}} 337 | """ 338 | @spec find(String.t, String.t, keyword) :: {:ok, Tx.t} | {:error, :not_found} 339 | def find(channel, txid, options \\ []) 340 | when is_binary(channel) 341 | and is_binary(txid) 342 | do 343 | rawtx = Keyword.get(options, :rawtx) 344 | tx = Tx 345 | |> Transactions.channel(channel) 346 | |> Transactions.query(%{rawtx: rawtx}) 347 | |> Transactions.get_tx(txid) 348 | 349 | case tx do 350 | %Tx{} = tx -> 351 | {:ok, tx} 352 | 353 | nil -> 354 | {:error, :not_found} 355 | end 356 | end 357 | 358 | 359 | @doc """ 360 | Adds the given transaction parameters into Txbox, within the specified channel. 361 | 362 | If the channel is omitted, it defaults to `default_channel/0`. 363 | 364 | ## Examples 365 | 366 | Add a transaction txid within the default channel (`"txbox"`). 367 | 368 | iex> Txbox.create(%{ 369 | ...> txid: "6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110" 370 | ...> }) 371 | {:ok, %Tx{}} 372 | 373 | Add a transaction with associated meta data, within a specified channel. 374 | 375 | iex> Txbox.create("photos", %{ 376 | ...> txid: "6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110", 377 | ...> tags: ["hubble", "universe"], 378 | ...> meta: %{ 379 | ...> title: "Hubble Ultra-Deep Field" 380 | ...> }, 381 | ...> data: %{ 382 | ...> bitfs: "https://x.bitfs.network/6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110.out.0.3" 383 | ...> } 384 | ...> }) 385 | {:ok, %Tx{}} 386 | """ 387 | @spec create(String.t, map) :: {:ok, Tx.t} | {:error, Ecto.Changeset.t} 388 | def create(channel \\ @default_channel, %{} = attrs) do 389 | attrs = Map.put(attrs, :channel, channel) 390 | 391 | case Transactions.create_tx(attrs) do 392 | {:ok, %Tx{} = tx} -> 393 | mapi_queue_push(tx) 394 | {:ok, tx} 395 | 396 | {:error, reason} -> 397 | {:error, reason} 398 | end 399 | end 400 | 401 | 402 | @doc """ 403 | Updates the given transaction with the updated parameters. 404 | 405 | It is only possible to update `"pending"` state transactions, otherwise the 406 | function will return an error. 407 | 408 | ## Examples 409 | 410 | iex> {:ok, tx} = Txbox.create(%{ 411 | ...> txid: "6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110", 412 | ...> meta: %{title: "Hubble Ultra-Deep Field"} 413 | ...> }) 414 | iex> 415 | iex> Txbox.update(tx, %{ 416 | ...> tags: ["hubble", "universe"] 417 | ...> }) 418 | {:ok, %Tx{ 419 | txid: "6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110", 420 | tags: ["hubble", "universe"], 421 | meta: %{ 422 | title: "Hubble Ultra-Deep Field" 423 | } 424 | }} 425 | """ 426 | @spec update(Tx.t, map) :: {:ok, Tx.t} | {:error, Ecto.Changeset.t} 427 | def update(%Tx{} = tx, %{} = attrs) do 428 | case Transactions.update_tx(tx, attrs) do 429 | {:ok, %Tx{} = tx} -> 430 | mapi_queue_push(tx) 431 | {:ok, tx} 432 | 433 | {:error, reason} -> 434 | {:error, reason} 435 | end 436 | end 437 | 438 | 439 | # Enqueues the transaction for mAPI processing if it has the correct state 440 | defp mapi_queue_push(%Tx{state: state} = tx) 441 | when state == "queued" 442 | or state == "pushed", 443 | do: Txbox.Mapi.Queue.push(tx) 444 | 445 | defp mapi_queue_push(%Tx{}), do: false 446 | 447 | end 448 | -------------------------------------------------------------------------------- /lib/txbox/mapi/processor.ex: -------------------------------------------------------------------------------- 1 | defmodule Txbox.Mapi.Processor do 2 | @moduledoc """ 3 | Miner API queue processor. 4 | 5 | Takes transactions one by one from the mAPI queue, and depending on the 6 | transaction state, either pushes the transaction to the miner, or checks the 7 | status of the transaction by its `txid`. 8 | """ 9 | require Logger 10 | use GenStage 11 | alias Txbox.Transactions 12 | alias Txbox.Transactions.Tx 13 | 14 | defstruct miner: nil 15 | 16 | @default_miner :taal 17 | 18 | 19 | @doc """ 20 | Starts the Queue Processor, linked to the current process. 21 | """ 22 | @spec start_link(keyword) :: GenServer.on_start 23 | def start_link(opts \\ []) do 24 | GenStage.start_link(__MODULE__, opts, name: __MODULE__) 25 | end 26 | 27 | 28 | # Callbacks 29 | 30 | 31 | @impl true 32 | def init(opts) do 33 | miner = case Keyword.get(opts, :miner, @default_miner) do 34 | {url, opts} -> Manic.miner(url, opts) 35 | url -> Manic.miner(url) 36 | end 37 | 38 | state = %__MODULE__{miner: miner} 39 | {:consumer, state, subscribe_to: [{Txbox.Mapi.Queue, max_demand: 1}]} 40 | end 41 | 42 | 43 | @impl true 44 | def handle_events(events, _, state) do 45 | Enum.each(events, fn 46 | %Tx{state: "queued"} = tx -> mapi_push(tx, state) 47 | %Tx{state: "pushed"} = tx -> mapi_status(tx, state) 48 | end) 49 | {:noreply, [], state} 50 | end 51 | 52 | 53 | # Sends the transaction to the miner 54 | defp mapi_push(%Tx{txid: txid, rawtx: rawtx} = tx, %{miner: miner}) do 55 | with {:ok, env} <- Manic.TX.push(miner, rawtx, as: :envelope), 56 | {:ok, payload} <- Manic.JSONEnvelope.parse_payload(env) 57 | do 58 | state = if payload["return_result"] == "success", do: "pushed", else: "failed" 59 | Transactions.update_tx_state(tx, state, Map.put(env, :payload, payload)) 60 | else 61 | {:error, error} -> 62 | Logger.error "mAPI push error: #{txid} : #{inspect error}" 63 | end 64 | end 65 | 66 | 67 | # Queries the status of a transaction by its txid 68 | defp mapi_status(%Tx{txid: txid} = tx, %{miner: miner}) do 69 | with {:ok, env} <- Manic.TX.status(miner, txid, as: :envelope), 70 | {:ok, payload} <- Manic.JSONEnvelope.parse_payload(env) 71 | do 72 | state = if payload["return_result"] == "success" 73 | and is_integer(payload["block_height"]) 74 | and payload["block_height"] > 0, 75 | do: "confirmed", 76 | else: "pushed" 77 | Transactions.update_tx_state(tx, state, Map.put(env, :payload, payload)) 78 | else 79 | {:error, error} -> 80 | Logger.error "mAPI status error: #{txid} : #{inspect error}" 81 | end 82 | end 83 | 84 | end 85 | -------------------------------------------------------------------------------- /lib/txbox/mapi/queue.ex: -------------------------------------------------------------------------------- 1 | defmodule Txbox.Mapi.Queue do 2 | @moduledoc """ 3 | Miner API background queue. 4 | 5 | Once a minute the Queue process queries the database for any transactions that 6 | should be added to the mAPI queue. 7 | """ 8 | use GenStage 9 | alias Txbox.Transactions 10 | alias Txbox.Transactions.Tx 11 | 12 | defstruct queue: :queue.new, demand: 0, retry: [] 13 | 14 | 15 | @doc """ 16 | Starts the Queue process, linked to the current process. 17 | """ 18 | @spec start_link(keyword) :: GenServer.on_start 19 | def start_link(opts \\ []) do 20 | GenStage.start_link(__MODULE__, opts, name: __MODULE__) 21 | end 22 | 23 | 24 | @doc """ 25 | Pushes the given transaction to the end of queue. 26 | """ 27 | @spec push(Tx.t) :: :ok 28 | def push(%Tx{} = tx), 29 | do: GenStage.cast(__MODULE__, {:push, tx}) 30 | 31 | 32 | # Callbacks 33 | 34 | 35 | @impl true 36 | def init(opts) do 37 | retry_opts = Keyword.take(opts, [:max_status_attempts, :retry_status_after]) 38 | state = %__MODULE__{retry: retry_opts} 39 | schedule_for_queue(0) 40 | {:producer, state} 41 | end 42 | 43 | 44 | @impl true 45 | def handle_cast({:push, %Tx{} = tx}, state) do 46 | update_in(state.queue, & :queue.in(tx, &1)) 47 | |> take_demanded_events 48 | end 49 | 50 | 51 | @impl true 52 | def handle_demand(demand, state) when demand > 0 do 53 | update_in(state.demand, & &1 + demand) 54 | |> take_demanded_events 55 | end 56 | 57 | 58 | @impl true 59 | def handle_info(:populate_queue, state) do 60 | queue = Transactions.list_tx_for_mapi(state.retry) 61 | |> :queue.from_list 62 | 63 | schedule_for_queue(60) 64 | 65 | update_in(state.queue, & :queue.join(&1, queue)) 66 | |> take_demanded_events 67 | end 68 | 69 | 70 | # Splits the queue according to the demand, and emits the demanded tx. 71 | defp take_demanded_events(state) do 72 | demand = :queue.len(state.queue) |> min(state.demand) 73 | {demanded, remaining} = :queue.split(demand, state.queue) 74 | 75 | state = update_in(state.demand, & &1 - :queue.len(demanded)) 76 | |> Map.put(:queue, remaining) 77 | 78 | {:noreply, :queue.to_list(demanded), state} 79 | end 80 | 81 | 82 | # Sends a :populate_queue message to self after the given number of seconds. 83 | defp schedule_for_queue(seconds), 84 | do: Process.send_after(self(), :populate_queue, seconds * 1000) 85 | 86 | end 87 | -------------------------------------------------------------------------------- /lib/txbox/transactions.ex: -------------------------------------------------------------------------------- 1 | defmodule Txbox.Transactions do 2 | @moduledoc """ 3 | Collection of functions for composing Ecto queries. 4 | 5 | The functions in this module can be broadly split into two types, expressions 6 | and queries. 7 | 8 | ## Expressions 9 | 10 | Expression functions can be used to compose queries following the Elixir 11 | pipeline syntax. 12 | 13 | iex> Tx 14 | ...> |> Transactions.confirmed(true) 15 | ...> |> Transactions.tagged(["space", "photos"]) 16 | %Ecto.Query{} 17 | 18 | ## Queries 19 | 20 | Query functions interface with the repo and either create or return records 21 | from the repo. 22 | 23 | iex> Transactions.list_tx() 24 | [%Tx{}, ...] 25 | """ 26 | import Ecto.Query, warn: false 27 | alias Ecto.Multi 28 | alias Txbox.Transactions.{Tx, MapiResponse} 29 | 30 | 31 | @query_keys [:channel, :search, :tagged, :from, :to, :at, :order, :limit, :offset, :rawtx] 32 | 33 | 34 | @doc """ 35 | Returns the application's configured Repo. 36 | 37 | Ensure your application's Repo is configured in `config.exs`: 38 | 39 | config :txbox, repo: MyApp.Repo 40 | """ 41 | @spec repo() :: module 42 | def repo(), do: Application.get_env(:txbox, :repo) 43 | 44 | 45 | @doc """ 46 | Get a transaction by it's internal ID or TXID. 47 | 48 | Can optionally pass a `Ecto.Queryable.t` as the first argument to compose 49 | queries. 50 | 51 | ## Examples 52 | 53 | # Find a tx by it's Txbox uuid 54 | iex> tx = Transactions.find_tx "e9d356cf-47e9-47c3-bfc8-c12673877302" 55 | 56 | # Composed query, found by txid 57 | iex> tx = Transactions.channel("mychannel) 58 | ...> |> Transactions.confirmed(true) 59 | ...> |> Transactions.find_tx("6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110") 60 | """ 61 | @doc group: :query 62 | @spec get_tx(Ecto.Queryable.t, binary) :: Ecto.Schema.t | nil 63 | def get_tx(tx \\ Tx, id) when is_binary(id) do 64 | pre_qry = from(r in MapiResponse, order_by: [desc: r.inserted_at]) 65 | qry = tx 66 | |> preload(status: ^pre_qry) 67 | |> optimize_select 68 | 69 | case String.match?(id, ~r/^[a-f0-9]{64}$/i) do 70 | true -> 71 | qry 72 | |> repo().get_by(txid: id) 73 | 74 | false -> 75 | qry 76 | |> repo().get(id) 77 | end 78 | end 79 | 80 | 81 | @doc false 82 | def list_tx(), do: list_tx(Tx, %{}) 83 | @doc false 84 | def list_tx(Tx = tx), do: list_tx(tx, %{}) 85 | def list_tx(%Ecto.Query{} = tx), do: list_tx(tx, %{}) 86 | 87 | @doc """ 88 | Returns a list of transactions. 89 | 90 | Can optionally pass a `Ecto.Queryable.t` as the first argument to compose 91 | queries. If a map of query options is given as a secndon argument, the query 92 | is filtered by those arguments. 93 | 94 | ## Examples 95 | 96 | iex> txns = Transactions.channel("mychannel) 97 | ...> |> Transactions.confirmed(true) 98 | ...> |> Transactions.list_tx 99 | """ 100 | @doc group: :query 101 | @spec list_tx(Ecto.Queryable.t, map) :: list(Ecto.Schema.t) 102 | def list_tx(tx \\ Tx, params) when is_map(params) do 103 | pre_qry = from(r in MapiResponse, order_by: [desc: r.inserted_at]) 104 | tx 105 | |> preload(status: ^pre_qry) 106 | |> query(params) 107 | |> optimize_select 108 | |> repo().all 109 | end 110 | 111 | 112 | @doc """ 113 | Returns a list of transactions that must be pushed or have their status 114 | confirmed by mAPI. 115 | 116 | This is used internally by `Txbox.Mapi.Queue` to fetch transactions for 117 | automatic processing. 118 | 119 | ## Options 120 | 121 | The accepted options are: 122 | 123 | * `:max_status_attempts` - How many times to poll mAPI for confirmation status. Defaults to `20`. 124 | * `:retry_status_after` - Number of seconds before polling mAPI for confirmation status. Defaults to `300` (5 minutes). 125 | """ 126 | @doc group: :query 127 | @spec list_tx_for_mapi(keyword) :: list(Ecto.Schema.t) 128 | def list_tx_for_mapi(opts \\ []) do 129 | max_status_attempts = Keyword.get(opts, :max_status_attempts, 20) 130 | retry_status_after = Keyword.get(opts, :retry_status_after, 300) 131 | retry_datetime = DateTime.now!("Etc/UTC") |> DateTime.add(-retry_status_after) 132 | 133 | pre_qry = from(r in MapiResponse, order_by: [desc: r.inserted_at]) 134 | 135 | Tx 136 | |> join(:left, [t], r in subquery(pre_qry), on: r.tx_guid == t.guid) 137 | |> preload(status: ^pre_qry) 138 | |> where([t], 139 | t.state == "queued" 140 | and fragment("SELECT COUNT(*) FROM txbox_mapi_responses WHERE type = ? AND tx_guid = ?", "push", t.guid) < 1) 141 | |> or_where([t, r], 142 | t.state == "pushed" 143 | and (is_nil(r) or r.inserted_at < ^retry_datetime) 144 | and fragment("SELECT COUNT(*) FROM txbox_mapi_responses WHERE type = ? AND tx_guid = ?", "status", t.guid) < ^max_status_attempts) 145 | |> repo().all 146 | end 147 | 148 | 149 | @doc """ 150 | Creates a transaction from the given params. 151 | 152 | Returns an `:ok` / `:error` tuple response. 153 | 154 | ## Examples 155 | 156 | iex> {:ok, tx} = Transactions.create_tx(%{ 157 | ...> txid: "6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110", 158 | ...> channel: "mychannel" 159 | ...> }) 160 | """ 161 | @doc group: :query 162 | @spec create_tx(map) :: {:ok, Ecto.Schema.t} | {:error, Ecto.Changeset.t()} 163 | def create_tx(attrs \\ %{}) do 164 | %Tx{} 165 | |> Tx.changeset(attrs) 166 | |> repo().insert 167 | end 168 | 169 | 170 | @doc """ 171 | Updates the transaction with the given params. 172 | 173 | Returns an `:ok` / `:error` tuple response. 174 | 175 | ## Examples 176 | 177 | iex> {:ok, tx} = Transactions.update_tx(tx, %{ 178 | ...> meta: %{ 179 | ...> title: "Hubble Ultra-Deep Field" 180 | ...> } 181 | ...> }) 182 | """ 183 | @doc group: :query 184 | @spec update_tx(Ecto.Schema.t, map) :: 185 | {:ok, Ecto.Schema.t} | 186 | {:error, Ecto.Changeset.t()} 187 | def update_tx(%Tx{} = tx, attrs \\ %{}) do 188 | tx 189 | |> Tx.changeset(attrs) 190 | |> repo().update 191 | |> case do 192 | {:ok, tx} -> 193 | pre_qry = from(r in MapiResponse, order_by: [desc: r.inserted_at]) 194 | {:ok, repo().preload(tx, [status: pre_qry], force: true)} 195 | 196 | error -> 197 | error 198 | end 199 | end 200 | 201 | 202 | @doc """ 203 | Updates the given transaction's state. 204 | 205 | Returns an `:ok` / `:error` tuple response. 206 | 207 | ## Examples 208 | 209 | iex> {:ok, tx} = Transactions.update_tx_state(tx, "pushed") 210 | """ 211 | @doc group: :query 212 | @spec update_tx_state(Ecto.Schema.t, String.t) :: 213 | {:ok, Ecto.Schema.t} | 214 | {:error, Ecto.Changeset.t()} 215 | def update_tx_state(%Tx{} = tx, state), 216 | do: update_tx(tx, %{state: state}) 217 | 218 | 219 | @doc """ 220 | Updates the given transaction's state, and stores the mAPI response. 221 | 222 | Returns an `:ok` / `:error` tuple response. 223 | 224 | ## Examples 225 | 226 | iex> {:ok, tx} = Transactions.update_tx_state(tx, "pushed", mapi_response) 227 | """ 228 | @doc group: :query 229 | @spec update_tx_state(Ecto.Schema.t, String.t, Manic.JSONEnvelope.t | map) :: 230 | {:ok, Ecto.Schema.t} | 231 | {:error, Ecto.Changeset.t} | 232 | {:error, %{required(atom) => Ecto.Changeset.t}} 233 | def update_tx_state(%Tx{state: "queued"} = tx, state, mapi_response) do 234 | mapi = Ecto.build_assoc(tx, :mapi_responses) 235 | Multi.new 236 | |> Multi.insert(:mapi_response, MapiResponse.push_changeset(mapi, mapi_response)) 237 | |> Multi.update(:tx, Fsmx.transition_changeset(tx, state, mapi_response)) 238 | |> update_tx_state 239 | end 240 | 241 | def update_tx_state(%Tx{state: "pushed"} = tx, state, mapi_response) do 242 | mapi = Ecto.build_assoc(tx, :mapi_responses) 243 | Multi.new 244 | |> Multi.insert(:mapi_response, MapiResponse.status_changeset(mapi, mapi_response)) 245 | |> Multi.update(:tx, Fsmx.transition_changeset(tx, state, mapi_response)) 246 | |> update_tx_state 247 | end 248 | 249 | def update_tx_state(%Tx{} = tx, state, _mapi_response) do 250 | Multi.new 251 | |> Multi.update(:tx, Fsmx.transition_changeset(tx, state)) 252 | |> update_tx_state 253 | end 254 | 255 | defp update_tx_state(%Multi{} = multi) do 256 | case repo().transaction(multi) do 257 | {:ok, %{tx: tx}} -> 258 | pre_qry = from(r in MapiResponse, order_by: [desc: r.inserted_at]) 259 | {:ok, repo().preload(tx, [status: pre_qry], force: true)} 260 | 261 | {:error, name, changeset, _} -> 262 | {:error, %{name => changeset}} 263 | 264 | {:error, error} -> 265 | {:error, error} 266 | end 267 | end 268 | 269 | 270 | @doc """ 271 | Deletes the given transaction from the repo. 272 | 273 | Returns an `:ok` / `:error` tuple response. 274 | 275 | ## Examples 276 | 277 | iex> {:ok, tx} = Transactions.get_tx("6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110") 278 | ...> |> Transactions.delete_tx 279 | """ 280 | @doc group: :query 281 | @spec delete_tx(Ecto.Schema.t) :: {:ok, Ecto.Schema.t} | {:error, Ecto.Changeset.t()} 282 | def delete_tx(%Tx{} = tx), 283 | do: repo().delete(tx) 284 | 285 | 286 | @doc """ 287 | Returns a list of transactions filtered by the given search term. 288 | 289 | Performs a full text search on the transactions' metadata. Can optionally pass 290 | a `Ecto.Queryable.t` as the first argument to compose queries. 291 | 292 | ## Examples 293 | 294 | iex> {:ok, txns} = Transactions.search_tx("unwriter bitpic") 295 | """ 296 | @doc group: :query 297 | @spec search_tx(Ecto.Queryable.t, String.t) :: list(Ecto.Schema.t) 298 | def search_tx(tx \\ Tx, term) when is_binary(term) do 299 | tx 300 | |> search(term) 301 | |> list_tx 302 | end 303 | 304 | 305 | @doc """ 306 | Query by the given query map. 307 | """ 308 | @doc group: :expression 309 | @spec query(Ecto.Queryable.t, map) :: Ecto.Queryable.t 310 | def query(tx, %{} = qry) do 311 | qry 312 | |> normalize_query 313 | |> Enum.reduce(tx, &build_query/2) 314 | end 315 | 316 | 317 | @doc """ 318 | Search by the given term. 319 | """ 320 | @doc group: :expression 321 | @spec search(Ecto.Queryable.t, String.t) :: Ecto.Queryable.t 322 | def search(tx, term) when is_binary(term) do 323 | tx 324 | |> where(fragment("search_vector @@ plainto_tsquery(?)", ^term)) 325 | |> order_by(fragment("ts_rank(search_vector, plainto_tsquery(?)) DESC", ^term)) 326 | end 327 | 328 | 329 | @doc """ 330 | Query by the given channel name. 331 | """ 332 | @doc group: :expression 333 | @spec channel(Ecto.Queryable.t, binary) :: Ecto.Queryable.t 334 | def channel(tx, "_"), do: tx 335 | def channel(tx, chan) 336 | when is_binary(chan), 337 | do: where(tx, channel: ^chan) 338 | 339 | 340 | @doc """ 341 | Query by the given tag or list of tags. 342 | 343 | Optionally tags can be specified as a comma seperated string 344 | """ 345 | @doc group: :expression 346 | @spec tagged(Ecto.Queryable.t, list | String.t) :: Ecto.Queryable.t 347 | def tagged(tx, tags) when is_list(tags), 348 | do: where(tx, fragment("tags @> ?", ^tags)) 349 | 350 | def tagged(tx, tags) when is_binary(tags), 351 | do: tagged(tx, String.split(tags, ",") |> Enum.map(&String.trim/1)) 352 | 353 | 354 | @doc """ 355 | Query by the transaction confirmation status. 356 | """ 357 | @doc group: :expression 358 | @spec confirmed(Ecto.Queryable.t, boolean) :: Ecto.Queryable.t 359 | def confirmed(tx, conf \\ true) 360 | 361 | def confirmed(tx, true), 362 | do: where(tx, [t], t.state == "confirmed") 363 | 364 | def confirmed(tx, false), 365 | do: where(tx, [t], t.state != "confirmed") 366 | 367 | 368 | @doc """ 369 | Ensures rawtx is selected in the given query. 370 | """ 371 | @doc group: :expression 372 | @spec with_rawtx(Ecto.Queryable.t) :: Ecto.Queryable.t 373 | def with_rawtx(tx), do: select(tx, [t], t) 374 | 375 | 376 | # Normalizes a query map by converting all keys to strings, taking the 377 | # allowed keys, and converting back to atoms 378 | defp normalize_query(query) do 379 | query 380 | |> Map.new(fn {k, v} -> {normalize_key(k), v} end) 381 | |> Map.take(Enum.map(@query_keys, &Atom.to_string/1)) 382 | |> Map.new(fn {k, v} -> {String.to_atom(k), v} end) 383 | end 384 | 385 | # Normalizes the given key as a string 386 | defp normalize_key(key) when is_atom(key), do: Atom.to_string(key) 387 | defp normalize_key(key), do: key 388 | 389 | 390 | # Composes a query from the given tuple, adding to the existing queryable 391 | defp build_query({:search, term}, tx), do: search(tx, term) 392 | 393 | defp build_query({:channel, chan}, tx), do: channel(tx, chan) 394 | 395 | defp build_query({:tagged, tags}, tx), do: tagged(tx, tags) 396 | 397 | defp build_query({:from, height}, tx), 398 | do: where(tx, [t], t.block_height >= ^height) 399 | 400 | defp build_query({:to, height}, tx), 401 | do: where(tx, [t], t.block_height <= ^height) 402 | 403 | defp build_query({:at, true}, tx), do: confirmed(tx, true) 404 | defp build_query({:at, "-null"}, tx), do: confirmed(tx, true) 405 | defp build_query({:at, false}, tx), do: confirmed(tx, false) 406 | defp build_query({:at, nil}, tx), do: confirmed(tx, false) 407 | defp build_query({:at, "null"}, tx), do: confirmed(tx, false) 408 | defp build_query({:at, height}, tx), 409 | do: where(tx, [t], t.block_height == ^height) 410 | 411 | defp build_query({:order, "created_at"}, tx), 412 | do: order_by(tx, asc: :inserted_at) 413 | defp build_query({:order, "inserted_at"}, tx), 414 | do: order_by(tx, asc: :inserted_at) 415 | defp build_query({:order, "-created_at"}, tx), 416 | do: order_by(tx, desc: :inserted_at) 417 | defp build_query({:order, "-inserted_at"}, tx), 418 | do: order_by(tx, desc: :inserted_at) 419 | defp build_query({:order, "i"}, tx), 420 | do: order_by(tx, asc: :block_height) 421 | defp build_query({:order, "block_height"}, tx), 422 | do: order_by(tx, asc: :block_height) 423 | defp build_query({:order, "-i"}, tx), 424 | do: order_by(tx, desc: :block_height) 425 | defp build_query({:order, "-block_height"}, tx), 426 | do: order_by(tx, desc: :block_height) 427 | defp build_query({:order, _order}, tx), do: tx 428 | 429 | defp build_query({:limit, num}, tx), do: limit(tx, ^num) 430 | defp build_query({:offset, num}, tx), do: offset(tx, ^num) 431 | 432 | defp build_query({:rawtx, val}, tx) when val in [false, nil], do: tx 433 | defp build_query({:rawtx, _}, tx), do: with_rawtx(tx) 434 | 435 | # Optimizes the select query unless already set 436 | defp optimize_select(%{select: sel} = tx) when not is_nil(sel), do: tx 437 | defp optimize_select(tx) do 438 | keys = Tx.__schema__(:fields) 439 | |> Enum.reject(& &1 == :rawtx) 440 | select(tx, ^keys) 441 | end 442 | 443 | end 444 | -------------------------------------------------------------------------------- /lib/txbox/transactions/mapi_response.ex: -------------------------------------------------------------------------------- 1 | defmodule Txbox.Transactions.MapiResponse do 2 | @moduledoc """ 3 | Miner API Response schema. 4 | """ 5 | use Ecto.Schema 6 | import Ecto.Changeset 7 | alias Txbox.Transactions.Tx 8 | 9 | 10 | @typedoc "MAPI Response schema" 11 | @type t :: %__MODULE__{ 12 | id: binary, 13 | type: String.t, 14 | payload: Manic.JSONEnvelope.payload, 15 | public_key: String.t, 16 | signature: String.t, 17 | verified: boolean, 18 | tx_guid: binary 19 | } 20 | 21 | 22 | @primary_key {:id, :binary_id, autogenerate: true} 23 | @foreign_key_type :binary_id 24 | 25 | schema "txbox_mapi_responses" do 26 | field :type, :string 27 | field :payload, :map 28 | field :public_key, :string 29 | field :signature, :string 30 | field :verified, :boolean, default: false 31 | 32 | belongs_to :tx, Tx, foreign_key: :tx_guid, references: :guid 33 | 34 | timestamps(type: :utc_datetime_usec, updated_at: false) 35 | end 36 | 37 | 38 | @doc false 39 | def changeset(response, %Manic.JSONEnvelope{} = env), 40 | do: changeset(response, Map.from_struct(env)) 41 | 42 | def changeset(response, attrs) do 43 | response 44 | |> cast(attrs, [:type, :payload, :public_key, :signature, :verified]) 45 | |> validate_required([:type, :payload]) 46 | |> validate_inclusion(:type, ["push", "status"]) 47 | end 48 | 49 | def push_changeset(response, attrs) do 50 | response 51 | |> change(%{type: "push"}) 52 | |> changeset(attrs) 53 | end 54 | 55 | def status_changeset(response, attrs) do 56 | response 57 | |> change(%{type: "status"}) 58 | |> changeset(attrs) 59 | end 60 | 61 | end 62 | -------------------------------------------------------------------------------- /lib/txbox/transactions/meta.ex: -------------------------------------------------------------------------------- 1 | defmodule Txbox.Transactions.Meta do 2 | @moduledoc """ 3 | Transaction metadata embedded schema module. 4 | 5 | The transaction metadata is a structured schema for describing human readable 6 | metadata about the transaction. The following attributes can be set: 7 | 8 | * `:title` - The title of the transaction. 9 | * `:description` - A short description of the transaction. 10 | * `:image` - The url of an approriate preview image for the transaction. 11 | * `:link` - The full permalink URL to access the transaction. 12 | * `:content` - Markdown formatted content of the transaction. 13 | """ 14 | use Ecto.Schema 15 | import Ecto.Changeset 16 | 17 | 18 | @typedoc "Transaction metadata schema" 19 | @type t :: %__MODULE__{ 20 | title: String.t, 21 | description: String.t, 22 | image: String.t, 23 | link: String.t, 24 | content: String.t 25 | } 26 | 27 | 28 | @primary_key false 29 | 30 | embedded_schema do 31 | field :title 32 | field :description 33 | field :image 34 | field :link 35 | field :content 36 | end 37 | 38 | 39 | @doc false 40 | def changeset(meta, attrs) do 41 | meta 42 | |> cast(attrs, [:title, :description, :image, :link, :content]) 43 | end 44 | 45 | end 46 | -------------------------------------------------------------------------------- /lib/txbox/transactions/tx.ex: -------------------------------------------------------------------------------- 1 | defmodule Txbox.Transactions.Tx do 2 | @moduledoc """ 3 | Transaction schema module. 4 | 5 | Txbox adds a single table to your database containing all of the transaction, 6 | channel and meta data. 7 | 8 | For any transaction, the only required attribute is the `txid`. If no channel 9 | is specified, the transaction will be added to the `Txbox.default_channel/0`. 10 | Optionally any of the following attributes can be set: 11 | 12 | * `:rawtx` - The raw transaction data. Must be given as a raw `t:binary/0`, not a hex encoded string. 13 | * `:tags` - A list of tags which can be used for organising and filtering transactions. 14 | * `:meta` - A map containing structured metadata about the transaction. See `t:Txbox.Transactions.Meta.t/0`. 15 | * `:data` - A map containing any other arbitarry fields. 16 | 17 | When searching Txbox, the data from `:tags` and `:meta` are incorporated into 18 | full text search. 19 | 20 | Txbox automatically syncs the transaction with your configured miner, and 21 | updates the `:status` attribute with a cached response from the miner's Merchant 22 | API. See `t:Txbox.Transactions.Status.t/0`. 23 | """ 24 | use Ecto.Schema 25 | import Ecto.Changeset 26 | alias Txbox.Transactions.{Meta, MapiResponse} 27 | 28 | @typedoc "Transaction schema" 29 | @type t :: %__MODULE__{ 30 | guid: binary, 31 | state: String.t, 32 | txid: String.t, 33 | rawtx: binary, 34 | channel: String.t, 35 | tags: list(String.t), 36 | meta: Meta.t, 37 | data: map, 38 | block_height: integer, 39 | inserted_at: DateTime.t, 40 | updated_at: DateTime.t 41 | } 42 | 43 | 44 | @default_state "pending" 45 | @default_channel "txbox" 46 | @primary_key {:guid, :binary_id, autogenerate: true} 47 | @foreign_key_type :binary_id 48 | 49 | schema "txbox_txns" do 50 | field :state, :string, default: @default_state 51 | field :txid, :string 52 | field :rawtx, :binary 53 | field :channel, :string, default: @default_channel 54 | field :tags, {:array, :string} 55 | field :data, :map 56 | field :block_height, :integer 57 | 58 | embeds_one :meta, Meta, on_replace: :update 59 | has_many :mapi_responses, MapiResponse, foreign_key: :tx_guid 60 | has_one :status, MapiResponse, foreign_key: :tx_guid 61 | 62 | timestamps(type: :utc_datetime_usec) 63 | end 64 | 65 | 66 | use Fsmx.Struct, transitions: %{ 67 | "pending" => ["queued", "pushed"], 68 | "queued" => ["pushed", "failed"], 69 | "pushed" => ["pushed", "confirmed"] 70 | } 71 | 72 | 73 | @doc false 74 | def changeset(tx, attrs) do 75 | tx 76 | |> cast(attrs, [:state, :txid, :rawtx, :channel, :tags, :data]) 77 | |> cast_embed(:meta, with: &Meta.changeset/2) 78 | |> validate_required([:state, :txid, :channel]) 79 | |> validate_format(:txid, ~r/^[a-f0-9]{64}$/i) 80 | |> validate_format(:channel, ~r/^\w[\w\-\/]*$/) 81 | |> validate_state 82 | end 83 | 84 | 85 | # Changeset for transitioning state from "pushed" to "confirmed" 86 | def transition_changeset(tx, "pushed", "confirmed", response) do 87 | block_height = get_in(response, [Access.key(:payload), "block_height"]) 88 | tx 89 | |> cast(%{block_height: block_height}, [:block_height]) 90 | end 91 | 92 | 93 | # Validates the changeset state change 94 | defp validate_state(%{data: %__MODULE__{} = tx} = changeset) do 95 | persisted? = Ecto.get_meta(tx, :state) == :loaded 96 | 97 | changeset = case persisted? && tx.state != "pending" do 98 | true -> add_error(changeset, :base, "cannot mutate non-pending transaction") 99 | false -> changeset 100 | end 101 | 102 | transitions = __MODULE__.__fsmx__().__fsmx__(:transitions) 103 | 104 | validate_change(changeset, :state, fn :state, state -> 105 | case Map.keys(transitions) |> Enum.member?(state) do 106 | true -> [] 107 | false -> [state: "cannot be #{state}"] 108 | end 109 | end) 110 | end 111 | 112 | end 113 | -------------------------------------------------------------------------------- /media/poster.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libitx/txbox/30ab11c939290832655eacab4411662dd4ee9cc7/media/poster.png -------------------------------------------------------------------------------- /media/state-machine.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/libitx/txbox/30ab11c939290832655eacab4411662dd4ee9cc7/media/state-machine.png -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | defmodule Txbox.MixProject do 2 | use Mix.Project 3 | 4 | def project do 5 | [ 6 | app: :txbox, 7 | version: "0.3.3", 8 | elixir: "~> 1.10", 9 | elixirc_paths: elixirc_paths(Mix.env()), 10 | start_permanent: Mix.env() == :prod, 11 | aliases: aliases(), 12 | deps: deps(), 13 | name: "Txbox", 14 | description: "Txbox is a Bitcoin transaction storage schema", 15 | source_url: "https://github.com/libitx/txbox", 16 | docs: [ 17 | main: "Txbox", 18 | groups_for_functions: [ 19 | "Expressions": & &1[:group] == :expression, 20 | "Queries": & &1[:group] == :query 21 | ], 22 | groups_for_modules: [ 23 | Schema: [ 24 | Txbox.Transactions.Tx, 25 | Txbox.Transactions.Meta, 26 | Txbox.Transactions.Status 27 | ], 28 | mAPI: [ 29 | Txbox.Mapi.Queue, 30 | Txbox.Mapi.Processor 31 | ] 32 | ] 33 | ], 34 | package: [ 35 | name: "txbox", 36 | files: ~w(lib .formatter.exs mix.exs priv README.md LICENSE), 37 | licenses: ["MIT"], 38 | links: %{ 39 | "GitHub" => "https://github.com/libitx/txbox" 40 | } 41 | ] 42 | ] 43 | end 44 | 45 | # Run "mix help compile.app" to learn about applications. 46 | def application do 47 | [ 48 | extra_applications: [:logger, :gen_stage] 49 | ] 50 | end 51 | 52 | # Run "mix help deps" to learn about dependencies. 53 | defp deps do 54 | [ 55 | {:ecto, "~> 3.5"}, 56 | {:ecto_sql, "~> 3.5"}, 57 | {:ex_doc, "~> 0.23", only: :dev, runtime: false}, 58 | {:jason, "~> 1.2"}, 59 | {:fsmx, "~> 0.3"}, 60 | {:gen_stage, "~> 1.0"}, 61 | {:manic, "~> 0.1"}, 62 | {:postgrex, "~> 0.15", optional: true} 63 | ] 64 | end 65 | 66 | defp elixirc_paths(:test), do: ["lib", "test/support"] 67 | defp elixirc_paths(:dev), do: ["lib", "test/support"] 68 | defp elixirc_paths(_), do: ["lib"] 69 | 70 | defp aliases do 71 | [ 72 | test: [ 73 | "ecto.drop --quiet", 74 | "ecto.create --quiet", 75 | "txbox.gen.migrations", 76 | "ecto.migrate", 77 | "test" 78 | ] 79 | ] 80 | end 81 | end 82 | -------------------------------------------------------------------------------- /mix.lock: -------------------------------------------------------------------------------- 1 | %{ 2 | "basefiftyeight": {:hex, :basefiftyeight, "0.1.0", "3d48544743bf9aab7ab02aed803ac42af77acf268c7d8c71d4f39e7fa85ee8d3", [:mix], [], "hexpm", "af12f551429528c711e98628c029ad48d1e5ba5a284f40b2d91029a65381837a"}, 3 | "bsv": {:hex, :bsv, "2.1.0", "3dd0be57a5272ca47ddd4e401b0e8f5a01937ea17399a0a7b60c34fe9248e2c7", [:mix], [{:basefiftyeight, "~> 0.1", [hex: :basefiftyeight, repo: "hexpm", optional: false]}, {:curvy, "~> 0.3", [hex: :curvy, repo: "hexpm", optional: false]}], "hexpm", "8f5f5e54574524dedb884848b63779faf9f717db13a505e22e16232c8c2a39a1"}, 4 | "castore": {:hex, :castore, "0.1.18", "deb5b9ab02400561b6f5708f3e7660fc35ca2d51bfc6a940d2f513f89c2975fc", [:mix], [], "hexpm", "61bbaf6452b782ef80b33cdb45701afbcf0a918a45ebe7e73f1130d661e66a06"}, 5 | "connection": {:hex, :connection, "1.1.0", "ff2a49c4b75b6fb3e674bfc5536451607270aac754ffd1bdfe175abe4a6d7a68", [:mix], [], "hexpm", "722c1eb0a418fbe91ba7bd59a47e28008a189d47e37e0e7bb85585a016b2869c"}, 6 | "curvy": {:hex, :curvy, "0.3.0", "cd097f77434baf783cd18a8374e1eae37dc203588902a3b59d1b4056ac9e7366", [:mix], [], "hexpm", "1fa6104cb458cfe6962b8353a0a6563bd191f9e72807fca0fa22b5a61809695c"}, 7 | "db_connection": {:hex, :db_connection, "2.3.1", "4c9f3ed1ef37471cbdd2762d6655be11e38193904d9c5c1c9389f1b891a3088e", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm", "abaab61780dde30301d840417890bd9f74131041afd02174cf4e10635b3a63f5"}, 8 | "decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"}, 9 | "earmark": {:hex, :earmark, "1.4.5", "62ffd3bd7722fb7a7b1ecd2419ea0b458c356e7168c1f5d65caf09b4fbdd13c8", [:mix], [], "hexpm", "b7d0e6263d83dc27141a523467799a685965bf8b13b6743413f19a7079843f4f"}, 10 | "earmark_parser": {:hex, :earmark_parser, "1.4.12", "b245e875ec0a311a342320da0551da407d9d2b65d98f7a9597ae078615af3449", [:mix], [], "hexpm", "711e2cc4d64abb7d566d43f54b78f7dc129308a63bc103fbd88550d2174b3160"}, 11 | "ecto": {:hex, :ecto, "3.5.5", "48219a991bb86daba6e38a1e64f8cea540cded58950ff38fbc8163e062281a07", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "98dd0e5e1de7f45beca6130d13116eae675db59adfa055fb79612406acf6f6f1"}, 12 | "ecto_sql": {:hex, :ecto_sql, "3.5.3", "1964df0305538364b97cc4661a2bd2b6c89d803e66e5655e4e55ff1571943efd", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.5.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.3.0 or ~> 0.4.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d2f53592432ce17d3978feb8f43e8dc0705e288b0890caf06d449785f018061c"}, 13 | "ex_doc": {:hex, :ex_doc, "0.23.0", "a069bc9b0bf8efe323ecde8c0d62afc13d308b1fa3d228b65bca5cf8703a529d", [:mix], [{:earmark_parser, "~> 1.4.0", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "f5e2c4702468b2fd11b10d39416ddadd2fcdd173ba2a0285ebd92c39827a5a16"}, 14 | "fsmx": {:hex, :fsmx, "0.3.0", "d60463b25681313514cfb7bdd92e18818dbc4c1967a5495f685b11b43b612f87", [:mix], [{:ecto, ">= 3.0.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_sql, ">= 3.0.0", [hex: :ecto_sql, repo: "hexpm", optional: true]}], "hexpm", "11829e293ab0987a0aa6046733ddef21dbce5f9798ac217e9de1ba78405a8c8a"}, 15 | "gen_stage": {:hex, :gen_stage, "1.0.0", "51c8ae56ff54f9a2a604ca583798c210ad245f415115453b773b621c49776df5", [:mix], [], "hexpm", "1d9fc978db5305ac54e6f5fec7adf80cd893b1000cf78271564c516aa2af7706"}, 16 | "hpax": {:hex, :hpax, "0.1.2", "09a75600d9d8bbd064cdd741f21fc06fc1f4cf3d0fcc335e5aa19be1a7235c84", [:mix], [], "hexpm", "2c87843d5a23f5f16748ebe77969880e29809580efdaccd615cd3bed628a8c13"}, 17 | "jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"}, 18 | "libsecp256k1": {:hex, :libsecp256k1, "0.1.10", "d27495e2b9851c7765129b76c53b60f5e275bd6ff68292c50536bf6b8d091a4d", [:make, :mix], [{:mix_erlang_tasks, "0.1.0", [hex: :mix_erlang_tasks, repo: "hexpm", optional: false]}], "hexpm", "09ea06239938571124f7f5a27bc9ac45dfb1cfc2df40d46ee9b59c3d51366652"}, 19 | "makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"}, 20 | "makeup_elixir": {:hex, :makeup_elixir, "0.15.0", "98312c9f0d3730fde4049985a1105da5155bfe5c11e47bdc7406d88e01e4219b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "75ffa34ab1056b7e24844c90bfc62aaf6f3a37a15faa76b07bc5eba27e4a8b4a"}, 21 | "manic": {:hex, :manic, "0.1.1", "0f9b6be86433be42115e8693252ff9e7e74786ea0394a3e0576d09a194328f1f", [:mix], [{:bsv, "~> 2.1", [hex: :bsv, repo: "hexpm", optional: false]}, {:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:curvy, "~> 0.3", [hex: :curvy, repo: "hexpm", optional: false]}, {:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: false]}, {:mint, "~> 1.4", [hex: :mint, repo: "hexpm", optional: false]}, {:recase, "~> 0.7", [hex: :recase, repo: "hexpm", optional: false]}, {:tesla, "~> 1.4", [hex: :tesla, repo: "hexpm", optional: false]}], "hexpm", "d14507df7885ff53b2570b0ca32cc5b931cfdbc21cb72714e085762af7c67309"}, 22 | "mime": {:hex, :mime, "2.0.3", "3676436d3d1f7b81b5a2d2bd8405f412c677558c81b1c92be58c00562bb59095", [:mix], [], "hexpm", "27a30bf0db44d25eecba73755acf4068cbfe26a4372f9eb3e4ea3a45956bff6b"}, 23 | "mint": {:hex, :mint, "1.4.2", "50330223429a6e1260b2ca5415f69b0ab086141bc76dc2fbf34d7c389a6675b2", [:mix], [{:castore, "~> 0.1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "ce75a5bbcc59b4d7d8d70f8b2fc284b1751ffb35c7b6a6302b5192f8ab4ddd80"}, 24 | "mix_erlang_tasks": {:hex, :mix_erlang_tasks, "0.1.0", "36819fec60b80689eb1380938675af215565a89320a9e29c72c70d97512e4649", [:mix], [], "hexpm", "95d2839c422c482a70c08a8702da8242f86b773f8ab6e8602a4eb72da8da04ed"}, 25 | "nimble_parsec": {:hex, :nimble_parsec, "1.1.0", "3a6fca1550363552e54c216debb6a9e95bd8d32348938e13de5eda962c0d7f89", [:mix], [], "hexpm", "08eb32d66b706e913ff748f11694b17981c0b04a33ef470e33e11b3d3ac8f54b"}, 26 | "postgrex": {:hex, :postgrex, "0.15.7", "724410acd48abac529d0faa6c2a379fb8ae2088e31247687b16cacc0e0883372", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "88310c010ff047cecd73d5ceca1d99205e4b1ab1b9abfdab7e00f5c9d20ef8f9"}, 27 | "qex": {:hex, :qex, "0.5.0", "5a3a9becf67d4006377c4c247ffdaaa8ae5b3634a0caadb788dc24d6125068f4", [:mix], [], "hexpm", "4ad6f6421163cd8204509a119a5c9813cbb969cfb8d802a9dc49b968bffbac2a"}, 28 | "recase": {:hex, :recase, "0.7.0", "3f2f719f0886c7a3b7fe469058ec539cb7bbe0023604ae3bce920e186305e5ae", [:mix], [], "hexpm", "36f5756a9f552f4a94b54a695870e32f4e72d5fad9c25e61bc4a3151c08a4e0c"}, 29 | "telemetry": {:hex, :telemetry, "0.4.3", "a06428a514bdbc63293cd9a6263aad00ddeb66f608163bdec7c8995784080818", [:rebar3], [], "hexpm", "eb72b8365ffda5bed68a620d1da88525e326cb82a75ee61354fc24b844768041"}, 30 | "tesla": {:hex, :tesla, "1.4.4", "bb89aa0c9745190930366f6a2ac612cdf2d0e4d7fff449861baa7875afd797b2", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}, {:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: true]}, {:finch, "~> 0.3", [hex: :finch, repo: "hexpm", optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, repo: "hexpm", optional: true]}, {:gun, "~> 1.3", [hex: :gun, repo: "hexpm", optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: true]}, {:ibrowse, "4.4.0", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: true]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "d5503a49f9dec1b287567ea8712d085947e247cb11b06bc54adb05bfde466457"}, 31 | } 32 | -------------------------------------------------------------------------------- /priv/templates/migrations/1_setup_txbox.exs.eex: -------------------------------------------------------------------------------- 1 | defmodule <%= module_prefix %>.Repo.Migrations.SetupTxBox do 2 | use Ecto.Migration 3 | 4 | def up do 5 | # Create the Txbox tx table 6 | create table(:txbox_txns, primary_key: false) do 7 | add :id, :binary_id, primary_key: true 8 | add :txid, :string 9 | add :rawtx, :binary 10 | add :channel, :string 11 | add :tags, {:array, :string} 12 | add :meta, :map 13 | add :data, :map 14 | add :status, :map 15 | add :block_hash, :string 16 | add :block_height, :integer 17 | add :search_vector, :tsvector 18 | add :mapi_attempt, :integer, default: 0 19 | add :mapi_attempted_at, :utc_datetime 20 | add :mapi_completed_at, :utc_datetime 21 | 22 | timestamps(type: :utc_datetime_usec) 23 | end 24 | 25 | 26 | # Add indexes for common queries and full-text search 27 | create index(:txbox_txns, [:txid], unique: true) 28 | create index(:txbox_txns, [:channel]) 29 | create index(:txbox_txns, [:tags], using: "GIN") 30 | create index(:txbox_txns, [:search_vector], using: "GIN") 31 | create index(:txbox_txns, [:block_height]) 32 | create index(:txbox_txns, [:mapi_attempt]) 33 | create index(:txbox_txns, [:inserted_at]) 34 | 35 | 36 | # Create function for populating search vector 37 | execute """ 38 | CREATE OR REPLACE FUNCTION txbox_search_vector_trigger() 39 | RETURNS trigger AS $$ 40 | BEGIN 41 | NEW.search_vector := 42 | setweight(to_tsvector('pg_catalog.english', coalesce(NEW.meta->>'title', '')), 'A') || 43 | setweight(to_tsvector('pg_catalog.english', coalesce(NEW.meta->>'description', '')), 'C') || 44 | setweight(to_tsvector('pg_catalog.english', coalesce(NEW.meta->>'content', '')), 'D') || 45 | setweight(to_tsvector('pg_catalog.english', coalesce(array_to_string(NEW.tags, ','), '')), 'B'); 46 | RETURN NEW; 47 | END 48 | $$ LANGUAGE plpgsql 49 | """ 50 | 51 | # Create trigger for updating search vector 52 | execute """ 53 | CREATE TRIGGER txbox_search_vector_update 54 | BEFORE INSERT OR UPDATE 55 | ON txbox_txns 56 | FOR EACH ROW 57 | EXECUTE PROCEDURE txbox_search_vector_trigger(); 58 | """ 59 | end 60 | 61 | 62 | def down do 63 | drop table(:txbox_txns) 64 | execute "DROP FUNCTION IF EXISTS txbox_search_vector_trigger;" 65 | end 66 | 67 | end -------------------------------------------------------------------------------- /priv/templates/migrations/2_create_txbox_mapi_responses.exs.eex: -------------------------------------------------------------------------------- 1 | defmodule <%= module_prefix %>.Repo.Migrations.CreateTxBoxMapiResponses do 2 | use Ecto.Migration 3 | 4 | def change do 5 | # Create the Txbox MAPI Responses table 6 | create table(:txbox_mapi_responses, primary_key: false) do 7 | add :id, :binary_id, primary_key: true 8 | add :type, :string 9 | add :payload, :map 10 | add :public_key, :string 11 | add :signature, :string 12 | add :verified, :boolean 13 | add :tx_guid, references(:txbox_txns, type: :binary_id, on_delete: :delete_all) 14 | 15 | timestamps(type: :utc_datetime_usec, updated_at: false) 16 | end 17 | 18 | # Add indexes 19 | create index(:txbox_mapi_responses, [:tx_guid]) 20 | create index(:txbox_mapi_responses, [:inserted_at]) 21 | 22 | # Make adjustments to Txbox tx table 23 | rename table(:txbox_txns), :id, to: :guid 24 | alter table(:txbox_txns) do 25 | remove :block_hash, :string 26 | remove :mapi_attempt, :integer 27 | remove :mapi_attempted_at, :utc_datetime 28 | remove :mapi_completed_at, :utc_datetime 29 | remove :status, :map 30 | add :state, :string 31 | end 32 | create index(:txbox_txns, [:state]) 33 | 34 | # Set default state for existing txns 35 | if direction() == :up do 36 | flush() 37 | Txbox.Transactions.repo().update_all(Txbox.Transactions.Tx, set: [state: "pending"]) 38 | end 39 | end 40 | 41 | end 42 | -------------------------------------------------------------------------------- /test/mix/tasks/txbox.gen.migrations_test.exs: -------------------------------------------------------------------------------- 1 | defmodule Mix.Tasks.Txbox.Gen.MigrationsTest do 2 | use ExUnit.Case, async: true 3 | import Mix.Tasks.Txbox.Gen.Migrations, only: [run: 1] 4 | import Txbox.Test.FileHelpers 5 | 6 | @tmp_path Path.join(tmp_path(), inspect(Txbox.Gen.Migrations)) 7 | @migrations_path Path.join(@tmp_path, "migrations") 8 | 9 | defmodule My.Repo do 10 | def __adapter__ do 11 | true 12 | end 13 | 14 | def config do 15 | [priv: Path.join("priv/tmp", inspect(Txbox.Gen.Migrations)), otp_app: :txbox] 16 | end 17 | end 18 | 19 | setup do 20 | create_dir(@migrations_path) 21 | on_exit(fn -> destroy_dir(@tmp_path) end) 22 | :ok 23 | end 24 | 25 | test "generates a new migration" do 26 | run(["-r", to_string(My.Repo)]) 27 | files = File.ls!(@migrations_path) 28 | assert Enum.any?(files, & String.match?(&1, ~r/^\d{14}_setup_txbox\.exs$/)) 29 | assert Enum.any?(files, & String.match?(&1, ~r/^\d{14}_create_txbox_mapi_responses\.exs$/)) 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /test/mocks/mapi-push-failure.json: -------------------------------------------------------------------------------- 1 | {"encoding":"UTF-8","mimetype":"application/json","payload":"{\"apiVersion\":\"0.1.0\",\"timestamp\":\"2020-04-21T13:34:07.051Z\",\"txid\":\"\",\"returnResult\":\"failure\",\"resultDescription\":\"Not enough fees\",\"minerId\":\"03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270\",\"currentHighestBlockHash\":\"000000000000000002a767362337684dfd90cd62389551144bfe60c819f52d7e\",\"currentHighestBlockHeight\":631598,\"txSecondMempoolExpiry\":0}","publicKey":"03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270","signature":"3045022100c8e7f9369545b89c978afc13cc19fc6dd6e1cd139d363a6b808141e2c9fccd2e02202e12f4bf91d10bf7a45191e6fe77f50d7b5351dae7e0613fecc42f61a5736af8"} -------------------------------------------------------------------------------- /test/mocks/mapi-push-success.json: -------------------------------------------------------------------------------- 1 | {"encoding":"UTF-8","mimetype":"application/json","payload":"{\"apiVersion\":\"0.1.0\",\"timestamp\":\"2020-04-21T14:04:39.563Z\",\"txid\":\"9c8c5cf37f4ad1a82891ff647b13ec968f3ccb44af2d9deaa205b03ab70a81fa\",\"returnResult\":\"success\",\"resultDescription\":\"\",\"minerId\":\"03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270\",\"currentHighestBlockHash\":\"00000000000000000397a5a37c1f9b409b4b58e76fd6bcac06db1a3004cccb38\",\"currentHighestBlockHeight\":631603,\"txSecondMempoolExpiry\":0}","publicKey":"03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270","signature":"3045022100a490e469426f34fcf62d0f095c10039cf5a1d535c042172786c364d41de65b3a0220654273ca42b5e955179d617ea8252e64ddf74657aa0caebda7372b40a0f07a53"} -------------------------------------------------------------------------------- /test/mocks/mapi-status-confirmed.json: -------------------------------------------------------------------------------- 1 | {"encoding":"UTF-8","mimetype":"application/json","payload":"{\"apiVersion\":\"0.1.0\",\"timestamp\":\"2020-04-20T21:45:38.808Z\",\"returnResult\":\"success\",\"resultDescription\":\"\",\"blockHash\":\"000000000000000000983dee680071d63939f4690a8a797c022eddadc88f925e\",\"blockHeight\":630712,\"confirmations\":765,\"minerId\":\"03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270\",\"txSecondMempoolExpiry\":0}","publicKey":"03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270","signature":"304502210092b822497cfe065136522b33b0fbec790c77f62818bd252583a615efd35697af022059c4ca7e97c90960860ed9d7b0ff4a1601cfe207b638c672c60a44027aed1f2d"} -------------------------------------------------------------------------------- /test/mocks/mapi-status-mempool.json: -------------------------------------------------------------------------------- 1 | {"encoding":"UTF-8","mimetype":"application/json","payload":"{\"apiVersion\":\"0.1.0\",\"timestamp\":\"2020-04-20T22:31:08.425Z\",\"returnResult\":\"success\",\"resultDescription\":\"\",\"blockHash\":\"\",\"blockHeight\":0,\"confirmations\":0,\"minerId\":\"03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270\",\"txSecondMempoolExpiry\":0}","publicKey":"03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270","signature":"304402200177eddec8d9d886169f400b048c07074b72fa08735f0484e054cbdc693231c20220397f1a60a797f7c8ddc23ad6cfdd53be2f89436f112ca34f1faab8885480ef36"} -------------------------------------------------------------------------------- /test/mocks/mapi-status-notfound.json: -------------------------------------------------------------------------------- 1 | {"encoding":"UTF-8","mimetype":"application/json","payload":"{\"apiVersion\":\"0.1.0\",\"timestamp\":\"2020-04-20T22:27:18.956Z\",\"returnResult\":\"failure\",\"resultDescription\":\"ERROR: No such mempool or blockchain transaction. Use gettransaction for wallet transactions.\",\"blockHash\":null,\"blockHeight\":null,\"confirmations\":0,\"minerId\":\"03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270\",\"txSecondMempoolExpiry\":0}","publicKey":"03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270","signature":"3045022100e3b64eb14523335621bd6111223d200b3b7b13db6c3b68f006287907bc5b1f900220437209ce39b066949330a860ccd3c4e6693877161d398086ba4ae12c37f896e3"} -------------------------------------------------------------------------------- /test/support/case_template.ex: -------------------------------------------------------------------------------- 1 | defmodule Txbox.Test.CaseTemplate do 2 | @moduledoc false 3 | 4 | use ExUnit.CaseTemplate 5 | alias Txbox.Test.Repo 6 | import Txbox.Test.FileHelpers 7 | 8 | @tmp_dir Path.join(tmp_path(), "txbox_test") 9 | 10 | using _opts do 11 | quote do 12 | import Txbox.Test.CaseTemplate 13 | alias Txbox.Test.Repo 14 | end 15 | end 16 | 17 | setup_all do 18 | on_exit(fn -> destroy_dir(@tmp_dir) end) 19 | :ok 20 | end 21 | 22 | setup do 23 | :ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo) 24 | Ecto.Adapters.SQL.Sandbox.mode(Txbox.Test.Repo, {:shared, self()}) 25 | :ok 26 | end 27 | 28 | end 29 | -------------------------------------------------------------------------------- /test/support/file_helpers.ex: -------------------------------------------------------------------------------- 1 | defmodule Txbox.Test.FileHelpers do 2 | @moduledoc false 3 | 4 | def tmp_path do 5 | Path.expand("../../priv/tmp", __DIR__) 6 | end 7 | 8 | def create_dir(path) do 9 | run_if_abs_path(&File.mkdir_p!/1, path) 10 | end 11 | 12 | def destroy_dir(path) do 13 | run_if_abs_path(&File.rm_rf!/1, path) 14 | end 15 | 16 | defp run_if_abs_path(fun, path) do 17 | if path == Path.absname(path) do 18 | fun.(path) 19 | else 20 | raise "Expected an absolute path" 21 | end 22 | end 23 | 24 | end 25 | -------------------------------------------------------------------------------- /test/support/repo.ex: -------------------------------------------------------------------------------- 1 | defmodule Txbox.Test.Repo do 2 | @moduledoc false 3 | 4 | use Ecto.Repo, 5 | otp_app: :txbox, 6 | adapter: Ecto.Adapters.Postgres 7 | 8 | end 9 | -------------------------------------------------------------------------------- /test/test_helper.exs: -------------------------------------------------------------------------------- 1 | {:ok, _pid} = Txbox.Test.Repo.start_link() 2 | ExUnit.start() 3 | -------------------------------------------------------------------------------- /test/txbox/mapi/processor_test.exs: -------------------------------------------------------------------------------- 1 | defmodule Txbox.Mapi.ProcessorTest do 2 | use Txbox.Test.CaseTemplate 3 | alias Txbox.Transactions 4 | alias Txbox.Mapi.{Queue, Processor} 5 | 6 | 7 | def fixture(attrs \\ %{}) do 8 | txid = :crypto.strong_rand_bytes(32) |> Base.encode16(case: :lower) 9 | {:ok, tx} = attrs 10 | |> Map.put(:txid, txid) 11 | |> Transactions.create_tx 12 | tx 13 | end 14 | 15 | 16 | describe "mAPI push tx" do 17 | setup do 18 | {:ok, pid1} = Queue.start_link 19 | {:ok, pid2} = Processor.start_link 20 | :timer.sleep(1) 21 | tx1 = fixture(%{state: "queued", rawtx: "01000000000000000000"}) 22 | tx2 = fixture(%{state: "queued", rawtx: "02000000000000000000"}) 23 | 24 | Tesla.Mock.mock_global fn env -> 25 | cond do 26 | String.match?(env.body,~r/01000000000000000000/) -> 27 | File.read!("test/mocks/mapi-push-success.json") |> Jason.decode! |> Tesla.Mock.json 28 | String.match?(env.body, ~r/02000000000000000000/) -> 29 | File.read!("test/mocks/mapi-push-failure.json") |> Jason.decode! |> Tesla.Mock.json 30 | end 31 | end 32 | 33 | %{pid1: pid1, pid2: pid2, tx1: tx1, tx2: tx2} 34 | end 35 | 36 | test "when mapi returns success, tx state should be pushed", ctx do 37 | Queue.push(ctx.tx1) 38 | :timer.sleep(100) # pause to allow the async http mock to return 39 | tx = Txbox.Transactions.get_tx(ctx.tx1.txid) 40 | assert tx.state == "pushed" 41 | assert is_map(tx.status) 42 | GenStage.stop(ctx.pid2) 43 | GenStage.stop(ctx.pid1) 44 | end 45 | 46 | test "when mapi returns failure, tx state should be failure", ctx do 47 | Queue.push(ctx.tx2) 48 | :timer.sleep(100) # pause to allow the async http mock to return 49 | tx = Txbox.Transactions.get_tx(ctx.tx2.txid) 50 | assert tx.state == "failed" 51 | assert is_map(tx.status) 52 | GenStage.stop(ctx.pid2) 53 | GenStage.stop(ctx.pid1) 54 | end 55 | end 56 | 57 | 58 | describe "mAPI status check " do 59 | setup do 60 | {:ok, pid1} = Queue.start_link 61 | {:ok, pid2} = Processor.start_link 62 | :timer.sleep(1) 63 | tx1 = fixture(%{state: "pushed"}) 64 | tx2 = fixture(%{state: "pushed"}) 65 | tx3 = fixture(%{state: "pushed"}) 66 | 67 | Tesla.Mock.mock_global fn env -> 68 | cond do 69 | String.ends_with?(env.url, tx1.txid) -> 70 | File.read!("test/mocks/mapi-status-confirmed.json") |> Jason.decode! |> Tesla.Mock.json 71 | String.ends_with?(env.url, tx2.txid) -> 72 | File.read!("test/mocks/mapi-status-mempool.json") |> Jason.decode! |> Tesla.Mock.json 73 | String.ends_with?(env.url, tx3.txid) -> 74 | File.read!("test/mocks/mapi-status-notfound.json") |> Jason.decode! |> Tesla.Mock.json 75 | end 76 | end 77 | 78 | %{pid1: pid1, pid2: pid2, tx1: tx1, tx2: tx2, tx3: tx3} 79 | end 80 | 81 | test "when mapi returns tx as confirmed, tx state should be confirmed", ctx do 82 | Queue.push(ctx.tx1) 83 | :timer.sleep(100) # pause to allow the async http mock to return 84 | tx = Txbox.Transactions.get_tx(ctx.tx1.txid) 85 | assert tx.state == "confirmed" 86 | assert is_integer(tx.block_height) 87 | assert is_map(tx.status) 88 | GenStage.stop(ctx.pid2) 89 | GenStage.stop(ctx.pid1) 90 | end 91 | 92 | test "when mapi returns tx as mempool, tx state should be pushed", ctx do 93 | Queue.push(ctx.tx2) 94 | :timer.sleep(100) # pause to allow the async http mock to return 95 | tx = Txbox.Transactions.get_tx(ctx.tx2.txid) 96 | assert tx.state == "pushed" 97 | assert is_map(tx.status) 98 | GenStage.stop(ctx.pid2) 99 | GenStage.stop(ctx.pid1) 100 | end 101 | 102 | test "when mapi returns tx as not found, tx state should be pushed", ctx do 103 | Queue.push(ctx.tx3) 104 | :timer.sleep(100) # pause to allow the async http mock to return 105 | tx = Txbox.Transactions.get_tx(ctx.tx3.txid) 106 | assert tx.state == "pushed" 107 | assert is_map(tx.status) 108 | GenStage.stop(ctx.pid2) 109 | GenStage.stop(ctx.pid1) 110 | end 111 | end 112 | 113 | end 114 | -------------------------------------------------------------------------------- /test/txbox/mapi/queue_test.exs: -------------------------------------------------------------------------------- 1 | defmodule Txbox.Mapi.StatusTest do 2 | use Txbox.Test.CaseTemplate 3 | alias Txbox.Transactions 4 | alias Txbox.Transactions.Tx 5 | alias Txbox.Mapi.Queue 6 | 7 | 8 | def fixture(attrs \\ %{}) do 9 | txid = :crypto.strong_rand_bytes(32) |> Base.encode16(case: :lower) 10 | {:ok, tx} = attrs 11 | |> Map.put(:txid, txid) 12 | |> Transactions.create_tx 13 | tx 14 | end 15 | 16 | 17 | describe "Queue startup" do 18 | setup do 19 | tx1 = fixture() 20 | tx2 = fixture(%{state: "queued"}) 21 | :timer.sleep(1) # pause to ensure inserted_at timestamp increments 22 | tx3 = fixture(%{state: "pushed"}) 23 | tx4 = fixture(%{state: "pushed"}) |> Transactions.update_tx_state("confirmed", %{payload: %{"block_height" => 9000}}) 24 | {:ok, pid} = Queue.start_link 25 | %{pid: pid, tx1: tx1, tx2: tx2, tx3: tx3, tx4: tx4} 26 | end 27 | 28 | test "automatically loads existing queued and pushed txns", ctx do 29 | stream = GenStage.stream([{Queue, max_demand: 1}]) 30 | assert [%Tx{} = tx] = Enum.take(stream, 1) 31 | assert tx.txid == ctx.tx2.txid 32 | assert [%Tx{} = tx] = Enum.take(stream, 1) 33 | assert tx.txid == ctx.tx3.txid 34 | GenStage.stop(ctx.pid) 35 | end 36 | end 37 | 38 | 39 | describe "Queue FIFO" do 40 | setup do 41 | {:ok, pid} = Queue.start_link 42 | :timer.sleep(1) # pause to ensure no async side effects to test 43 | tx1 = fixture(%{state: "queued"}) 44 | tx2 = fixture(%{state: "pushed"}) 45 | %{pid: pid, tx1: tx1, tx2: tx2} 46 | end 47 | 48 | test "txns emitted in order of entry", ctx do 49 | Queue.push(ctx.tx1) 50 | Queue.push(ctx.tx2) 51 | stream = GenStage.stream([{Queue, max_demand: 1}]) 52 | assert [%Tx{} = tx] = Enum.take(stream, 1) 53 | assert tx.txid == ctx.tx1.txid 54 | assert [%Tx{} = tx] = Enum.take(stream, 1) 55 | assert tx.txid == ctx.tx2.txid 56 | GenStage.stop(ctx.pid) 57 | end 58 | end 59 | 60 | end 61 | -------------------------------------------------------------------------------- /test/txbox/transactions_test.exs: -------------------------------------------------------------------------------- 1 | defmodule Txbox.TransactionsTest do 2 | use Txbox.Test.CaseTemplate 3 | alias Txbox.Transactions 4 | alias Txbox.Transactions.{Tx, MapiResponse} 5 | 6 | 7 | def fixture(attrs \\ %{}) do 8 | txid = :crypto.strong_rand_bytes(32) |> Base.encode16(case: :lower) 9 | {:ok, tx} = attrs 10 | |> Map.put(:txid, txid) 11 | |> Transactions.create_tx 12 | tx 13 | end 14 | 15 | 16 | describe "get_tx/2" do 17 | setup do 18 | %{ 19 | tx1: fixture(%{meta: %{title: "test1"}}), 20 | tx2: fixture(%{channel: "testing", meta: %{title: "test2"}}) 21 | } 22 | end 23 | 24 | test "returns a tx", %{tx1: %{txid: txid}} do 25 | assert %Tx{} = tx = Transactions.get_tx(txid) 26 | assert tx.channel == "txbox" 27 | assert tx.meta.title == "test1" 28 | end 29 | 30 | test "returns a tx when querying by channel", %{tx2: %{txid: txid}} do 31 | assert %Tx{} = tx = Tx 32 | |> Transactions.channel("testing") 33 | |> Transactions.get_tx(txid) 34 | assert tx.channel == "testing" 35 | assert tx.meta.title == "test2" 36 | end 37 | 38 | test "returns nil when not found" do 39 | assert Transactions.get_tx("0000000000000000000000000000000000000000000000000000000000000000") == nil 40 | end 41 | end 42 | 43 | 44 | describe "list_tx/1" do 45 | setup do 46 | %{ 47 | tx1: fixture(), 48 | tx2: fixture(%{channel: "testing"}) 49 | } 50 | end 51 | 52 | test "returns all tx", ctx do 53 | txns = Transactions.list_tx() 54 | assert length(txns) == 2 55 | assert Enum.map(txns, & &1.txid) |> Enum.member?(ctx.tx1.txid) 56 | assert Enum.map(txns, & &1.txid) |> Enum.member?(ctx.tx2.txid) 57 | end 58 | 59 | test "returns tx filtered by channel", %{tx2: tx} do 60 | txns = Tx 61 | |> Transactions.channel("testing") 62 | |> Transactions.list_tx 63 | assert length(txns) == 1 64 | assert Enum.map(txns, & &1.txid) |> Enum.member?(tx.txid) 65 | end 66 | 67 | test "returns empty when none found" do 68 | assert [] = Tx 69 | |> Transactions.channel("foobar") 70 | |> Transactions.list_tx 71 | end 72 | end 73 | 74 | 75 | describe "create_tx/1" do 76 | test "returns Tx with valid attributes" do 77 | assert {:ok, %Tx{}} = Transactions.create_tx(%{txid: :crypto.strong_rand_bytes(32) |> Base.encode16}) 78 | end 79 | 80 | test "returns Changeset with invalid attributes" do 81 | assert {:error, %Ecto.Changeset{}} = Transactions.create_tx(%{txid: "foobar"}) 82 | end 83 | end 84 | 85 | 86 | describe "update_tx/2" do 87 | setup do 88 | %{ 89 | tx1: fixture(%{data: %{foo: "bar"}}), 90 | tx2: fixture(%{state: "queued", data: %{foo: "bar"}}) 91 | } 92 | end 93 | 94 | test "returns updated Tx with valid attributes", ctx do 95 | {:ok, %Tx{} = tx} = Transactions.update_tx(ctx.tx1, %{channel: "foobar", data: %{qux: "baz"}}) 96 | assert tx.channel == "foobar" 97 | assert tx.data == %{qux: "baz"} 98 | end 99 | 100 | test "returns Changeset if mutating non-pending transaction", ctx do 101 | assert {:error, %Ecto.Changeset{}} = Transactions.update_tx(ctx.tx2, %{data: %{qux: "baz"}}) 102 | end 103 | 104 | test "returns Changeset with invalid attributes", ctx do 105 | assert {:error, %Ecto.Changeset{}} = Transactions.update_tx(ctx.tx1, %{txid: "foobar"}) 106 | end 107 | end 108 | 109 | describe "update_tx_state/2" do 110 | setup do 111 | %{tx: fixture()} 112 | end 113 | 114 | test "returns updated Tx with allowed state change", ctx do 115 | assert {:ok, %Tx{state: "pushed"}} = Transactions.update_tx_state(ctx.tx, "pushed") 116 | end 117 | 118 | test "returns Changeset if try to set invalid state", ctx do 119 | assert {:error, %Ecto.Changeset{}} = Transactions.update_tx_state(ctx.tx, "confirmed") 120 | end 121 | end 122 | 123 | describe "update_tx_state/3" do 124 | setup do 125 | %{ 126 | tx1: fixture(), 127 | tx2: fixture(%{state: "queued"}), 128 | tx3: fixture(%{state: "pushed"}), 129 | status: %{payload: %{foo: :bar}} 130 | } 131 | end 132 | 133 | test "returns updated Tx with allowed state change and valid status attributes", ctx do 134 | assert {:ok, %Tx{} = tx} = Transactions.update_tx_state(ctx.tx2, "pushed", ctx.status) 135 | assert tx.state == "pushed" 136 | assert tx.status.payload == %{"foo" => "bar"} 137 | end 138 | 139 | test "returns Changeset with invalid state change", ctx do 140 | assert {:error, %{tx: %Ecto.Changeset{}}} = Transactions.update_tx_state(ctx.tx1, "confirmed", ctx.status) 141 | end 142 | 143 | test "returns Changeset with invalid status attributes", %{tx2: tx} do 144 | assert {:error, %{mapi_response: %Ecto.Changeset{}}} = Transactions.update_tx_state(tx, "pushed", %{payload: ""}) 145 | end 146 | 147 | test "always returns the most resent mapi response", %{tx3: tx} do 148 | assert {:ok, %Tx{} = tx} = Transactions.update_tx_state(tx, "pushed", %{payload: %{i: 1}}) 149 | :timer.sleep(1) # pause to ensure inserted_at timestamp increments by nanosecond 150 | assert {:ok, %Tx{} = tx} = Transactions.update_tx_state(tx, "pushed", %{payload: %{i: 2}}) 151 | :timer.sleep(1) # pause to ensure inserted_at timestamp increments by nanosecond 152 | assert {:ok, %Tx{} = tx} = Transactions.update_tx_state(tx, "confirmed", %{payload: %{i: 3}}) 153 | assert tx.state == "confirmed" 154 | assert tx.status.payload == %{"i" => 3} 155 | end 156 | end 157 | 158 | 159 | describe "delete_tx/1" do 160 | setup do 161 | %{tx: fixture()} 162 | end 163 | 164 | test "deletes the given tx", %{tx: tx} do 165 | assert {:ok, %Tx{}} = Transactions.delete_tx(tx) 166 | assert Transactions.get_tx(tx.txid) == nil 167 | end 168 | end 169 | 170 | 171 | describe "list_tx_for_mapi/0" do 172 | setup do 173 | mapi = %{payload: %{foo: :bar}} 174 | tx1 = fixture(%{state: "pending", data: %{n: "tx1"}}) 175 | tx2 = fixture(%{state: "queued", data: %{n: "tx2"}}) 176 | {:ok, tx3} = fixture(%{state: "queued", data: %{n: "tx3"}}) |> Transactions.update_tx_state("failed", mapi) 177 | tx4 = fixture(%{state: "pushed", data: %{n: "tx4"}}) 178 | {:ok, tx5} = fixture(%{state: "pushed", data: %{n: "tx5"}}) |> Transactions.update_tx_state("pushed", mapi) 179 | {:ok, tx6} = fixture(%{state: "pushed", data: %{n: "tx6"}}) |> Transactions.update_tx_state("confirmed", mapi) 180 | tx7 = Enum.reduce(1..20, fixture(%{state: "pushed", data: %{n: "tx7"}}), fn _n, tx -> 181 | case Transactions.update_tx_state(tx, "pushed", mapi) do 182 | {:ok, tx} -> tx 183 | {:error, error} -> raise error 184 | end 185 | end) 186 | 187 | %{tx1: tx1, tx2: tx2, tx3: tx3, tx4: tx4, tx5: tx5, tx6: tx6, tx7: tx7} 188 | end 189 | 190 | test "returns the correct transactions according the the rules", ctx do 191 | txns = Transactions.list_tx_for_mapi() 192 | txids = Enum.map(txns, & &1.txid) 193 | refute Enum.member?(txids, ctx.tx1.txid) 194 | assert Enum.member?(txids, ctx.tx2.txid) 195 | refute Enum.member?(txids, ctx.tx3.txid) 196 | assert Enum.member?(txids, ctx.tx4.txid) 197 | refute Enum.member?(txids, ctx.tx5.txid) 198 | refute Enum.member?(txids, ctx.tx6.txid) 199 | refute Enum.member?(txids, ctx.tx7.txid) 200 | end 201 | 202 | test "includes tx if last status is over 5 mintutes ago", ctx do 203 | datetime = DateTime.now!("Etc/UTC") |> DateTime.add(-600) 204 | Transactions.repo().update_all(MapiResponse, set: [inserted_at: datetime]) 205 | txns = Transactions.list_tx_for_mapi() 206 | txids = Enum.map(txns, & &1.txid) 207 | assert Enum.member?(txids, ctx.tx5.txid) 208 | end 209 | end 210 | 211 | 212 | describe "search_tx/2" do 213 | setup do 214 | %{ 215 | tx1: fixture(%{meta: %{title: "One small step for man"}}), 216 | tx2: fixture(%{meta: %{title: "One giant leap for mankind"}}), 217 | tx3: fixture(%{meta: %{content: "History of mankind"}}), 218 | tx4: fixture(%{tags: ["history"]}) 219 | } 220 | end 221 | 222 | test "searches meta content", ctx do 223 | [tx] = Tx |> Transactions.search_tx("one man") 224 | assert tx.txid == ctx.tx1.txid 225 | end 226 | 227 | test "searches meta content and tags", ctx do 228 | res = Tx |> Transactions.search_tx("history") 229 | assert length(res) == 2 230 | assert Enum.map(res, & &1.txid) |> Enum.member?(ctx.tx3.txid) 231 | assert Enum.map(res, & &1.txid) |> Enum.member?(ctx.tx4.txid) 232 | end 233 | end 234 | 235 | 236 | describe "channel/2" do 237 | setup do 238 | %{ 239 | tx1: fixture(%{channel: "test1"}), 240 | tx2: fixture(%{channel: "test2"}) 241 | } 242 | end 243 | 244 | test "returns tx from the specified channel", ctx do 245 | res = Tx 246 | |> Transactions.channel("test1") 247 | |> Transactions.list_tx 248 | assert Enum.map(res, & &1.txid) |> Enum.member?(ctx.tx1.txid) 249 | refute Enum.map(res, & &1.txid) |> Enum.member?(ctx.tx2.txid) 250 | end 251 | end 252 | 253 | 254 | describe "tagged/2" do 255 | setup do 256 | %{ 257 | tx1: fixture(%{tags: ["foo", "bar", "bas"]}), 258 | tx2: fixture(%{tags: ["bar", "bas", "bish"]}), 259 | tx3: fixture(%{tags: ["bish"]}), 260 | } 261 | end 262 | 263 | test "returns tx with all matching tags", ctx do 264 | assert [tx] = Tx 265 | |> Transactions.tagged(["foo", "bar"]) 266 | |> Transactions.list_tx 267 | assert tx.txid == ctx.tx1.txid 268 | 269 | assert res = Tx 270 | |> Transactions.tagged(["bar", "bas"]) 271 | |> Transactions.list_tx 272 | assert length(res) == 2 273 | assert Enum.map(res, & &1.txid) |> Enum.member?(ctx.tx1.txid) 274 | assert Enum.map(res, & &1.txid) |> Enum.member?(ctx.tx2.txid) 275 | end 276 | end 277 | 278 | 279 | describe "confirmed/2" do 280 | setup do 281 | {:ok, tx2} = fixture(%{state: "pushed"}) |> Transactions.update_tx_state("confirmed", %{payload: %{"block_height" => 9000}}) 282 | %{tx1: fixture(), tx2: tx2} 283 | end 284 | 285 | test "returns confirmed transactions by default", %{tx2: tx} do 286 | txns = Tx 287 | |> Transactions.confirmed() 288 | |> Transactions.list_tx() 289 | assert Enum.map(txns, & &1.txid) |> Enum.member?(tx.txid) 290 | end 291 | 292 | test "returns unconfirmed transactions when set to false", %{tx1: tx} do 293 | txns = Tx 294 | |> Transactions.confirmed(false) 295 | |> Transactions.list_tx() 296 | assert Enum.map(txns, & &1.txid) |> Enum.member?(tx.txid) 297 | end 298 | end 299 | 300 | 301 | describe "with_rawtx/1" do 302 | setup do 303 | %{ 304 | tx1: fixture(%{rawtx: <<1,0,0,0,0,0,0,0,0,0>>}), 305 | tx2: fixture(%{rawtx: <<1,0,0,0,0,0,0,0,0,0>>}) 306 | } 307 | end 308 | 309 | test "and raw_tx/1 by default does not select the rawtx", %{tx1: %{txid: txid}} do 310 | assert %Tx{} = tx = Transactions.get_tx(txid) 311 | assert is_nil(tx.rawtx) 312 | end 313 | 314 | test "and get_tx/1 selects rawtx", %{tx1: %{txid: txid}} do 315 | assert %Tx{} = tx = Tx 316 | |> Transactions.with_rawtx() 317 | |> Transactions.get_tx(txid) 318 | refute is_nil(tx.rawtx) 319 | end 320 | 321 | test "and list_tx/1 by default does not select the rawtx" do 322 | txns = Transactions.list_tx() 323 | assert Enum.all?(txns, & is_nil(&1.rawtx)) 324 | end 325 | 326 | test "and list_tx/1 selects rawtx" do 327 | txns = Tx 328 | |> Transactions.with_rawtx() 329 | |> Transactions.list_tx() 330 | refute Enum.any?(txns, & is_nil(&1.rawtx)) 331 | end 332 | 333 | end 334 | 335 | end 336 | -------------------------------------------------------------------------------- /test/txbox_test.exs: -------------------------------------------------------------------------------- 1 | defmodule TxboxTest do 2 | use Txbox.Test.CaseTemplate 3 | alias Txbox.Transactions 4 | alias Txbox.Transactions.Tx 5 | 6 | 7 | def fixture(attrs \\ %{}) do 8 | txid = :crypto.strong_rand_bytes(32) |> Base.encode16(case: :lower) 9 | {:ok, tx} = attrs 10 | |> Map.put(:txid, txid) 11 | |> Transactions.create_tx 12 | tx 13 | end 14 | 15 | 16 | describe "all/2" do 17 | setup do 18 | tx1 = fixture(%{rawtx: <<1,0,0,0,0,0,0,0,0,0>>, meta: %{title: "test1"}, tags: ["foo"]}) 19 | tx2 = fixture(%{channel: "test", meta: %{title: "test2"}, tags: ["foo", "bar", "baz"]}) 20 | {:ok, tx3} = fixture(%{state: "pushed", meta: %{title: "test3"}}) |> Transactions.update_tx_state("confirmed", %{payload: %{"block_height" => 1}}) 21 | {:ok, tx4} = fixture(%{state: "pushed", meta: %{title: "test4"}}) |> Transactions.update_tx_state("confirmed", %{payload: %{"block_height" => 2}}) 22 | {:ok, tx5} = fixture(%{state: "pushed", meta: %{title: "test5"}}) |> Transactions.update_tx_state("confirmed", %{payload: %{"block_height" => 3}}) 23 | %{tx1: tx1, tx2: tx2, tx3: tx3, tx4: tx4, tx5: tx5} 24 | end 25 | 26 | test "get all tx in default channel" do 27 | assert {:ok, txns} = Txbox.all() 28 | assert length(txns) == 4 29 | end 30 | 31 | test "get all tx in specific channel" do 32 | assert {:ok, txns} = Txbox.all("test") 33 | assert length(txns) == 1 34 | end 35 | 36 | test "get all tx with global channel scope" do 37 | assert {:ok, txns} = Txbox.all("_") 38 | assert length(txns) == 5 39 | assert Enum.all?(txns, & is_nil(&1.rawtx)) 40 | end 41 | 42 | test "get all tx with rawtx selected" do 43 | assert {:ok, txns} = Txbox.all("_", %{rawtx: true}) 44 | assert length(txns) == 5 45 | refute Enum.all?(txns, & is_nil(&1.rawtx)) 46 | end 47 | 48 | test "get all tx with matching tags" do 49 | assert {:ok, txns} = Txbox.all("_", %{tagged: "foo"}) 50 | assert length(txns) == 2 51 | end 52 | 53 | test "get all tx with matching tags as array" do 54 | assert {:ok, txns} = Txbox.all("_", %{tagged: ["foo", "bar"]}) 55 | assert length(txns) == 1 56 | end 57 | 58 | test "get all tx with matching tags as comma seperated list" do 59 | assert {:ok, txns} = Txbox.all("_", %{tagged: "bar, baz"}) 60 | assert length(txns) == 1 61 | end 62 | 63 | test "get all tx from specific height" do 64 | assert {:ok, txns} = Txbox.all(%{from: 2}) 65 | assert length(txns) == 2 66 | assert {:ok, txns} = Txbox.all(%{from: 3}) 67 | assert length(txns) == 1 68 | end 69 | 70 | test "get all tx to specific height" do 71 | assert {:ok, txns} = Txbox.all(%{to: 2}) 72 | assert length(txns) == 2 73 | assert {:ok, txns} = Txbox.all(%{to: 3}) 74 | assert length(txns) == 3 75 | end 76 | 77 | test "get all tx at specific height" do 78 | assert {:ok, txns} = Txbox.all(%{at: 2}) 79 | assert length(txns) == 1 80 | end 81 | 82 | test "get all unconfirmed tx" do 83 | assert {:ok, txns} = Txbox.all("_", %{at: "null"}) 84 | assert length(txns) == 2 85 | end 86 | 87 | test "get all confirmed tx" do 88 | assert {:ok, txns} = Txbox.all(%{at: "-null"}) 89 | assert length(txns) == 3 90 | end 91 | 92 | test "get tx sorted by block height" do 93 | assert {:ok, [tx]} = Txbox.all(%{at: true, order: "i", limit: 1}) 94 | assert tx.meta.title == "test3" 95 | assert {:ok, [tx]} = Txbox.all(%{at: true, order: "-i", limit: 1}) 96 | assert tx.meta.title == "test5" 97 | end 98 | 99 | test "get tx paged and offset" do 100 | assert {:ok, txns} = Txbox.all(%{at: true, order: "i", limit: 2}) 101 | assert length(txns) == 2 102 | assert {:ok, [tx]} = Txbox.all(%{at: true, order: "i", limit: 2, offset: 2}) 103 | assert tx.meta.title == "test5" 104 | end 105 | end 106 | 107 | 108 | describe "all/2 searching" do 109 | setup do 110 | tx1 = fixture(%{meta: %{title: "Hubble Ultra-Deep Field"}, tags: ["photos"]}) 111 | tx2 = fixture(%{channel: "test", meta: %{title: "Bitcoin: A Peer-to-Peer Electronic Cash System"}, tags: ["bsv"]}) 112 | %{tx1: tx1, tx2: tx2} 113 | end 114 | 115 | test "get all tx by the search term" do 116 | assert {:ok, [tx]} = Txbox.all(%{search: "hubble photos"}) 117 | assert tx.meta.title == "Hubble Ultra-Deep Field" 118 | assert {:ok, []} = Txbox.all(%{search: "bitcoin bsv"}) 119 | end 120 | 121 | 122 | test "get all tx by the search term scoped by channel" do 123 | assert {:ok, [tx]} = Txbox.all("test", %{search: "bitcoin bsv"}) 124 | assert tx.meta.title == "Bitcoin: A Peer-to-Peer Electronic Cash System" 125 | assert {:ok, []} = Txbox.all("test", %{search: "hubble photos"}) 126 | end 127 | end 128 | 129 | 130 | describe "find/2" do 131 | setup do 132 | tx1 = fixture(%{rawtx: <<1,0,0,0,0,0,0,0,0,0>>, meta: %{title: "test1"}, tags: ["foo"]}) 133 | tx2 = fixture(%{channel: "test", meta: %{title: "test2"}, tags: ["foo", "bar", "baz"]}) 134 | %{tx1: tx1, tx2: tx2} 135 | end 136 | 137 | test "get a tx in default channel", %{tx1: %{txid: txid}} do 138 | assert {:ok, %Tx{channel: "txbox"} = tx} = Txbox.find(txid) 139 | assert tx.meta.title == "test1" 140 | assert is_nil(tx.rawtx) 141 | end 142 | 143 | test "get a tx with rawtx selected", %{tx1: %{txid: txid}} do 144 | assert {:ok, %Tx{channel: "txbox"} = tx} = Txbox.find(txid, rawtx: true) 145 | assert tx.meta.title == "test1" 146 | refute is_nil(tx.rawtx) 147 | end 148 | 149 | test "get a tx scoped by channel", %{tx2: tx} do 150 | assert {:ok, %Tx{channel: "test"} = tx} = Txbox.find("test", tx.txid) 151 | assert tx.meta.title == "test2" 152 | end 153 | 154 | test "wont find the tx in incorrect channel", %{tx1: tx} do 155 | assert {:error, :not_found} = Txbox.find("test", tx.txid) 156 | end 157 | 158 | test "find the tx with global channel scope", %{tx2: tx} do 159 | assert {:ok, %Tx{channel: "test"} = tx} = Txbox.find("_", tx.txid) 160 | end 161 | 162 | test "wont find the tx in doesnt exist" do 163 | assert {:error, :not_found} = Txbox.find("_", "0000000000000000000000000000000000000000000000000000000000000000") 164 | end 165 | end 166 | 167 | 168 | describe "create/2" do 169 | test "add a tx to default channel" do 170 | txid = :crypto.strong_rand_bytes(32) |> Base.encode16 171 | assert {:ok, %Tx{} = tx} = Txbox.create(%{txid: txid, meta: %{title: "test-a"}}) 172 | assert tx.meta.title == "test-a" 173 | assert tx.channel == "txbox" 174 | end 175 | 176 | test "add a tx to specific channel" do 177 | txid = :crypto.strong_rand_bytes(32) |> Base.encode16 178 | assert {:ok, %Tx{} = tx} = Txbox.create("test", %{txid: txid, meta: %{title: "test-b"}}) 179 | assert tx.meta.title == "test-b" 180 | assert tx.channel == "test" 181 | end 182 | 183 | test "returns error with invalid attributes" do 184 | assert {:error, %{errors: errors}} = Txbox.create(%{}) 185 | assert Keyword.keys(errors) |> Enum.member?(:txid) 186 | end 187 | end 188 | 189 | 190 | describe "update/2" do 191 | setup do 192 | tx1 = fixture(%{state: "pending", meta: %{title: "test1"}}) 193 | tx2 = fixture(%{state: "queued", meta: %{title: "test2"}}) 194 | %{tx1: tx1, tx2: tx2} 195 | end 196 | 197 | test "updates a pending tx with valid params", %{tx1: tx} do 198 | assert {:ok, %Tx{} = tx} = Txbox.update(tx, %{meta: %{title: "test-a"}}) 199 | assert tx.meta.title == "test-a" 200 | end 201 | 202 | test "returns error with invalid params", %{tx1: tx} do 203 | assert {:error, %{errors: errors}} = Txbox.update(tx, %{txid: "zzzzz"}) 204 | assert Keyword.keys(errors) |> Enum.member?(:txid) 205 | end 206 | 207 | test "returns error when updating non-pending tx", %{tx2: tx} do 208 | assert {:error, %{errors: errors}} = Txbox.update(tx, %{meta: %{title: "test-a"}}) 209 | assert Keyword.get(errors, :base) |> elem(0) == "cannot mutate non-pending transaction" 210 | end 211 | end 212 | 213 | end 214 | --------------------------------------------------------------------------------