并发生交易,以太坊代币智能合约底层开荒教程Geth基本命令

Posted by

第一节中询问了选择geth搭建以太坊私有网络,那1回则要在私有互联网中国建工业总会公司立四个node组成的集群,并相互开掘,发生交易.

上一节中大家应用geth搭建了以太坊私有网络,这三回则要在个体互连网中确立多节点集群,并互相发掘,发生交易.

区块链爱好者(QQ:5301635三)  

为了在本地网络运转四个以太坊节点的实例,必须保证一下几点:
1.
每种实例都有独立的数目目录(–datadir)
2.
各种实例运营都有单独的端口.(eth和rpc两者都以)(–port 和
–rpcprot)
三.
在集群的情景下, 实例之间都必须求明白相互.
四.
唯壹的ipc通讯端点,也许剥夺ipc.

为了在本地网络运营八个以太坊节点的实例,必须确认保证以下两点:

跻身Geth 命令行方式

开首第一个节点(钦定端口,并禁止使用ipc),运营命令和结果如下:

  • 各类实例都有独立的数码目录--datadir
  • 种种实例运维都有独立的端口.

咱俩得以用上边发号施令,建立三个新的私有链

~/Documents/private-geth  geth --datadir ./data/00 --networkid 314590 --ipcdisable --port 61910 --rpcport 8200 console
INFO [05-29|16:18:59] Starting peer-to-peer node               instance=Geth/v1.6.1-stable-021c3c28/linux-amd64/go1.8.1
INFO [05-29|16:18:59] Allocated cache and file handles         database=/home/zl/Documents/private-geth/data/00/geth/chaindata cache=128 handles=1024
INFO [05-29|16:18:59] Initialised chain configuration          config="{ChainID: 15 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Engine: unknown}"
INFO [05-29|16:18:59] Disk storage enabled for ethash caches   dir=/home/zl/Documents/private-geth/data/00/geth/ethash count=3
INFO [05-29|16:18:59] Disk storage enabled for ethash DAGs     dir=/home/zl/.ethash                                    count=2
INFO [05-29|16:18:59] Initialising Ethereum protocol           versions="[63 62]" network=314590
INFO [05-29|16:18:59] Loaded most recent local header          number=29 hash=8ff3ff…dac4a2 td=7372364
INFO [05-29|16:18:59] Loaded most recent local full block      number=29 hash=8ff3ff…dac4a2 td=7372364
INFO [05-29|16:18:59] Loaded most recent local fast block      number=29 hash=8ff3ff…dac4a2 td=7372364
WARN [05-29|16:18:59] Blockchain not empty, fast sync disabled 
INFO [05-29|16:18:59] Starting P2P networking 
INFO [05-29|16:19:01] Mapped network port                      proto=udp extport=61910 intport=61910 interface="UPNP IGDv1-IP1"
INFO [05-29|16:19:02] RLPx listener up                         self=enode://ad307e052d0e04af519b8999fa870800df8a7a0cc2a91e6aea30e879b75c344dfa12c773a63a71677c2a3ea1254cf982815817f7ff58bd79e5837ea44d791a2d@192.168.1.2:61910
INFO [05-29|16:19:02] Mapped network port                      proto=tcp extport=61910 intport=61910 interface="UPNP IGDv1-IP1"
Welcome to the Geth JavaScript console!

instance: Geth/v1.6.1-stable-021c3c28/linux-amd64/go1.8.1
coinbase: 0x5fba50fce50baf0b8a7314200ba46336958ac97e
at block: 29 (Mon, 29 May 2017 13:13:46 CST)
 datadir: /home/zl/Documents/private-geth/data/00
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

搭建多节点意况

geth –datadir “./” –nodiscover console 2>>geth.log
进去命令行格局,当中参数

地方的下令以命令行的(console)的点子运行了节点,
所以我们能够通过一而再输入上边包车型客车指令获取节点实例的enode url:

伊始第壹个节点

geth --datadir data0 --networkid 1108 console

–datadir 代表文件夹地址,
–nodiscover 代表该链条不指望被其余节点开采,
console >> geth.log 代表将调节台出口到文件geth.log中去
本来从命令行情势剥离,也很轻易,只要打入exit, 就可以退出

>admin.nodeInfo.enode
"enode://ad307e052d0e04af519b8999fa870800df8a7a0cc2a91e6aea30e879b75c344dfa12c773a63a71677c2a3ea1254cf982815817f7ff58bd79e5837ea44d791a2d@192.168.1.2:61910"
>

运作结果如下:

INFO [03-01|10:57:17] Starting peer-to-peer node               instance=Geth/v1.7.3-stable/darwin-amd64/go1.9.2
INFO [03-01|10:57:17] Allocated cache and file handles         database=/Users/wangsanjun/privatechain/data0/geth/chaindata cache=128 handles=1024
INFO [03-01|10:57:17] Initialised chain configuration          config="{ChainID: 10 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Engine: unknown}"
INFO [03-01|10:57:17] Disk storage enabled for ethash caches   dir=/Users/wangsanjun/privatechain/data0/geth/ethash count=3
INFO [03-01|10:57:17] Disk storage enabled for ethash DAGs     dir=/Users/wangsanjun/.ethash                        count=2
INFO [03-01|10:57:17] Initialising Ethereum protocol           versions="[63 62]" network=1108
INFO [03-01|10:57:17] Loaded most recent local header          number=72 hash=8df4da…335b9f td=9710147
INFO [03-01|10:57:17] Loaded most recent local full block      number=72 hash=8df4da…335b9f td=9710147
INFO [03-01|10:57:17] Loaded most recent local fast block      number=72 hash=8df4da…335b9f td=9710147
INFO [03-01|10:57:17] Loaded local transaction journal         transactions=0 dropped=0
INFO [03-01|10:57:17] Regenerated local transaction journal    transactions=0 accounts=0
WARN [03-01|10:57:17] Blockchain not empty, fast sync disabled 
INFO [03-01|10:57:17] Starting P2P networking 
INFO [03-01|10:57:19] UDP listener up                          self=enode://c1f6afa8c620b842b6ff36b9321ec2f7dd6a08037a31ee093e9acc4c5a13bcb93b5ce95b10768eedafe144f55c79858a8b8e59f121674dbe267d23dba1df3e7a@[::]:30303
INFO [03-01|10:57:19] RLPx listener up                         self=enode://c1f6afa8c620b842b6ff36b9321ec2f7dd6a08037a31ee093e9acc4c5a13bcb93b5ce95b10768eedafe144f55c79858a8b8e59f121674dbe267d23dba1df3e7a@[::]:30303
INFO [03-01|10:57:19] IPC endpoint opened: /Users/wangsanjun/privatechain/data0/geth.ipc 
Welcome to the Geth JavaScript console!

instance: Geth/v1.7.3-stable/darwin-amd64/go1.9.2
coinbase: 0x0416f04c403099184689990674f5b4259dc46bd8
at block: 72 (Wed, 28 Feb 2018 18:33:09 CST)
 datadir: /Users/wangsanjun/privatechain/data0
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

Geth命令行中的Eth.accounts
大家在命令行输入 eth.accounts
可以看出目前该区块链中国共产党有多少个账号,以及各种账号的公钥地址。

###########################获得本机IP地址###########################

赢得节点实例的enode url:

> admin.nodeInfo.enode
"enode://c1f6afa8c620b842b6ff36b9321ec2f7dd6a08037a31ee093e9acc4c5a13bcb93b5ce95b10768eedafe144f55c79858a8b8e59f121674dbe267d23dba1df3e7a@[::]:30303"

p1

$ ifconfig|grep netmask|awk '{print $2}'
127.0.0.1
192.168.1.102

再展开多少个终端,发轫化第3个节点:

mkdir data1 //和上一个节点的data0同目录,即在privatechain下面
geth --datadir data1 account new
geth --datadir data1 --networkid 1108 init genesis.json
geth --datadir data1 --networkid 1108 --port 30304 --bootnodes "enode://c1f6afa8c620b842b6ff36b9321ec2f7dd6a08037a31ee093e9acc4c5a13bcb93b5ce95b10768eedafe144f55c79858a8b8e59f121674dbe267d23dba1df3e7a@172.16.70.76:30303" console

地方的授命中,--bootndoes是设置当前节点运维后,直接通过安装--bootndoes的值来链接第2个节点,--bootnoedes
的值能够透过在首先个节的吩咐行中,输入admin.nodeInfo.enode一声令下打字与印刷出来,必要小心的是索要把@[::]沟通实际电脑的IP地址
也能够不安装 --bootnodes, 直接开发银行,运营后进入命令行,
通过命令admin.addPeer(enodeUrlOfFirst Instance)把它作为1个peer加多进来.

此地就要谈到以太坊的账户体系了,

###########################

为了确认链接成功,第三个节点输入:

> admin.nodeInfo
{
  enode: "enode://61eac7f7900bd17cac39ce506cd20c1f67a2a43fb5f5c7a46ace337922a9e325a63a7aa9cb18df12715550ffa0b66c44272132b93245d15825c2ac2c1ddb8cd8@[::]:30304",
  id: "61eac7f7900bd17cac39ce506cd20c1f67a2a43fb5f5c7a46ace337922a9e325a63a7aa9cb18df12715550ffa0b66c44272132b93245d15825c2ac2c1ddb8cd8",
  ip: "::",
  listenAddr: "[::]:30304",
  name: "Geth/v1.7.3-stable/darwin-amd64/go1.9.2",
  ports: {
    discovery: 30304,
    listener: 30304
  },
  protocols: {
    eth: {
      difficulty: 9972291,
      genesis: "0x5e1fc79cb4ffa4739177b5408045cd5d51c6cf766133f23f7cd72ee1f8d790e0",
      head: "0xbcc9172f244c3c86d15b04451d2d72dca6953cc1838910f35f486149a37ddc87",
      network: 1108
    }
  }
}

在以太坊连串中,状态是由被誉为“账户”(各个账户由贰个20字节的地点)的靶子和在多个账户里面转变价值和信息的境况转变构成的。以太坊的账户包涵多个部分:
专断数,用于分明每笔交易只可以被拍卖一回的计数器
账户近日的以太币余额
账户的合同代码,要是有个别话
账户的蕴藏(默感觉空)
简短地说,每三个以太坊账户都有1对公钥和私钥组成。

再展开四个极限,初叶化第3个节点:

先是个节点输入:

> net.peerCount
2
> admin.peers
[{
    caps: ["eth/63"],
    id: "61eac7f7900bd17cac39ce506cd20c1f67a2a43fb5f5c7a46ace337922a9e325a63a7aa9cb18df12715550ffa0b66c44272132b93245d15825c2ac2c1ddb8cd8",
    name: "Geth/v1.7.3-stable/darwin-amd64/go1.9.2",
    network: {
      localAddress: "172.16.70.76:30303",
      remoteAddress: "172.16.70.76:53634"
    },
    protocols: {
      eth: {
        difficulty: 131072,
        head: "0x5e1fc79cb4ffa4739177b5408045cd5d51c6cf766133f23f7cd72ee1f8d790e0",
        version: 63
      }
    }
}]

从获得的结果能够看到,第3个节点有二个peer链接, 链接的node id为:
“61eac7f7900bd17cac39ce506cd20c1f67a2a43fb5f5c7a46ace337922a9e325a63a7aa9cb18df12715550ffa0b66c44272132b93245d15825c2ac2c1ddb8cd8”
其1id,正好正是第贰个节点的id.

公钥大家能够知道为正是账户地址,任何其余账户都可以访问该地点
私钥能够知晓为壹段加密过的密码,那壹对公钥和私钥共同组成一个唯一标示的以太坊账户。
举个例子说在上节大家树立的第2个以太坊账户 eth.accounts[0] 中,地址
0xbcf5b841303bc0802陆ce②d叁b八f834玖八ffe4二c1二f
正是公钥,而对密码加密而成的,正是私钥。

geth --datadir ./data/01 init ./genesis.json 

链接成功后,使用第三个节点挖矿的账户,向第四个节点发送 “ether”

什么新扩大账户
作者们能够输入指令 personal.newAccount(“1贰叁”)
来新建3个账户,(注意12三方可修改为别的别的密码)

伊始第三个节点:

首先查看第一个节点的Wei数量和整个网络的区块号,还有接收货币的账号id:

> eth.getBalance(eth.accounts[0])
0
> eth.blockNumber
73
> eth.accounts[0]
"0x0c7df03a68bd85ef3abcfc63a6b788a2faf8ef27"
>

p2

 ~/Documents/private-geth  geth --datadir ./data/01 --networkid 314590 --ipcdisable --port 61911 --rpcport 8101 --bootnodes "enode://ad307e052d0e04af519b8999fa870800df8a7a0cc2a91e6aea30e879b75c344dfa12c773a63a71677c2a3ea1254cf982815817f7ff58bd79e5837ea44d791a2d@192.168.1.2:61910" console
INFO [05-29|18:42:15] Starting peer-to-peer node               instance=Geth/v1.6.1-stable-021c3c28/linux-amd64/go1.8.1
INFO [05-29|18:42:15] Allocated cache and file handles         database=/home/zl/Documents/private-geth/data/01/geth/chaindata cache=128 handles=1024
INFO [05-29|18:42:15] Initialised chain configuration          config="{ChainID: 15 Homestead: 0 DAO:  DAOSupport: false EIP150:  EIP155: 0 EIP158: 0 Engine: unknown}"
INFO [05-29|18:42:15] Disk storage enabled for ethash caches   dir=/home/zl/Documents/private-geth/data/01/geth/ethash count=3
INFO [05-29|18:42:15] Disk storage enabled for ethash DAGs     dir=/home/zl/.ethash                                    count=2
INFO [05-29|18:42:15] Initialising Ethereum protocol           versions="[63 62]" network=314590
INFO [05-29|18:42:15] Loaded most recent local header          number=36 hash=e1541c…418ce3 td=8938686
INFO [05-29|18:42:15] Loaded most recent local full block      number=36 hash=e1541c…418ce3 td=8938686
INFO [05-29|18:42:15] Loaded most recent local fast block      number=36 hash=e1541c…418ce3 td=8938686
WARN [05-29|18:42:15] Blockchain not empty, fast sync disabled 
INFO [05-29|18:42:15] Starting P2P networking 
INFO [05-29|18:42:17] Mapped network port                      proto=udp extport=61911 intport=61911 interface="UPNP IGDv1-IP1"
INFO [05-29|18:42:17] RLPx listener up                         self=enode://2261c433ed5d12924f727b61bf4084f22f4199b430115827c8eae3bb210c0dd5b3dd7df8dc13d8ca80c80f4a36e25c7bc7737737001d0b09324ee43ca6b9d7f8@192.168.1.2:61911
INFO [05-29|18:42:17] Mapped network port                      proto=tcp extport=61911 intport=61911 interface="UPNP IGDv1-IP1"
Welcome to the Geth JavaScript console!

instance: Geth/v1.6.1-stable-021c3c28/linux-amd64/go1.8.1
coinbase: 0x0a8c35653d8b229c16f0c9ce6f63cffb877cfdcf
at block: 36 (Mon, 29 May 2017 18:30:22 CST)
 datadir: /home/zl/Documents/private-geth/data/01
 modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

在率先个节点命令行中,实践下边包车型的士操作:

> personal.unlockAccount(eth.accounts[0])
Unlock account 0x0416f04c403099184689990674f5b4259dc46bd8
Passphrase: 
true
> eth.sendTransaction({from:"0x0416f04c403099184689990674f5b4259dc46bd8",to:"0x0c7df03a68bd85ef3abcfc63a6b788a2faf8ef27",value:web3.toWei(1, "ether")})
INFO [03-01|15:03:04] Submitted transaction                    fullhash=0xb92d256b66c336c9ffa5c1872bb5b20b6b704d7fbc4dd7aa3cdb97b8668d407d recipient=0x0C7dF03a68Bd85EF3abcfC63A6b788a2fAf8EF27
"0xb92d256b66c336c9ffa5c1872bb5b20b6b704d7fbc4dd7aa3cdb97b8668d407d"
> eth.pendingTransactions
[{
    blockHash: null,
    blockNumber: null,
    from: "0x0416f04c403099184689990674f5b4259dc46bd8",
    gas: 90000,
    gasPrice: 18000000000,
    hash: "0xb92d256b66c336c9ffa5c1872bb5b20b6b704d7fbc4dd7aa3cdb97b8668d407d",
    input: "0x",
    nonce: 5,
    r: "0xfd4d1b29e0ae3bce53599d4e0f89a093b1b483de2cf85f6acdb66c88dd594242",
    s: "0x60b4a92b9a2744b3c2f9cd5c5cd0d406c93b4b18877d4cb6357ec7946019bfe2",
    to: "0x0c7df03a68bd85ef3abcfc63a6b788a2faf8ef27",
    transactionIndex: 0,
    v: "0x37",
    value: 1000000000000000000
}]

eth.sendTransaction正是试行发送以太币的操作, 参数from,
to分别是出殡和埋葬账户和吸收账户, web三.toWei(一,
“ether”)是将1单位”ether”转变为对应的”Wei”数量.

这年我们可以看看除了第2个账户0xbcf5b84130三bc080贰6ce二d三b八f834玖捌ffe4二c1贰f之外,还新添了另一个账户
0xb8b1二a80一b陆十176935a153二一f77b4八dd伍c0c44八, 此时输入eth.accounts,
就足以很轻巧的看出有五个账户的公钥地址。

地点的一声令下中,–bootndoes
是安装当前节点运营后,直接通过设置–bootndoes 的值来链接第玖个节点,
–bootnoedes
的值能够通过在率先个节的指令行中,输入:admin.nodeInfo.enode命令打字与印刷出来.

接下来试行挖矿

>  miner.start(1);admin.sleepBlocks(1);miner.stop();
INFO [03-01|15:05:47] Updated mining threads                   threads=1
INFO [03-01|15:05:47] Transaction pool price threshold updated price=18000000000
INFO [03-01|15:05:47] Starting mining operation 
INFO [03-01|15:05:47] Commit new mining work                   number=74 txs=1 uncles=0 elapsed=388.33µs
INFO [03-01|15:06:23] Successfully sealed new block            number=74 hash=bcc917…7ddc87
INFO [03-01|15:06:23]  mined potential block                  number=74 hash=bcc917…7ddc87
INFO [03-01|15:06:23] Commit new mining work                   number=75 txs=0 uncles=0 elapsed=202.123µs
true

何以赢得账户的以太币余额
在上一章中大家说过,当以太坊的私链在挖矿时候,所挖到的以太币都会存入第几个以太坊账户中,即eth.accounts[0]
中,而eth.accounts[1]暗中同意是不会有以太币的。那年我们能够用上边的指令来查阅eth.accounts[0]
中的以太币余额。

也足以不设置 –bootnodes, 直接开发银行,运行后进入命令行,
通过命令admin.addPeer(enodeUrlOfFirst
Instance)把它当作一个peer增添进来.

再在其次个节点的命令行输入:

> eth.blockNumber
74
> eth.getBalance(eth.accounts[0])
1000000000000000000

能够观察第1个节点中的账户,已经得有了3个”ether”,
并且能够看出,以太坊中,一”ether”=一千000000000000000″Wei.
后边输入eth.blockNumber,获得的值为7三,以后加多了1个区块形成了7四

eth.getBalance(“0xbcf5b841303bc08026ce2d3b8f83498ffe42c12f”)
eth.getBalance(“0xb8b12a801b610176935a15321f77b48dd5c0c448”)
其间0xbcf五b84130三bc080二陆ce二d三b8f834九八ffe4贰c12f是率先个账户的地址,
0xb8b1二a80一b陆十17693五a153二一f77b4捌dd伍c0c448 是首个账户的地点 结果如下:
p1一
从上我们得以看得很精通,挖矿得来的以太币都进了第三个账户,同时每一种账户的公钥是该账户的基本。通过公钥小编么能够对该账户的以太币举行增加和删除改查各类操作

为了确认链接成功,第三个节点输入:

小结一下:

这一次我们做到了以下内容:
一)创制区块链私有互联网,并在互联网中树立多节点集群
二)在多节点景况中,通过贰个节点的账户向网络中的别的三个节点的账户转了二个以太坊币,交易成功.

怎么在八个账户之间开始展览以太币转变
前方说过每一种账户的公钥(地址)是一切以太坊账户操作的主干,但地址字符串太长,大家用acc0/acc壹分别代表accounts[0]和[1],此外安装要改造0.0一个以太币

> admin.nodeInfo
{
  enode: "enode://2261c433ed5d12924f727b61bf4084f22f4199b430115827c8eae3bb210c0dd5b3dd7df8dc13d8ca80c80f4a36e25c7bc7737737001d0b09324ee43ca6b9d7f8@192.168.1.2:61911",
  id: "2261c433ed5d12924f727b61bf4084f22f4199b430115827c8eae3bb210c0dd5b3dd7df8dc13d8ca80c80f4a36e25c7bc7737737001d0b09324ee43ca6b9d7f8",
  ip: "192.168.1.2",
  listenAddr: "[::]:61911",
  name: "Geth/v1.6.1-stable-021c3c28/linux-amd64/go1.8.1",
  ports: {
    discovery: 61911,
    listener: 61911
  },
  protocols: {
    eth: {
      difficulty: 8938686,
      genesis: "0xa0e580c6769ac3dd80894b2a256164a76b796839d2eb7f799ef6b9850ea5e82e",
      head: "0xe1541cc54dbcade54fb61053ffa71391c44bb6655cf9619635263960bc418ce3",
      network: 314590
    }
  }
}
> 

> acc0 = eth.accounts[0]
“0xbcf5b841303bc08026ce2d3b8f83498ffe42c12f”
> acc1 = eth.accounts[1]
“0xb8b12a801b610176935a15321f77b48dd5c0c448”
> amount = web3.toWei(0.01)
“10000000000000000”
这一年大家得以行使eth.sendTransaction来将0.0一个以太币从acc0转移到acc第11中学。

首先个节点输入:

> eth.sendTransaction({from: acc0, to: acc1, value: amount})
结果如下:

> net.peerCount
1
> admin.peers
[{
    caps: ["eth/62", "eth/63"],
    id: "2261c433ed5d12924f727b61bf4084f22f4199b430115827c8eae3bb210c0dd5b3dd7df8dc13d8ca80c80f4a36e25c7bc7737737001d0b09324ee43ca6b9d7f8",
    name: "Geth/v1.6.1-stable-021c3c28/linux-amd64/go1.8.1",
    network: {
      localAddress: "192.168.0.103:61910",
      remoteAddress: "192.168.1.2:41912"
    },
    protocols: {
      eth: {
        difficulty: 8938686,
        head: "0xe1541cc54dbcade54fb61053ffa71391c44bb6655cf9619635263960bc418ce3",
        version: 63
      }
    }
}]
> 

那么些是以太坊的1个维护机制,每隔1段时间账户就会自行锁定,那一年任何以太币在账户之间的转移都会被驳回,除非把该账户解锁.

从获得的结果能够看出,第二个节点有贰个peer链接,
链接的node id为:

以此时候大家就需求实行 personal.unlockAccount(acc0)
并输入密码来解锁acc0才可。

“2261c433ed5d12924f727b61bf4084f22f4199b430115827c8eae3bb210c0dd5b3dd7df8dc13d8ca80c80f4a36e25c7bc7737737001d0b09324ee43ca6b9d7f8”

> personal.unlockAccount(acc0)
Unlock account 0xbcf5b841303bc08026ce2d3b8f83498ffe42c12f
Passphrase: 
true
>
以此时候我们再次推行命令eth.sendTransaction({from: acc0, to: acc壹,
value: amount}), 结果如下:

以此id,正好正是第一个节点的id.

> eth.sendTransaction({from: acc0, to: acc1, value: amount})
“0xeea74dd5ff3f1287614d52ebb674edb93e8c5e51e4296835044d3d858d3d9f10”
> eth.getBalance(acc1)
10000000000000000
>
咱俩得以看来这一年acc一有了数值一千0000000000000,
而不再是事先的0了。但咱们赫赫有名要给0.01ether币的,为什么数值会那样大吗?
其实是对的,大家只要输入指令web三.fromWei(一千0000000000000,”ether”)
就能够见道了。

服从那样的主意继续庞大,能够非凡轻易就足以成立地方节点集群.这一个工作都足以写成脚本代码来完结,
里面还足以包含成立账户,挖矿等..

> web3.fromWei(10000000000000000,”ether”)
“0.01”
为啥吗,这一个就关系到以太坊的主导单位了,咱们下章讲授.
———————————————————————————-

请参考:.
 

Ether币的大旨单位
Ether币最小的单位是Wei,也是命令行暗中认可的单位,
然后每一千个进四个单位,依次是

链接成功后,使用大家在上一篇小说中挖矿的账户,向第二个节点发送
“ether”(以太币的货币单位,还有1种叫”Wei”,基本上那几个货币单位都以用部分牛逼的人的名字来命名的).

kwei (1000 Wei)
mwei (1000 KWei)
gwei (1000 mwei)
szabo (1000 gwei)
finney (1000 szabo)
ether (1000 finney)
归纳地说正是正是壹 以太币 = 一千000000000000000 Wei
(那就是上一站章中为何大家转移0.03个以太币,结果却展现不短的原因)

首先查看第叁个节点的Wei数量和整个互连网的区块号,还有接收货币的账号id:

什么样开始展览ether 和 Wei之间的退换
Ether–> Wei:web3.toWei

> eth.getBalance(eth.accounts[0])
0000000000000000000
> eth.blockNumber
30 
> eth.accounts[0]
"0x0a8c35653d8b229c16f0c9ce6f63cffb877cfdcf"
> 

> web3.toWei(1)
“1000000000000000000”
> web3.toWei(1.3423423)
“1342342300000000000”
> web3.toWei(0.00034)
“340000000000000”
>
Wei –> Ether: web3.fromWei

在第一个节点命令行中,实践上面包车型客车操作:

> web3.fromWei(10000000000000000)
“0.01”
> web3.fromWei(1000000000000000000)
“1”
>
1个以太币各单位之间的转换工具

> personal.unlockAccount(eth.accounts[0], "ko2005")
true
> eth.sendTransaction({from: "0x5fba50fce50baf0b8a7314200ba46336958ac97e", to: "0x0a8c35653d8b229c16f0c9ce6f63cffb877cfdcf", value: web3.toWei(1, "ether")})
INFO [05-29|17:33:42] Submitted transaction                    fullhash=0x51a75422f79fa96e70a0c1481851bc9f827868c44203b68d74f9815ffb367d5f recipient=0x0a8c35653d8b229c16f0c9ce6f63cffb877cfdcf
"0x51a75422f79fa96e70a0c1481851bc9f827868c44203b68d74f9815ffb367d5f"
> eth.pendingTransactions
[{
    blockHash: null,
    blockNumber: null,
    from: "0x5fba50fce50baf0b8a7314200ba46336958ac97e",
    gas: 90000,
    gasPrice: 20000000000,
    hash: "0x51a75422f79fa96e70a0c1481851bc9f827868c44203b68d74f9815ffb367d5f",
    input: "0x",
    nonce: 0,
    r: "0x5632a8ade4a767dbd949ba1042cb33f98dd0722ab999ba18e1454d19d8bd1f6d",
    s: "0x515dcfa3de297f0c956ad9a061a5561f47cc9ccbb0a547cda59193c77fcbe3f7",
    to: "0x0a8c35653d8b229c16f0c9ce6f63cffb877cfdcf",
    transactionIndex: 0,
    v: "0x42",
    value: 1000000000000000000
}] 

使用比非常的粗略,输入各个单位,就足以自动获取各样转变结果,举例输入0.0一ether
足以得到多少Wei, 多少finney等。

eth.sendTransaction正是奉行发送以太币的操作,
参数from, to分别是出殡和埋葬账户和接收账户, web叁.toWei(一,
“ether”)是将一单位”ether”转变为对应的”Wei”数量.

p4

下一场施行挖矿(那里小编也不知情,为何发送货币现在,要透过挖矿才具让交易生效)

千帆竞发挖矿 & 甘休挖矿
> miner.start() //开端挖矿
true
> miner.stop() //停止挖矿
true
>
铺排合约
注意合同安插的时候,以太坊的私有链必须处在挖矿实行的状态,不然合约铺排将不会收效
大家在指令行中,首先unlock(eth.accounts[0]),因为布置合约须要消耗瓦斯,也等于以太币。而在此以前说过由于爱惜体制,不解锁账户,是不会允许别的以太币流出的。
> personal.unlockAccount(acc0)
Unlock account 0xbcf5b841303bc08026ce2d3b8f83498ffe42c12f
Passphrase: 
true
>
下一场大家复制黏贴下边代码到geth 命令行中。
var a_demotypesContract =
web3.eth.contract([{“constant”:false,”inputs”:[{“name”:”a”,”type”:”uint256″}],”name”:”f”,”outputs”:[{“name”:”b”,”type”:”uint256″}],”payable”:false,”type”:”function”}]);
var a_demotypes = a_demotypesContract.new(
   {
     from: web3.eth.accounts[0], 
     data:
‘0x6060604052341561000c57fe5b5b60ab8061001b6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063b3de648b14603a575bfe5b3415604157fe5b60556004808035906020019091905050606b565b6040518082815260200191505060405180910390f35b600060006008830290508091505b509190505600a165627a7a7230582010decdc0b0a43b565814fe904eae2544665457d6353c7d906fc2c43c81c867e40029’, 
     gas: ‘4700000’
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== ‘undefined’) {
         console.log(‘Contract mined! address: ‘ + contract.address + ‘
transactionHash: ‘ + contract.transactionHash);
    }
 })
结果如下图:

> miner.start()
INFO [05-29|18:26:47] Updated mining threads                   threads=0
INFO [05-29|18:26:47] Starting mining operation 
null
> INFO [05-29|18:26:47] Commit new mining work                   number=31 txs=1 uncles=0 elapsed=1.094ms

> INFO [05-29|18:30:14] Successfully sealed new block            number=31 hash=19e3d7…a6ecd5
INFO [05-29|18:30:14] 🔨 mined potential block                  number=31 hash=19e3d7…a6ecd5
INFO [05-29|18:30:14] Commit new mining work                   number=32 txs=0 uncles=0 elapsed=2.314ms
INFO [05-29|18:30:17] Successfully sealed new block            number=32 hash=94748a…cdbc17
INFO [05-29|18:30:17] 🔨 mined potential block                  number=32 hash=94748a…cdbc17
INFO [05-29|18:30:17] Commit new mining work                   number=33 txs=0 uncles=0 elapsed=156.295µs
INFO [05-29|18:30:19] Successfully sealed new block            number=33 hash=b8e037…cd50ff
INFO [05-29|18:30:19] 🔨 mined potential block                  number=33 hash=b8e037…cd50ff
INFO [05-29|18:30:19] Commit new mining work                   number=34 txs=0 uncles=0 elapsed=131.676µs
> mINFO [05-29|18:30:20] Successfully sealed new block            number=34 hash=7ad61a…f63067
INFO [05-29|18:30:20] 🔨 mined potential block                  number=34 hash=7ad61a…f63067
INFO [05-29|18:30:20] Commit new mining work                   number=35 txs=0 uncles=0 elapsed=138.957µs
> miner.stINFO [05-29|18:30:22] Successfully sealed new block            number=35 hash=eb9652…a1a9e3
INFO [05-29|18:30:22] 🔨 mined potential block                  number=35 hash=eb9652…a1a9e3
INFO [05-29|18:30:22] Commit new mining work                   number=36 txs=0 uncles=0 elapsed=334.318µs
> miner.stopINFO [05-29|18:30:22] Successfully sealed new block            number=36 hash=e1541c…418ce3
INFO [05-29|18:30:22] 🔗 block reached canonical chain          number=31 hash=19e3d7…a6ecd5
INFO [05-29|18:30:22] 🔨 mined potential block                  number=36 hash=e1541c…418ce3
INFO [05-29|18:30:22] Commit new mining work                   number=37 txs=0 uncles=0 elapsed=117.185µs
> miner.stop()
true 
> 

p5

从下边包车型客车日志能够见到,实践挖矿之后,一共有四个区块发生.

伺机片刻,会发觉合约被安排到挖矿挖出来的区块中了, 按下回车代表成功

再在其次个节点的命令行输入:

p6

> eth.blockNumber
36
> eth.getBalance(eth.accounts[0])
1000000000000000000

那会儿输入合同安插的实例a_demotypes, 能够见见a_demotypes的详情。

能够见到第3个节点中的账户,已经得有了一个”ether”,
并且能够看到,以太坊中,一”ether”=一千000000000000000″Wei.

> a_demotypes
{
  abi: [{
      constant: false,
      inputs: [{…}],
      name: “f”,
      outputs: [{…}],
      payable: false,
      type: “function”
  }],
  address: “0x54ed7a5f5a63ddada3bfe83b3e632adabaa5fc2f”,
  transactionHash:
“0x69cde62bcd6458e14f40497f4840f422911d63f5dea2b3a9833e6810db64a1c9”,
  allEvents: function(),
  f: function()
}
>
也足以调用a_demotypes的方法f,
输入任何数字,会回到八*n,如输入100,返回800,输入125,返回1000

事先输入eth.blockNumber,获得的值为30,其实只要挖出第一个区块的时候,就能够告壹段落,发送到第叁个node账户中的三个”ether”,就早已生效.
 

> a_demotypes.f.call(100)
800
> a_demotypes.f.call(125)
1000
有鉴于此该合同在大家的私有链上布署成功!

计算一下:

此次以大家达成了以下内容:

一)创立区块链私有网络,并在互连网中,建立和睦的节点集群.

2)在接点集群中,通过二个节点的账户向互连网中的别的三个节点的账户转了一个以太坊币,交易成功.

下一次,我们将起始使用truffle,写贰个以太坊的”Hello
World”,正式进入区块链的开拓.

其余说一下,

自己也是区块链新手,1边读书,壹边把读书的经过记录下来向大家大饱眼福,中间走了卓殊多的弯路,

费用了大批量日子,
小说首假如把本身的就学经验分享出去,一个是投机能够巩固,其余能够扶持我们,幸免我们和小编同样走多数弯路.

万一小说中有写得语无伦次的,恐怕有越来越好的办法,还请各位大神研讨指正.

相关文章

Leave a Reply

电子邮件地址不会被公开。 必填项已用*标注