Browse Source

Added tests and contracts for Petitions

Rylan Gooch 2 years ago
parent
commit
920346a8e7

+ 0 - 5
ethereum-private/.gitignore

@@ -1,5 +0,0 @@
1
-geth/
2
-keystore/
3
-build/
4
-.idea/
5
-.DS_Store

+ 0 - 220
ethereum-private/README.md

@@ -1,220 +0,0 @@
1
-# Ethereum -
2
-
3
-## Setup private network
4
-
5
-Start a private cluster of nodes on your local machine and with other nodes running on the same LAN.
6
-
7
-### Setup
8
-
9
-TODO (Brew install ethereum and geth)
10
-
11
-### Start the node(s)
12
-
13
-The script allows you to start up to *9* nodes on your local machine (`node0`, `node1`, `node3` etc).
14
-To start a node run the script passing start and the node name as arguments:
15
-
16
-```
17
-./go.sh start node0
18
-```
19
-
20
-### Attach javascript console
21
-
22
-In a new terminal window you can attach a javascript console to interact with the node:
23
-
24
-```
25
-./go.sh console node0
26
-```
27
-
28
-In the console you can:
29
-
30
-##### List accounts
31
-
32
-```
33
-> eth.accounts
34
-```
35
-
36
-##### Create new account
37
-
38
-```
39
-> personal.newAccount()
40
-```
41
-(It will ask for a passphrase)
42
-You can see the new key pair being created in the folder `keystore`.
43
-
44
-##### Mine to get ether
45
-
46
-Let's get rich!
47
-
48
-```
49
-> miner.start(1)
50
-...... (mine some blocks....)
51
-> miner.stop()
52
-```
53
-
54
-##### Check balance
55
-
56
-You should be richer
57
-
58
-```
59
-> eth.getBalance(eth.accounts[0])
60
-
61
-Or convert to ether
62
-> web3.fromWei(eth.getBalance(eth.accounts[0]), "ether");
63
-```
64
-
65
-##### Connect to other peers
66
-
67
-If you have nodes running in the same Ethereum private network (same network id and same genesis file) you can connect them.
68
-Let's say you want to connect A <-> B.
69
-Find the `enode` of the node B:
70
-
71
-```
72
-> admin.nodeInfo.enode
73
-
74
-"enode://fbd893902e0c6be1614f2d8b764d04d7dac61a2fb5fb54a0473e92360e250cd47dd5ca3063316c30ffdd3610a4d0257b33bf10affdfdc615808c5ae47dc8b2c7@[::]:30303"
75
-```
76
-If the nodes are on different machines in the same LAN, find the private IP address of node B:
77
-```
78
-ifconfig | grep inet
79
-```
80
-
81
-If the nodes are on the same machine, find the localhost IP address (probably 127.0.0.1) of node B:
82
-```
83
-ifconfig | grep netmask | awk '{print $2}'
84
-```
85
-
86
-In node A then add the peer:
87
-```
88
-> admin.addPeer("enode://fbd893902e0c6be1614f2d8b764d04d7dac61a2fb5fb54a0473e92360e250cd47dd5ca3063316c30ffdd3610a4d0257b33bf10affdfdc615808c5ae47dc8b2c7@[PRIVATE IP ADDRESS or LOCALHOST IP ADDRESS if on the same machine]:30303")
89
-```
90
-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.
91
-If the syncing between nodes has ended, A and B should have the same ending block (check the hash).
92
-
93
-```
94
-> admin.getBlock("latest")
95
-```
96
-
97
-##### Send ether to another account
98
-
99
-```
100
-> personal.unlockAccount(eth.accounts[0])
101
-> eth.sendTransaction({ from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(5, "ether") })
102
-```
103
-
104
-### Deploy contract
105
-
106
-You can find example contracts `greeter.sol` and `voting.sol` and a deployment script `deploy.js` in the folder `contracts/`.
107
-The first step to deploy is to compile the contract. To do so install the `solcjs` dependency:
108
-
109
-```
110
-npm i -g solc
111
-```
112
-
113
-Then use it to compile the contract. In the following command, you can replace `contracts/greeter.sol` with the path to the contract of your choice:
114
-```
115
-solcjs --bin --abi contracts/greeter.sol -o build
116
-```
117
-
118
-This will create a folder `build` with the compiled contract in `.abi` and `.bin` formats. The .abi file specifies the methods available (interface) in your contract, the .bin is the bytecode that actually gets deployed onto the blockchain.
119
-
120
-In your Geth console load the script to generate function that will deploy the contract (you might need to unlock your account).
121
-
122
-```
123
-> personal.unlockAccount(eth.accounts[0])
124
-> loadScript("contracts/deploy.js")
125
-```
126
-
127
-The generated function `deployContract` requires abi, bin and data to be passed as arguments. Assign values to these arguments.
128
-
129
-```
130
-> var bin = "<paste the contents of the .bin file in the build folder>"
131
-> var abi = <paste the contents of the .abi file in the build folder>
132
-> var data = <if your contract requires data, paste it here. for greeter example, that will be "hello world">
133
-```
134
-
135
-Execute the deployment function.
136
-
137
-```
138
-> deployContract(abi, bin, data)
139
-```
140
-
141
-You will need miners to accept the contract in the blockchain. Once the contract has been mined you should see in your console:
142
-
143
-```
144
-Contract mined! address: 0x3c96ce5b6760fc61c22f922514f98bed61172f14 transactionHash: 0x6a75e690fbf6381b28b5873db39a7f4714eb55b759702b23f1a02fa8a9ec5218
145
-```
146
-
147
-Note down the contract address as it will be handy to find it again in the future.
148
-Now in the console you can interact with the functions available in the contract:
149
-
150
-```
151
-> greeter.greet()
152
-```
153
-
154
-That was an example of a 'call', the execution of code without permanent changes. A call is free, is immediate and simply reads and returns data.
155
-Other than calls you can also do a 'transaction'. This is not free (you need to specify the account paying for it), is not processed immediately (requires mining) and changes the state of the network:
156
-
157
-```
158
-> personal.unlockAccount(eth.accounts[0])
159
-> greeter.setGreeting("Hello Camilla!", {from:eth.accounts[0]})
160
-```
161
-
162
-If you look in the Ethereum storage at the address of the greeting object, you will see the new value stored:
163
-```
164
-> var address = greeter.address
165
-> var data = eth.getStorageAt(add, 1)
166
-> web3.toAscii(data)
167
-
168
-"Hello Camilla!\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1a"
169
-```
170
-
171
-##### Finding the contract object
172
-
173
-If you have lost the contract object by exiting the console, or if you are trying to find the contract from a different node, you can do so doing:
174
-
175
-```
176
-var greeter = web3.eth.contract(add the contract ABI).at(add the contract hash address as a string)
177
-```
178
-
179
-## Eth-stats
180
-
181
-There is a cool monitoring that can be set up for the local nodes network.
182
-
183
-### Setup front-end
184
-
185
-```
186
-git clone https://github.com/cubedro/eth-netstats
187
-cd eth-netstats
188
-npm i
189
-WS_SECRET=<chosen_secret> npm start
190
-```
191
-Visit http://localhost:3000 (will stay empty until the back end is ready).
192
-
193
-### Create start config for back-end
194
-
195
-```
196
-git clone https://github.com/ethersphere/eth-utils.git
197
-cd eth-utils
198
-bash netstatconf.sh <number_of_nodes_in_cluster> decode http://localhost:3000 <chosen_ws_secret> > app.json
199
-```
200
-
201
-### Setup back-end
202
-
203
-```
204
-git clone https://github.com/cubedro/eth-net-intelligence-api
205
-cd eth-net-intelligence-api
206
-npm i
207
-npm i -g pm2
208
-```
209
-
210
-Move the `app.json` file previously created into the `eth-net-intelligence-api/` directory.
211
-
212
-Then start the back end!
213
-```
214
-pm2 start app.json
215
-```
216
-
217
-To kill the back end:
218
-```
219
-pm2 delete app.json
220
-```

+ 23 - 0
ethereum-private/contracts/Petition.sol

@@ -0,0 +1,23 @@
1
+pragma solidity ^0.4.11;
2
+
3
+contract Petition {
4
+    bytes32 UUID;
5
+    bytes32[] signatureIDs;
6
+
7
+    function Petition(bytes32 _UUID) {
8
+        UUID = _UUID;
9
+    }
10
+
11
+    function sign(bytes32 _signatureID) {
12
+        signatureIDs.push(_signatureID);
13
+    }
14
+
15
+    function getSignatureCount() returns(uint256) {
16
+        return signatureIDs.length;
17
+    }
18
+
19
+    function getUUID() returns (bytes32) {
20
+        return UUID;
21
+    }
22
+}
23
+

+ 5 - 0
ethereum-private/migrations/1_initial_migration.js

@@ -0,0 +1,5 @@
1
+var Migrations = artifacts.require("./Migrations.sol");
2
+
3
+module.exports = function(deployer) {
4
+  deployer.deploy(Migrations);
5
+};

+ 8 - 0
ethereum-private/migrations/2_deploy_contracts.js

@@ -0,0 +1,8 @@
1
+var ConvertLib = artifacts.require("./ConvertLib.sol");
2
+var MetaCoin = artifacts.require("./MetaCoin.sol");
3
+
4
+module.exports = function(deployer) {
5
+  deployer.deploy(ConvertLib);
6
+  deployer.link(ConvertLib, MetaCoin);
7
+  deployer.deploy(MetaCoin);
8
+};

+ 86 - 0
ethereum-private/test/TestPetition.sol

@@ -0,0 +1,86 @@
1
+pragma solidity ^0.4.2;
2
+
3
+import "truffle/Assert.sol";
4
+import "truffle/DeployedAddresses.sol";
5
+import "../contracts/Petition.sol";
6
+
7
+contract TestPetition {
8
+
9
+//    function beforeEach() {
10
+//        bytes32 testUUID = "123";
11
+//        Petition petition = new Petition(testUUID);
12
+//    }
13
+
14
+    function testInitialSignatureCountIsZero() {
15
+        Petition testPetition = Petition(DeployedAddresses.Petition());
16
+
17
+        uint expected = 0;
18
+
19
+        Assert.equal(testPetition.getSignatureCount(), expected, "Petition should have zero signatures initially");
20
+    }
21
+
22
+    function testInitialSignatureCountWithNewPetition() {
23
+        bytes32 testUUID = "123";
24
+        Petition petition = new Petition(testUUID);
25
+
26
+        uint expected = 0;
27
+
28
+        Assert.equal(petition.getSignatureCount(), expected, "Petition should have zero signatures initially");
29
+    }
30
+
31
+    function testPetitionHasUUID() {
32
+        bytes32 testUUID = "123";
33
+        Petition petition = new Petition(testUUID);
34
+
35
+        Assert.equal(petition.getUUID(), testUUID, "Petition should have a UUID given to it upon instantiation");
36
+    }
37
+
38
+    function testThatSignMethodIncreasesSignatureCount() {
39
+        bytes32 testUUID = "123";
40
+        bytes32 signatureID = "990";
41
+        Petition petition = new Petition(testUUID);
42
+
43
+        petition.sign(signatureID);
44
+        uint expected = 1;
45
+
46
+        Assert.equal(petition.getSignatureCount(), expected, "Sign method should increase signature count by zero");
47
+    }
48
+
49
+//    function testThatMultipleSignaturesIncreasesSignatureCount() {
50
+//        bytes32 testUUID = "123";
51
+//        bytes32 testSignatureID1 = "990";
52
+//        bytes32 testSignatureID2 = "991";
53
+//
54
+//        Petition petition = new Petition(testUUID);
55
+//        uint expected = 2;
56
+//
57
+//        petition.sign(testSignatureID1);
58
+//        petition.sign(testSignatureID2);
59
+//
60
+//        Assert.equal(petition.getSignatureCount(), expected, "Sign method should increase signature count by zero");
61
+//    }
62
+
63
+//    function testThatSignMethodStoresSignatureIDInPetition() {
64
+//        bytes32 testUUID = "123";
65
+//        bytes32 signatureID = "990";
66
+//
67
+//        Petition petition = new Petition(testUUID);
68
+//
69
+//        petition.sign(signatureID);
70
+//
71
+//        Assert.equal(petition.signatureIDs(), signatureID, "Sign method should store the given signature ID in the petition");
72
+//    }
73
+
74
+//    function testThatSignMethodStoresMultipleSignatureIDInPetition() {
75
+//        bytes32 testUUID = "123";
76
+//        bytes32 testSignatureID1 = "990";
77
+//        bytes32 testSignatureID2 = "991";
78
+//
79
+//        Petition petition = new Petition(testUUID);
80
+//
81
+//        petition.sign(testSignatureID1);
82
+//        petition.sign(testSignatureID2);
83
+//
84
+//        Assert.equal(petition.signatureID(), signatureID, "Sign method should store the given signature ID in the petition");
85
+//    }
86
+}

+ 9 - 0
ethereum-private/truffle.js

@@ -0,0 +1,9 @@
1
+module.exports = {
2
+  networks: {
3
+    development: {
4
+      host: "localhost",
5
+      port: 8100,
6
+      network_id: "791" // Match any network id
7
+    }
8
+  }
9
+};