📜 ⬆️ ⬇️

How I wrote my cryptocurrency

3 years ago I had the idea to create my own cryptocurrency. From bitcoin I decided to take the idea with nodes and blockchain.


I decided to bind each node to a person. Anyone who wants to become a miner should video his face and take a photo. When checking photos and videos are stained to make sure that the face is the same. The video is needed only because it is almost impossible to fake it, unlike a photo.
In the photo you need to put points in certain parts of the face in order to compare proportions when searching by database.
This is checked by all the miners who have previously passed such a test. In the end, you need to get 10 positive votes and less than 10 negative during the day.
All this happens by sending transactions and blocks between nodes, there are no central servers. All photos / videos are in public.

If you bribe 10 miners to vote for you, this will not guarantee that you will become a miner, because there is a chance to catch 10 minuses.

There is also a backup option when clones have already penetrated the DC network. In this case, someone must send a transaction with a complaint to them (ParseData-> abuses ()). Then admin, i.e. I get the right to send a transaction that will transfer this miner to the ranks of ordinary users.
In total, the administrator has 11 types of transactions, all of them are processed by methods from ParseData :
admin_1block - Handles the very first block.
admin_add_currency - Add a new currency. Now there are not all nat currencies, but only the main ones.
admin_answer - The answer to the bug report.
admin_ban_miners - Transfer of miners from miners to users if there was a complaint about it. At the same time, everything that managed to get engaged remains at the user's disposal.
admin_message - A short message that is shown in the interface.
admin_blog - For the future, suddenly you will need to report something that does not fit in a short message.
admin_new_version - Fill a new version.
admin_new_version_alert - Gives an alert in the interface that you need to update.
admin_spots - Everything for searching for clones - sets of points, compatibility of versions of points, from which points we make segments, allowable discrepancies.
admin_unban_miners - Transfer degraded miner back to miners.
admin_variables - Variables in the database table variables. Then I plan to make constants.
')
I immediately listed all admin options so that no one has a question - can an admin block an account with money? No, it can not, the maximum that is allowed to the administrator, is a lowering of the privileges of the account without affecting the funds on the account (admin_ban_miners).

Legend


The word miner itself (translated as a miner) is probably not very suitable for designating a user with elevated privileges. But historically, I used miner everywhere.

The order of generation of blocks


All nodes are divided into levels depending on the current header hash (node_id, block_id, prev_head_hash).
A node that is at the 0th level is determined by the testblock-> get_block_generator_miner_id () method.
If, for example, node ID 12689 is now at the 0th level, then node 1 will be 12690-12691 at level 1, 12692-126995 will be on level 2, and so on.
If the node at the 0th level could not generate a block, then the nodes of the 1st level are included in the work, if they could not, then the 2nd, etc.
Those. the attacker will not be able to make it so that the block is generated by his nodes.

Coin generation


Now we have nodes, each of which is assigned one living person. One could simply hand out X coins to each miner. But for some reason I didn’t like this idea right away, as it turned out, for good reason. Here is an example of what could happen - habrahabr.ru/post/217111 and further - coinmarketcap.com/aur_90.html .
I also didn’t get out of my head the idea that if Dcoin becomes popular, the authorities can start fighting with it by imposing sanctions against the exchanges. We needed a mechanism that would allow Dcoin to exist without exchanges, banks and other intermediaries.
By the way, Dcoin is short for Democratic Coin, it’s also DC.

The solution was quite simple. You need to combine the distribution of coins with the function of exchanging Dcoin for cash. To do this, I needed to copy the main currencies in Dcoin. I just added the letter D to the 3 letter code of the currency. It turned out DUSD, DEUR, DRUB, etc.
If the miner wants to buy, for example, 1000 DUSD, then he sends a transaction to the DC network, which indicates that he wants to buy 1000 DUSD for 1000 USD. But willing to sell DUSD may not be. The miner must be prepared to wait indefinitely until a DUSD dealer is found.

Now, we have, for example, 100k records in our distributed database that miners are ready to buy DUSD for $ 100 million. It remains to create a DUSD. Everyone probably thought about the minion. But I do not believe that someone will want to pay me $ for some kind of DUSD, so no. Yes, and instead of the Prime Minister, it would be more logical to just distribute coins.

I decided to charge DUSD to the miner's account for the fact that the miner waits until someone wants to sell him DUSD.
DUSD is calculated according to the following formula:
nDUSD = bDUSD * ((1 + K) ^ T) -bDUSD
where nDUSD is the newly created DUSD, bDUSD is the sum of the DUSD that the miner is ready to buy, K is the coefficient about which I will write below, T is the time in seconds.
Those. the more DUSD the miner is ready to buy and the longer he waits until someone sells them, the more new DUSDs are created and credited to him.
In the source, this formula is in the ParseData method -> calc_profit (). calc_profit includes a few more parameters, but if I start describing them, it will be too boring and long. But if someone is interested, I will write.

K coefficient


This is a very important factor, since emission depends on it. I thought for a long time how to make it, to which parameters it should be attached. After all, he should not succumb. The most democratic option turned out to be the one where this coefficient is determined by the miners of this currency themselves by voting. For convenience, in the interface part the K coefficient is displayed as% / year. Valid values ​​are from 0 to 1000% / year. In source code ParseData-> votes_complex (). The miner can only vote for currencies that were added more than $ variables ['min_hold_time_promise_amount'] seconds ago. To protect against clone attacks so that they cannot destabilize the network with their votes.
The vote recount and update coefficients are done every $ variables ['new_pct_period'] seconds. Now there are 2 weeks.

Now in the distributed database we have not only the promises of miners to buy 100 million DUSD, but also their votes for the emission rate, i.e. for the coefficient K. And after some time, the first DUSDs will start to appear on the miners' accounts.

It turns out that an ordinary miner should simply indicate that he is ready to give $ 1000 for 1000 DUSD, and if, for example, 900% / year is chosen according to the voting results, in a month he will have +200 DUSD, and in a year +9000 DUSD which are equivalent to $ 9000.
But I do not recommend flirting with the upper limits of the coefficient. About the consequences will be written below.

WOC


By analogy with ru.wikipedia.org/wiki/Wocu, I made a WOC. It is charged to each miner, it can not be exchanged for cash from another miner. It can be transferred from wallet to wallet. In theory, the WOC should replace the dollar on the pedestal of world currency.

Exchange


If they are - well, but if they are banned - it is not scary. In large cities, I think, the preference of miners will be given to the built-in exchange mechanism, because it converts the Dcoin currency immediately into cash.

Geographical and national restrictions


Perhaps the majority of miners will be against the mining of their national currency somewhere outside their country or monetary union, or they want miners to be only in large cities. Therefore, when adding any currency, the miner will need to record a video in which he will speak in his national language, something like: “I promise to give 100 NOK for 100 DNOK”. Next, you will need to specify the country and mark a point on the map. And the request will have to be approved by 10 miners, in whom the Norwegian krone is mined. Perhaps the DNOK miners will introduce a rule according to which the new DNOK minners will have to shoot a video on the background of some famous monument in a major city in Norway.

Inflation


I do not know how the miners will behave and for what coefficient they will vote. But such a scenario is not excluded, when, for example, DUSD will be created more than the market needs. If DUSD will be traded on exchanges, the rate will fall below 1: 1, i.e. for 1 DUSD will give, for example, 0.9 USD. In this case, the miners must be conscious and reduce the total mass of the DUSD. By the same vote. For example,> 50% will vote for a decrease of 10% and in the database instead of 100 million. DUSD all will have 90 million. DUSD (ParseData-> reduction ()), which most likely will return the rate to 1: 1.
Miners can also vote for changing the maximum promised amount and for the number of currencies that can be mined together (ParseData-> votes_complex ()).

upd. If the total amount of the promised amounts becomes less than the volume of coins created by any currency, then the automatic reduction of the volume of coins of the problem currency by 10% will start.

Ordinary users


In addition to miners, there are still ordinary users. They do not need any photos or videos, they are completely anonymous. They can only accept and send Dcoin coins. You do not need to keep your node. He can simply send his transactions to any node in the DC-Network.
Miners can vote for the coins of ordinary users to grow in their wallets. For example, at 30% / year. Those. A simple user can buy on the stock exchange some quantity of DUSD, for example 10k, and in a year he will have 13k DUSD. I do not know if such a feature is needed, if it is not needed, the miners can simply keep the% for users equal to zero, and nothing will grow.

New users


With this, too, had to suffer. After all, if you give the opportunity to send transactions with the registration of new users to everyone, then someone will take and generate 1 billion transactions and fill the entire DC network. At first I did it with invites, but the problem was clarified - a registration transaction in which an invite is specified, you can intercept and use the invite yourself. Therefore, only the miner and no more than $ variables ['limit_new_user'] can send a transaction with the registration of a new user for $ variables ['limit_new_user_period'] seconds. Now there are 2 per month.

Anti-theft key protection


Up to 3 primary keys can be attached to the account. This means that the adoption by the DC network of any of your transactions will require 3 signatures. You can keep one key for yourself, two others, for example, on some third-party services that will sign your transactions only if you enter an SMS code.

A few words about the development process


The fact that it will take 3 years, I could not imagine even in the worst dream. I thought to manage in a few months. For the same reason, he wrote in a language that he knew at a more or less acceptable level. If I could go back to the past for 3 years, I would probably use C ++.
The first 2 and a half years he worked in the evenings and weekends, which was terribly annoying, but six months ago he got rid of work that was in the way, and began to devote all the time to Dcoin. The last 3 months, 10-16 hours a day, searched and fixed bugs. I thought a maximum of a month - and there will be no bugs, but no - 3 is gone.
I did not describe a lot of the subtleties of Dcoin’s work, since otherwise, the article will be too long and difficult to read. In Dcoin 41 transaction types, the methods that process them are in the ParseData class. Write questions, I will explain everything.
Comments in the source will contain a lot of errors and typos, then be sure to clean. The code is written, most likely, not very well, but the main thing is that everything works.

Installation


When installing requires a private key. It can be taken either here or from some miner.
Installation is simple and takes about 2-3 minutes. In order for someone else to take an account, you need to immediately change the key.
You can use it only on 32-bit OS. It is necessary to rewrite a couple of functions, in particular pow, and will be compatible with 64-bit ones.
On tests I used simple VPS with 512 RAM and 700Mhz CPU, for the first time such capacity will be quite enough.
Apache or nginx is needed, PHP> 5.2.4, Mysql> 5.0.
If you bet on win, then it is better to use nginx, I tested with winginx.

upd
At the request of the comments wrote a description of the installation:
For win
1. Install winginx.com/en/download
2. Create a project in the Winginx Workbench
3. Download github.com/c-darwin/dcoin/archive/master.zip
4. Unpack the site directory
5. Create a database in pma: localhost : 81 /
6. Go to localhost / dcoin-master / index.php
7. php_path, most likely, will be such - C: \ Winginx \ php5 \ php.exe
8. Follow the installation instructions

To upgrade your account to the miners, you will need to open the 80th port for incoming connections.

For nix
1. If not installed, then install nginx, PHP> 5.2.4, Mysql> 5.0, create a database
2. Download github.com/c-darwin/dcoin/archive/master.zip
3. Unpack to the site directory
4. Go by ip_server / dcoin-master / index.php
5. Follow the installation instructions

To upgrade your account to the miners, you will need to open the 80th port for incoming connections.

upd1
There was a major update of the sources - now there is an automatic reduction of Dcoin.

upd2

Detailed answers to questions asked in the comments


Fake video


And what if we take and generate videos in some software, then register 100500 mine accounts, destroying Dcoin in the bud? Or hire make-up artists who will create thousands of new faces a day?
For little money, this is only possible in science fiction. In order not to be unfounded, I will bring proof links.

Computer fake


The minimum price for creating one such video will be from $ 2,000, although some experts say that in order to achieve a more or less acceptable level of realism, you need from $ 1 million for each video. Proof

Professional makeup


If you take, for example, one person and 50 make-up and razgrimirovat it?
It sounds pretty simple, but in fact such a pleasure would cost from $ 100k to $ 285k for 50 makeup. Proof
In the studio of Peter Gorshenin, where I was announced the amount of $ 285k, the process was described like this:
“It is necessary to remove the form from a person, to cast a model of his face. Then make the model parts of the face as much as there will be different parts, about 5 (separate forehead, nose, chin and two cheekbones). Next, they are made sculptures of changes from clay. Then the forms are removed from this again, in which the parts themselves will be cast from silicone. And all this 50 times with the exception of the first removal of the form from a person. ”

About the possibility of "farms"



Most likely, sooner or later, there will be “Farms”, where savvy businessmen will pay $ 10-20 for full account registration with the added amount promised and offline verification. But if such tricksters appear, then the profit they will receive from each such account will be no less than a few hundred dollars. If someone sends a request to exchange Dcoin for the promised amount indicated there to such a gray account, then it will be completely unprofitable for the owner of such a farm to get a freeze in the growth of coins for its promised amounts. After all, the received Dcoin can be easily sold on the exchange. And he will have to give the promised amount in exchange for the same amount of Dcoin. Those. such a farm, in fact, will not differ from the group of simple miners, the only thing that one person will get all the cream.
But large amounts of such farms will not be. Since when the first such scheme is detected, the rule for recording the first video will be added, which will say that you need to show a piece of paper to the camera, which says "I agree with the rules on democratic-coin.com". Most curiosities will go to the site to find out what they agree with. And on the site it will be written that selling an account for $ 10-20 is not very profitable, it is much more profitable to use this account yourself.

And what if you go to the remote province of India, where 85% of the population does not have the Internet, and shoot people there in the studio?
Again, the result is a farm that will not have a serious negative, because it would be unprofitable for the farm owner to ignore requests for his promised amounts.
But not to burn such a farm will be much more difficult, because To go offline and get lost in a large city will not work, and therefore have to pretend to be a resident of some small cities, which have not yet accumulated 25 miners. And all such cities will be carefully checked by the admin. It remains to find similar elements in the video and send requests with a request to make a new video.

The very existence of farms suggests that Dcoin has already developed and every mining account is a source of constant profit. And the owners of such farms get super-profits, taking advantage of the ignorance of the inhabitants. This fact will not be able to go unnoticed for a long time and sooner or later will get into the media. And all these ignorant people will very quickly become aware that it is more profitable to become a miner myself, especially since by that time you may not even need a computer, you just need to register once in some Internet cafe and continue to use your mobile phone like 37coins .

Sandbox for new miners


It's just impossible to take and flood the DC-network with bots-miners, because a new miner can register new users not earlier than in one month. And per month you can register no more than 2 users. Voting and exchange for cash to new miners also become available only in a month.

Day to check


And what if somewhere to get 10 accounts and put on 10 plus signs to their bots?
Nothing will come of it. Since Voting cannot be completed earlier than 24 hours from the occurrence of a transaction in a DC network. And if during this time the challenger collects 10 minuses, he will not become a miner. And so that all the miners have the motivation to check other miners, there are points, the shortage of which leads to a decrease in the% growth of the promised amounts.

Offline check


(In the process of implementation)
If within a radius of 25km from a user who wants to become a miner, there are at least 25 other miners, then 1 of them will be chosen pseudo-random.
Code
/** * @param int $user_id - user_id   . * @param int $size - -    ,     25. * @return int */ function get_checking_miner_id ($user_id, $size) { $hi = $user_id / 127773; $lo = $user_id % 127773; $x = 16807 * $lo - 2836 * $hi; if ($x <= 0) $x += 0x7fffffff; $key = ( ($user_id = $x) % ($size + 1)); return $key; } 

As can be seen from the algorithm, it is impossible to make sure that a particular miner does the check. The candidate’s miner’s contacts are sent to the chosen miner; he must call him and appoint a meeting place. Within 2 days they must meet, the chosen miner must take their photo together, put it on his node and send the transaction to the DC network.

For this, he will get 1000 points. He can also send a transaction in which he refuses to do a check. If 2 days has passed or a refusal transaction has been sent, the applicant will be entitled to send a new request to the next random miner.
If there are less than 25 miners within a 25 km radius, then the check takes place without a personal meeting. In a radius of 50,100,500,1000,10000,100000km, 25 miners are searched for, a pseudo-random is selected, a request is sent to it. He should call the applicant via Skype, look at him through the camera, take a photo, put it on his node, send the transaction to the DC-Network.
After the transaction with the photo is sent, the applicant gets the right to send a transaction to the DC network, in which he will indicate the link to his video, his photo hashes, and points on the photo. Next comes the miner check and vote.

Thus, the “farm” will not be lost in the crowd in a large city, and all miners, which will appear in places where there are no 25 other miners, will be additionally checked by the administrator.

About points


In order to receive a Miner%, you need to earn a number of points every month, no less than:
The total points of all miners for the month / number of miners * Coefficient.
The coefficient is currently equal to 0.3
If the points are lower, then next month there will be a user percentage.

Sending data to a DC network and checking nodes


After successfully completing an offline check, a user who wants to become a miner, uploads 2 photos to his node, marks points on them, points the url to the video and sends a transaction to the DC network. In the same transaction are the hashes of the photo.
Nodes receive this transaction in one of the blocks and if they find themselves among those who have to download these photos to themselves, they will automatically download them.
Nodes, which must keep the photo in themselves, are determined by this algorithm:
Code
 /** * @param int $block_id  ,     . * @param int $max_miner_id -  id   ,  -   . * @param int $miners_keepers -       .   10. * @return array */ function get_miners_keepers ($block_id, $max_miner_id, $miners_keepers) { for ($i=0; $i < $miners_keepers; $i++) { $hi = $block_id / 127773; $lo = $block_id % 127773; $x = 16807 * $lo - 2836 * $hi; if ($x <= 0) $x += 0x7fffffff; $miner_id = ( ($block_id = $x) % ($max_miner_id + 1)); $miner_id = ($miner_id==0)?1:$miner_id; $array[] = $miner_id; } return $array; } 

Those. make so that the photo saved any specific node is impossible.
Each node that has saved the photo in itself compares their hashes with the hashes specified in the transaction. If they converge, it sends a transaction to the DC network in which it reports that the test has passed. If> = 50% of the nodes have confirmed that they have successfully downloaded the photos to themselves, then the miner check begins. Checking the nodes occurs automatically, without the participation of a miner.

Check by miners


In the first step, you need to check whether the points on the photo are true.
Next, you need to compare photos and videos, whether there really is one and the same person. And also the photo received from the miner who did the check in offline.
After that, a page is displayed showing photos of similar people. You need to visually check if they are suspiciously similar. There is a choice of race, because for Europeans, all Asians are the same person, and for Asians, all Europeans are the same.

In this example, it is clear that the applicant had found the exact clone
If the photo is not displayed, then you need to click on the "reload" button, then this photo will be loaded from another node. If it is impossible to upload a photo with any of the 10 nodes, then there is a chance that our applicant for the miner simply zaddosil 10 nodes, where the photo from his previous account. Therefore, you need to refuse such a miner. For completing the task of checking the applicant in the miner is awarded 50 points

Will miners be responsible for fulfilling tasks?


If you have any btc count right now, then any negative in the media towards bitcoin will be unpleasant to you, since This negative can affect the btc rate, and, consequently, the number of benefits that you could buy for your btc. At the same time, when you did not have btc, then you had a more neutral attitude to the news about bitcoin. Right?
Imagine that you have bought Dcoin for some amount or you have added the promised amount, which brings a steady profit. Only in this case, the fate of Dcoin will really care for you. And if it will depend on you if someone’s clones pass to the DC network and can get undeserved coins, will you react responsibly to the verification procedure? At the same time, it is impossible to prohibit everyone in a row, because Dcoin must penetrate into the masses so that your coins can be exchanged for fiat without any problems.

Search for clones from the inside



We need one segment on each of the 2 photos, with which we will compare all the other segments.
Now this is the distance between the eyes in the photo full face and the distance from the eye to the edge of the ear in the photo in profile.

The spots_compatibility table stores:
1. Current version.
2. Points that are applied to 2 photos of the example.
3. Which version is compatible with.
4. What points with which we will connect to obtain segments.
5. Permissible discrepancies in the search for clones.

The faces table stores:
1. f1, f2 ... f20 - the ratio of the segments on the face full face to the distance between the eyes.
2. p1, p2 ... p20 is the ratio of the segments on the face in profile to the distance from the eye to the edge of the ear.
3. Version of spots_compatibility, which was at that time.

If a more effective set of points for searching clones is found, the data in the spots_compatibility table is updated and the segments will be entered into the faces table based on the new set of points.

In the first step of checking the applicant for the miner, we made sure that the points on the photo are plotted correctly, which means that the segments will also be obtained correct. It remains to simply make a request to the database, where the comparison with f1, f2 ... f20 and with p1, p2 ... p20 will take place, taking into account the allowable differences from spots_compatibility. As a result, we get the id miners, which are similar to the applicant.
Tests :
i7-2600k / centos i386 / standard mysql and php settings
RecordsTime in seconds
1 million0.12
3 million0.39
5 million0.8

Admin Check


Perhaps due to imperfect values ​​in spots_compatibility, clones will be able to skip. Therefore, the admin has the opportunity to degrade the already tested miners to users (the admin in Dcoin is needed for this, but not more than 1000 per day. As a result of this transfer, the funds on the miner's account will not disappear anywhere, they will simply grow with user%, and the promised amounts will stop bringing new coins.

Admin Picks


If the actions of the admin will not suit the majority, then the miners can vote for the transfer of admin functions to any user. If more than 50% of the votes are cast, then all admin functions will be completely transferred to the selected user.

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


All Articles