Getting started with the use of Geth

Source: Internet
Author: User

Geth's full name is Go-ethereum, is an Ethernet square client, written in the go language, should be currently the most commonly used clients. Of course, the Ethernet square client is also useful C++,ruby,python,java and other languages written, different types of clients to meet the different requirements of the scene. Today we will mainly introduce the use of geth (pronounced with guess).

For our development, there are generally two ways to open the Geth console, the development of the way to login and private link login. These two ways are completely different, the account is not shared, which is the development of the way to log in relatively simple, the private link to login also need to carry out additional configuration, I will be in the back of the blog to introduce.

(1) Open the terminal terminal and execute the following command to start the Geth in development mode

Geth--datadir "~/ethdev"--dev

This will create a Ethdev folder under the current root directory, of course, the name of the folder you can take it yourself, we will leave this folder for what it is used, and later introduced.



(2) After the successful start of Geth, reopen a terminal, execute the following command, open the Geth console:

Geth--dev Console 2>>file_to_log_output

This command opens the Geth console, while generating a log file called File_to_log_output in the directory, which we will check for this log file.



(3) See what accounts are currently available

Eth.accounts

.

Visible there are no accounts currently.

(4) Create a new account

Personal.newaccount (' password ')


We create two accounts here. Then use the eth.accounts to view the account:


It was found that there were two accounts.

(5) The account can be assigned to a variable, while the balance of the account is queried


You can see that the two-account Ethernet currency is 0. Because we haven't started digging mine yet.

(6) The above mentioned log file, we reopen a terminal, we use the following command to open the log:

Tail-f File_to_log_output


We put this terminal interface aside, and when we have any action in the Geth console, we can look at the log here.

(7) Start ore digging in Geth console:

Miner.start ()



To observe log output:


Can be seen in the log interface has been shown in the mining.


(8) Stop digging mine

Miner.stop ()


At this point you can also see the log interface output also stopped.


(9) We will check the balance of two accounts at this time


This shows that the mining of the etheric currency by default is passed into the first account.


(a) User1 transfer of etheric currency to User2

Eth.sendtransaction ({From:user1,to:user2,value:web3.towei (3, "Ether")})


Our account is locked, so we need to unlock the account.


Once the account is unlocked, the transfer operation succeeds. The address of this transaction is printed.


(11) Re-view the User2 account balance after the transfer of the Ethernet currency


But User2 's account was found to be 0. This is because there are no miners to dig the mines. According to the concept of block chain, we know that the confirmation of each transaction, in fact, are required to dig the mine, that is, the consensus confirmed by other miners, and then to join the block chain of the ledger.

Well, we're doing "miner.start ()" To start digging,

.

Then we find that the account User2 has 3 Ethernet coins. The transfer operation was successful.


(12) Let's implement the following to compile an intelligent contract in Geth

[plain] view plain copy print?        Contract Test {function multiply (uint a) returns (UINT D) {return a * 7; Perform the following in Geth:

Source = "Contract Test {function multiply (uint a) returns (UINT D) {return a * 7;}}"

Then compile the contract:

Contract = eth.compile.solidity (source). Test

The results of the compilation are as follows:


Code: is the contract compiled bytecode file, in the etheric square virtual machine EVM running on the byte code;

Abidefinition: is actually the contract binary interface, can be used for external calls;

Some of the other parameters can be known.


Then you get the ABI for the contract (in fact, the ABI you really want to use can be obtained by https://ethereum.github.io/browser-solidity this online compiler):

Abi=[{"constant": false, "inputs": [{"Name": "A", "type": "uint256"}], "name": "Multiply", "outputs": [{"Name": "D", "type ': ' uint256 '}], ' payable ': false, ' type ': ' function '}]



Then you access the ABI interface:

Mycontract=eth.contract (ABI)




Then get an example of the contract:

Mycontract=mycontract.new ({From:user1,data:contract.code})


If you're prompted not to unlock it, just unlock it.


Calling function Pass Arguments


The function is multiplied by 7, and the output conforms to expectations. The contract was executed successfully.

The above is the most basic use of geth, we will be in the future development of learning geth more in-depth functions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.