Commit Graph

20 Commits

Author SHA1 Message Date
Eric Mastro 66912fd811 feat: introduce Task Manager threadpool
The `TaskManager` threadpool is a memory-safe replacement for the `spawnAndSend` operations that are currently causing memory issues in status-desktop.

From a fundamental memory management point of view, `libstatus/settings`, `libstatus/contracts`, and `libstatus/tokens` (custom tokens) have all been converted to `{.threadvar.}`s and `Atomic[bool]`s to maintain the cache and `dirty` flag across threads, respectively, eliminating the need for thread locks and incorrect `{.gcsafe.}` compiler overrides.

The successful [recyclable threadpool experiment from `nim-task-runner`](https://github.com/status-im/nim-task-runner/blob/test/use-cases/test/use_cases/test_sync.nim) using `AsyncChannel[ThreadSafeString]`s was brought over to `status-desktop` and implemented in somewhat of a hardcoded manner, as we knew this would save some time instead of trying to create a fully fleshed out `nim-task-runner` API and build a miraculous macro that may or may not be able to generate the needed API.

The threadpool is started by the `TaskManager` and both the `TaskManager` and the `TaskManager`'s threadpool are started as early as possible in the application lifecycle (in `nim_status_client.nim`). The `TaskManager` creates a thread to run the threadpool. During its initialization, the threadpool then spools up all the threads it will manage and puts them in an idle thread sequence. This is to prevent expensive thread creation and teardown happening during the app's lifetime as it is quite expensive and blocks the main thread. When tasks comes in to the pool, the task is sent to an idle thread, or put in a queue if all threads are busy. The idle thread is moved to the busy thread sequence. When a task is completed, the thread is taken out of the busy threads sequence and moved back in to the sequence of idle threads, effectively recycling it.

The first `spawnAndSend` we were able to change over to the new threadpool was `estimate`, which estimates the gas of a sticker purchase transaction.

From the consumer point of view, the existing `spawnAndSend` to achieve this looks like:
```nim
  proc estimate*(self: StickersView, packId: int, address: string, price: string, uuid: string) {.slot.} =
    let status_stickers = self.status.stickers
    spawnAndSend(self, "setGasEstimate") do:
      var success: bool
      var estimate = status_stickers.estimateGas(packId, address, price, success)
      if not success:
        estimate = 325000
      let result: tuple[estimate: int, uuid: string] = (estimate, uuid)
      Json.encode(result)
```
And the new syntax looks like this:
```nim
  proc estimate*(self: StickersView, packId: int, address: string, price: string, uuid: string) {.slot.} =
    self.status.taskManager.threadPool.stickers.stickerPackPurchaseGasEstimate(cast[pointer](self.vptr), "setGasEstimate", packId, address, price, uuid)
```
The logic inside the `spawnAndSend` body was moved to [src/status/tasks/stickers.nim](https://github.com/status-im/status-desktop/compare/experiment/tasks-3?expand=1#diff-09e57eef00b0cee5c4abdb9039f948d8372e7003e09e934a9b4c7e9167d47658).

This is just the first migration of `spawnAndSend`, however moving the majority of the remaining `spawnAndSend`s will likely just be an exercise in copy/pasta. There will be one or two that may require a bit more thinking, depending how they rely on data from the model.

Once the `spawnAndSend`s have been converted to the threadpool, we can start implementing the [long-running process from the task runner use case experiments](https://github.com/status-im/nim-task-runner/blob/test/use-cases/test/use_cases/test_long_running.nim).

And finally, we can then implement the [async tasks](https://github.com/status-im/nim-task-runner/blob/test/use-cases/test/use_cases/test_async.nim) if needed.

@michaelsbradleyjr and I spent many hours digging in to the depths of nim's memory management in an attempt to understand it. We have created [a presentation with our task runner experiment findings](https://docs.google.com/presentation/d/1ItCxAfsVTcIoH_E4bgvmHljhbU-tC3T6K2A6ahwAedk/edit?usp=sharing), and @michaelsbradleyjr has spent time [answering questions off the back of that presentation.](https://gist.github.com/michaelsbradleyjr/1eaa9937b3fbb4ffff3fb814f0dd82a9).

We have created a fork of `edn.nim` at `status-im/edn.nim` and we need the PR to be merged and the commit hash updated before we can merge this PR in to `status-desktop`.
2021-03-18 13:15:05 -04:00
Michael Bradley, Jr c4e786bae3 build: add vendor/nim-status-go submodule 2021-03-16 16:37:27 -04:00
Michael Bradley, Jr 470d2e5a17 build: remove vendor/nim-status submodule 2021-03-16 16:37:27 -04:00
Richard Ramos e8183f3a9f feat: use base32 + status infura ipfs for ens contenthash 2020-10-20 14:42:36 -04:00
emizzle e58f5b03eb refactor: make event emitter threadsafe 2020-10-16 10:45:24 -04:00
emizzle 078e9758f0 refactor: use nim-web3 library
Use nim-web3 library and remove internal encoding funcs that were copied from nim-web3.

Remove all instances of EthAddress (and therefore eth/common/eth_types imports)
2020-09-29 13:28:08 -04:00
Richard Ramos 75cb28b24d adding missing deps 2020-09-04 15:57:29 -04:00
Richard Ramos 74a38c671d feat: use nim-status instead of libstatus 2020-07-17 12:59:20 -04:00
Michael Bradley, Jr 4291754283 fix: add vendor/edn.nim to gitmodules 2020-07-14 16:47:20 -05:00
Richard Ramos 637484bd79 feat: show qr codes 2020-06-23 18:52:54 -04:00
Ștefan Talpalaru 150f38b87d remove vendor/nim-nat-traversal
and fix/hide some warnings
2020-06-23 07:37:17 -04:00
Jonathan Rainville eff29af548 feat: get collectibles from the contracts and their respective apis
With collaborative work from @emizzle
2020-06-18 10:55:48 -04:00
emizzle 87c603097e feat: Decode sticker hashes
Decode multihash content identifier (CID) using `libp2p` libraries. Makes some assumptions that we only handle `ipfs-ns` codec hashes and that the sticker hash starts with `e3xxxxyy`, where xxxx is essentially ignore and yy indicates the cid codec.
2020-06-10 14:52:45 -04:00
Ștefan Talpalaru 24505c9b0f .gitmodules: fix URLs so `make github-ssh` works as intended 2020-05-29 12:31:00 -04:00
Pascal Precht 985b5d93d0 fix(ui/wallet): don't require password to be entered as hashed hex 2020-05-21 19:50:58 -04:00
emizzle dafd11fbc0 feat: onboarding generate new account
Generates 5 random accounts with identicons, allows user to enter password, then stores the account and logs in to the statusgo node.

Add EventEmitter that notifies nim_status_client.nim once node has started and is logged in (likely needs some refactoring to include the eventemitter in the base controller class).

Add QML StateMachine for the onboarding view.

Add nimcrytpo, uuids, eventemitter, isaac dependencies via submodules.

Add button to Intro view to randomly gen account.
2020-05-21 19:33:14 -04:00
Richard Ramos 1a92cda0d9 feat: add nim-chronicles 2020-05-21 15:16:24 -04:00
Richard Ramos a80a6c623b fix: signal handler integration with status-go 2020-05-18 11:42:50 -04:00
Richard Ramos ce41388a51 AppImg packaging 2020-05-15 17:18:20 -04:00
Richard Ramos c1d1b60f46 Adding nimbus-build-system 2020-05-15 17:18:20 -04:00