In my opinion, the fastest way to learn a new thing is to get hands-on practice, and constantly discover and solve problems in practice. Today we’re trying to get up close and personal with this new thing called Ethereum.

Install the geth

Almost every developer who touches Ethereum cannot fail to encounter Geth, the Ethereum client written in the Go language. The installation method is simple:

brew tap ethereum/ethereum
brew install ethereumCopy the code

Run the geth:

network

Once geth is installed, don’t run geth directly as a normal program. This is because Geth is not connected to the official Network of Ethereum. There are already over 5 million blocks in the network, and all of them are for real money transactions. Let’s try it out by connecting to Ethereum’s test network.

In addition to the official network, Ethereum has four test networks, and Geth has three working. Since the official network number is 1, the three test networks are numbered 2, 3, and 4 respectively. Test network 2, called Morden, is now obsolete; Ropsten number 3 and Rinkeby number 4 are named after the stations in London and Stockholm, Sweden, where their main maintainers live, and where a test network like Wudaokou may one day emerge.

At present, the test networks in use are mainly Ropsten and Rinkeby. The difference lies in that Ropsten uses PoW as the consensus mechanism, while Rinkeby uses PoA as the consensus mechanism. Don’t be daunted by these terrible nouns, as long as we know that the difference in usage is PoW. If you want to acquire Ether, you need to mine it yourself. PoA does not and cannot mine it. You need to ask the network administrator for a few ether coins to experiment with. The specific differences between these networks can be seen in this article.

The way to specify a network is simple. Suppose we want to use the Rinkeby network, we would write:

geth --rinkeby

The data file

Although the network is specified, we can’t launch Geth right away because the default path for geth data files is ~/Library/Ethereum. If we launch geth directly, the data files are placed in this directory that was reserved for the official network numbered 1. If we put our data files here, they will be overwritten if we want to start the network again, so we need to create a subfolder in this directory, rinkeby, and tell Geth to store our data files in this subfolder:

geth --rinkeby --datadir=$HOME/Library/Ethereum/rinkeby

Synchronous mode

Before starting Geth, we also need to tell Geth which way we want to synchronize data. Geth has three ways to synchronize data: Full, Fast, and Light. The fast synchronization mode is faster than the full mode, which only verifies the latest 1024 blocks, while the light mode is the fastest, because it verifies only the latest state. The default synchronization for Geth is fast, and with more than 1.8 million blocks currently available on Rinkeby, synchronization takes about half an hour to two hours, so you’ll need to be patient here. The command to specify the synchronization mode is as follows:

geth --rinkeby --datadir=$HOME/Library/Ethereum/rinkeby --syncmode=fast

Starting the RPC Server

To enable our application to connect to GEth, we also need to start the RPC server at the same time as geTH. This command is very simple:

geth --rinkeby --datadir=$HOME/Library/Ethereum/rinkeby --syncmode=fast --rpc

Connect to the status server

Has the GETH environment that we have worked so hard to build been recognized? Let’s connect it to rinkeby’s official status server and have a look:

geth --rinkeby --datadir=$HOME/Library/Ethereum/rinkeby --syncmode=fast --rpc --ethstats='fengerzh:Respect my authoritah! @stats.rinkeby.io'

Note: Fengerzh is the name I gave my machine, you need to change it to a name that will not be repeated by others, otherwise you will get an error. Respect my authoritah! It’s a password, but it has to be like this, or it won’t pass either.

Ok, we are finally connected, run the command to see the effect:

The node we just set has appeared on the official website, indicating that our setting has been successful!

The configuration file

The above list of commands is too long to remember what to do? We can export the configuration information to a configuration file:

geth --rinkeby --datadir=$HOME/Library/Ethereum/rinkeby --syncmode=fast --rpc --ethstats='fengerzh:Respect my authoritah! @stats.rinkeby.io' dumpconfig > ~/rinkeby.toml

So that when we do it again, we do it directly

geth --config ~/rinkeby.toml console

It’ll be ok.

account

Establish account

Now that our geth environment is up and running, we need to create an account in it, preferably with a few Ether coins to play with. We can create an account with a simple command:

geth account new --datadir=~/Library/Ethereum/rinkeby/

Don’t forget to add datadir here, otherwise your account will be on the official network instead of rinkey’s test network.

Demand Ether

As we mentioned above, since Rinkeby’s network is PoA based, the only way you can get ether here is to ask for it, not mine it, so you need to go to the site, click on one of the three links below, and post the information about the account you just created on Twitter. Then paste the Twitter url back to the same site, and after a while you’ll receive 3 Ether tokens from the administrator.

Is 3 ether too little? You can go to this website to convert the amount of wei for 3 Ether coins:

Typically, the deployment cost of a smart contract is about 1,400,000 gas, depending on the complexity of your contract. For example, an addition operation costs you 3 gas, a multiplication operation costs 5 gas, and creating a new account costs 32,000 gas. And gas but also by the unit price into the etheric currency conversion, like gasoline, the deployment of a smart deal cost 1.4 million litres of oil, and the price of a litre of oil is not fixed, can be up to you, you to high oil prices, is quickly, you give price low, calculate is slow, if is too low, so no one will forget it, official web oil can look it up in here, The oil price of rinkeby, a testing network, is shown on the official website. The current price is about 20.687gwei. So, the cost of a smart contract is about 1400000*20.687= 28961800Gwei, equivalent to 0.0289618 Ether. So we can deploy about 100 contracts with three ether coins. For a formal network, the unit price of gasoline is less expensive, about 2 or 3gwei, which translates into 0.003 ether. Currently, the price of ether is about 5000 RMB, so the price of a smart contract is 15 RMB.

If that’s too high a price to pay, consider starting your own private network, which I’ll discuss in another article.