[Ethereum Development] Getting started with geth

The full name of geth is go-ethereum, which is an Ethereum client. It is written in go language and should be the most commonly used client at present. Of course, Ethereum clients are also written in C++, Ruby, Python, Java and other languages. Different types of clients are used to meet different demand scenarios. Today we mainly introduce the use of geth (pronounced with guess).

      For our development, there are generally two ways to open the geth console, login in development mode and login in private chain mode. These two methods are completely different, and the accounts of the two are not shared. It is relatively simple to log in in the development mode, and additional configuration is required to log in in the private chain mode, which I will introduce in a later blog.

(1) Open the Terminal and execute the following command to start geth in development mode

geth  --datadir “c:/ethdev” --dev 

This will create an ethdev folder in the current root directory. Of course, you can choose the name of this folder by yourself. We don't care what this folder is used for, and will be introduced later.



(2) After geth starts successfully, reopen a terminal and execute the following command to open the geth console:

geth --dev console 2>>file_to_log_output

This command will open the geth console and generate a log file called file_to_log_output in the directory. We will look at this log file later.



(3) Check which accounts currently have

eth.accounts

.

It can be seen that there are currently no accounts.

(4) Create a new account

personal.newAccount('password')


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


It turns out that there are two accounts.

(5) The account can be assigned to a variable, and the balance in the account can be queried at the same time


It can be seen that the ether in both accounts is 0. Because we have not started mining yet.

(6) The log file was mentioned above, 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 there is any operation in the geth console, you can come here to see the log.

(7) Start mining in the geth console:

miner.start()



来观察日志输出:


可以看到在日志界面中已经显示在挖矿了。


(8)停止挖矿

miner.stop()


此时也可以看到日志界面输出也停止了。


(9)此时我们再来查看两个账户的账户余额


由此可见,挖矿所得的以太币默认是传入第一个账户的。


(10)user1向user2转移以太币

eth.sendTransaction({from: user1,to: user2,value: web3.toWei(3,"ether")})


提示我们账号是锁定的,所以我们需要解锁账户。


解锁账户后,转移操作就成功了。打印出来的是本次交易的地址。


(11)再次查看以太币转移后的user2账户余额


但是发现user2的账户还是为0.这是因为没有矿工来挖矿处理。根据区块链的概念,我们知道,每次交易的确认,其实都是需要挖矿的,也就是被其他矿工共识确认,然后才能加入区块链的账本中。

好,我们执行“miner.start()”开始挖矿,

.

然后就发现账户user2有3个以太币了。转账操作成功。


(12)下面我们来实现下在geth中编译一个智能合约

[plain]  view plain  copy
  1. contract test {  
  2.     function multiply(uint a) returns(uint d) {  
  3.         return a * 7;   
  4.     }   
  5. }  
在geth中执行如下:

source = "contract test { function multiply(uint a) returns(uint d) { return a * 7; } }"

然后编译该合约:

contract = eth.compile.solidity(source).test

编译后的结果如下:


code:就是合约编译后的字节码文件,在以太坊虚拟机EVM上运行的就是这个字节码;

abiDefinition:其实就是合约的二进制接口,可用来外部调用;

其他的一些参数也都是可以见名知意了。


然后就要获取合约的abi(其实该真正要用的abi可以通过https://ethereum.github.io/browser-solidity这个在线编译器获得):

abi=[{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"payable":false,"type":"function"}]



然后是访问abi接口:

MyContract=eth.contract(abi)




然后获得合约的实例:

myContract=MyContract.new({from:user1,data:contract.code})


如果提示你没有解锁的话,只要解锁即可。


调用函数传递参数


函数做了乘以7的操作,输出符合预期。合约执行成功。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325772335&siteId=291194637