No project description provided
Project description
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 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
pip install cpr-gym
If this worked, you are ready to go. The following snippet simulates 2016 steps of honest behaviour in Nakamoto consensus.
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.
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.
The editable install will automatically invoke dune
to keep the
cpr_gym_engine.so
up to date. If you change the OCaml code for the
simulator, these changes will be reflected in the Python gym
environment. Updating the cpr_gym_engine.so
happens during import
,
so you'll have to restart your kernel if you work from a notebook.
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
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for cpr_gym-0.6.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a52705b5085e0fe0a2f8145db13d21018a624065dc4ff1b4f15df33e6da64c35 |
|
MD5 | 72f7c9aab183df7bff70aa1a8b3992f7 |
|
BLAKE2b-256 | bc65fe53af6f464c957b8c7678d5673a5df88b53e724371ed398b6f77cc10e75 |
Hashes for cpr_gym-0.6.3-cp39-abi3-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04606f47c512fb283d01ff0ba3c1af6cefc975f0278a58b905fb50c7aeefb993 |
|
MD5 | 31b66601437e6d70390c07d08c8fa996 |
|
BLAKE2b-256 | e72381bbfcfec8f92b9713412c1671e70da56f99e1cc165910d7ec547bf6d904 |