Giter Site home page Giter Site logo

ledger_sim's Introduction

Ledger Simulator

What is it?

This is a simple simulator of a ledger that will use the same transaction rules as the final chia network. It’s intended to be used by those creating and testing smart transactions.

Python 3.7 or later is required.

Launching

  • create a virtual environment
$ python3 -m venv env
$ ln -s env/bin/activate
$ . activate
$ pip install -e .

Note The -e flag installs in “editable mode” so you can quickly iterate with changes to ledger-sim code. See documentation for more info.

  • launch
$ ledger-sim -h
$ ledger-sim

If you don’t specify a port, the default of 9868 will be selected automatically.

  • install ipython if necessary
$ pip install ipython

We need ipython because it allows await syntax on the command-line.

  • test a client
$ ipython
In [1]: from chiasim.clients.ledger_sim import connect_to_ledger_sim
In [2]: ledger_api = await connect_to_ledger_sim("localhost", 9868)
In [3]: await ledger_api.ping(m="foo")
Out[3]: {'response': "got ping message 'foo' at time 2019-07-17 21:20:49.133717"}
In [4]: await ledger_api.all_unspents()
In [5]: await ledger_api.get_tip()
{'tip_hash': <HeaderPointer: 0000000000000000000000000000000000000000000000000000000000000000>,
 'tip_index': 0,
 'genesis_hash': <HeaderPointer: 0000000000000000000000000000000000000000000000000000000000000000>}
  • create and spend a coin
$ ipython
from chiasim.hack.keys import conditions_for_payment, puzzle_hash_for_index, spend_coin
from chiasim.clients.ledger_sim import connect_to_ledger_sim

ledger_api = await connect_to_ledger_sim("localhost", 9868)
coinbase_puzzle_hash = puzzle_hash_for_index(0)
fees_puzzle_hash = puzzle_hash_for_index(1)
r = await ledger_api.next_block(coinbase_puzzle_hash=coinbase_puzzle_hash, fees_puzzle_hash=fees_puzzle_hash)
body = r["body"]
conditions = conditions_for_payment([(puzzle_hash_for_index(2), 1000)])
tx = spend_coin(body.coinbase_coin, conditions, 0)
r = await ledger_api.push_tx(tx=tx)

ledger_sim's People

Contributors

devrandom avatar matt-o-how avatar richardkiss avatar wbendick avatar yostra avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.