Blockchain Developer Bootcamp 2020 – Consensys – Learning Note 1

RgRi2_ulPVcDc3BjQgpg76V2-WAGaQvAUhVmYXJtb3VudGFpbkBnbWFpbC5jb21YBAAAAAY~
Documentation

https://docs.soliditylang.org/en/v0.8.4/

https://learnblockchain.cn/docs/ethers.js/api-wallet.html#wallet

Tools

Github – https://www.youtube.com/watch?v=SWYqp7iY_Tc
Visual Studio Code – https://www.youtube.com/watch?v=cWFniUXr-W8
Atom Editor – https://www.youtube.com/watch?v=WWwBQQOGllo&list=PLYzJdSdNWNqwNWlxz7bvu-lOYR0CFWQ4I

JavaScripts
Node – https://www.youtube.com/watch?v=fBNz5xF-Kx4&t=1733s
npm – https://www.youtube.com/watch?v=jHDhaSSKmB0
npx / nvm

React/Typescripts –

React Native – https://www.youtube.com/watch?v=Hf4MJH0jDb4

Go – https://www.youtube.com/watch?v=SqrbIlUwR0U

Solidity – https://www.youtube.com/watch?v=ipwxYa-F1uY&t=14s
Truffle – https://www.youtube.com/watch?v=b2VInFwZmNw
Remix – https://remix.ethereum.org/

Blockchain Technology

Distributed Ledger

  • foundation of accounting => Ledger
  • transaction records
  • transaction protected via public/private key cryptography

Consensus Mechanism

  • facilitates the agreement process to ensure the data consistency
    • PBFT (Practical Byzantine Fault Tolerance) => Byzantine Generals Problem
    • Proof of works => guessing game

    • proof of stakes
  • To determine what is a valid block

Types of Blockchains

  • Public (Permission-less)
  • Shared (Consortium)
  • Private (Sandbox / Permission)

Blockchain Platforms

### Key Developer Tools

### Geth

### Mist

### Parity

### Metamask

### Remix

### truffle

### Ganache

### Web3.js

### IPFS / SWARM

### Developer Tools overview

Installing Geth

Geth (Go-Ethereum) is a command line interface for running a full ethereum node implemented inGo.

Geth Capabilities

By installing and running geth, you can take part in the Ethereum live network and

  1. mine real ether
  2. transfer funds between addresses
  3. create contracts and send transactions
  4. explore block history
  5. and much more

Geth Interfaces:

  • Javascript Console
  • JSON-RPC server
  • Command line

Installation guide on Geth
https://github.com/ethereum/go-ethereum/wiki/Building-Ethereum

Mac
brew tap ethereum/ethereum
brew install ethereum

Window
https://geth.ethereum.org/downloads/.

Ubuntu
https://github.com/ethereum/go-ethereum/wiki/Installation-Instructions-for-Ubuntu

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

Operating Geth:

$ geth
$ geth –help

$ geth console

### remove log pollute your console
$ geth –verbosity 0 console

or

$ debug.verbosity(0)

$ geth attach <example below>

$ geth attach ipc:/some/custom/path
$ geth attach http://191.168.1.1:8545
$ geth attach ws://191.168.1.1:8546

### write log file
$geth –verbosity 5 console 2>> /tmp/eth.log

$ geth.account new

created below address with password; kanthan
Account 1:
0xB6b4EB16d68Db203A41fD53124a57e2Bd8e3e38F
Account 2:
0x008e166A3A2Aad7FF08483fe3CCA920c1f78F738

### Configure a private blockchain genesis.json file
{
“config”: {
“chainId”: 4568,
“homesteadBlock”: 0,
“eip150Block”: 0,
“eip155Block”: 0,
“eip158Block”: 0
},
“alloc” : {},
“difficulty” : “0x100”,
“extraData” : “”,
“gasLimit” : “0x7A1200”,
“parentHash” : “0x0000000000000000000000000000000000000000000000000000000000000000”,
“timestamp” : “0x00”
}

The chainId identifies the blockchain. The Ethereum main net has a chainId of 1. The Ropsten testnet has a chainId of 3, Rinkeby is 4 and Kovan is 42. This genesis file has a chainId of 4568, hopefully nobody else is running a chain with the same chainId. We can use the same client to connect to all of the different Ethereum networks.
The Ethereum protocol has hard forked and introduced several backward-incompatible protocol changes. homesteadBlock and eip155/8Block tell geth in which blocks the changes start. We set these to zero.
alloc allows us to create addresses and fill the accounts with ether upon initializing the blockchain. We will leave this empty and mine ether to fill our accounts.
Difficulty indicates how difficult it will be to discover a valid hash of the block. It defines the mining target, which the clients calculate from the previous block’s difficulty level and the timestamp. The higher the difficulty, the more calculations the miner will have to do to find a block (on average). This value fluctuates to maintain a target block generation time (~15 seconds in Ethereum). We keep this value low on our private chain so we can quickly find blocks so we don’t have to wait.
extraData is an optional 32 byte value where we can add anything.
gasLimit is a value that sets the chain-wide limit of gas expenditure per block. We set the value to 10,000,000 as that is the current limit on the main net.
parentHash is the keccak256 hash of the parent block’s header. This pointer to the parent block builds the chain of blocks.
timestamp is the output of Unix time() function at the block’s creation. This parameter helps determine if the difficulty level should be changed to maintain a consistent average block time. It also allows us to verify the order of the blocks in the chain.

### Initiate private blockchain [ or we can use puppeth, which is a private blockchain manager]
$ geth –datadir . init genesis.json

The last line of output in the console should say Successfully wrote genesis state.
### Create new account in the private blockchain
$ geth –datadir . account new

created below address with password:1234
Account 1:
0x1367E1C8772FB7123e1fb9f2976A9535bF0c7A7d
Account 2:
0xA96C596c1e654Cb2af4e5d033Ee6E2c667Bec6E1

### Start the private network so that we can mine blocks on the private chain
If you’re on a Win/Mac, run:
>>geth –allow-insecure-unlock –datadir . –keystore ~/Library/ethereum/keystore –networkid 4568 –http –http.addr ‘0.0.0.0’ –http.corsdomain “*” –http.port 8502 –http.api ‘personal,eth,net,web3,txpool,miner’ –mine –miner.etherbase=YOUR_ETHEREUM_ADDRESS_HERE

### example
>>geth –allow-insecure-unlock –datadir . –keystore ~/Library/ethereum/keystore –networkid 4568 –http –http.addr ‘0.0.0.0’ –http.corsdomain “*” –http.port 8502 –http.api ‘personal,eth,net,web3,txpool,miner’ –mine –miner.etherbase=0x3abb55dcba6302d01cd1e4490c32681886f06141
If you’re on Linux, run:
>>geth –allow-insecure-unlock –datadir . –keystore ~/.ethereum/keystore –networkid 4568 –http –http.addr ‘0.0.0.0’ –http.corsdomain “*” –http.port 8502 –http.api ‘personal,eth,net,web3,txpool,miner’ –mine –miner.etherbase=YOUR_ETHEREUM_ADDRESS_HERE
Put the Ethereum address generated earlier instead of YOUR_ETHEREUM_ADDRESS_HERE.

Open another Terminal,
>>geth attach \.pipegeth.ipc

>>web3
### Create new accounts in private blockchain
>>personal.newAccount(‘passwordForTheAccount’)

>> eth.accounts
> eth.accounts
[“0x3abb55dcba6302d01cd1e4490c32681886f06141”, “0xf8c5003bf9c3d929827c6cbe9b0149b5c58606c3”]
>> eth.getBalance(eth.accounts[0])

### Mining the private blockchain
>> miner.start()
>>miner.stop()

### transfer eth to another account
>> eth.getBalance(eth.accounts[0])
>> eth.sendTransaction({to: eth.account[1], from: eth.accounts[0], value: 100})

Note: Make sure your account is unlock
>> personal.unlockAccount(eth.accounts[0])

Note: after transaction is done, the miner must at least mine for 1 block in the private blockchain, otherwise the transfer amount will not shown in the recipient account.

Geth is the portal to any Ethereum Network (e.g. Private, ganache, test and mainnet)
Test network => Ropsten, Kovan, Rinkeby, Goerli

Geth sync options: full, fast, light

### Connecting to Test Network
$ geth –rinkeby –syncmode fast console

Wait for the terminal to say that the blockchain is syncing
INFO [09-11|14:14:28.922] Block synchronisation started

### To avoid the overwhelming info on screen
>>debug.verbosity(0)

### Check the status of the testing node
>> eth.syncing

### Retrieving block data
>> eth.getBlock(3188009)

Note: The miner address is “0x0000…0000” and the nonce is also zeros. This is because the Rinkeby network does not run on proof of work. Rinkeby is a proof-of-authority test network running the Clique consensus protocol. A proof-of-authority network relies on trusted nodes designated as “signers” that have the ability to create blocks. A majority of signers on the network are required to validate the chain. Rinkeby has seven designated signers maintained by the following groups:

  • Ethereum Foundation (3)
  • Infura (1)
  • Oraclize (1)
  • Augur (1)
  • Akasha (1)

### Retrieving Transaction data, getting 1st transaction of the block
>> eth.getTransaction(eth.getBlock(3188009).transactions[0])

### retrieving contract data
>> eth.getCode(0x0e2298E3B3390e3b945a5456fBf59eCc3f55DA16)
>>eth.getStorageAt(“0x832b52302b89fa8E703Cc12dB1B6049984d6fEF7”)

### Get test ether
https://faucet.rinkeby.io/

Note:
If you run into an error
Error: no suitable peers available at
web3.js:3143:20
at web3.js:6347:15
at web3.js:5081:36
at anonymous:1:1

try adding a peer to your node with this command
admin.addPeer(“enode://05b03241bae2a17534a4ffa005d075e38868f89c6db95b0e089c67ff6d3e9ed3f7132d4e9d57f09628f4827cfb370fe5f624c36af44899e423aacf4869a3adf3@13.124.4.106:30303“);
reference page: https://gist.github.com/rfikki/e2a8c47f4460668557b1e3ec8bae9c11

### Proof of Authority (PoA)
Proof-of-Authority is a consensus mechanism used as an alternative to Proof of Work. Where Proof of Work relies on miners expending computing power in a race to create the next block and secure the blockchain, Proof of Authority designates this role to a number of “authority” nodes. In a typical Proof of Authority network, one node or a series of nodes act as validators for the entire network.
Proof-of-Authority (PoA) is an easier way to run a blockchain with semi-trusted participants, such as a consortium blockchain. Designated signers create the possibility of a small, secure blockchain not worried about 51% attacks endemic to Proof-of-Work secured blockchains. PoA also comes with transaction finality, so a new node syncing to the PoA network only has to obtain the latest block to know the state of the network. However, PoA-backed network have their own series of security issues (which are outside the scope of this tutorial).

Hackernoon Tutorial: Setup your own private PoA Ethereum network with Geth
https://hackernoon.com/setup-your-own-private-proof-of-authority-ethereum-network-with-geth-9a0a3750cda8

Tutorial Objectives:

  1. To understand how a Proof of Authority consensus algorithm works.
  2. Learn to implement a PoA using Geth’s Clique module
  3. Run and conduct transactions on the PoA network

### Clique

geth comes with a native Proof-of-Authority protocol called Clique. It creates a genesis block detailing the important specifications of a Proof-of-Authority network. These include:

  • Who are the valid signers of blocks in this network?
  • Which accounts are pre-funded in this network?
  • How often will a block be broadcast by valid signers?

Clique has an important security feature. Any signer can only produce a certain number of consecutive blocks. That number is contingent on the number of total number of signers encoded in the genesis block. The equation for that number is CONSECUTIVE_BLOCK_LIMIT = (NUMBER_OF_TOTAL_SIGNERS / 2) + 1
For example, if there are four nodes designated as signers in the genesis block, one of those signers can only broadcast (4 / 2) + 1 = 3 blocks before the network will refuse any more blocks confirmed by that node. Only until another one of the four designated signers confirms a block can the original node resume submitting.
This creates a mild safeguard against a rogue node overtaking a network and maliciously altering the network state. It also, however, can freeze your network if not enough valid signers are online.
Fun Fact: The Rinkeby testnet runs using Clique-generated Proof-of-Authority.
Reference: https://github.com/ethereum/EIPs/issues/225

### Configure Clique via Puppeth
>> puppeth

In blockchain networks, the block time is considered the “network heartbeat” — how often a confirmed block containing the latest confirmed transactions is broadcast out to the network.
In Proof-of-Work on Ethereum and Bitcoin, this time is moderated by a complex algorithm which has a target network time (~10m for Bitcoin, ~14s for Ethereum). It adjusts variables according to the current capacity of miners on the network.
In Proof-of-Authority, we don’t need that complicated algorithm but we do need a set time to run an orderly network. Since Ethereum’s block time is 12-14 seconds, we’ll put our network’s block time at 7 seconds.
Proof-of-Authority networks can decrease their block time and therefore increase their transaction throughput (the number of transactions processed per second). This is a desirable goal for certain blockchain use cases.

### Sealer Nodes

As discussed above, Proof-of-Authority networks allow only certain nodes, called “sealers” to confirm blocks in the network. Furthermore, the Clique consensus protocol only allows each sealer to confirm a certain number of consecutive blocks. For the sake of demonstrating Proof-of-Authority networks, we’ll just put one here for simplicity’s sake.
This is the address associated with Node 1 in our network: 0x1a4b71b48498237d2817be049b4bc43fad971bca

### Pre-Funded Accounts

Cryptocurrency units can be created one of two ways. First, someone can mine new blocks for a network and be rewarded in that cryptocurrency. Second, the creator of a network can designate certain accounts to have a certain balance in the genesis block (also known as a “pre-mine”).

### Pre-Funded Precompiles
>> Yes
reference: https://ethereum.stackexchange.com/questions/68056/puppeth-precompile-addresses

Last, simply press enter to have the genesis block saved to the default setting of the current directory. Don’t mind the errors you see there — Aleth and Parity are two Ethereum clients that don’t have support for Clique, the PoA consensus mechanism we picked.

### Launching a Bootnode using new terminal
open new terminal, change directory to geth-poa-tutorial

>>bash ./bootnode/bootnode-start-local.sh

This terminal window is now our window into the networking elements of our PoA blockchain. The geth client will attempt to find other nodes via this bootnode
reference reading: https://github.com/ethereum/go-ethereum/wiki/Connecting-to-the-network
We have asked it to provide all the information it receives and gives so we can watch the nodes come online and discover each other.
Note: The bootnode acts more as a network router or hub. It is a central point the nodes can contact to pass through their information and receive other nodes’ information. It does not contain our custom genesis block.

### Launching Node 1 via new terminal
open a new terminal
>> cd node1
>>geth –datadir ./ init poa.json

You should see the statement “Successfully wrote genesis state”
### In the previous terminal, new node found
### start running the ethereum protocol on PoA genesis block
>>geth –datadir ./ –syncmode ‘full’ –port 30311 –rpc –rpcaddr ‘0.0.0.0’ –rpccorsdomain “*” –rpcport 8502 –rpcapi ‘personal,db,eth,net,web3,txpool,miner’ –bootnodes ‘enode://ea2cab82d19b0704299ff837c9e10ee90841d24503e2f6d993fafbf351d9b6a1860cb6f20eee0f35412c4c28ca68c0720f623792f24abdf2ad0d386598a5b4e2@127.0.0.1:30310‘ –networkid 1515 –gasprice ‘1’ –allow-insecure-unlock -unlock 1a4b71b48498237d2817be049b4bc43fad971bca –password password.txt –mine

You should see traffic and mining new block.

Note: You may encounter below error, possibly due to multiple geth instances running without specify the –ipcdisable flag

Just add “–ipcdisable” flag to your command
>>geth –datadir ./ –syncmode ‘full’ –port 30312 –rpc –rpcaddr ‘0.0.0.0’ –rpccorsdomain “*” –rpcport 8503 –rpcapi ‘personal,db,eth,net,web3,txpool,miner’ –bootnodes ‘enode://ea2cab82d19b0704299ff837c9e10ee90841d24503e2f6d993fafbf351d9b6a1860cb6f20eee0f35412c4c28ca68c0720f623792f24abdf2ad0d386598a5b4e2@127.0.0.1:30310‘ –networkid 1515 –gasprice ‘1’ –allow-insecure-unlock -unlock f59a61caf69f7216b83f063c2b9b712b82e50e84 –password password.txt –ipcdisable

Note: CHMOD in Powershell => How to set ownership of file in power shell
::Set Owner of a specific file
>>ICACLS “FolderName” /setowner “YourUserName”

### Launching Node2 via new terminal
Open a new terminal
>> cd node2
>> geth –datadir ./ init poa.json

### Start the ethereum protocol for node2
>> geth –datadir ./ –syncmode ‘full’ –port 30312 –rpc –rpcaddr ‘0.0.0.0’ –rpccorsdomain “*” –rpcport 8503 –rpcapi ‘personal,db,eth,net,web3,txpool,miner’ –bootnodes ‘enode://ea2cab82d19b0704299ff837c9e10ee90841d24503e2f6d993fafbf351d9b6a1860cb6f20eee0f35412c4c28ca68c0720f623792f24abdf2ad0d386598a5b4e2@127.0.0.1:30310‘ –networkid 1515 –gasprice ‘1’ –allow-insecure-unlock -unlock f59a61caf69f7216b83f063c2b9b712b82e50e84 –password password.txt –ipcdisable

Now you should see boot node with communication with both Node 1 (30311) and node 2 (30312).

but in node 1 and node2 terminal window, you should see the difference. Node 1 has mining traffic while node 2 just constantly looking for peers, this is because node 2 is not the sealer node (or otherwise known as valid signer node) in the private blockchain.

NOTE: You do not see any mining activity from Node 2 because Node 2 is not a valid signer node. You can run it as a miner, but any blocks it submits, even valid ones, will be rejected from the network.

### Launching the node 3 via new terminal window
repeat the same process of node 2
Open new terminal
>> cd node3
>> geth –datadir ./ init poa.json
>> geth –datadir ./ –syncmode ‘full’ –port 30313 –rpc –rpcaddr ‘0.0.0.0’ –rpccorsdomain “*” –rpcport 8504 –rpcapi ‘personal,db,eth,net,web3,txpool,miner’ –bootnodes ‘enode://ea2cab82d19b0704299ff837c9e10ee90841d24503e2f6d993fafbf351d9b6a1860cb6f20eee0f35412c4c28ca68c0720f623792f24abdf2ad0d386598a5b4e2@127.0.0.1:30310‘ –networkid 1515 –gasprice ‘1’ –allow-insecure-unlock -unlock 6dcccc3ab843cf7973986870fbffe55fca71acbd –password password.txt –ipcdisable

### PoA transactions

An Ethereum client creates an endpoint to connect with whatever blockchain its running. geth provides a Javascript-based, REPL command-line interface to connect and interact with that endpoint. We can connect to that console and interact on our newly-created PoA blockchain.

In the terminal window for Node 2, I open a new tab within the window using command + T. In that window, I navigate back to geth-poa-tutorial/node2 and type the following command:

>> geth attach \.pipegeth.ipc

### Test connect to network
>> admin.peers

### some commends to be handy
>>net.version
>>eth.blockNumber
>>eth.coinbase

### Send Transactions


### Adding and removing the signers from the network
reference: https://ethereum.stackexchange.com/questions/15541/how-to-add-new-sealer-in-geth-1-6-proof-of-authority

Related Posts