Fun And Games With The Libra Testnet
What better way is there to spend Friday night than playing around with Facebook’s new cryptocurrency, Libra?
Summary
This article walks through some of the things you can do with the Libra command line interface and even gives you some tools to automate submitting commands to it. They’re tools I’ve used in the past to automate CLIs that don’t have an API to make life easier, but you will have to be familiar with Linux command prompts and a bit of bash scripting.
Update: now with sample scripts in Github.
Back to Libra
The actual currency isn’t live yet, but there are a code base and a testnet, which gives you plenty of opportunities to play about. If like me, you use a Ubuntu Linux box, you can get started fairly quickly by opening up a terminal (Ctrl+Alt+T) and entering the following:
$ git clone https://github.com/libra/libra.git
$ cd libra
$ ./scripts/dev_setup.sh
$ ./scripts/cli/start_cli_testnet.sh
And then you’re in a CLI with a libra%
prompt, connected to the testnet and you can start playing.
Addresses and Seeds
One of the neat things about the CLI is that you don’t need to use those long strings that represent the blockchain addresses. If you create five addresses, they are referenced with an index, i.e. 0 through to 4.
So the first thing I did, after manually trying out the initial tutorial, was write out the address mnemonic seed to disk:
libra% a w seed.txt
The seed is a collection of 24 words that are used to generate your wallet addresses, followed by a semi-colon and a number, for example:
correct horse battery staple flight banana car gumboot plug bread streamer handle ice blind letter potato pizza foot dish elbow can chair carpet spray;5
Of course the above isn’t my mnemonic — it’s just something I made up. The number at the end is how many addresses to generate. You can keep generating more and more. I edited it to 500 and imported the seed file, which initially caused some errors (see this Libra github issue), but they seem to have followed my suggestion and it’s now closed.
Now the problem I have is that I don’t know Rust, and don’t really feel like learning it tonight. Furthermore, the start_cli_testnet.sh
script isn’t just an executable: it does a load of compiling before dropping you into the Libra command prompt. And there doesn’t appear to be an API, so programmatically driving the client system isn’t just a matter of writing a neat Python script.
Automatically driving the CLI
Fortunately, Linux has this excellent tool called xdotool
. Simply open up a new terminal (you’re going to need this shortly anyway) and install it with:
$ sudo apt-get update$ sudo apt-get install xdotool
This little tool allows you to issue commands at the prompt that do all sorts of nifty stuff, like move windows around, shift focus to a different window, or emulate key presses. As a result, you can build a bash script with it that drives other applications. It’s quick and dirty, but it works.
Right, now you need to get the window ID of your terminal that is running the Libra CLI. From now on, if the code snippet has a $
prompt you should enter that stuff in the new terminal, and if it has a libra%
prompt it should go in the Libra CLI terminal.
Anyway, back to the window ID question. Simply run:
$ xdotool search --name "/libra"
You’ll get an eight digit number like 67108874, which is the ID of the Libra CLI terminal. If for some reason you get more than one, try the following command with each of the numbers until your Libra terminal jumps up into the top-left corner:
$ xdotool windowmove 67108874 0 0
(Replace the ID number in the above example with each of the results the xdotool search command returned).
Now you can do clever command line stuff like this in your terminal:
$ xdotool windowactivate 67108874; sleep 1; xdotool type 'a m 0 100'; xdotool key KP_Enter;
It moves the focus to the Libra terminal, waits a second for the focus to arrive, does the mechanical equivalent of you typing “a m 0 100” — which mints 100 libra to your first address, and then presses enter. So now we have a way to drive the Libra CLI in the first terminal with commands from the second, and that means we can start scripting!
Making a Mint
In the examples that follow I use bash, because it’s quick and easy, but you can equally write a Python script, for example, and call xdotool from it (e.g. using subprocess.check_output). So here’s a script that generates 100 new addresses and then tries to mint the maximum amount of coins for them:
#!/bin/bash
xdotool windowactivate 67108874 # change this to your terminal ID
for i in {0..99}
do
sleep 1
xdotool type "a c"
xdotool key KP_Enter;
sleep 1
xdotool type "a m $i 10000000000000"
xdotool key KP_Enter;
sleep 30
done
Save that to a text file, for example automint.sh
, make it executable with chmod +x automint.sh
and run it from the second terminal.
Incidentally, ten trillion libras is the most you can mint in one go.
You need the sleep commands because you soon start hitting “429 Too Many Requests” errors if you start hammering the server. They have to stop denial of service attacks somehow. Thirty seconds seems to work fairly well though.
And so, after just under an hour (I went off and watched an episode of Jessica Jones), you should have about a quadrillion (i.e. 1000 trillion) libras.
Unfortunately, they’re only testnet libras, and so have no value. But there’s a lot of them…
Move Your Money
How about we try to move our fortune into the first address? After all, it’s so much nicer to have one big pile rather than a lot of little ones:
#!/bin/bash
xdotool windowactivate 67108874 # change this to your terminal ID
for i in {1..99}
do
sleep 1
xdotool type "tb $i 0 10000000000000"
xdotool key KP_Enter;
sleep 5
done
The server is a bit kinder to transactions than minting, so you can run through the loop at a faster 6 seconds and still get most to go through.
But hang on, when you query the Libra CLI for the balance of your first address, something’s gone wrong:
libra% q b 0
Balance is: 10000000000000.000000
Try running the script above again, but with a transfer of 100 libra rather than 10 trillion, and it does work:
libra% q b 0
Balance is: 10000000009300.000000
Two things here — firstly it looks like only 93 of my other addresses managed to get minted libras due to the 429 too many requests, but that’s not a problem.
The second issue is that it turns out there’s a maximum number of libras an address can hold, namely: 18446744073709.551615.
Microlibras and Libras
Now that’s an interesting number. Anyone who’s worked down at the bits and bytes level will instantly recognize that it’s 2⁶⁴ -1 / 1000000.
Or more practically speaking, libras are:
- stored as microlibras (i.e. the smallest unit is a millionth of a libra), and
- the size of the data field is 64 bits.
I’ll be posting a Linkedin post on the economic and social significance of this later.
In Conclusion
I’ve certainly had an enjoyable wander through the new world of Facebook’s Libra testnet, and I hope you have too.
And now you have a set of tools that you can expand to experiment at your leisure with this new impending cryptocurrency.
(Of course, it will be easier when a proper API is released.)