Development

Introduction

Swipechain Core is now written in TypeScript, and it has been using Lerna to manage the development and publication of its packages.

These tools make it easy for us to set up the development environment, everything is handled by scripts provided in the Core's package.json file.

Getting Started

Firstly, you should have git installed.

With git installed, the first step is to clone the repository so we can have access to some important files locally:

git clone https://github.com/SwipeChain/swipechain-core
cd core

For example, the .nvmrc file located at core/.nvmrc will tell us which version of node we should use for development.

If you don't have NodeJS and npm installed, the simplest way to manage both is through following instructions on how to set up nvm for your platform.

Note that the command to install the appropriate version of NodeJS and npm through nvm is:

nvm install [version in .nvmrc file]

Otherwise, you may choose to install a version of NodeJS LTS that has the same major version (10.x or 11.x or 12.x) as the .nvmrc file shows.

The next important required dependency is a global installation of yarn through npm:

npm i -g yarn

Yarn is the package manager used by Swipechain Core; it replaces npm from here on out.

With yarn installed globally, we can proceed to set up the Swipechain Core repository.

Before we install all the JavaScript and TypeScript packages, we need to make sure we are on the develop branch.

If the output of git branch doesn't show 'develop' as the current branch, but shows 'master' in green (current branch), you need to run the following:

git fetch https://github.com/SwipeChain/swipechain-core develop:develop
git checkout develop

This will ensure that your local files reflect those of the Core's development branch.

Finally, we can set up the entire repository with one command:

yarn setup

If you wish to run tests from this point, follow the instructions in the docker guide (use case #1). Then, you may run tests on the whole repository with yarn test.

Just follow those steps and you are ready to get started. Happy Hacking!

Database

Swipechain Core stores all the blockchain data in a database. You could read more about it, in the database section.

For development, you could use our ready-to-use Docker Compose configurations, following the instructions at Docker section.

Starting a Node

If you want to start a node which consists of a relay and forger you can use any of the following commands (inside packages/core).

  • yarn start:mainnet => packages/core/bin/config/networks/mainnet
  • yarn start:devnet => packages/core/bin/config/networks/devnet
  • yarn start:testnet => packages/core/bin/config/networks/testnet

Starting a Relay

If you want to start a relay, you can use any of the following commands (inside packages/core).

  • yarn relay:mainnet => packages/core/bin/config/networks/mainnet
  • yarn relay:devnet => packages/core/bin/config/networks/devnet
  • yarn relay:testnet => packages/core/bin/config/networks/testnet

Starting a Forger

If you want to start a forger, you can use any of the following commands (inside packages/core).

  • yarn forger:mainnet => packages/core/bin/config/networks/mainnet
  • yarn forger:devnet => packages/core/bin/config/networks/devnet
  • yarn forger:testnet => packages/core/bin/config/networks/testnet

Debugging

It is possible to run a variation of these commands that enables the Node debugger:

  • yarn debug:start
  • yarn debug:relay
  • yarn debug:forger

A good introduction about how to use the debugger is the guide to debugging of Node.js.

Tests

Every package that is developed should provide tests to guarantee it gives the expected behavior.

Our tool of choice for tests is Jest by Facebook which provides us with the ability to add custom matchers, snapshot testing and parallelizes our test runs.

All packages have a yarn test command which you should run before sending a PR or pushing to GitHub to make sure all tests are passing. You could use yarn test:watch to listen to changes on the files and run the tests automatically.

Additionally, we provide a variant (yarn test:debug) that enables the Node debugger.

Linting

In order to make everyone life easier to work with the code and guarantee a certain style guide we use Prettier in combination with TSLint.

Before sending any PRs or pushing to GitHub, please make sure to run yarn format to enforce the rules described in tslint.json and .prettierrc.json.