-
Notifications
You must be signed in to change notification settings - Fork 1
feat: initial daemon implementation #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
66 commits
Select commit
Hold shift + click to select a range
07b1b3e
feat: initial commit
andreabadesso f47eff9
feat: using generator to download blocks to be able to cancel it when…
andreabadesso e0d6fa4
chore: updated package.json, lock and tsconfig
andreabadesso 7a7b6ab
chore: removed .env from git
andreabadesso f112121
feat: resetting blocks on syncing state entry and moving to idle when…
andreabadesso a1059d5
feat: checking if our best block is still valid (not voided) before c…
andreabadesso 514e50f
refactor: handling errors better by yielding instead of throwing
andreabadesso 501dcc3
refactor: using bi-directional callbacks on state machine instead of …
andreabadesso 2d5831c
feat: using hathorLib Connection class to maintain a connection to th…
andreabadesso 7a431d9
feat: starting the state machine only on START event
andreabadesso 2473f7a
feat: added type to generator and yields
andreabadesso 15b359f
feat: added tests for utils methods
andreabadesso 95018d8
refactor: moved state machine methods to a dedicated file
andreabadesso 8d8b42a
fix: typo when parsing outputs
andreabadesso 92a5d50
feat: added tests for the state machine
andreabadesso a7e9039
chore: added baseUrl to prevent warnings
andreabadesso ace3568
feat: transition to reorg when a reorg is detected
andreabadesso c28cbcb
docs: added README
andreabadesso 4c152de
feat: getting NETWORK from env var
andreabadesso 942a174
fix: handling lambda call failures
andreabadesso 1d2b1f5
chore: removed size.yml github action (came from tsdx template)
andreabadesso 211417e
docs: added comment explaining the block sync strategy on ws events
andreabadesso e8daa1c
docs: added comment explaining state updates from ws conn
andreabadesso 3d3cbed
chore: removed unused and commented import on machine and commented u…
andreabadesso 749d968
docs: added comment on the machine refering to the README
andreabadesso 5184025
refactor: getting constant from hathor-lib and removed useless commen…
andreabadesso cc6b152
feat: typed prepareTx
andreabadesso cf3a623
fix: returning success false if a failure has ocurred inside the tx loop
andreabadesso 0b10072
fix: added missing attributes to PreparedTx type
andreabadesso f054877
docs: added docstring to util methods
andreabadesso a168187
feat: using a LRU cache for transactions
andreabadesso 5d393bc
refactor: removed misleading log
andreabadesso 9354570
feat: loading wallet_service_local_url from env variables
andreabadesso fd11312
docs: added a TODO to replace the getFullNodeBestBlock API
andreabadesso 8bc8790
fix: added missing type to sendTx parameter
andreabadesso 4e30d3f
fix: added missing types and fixed lint on LRU cache
andreabadesso 742f777
Added dockerfile to the project (#4)
andreabadesso bff6a3f
docs: added a simple explaination on how to run locally, build and ru…
andreabadesso cb1a5b3
chore: updated state machine diagram URL to render automatically
andreabadesso 6b27d88
docs: added missing hathor header
andreabadesso d130090
feat: added winston as a logger and refactored console.logs to use it
andreabadesso c5e1e4b
refactor: moved api methods to src/api
andreabadesso 36578e0
fix: winston import breaking production build
andreabadesso c0ce52c
refactor: using wallet util method to get token index
andreabadesso e3ae097
refactor: changed reorg message
andreabadesso e210f76
refactor: improved blocks download message
andreabadesso f654dbe
refactor: using a better algorithm to filter unique txs
andreabadesso 1b3435e
docs: added comment explaining Map queue order
andreabadesso fd840b2
fix: typo
andreabadesso 908acaf
feat: typed yield result from generator
andreabadesso 6d80493
feat: added type and docstring to onReceive from syncHandler
andreabadesso 07b9c1e
refactor: using strict, refactor types to pass tslint
andreabadesso 65f3774
fix: token creation tx was not sending token_name and token_symbol
andreabadesso 09ce983
fix: breaking type on fullnode response
andreabadesso 07c4624
feat: logging full data instead of data.Response on lambda response
andreabadesso 1e78060
tests: removed isVoided from tx
andreabadesso 83d2070
tests: fix typo on tests, clearing all mocks before every test instea…
andreabadesso 9b01f07
tests: fix test by using the hathorLib to define whether a tx is a bl…
andreabadesso 9eb23a9
feat: typed raw tx response from fullnode
andreabadesso 9a37b7f
refactor: typed rawTx and refactored types
andreabadesso 3ff7143
chore: set build output to cjs as the project is node-only
andreabadesso d8b3f67
chore: removed dom from lib
andreabadesso b4c2e15
feat: added spent_by and token to RawOutput and RawInput to handle bl…
andreabadesso 135191c
chore: ignoring some ts errors that can be handled by typing for now.
andreabadesso 3b7f2ea
chore: using strict: true
andreabadesso 7bcdcf7
feat: ignoring genesis transactions before sending them to the wallet…
andreabadesso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| dist/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| root = true | ||
|
|
||
| [*] | ||
| charset = utf-8 | ||
| end_of_line = lf | ||
| indent_size = 2 | ||
| indent_style = space | ||
| insert_final_newline = true | ||
| trim_trailing_whitespace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| name: CI | ||
| on: [push] | ||
| jobs: | ||
| build: | ||
| name: Build, lint, and test on Node ${{ matrix.node }} and ${{ matrix.os }} | ||
|
|
||
| runs-on: ${{ matrix.os }} | ||
| strategy: | ||
| matrix: | ||
| node: ['10.x', '12.x', '14.x'] | ||
| os: [ubuntu-latest, windows-latest, macOS-latest] | ||
|
|
||
| steps: | ||
| - name: Checkout repo | ||
| uses: actions/checkout@v2 | ||
|
|
||
| - name: Use Node ${{ matrix.node }} | ||
| uses: actions/setup-node@v1 | ||
| with: | ||
| node-version: ${{ matrix.node }} | ||
|
|
||
| - name: Install deps and build (with cache) | ||
| uses: bahmutov/npm-install@v1 | ||
|
|
||
| - name: Lint | ||
| run: yarn lint | ||
|
|
||
| - name: Test | ||
| run: yarn test --ci --coverage --maxWorkers=2 | ||
|
|
||
| - name: Build | ||
| run: yarn build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| *.log | ||
| .DS_Store | ||
| node_modules | ||
| dist | ||
| .env |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # Copyright 2020 Hathor Labs | ||
| # This software is provided ‘as-is’, without any express or implied | ||
| # warranty. In no event will the authors be held liable for any damages | ||
| # arising from the use of this software. | ||
| # This software cannot be redistributed unless explicitly agreed in writing with the authors. | ||
|
|
||
| FROM node:14 AS builder | ||
|
|
||
| COPY package.json /app/ | ||
|
|
||
| RUN cd /app && npm install --production | ||
|
|
||
| COPY . /app/ | ||
|
|
||
| RUN cd /app && npm run build | ||
|
|
||
| FROM node:14-alpine3.13 AS builder | ||
|
|
||
| COPY --from=builder /app/dist/ /app/ | ||
| COPY --from=builder /app/node_modules /app/node_modules | ||
|
|
||
| CMD node /app/index.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2020 Hathor Labs | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| # Hathor Wallet Service -- Sync Daemon | ||
|
|
||
| ## Running | ||
|
|
||
| ### Local environment | ||
|
|
||
| #### System dependencies | ||
|
|
||
| You need nodejs installed on your enviroment, we are using the latest Active LTS version (v14.16.1) on the dev environment. You can read more about installing nodejs on https://nodejs.org/en/download/package-manager/ | ||
|
|
||
| #### Clone the project and install dependencies | ||
|
|
||
| `git clone https://github.com/HathorNetwork/hathor-wallet-service-sync_daemon.git && npm install` | ||
|
|
||
| #### Add env variables or an .env file to the repository: | ||
|
|
||
| Example: | ||
|
|
||
| ``` | ||
| STAGE=local | ||
| NETWORK=testnet | ||
| MAX_ADDRESS_GAP=20 | ||
| SERVICE_NAME=hathor-wallet-service | ||
| DEFAULT_SERVER=http://fullnode_url/v1a/ | ||
| ``` | ||
|
|
||
| `STAGE` - Wallet-Service's deployment stage, e.g. `local`, `production`, `staging` | ||
| `NETWORK` - The current hathor network we want to connect to | ||
| `MAX_ADDRESS_GAP` - The full-node configured GAP between addresses | ||
| `SERVICE_NAME` - The Wallet-Service's service name as it was registered on AWS | ||
| `DEFAULT_SERVER` - The full-node API url | ||
|
|
||
| If the wallet-service is not running locally, you also need to specify the AWS-SDK env variables: | ||
|
|
||
| ``` | ||
| AWS_REGION="us-east-1" | ||
| AWS_DEFAULT_REGION="us-east-1" | ||
| AWS_ACCESS_KEY_ID="..." | ||
| AWS_SECRET_ACCESS_KEY="..." | ||
| ``` | ||
|
|
||
| #### Run: | ||
|
|
||
| `npm start` | ||
|
|
||
|
|
||
| ### Deploy | ||
|
|
||
| The recommended way to deploy this service is to use docker. | ||
|
|
||
| #### Building the image: | ||
|
|
||
| `docker build -t hathor/sync-daemon .` | ||
|
|
||
| #### Running: | ||
|
|
||
| ``` | ||
| docker run -d -e STAGE="production" \ | ||
| -e NODE_ENV="production" \ | ||
| -e AWS_REGION="us-east-1" \ | ||
| -e AWS_DEFAULT_REGION="us-east-1" \ | ||
| -e AWS_ACCESS_KEY_ID="..." \ | ||
| -e AWS_SECRET_ACCESS_KEY="..." \ | ||
| -e NETWORK="testnet" \ | ||
| -e MAX_ADDRESS_GAP=20 \ | ||
| -e NETWORK="testnet" \ | ||
| -e SERVICE_NAME="hathor-wallet-service" \ | ||
| -e DEFAULT_SERVER="http://fullnode:8082/v1a/" \ | ||
| -ti localhost/hathor/sync-daemon | ||
| ``` | ||
|
|
||
| In this example, we are passing the env variables to the container and running as a daemon (`-d`). We are also expecting a fullnode to be running on fullnode:8082. | ||
|
|
||
| ## State Machine | ||
|
|
||
| The state machine diagram can be visualized at https://xstate.js.org/viz/?gist=7299c0ed0ce189bc121a06dce1e11638 | ||
|
|
||
| ## States: | ||
|
|
||
| ### Idle | ||
|
|
||
| The machine starts at the idle state, it will stay there until a `NEW_BLOCK` action is received. | ||
|
|
||
| Every time the state of the machine is transitioned to `idle`, the machine will check if `hasMoreBlocks` is set on the state context. If it is, the machine will transition to `syncing`. | ||
|
|
||
| #### Actions: | ||
| `NEW_BLOCK`: When a `NEW_BLOCK` action is received, the machine will transition to the `syncing` state. | ||
|
|
||
| ### Syncing | ||
|
|
||
| Everytime the state of the machine is transitioned to `syncing`, the machine will invoke the `syncHandler` service that will start syncing new blocks. | ||
|
|
||
| #### Actions: | ||
| `NEW_BLOCK`: When a `NEW_BLOCK` action is received, the machine will assign `true` to the `hasMoreBlocks` context on the state, so the next time we transition to `IDLE`, the machine will know that there are more blocks to be downloaded. | ||
| `DONE`: When a `DONE` action is received, the machine will transition to `idle` to await for new blocks | ||
| `ERROR`: When a `ERROR` action is received, the machine will transition to the `failure` state | ||
| `REORG`: When a `REORG` action is received, the machine will transition to the `reorg` state | ||
| `STOP`: When a `STOP` action is received, the machine will transition to the `idle` state | ||
|
|
||
| ### Failure | ||
|
|
||
| This is a `final` state, meaning that the machine will ignore all actions and wait for a manual restart. | ||
|
|
||
| This state can trigger actions to try to automatically solve issues or notify us about it. | ||
|
|
||
| ### Reorg | ||
|
|
||
| This is temporarily a `final` state, this will be changed on a new PR with the reorg code. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.