📜 ⬆️ ⬇️

GoToChain: how schoolchildren of the blockchain in the village wrote

It was a hot August. There was another summer school. In the direction of functional programming, everything was restless: instead of sweat, monads were dripping from us, and the gloomy shadow of Coq flew in the sky.


The school was held quite normally, like all children's camps. After putting Peano's numbers on Haskell, we sawed compilers and interpreters of different varieties and sizes (from self-made to LLVM), wrote our core in Rust and discussed group theory, drawing analogies with light French novels in general, it was fervent.


We also had a hackathon. One of the teams, inspired by Zeitgeist and partner spinners, set about developing a system for conducting tenders and processing decisions using smart contracts, having planned several ideas for using the blockchain for the community around the school. What innovations from this left - under cat.



Well, it's time to meet you. We are a team of St. Petersburg and Moscow schoolchildren who have implemented the “miracle” described below. We learned about the mechanisms of the blockchain and for the first time heard the word “smart contract” the day before the hackathon, so the experience was extremely entertaining. But first things first.


The results of the hackathon, organized in order to cheer the schoolboys cared for half to death, had to turn GoTo’s life upside down and open up the cosmic yields to all of them. Naturally, this action could not pass by us, therefore, having called one charming technical evangelist from the direction of bioinformatics, we began work.


The concepts changed one after another: from cryptography on grids (after all, no self-respecting camp could exist) and a formally verified lock for the Boeing we came up with the idea of ​​integrating the blockchain into the life of GoTo. “What will he do useful?” One of our team members asked. Since at that moment our inexperienced minds were very impressed with everything that was happening, we immediately stoned him with stones. And continued.


But now we have restrained ardor and decided to still think: why the blockchain camp?


Before we get to our case, I suggest those who want to get acquainted with what blockchains and smart contracts are and why we (and not only us) made so much noise around them.



"Likbez"

Likbez


In everyday life and in business, situations often arise when two or more parties need to make an exchange. So, for example, when buying an apartment, we change several millions for the keys of 14 square meters on Petrogradka, or by renting a car to the salon, we get a discount on a new one.


People have made exchanges for thousands of years and during this time everyone understood that without external control this process could turn into a circus with clowns: the designer can run away with prepayment at sunset, the apartment seller comes a month after the purchase and declare that the signature on the documents is not his and that you must vacate the living quarters. In general, you cannot get far on trust alone.


Then people decided to attract a third independent party, which would give guarantees - after the exchange, there will be no problems. This role in our life is played by banks, state registries and other organizations that receive money for the support of excellent human relations. From frauds, these organizations held back the desire to preserve their reputation and to have a steady income from this work, as well as state control.
However, it became obvious a long time ago that such mediators have a number of drawbacks:


  1. You still have to completely trust one side. With proper skill, any government control can be circumvented, and the desire for a large one-time replenishment of your bank account can easily overcome the desire for a stable income.
  2. Third parties require a fee (often large enough) for their services, and who would not want to give up extra expenses
  3. The emergence of third parties and bureaucracy, seeking to keep these same persons in check, extremely slowed down the exchange process, since any action is now accompanied by the passage of a billion instances and the signing of a trillion documents.
  4. Third parties are not insured against unforeseen situations: the bank can always go bankrupt, the housing registry can forget to backup, or Aunt Luda from the patent office can forget your perpetual motion documents in a minibus.

A canonical example of such an intermediary is the Certificate Authority in the public key infrastructure (PKI), which associates public keys with the parties to which they belong.


The most famous uses of CA are TLS certificates, which make HTTPS work. However, as practice shows, these “professionals who sell trust” sometimes allow themselves to give certificates for arbitrary domains to arbitrary people , or to give someone a certificate for google.com .


Therefore, society has a logical desire to exclude third parties from this process.


The blockchain can help them in this.


What a blockchain?

Blockchain


Blockchain is a distributed database that allows you to maintain a single version of the list of transactions for all network members. Transactions can be completely different: remittances, transfer of property, registration of copyright and the like.


Transactions are combined into so-called blocks. Each block stores the previous hash, so that past data cannot be changed, as this will lead to changes in the hashes of all subsequent blocks. These blocks are combined into hashes in a linked list, or a metaphorical chain that is difficult to break or redo ... In the chain ... The chain of blocks ... BLOCKCHAIN ​​!!! Hmm, well, you understand.


When a user wants to add a new entry to the blockchain, he tells the whole network about it. Then some participants take this transaction and check it for “legitimacy” (in the case of cryptocurrency, for example, they see if the user has enough money for the transfer operation), and if it is being tested, then a new block is collected from it and several others. Then it is transmitted throughout the network, each participant checks for validity all the entries in it, checks the compliance of the hashes, and if all checks are successful, add it to their own chain.


The hash in the case of the blockchain is the result of the so-called cryptographic hash function. Its goal is to translate an object of any size into a characteristic sequence of bytes of fixed length. Such a function when submitting to it the same object will always produce the same sequence of bytes. Its second important feature is that if we change the object at least a little at the entrance, the sequence will be completely different and there will be no pattern in its change.


By changing even one object in the block, we change its hash, and since the hash of the next block will be considered, taking into account the current hash, we break the entire subsequent chain.



Therefore, if the evil hacker Vasya wants to transfer a billion bitcoins backdating / erase information about his debt / <insert another crime>, he will have to persuade the entire network to switch to a new version of the chain with completely different hashes.


The question remains, who collects these records in blocks?


There are two types of blockchains - public and private, and for each of them the answer to this question will be different.


In a public block, any member of the network can assemble, however, in order for the system to accept its block, it is necessary for the person to solve a computationally difficult task (proof of work), or, for example, the probability of creating a block will be directly proportional to their means of proof.


The method of choosing the creators of new units is extremely important, as it may jeopardize the functioning of the system. So, for example, now most of Bitcoin’s computing power is located in China , so if suddenly all the mining farms of the state decide to unite (either at the behest of the state or in a single communist rush), then they will be able to control all chain (up to the first hard fork of course). Even if this does not happen centralization indicates obvious problems.


In private, the right to create blocks has only a specified circle of persons - validators. One of the possible algorithms looks like this: the system chooses one of the validators with lots, it collects a block from the new applications and sends to the others, if more than half of the validators say that the block is valid, then it officially becomes a continuation of the circuit.


So what needs to be taken out of these lines?


Blockchain is a storage system that is very difficult to compromise due to cryptography and collaboration between network members.


What kind of smart contracts?

Smart contracts


Another fashionable word, the meaning of which is far from clear to everyone. But since we are now blockchain experts, let's see


The formal definition of a smart contract (aka a definition from Wikipedia) is an algorithm for monitoring the fulfillment of obligations by two or more parties, using the blockchain to record the actions of all parties.


It can be said that “creating a smart contract” is an articulate term for adding business logic to the blockchain. Those. add some rules that affect the behavior of the system.


Imagine that you want to buy a batch of coal from the company “NovokuznetskKovorking”. You agree that you will transfer the payment to them when the coal cars are unloaded in St. Petersburg and in order to automate the entire process and remove the human factor from it to the maximum, put a GPS tracker and a sensor on it that shows the weight of the load in it. You give the system an obligation to pay the agreed amount if the cargo is unloaded where necessary and on time. When this sensor sends information that the load is unloaded, and the coordinates of the place where it happened (let's believe that this sensor works perfectly and is perfectly protected from hacking), the system will compare them with the given ones, and if unloading occurs where necessary, it will automatically makes a transfer, if the unloading does not occur on time or occurs, but somewhere in China, your obligation is removed from you. All your promises and everything that happens with the load is recorded in the blockchain, which means that the system will always work with true information about what is happening and affect the programmed behavior is impossible.


Thanks to this, NovokuznetskKovorking can safely carry you its coal, not doubting that the work will be paid, but you can not worry that they will not bring you anything if the prepayment is transferred.


Obviously, smart contracts have a great potential for use in trade , logistics , medicine and other industries. This technology can change the way we approach our business, the place banks and governments occupy in our lives.


As you understand, we were very impressed by the coolness of this technology and really wanted to just gash something help the world and the camp.


PS If you are interested in continuing to study all of the above, then there is a huge amount of literature on the Internet and tutorials are becoming quite popular ... Here GoTo, for example.


What are we doing


It is not the first time that GoTo holds contests in which participants are invited to implement one of the proposed projects and compete for the opportunity to receive a grant that fully or partially covers the cost of school tuition.


We decided to replace the centralized system for storing and processing the submitted solutions with the blockchain option. This allowed to make the process more transparent for participants and investors who allocate money for grants.


Why do we do it


The blockchain ensures the general availability and immutability of data on applications and their verification, which will allow partners and participants to follow the process of the competition and will create verifiable portfolio items for all contestants.


After the winners are announced, each other’s decisions become available to the participants - this will allow them to get acquainted and further interaction within the community. It also gives participants the opportunity to control the fairness of the competition, as they can see whether a grant was allocated to a person with a normal decision or a cousin of her brother’s sister was chosen. dogs director.


It is also important that the project will allow to attract more attention of future school participants to blockchain technologies. Since grant competitions are only part of the platform, they will be able to expand it with their smart contracts, which gives a lot of practice.


Contract logic


The logic implemented in our smart contract is quite simple.


First, the organizers add transactions to the blockchain with a description of projects for implementation and the reward that the participant will receive with the best solution.


Then those who wish to choose a project and publicly declare their intentions. It is necessary for the organizers and participants to understand how many applicants there are for the grant.


After the participant has implemented his unique and unique vision of solving the problem, he puts a link to it in the blockchain.


(Of course, the decision saved by reference can be changed, because the blockchain with its super powers only ensures that the URL itself remains unchanged, but this is a temporary solution and will be fixed in future versions.)


When the competition comes to an end, the organizers look at the decisions and, choosing, in their opinion, the best, vote for it. The system recognizes the person with the most votes - the winner. He needs to send a transaction agreement to accept the grant, if he does not do it in a certain period, the system will automatically switch to the second by the number of votes.


After signing this agreement, the competition is officially considered closed.


How do


After a bit of thought and having painted all the walls with diagrams and diagrams of different degrees of utility, we finally formed the GoToChain concept. This was supposed to be a private blockchain, where the validators are the GoTo administration and partner companies, ordinary users are participants in contests. We had to implement a smart contract with the logic stated above and quickly roll everything into the light.


Naturally, the first thought was to implement the blockchain from scratch on Haskell. However, after looking at a couple of implementations, we concluded that we are not yet ready for applicative functors, monad transformers and writing Paxos.


We decided to use Exonum , released in July, as a more realistic and safer implementation (according to the advice of the great, terrible and sweet wldhx ).


In this platform (written in Rust, by the way), developers have already implemented blockchain storage, communication between nodes and other useful mechanisms, such as anchoring to prevent auditors from rewriting transaction history and “light clients” to provide information to regular users. In terms of the platform, smart contracts are called Services, and it was up to us to develop the service.


In the end, we quite successfully traversed through Rust and the Exonum documentation, even slightly expanding the default functionality of the “light clients” along the way.



This we support each other in the development process.


What do we do next


As already mentioned, we had a lot of ideas related to the blockchain, so we do not plan to stop grants.


We plan to expand GoToChain with smaller-scale contests, which will be held directly at schools, voting system for the best content about past events, introduce verified diplomas, give an opportunity to outsource contracts with school partners and much more.


Some of these features will be implemented by other GoTo school participants, so this platform is important not only as a tool for improving everyday life, but also as a platform for future experiments.


A few words about the team


Dmitry Volkov aka wldhx - the man, the man, the legend. Techlide. Enlightening, punishing and touching.



Stepan Kuznetsov - Rust-developer. Without it, nothing would work.



Alexander Danilov - JS-developer who made the geometry.



Maxim Manainen - Technical Evangelist and Bioinformatics . The author of great articles with a bunch of demonstrative pronouns and water. Well, without him nowhere.


The logic and mechanism of the smart contract were developed by the joint efforts of all the above-mentioned gentlemen.



Andrei Curds - Frontend-developer. Without it, everything would work, but you could not use it.


Andrei Kosorukov is also a frontend developer. Without it, the first version of the platform would be a console application.


Epilogue


Working on GoToChain was an extremely exciting experience. In addition to being better acquainted with the work of the blockchain and the process of creating smart contracts, we learned a lot about teamwork and self-discipline. Of course, the buzz from mopping up the docks can not be compared with the enumeration of group theory terms, but also quite well.



And now, if you have a desire and this allows you to make your age - welcome to test the platform personally and try your hand at the autumn school competition in ITMO.


By the way , during the school, the teams will continue to work on expanding the GoToChain functionality, complementing it with a system of community self-regulation and portfolio verification. Research tasks will also be offered: for example, to ensure the security of contracts, speed up the flow of transactions and the use of non-linear data structures. There will also be tasks from partners: a system for a charitable foundation and cases in the framework of public services from VEB. In addition, we will talk about data verification and static contract analysis, distributed systems architecture, consensus and linearization algorithms, and modern cryptographic methods. We study the blockchain platforms Exonum and Ethereum, formal analysis techniques, the topic of information security at the level of both applications and platforms, transport and lower.


')

Source: https://habr.com/ru/post/339796/


All Articles