To see the youtube walkthrough of this tutorial, please click here.
Introduction
Hyperledger is an open source collaborative effort created to advance blockchain technologies. The Hyperledger organization has a number of projects (10 right now) for various blockchain solutions, such as smart contract engines, permissioned networks, querying for information inside a ledger, etc.
Iroha Terminology and Concepts
There are a few terms we need to know to get a good grasp on Iroha. There's an official core concept list here, but we'll go over the important ones right here in this post. (We'll assume you know the basics of blockchain. If not, read this blog post quickly.)
To begin learning the terms and concepts, we'll setup an example scenario, which we're going to model on the blockchain.
We own a farm, named "srcmakeFarm". We have some sheep, corn, a barn, and some other farm stuff on our land. We also have a few workers on our farm.
With that in mind, let's see the Iroha terminology that we need to model our farm.
There are a few more terms that we could learn, but they're a bit advanced for this getting started guide, so we'll talk about them in another blog post.
Creating An Iroha Network
We're going to create a basic Iroha network, following the Iroha Getting Started guide found here.
Any Unix style terminal will work, but I've personally used Linux (Ubuntu 16.04). The first thing we need to do is install Docker if you don't already have it. (If you've never used Docker before, you can check out this blog post, but finish this blog post first.) The commands to install Docker on Ubuntu from the terminal are: sudo apt-get install curl curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" sudo apt-get update apt-cache policy docker-ce sudo apt-get install -y docker-ce Next, we need to create a docker network. We'll name it "srcmake-iroha-network".
sudo docker network create srcmake-iroha-network
Next, we're going to add PostgreSQL to our network. sudo docker run --name some-postgres \ -e POSTGRES_USER=postgres \ -e POSTGRES_PASSWORD=mysecretpassword \ -p 5432:5432 \ --network=srcmake-iroha-network \ -d postgres:9.5 Next, we'll create a volume of persistant storage named "blockstore" to store the blocks for our blockchain.
sudo docker volume create blockstore
Now, we need to configure Iroha on the network. Download the Iroha code from github. (And install git if you don't already have it.) sudo apt-get install git git clone -b develop https://github.com/hyperledger/iroha --depth=1 We're going to use the Iroha configuration that's been prepared as an example to run the Iroha docker container. sudo docker run -it --name iroha \ -p 50051:50051 \ -v $(pwd)/iroha/example:/opt/iroha_data \ -v blockstore:/tmp/block_store \ --network=srcmake-iroha-network \ --entrypoint=/bin/bash \ hyperledger/iroha:x86_64-develop-latest Now we're going to actually run Iroha.
irohad --config config.docker --genesis_block genesis.block --keypair_name node0
And now our Iroha blockchain is running in our terminal, so don't close it! Next, we're going to play with Iroha by doing some transactions. Interacting with our Iroha Network
So we have our Iroha network running, so let's make some transactions. If you look at the code for our genesis block, you can see that some commands were invoked to help get us started, so we're going to use those.
To interact with Iroha, we're going to use the command line tool. Open a new terminal (don't close the one with our Iroha network!) and attach the docker container to our terminal.
sudo docker exec -it iroha /bin/bash
We should be inside the docker container's shell. Launch the iroha-cli tool and login as admin@test.
iroha-cli -account_name admin@test
You should see the picture above. We're inside the Iroha CLI, and there are a certain number of things we can do.
Type 1 and press enter to start a new transaction.
1
There are a lot of commands that we can do. Let's try to model our farm a bit. Let's first create the domain.
Type 10 and create an domain with the id "srcmakeFarm". The default role name is role (list of permissions) the domain has, and we'll give it a "user" role that was created for us as part of this example. 10 srcmakeFarm user
Next, let's add some sheep to our farm. Type 1 to add one more command to the transaction. Then type 14 to create the sheep asset. It belongs to the domain srcmakeFarm, and the precision is 0 (meaning we don't allow decimals. Sheeps only come in whole numbers.)
1 14 sheep srcmakeFarm 0 2
Our farm looks pretty good, so let's send this to Iroha peer (the network) by typing 2. The peer address is "localhost" and it's on port "50051".
localhost 50051
You can see we get a hash for the transaction, and back in our Iroha network terminal, stuff happens (because we pushed a transaction onto the blockchain. More on those details in another blog post.)
Okay, so we have our srcmakeFarm and created an asset named sheep. Let's add 50 sheep (and give it to admin@test since we haven't made our own account).
Type 1 to make another transaction. Type 16 to add some quantity to an asset. The account is "admin@test", the asset is "sheep#srcmakeFarm" (notice the #domain), there are 50 sheep, and the precision is 0. Type 2 and send it to the localhost at 50051.
1 16 admin@test sheep#srcmakeFarm 50 0 2 localhost 50051
Another successful transaction. Now let's make a sample query.
Making A Query
Let's check how many sheep we have. Type 2 to make a new query this time.
2
There are lots of query types. We're going to query a particular account's assets, so type 7.
The account that owns the sheep is "admin@test", the asset we need is "sheep#srcmakeFarm". Send the query request to "localhost" at port "50051". 7 admin@test sheep#srcmakeFarm 1 localhost 50051
You can see the query results immediately. admin@test owns 50 sheep from srcmakeFarm.
And that's how easy querying is! Conclusion
In this blog post, we learned about some basic Hyperledger Iroha terminology and concepts, and applied it by creating our own Iroha network. We also modeled our farm on the network.
Of course, we could have done a lot more and built a much more complex model. Feel free to explore the other possible transactions. And while we worked from the Iroha command line, we could have interacted with Iroha using some of the native programming language libraries. Of course, Iroha is open source and is written in C++, so you can see how it works right on github. In future tutorials, we'll look at creating more in-depth models, deploying Iroha on more than just our computer, using Iroha from a programming language, and contributing to the project itself.
The youtube walkthrough of this tutorial:
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake IntroductionBlockchain, Ethereum, and Bitcoin can be confusing. To be honest, most people understand some aspects of it, but not others. For example, there's "mining" and "cryptocurrencies" on the economics side, but "blockchain" and "decentralized" and a whole bunch of other words and terms on the tech side. We're going to look through 10 simple questions and and give their answers, and by the end you should know how Blockchain, Ethereum, and Bitcoin work. 1. What is Blockchain?It's just a chain of blocks. Literally, a line of "blocks". 2. What's a Block?A block has some data inside of it. There are four things inside of a block: 1. A reference to the previous block in the chain. 2. The "proof of work". 3. The timestamp of the block. 4. The Merkle tree root for all of the transactions that are included in the block. 3. What's "Proof Of Work"?The Proof of Work is a specific consensus algorithm. "Proof of work" is used in bitcoin mining. Basically, it involves solving a puzzle (the computers do it) that's easy to check, but hard to solve. For example, it's easy to check if a key works on a lock, but hard to create a key that works on a lock. Besides Proof of Work, "Proof of Stake", "Proof of Elapsed Time", and "Proof of Authority" are also some popular consensus algorithms. 4. What's a Consensus Algorithm?Consensus means that everyone on the system (network) agrees that the data in the ledger is correct. Imagine that we were in doing a group project in school in a shared Google Doc, and maybe we have a title page with everyone's names. If everyone prints a copy of the paper the day the project is due, but someone's paper has their name on top and in bold, everyone else would immediately realize they changed it since it's different from theirs (and everyone else's). Consensus is sort of like that. (But because there are a lot more people on the blockchain, it's more fool-proof. A consensus algorithm is just the computer version of checking that everyone's distributed ledger is correct, so that one person doesn't change the data. 5. What is a Distributed Ledger?It's a copy of the blockchain. It exists on all participant's computers. If the blockchain gets updated, and the consensus algorithm agrees "yeah this data looks right", then everyone else's copy of the blockchain gets updated. Distributed ledgers mean everyone gets a copy of the blockchain, which is why blockchains are "decentralized". 6. What does "Decentralized" mean?Decentralized means that the data in the blockchain is stored on a lot of people's computers, not just one company/persons. Meaning that the data is very hard (basically impossible) to edit. 7. So what's Bitcoin?Bitcoin is a cryptocurrency that uses a blockchain. Miners "mine" blocks to generate coins. 8. What's a cryptocurrency?It's an online (virtual) currency. It has value because people place decide it has value. (Sort of like jewelry. An emerald looks nice, and it costs some money to mine and refine, but it's actual value is because people like it since it looks pretty. Bitcoin is sort of the same. (Which is still valid.)) This is opposed to government backed currency like $US dollars of the Euro, where there's a real authority and a system in place for why the currency has value. 9. What about Ethereum?Ethereum is a system that uses blockchain, mostly to deploy apps and use smart contracts that are backed with blockchain technologies. It's a blockchain, and people who do the computing power to mine blocks are awarded the cryptocurrency "ether". 10. What are Smart Contracts?It's basically a set of rules and instructions that exist as software that automatically get fulfilled as conditions are met. Which is basically like any other program. The reason smart contracts are good though is that they get pushed onto the blockchain, meaning they do get invoked and no one can tamper with them or change their mind or break the rules. ConclusionWe went over blockchains and cryptocurrencies. The technology itself is pretty complicated if you want to know exactly how it works, but to get a general understanding is pretty simple. Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake Introduction: Connected Components and Cycles
In previous blog posts, we've gone over graph theory, adjacency lists, adjacency matrixes, and BFS/DFS. We had another blog post on shortest paths in a graph. We also looked at maximum flow using the Ford Fulkerson Algorithm.
Today, we'll look at connected components and cycles in a graph. The graph that we'll be playing with is below. Connected Components
A graph has some vertices, named nodes. It also has some edges, that connect nodes. But that doesn't mean that all nodes are reachable from all other nodes, and that everything is connected nicely. For example, we could have 10 nodes, but only 1 edge. That's still a valid graph.
A "connected component" is a set of vertices/edges that are connected. (The vertices can reach each other through edges.) In our code, we'll represent connected components by labeling them with a number. We'll create an array that, for each node in the graph (the node being represented by the index in the array), holds the number for the connected component the node is in. To find out which connected component each node is in, we'll do a DFS and label each node as we see it. Note: There's a special case where a node in a connected component does not get reached by other nodes, but is still part of a connected component. For example, node 15 in our group can go to node 14, but no other node can get to 15. We have to make sure that if a node can reach another node that's already part of a connected component, (Basically, someone shows up late to class, the class has people getting together to form groups for a project, but the guy's friend already got them both a group.)
Cycles
A cycle in a graph happens if it's possible to travel along the edges on a graph where you can revisit a previous node. For example, in our graph above, someone on node 4 can get to node 3, and from node 3 they can get to node 1, and then from node 1 they can get back to node 4. That's a cycle.
The code to find a cycle is pretty simple. It's just a DFS or BFS, and if we ever "visit" a node that's already been visited before, then we've detected a cycle.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake |
AuthorHi, I'm srcmake. I play video games and develop software. Pro-tip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
License: All code and instructions are provided under the MIT License.
|