geth+remix ide简单介绍

私有链搭建

第一步:
下载并安装geth客户端
Geth客户端网址:https://ethfans.org/wikis/Ethereum-Geth-Mirror
geth+remix ide简单介绍
一直点下一步,直至完成。
安装完成后,为了方便使用,需要将geth添加进系统变量。

第二步:新建一个文件夹BlockChain,并在里面创建一个Genesis.json文件。打开Genesis.json文件编写创世区块。创世区块可以理解为区块链的第一个区块,例子如下:
geth+remix ide简单介绍
第三步:打开命令行,执行命令geth --datadir data0 init genesis.json,运行结果如下:
geth+remix ide简单介绍
第四步:执行geth --datadir data0 --networkid 1108 console,运行结果如下表示私有链创建成功。
geth+remix ide简单介绍
这是一个交互式的javascript console界面,可以在里面输入命令
例如:
personal.newAccount()创建一个账号,绿色就是账号
eth.accounts查看已创建的账号
geth+remix ide简单介绍
eth.getBalance(xxx)可以获取账户的余额,单位是wei,1个以太币=10的18次方个wei
web3.fromWei()将返回值换算成以太币:
geth+remix ide简单介绍
geth+remix ide简单介绍
miner.start(xx)启动挖矿,xx表示线程数,下图中number表示挖到的区块的编号
miner.stop()停止挖矿,返回结果true
geth+remix ide简单介绍
geth+remix ide简单介绍
再次查看账户的余额,会发现账户的余额变多了
geth+remix ide简单介绍
eth.blockNumber查看区块数量
geth+remix ide简单介绍
eth.getBlock(xx) xx表示区块号,查看区块的具体信息
geth+remix ide简单介绍
eth:包含一些跟操作区块链相关的方法
admin:包含一些与管理节点相关的方法
miner:包含启动&停止挖矿的一些方法
personal:主要包含一些管理账户的方法
txpool:包含一些查看交易内存池的方法
web3:包含了以上对象,还包含一些单位换算的方法

也可以同时开启多个console,一个console挖矿,另一个console输入命令。首先启动私有链时,添加—rpc开启rpc接口,geth --datadir data0 --networkid 1108 console –rpc,然后在新的终端上输入geth attach http://127.0.0.1:8545 console,即可打开另一个终端。

更多命令可以查看文档https://geth.ethereum.org/docs/getting-started

remix-ide连接私有链

启动私有链时输入命令
geth --datadir data0 --networkid 1108 console --rpc --rpccorsdomain http://remix.ethereum.org
当看到“HTTP endpoint opened url=http://127.0.0.1:8545 cors=http://remix.ethereum.org vhosts=localhost”这条信息时,说明私有链的rpc接口启动成功,此时可以在remix-ide连接私有链。
geth+remix ide简单介绍
打开remixide http://remix.ethereum.org/ remixide的具体使用在下篇介绍。
geth+remix ide简单介绍
点击左边第三个按钮,进入依赖和运行交易的界面
geth+remix ide简单介绍
在Environment中选择web3 Provider,在弹出框输入http://127.0.0.1:8545,点击OK。此时就可以在左边看到私有链的账户信息,并可以在私有链上测试智能合约。
geth+remix ide简单介绍
geth+remix ide简单介绍

Remix-ide介绍

Remix-ide是一款基于浏览器的ide,一些ide常有的功能它都有。用remix-ide可以开发以太坊区块链的智能合约。智能合约的开发语言是solidity。Solidity文档地址https://solidity-cn.readthedocs.io/zh/develop/下面是页面介绍。
geth+remix ide简单介绍
左边按钮从上到下依次是文件管理,编译、依赖测试、debugger、插件。
我们在ide新建一个文件helloworld.sol,代码示例如下
geth+remix ide简单介绍
第一行表示编译器版本
Contract表示合约,建立了一个叫Helloworld的智能合约,里面是合约的函数。

接着点击编译,编译器版本选择0.4.26或更高版本但只能选择0.4.xx版本,点击Compile Helloworld.sol,显示界面如下表示编译成功。
geth+remix ide简单介绍
点击依赖(左边第三个按钮),可以在Environment中选择环境,第一个JavaScript VM是ide会自动创建一个虚拟器,虚拟机内部提供若干个区块链账号,供你测试代码。也可以选择第三个Web3Provider,连接自己搭建的私有链测试,连接方法如上。
点击Deploy,智能合约就已经部署到区块链上了。此时下方会出现部署的合约,并且可以通过点击sayHello调用合约中的sayHello()方法。调用sayHello()方法后,即可在下方控制台查看结果。
geth+remix ide简单介绍
geth+remix ide简单介绍