Reference python implementation of Chia pool operations for pool operators

Overview

Pool Reference V1

This code is provided under the Apache 2.0 license. Note: the draft specification is in the SPECIFICATION.md file.

Summary

This repository provides a sample server written in python, which is meant to server as a basis for a Chia Pool. While this is a fully functional implementation, it requires some work in scalability and security to run in production. An FAQ is provided here: https://github.com/Chia-Network/chia-blockchain/wiki/Pooling-FAQ

Customizing

Several things are customizable in this pool reference. This includes:

  • How long the timeout is for leaving the pool
  • How difficulty adjustment happens
  • Fees to take, and how much to pay in blockchain fees
  • How farmers' points are counted when paying (PPS, PPLNS, etc)
  • How farmers receive payouts (XCH, BTC, ETH, etc), and how often
  • What store (DB) is used - by default it's an SQLite db. Users can use their own store implementations, based on AbstractPoolStore, by supplying them to pool_server.start_pool_server

However, some things cannot be changed. These are described in SPECIFICATION.md, and mostly relate to validation, protocol, and the singleton format for smart coins.

Pool Protocol Benefits

The Chia pool protocol has been designed for security, and decentralization, not relying on any 3rd party, closed code, or trusted behaviour.

  • The farmer can never steal from the pool by double farming
  • The farmer does not need collateral to join a pool, they only need a few cents to create a singleton
  • The farmer can easily and securely change pools if they want to
  • The farmer can run a full node (increasing decentralization)
  • The farmer can log into another computer with only the 24 words, and the pooling configuration is detected, without requiring a central server

Pool Protocol Summary

When not pooling, farmers receive signage points from full nodes every 9 seconds, and send these signage points to the harvester. Each signage point is sent along with the sub_slot_iters and difficulty, two network-wide parameters which are adjusted every day (4608 blocks). The sub_slot_iters is the number of VDF iterations performed in 10 minutes for the fastest VDF in the network. This increases if the fastest timelord's speed increases. The dififculty is similiarly affected by timelord speed (it goes up when timelord speed increases, since blocks come faster), but it's also affected by the total amount of space in the network. These two parameters determine how difficult it is to "win" a block and find a proof.

Since only about 1 farmer wordwide finds a proof every 18.75 seconds, this means the chances of finding one are extremely small, with the default difficulty and sub_slot_iters. For pooling, what we do is we increase the sub_slot_iters to a constant, but very high number: 37600000000, and then we decrease the difficulty to an artificially lower one, so that proofs can be found more frequently.

The farmer communicates with one or several pools through an HTTPS protocol, and sets their own local difficulty for each pool. Then, when sending signage points to the harvester, the pool difficulty and sub_slot_iters are used. This means that the farmer can find proofs very often, perhaps every 10 minutes, even for small farmers. These proofs, however, are not sent to the full node to create a block. They are instead only sent to the pool. This means that the other full nodes in the network do not have to see and validate everyone else's proofs, and the network can scale to millions of farmers with no issue, as long as the pool scales properly. Since many farmers are part of the pool, only 1 farmer needs to win a block, for the entire pool to be rewarded proportionally to their space.

The pool then keeps track of how many proofs (partials) each farmer sends, weighing them by difficulty. Occasionally (for example every 3 days), the pool can perform a payout to farmers based on how many partials they submitted. Farmers with more space, and thus more points, will get linearly more rewards.

Instead of farmers using a pool_public_key when plotting, they now use a puzzle hash, referred to as the p2_singleton_puzzle_hash, also known as the pool_contract_address. These values go into the plot itself, and cannot be changed after creating the plot, since they are hashed into the plot_id. The pool contract address is the address of a chialisp contract called a singleton. The farmer must first create a singleton on the blockchain, which stores the pool information of the pool that that singleton is assigned to. When making a plot, the address of that singleton is used, and therefore that plot is tied to that singleton forever. When a block is found by the farmer, the pool portion of the block rewards (7/8, or 1.75XCH) go into the singleton, and when claimed, go directly to the pool's target address.

The farmer can also configure their payout instructions, so that the pool knows where to send the occasional rewards to.

Receiving partials

A partial is a proof of space with some additional metadata and authentication info from the farmer, which meets certain minimum difficulty requirements. Partials must be real proofs of space responding to blockchain signage points, and they must be submitted within the blockchain time window (28 seconds after the signage point).

The pool server works by receiving partials from the users, validating that they are correct and correspond to a valid signage point on the blockchain, and then adding them to a queue. A few minutes later, the pool pulls from the queue, and checks that the signage point for that partial is still in the blockchain. If everything is good, the partial is counted as valid, and the points are added for that farmer.

Collecting pool rewards

Pool absorbing rewards image

The pool periodically searches the blockchain for new pool rewards (1.75 XCH) that go to the various p2_singleton_puzzle_hashes of each of the farmers. These coins are locked, and can only be spent if they are spent along with the singleton that they correspond to. The singleton is also locked to a target_puzzle_hash, which in this diagram is the red pool address. Anyone can spend the singleton and the p2_singleton_puzzle_hash coin, as long as it's a block reward, and all the conditions are met. Some of these conditions require that the singleton always create exactly 1 new child singleton with the same launcher id, and that the coinbase funds are sent to the target_puzzle_hash.

Calculating farmer rewards

Periodically (for example once a day), the pool executes the code in create_payment_loop. This first sums up all the confirmed funds in the pool that have a certain number of confirmations.

Then, the pool divides the total amount by the points of all pool members, to obtain the mojo_per_point (minus the pool fee and the blockchain fee). A new coin gets created for each pool member (and for the pool), and the payments are added to the pending_payments list. Note that since blocks have a maximum size, we have to limit the size of each transaction. There is a configurable parameter: max_additions_per_transaction. After adding the payments to the pending list, the pool members' points are all reset to zero. This logic can be customized.

1/8 vs 7/8

Note that the coinbase rewards in Chia are divided into two coins: the farmer coin and the pool coin. The farmer coin (1/8) only goes to the puzzle hash signed by the farmer private key, while the pool coin (7/8) goes to the pool. The user transaction fees on the blockchain are included in the farmer coin as well. This split of 7/8 1/8 exists to prevent attacks where one pool tries to destroy another by farming partials, but never submitting winning blocks.

##% Difficulty The difficulty allows the pool operator to control how many partials per day they are receiving from each farmer. The difficulty can be adjusted separately for each farmer. A reasonable target would be 300 partials per day, to ensure frequent feedback to the farmer, and low variability. A difficulty of 1 results in approximately 10 partials per day per k32 plot. This is the minimum difficulty that the V1 of the protocol supports is 1. However, a pool may set a higher minimum difficulty for efficiency. When calculating whether a proof is high quality enough for being awarded points, the pool should use sub_slot_iters=37600000000. If the farmer submits a proof that is not good enough for the current difficulty, the pool should respond by setting the current_difficulty in the response.

Points

X points are awarded for submitting a partial with difficulty X, which means that points scale linearly with difficulty. For example, 100 TiB of space should yield approximately 10,000 points per day, whether the difficulty is set to 100 or 200. It should not matter what difficulty is set for a farmer, as long as they are consistently submitting partials. The specification does not require pools to pay out proportionally by points, but the payout scheme should be clear to farmers, and points should be acknowledged and accumulated points returned in the response.

Difficulty adjustment algorithm

This is a simple difficulty adjustment algorithm executed by the pool. The pool can also improve this or change it however they wish. The farmer can provide their own suggested_difficulty, and the pool can decide whether or not to update that farmer's difficulty. Be careful to only accept the latest authentication_public_key when setting difficulty or pool payout info. The initial reference client and pool do not use the suggested_difficulty.

  • Obtain the last successful partial for this launcher id
  • If > 3 hours, divide difficulty by 5
  • If > 45 minutes < 6 hours, divide difficulty by 1.5
  • If < 45 minutes:
    • If have < 300 partials at this difficulty, maintain same difficulty
    • Else, multiply the difficulty by (24 * 3600 / (time taken for 300 partials))

The 6 hours is used to handle rare cases where a farmer's storage drops dramatically. The 45 minutes is similar, but for less extreme cases. Finally, the last case of < 45 minutes should properly handle users with increasing space, or slightly decreasing space. This targets 300 partials per day, but different numbers can be used based on performance and user preference.

Making payments

Note that the payout info is provided with each partial. The user can choose where rewards are paid out to, and this does not have to be an XCH address. The pool should ONLY update the payout info for successful partials with the latest seen authentication key for that launcher_id.

Install and run (Testnet)

To run a pool, you must use this along with the main branch of chia-blockchain.

  1. Checkout the main branch of chia-blockchain, and install it. Checkout this repo in another directory next to (not inside) chia-blockchain.
    If you want to connect to testnet, use export CHIA_ROOT=~/.chia/testnet9, or whichever testnet you want to join, and run chia configure -t true. You can also directly use the pools.testnet9 branch, although this branch will be removed in the future (or past).

  2. Create three keys, one which will be used for the block rewards from the blockchain, one to receive the pool fee that is kept by the pool, and the third to be a wallet that acts as a test user.

  3. Change the wallet_fingerprint and wallet_id in the config.yaml config file, using the information from the first key you created in step 2. These can be obtained by doing chia wallet show.

  4. Do chia keys show and get the first address for each of the keys created in step 2. Put these into the config.yaml config file in default_target_address and pool_fee_address respectively.

  5. Change the pool_url in config.yaml to point to your external ip or hostname. This must match exactly with what the user enters into their UI or CLI, and must start with https://.

  6. Start the node using chia start farmer, and log in to a different key (not the two keys created for the pool). This will be referred to as the farmer's key here. Sync up your wallet on testnet for the farmer key. You can log in to a key by running chia wallet show and then choosing each wallet in turn, to make them start syncing.

  7. Create a venv (different from chia-blockchain) and start the pool server using the following commands:

cd pool-reference
python3 -m venv ./venv
source ./venv/bin/activate
pip install ../chia-blockchain/ 
sudo CHIA_ROOT="/your/home/dir/.chia/testnet9" ./venv/bin/python -m pool

You should see something like this when starting, but no errors:

INFO:root:Logging in: {'fingerprint': 2164248527, 'success': True}
INFO:root:Obtaining balance: {'confirmed_wallet_balance': 0, 'max_send_amount': 0, 'pending_change': 0, 'pending_coin_removal_count': 0, 'spendable_balance': 0, 'unconfirmed_wallet_balance': 0, 'unspent_coin_count': 0, 'wallet_id': 1}
  1. Create a pool nft (on the farmer key) by doing chia plotnft create -s pool -u https://127.0.0.1:80, or whatever host:port you want to use for your pool. Approve it and wait for transaction confirmation. This url must match exactly with what the pool uses.

  2. Do chia plotnft show to ensure that your plotnft is created. Now start making some plots for this pool nft. You can make plots by specifying the -c argument in chia plots create. Make sure to not use the -p argument. The value you should use for -c is the P2 singleton address from chia plotnft show output. You can start with small k25 plots and see if partials are submitted from the farmer to the pool server. The output will be the following in the pool if everything is working:

INFO:root:Returning {'new_difficulty': 1963211364}, time: 0.017535686492919922 singleton: 0x1f8dab79a614a82f9834c8f395f5fe195ae020807169b71a10218b9788a7a573

Please send a message to @sorgente711 on keybase if you have questions about the 9 steps explained above. All other questions should be send to the #pools channel in keybase.

Owner
Chia Network
The green cryptocurrency with Chialisp
Chia Network
A light library to build tiny websites

A light library to build tiny websites

BT.Q 1 Dec 23, 2021
Airplane reservation system python 2

airplane-reservation-system-python-2 Announcement 🔊 : 🔴 IMPORTANT 🔴 : Few new things have been added into the code [16/05/2021] different names is

voyager2005 1 Dec 06, 2021
List of Linux Tools I put on almost every linux / Debian host

Linux-Tools List of Linux Tools I put on almost every Linux / Debian host Installed: geany -- GUI editor/ notepad++ like chkservice -- TUI Linux ser

Stew Alexander 20 Jan 02, 2023
Simple web application, which has a single endpoint, dedicated to annotation parsing and convertion.

Simple web application, which has a single endpoint, dedicated to annotation parsing and conversion.

Pavel Paranin 1 Nov 01, 2021
【幼盾】个性化图片徽章服务!

【幼盾】个性化图片徽章服务! 你对方形的徽章感到无聊了吗?想要定制属于自己的开源项目徽章了吗? 快来使用unv-shield吧! unv-shield提供包含自定义图片的徽章服务,可以让你的项目主页更加个性化!

黄巍 130 Dec 23, 2022
Simulation-Based Inference Benchmark

This repository contains a simulation-based inference benchmark framework, sbibm, which we describe in the associated manuscript "Benchmarking Simulation-based Inference".

SBI Benchmark 58 Oct 13, 2022
poetry2nix turns Poetry projects into Nix derivations without the need to actually write Nix expressions

poetry2nix poetry2nix turns Poetry projects into Nix derivations without the need to actually write Nix expressions. It does so by parsing pyproject.t

Nix community projects 405 Dec 29, 2022
Yet another Airflow plugin using CLI command as RESTful api, supports Airflow v2.X.

中文版文档 Airflow Extended API Plugin Airflow Extended API, which export airflow CLI command as REST-ful API to extend the ability of airflow official API

Eric Cao 106 Nov 09, 2022
Meower a social media platform written in Scratch 3.0 and Python

Meower Meower is a social media platform written in Scratch 3.0 and Python, ported to HTML for self-hosting. Try Beta 4.6 Changelog for 4.6 Start impl

Meower Media Co. 23 Dec 02, 2022
Easily Generate Revolut Business Cards

RevBusinessCardGen Easily Generate Revolut Business Cards Prerequisites Before you begin, ensure you have met the following requirements: You have ins

Younes™ 35 Dec 14, 2022
Persistent/Immutable/Functional data structures for Python

Pyrsistent Pyrsistent is a number of persistent collections (by some referred to as functional data structures). Persistent in the sense that they are

Tobias Gustafsson 1.8k Dec 31, 2022
synchronize projects via yaml/json manifest. built on libvcs

vcspull - synchronize your repos. built on libvcs Manage your commonly used repos from YAML / JSON manifest(s). Compare to myrepos. Great if you use t

python utilities for version control 200 Dec 20, 2022
A feed generator. Currently supports generating RSS feeds from Google, Bing, and Yahoo news.

A feed generator. Currently supports generating RSS feeds from Google, Bing, and Yahoo news.

Josh Cardenzana 0 Dec 13, 2021
An ongoing curated list of frameworks, libraries, learning tutorials, software and resources in Python Language.

Python Development Welcome to the world of Python. An ongoing curated list of frameworks, libraries, learning tutorials, software and resources in Pyt

Paul Veillard 2 Dec 24, 2021
Esercizi di Python svolti per il biennio di Tecnologie Informatiche.

Esercizi di Python Un piccolo aiuto per Sofia che nel 2° quadrimestre inizierà Python :) Questo repository (termine tecnico di Git) puoi trovare tutti

Leonardo Essam Dei Rossi 2 Nov 07, 2022
Sodium is a general purpose programming language which is instruction-oriented (a new programming concept that we are developing and devising) [Still developing...]

Sodium Programming Language Sodium is a general purpose programming language which is instruction-oriented (a new programming concept that we are deve

Instruction Oriented Programming 22 Jan 11, 2022
ripgrep recursively searches directories for a regex pattern while respecting your gitignore

ripgrep (rg) ripgrep is a line-oriented search tool that recursively searches the current directory for a regex pattern. By default, ripgrep will resp

Andrew Gallant 35k Dec 31, 2022
This is an API to get user details for competitive coding platforms - Codeforces, Codechef, SPOJ, Interviewbit. More Platform will be Added Soon.

Competitive-Programming-Score-API An API to get user details for competitive coding platforms - Codeforces, Codechef, SPOJ, Interviewbit Platforms Ava

Aaditya Prakash 3 Jan 17, 2022
Fetch PRs from GitHub and analyze which ones are unmergeable

Set up token Generate a personal access token on GitHub. Add repo permissions. export GH_TOKEN="abcdefg" Pull PR data make Usually, GitHub doesn't h

Stefan van der Walt 1 Nov 05, 2021
Opendrop - An open Apple AirDrop implementation written in Python

OpenDrop: an Open Source AirDrop Implementation OpenDrop is a command-line tool that allows sharing files between devices directly over Wi-Fi. Its uni

Secure Mobile Networking Lab 7.5k Jan 03, 2023