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)