项目作者: florianmorath

项目描述 :
A fault-tolerant distributed state machine.
高级语言: Python
项目地址: git://github.com/florianmorath/piChain.git
创建时间: 2017-10-05T12:27:37Z
项目社区:https://github.com/florianmorath/piChain

开源协议:MIT License

下载


piChain

piChain is a library which implements a fault-tolerant distributed state machine that inherits features of Paxos, providing strong consistency, and of a blockchain, providing eventual consistency and simplicity. For example the library can be used to keep replicas of a distributed data storage consistent in case of failures.

Requirements

Python 3.6, pip

Installation

It is recommended to install piChain inside a virtual environment.

Setup a virtual environment:

  1. virtualenv --python=python3 <venv-name>
  2. source <venv-name>/bin/activate

Install piChain:

  1. git clone https://github.com/florianmorath/piChain.git
  2. cd piChain
  3. pip install -r requirements.txt
  4. python setup.py install

Note: before running pip install -r requirements.txt you need to have a leveldb instance installed. The installation process depends on the OS. For macOS do the following:

  1. brew install leveldb
  2. CFLAGS='-mmacosx-version-min=10.7 -stdlib=libc++' pip install --no-use-wheel plyvel

Usage

First, one needs to setup the Node instances (each node represents a peer in the network):
A Node constructor takes two arguments, a node index and a peers dictionary. The peers dictionary contains an (ip,port) pair for each node. With the node_index argument one can select which node from the peers dictionary is running “locally”.
The tx_committed field of a Node instance is a callable that is called once a transaction has been committed. By calling start_server() on the Node instance the local node will try to connect to its peers.
Note: start_server() starts the twisted main loop.

  1. from piChain import Node
  2. def tx_committed(commands):
  3. """
  4. Args:
  5. commands (list of str): List of committed Transaction commands.
  6. """
  7. for command in commands:
  8. print('command committed: %s' % command)
  9. def main():
  10. node_index = 0
  11. peers = {
  12. '0': {'ip': '127.0.0.1', 'port': 7982},
  13. '1': {'ip': '127.0.0.1', 'port': 7981},
  14. '2': {'ip': '127.0.0.1', 'port': 7980}
  15. }
  16. node = Node(node_index, peers)
  17. node.tx_committed = tx_committed
  18. node.start_server()

Transactions can be committed by calling make_txn('command') on a Node instance:

  1. node.make_txn('command')

Performance

This plot shows the benchmark results of how many Requests Per Second (RPS) piChain can handle for different cluster sizes.



More information

  • There is a distributed database implementation in the examples folder to demonstrate an application of the piChain package.
  • The API documentation can be build as follows: (requires installation of the piChain package)
    1. pip install sphinx
    2. pip install sphinxcontrib-napoleon
    3. cd docs
    4. make html

Publications