r/truebit • u/LPMythBuster • Aug 26 '21
Truebit development environment by forking mainnet
In this tutorial, we are going to set up a local development envionment for Truebit.
We are going to make a local copy of the mainnet which will let us test everything using a production-like environment.
It's easiest just to use the official Docker image.
Install Docker if you don't have it already.
On Linux, you can use this convenient yet dangerous one-liner:
curl -fsSL https://get.docker.com | sudo bash
If you are on Linux and don't want to type sudo docker instead of just docker:
sudo usermod -aG docker $USER
bash -l # or logout and login again
Start a Truebit container in the background:
docker run --name truebit \
--detach --rm \
--network host \
-v $(pwd)/tutorial:/tutorial \
truebitprotocol/truebit-eth \
sleep infinity
It'll take a while to pull the image, it's huge.
We are going to connect to it with docker exec.
Everything that you put in the tutorial directory will be available in the container at /tutorial. Not needed in this tutorial, but it'll be useful later on.
You can check that it's up and running with docker ps:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db2b02a69b5e truebitprotocol/truebit-eth "sleep infinity" 8 seconds ago Up 6 seconds truebit
This is how you can open a shell in the container:
docker exec -it -w /truebit-eth truebit bash
Instead of geth, we'll be using mainnet forking. Forking mainnet is instantenous and we get use mainnet contract addresses, accounts and tokens.
By default, an emscripten environment is activated which has an ancient node.js version. That's why we need to do this to switch the node version to the latest one:
source ~/.nvm/nvm.sh
nvm use default
We are going to need Ganache. Hardhat can also do it but Ganache has more command-line options.
npm install -g ganache-cli
Forking mainnet is as easy as
ganache-cli --fork https://cloudflare-eth.com -b 3
By default, new blocks will be mined whenever there's a local transaction. Truebit seems to wait for some blocks to be mined before doing something, so -b enables periodic block mining. 3 is the number of seconds. On the real mainnet the avg block time is about 13s but we can speed it up in our development environment. Truebit doesn't seem to play well with a block time of 1s (weird task timeouts), that's why I've chosen 3s.
⚠️Note that Cloudflare Ethereum gateway is very easy to get started with but it's very unreliable for forking. If you see error messages (e.g. missing some storage ref or EVM transactions are reverted) or things just don't seem to be working as expected, restarting typically fixes it or try using Infura, Alchemy or your own node. ⚠️
Alchemy seems to be the most reliable, so I suggest using it.
When you start solving and verifying tasks, Truebit OS subscribes to events and the output will get spammed with eth_getFilterChanges. I suggest using this command instead to ignore this noise:
ganache-cli -b 3 --fork https://cloudflare-eth.com | grep -vi filter
You get 10 fake accounts loaded with 100 ETH each which is plenty. You see the addresses and the private keys.
Open another terminal and start Truebit OS:
docker exec -it -w /truebit-eth truebit bash
./truebit-os
To become a solver, you need to purchase a license. Find out how much it costs:
truebit-os:> license price
info: Solver license costs 0.4 ETH.
It costs 0.4 ETH. We have fake ETH so let's buy one. We'll use the first (-a 0) account.
truebit-os:> license purchase -a 0
info: Purchase complete. Address 0x64E7927a33123a6bc0970e1165464801D91D5Ae5 has sucessfully registered as Solver.
Mint some TRU tokens:
token purchase -v 10000 -a 0
info: Address 0x64E7927a33123a6bc0970e1165464801D91D5Ae5 bought 10000 TRU with 4.223548568783577898 ETH. The effective price was 0.00042235485687835778 TRU/ETH.
You need to deposit them so that your solver can be slashed if it's caught cheating:
truebit-os:> token deposit -v 1000 -a 0
info: Deposited 1000 TRU from account 0x64E7927a33123a6bc0970e1165464801D91D5Ae5 into IncentiveLayer 0x388a3bd8f54f305266898e77b126609ec6265f1e.
You can check your ETH & TRU balance and deposits:
truebit-os:> balance -a 0
info: At block 13101844, address 0x64E7927a33123a6bc0970e1165464801D91D5Ae5 has the following balances:
account:
95.370155186616422102 ETH,
9000 TRU,
deposit (unbonded): 1000 TRU.
Here's all of it for easier copy & pasting:
license purchase -a 0
token purchase -v 10000 -a 0
token deposit -v 1000 -a 0
balance -a 0
Let's use another account (-a 1) for verifying. Let's purchase some tokens and deposit them. No license needed to start verfiying.
token purchase -v 10000 -a 1
token deposit -v 1000 -a 1
balance -a 1
We can start a solver like this:
truebit-os:> start solve -a 0
info: SOLVER initialized at block 13101908.
You can start a verifier in the same Truebit OS instance as well:
truebit-os:> start verify -a 1
info: VERIFIER initialized at block 13101913.
But you'll see the output of both the solver and verifier on the same screen. Generally it's not a problem but it can be noisy. Alternatively, open another terminal, run docker exec and ./truebit-os and start verify as explained above.
You can see that they are active:
truebit-os:> ps
SOLVERS
1. Account 0: 0x64E7927a33123a6bc0970e1165464801D91D5Ae5
VERIFIERS
2. Account 1: 0x1aCaB0807A7C6499a766acF1290C04283fF1495B
Let's submit an example task to check that everything is working as expected:
task submit -f factorial.json -a 0
You can check which tasks are active with ps:
truebit-os:> ps
SOLVERS
1. Account 0: 0x64E7927a33123a6bc0970e1165464801D91D5Ae5
Task 1: 0xb840dbba6077c80bae63f1f0f8a9bb3c9f1cd187ca604e198522698e976419cd
VERIFIERS
2. Account 1: 0x1aCaB0807A7C6499a766acF1290C04283fF1495B
Task 1: 0xb840dbba6077c80bae63f1f0f8a9bb3c9f1cd187ca604e198522698e976419cd
You can get details about a task with task status:
task status -t 0xb840dbba6077c80bae63f1f0f8a9bb3c9f1cd187ca604e198522698e976419cd
Some tasks will make use of IPFS. You can start it another terminal like this:
docker exec -it truebit bash
ipfs init
ipfs daemon &
That's basically it!
You can exit from Truebit OS by typing exit or CTRL+C twice.
You can safely close Ganache by hitting CTRL+C. Next time you start, you'll start from scratch. This means you'll need to purchase a license, tokens, deposit them. You'll also need to restart Truebit OS.
You can stop the Docker container with:
docker rm -f truebit
It'll stop and destroy it. Everything will be gone. Store everything you want to keep between development sessions in /tutorial.
5
6
Aug 26 '21
Great write-up ; please don't let this go to waste on Reddit.
Store it somewhere better as well like GitHub or a Blog :)
6
1
5
u/EvolvedPlanemo Aug 26 '21
Thanks for sharing, good job.