From shiba to lion : second session

Two weeks after the first session of the series, we met up for the second one, which was about dropping the GUI, setting up a service for the daemon, using the CLI, and viewing transactions and blocks structures.

Dropping the GUI

We started the workshop, making sure everyone had its node up and running since we updated Insacoin to version 0.10 last week.

We’ve then stopped insacoin-qt in order to start the insacoind daemon. This has been done with :

insacoind -daemon -txindex -reindex

  • -daemon In order to execute the double fork magic and have the server running in background.
  • -txindex In order to get info about all the transactions on the network (not just involving our addresses).
  • -reindex In order to rescan the chain to hear about all transactions since the genesis block.

We encountered an expected error :

Error: To use insacoind, or the -server option to insacoin-qt, you must set an rpcpassword in the configuration file:
/home/antoine/.insacoin/insacoin.conf
It is recommended you use the following random password:
rpcuser=insacoinrpc
rpcpassword=7euYgcdSLzATwsJ8CpGiz6NijAaTS1KiQY3U9KJsQaTS
(you do not need to remember this password)
The username and password MUST NOT be the same.
If the file does not exist, create it with owner-readable-only file permissions.
It is also recommended to set alertnotify so you are notified of problems;
for example: alertnotify=echo %s | mail -s "Insacoin Alert" admin@foo.com

And we solved it by adding rpc passwords and users, as well as other options like txindex=1 we passed by command line. (You can find all the running options here)

We are now done :) :

$ insacoind
Insacoin server starting

Setting up the service

As root, we set up a systemd service in order to automate the process of running the daemon. I had already made a very basic script in contrib/init/insacoind.service (link on Github) and we copied it (still as root) in /etc/systemd/system/ :

cp contrib/init/insacoind.service /etc/systemd/system/

We modified it with the appropriate values (for instance, User=darosiorin the script was specific to me) and then launched and activated it with :

systemctl daemon-reload
systemctl start insacoind
systemctl enable insacoind # To start it when you boot

At this point, we could remove reindex=1 from insacoin.conf to not have it reindexing all transactions at every boot.


If you are trying to setup this yourself and running into troubles, feel free to ask questions on our Discord or in comments of this post on our website.


Using the CLI to speak to the daemon

Having the daemon running, we could speak to it using insacoin-cli . A list of all calls can be found here or displayed with

insacoin-cli help

We then starting generating with :

insacoin-cli setgenerate true

And made a transaction with :

insacoin-cli sendtoaddress iINSACOINADDRESS AMOUNT

We could then see our transactions in the chain using our fresh explorer or with insacoin-cli getrawtransaction id with id being the output of the sendtoaddress call. It would output something like :

01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff200374990004d1edfa5b0870000000010000000d2f6e6f64655374726174756d2f000000000100f2052a010000001976a9141a7314c5769f524fe1e988b67ec1408d388c93a388ac00000000

If you want to have it decoded, just use :

$ insacoin-cli getrawtransaction ab8091d07897c86f71954b04f6976dcaf6a5ef0850ac0be4435a522e076bd86c 1
{
"hex" : "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff200374990004d1edfa5b0870000000010000000d2f6e6f64655374726174756d2f000000000100f2052a010000001976a9141a7314c5769f524fe1e988b67ec1408d388c93a388ac00000000",
"txid" : "ab8091d07897c86f71954b04f6976dcaf6a5ef0850ac0be4435a522e076bd86c",
"version" : 1,
"locktime" : 0,
"vin" : [
{
"coinbase" : "0374990004d1edfa5b0870000000010000000d2f6e6f64655374726174756d2f",
"sequence" : 0
}
],
"vout" : [
{
"value" : 50.00000000,
"n" : 0,
"scriptPubKey" : {
"asm" : "OP_DUP OP_HASH160 1a7314c5769f524fe1e988b67ec1408d388c93a3 OP_EQUALVERIFY OP_CHECKSIG",
"hex" : "76a9141a7314c5769f524fe1e988b67ec1408d388c93a388ac",
"reqSigs" : 1,
"type" : "pubkeyhash",
"addresses" : [
"i5tNs3PTenm8YuMZ76rrLkEk9VUKQnrPcu"
]
}
}
],
"blockhash" : "a67d68447d7d86a29f37ad99283a397303f893b1d2ac91c30bb2befdfa622ff8",
"confirmations" : 6,
"time" : 1543171537,
"blocktime" : 1543171537
}

I described each field and their caracteristics, which I won’t detail here but could be the subject of another post. I’ll just give you my beautiful graphic describing how transactions are linked thanks to outputs and inputs :

Seeing it now in the middle of my post, I’m a little bit ashamed xD

Parsing the genesis block

To finish this session, I then presented the structure of a block (as it was in ~2013) with this graphic I used when I parsed the Bitcoin block chain for the first time and I was trying to figure out how all of this happened :

Thank you John W. Ratcliff, you helped me a lot !

To put this in practice I wrote a quick and verbose script which parsed the Insacoin genesis block and displayed it, in order to understand how the data were serialized (in french) :

$ python3 parser.py ~/.insacoin/blocks/blk00000.dat 
Je lis le header :
Je lis la taille du header
Je lis la version du bloc
Je lis le hash du block précédent
Je lis la merkle root
Je lis le timestamp
Je lis la target
Je lis le nonce
Je lis le nombre de transactions (un varint)
Je lis les  transactions
Je lis la première transaction
Je lis la version de la transaction
Je lis le nombre d'inputs de la tx (un varint)
Je lis les inputs de la transactions
Je lis le premier input
Je lis le hash de la transaction ayant créé cet input
Je lis l'index de l'input dans cette transaction
Je lis la taille du script (un varint)
Je lis le bytecode du script
Je lis la sequence
J'ai fini de lire les inputs !
Je lis le nombre d'outputs (un varint)
Je lis le premier ouput
Je lis la valeur (en satoshis) de l'output
Je lis la taille du script (un varint)
Je lis le script
J'ai fini de lire les outputs !
Je lis le locktime de la transaction
J'ai fini de lire les transactions !!!!!
Bon bah j'ai fini de lire le bloc
On a fini de lire le block, on l'affiche
========================================================================
========================================================================
|| HEADER ||
||====================================================================||
|| Taille : 402718720 octets ||
|| Version : 16777216 ||
|| Prev hash : 0 ||
||Merkle root : 5220375632694989237359085370456891584660347411425988637949128315404191421364||
|| Timestamp : 246718555 ||
|| Target : 4043247390 ||
|| Nonce : 3588123004 ||
||====================================================================||
|| TRANSACTIONS ||
||====================================================================||
||Transaction 0 : ||
|| version : 16777216 ||
|| input 0 : ||
|| hash : 0 ||
|| txindex : 4294967295 ||
|| script : b'\x04\xff\xff\x00\x1d\x01\x04@NY Times 05/Oct/2011 Steve Jobs, Apple\xe2\x80\x99s Visionary, Dies at 56' ||
|| sequence : 4294967295 ||
|| output 0 : ||
|| value : 65960776292106240 satoshis ||
|| script : b'A\x04\x97\x8a`\xa5Zr\x8d\x1d\x12`\x8dC+3\xd5\xcc\xe8@ZM9\xa7\xb5\n\xa9\xd9\xe8\xa2.b\xb7N\x98\x82\xa5!\x08\x07!\x04\xad=\x03V\xe4Wu\x18y\xdf\xbe\xf2}\x05\x1c\xe4!\x96\x82Y\x18D\x82\xfc\xac' ||
|| locktime : 0 ||
|| ||
========================================================================
========================================================================

The code, commented in french, can be found here.


The topic of the next “from shiba to lion” session (which will take place on 11/06) : Script, the Bitcoin script language and the creation of raw transactions outside of the core wallet. :)

https://www.meetup.com/Crypto-Lyon/events/256646306/