All Projects → openhive-network → hivemind

openhive-network / hivemind

Licence: MIT license
Hive API server (offloads most API calls from hived) implemented using Python+SQL

Programming Languages

python
139335 projects - #7 most used programming language
PLpgSQL
1095 projects
shell
77523 projects
HTML
75241 projects
CMake
9771 projects
Dockerfile
14818 projects

Projects that are alternatives of or similar to hivemind

awesome-hive
A curated list of awesome Hive resources.
Stars: ✭ 20 (-56.52%)
Mutual labels:  hive, communities, dapps
Hive
Fast. Scalable. Powerful. The Blockchain for Web 3.0
Stars: ✭ 142 (+208.7%)
Mutual labels:  hive, decentralization, dapps
Awesome Solidity
⟠ A curated list of awesome Solidity resources, libraries, tools and more
Stars: ✭ 3,078 (+6591.3%)
Mutual labels:  web3, dapps
Congress
Helping humanity evolve
Stars: ✭ 191 (+315.22%)
Mutual labels:  web3, decentralization
start-here
Join our community and build with us. This repository is for Community help and support 💜 ✨
Stars: ✭ 67 (+45.65%)
Mutual labels:  web3, dapps
Unstoppable Wallet Ios
A secure and decentralized Bitcoin and other cryptocurrency wallet for iPhone. Supports Bitcoin, Ethereum, EOS, Binance Chain, Bitcoin Cash, DASH, ...
Stars: ✭ 180 (+291.3%)
Mutual labels:  decentralization, dapps
awesome-waves
Curated list of awesome things for development on Waves blockchain.
Stars: ✭ 60 (+30.43%)
Mutual labels:  web3, dapps
Trust Web3 Provider
Web3 javascript wrapper provider for iOS and Android platforms.
Stars: ✭ 210 (+356.52%)
Mutual labels:  web3, dapps
trust-api
No description or website provided.
Stars: ✭ 20 (-56.52%)
Mutual labels:  web3, dapps
superhighway84
USENET-inspired, uncensorable, decentralized internet discussion system running on IPFS & OrbitDB
Stars: ✭ 437 (+850%)
Mutual labels:  web3, decentralization
platform
L2 solution for seriously fast decentralized applications for the Dash network
Stars: ✭ 24 (-47.83%)
Mutual labels:  decentralization, dapps
micro-stacks
Tiny libraries for building the next generation of Stacks apps.
Stars: ✭ 56 (+21.74%)
Mutual labels:  web3, dapps
dapps-browser
DApps browser
Stars: ✭ 114 (+147.83%)
Mutual labels:  web3, dapps
ipfs-blog
IPFS Blog & News
Stars: ✭ 31 (-32.61%)
Mutual labels:  web3, decentralization
svelteWeb3
A simple, dependency minimized package for building modern dApps with Svelte
Stars: ✭ 14 (-69.57%)
Mutual labels:  web3, dapps
etherbrite
🗓 Clone eventbrite on Ethereum, built in Solidity, TruffleJS, Web3js and React/Redux.
Stars: ✭ 19 (-58.7%)
Mutual labels:  web3
alternative-frontends
🔐🌐 Privacy-respecting web frontends for popular services
Stars: ✭ 821 (+1684.78%)
Mutual labels:  decentralization
waggle-dance
Hive federation service. Enables disparate tables to be concurrently accessed across multiple Hive deployments.
Stars: ✭ 194 (+321.74%)
Mutual labels:  hive
DaFlow
Apache-Spark based Data Flow(ETL) Framework which supports multiple read, write destinations of different types and also support multiple categories of transformation rules.
Stars: ✭ 24 (-47.83%)
Mutual labels:  hive
web-click-flow
网站点击流离线日志分析
Stars: ✭ 14 (-69.57%)
Mutual labels:  hive

Hivemind

Developer-friendly microservice powering social networks on the Hive blockchain.

Hivemind is a "consensus interpretation" layer for the Hive blockchain, maintaining the state of social features such as post feeds, follows, and communities. Written in Python, it synchronizes an SQL database with chain state, providing developers with a more flexible/extensible alternative to the raw hived API.

Development Environment

  • Python 3.6+ required
  • Python setuptools in version >= 57.0, like also pip tool in version >= 21.0
  • Postgres 10+ recommended

Dependencies:

  • OSX: $ brew install python3 postgresql
  • Ubuntu: $ sudo apt-get install python3 python3-pip

Installation:

Try to update Python installation tools first, by specifying:

$ python3 -m pip install --upgrade pip setuptools wheel

If you have too old setup tools, pip spawn will fail with error similar to:

Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    ModuleNotFoundError: No module named 'setuptools'

Before creating the hive database, Hivemind requires the postgresql 'intarray' extension. The postgresql user who has CREATE privilege can load the module with the command CREATE EXTENSION IF NOT EXISTS intarray;.

$ createdb hive
$ export DATABASE_URL=postgresql://user:pass@localhost:5432/hive
$ git clone --recurse-submodules https://gitlab.syncad.com/hive/hivemind.git
$ cd hivemind
$ python3 -m pip install --no-cache-dir --verbose --user . 2>&1 | tee pip_install.log

Updating from an existing hivemind database:

$ cd hivemind
$ python3 -m pip install --no-cache-dir --verbose --user . 2>&1 | tee pip_install.log
$ cd hive/db/sql_scripts
$ ./db_upgrade.sh <user-name> hive

(where is your database login name)

Start the indexer:

$ hive sync
$ hive status
{'db_head_block': 19930833, 'db_head_time': '2018-02-16 21:37:36', 'db_head_age': 10}

Start the server:

$ hive server
$ curl --data '{"jsonrpc":"2.0","id":0,"method":"hive.db_head_state","params":{}}' http://localhost:8080
{"jsonrpc": "2.0", "result": {"db_head_block": 19930795, "db_head_time": "2018-02-16 21:35:42", "db_head_age": 10}, "id": 0}

Run tests:

To run unit tests:

$ make test

To run api tests:

  1. Make sure that current version of hivemind is installed,
  2. Api tests require that hivemind is synced to a node replayed up to 5 000 000 blocks,
  3. Run hivemind in server mode
  4. Set env variables:
$ export HIVEMIND_PORT=8080
$ export HIVEMIND_ADDRESS=127.0.0.1
  1. Run tests using tox:
$ tox -e tavern -- --workers auto --tests-per-worker auto --durations=0

Production Environment

Hivemind is deployed as a Docker container.

Here is an example command that will initialize the database schema and start the syncing process:

docker run -d --name hivemind --env DATABASE_URL=postgresql://user:pass@hostname:5432/databasename --env STEEMD_URL='{"default":"https://yourhivenode"}' --env SYNC_SERVICE=1 -p 8080:8080 hive/hivemind:latest

Be sure to set DATABASE_URL to point to your postgres database and set STEEMD_URL to point to your hived node to sync from.

Once the database is synced, Hivemind will be available for serving requests.

To watch the logs on your console:

docker logs -f hivemind

Configuration

Environment CLI argument Default
LOG_LEVEL --log-level INFO
HTTP_SERVER_PORT --http-server-port 8080
DATABASE_URL --database-url postgresql://user:pass@localhost:5432/hive
STEEMD_URL --steemd-url '{"default":"https://yourhivenode"}'
MAX_BATCH --max-batch 35
MAX_WORKERS --max-workers 6
MAX_RETRIES --max-retries -1
TRAIL_BLOCKS --trail-blocks 2
HIVED_DATABASE_URL --hived-database-url

Precedence: CLI over ENV over hive.conf. Check hive --help for details.

Requirements

Hardware

  • Focus on Postgres performance
  • 2.5GB of memory for hive sync process
  • 500GB storage for database

Hive config

Plugins

  • Required: database_api condenser_api block_api account_history_api account_history_rocksdb
  • Not required: follow*, tags*, market_history, account_history (obsolete, do not use), witness

Postgres Performance

For a system with 16G of memory, here's a good start:

effective_cache_size = 12GB # 50-75% of avail memory
maintenance_work_mem = 2GB
random_page_cost = 1.0      # assuming SSD storage
shared_buffers = 4GB        # 25% of memory
work_mem = 512MB
synchronous_commit = off
checkpoint_completion_target = 0.9
checkpoint_timeout = 30min
max_wal_size = 4GB

JSON-RPC API

The minimum viable API is to remove the requirement for the follow and tags plugins (now rolled into condenser_api) from the backend node while still being able to power condenser's non-wallet features. Thus, this is the core API set:

condenser_api.get_followers
condenser_api.get_following
condenser_api.get_follow_count

condenser_api.get_content
condenser_api.get_content_replies

condenser_api.get_state

condenser_api.get_trending_tags

condenser_api.get_discussions_by_trending
condenser_api.get_discussions_by_hot
condenser_api.get_discussions_by_promoted
condenser_api.get_discussions_by_created

condenser_api.get_discussions_by_blog
condenser_api.get_discussions_by_feed
condenser_api.get_discussions_by_comments
condenser_api.get_replies_by_last_update

condenser_api.get_blog
condenser_api.get_blog_entries
condenser_api.get_discussions_by_author_before_date

Overview

Purpose

Hivemind is a 2nd layer microservice that reads blocks of operations and virtual operations generated by the Hive blockchain network (hived nodes), then organizes the data from these operations into a convenient form for querying by Hive applications. Hivemind's API is focused on providing social media-related information to Hive apps. This includes information about posts, comments, votes, reputation, and Hive user profiles.

Hivemind tracks posts, relationships, social actions, custom operations, and derived states.
  • discussions: by blog, trending, hot, created, etc
  • communities: mod roles/actions, members, feeds (in 1.5; spec)
  • accounts: normalized profile data, reputation
  • feeds: un/follows and un/reblogs
Hivemind does not track most blockchain operations.

For anything to do with wallets, orders, escrow, keys, recovery, or account history, you should query hived.

Hivemind can be extended or leveraged to create:
  • reactions, bookmarks
  • comment on reblogs
  • indexing custom profile data
  • reorganize old posts (categorize, filter, hide/show)
  • voting/polls (democratic or burn/send to vote)
  • modlists: (e.g. spammy, abuse, badtaste)
  • crowdsourced metadata
  • mentions indexing
  • full-text search
  • follow lists
  • bot tracking
  • mini-games
  • community bots

Core indexer

Ingests blocks sequentially, processing operations relevant to accounts, post creations/edits/deletes, and custom_json ops for follows, reblogs, and communities. From these we build account and post lookup tables, follow/reblog state, and communities/members data. Built exclusively from raw blocks, it becomes the ground truth for internal state. Hive does not reimplement logic required for deriving payout values, reputation, and other statistics which are much more easily attained from hived itself in the cache layer.

For efficiency reasons, when first started, hive sync will begin in an "initial sync" mode where it processes in chunks of 1000 blocks at a time until it gets near the current head block, then it will switch to LIVE SYNC mode, where it begins processing blocks one at a time, as they are produced by hive nodes. Before it switches to LIVE SYNC mode, hive sync will create the database indexes necessary for hive server to efficiently process API queries.

Cache layer

Synchronizes the latest state of posts and users, allowing us to serve discussions and lists of posts with all expected information (title, preview, image, payout, votes, etc) without needing hived. This layer is first built once the initial core indexing is complete. Incoming blocks trigger cache updates (including recalculation of trending score) for any posts referenced in comment or vote operations. There is a sweep to paid out posts to ensure they are updated in full with their final state.

API layer

Performs queries against the core and cache tables, merging them into a response in such a way that the frontend will not need to perform any additional calls to hived itself. The initial API simply mimics hived's condenser_api for backwards compatibility, but will be extended to leverage new opportunities and simplify application development.

Fork Resolution

Latency vs. consistency vs. complexity

The easiest way to avoid forks is to only index up to the last irreversible block, but the delay is too much where users expect quick feedback, e.g. votes and live discussions. We can apply the following approach:

  1. Follow the chain as closely to head_block as possible
  2. Indexer trails a few blocks behind, by no more than 6s - 9s
  3. If missed blocks detected, back off from head_block
  4. Database constraints on block linking to detect failure asap
  5. If a fork is encountered between hive_head and steem_head, trivial recovery
  6. Otherwise, pop blocks until in sync. Inconsistent state possible but rare for TRAIL_BLOCKS > 1.
  7. A separate service with a greater follow distance creates periodic snapshots

Documentation

$ make docs && open docs/hive/index.html

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].