其次节中打听了利用geth搭建以太坊私有网络,那一回则要在个人互联网中树立多个node组成的集群,并互相发掘,产生交易.

上一节中我们运用geth搭建了以太坊私有网络,那三次则要在个体互连网中确立多节点集群,并相互开掘,发生交易.

区块链爱好者(QQ:53016353)  

为了在本地网络运转五个以太坊节点的实例,必须保障一下几点:
1.
各样实例都有单独的数量目录(–datadir)
2.
每一种实例运转都有单独的端口.(eth和rpc两个皆以)(–port 和
–rpcprot)
3.
在集群的事态下, 实例之间都无法不要通晓相互.
4.
独一的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

再张开三个巅峰,伊始化第1个节点:

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的值来链接第一个节点,--bootnoedes
的值能够通过在率先个节的指令行中,输入admin.nodeInfo.enode一声令下打字与印刷出来,要求小心的是必要把@[::]交换实际Computer的IP地址
也得以不安装 --bootnodes, 直接开发银行,运维后跻身命令行,
通过命令admin.addPeer(enodeUrlOfFirst Instance)把它看做二个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字节的地方)的对象和在三个账户里面调换价值和音讯的状态调换构成的。以太坊的账户满含四个部分:
轻松数,用于鲜明每笔交易只可以被管理一遍的计数器
账户近期的以太币余额
账户的合约代码,假设有的话
账户的累积(默感觉空)
简单的讲地说,每贰个以太坊账户都有一对公钥和私钥组成。

再展开多个终端,初步化第三个节点:

率先个节点输入:

> 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
      }
    }
}]

从获得的结果可以看出,第一个节点有1个peer链接, 链接的node id为:
“61eac7f7900bd17cac39ce506cd20c1f67a2a43fb5f5c7a46ace337922a9e325a63a7aa9cb18df12715550ffa0b66c44272132b93245d15825c2ac2c1ddb8cd8”
其一id,正好就是第叁个节点的id.

公钥我们得以知晓为正是账户地址,任何别的账户都能够访谈该地方
私钥能够通晓为一段加密过的密码,这一对公钥和私钥共同整合多个独一标示的以太坊账户。
比方说在上节大家树立的首先个以太坊账户 eth.accounts[0] 中,地址
0xbcf5b841303bc08026ce2d3b8f83498ffe42c12f
正是公钥,而对密码加密而成的,就是私钥。

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

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

什么新添账户
我们能够输入指令 personal.newAccount(“123”)
来新建二个账户,(注意123方可修改为其余其余密码)

先河第三个节点:

首先查看第一个节点的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分别是发送账户和吸取账户, web3.toWei(1,
“ether”)是将1单位”ether”调换为对应的”Wei”数量.

其不时候大家得以看来除了第二个账户0xbcf5b841303bc08026ce2d3b8f83498ffe42c12f之外,还新扩大了另多个账户
0xb8b12a801b610176935a15321f77b48dd5c0c448, 此时输入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

能够看来第2个节点中的账户,已经得有了1个”ether”,
何况能够观察,以太坊中,1″ether”=1000000000000000000″Wei.
事先输入eth.blockNumber,获得的值为73,未来追加了一个区块变成了74

eth.getBalance(“0xbcf5b841303bc08026ce2d3b8f83498ffe42c12f”)
eth.getBalance(“0xb8b12a801b610176935a15321f77b48dd5c0c448”)
个中0xbcf5b841303bc08026ce2d3b8f83498ffe42c12f是首先个账户的地点,
0xb8b12a801b610176935a15321f77b48dd5c0c448 是第叁个账户的位置 结果如下:
p11
从上大家得以看得很精通,挖矿得来的以太币都进了第三个账户,同一时候种种账户的公钥是该账户的为主。通过公钥小编么能够对该账户的以太币进行增加和删除改查各个操作

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

总括一下:

本次我们成功了以下内容:
1)创制区块链私有互连网,并在互连网中确立多节点集群
2)在多节点景况中,通过多个节点的账户向网络中的别的贰个节点的账户转了1个以太坊币,交易成功.

何以在七个账户里面举办以太币调换
如今说过种种账户的公钥(地址)是全方位以太坊账户操作的大旨,但地点字符串太长,我们用acc0/acc1
分别表示accounts[0]和[1],别的安装要改变0.01个以太币

> 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.01个以太币从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个peer链接,
链接的node id为:

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

“2261c433ed5d12924f727b61bf4084f22f4199b430115827c8eae3bb210c0dd5b3dd7df8dc13d8ca80c80f4a36e25c7bc7737737001d0b09324ee43ca6b9d7f8”

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

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

> eth.sendTransaction({from: acc0, to: acc1, value: amount})
“0xeea74dd5ff3f1287614d52ebb674edb93e8c5e51e4296835044d3d858d3d9f10”
> eth.getBalance(acc1)
10000000000000000
>
咱俩得以看看今年acc1有了数值一千0000000000000,
而不再是事先的0了。但我们一览了解要给0.01ether币的,为什么数值会这么大呢?
其实是对的,大家借使输入指令web3.fromWei(10000000000000000,”ether”)
就足以精晓了。

依照那样的不二等秘书技继续增加,能够特别轻巧就足以创立地点节点集群.那些职业都能够写成脚本代码来变成,
里面还能涵盖创设账户,挖矿等..

> web3.fromWei(10000000000000000,”ether”)
“0.01”
为啥吗,这一个就涉嫌到以太坊的主导单位了,大家下章解说.
———————————————————————————-

请参考:.
 

Ether币的骨干单位
Ether币最小的单位是Wei,也是命令行暗许的单位,
然后每1000个进贰个单位,依次是

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

kwei (1000 Wei)
mwei (1000 KWei)
gwei (1000 mwei)
szabo (1000 gwei)
finney (1000 szabo)
ether (1000 finney)
简言之地说正是就是1 以太币 = 一千000000000000000 Wei
(那正是上一站章中为啥大家转移0.01个以太币,结果却显得不长的来由)

先是查看第一个节点的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”
>
叁个以太币各单位之间的转变工具

> 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.01ether
得以收获多少Wei, 多少finney等。

eth.sendTransaction正是进行发送以太币的操作,
参数from, to分别是殡葬账户和接收账户, web3.toWei(1,
“ether”)是将1单位”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

从上边的日志能够见到,施行挖矿之后,一共有6个区块产生.

等待片刻,会意识合约被布置到挖矿挖出来的区块中了, 按下回车代表成功

再在第二个节点的命令行输入:

p6

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

那时输入合同计划的实例a_demotypes, 能够看到a_demotypes的详情。

能够见到第三个节点中的账户,已经得有了1个”ether”,
何况能够看来,以太坊中,1″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,
输入任何数字,会回来8*n,如输入100,返回800,输入125,返回1000

事先输入eth.blockNumber,得到的值为30,其实若是挖出第三个区块的时候,就足以告一段落,发送到第三个node账户中的叁个”ether”,就早就生效.
 

> a_demotypes.f.call(100)
800
> a_demotypes.f.call(125)
1000
不问可见该合约在我们的私有链上铺排成功!

小结一下:

这一次以大家做到了以下内容:

1)创制区块链私有互连网,并在互联网中,组建和谐的节点集群.

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

下壹回,大家将起来使用truffle,写二个以太坊的”Hello
World”,正式步向区块链的开采.

另外说一下,

自己也是区块链新手,一边读书,一边把读书的进度记录下来向我们大饱眼福,中间走了非常的多的弯路,

开销了多量光阴,
小说首假使把温馨的求学经历分享出来,一个是协调能够加强,别的能够扶助大家,制止大家和自己同一走比比较多弯路.

若果小说中有写得格外的,也许有越来越好的点子,还请各位大神研讨指正.

相关文章