People interested in the blockchain theme have already heard about the project of the Russian-Canadian programmer Vitalik Buterin - Ethereum , and along with it about the so-called smart contracts. In this series of articles, I will try as simple as possible to describe the essence of Ethereum, smart contracts, the concept of gas and show how smart contracts are written.
If on the fingers, a “smart contract” is some code that lives inside the blockchain. Any member of the network can call it for a small fee. This fee is called Gas, literally "fuel." Why do you need it? To protect the miner from the abuse of fraud his resources.
Few people know, but even in Bitcoin there is an opportunity to write these very contracts, but for some reason very few people do it. One of the main problems is that the Script language is not Turing-complete and it is not easy to write something less serious (so that you understand the scale of the problem, there is not even the possibility to add a loop). In the case of Ethereum, everything is a little different, Turing languages ​​are full, and there is a risk that someone will write a contract of the form.
// foo = 0; while (True) { foo++; }
It is clear that the miner who launched this contract will not finish soon and in fact will simply spend his resources to nowhere. So that this did not happen, the developers of Ethereum and invented gas - in reality, it would be economically inexpedient to run a code like what I wrote, because the caller will have to pay for every contract action.
The cost of calling a contract in gas is very easy to calculate - the ready code can be compiled and presented as a sequence of assembler commands. Here is the online compiler , it already has a sample code, you just need to click Compile > Toggle Details > Assembly . For each team there is a hardcoded cost.
At the moment, the cost of one gas is 50 wei - that is, 50 * 10 ^ -18 ether.
In order to start working with Ethereum, it is not necessary to synchronize the entire current blockchain - the platform makes it easy to create a so-called "private blockchain", which we will do. This is reasonable not only because it will take you a couple of days to download the whole chain, but also because you can play with the contracts without paying real money for it (and you have to pay for gas, including loading the contract into the blockchain).
We will use geth . It is written in Go and is recommended to be used in most articles. Here is his official documentation .
sudo apt-get install software-properties-common sudo add-apt-repository -y ppa:ethereum/ethereum sudo apt-get update sudo apt-get install ethereum
Installation instructions for other operating systems can be found here . Here you can find other client implementations - in C ++ or in Python.
To get started, just write in the terminal geth
. This command will start the node to work in the main blockchain and start synchronizing the blocks. We will not do this, but if you really need, I recommend using geth --fast --cache 1024
. The important point is that with the --fast flag you download only block headers and, according to the Internet, you can download 500,000 blocks in half an hour, which is an order of magnitude faster than classical synchronization. But if you have already started downloading without this flag, you will have to delete everything and start again. Read more here .
The main option for us is the console, which will allow us to work with geth in an interactive mode. We will also use the --dev flag, which runs geth in private blockchain mode. geth --dev console
. We assume that now you have a good deal with the subtleties of geth. As an example, below is the code of work with the miner, I think you will understand everything yourself.
> personal.newAccount("123") // "123" "0x07ae7ebb7b9c65b51519fc6561b8a78ad921ed13" // > eth.accounts // ["0x07ae7ebb7b9c65b51519fc6561b8a78ad921ed13"] > miner.setEtherbase(eth.accounts[0]) // true > eth.coinbase // "0x07ae7ebb7b9c65b51519fc6561b8a78ad921ed13" // > miner.start() // , 31,32,... true I1005 09:25:44.363901 miner/miner.go:136] Starting mining operation (CPU=2 TOT=3) I1005 09:25:44.364247 miner/worker.go:539] commit new work on block 31 with 0 txs & 0 uncles. Took 291.8µs I1005 09:25:45.049267 miner/worker.go:342] Mined block (#31 / 4ca861c2). Wait 5 blocks for confirmation I1005 09:25:45.049567 miner/worker.go:539] commit new work on block 32 with 0 txs & 0 uncles. Took 133.101µs I1005 09:25:45.049976 miner/worker.go:539] commit new work on block 32 with 0 txs & 0 uncles. Took 121.3µs I1005 09:25:45.632474 miner/worker.go:342] Mined block (#32 / f79f0df7). Wait 5 blocks for confirmation I1005 09:25:45.632796 miner/worker.go:539] commit new work on block 33 with 0 txs & 0 uncles. Took 182.601µs I1005 09:25:45.632915 miner/worker.go:539] commit new work on block 33 with 0 txs & 0 uncles. Took 86.9µs I1005 09:25:46.441888 miner/worker.go:342] Mined block (#33 / 16e99579). Wait 5 blocks for confirmation I1005 09:25:46.442257 miner/worker.go:539] commit new work on block 34 with 0 txs & 0 uncles. Took 268.9µs I1005 09:25:46.442440 miner/worker.go:539] commit new work on block 34 with 0 txs & 0 uncles. Took 120.201µs > miner.stop() true > eth.getBalance(eth.coinbase) // 15000000000000000000
The attentive reader noticed that everything written suspiciously reminds JS - this is what he is. For example, the function code that displays accounts with balances in a readable form:
function checkAllBalances() { var totalBal = 0; for (var acctNum in eth.accounts) { var acct = eth.accounts[acctNum]; var acctBal = web3.fromWei(eth.getBalance(acct), "ether"); totalBal += parseFloat(acctBal); console.log(" eth.accounts[" + acctNum + "]: \t" + acct + " \tbalance: " + acctBal + " ether"); } console.log(" Total balance: " + totalBal + " ether"); };
Save this code in geth_scripts.js and run it.
> loadScript("geth_scripts.js") true > checkAllBalances() eth.accounts[0]: 0x07ae7ebb7b9c65b51519fc6561b8a78ad921ed13 balance: 15 ether undefined
Now that you have become a recognized Geth expert, you can close the console with peace of mind and start using the GUI as a white man.
Mist is by far the most common wallet for Ethereum. Written using Meteor, cross-platform, for installation you just need to download the installation file from the releases page .
To date, there is no so-called light wallet that would allow working with contracts, so the first thing Mist will offer you is to synchronize either the Main network or Test-net. We don't need all this for now, we will launch Mist on our private blockchain. This is very easy to do:
geth --dev --rpc --rpcaddr "0.0.0.0" --rpcapi "admin,debug,miner,shh,txpool,personal,eth,net,web3" console mist.exe --rpc http://localhost:8545
The first line does the same thing as before, but this time we also open the HTTP-RPC server, defaulting to http: // localhost: 8545 . The rpcapi flag defines the permission set that the Mist will have after connecting to the server. In this case, all that is indicated is general. If, for example, you do not specify personal, then Mist will not be able to create new accounts, etc. News set of command line options is listed here .
The second line starts Mist with the specified RPC server. If everything worked correctly, then the Private-net message should appear on startup.
It's time to create your first contract. To do this, click on Contracts > Deploy new contract .
In the Solidity contract source code window we write:
contract mortal { /* */ address owner; /* - owner , */ function mortal() { owner = msg.sender; } /* selfdestruct , */ /* Ethereum */ function kill() { if (msg.sender == owner) selfdestruct(owner); } } /* is */ /* contract_1 is contract_2, contract_3*/ contract greeter is mortal { string greeting; /* - "Hello, world!"*/ function greeter(string _greeting) public { greeting = _greeting; } // "Hello, world!" function greet() constant returns (string) { return greeting; } }
The language itself is pretty simple, here is its documentation , many questions have already been discussed at ethereum.stackexchange.com , in principle there is a chance to get answers in gitter .
After you selected the greeting contract, you specified "Hello, world" as the argument, clicked Deploy and entered a password, your contract only needs to be "minted" in the blockchain. To do this, open a terminal with Geth enabled and a pair of mines (as described above). Everything!
Now going to the Contracts tab you will see a new contract with the name GREETER 7D5D , or something like that. Click on it and enjoy the result. If you wish, you can kill him by selecting the Kill function in the drop-down list on the right.
PS My first article, scold strictly. In the following parts, I think to write a cryptocurrency, tell about EVM nuances, show how to tie browser interfaces to Ethereum and maybe something else. Sound criticism is welcome, thank you for your attention :)
Source: https://habr.com/ru/post/312008/
All Articles