Browse Source

You can run multiple nodes on your local machine

cserri-tw 2 years ago
parent
commit
eaa8786fbc
3 changed files with 46 additions and 25 deletions
  1. 21 15
      ethereum-private/README.md
  2. 24 9
      ethereum-private/go.sh
  3. 1 1
      ethereum-private/genesis791.json

+ 21 - 15
ethereum-private/README.md

@@ -1,31 +1,33 @@
1 1
 # Ethereum - Our private network
2 2
 
3
+Start a private cluster of nodes on your local machine and with other nodes running on the same LAN.
4
+
3 5
 ## Setup
4 6
 
5
-Brew install ethereum and geth
7
+TODO (Brew install ethereum and geth)
6 8
 
7 9
 ## Init
8 10
 
9
-Initialise the private network (you only need to do it once).
11
+Initialise the private network (you only need to do it once for each node).
10 12
 
11 13
 ```
12
-./go.sh init
14
+./go.sh init node1
13 15
 ```
14 16
 
15 17
 ## Start the network
16 18
 
17
-Every time you want to start the network:
19
+Every time you want to start the node's network:
18 20
 
19 21
 ```
20
-./go.sh start
22
+./go.sh start node1
21 23
 ```
22 24
 
23
-## Attach console
25
+## Attach javascript console
24 26
 
25
-In a new terminal window:
27
+In a new terminal window you can attach a javascript console to interact with the node:
26 28
 
27 29
 ```
28
-./go.sh attach
30
+./go.sh console node1
29 31
 ```
30 32
 
31 33
 In the console you can:
@@ -64,8 +66,7 @@ You should be richer
64 66
 
65 67
 #### Connect to other peers
66 68
 
67
-If you have nodes running the private network (same network id and same genesis file) on different machines in the same LAN, you can connect them.
68
-As soon as a node connects to a peer the Ethereum protocol to understand who has the 'best' version of the blockchain kicks in. If the two nodes have different forks (ending chain of blocks), only one will be accepted.
69
+If you have nodes running in the same Ethereum private network (same network id and same genesis file) you can connect them.
69 70
 Let's say you want to connect A <-> B.
70 71
 Find the `enode` of the node B:
71 72
 
@@ -74,17 +75,22 @@ Find the `enode` of the node B:
74 75
 
75 76
 "enode://fbd893902e0c6be1614f2d8b764d04d7dac61a2fb5fb54a0473e92360e250cd47dd5ca3063316c30ffdd3610a4d0257b33bf10affdfdc615808c5ae47dc8b2c7@[::]:30303"
76 77
 ```
77
-And the private IP address of B:
78
+If the nodes are on different machines in the same LAN, find the private IP address of node B:
78 79
 ```
79 80
 ifconfig | grep inet
80 81
 ```
81 82
 
82
-In A then add the peer:
83
+If the nodes are on the same machine, find the localhost IP address (probably 127.0.0.1) of node B:
83 84
 ```
84
-> admin.addPeer("enode://fbd893902e0c6be1614f2d8b764d04d7dac61a2fb5fb54a0473e92360e250cd47dd5ca3063316c30ffdd3610a4d0257b33bf10affdfdc615808c5ae47dc8b2c7@[PRIVATE IP ADDRESS]:30303")
85
+ifconfig | grep netmask | awk '{print $2}'
85 86
 ```
86 87
 
87
-If the two nodes are connected, and the syncing has ended, A and B should have the same ending block (check the hash).
88
+In node A then add the peer:
89
+```
90
+> admin.addPeer("enode://fbd893902e0c6be1614f2d8b764d04d7dac61a2fb5fb54a0473e92360e250cd47dd5ca3063316c30ffdd3610a4d0257b33bf10affdfdc615808c5ae47dc8b2c7@[PRIVATE IP ADDRESS or LOCALHOST IP ADDRESS if on the same machine]:30303")
91
+```
92
+As soon as a node connects to a peer the Ethereum protocol kicks in, trying to understand who has the 'best' version of the blockchain. If the two nodes have different forks (ending chain of blocks), only one will be accepted.
93
+If the syncing between nodes has ended, A and B should have the same ending block (check the hash).
88 94
 
89 95
 ```
90 96
 > admin.getBlock("latest")
@@ -126,7 +132,7 @@ The generated function `deployContract` requires abi, bin and data to be passed
126 132
 > var bin = "<paste the contents of the .bin file in the build folder>"
127 133
 > var abi = <paste the contents of the .abi file in the build folder>
128 134
 > var data = <if your contract requires data, paste it here. for greeter example, that will be "hello world">
129
-``` 
135
+```
130 136
 
131 137
 Execute the deployment function.
132 138
 

+ 24 - 9
ethereum-private/go.sh

@@ -1,24 +1,39 @@
1 1
 #!/bin/bash
2
+PORT_NODE1="30301"
3
+PORT_NODE2="30302"
4
+RPC_PORT_NODE1="8101"
5
+RPC_PORT_NODE2="8102"
6
+NETWORK_ID="791"
7
+APP_CORS_DOMAIN="http://localhost:8000"
2 8
 
3 9
 init() {
4
-  echo "Setting up ethereum in directory" + $PWD
5
-  geth --datadir $PWD init $PWD/genesis791.json
10
+  echo "Setting up $NODE in directory" + $PWD
11
+  geth --datadir $PWD/nodes/$NODE init $PWD/nodes/genesis791.json
6 12
 }
7 13
 
8 14
 start() {
9
-  geth --datadir $PWD --networkid 791 --nodiscover --verbosity 5 --rpccorsdomain "http://localhost:8000" --rpc
15
+  PORT=$PORT_NODE1
16
+  RPC_PORT=$RPC_PORT_NODE1
17
+  if [ $NODE == "node2" ]; then
18
+    PORT=$PORT_NODE2
19
+    RPC_PORT=$RPC_PORT_NODE2
20
+  fi
21
+  geth --datadir $PWD/nodes/$NODE --port $PORT --rpc --rpcport $RPC_PORT --networkid $NETWORK_ID --nodiscover --verbosity 5 --rpccorsdomain $APP_CORS_DOMAIN --ipcpath $PWD/nodes/$NODE/geth.ipc
10 22
 }
11 23
 
12
-attach() {
13
-  geth attach $PWD/geth.ipc
24
+console() {
25
+  geth attach $PWD/nodes/$NODE/geth.ipc
14 26
 }
15 27
 
16 28
 usage() {
17
-  echo "Please provide an option:"
18
-  echo "init, start, attach"
29
+  echo "Usage:"
30
+  echo "init    <node1, node2> : initialises the node's network"
31
+  echo "start   <node1, node2> : starts the node in the network"
32
+  echo "console <node1, node2> : attaches a js console to the node"
19 33
 }
20 34
 
21 35
 OPTION="$1"
36
+NODE="$2"
22 37
   case $OPTION in
23 38
     init)
24 39
       init
@@ -26,8 +41,8 @@ OPTION="$1"
26 41
     start)
27 42
       start
28 43
       ;;
29
-    attach)
30
-      attach
44
+    console)
45
+      console
31 46
       ;;
32 47
     *)
33 48
       usage

+ 1 - 1
ethereum-private/genesis791.json

@@ -7,7 +7,7 @@
7 7
         "eip155Block": 0,
8 8
         "eip158Block": 0
9 9
     },
10
-    "nonce": "0x0000000000000042",
10
+    "nonce": "0x0000000000000791",
11 11
     "timestamp": "0x00",
12 12
     "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
13 13
     "extraData": "0x00",