# Consensus Protocol Research
CPR is a toolbox for specifying, simulating, and attacking proof-of-work
consensus protocols. In this repository you find
- protocol specifications for Bitcoin, Ethereum PoW, and others,
- implementations of known attacks against these protocols,
- a simulator that executes the specified protocols and attacks in a,
virtual environment,
- tooling for automatic attack-search with reinforcement learning (RL) and
- evaluation scripts and notebooks for the above.
I'm working on [a website](https://pkel.github.io/cpr/) with more details.
## Python/RL Quickstart
CPR provides an OpenAI Gym environment for attack search with Python RL
frameworks. If you meet the following **requirements**, you can install
it from PyPI.
- Unix-like operating system with x86_64 support
- CPython, version >= 3.9
```shell
pip install cpr-gym
```
If this worked, you are ready to go. The following snippet simulates
2016 steps of honest behaviour in Nakamoto consensus.
```python
import gym
import cpr_gym
env = gym.make("cpr-nakamoto-v0", episode_len = 2016)
obs = env.reset()
done = False
while not done:
action = env.policy(obs, "honest")
obs, rew, done, info = env.step(action)
```
## Install from Source
The protocol specifications and simulator are OCaml programs. Also most
parts of the Gym environment are written in OCaml. The Python module
`cpr_gym` loads the OCaml code from a pre-compiled shared object named
`cpr_gym_engine.so`. In order to install the package from source, you
have to build this shared object and hence have the OCaml toolchain
installed.
**Opam** is the OCaml package manager. It's a bit like Python's `pip` or
Javascript's `npm`. We use it do download and install our OCaml
dependencies and to manage different versions of the OCaml compiler.
Make sure that a recent version (>= 2.0) is installed on your system.
[Follow these instructions](https://opam.ocaml.org/doc/Install.html).
Then use `make setup` to get compiler and dependencies setup in the
current working directory under `_opam`. Later, e.g. when dependencies
change, run `make dependencies` to update the toolchain. If you ever
suspect that the OCaml dependencies are broken, and you do not know how
to fix it, delete the `_opam` directory and run `make setup` again.
**Dune** is an OCaml build system. We use it to build executables and
shared objects, and to run tests. You do not have to interact with dune
directly. Just run `make build` to test whether the OCaml build works.
Now, installing `cpr_gym` as editable Python package should work. Try
`pip install -e .` and follow the short Python example above. If it
works, you're ready to go.
`import cpr_gym` tries to detect editable installs. If so,
`cpr_gym_engine.so` is loaded from the OCaml build directory
(`./_build`). You can rebuild the DLL with `make build`.
It might be useful to install all Python development dependencies with
`pip install -r requirements.txt`. Afterwards, you can run the full test
suite, OCaml and Python, with `make test`.
Raw data
{
"_id": null,
"home_page": "https://github.com/pkel/cpr",
"name": "cpr-gym",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "proof-of-work consensus rl gym selfish-mining reinforcement-learning",
"author": "Patrik Keller",
"author_email": "git@pkel.dev",
"download_url": "",
"platform": null,
"description": "# Consensus Protocol Research\n\nCPR is a toolbox for specifying, simulating, and attacking proof-of-work\nconsensus protocols. In this repository you find\n- protocol specifications for Bitcoin, Ethereum PoW, and others,\n- implementations of known attacks against these protocols,\n- a simulator that executes the specified protocols and attacks in a,\n virtual environment,\n- tooling for automatic attack-search with reinforcement learning (RL) and\n- evaluation scripts and notebooks for the above.\n\nI'm working on [a website](https://pkel.github.io/cpr/) with more details.\n\n## Python/RL Quickstart\n\nCPR provides an OpenAI Gym environment for attack search with Python RL\nframeworks. If you meet the following **requirements**, you can install\nit from PyPI.\n\n- Unix-like operating system with x86_64 support\n- CPython, version >= 3.9\n\n```shell\npip install cpr-gym\n```\n\nIf this worked, you are ready to go. The following snippet simulates\n2016 steps of honest behaviour in Nakamoto consensus.\n\n```python\nimport gym\nimport cpr_gym\n\nenv = gym.make(\"cpr-nakamoto-v0\", episode_len = 2016)\nobs = env.reset()\ndone = False\nwhile not done:\n action = env.policy(obs, \"honest\")\n obs, rew, done, info = env.step(action)\n```\n\n## Install from Source\n\nThe protocol specifications and simulator are OCaml programs. Also most\nparts of the Gym environment are written in OCaml. The Python module\n`cpr_gym` loads the OCaml code from a pre-compiled shared object named\n`cpr_gym_engine.so`. In order to install the package from source, you\nhave to build this shared object and hence have the OCaml toolchain\ninstalled.\n\n**Opam** is the OCaml package manager. It's a bit like Python's `pip` or\nJavascript's `npm`. We use it do download and install our OCaml\ndependencies and to manage different versions of the OCaml compiler.\nMake sure that a recent version (>= 2.0) is installed on your system.\n[Follow these instructions](https://opam.ocaml.org/doc/Install.html).\nThen use `make setup` to get compiler and dependencies setup in the\ncurrent working directory under `_opam`. Later, e.g. when dependencies\nchange, run `make dependencies` to update the toolchain. If you ever\nsuspect that the OCaml dependencies are broken, and you do not know how\nto fix it, delete the `_opam` directory and run `make setup` again.\n\n**Dune** is an OCaml build system. We use it to build executables and\nshared objects, and to run tests. You do not have to interact with dune\ndirectly. Just run `make build` to test whether the OCaml build works.\n\nNow, installing `cpr_gym` as editable Python package should work. Try\n`pip install -e .` and follow the short Python example above. If it\nworks, you're ready to go.\n\n`import cpr_gym` tries to detect editable installs. If so,\n`cpr_gym_engine.so` is loaded from the OCaml build directory\n(`./_build`). You can rebuild the DLL with `make build`.\n\nIt might be useful to install all Python development dependencies with\n`pip install -r requirements.txt`. Afterwards, you can run the full test\nsuite, OCaml and Python, with `make test`.\n",
"bugtrack_url": null,
"license": "",
"summary": "Gym environment for attacking proof-of-work protocols with RL",
"version": "0.7.0",
"split_keywords": [
"proof-of-work",
"consensus",
"rl",
"gym",
"selfish-mining",
"reinforcement-learning"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "bc3475dd622494cfb0668befcc6c589d",
"sha256": "3b822ac9dcaaed1f9c8255059a55b53807f8358ca98be70b258cfa483d64a4cb"
},
"downloads": -1,
"filename": "cpr_gym-0.7.0-cp39-abi3-macosx_10_9_x86_64.whl",
"has_sig": false,
"md5_digest": "bc3475dd622494cfb0668befcc6c589d",
"packagetype": "bdist_wheel",
"python_version": "cp39",
"requires_python": null,
"size": 6156534,
"upload_time": "2022-12-21T10:11:41",
"upload_time_iso_8601": "2022-12-21T10:11:41.962207Z",
"url": "https://files.pythonhosted.org/packages/c0/3b/ec5c8e4c58c89570f7a196abaa28d3b03676f463639d4900903507a455f7/cpr_gym-0.7.0-cp39-abi3-macosx_10_9_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "d9f8dab8105a38195b726a2a5754bc2a",
"sha256": "fe4cd23732460660ee4ba99db886cfc41da0115c82fabc6518024072b1e0231f"
},
"downloads": -1,
"filename": "cpr_gym-0.7.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"has_sig": false,
"md5_digest": "d9f8dab8105a38195b726a2a5754bc2a",
"packagetype": "bdist_wheel",
"python_version": "cp39",
"requires_python": null,
"size": 8471680,
"upload_time": "2022-12-21T10:11:43",
"upload_time_iso_8601": "2022-12-21T10:11:43.558691Z",
"url": "https://files.pythonhosted.org/packages/b5/f2/259c9c1907157e0ae9a16b16ad569515a13f651987c0fa9d95403aa26597/cpr_gym-0.7.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-12-21 10:11:41",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "pkel",
"github_project": "cpr",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "black",
"specs": []
},
{
"name": "flake8",
"specs": []
},
{
"name": "flake8-pyproject",
"specs": []
},
{
"name": "pytest",
"specs": []
},
{
"name": "pytest-benchmark",
"specs": []
},
{
"name": "pytest-forked",
"specs": []
},
{
"name": "pandas",
"specs": []
},
{
"name": "pandarallel",
"specs": []
},
{
"name": "pydantic",
"specs": []
},
{
"name": "numpy",
"specs": []
},
{
"name": "jupyter",
"specs": []
},
{
"name": "matplotlib",
"specs": []
},
{
"name": "tqdm",
"specs": []
},
{
"name": "scikit-optimize",
"specs": []
},
{
"name": "seaborn",
"specs": []
},
{
"name": "networkx",
"specs": []
},
{
"name": null,
"specs": []
},
{
"name": "stable-baselines3",
"specs": []
},
{
"name": "wandb",
"specs": []
}
],
"lcname": "cpr-gym"
}