GithubHelp home page GithubHelp logo

brave-intl / bat-ledger Goto Github PK

View Code? Open in Web Editor NEW
72.0 17.0 24.0 6.59 MB

A BAT-based micropayments system for users and publishers

License: Mozilla Public License 2.0

JavaScript 97.02% Dockerfile 0.15% Shell 2.44% PLpgSQL 0.25% Makefile 0.15%

bat-ledger's Introduction

bat-ledger

BAT back-end servers (ledger, eyeshade, and balance)

Running locally with docker-compose

It is important to configure your .env file before attemptiing to bring up the services . See Prepare .env file below

First, install docker and docker compose.

Check out https://github.com/brave-intl/bat-ledger

You can add any environment variables that need to be set by creating a .env file at the top of the repo. Docker compose will automatically load from this file when launching services.


# To bring up all the services :
    docker-compose up

# Logs from all services presented interleaved, you can press ctrl-c to stop.
# Ledger listens on port 3001, eyeshade on 3002, and balance on 3003

# Note you can run any subset of services (e.g. only eyeshade)
docker-compose up eyeshade-web eyeshade-consumer

# You can also launch and run services in the background
docker-compose up -d eyeshade-web eyeshade-consumer

# And stop running background services with
docker-compose stop

Docker Compose Network Configuration

All containers running within the legders docker-compose context are running in the default network named "ledger". If you need to have other docker containers directly access other containers from within the network (i.e. from WITHIN the container itself, not on your local development context), you can configure your application (i.e. publishers) to join the external network "ledger" by adding the following to the network configuration of the appropriate application's docker compose file

networks:
  ledger:
      external: true

Configuration

Configuration variables are stored as environment preferences. See config.js for a list of these variables for ledger, eyeshade, and balance respectively.

StandardJS

For linting we use StandardJS. It's recommended that you install the necessary IDE plugin. Since this repo uses ES7 features, you'll need a global install of both the standard and babel-eslint packages.

Running tests

Please note: Some tests access live APIs and require auth tokens which are not stored in the repo.

Prepare .env file

  1. Copy example over: cp .env.example .env.
  2. Confirm .env vars match the contents of .github/workflows/ci.yaml section env.
  3. Fill in the remaining {CHANGE_ME} .env vars appropriately; please consult your local BAT dev to find the answers.

Running Individial tests

bat-ledgers is executing tests using ava which can be executed via npm run ava and the requires args for any individual test can be passed to the command using npm scripts args syntax. See below:

npm run ava -- -v -s eyeshade/workers/referrals.test.js

Or, if invoking the container externally,

docker-compose run eyeshade-web npm run ava -- -v -s test/eyeshade/suggestions.integration.test.js

See the github issue where this ability was added

Build local servers

npm run docker-build

Start local servers

npm run docker-up

Postgres migrations

You can run all migrations to upgrade the schema to the latest version using:

npm run docker-migrate-up

You can reverse a particular migration by running:

npm run docker-migrate-down -- migrations/0001_transactions/down.sql

Run tests

best to do in another terminal

npm run docker-test

Testing contribution

If you are testing contributions locally (not with e2e automated tests) you need to add surveyors manually. You can do this by running bellow command when ledger service is running.

curl -X POST --header 'Authorization: Bearer foobarfoobar' --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{"adFree":{"fee":{"USD":5},"votes":50,"altcurrency":"BAT","probi":"27116311373482831368"}}' 'http://127.0.0.1:3001/v2/surveyor/contribution'

Debugging

docker-compose run --rm -p 9229:9229 eyeshade-web npm run start-eyeshade -- --inspect=0.0.0.0

gyp

You may have to install node-gyp if you do not already have it on your machine. Use this document to install: https://github.com/nodejs/node-gyp#installation. Catalina users may have a longer road.

npm install

you may also have to use npm without running the postinstall scripts. use the --ignore-scripts flag.

bat-ledger's People

Contributors

ayumi avatar dependabot[bot] avatar diracdeltas avatar evq avatar genysys avatar husobee avatar jlbyrne avatar maikelmclauflin avatar mihaiplesa avatar mrose17 avatar nejczdovc avatar nvonpentz avatar oauthbringer avatar orspetol avatar renovate[bot] avatar tarikdem avatar thypon avatar tsmartt avatar yachtcaptain23 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bat-ledger's Issues

retire github authentication in eyeshade

we should retire github auth in eyeshade and other services where possible, we're moving toward a publishers based admin panel and our github auth has known issues, e.g. it does not work across dynos:

if the first hit to v1/login (redirects the user to github) and the second (user is redirected back from github) happen on different dynos, the login does not succeed

apps we could remove it for currently:

  • balance
  • helper
  • extractor

remove setup scripts

setup scripts have been abstracted to tests. remove setup folder, update readme

Make code more legible and testable

there are a variety of improvements that could be made to make the code more legible and testable

legible

  • using imports instead of requires and filename patterns
  • breaking apart monolithic files
  • destructuring
  • using default values

testable

  • breaking apart monolithic files
  • more (and exporting existing) pure functions (where available)

these items can be addressed in any order, as well as broken up by server / worker

move source-of-truth for publisher rules to eyeshade

  1. Deprecate these calls in the ledger:

    GET /v2/publisher/identity
    GET /v2/publisher/identity/verified

  2. Move these calls from the ledger to the eyeshade server:

    POST /v2/publisher/ruleset
    PATCH /v2/publisher/rulesets
    PUT /v2/publisher/ruleset/{publisher}
    DELETE /v2/publisher/ruleset/{publisher}
    GET /v2/reports/publisher/rulesets

  3. Add messages from eyeshade to ledger that provide the functionality in the five calls above.

implement swaggerstats equivalent in runtime-prometheus.js

cf., http://swaggerstats.io/apidoc.html#operation/%2Fswagger-stats%2Fmetrics

Name Type Labels Description
api_all_request_total counter - The total number of all API requests received
api_all_success_total counter - The total number of all API requests with success response
api_all_errors_total counter - The total number of all API requests with error response
api_all_client_error_total counter - The total number of all API requests with client error response
api_all_server_error_total counter - The total number of all API requests with server error response
api_all_request_in_processing_total gauge - The total number of all API requests currently in processing (no response yet)
nodejs_process_memory_rss_bytes gauge - Node.js process resident memory (RSS) bytes
nodejs_process_memory_heap_total_bytes gauge - Node.js process memory heapTotal bytes
nodejs_process_memory_heap_used_bytes gauge - Node.js process memory heapUsed bytes
nodejs_process_memory_external_bytes gauge - Node.js process memory external bytes
nodejs_process_cpu_usage_percentage gauge - Node.js process CPU usage percentage
api_request_total counter methodpathcode The total number of all API requests
api_request_duration_milliseconds histogram methodpathle API requests duration
api_request_size_bytes histogram methodpathle API requests size
api_response_size_bytes histogram methodpathle API response size

upgrade old surveyors to un-stick contributions

currently we have a number of ledger clients stuck using old surveyors instead of the current one. it seems most likely that this is caused by brave-intl/bat-client#35 in combination with the fact that some clients reconcile date was not pushed back on grant claim and grants were made unredeemable before their maturity date.

we will fix the client side - to go out in a browser-laptop release but as a quick fix we should unstick clients by upgrading old surveyors servers side on demand.

verify all channels sent to POST /v2/owners by publishers

currently only non-website channels are verified (all are sent by publishers), but with brave-intl/creators-private-issues#940 we will need this change to keep things in sync until we can remove eyeshade's copy of the channel/owner relationships

blacklist fraudulant publishers and do sanity check when creating report

blacklisting publishers currently happens manually on voting document basis
mark publisher's excluded boolean if they are registered in our blacklist
if excluded is true, do not include in payout information
take the publisher, check the blacklist, flip the bool if

create mongodb indeces for blacklist and query them
  eyeshade/workers/wallet.js:voting-report
    mark excluded
  eyeshade/workers/reports.js:report-publishers-contributions
    sanity check to make sure no publishers in report have

Reduce Rate Limit for ledger-web:3001/v1/grants/{grantId}

isolate why this is happening and allow tests to turn off / reduce time during testing

during the script

npm run test-integration

or, if using the docker container

docker-compose run --rm -v $(pwd)/test:/usr/src/app/test ledger-web npm run test-integration

the following error is received. if the endpoint is hit too often

status: 429
http://ledger-web:3001/v1/grants/d6a9c116-5779-4442-a0dc-6237e295ae29
{ statusCode: 429,
  error: 'Too Many Requests',
  message: 'try again in 86400 seconds' }

First request to helper's /v1/rates results in a subset of values

when hitting /v1/rates immediately after the server starts up, a subset of rate conversions are sent back, instead of the full 20, which is denoted in the helper tests.

To reproduce, shut everything down, start up the servers, and run the helper integration test and see it fail the first time because only 8 values are sent back. Sometimes 9 are sent back, depending on if other tests are running in parallel.

Add bat-helper server

and have bat-ledger and bat-eyeshade talk to it instead of the exchange rate services directly

GET /v1/owners/{owner}/wallet returns only settlement info for one channel

To implement https://github.com/brave-intl/creators-private-issues/issues/960 publishers needs to know the total amount an owner earned during the last settlement.

It appears as though a settlement document only has the information for a single channel, and we are only returning one settlement per owner in this request.

entries = await settlements.find({ owner: owner }, { sort: { timestamp: -1 }, limit: 1 })

cc @evq

Support additional uphold conversions to purchase BAT

e.g., DASH, BCH, BTG.

this requires adding the appropriate network address. we probably want to rework the algorithm so that an address generation request is sent to uphold only if the user selects a particular alt-currency (at present, a wallet creation gets addresses for BTC, ETH, and LTC).

create full lifecycle for grant server during tests

during the test for settling a grant, the tester should not have to manage the db before running the tests. manual management can cause the db to be in poor state and for tests to fail. instead, automate by making sure that during a test, a single grant is added to the db, it is used, and subsequently flushed from the redis db by the end of the test.

Proxy ledger publisher verification related endpoints to Publishers app

In the Brave browser, the payments tab has publisher verified green checkmarks. This green checkmark is queried from Ledger.

Instead of Ledger fetching this information from Eyeshade, we're going to fetch the verified_status for an owner from the publishers api.

We'll be disabling the data propagation in a separate task.

Add Promotion creation to Tests

while it is implicit currently, the promotion creation under the npm script create-promotion should be a test that can be isolated and integrated with the rest of the testing flow

Owner accounts can't be easily deleted

Many publishers have requested that their accounts be deleted. We do not have an easy way to do this. Right now we must manually delete the records from both eyeshade and publishers. This is error prone and not scalable.

We should create some endpoint on eyeshade that publishers can hit that deletes all records associated with an owner (owner, their channels, token, etc). Something like this would work:

DELETE /v2/owners/{owner}

Related to https://github.com/brave-intl/creators-private-issues/issues/969

Relation download does not exist

when starting up the extractor worker, the relation download errs in

npm run start-extractor-worker

saying that it does not exist.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.