A Tutorial Introduction to Michelson & Tezos, Part II: Hello Tezos

John Burnham
Coinmonks
11 min readDec 17, 2018

--

Now that we know a little Michelson, let’s take a look at the Tezos blockchain, which is the “environment” in which our Michelson contracts are actually going to run.

This is not going to be an exhaustive overview of Tezos; we’re going to focus on those aspects that are most relevant from the perspective of a Michelson programmer. We’re going to look at a few useful tools, such as the tezos-client program that is in the alphanet docker container we installed last chapter and an online blockchain explorer.

These tools, like Tezos itself, are still immature and undergoing rapid development. For the most part, they employ the “info-dump” style of user interface, which works fine for e.g. a Tezos core developer, but can be very overwhelming to newcomers. For the purposes of this tutorial, we’re specifically interested in how to program in Michelson, so we can mostly abstract over a lot the detail exposed by the tooling that would be more relevant in other circumstances, such as if we wanted to run a baker. Some of these details will be covered again in greater depth in future chapters.

For now, our goal is to gain a basic understanding of how to use the current Tezos tooling, with the goal, by the end of the next chapter, of running and interacting with a simple Michelson contract on the Tezos alphanet.

The Tezos Blockchain

In your terminal, in the directory where you installed the alphanet container from the last chapter, execute the following command:

Assuming the node is running (if not, start it with ./alphanet.sh start), this should output something like:

Please ignore everything in this for the moment, except the following line:

Please copy the value on the right, as we will use it in a moment. Note that the actual value displayed in your terminal will almost certainly be different for you than what I’ve written down here. Please copy the value from your terminal.

Now open tzcan.io’s alphanet block explorer in your browser, and in the search box paste the hash and hit enter.

You should see something like this:

This is the hash value of the block that your node thinks is currently the head of the Tezos alphanet blockchain.

Blockchains: A brief review

Look at the sections that say

To briefly review, a blockchain is, as the name suggests, a chain of blocks. Each block includes a hash of its predecessor block, which in turn includes a hash of its predecessor, and so on and so forth all the way back to the first (or “genesis”) block in the chain that has no predecessor. (Note only the predecessor hash is actually in the block, the successor hash is an out-of-band piece of information that tzScan has added for our convenience).

You can take a look at the alphanet Genesis block here: https://alphanet.tzscan.io/0

The reason each block includes the hash of its predecessor is to create a data structure that is prohibitively difficult to surreptitiously modify.

For example, suppose I wanted to edit my copy of block BLe9jh... and add a transaction that credited one of my accounts with 1 billion tez, I could do that. But it would be obvious to everyone that I had tampered with the block, because the hash of the block would no longer match.

All the information in the block (when serialized) is just a number. The hash of the block is just the output of a specific hash function on that number:

So if I add a transaction and change the block information, I change the serialized number represents that block, which will change the hash of the block. Since hash functions are designed to take negligible time and cost to run, if I try to send my tampered copy of the block to anyone else, they can easily recompute the hash function and discover that the hashes don’t match.

Furthermore, because each block includes the hash of its predecessor, changing any block changes all the other blocks “downstream” of it. This means that as long as you and I agree on what the latest block in the chain (the head) is, we can agree on the whole history of the chain. (Agreeing on the head is problem solved by the consensus algorithm, which in Tezos is a variant of delegated proof-of-stake).

This type of data structure, in general, is called a hash tree or a Merkle Tree. It’s important to emphasize, given the various abuses of the term, that all “blockchain” refers to is a hash tree whose nodes are blocks of transactions. It is a term in the same category as “key-value map”, or “linked-list”.

Tezos Mainnet, Alphanet, Zeronet

Returning to our block explorer:

We can see that block BLe9jh.. does not actually have any transactions in it. (your specific block may). This is because the Tezos Alphanet is designed for testing, so transactions are fairly rare and blocks are frequent (one every 30 seconds). Alphanet tez have negligible value and are in fact given away for free to anyone who wants them by a limitless faucet (which we will use later on). The Alphanet is not decentralized and is periodically reset from genesis every few weeks.

Tezos has three parallel blockchains, called “Mainnet”, “Alphanet” and “Zeronet”

Mainnet is where tez have real dollar value and where the network is decentralized. Zeronet is a bleeding edge test network used mainly by the core developers and is reset usually several times per day.

To see a block with actual transactions go to the Mainnet block list.

For this tutorial we’ll do most of our work on the Alphanet, although in a later chapter we’ll go through bootstrapping our own test network with a new genesis block.

By the way, if you re-run:

You’ll notice that while you’ve been reading this section the head has updated.

Interacting with the Tezos Alphanet

Now that we’ve covered enough context, we’re going to actually do a few things on the Alphanet.

First, we’re going to need an identity, go to your terminal and run:

This drops us inside the docker container, so we can directly execute commands on the tezos-node. Your prompt might change and you can run some commands to verify that you’re now inside a docker container:

You can use <Ctrl-D> or

to return to your regular prompt.

Generating an Alphanet identity

This will take a short amount of time, so read on while it works.

A Tezos identity consists of the following things:

  1. A peer_id that uniquely identifies us to the network. You can see a list of alphanet peers on tzScan's network page
  2. A key-pair of public_key, secret_key that lets us securely communicate with other peers. E.g. we can use our secret key to generate transactions that other peers can verify came from us using our public key.
  3. A proof_of_work_stamp that shows that we did some amount of computational work while generating the identity. This is why the identity generate command takes some time, and is to prevent people from spamming the network by generating endless identities.

You can read more about identity generation with

Now that that’s done we can see the identity file generated by running:

(Actually this .tezos-node/identity.json file is a dummy file, the docker container stores the real identity in /var/run/tezos/node/data/identity.json, but if you installed tezos from source it would be under .tezos-node)

Remember to never ever ever post your secret key anywhere. If anyone other than you learns your secret key your whole identity is compromised. On the Alphanet it doesn’t really matter, but it’s a good idea to practice good operational-security even when it doesn’t matter so that it becomes a habit for when it does.

Activate an account with $$ Free Money $$

The Tezos Alphanet faucet is a website run by the Tezos developers that lets us download a wallet file. Essentially, whenever the devs reset the alphanet, they include a bunch of funded accounts in the genesis block and then give the keys to those accounts away for free to anyone who wants to do some alphanet testing.

Navigate to https://faucet.tzalpha.net/ and download the file.

Now quit out of the alphanet docker container and cat the contents of that file:

The two lines of relevance for us are:

This wallet file, like the identity file, has a cryptographic secret key. After activating the account listed in this file, the way you’re going to tell the network that you want to make a transfer is by broadcasting a transaction message (signed by this secret key) over the peer-to-peer network, using the p2p identity we generated earlier. Other peers can verify the correctness of transactions (whether we have enough tez to make a transfer) by validating our signature with our accounts public-key, which is the “address” of the account, as well as by looking up our balance in the blockchain.

Now let’s activate the account, giving it the local nickname alice:

This should output:

Let’s open up tzScan and take a look at block BM5RPwciV64Bz5nG3FbYY6xusrFt2j6EHTBrA7Hhye9fzWUhMK1 which includes this activation:

And now if we click on the account activation, we should see:

with the balance!

(It can take up to a few minutes for transactions to appear on tzScan, so if you don’t see your transaction just sit tight for a bit while more blocks get confirmed and try again.)

Generating a new account

Now lets generate a brand new account called “bob” with no balance of tez:

This should just output the Alphanet warning, but we can go inside the docker container and see the accounts we’ve generated or activated with

All the internal stored client data is in /var/run/tezos/client in the docker container, and in .tezos-client if you installed from source.

The client provides a better interface to the internal stored data, however:

Transfering tez from alice to bob

Lets check the balances for alice and bob:

Let’s transfer 420 tokens to bob from alice:

There’s burn-cap setting in the client to prevent us from accidentally sending a transaction that would burn more tez than want:

This prints a nice receipt for us detailing exactly what happened to our tez.

Let’s take a look at bob, or tz1QCUKL2YZuGLSdTesXtVHJB8zRcG6XTz8f on tzScan:

But wait, why is it 420.5 tez? Didn't we pay a fee of 0.5 tez? Take a look at the balance update sections of the receipt again (I've added notes for which address is alice and which is bob)

The fees came out of alices account separately from the transaction. So the total amount alice payed was -ꜩ421.257

Conclusion

This concludes our chapter on basic Tezos operations. Next chapter, we’ll integrate what we’ve learned here with the Michelson we learned in chapter I to actually run a Michelson contract live on the Tezos blockchain!

Exercises

Exercise 1: The Tezos client comes with a manual, which you can access with

$ ./alphanet.sh client man

This will by default output the manual formatted with colors. Use the manual to figure out what options to pass to the client man command to turn the colors off.

The Unix tools grep and less may be useful here, and there is also a copy on the web: http://tezos.gitlab.io/alphanet/api/cli-commands.html

Exercise 2: Go into the docker container shell (as we described earlier) and run

$ tezos-admin-client-p2p stat

Scroll to the section that says “Known Peers”.In your browser, open tzScan’s alphanet Network Stats page.

Find a peer ID that appears in both lists.

Exercise 3: Find the hashes of the Mainnet, Alphanet and Zeronet genesis blocks. How do they differ?

Exercise 4: Generate four new addresses in your client called eenie, meenie, meinie and mo. Conduct transfers to each address such that eenie has exactly 1 tez, meenie has exactly 2 tez, meinie has exactly 3 and mo has exactly 4.

--

--