From a7c784bf3946ca973013be09fcc9e15645a68dd8 Mon Sep 17 00:00:00 2001 From: Boris Kuznetsov Date: Tue, 19 Feb 2019 18:28:11 +0300 Subject: [PATCH 1/2] make js look great again --- 01_channels.md | 12 +++++++----- 03_rebalance.md | 2 +- 05_consensus.md | 6 ++++-- 09_receive_and_pay.md | 10 +++++----- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/01_channels.md b/01_channels.md index 2677287..bb59829 100644 --- a/01_channels.md +++ b/01_channels.md @@ -12,7 +12,9 @@ Participants commit a specific state between them to a blockchain to start a cha All users have payment channels with everyone else by default with `insurance=0 ondelta=0 offdelta=0` and no payments can happen, so we don't use the term "open a channel". To start paying through a channel Alice must send a tx onchain that deposits `insurance` into the channel with Bank. Since all tx in Fair are batched, this code will create a new deposit. -`batch.push('depositTo', asset, [amountToDeposit, giveTo, withPartner, invoice])` +```js +batch.push('depositTo', asset, [amountToDeposit, giveTo, withPartner, invoice])` +``` Let's say asset=1 (FRD), amountToDeposit=$6, giveTo=user id 5, withPartner=user id 7 and `invoice` is empty (this field exists for paying large purchases with direct onchain settlement). @@ -26,7 +28,7 @@ For simplicity we want to have deterministic channel where two users can have on Let's say 5 happened to have lower pubkey and is left one. How do we deposit to 5@7? This is actual code: -``` +```js ins.insurance += amount if (compared == -1) ins.ondelta += amount ``` @@ -59,7 +61,7 @@ Alright, now the ondelta is 6, insurance is 6 and offdelta is still 0. This function is used to define what parts of balance are insured/uninsured for you and counterparty. -``` +```js resolveChannel = (insurance, delta, is_left = true) => { var parts = { // left user promises only with negative delta, scenario 3 @@ -116,7 +118,7 @@ resolveChannel = (insurance, delta, is_left = true) => { In order to make our first payment, we must figure out the common canonical representation of a state channel. We don't need to bother to use actual onchain tx like they do in Lightning, we also don't want to send always changing balance of the counterparty like in Raiden. All we care about is offdelta. -``` +```js var state = [ map('disputeWith'), [ @@ -147,7 +149,7 @@ Now both parties know that there's collateral/insurance locked between them onch Internally all payments are expressed in flush_channel and update_channel files. Flush channel finds all pending transitions and applies them on the state, then send the request of following format: -``` +```js me.envelope( map('update'), // denotes that it is an update message asset, // asset id we are operating with diff --git a/03_rebalance.md b/03_rebalance.md index 1589e13..251c03e 100644 --- a/03_rebalance.md +++ b/03_rebalance.md @@ -26,7 +26,7 @@ If Alice or Bob are unresponsive or gone offline, the bank can start a dispute o Now bank has accumulated $1600 in withdrawals, $1550 in deposits, and broadcasts a single batched onchain tx which roughly looks like: -``` +```js [ "batch", nonce... diff --git a/05_consensus.md b/05_consensus.md index 5ee67bf..e24c5df 100644 --- a/05_consensus.md +++ b/05_consensus.md @@ -36,9 +36,11 @@ Now let's try to build the specification around that. Each round we deterministically decide who proposes a new block. That's pretty easy: we take Unix timestamp, divide by blocktime: -`epoch = Math.floor(timestamp() / blocktime)` +```js +epoch = Math.floor(timestamp() / blocktime) -`current_validator = epoch % validators.length` +current_validator = epoch % validators.length +``` This validator must broadcast to everyone else signed block built from tx taken from their mempool, receive signatures back, and as soon as they receive 2/3+ of sigs they can broadcast to all users valid and final block, prepended with commitments from other validators: diff --git a/09_receive_and_pay.md b/09_receive_and_pay.md index 566207d..9282635 100644 --- a/09_receive_and_pay.md +++ b/09_receive_and_pay.md @@ -20,7 +20,7 @@ You can modify the port Fair daemon occupies on your server. Pass `-pXXXX` to th Put something like this helper in util libs: -``` +```js FairRPC = (params, cb) => { http.get('http://127.0.0.1:8002/rpc?auth_code=AUTH_CODE&'+params, (res) => { let rawData = ''; @@ -45,7 +45,7 @@ For someone to pay they need destination address, an amount and optional invoice You can read your own address at bootstrap or have it hardcoded -``` +```js FairRPC('method=getinfo', (r)=>{ console.log('My address', r.address) }) @@ -53,7 +53,7 @@ FairRPC('method=getinfo', (r)=>{ Once you have an address, you can make a button "Pay with Fair" or simply show the address having onclick action. This action should open user's local Fair wallet with all the values prefilled: -``` +```js var fs_origin = 'http://127.0.0.1:8001' deposit.onclick = function(){ fs_w = window.open(fs_origin+'#wallet?invoice='+id+"&address=${address}&amount=10&asset=1") @@ -83,7 +83,7 @@ The user's wallet makes a postMessage event to the opener to notify your app's a The app that integrates Fair should set up a periodic pulling request to the daemon e.g. every 1 second: -``` +```js FairRPC('method=receivedAndFailed', (r)=>{ if (!r.receivedAndFailed) return for (obj of r.receivedAndFailed) { @@ -131,7 +131,7 @@ Then make a request to your local Fair daemon with **following parameters carefu - `params[invoice]` - set the same invoice you would use to receive assets from this user, so if the payment fails it will be credited back according to this invoice. - `params[asset]` - id of asset to operate in. 1 for FRD, 2 for FRB and so on. -``` +```js FairRPC('method=send¶ms[address]=ADDRESS¶ms[asset]=1¶ms[amount]=200¶ms[invoice]=INVOICE', (r)=>{ // sent }) From dbf58b4160eb2b40b8df1fbea6265538b126cf84 Mon Sep 17 00:00:00 2001 From: Boris Kuznetsov Date: Tue, 19 Feb 2019 18:28:44 +0300 Subject: [PATCH 2/2] make shell look great again --- 10_genesis.md | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/10_genesis.md b/10_genesis.md index 194bf1f..7b67c75 100644 --- a/10_genesis.md +++ b/10_genesis.md @@ -22,17 +22,17 @@ It's recommended to use Prettier, you can add it to Sublime Text (don't forget t Perfect way to run new code against old blockchain: -``` -rm -rf fair -id=fair -f=Fair-1.tar.gz -mkdir $id && cd $id && curl https://fairlayer.com/$f -o $f -tar -xzf $f && rm $f -ln -s ~/work/fair/node_modules -ln -s ~/work/fair/wallet/node_modules wallet/node_modules -rm -rf ./src -cp -r ~/work/fair/src src -node fair -p8001 +```sh +$ rm -rf fair +$ id=fair +$ f=Fair-1.tar.gz +$ mkdir $id && cd $id && curl https://fairlayer.com/$f -o $f +$ tar -xzf $f && rm $f +$ ln -s ~/work/fair/node_modules +$ ln -s ~/work/fair/wallet/node_modules wallet/node_modules +$ rm -rf ./src +$ cp -r ~/work/fair/src src +$ node fair -p8001 ``` ## Reading the codebase @@ -59,8 +59,8 @@ Start with `/src/fair.js`. ## Deploy FL -``` -./deploy +```sh +$ ./deploy ``` Ensure https://fairlayer.com/ opens. @@ -68,15 +68,15 @@ Ensure https://fairlayer.com/ opens. #### Troubleshooting ```sh -./l # prints FL logs +$ ./l ``` ## Reconfigure caddy -``` -vi Caddyfile -caddy -service restart +```sh +$ vi Caddyfile +$ caddy -service restart ``` Ensure https://fairlayer.com/ opens. @@ -84,8 +84,8 @@ Ensure https://fairlayer.com/ opens. #### Troubleshooting ```sh -journalctl --boot -u Caddy.service # prints Caddy logs +$ journalctl --boot -u Caddy.service ``` # [2. Step by step](/11_step_by_step.md) / [Home](/README.md)