Sep.02 KIT students smart contract learning

allonchain
3 min readSep 5, 2018

--

1 basic knowledge about blockchain

in this part, a free online course <Bitcoin and Cryptocurrency Technologies> is suggested.

2 private Ethereum network

since deploying smart contracts on the ethereum main network is costing and even the testnet is not easy to mine, it is necessary to build the private network. For me, AWS t2.micro instance is a good choice to be an intermediate full node without mining. It serves as a transfer node offering 24 hours online recording transactions.

I installed Ubuntu 18.04 system.

Now, start to install geth, which is actually the ledger management software, it connects other geth node through p2p network and offers some rpc api to users to interact with the ledger.

sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum

check installation

geth version

Now you have the geth client, you need prepare the genesis file, which defines the genesis block information of our private network.
File name: genesis_aws.json
content is below,

create a folder “newnode” , and a folder “datadir” inside folder “newnode”,

Init the geth

geth --networkid 19891110 --datadir ./datadir/ init genesis_aws.jsongeth --networkid 19891110 --datadir ./datadir/ --ipcpath /home/dali/.ethereum/geth.ipc --syncmode full --rpc  --rpcaddr ‘127.0.0.1’ --rpccorsdomain “*” --rpcport 8545 --rpcapi db,eth,net,web3,personal console

--datadir: all the data are stored in this folder
--networkid: must be the same with the id in the genesis file.

the meaning of all commands could be searched online.

Create account

personal.newAccount()

Mining

miner.start(1)

repeate to create another node, and try to add the aws node as a peer.

admin.addPeer(“enode://34e249eff0b78db8a279410b9a61ea43b329c2c6be6e5f145c3123f5fe117ee28185b6379ed8cdb8c6a40107a7f27c2f43de87e7e625decf5eef5ff7d65e9ef0@[18.194.45.47]:8057?discport=0”)

3 Voting smart contract

now you have the private network, we could start to use the truffle framwork to interact with geth to deploy the contract.

3.1 install required softwares

sudo apt install npmnpm install -g trufflesudo apt-get install nodejsnpm install web3

3.2 Get the source code from github

dali@dali:~/Desktop/0902-KIT-blockchain$ git clone https://github.com/allonchain/Voting0902.gitdali@dali:~/Desktop/0902-KIT-blockchain$ cd Voting0902/dali@dali:~/Desktop/0902-KIT-blockchain/Voting0902$ ls
app build node_modules test box-img-lg.png contracts npm-debug.log truffle.js box-img-sm.png migrations package.json webpack.config.js
dali@dali:~/Desktop/0902-KIT-blockchain/Voting0902$ truffle console
truffle(development)> web3.personal.unlockAccount(“0xda5f372bba3e16531e9bbb244441be69fe46a193”,”YOUR PASSWORD”,100000)
true
dali@dali:~/Desktop/0902-KIT-blockchain/Voting0902$ truffle migrate

3.3 Voting

Now, go ahead and start the the server, hosting a web page to display the information.

dali@dali:~/Desktop/0902-KIT-blockchain/Voting0902$ npm run dev

you can vote through the web page, it connects the local geth node or your web metamask wallet.

You can also try to use the ethereum wallet to vote:

dali@dali:~$ ethereumwallet

GitHub Repository : https://github.com/allonchain/Voting0902

REFERENCE:

--

--

allonchain
0 Followers

allonchain.org ; Records both protocol and data on chain