Amit Kumar Jaiswal
Google Summer of Code 2017 Intern at CNCF [Kubernetes]
amitkumarj441@gmail.com
Twitter/Medium : @AMIT_GKP
Github : @amitkumarj441
1. The philosophy behind blockchain
2. What is Ethereum?
3. Pros & Cons of ethereum
4. Supporting tools for ethereum
5. Developing framework
6. BlockCypher
7. Truffle & TestRPC – A quick testing environment for block chain
Outline
Blockchain technology is the technological basis of Bitcoin, first described by its mysterious author Satoshi Nakamoto in his white paper
“Bitcoin: A Peer-to-Peer Electronic Cash System"
How to let many different users agree on the current state of the blockchain even though they don't trust each other or any central authority?
Proof of Work, also called POW
PoW algorithms are able to use the number and difficulty of solutions being found to measure how much of the network agrees on the current state of the blockchain.
That way, users can simply pick the longest valid chain with the highest amount of work as the correct chain.
That requirement for resources is a good thing, because it means that interfering with the group's consensus takes a lot of resources (a.k.a. money).
But this implies that Proof of Work is extremely inefficient in term of energy, and therefore also very expensive; which incentivize miners to centralize the hashing power
But this implies that Proof of Work is extremely inefficient in term of energy, and therefore also very expensive; which incentivize miners to centralize the hashing power
→ obviously not desirable for a network whose goal is to minimize the need to trust third parties.
Bitcoin maybe viewed as centralized in China by a small group of people holding the mining power which may be greater than 51%.
But it is another story :p
They both play their roles well, even though they are separate roles with some overlap
1. Cryptocurrency
2. An inherent blockchain
3. A decentralized consensus-based proof mechanism
4. Miners that support the network
So what is the difference between ether & bitcoin
It’s a form of cloud-based micro-value pricing that un-bundles a layer from the traditional cloud computing stack.
Programmability
Scalability
Upgradability
Transactions Manageability
Visibility
Affordability
Security
Speed/Performance
High Availability
Extensibility
Mining in the Ethereum context can be done by regular computers !
When a contract is executed as a result of being triggered by a message or transaction, every instruction is executed on every node of the network.
This has a cost: for every executed operation there is a specified cost, expressed in a number of gas units.
→ Prevent deliberate attacks and abuse on the Ethereum network.
→ Gas limit keeps the network decentralized
curl -s https://api.blockcypher.com/v1/eth/main
{
"name": "ETH.main",
"height": 1663353,
"hash": "863dda1124f2b438c607f5b8d00e8511f6f8d206b21aad3b9c460b8c5221e31b",
"time": "2016-06-08T00:46:34.795856213Z",
"latest_url": "https://api.blockcypher.com/v1/eth/main/blocks/863dda1124f2b438c607f5b8d00e8511f6f8d206b21aad3b9c460b8c5221e31b",
"previous_hash": "783aa3ef1b45121ee5bc33acb6c5986d6132d04cf20c85ba256b155b2c196006",
"previous_url": "https://api.blockcypher.com/v1/eth/main/blocks/783aa3ef1b45121ee5bc33acb6c5986d6132d04cf20c85ba256b155b2c196006",
"peer_count": 52,
"unconfirmed_count": 11924,
"high_gas_price": 40000000000,
"medium_gas_price": 20000000000,
"low_gas_price": 5000000000,
"last_fork_height": 1661588,
"last_fork_hash": "79075d95aacc6ac50dbdf58da044af396ca97e09cbb31527809579cc96f1c8a7"
}
The API always returns values in wei, the lowest non-divisible unit in Ethereum.
There are 10^18 wei in a single ether (1,000,000,000,000,000,000w = 1ETH).
curl -s https://api.blockcypher.com/v1/eth/main/blocks/7
{
"hash": "e0c7c0b46e116b874354dce6f64b8581bd239186b03f30a978e3dc38656f723a",
"height": 7,
"chain": "ETH.main",
"total": 0,
"fees": 0,
"size": 1078,
"ver": 0,
"time": "2015-07-30T15:28:30Z",
"received_time": "2015-07-30T15:28:30Z",
"coinbase_addr": "dd2f1e6e498202e86d8f5442af596580a4f03c2c",
"relayed_by": "",
"nonce": 13599487003767981,
"n_tx": 0,
"prev_block": "1f1aed8e3694a067496c248e61879cda99b0709a1dfbacd0b693750df06b326e",
"mrkl_root": "ff8d97d9ca01ee59c793c4da2ba4ce8c31d358b42f216ab2f11af4bb097b6a2b",
"uncles": [
"4b8729311c5b59f418c5154fd54d85e6a8b42eabf83a1d3c05c754a8f10354cc"
],
"txids": [],
"depth": 1656646,
"prev_block_url": "https://api.blockcypher.com/v1/eth/main/blocks/1f1aed8e3694a067496c248e61879cda99b0709a1dfbacd0b693750df06b326e",
"tx_url": "https://api.blockcypher.com/v1/eth/main/txs/"
}
The API always returns values in wei, the lowest non-divisible unit in Ethereum.
There are 10^18 wei in a single ether (1,000,000,000,000,000,000w = 1ETH).
curl -sX POST https://api.blockcypher.com/v1/eth/main/addrs?token=YOURTOKEN
{
"private": "44c984a089184a9a659e10210af360049f366a34f7f3cb253ac5cbfcb33f3d7c",
"public": "04b7de9b47fc1fb278648266316dffe47cc7f5280b69b05b5770ad1624ee3da7647c98ade5e4c35b99e606535eaca54db0f913c8aa1fa7226f15b9996325d0989d",
"address": "e3f7e628fff7589218d88ae1d6bcdac52fef2168"
}
curl -sd '{
"inputs":[
{"addresses": ["add42af7dd58b27e1e6ca5c4fdc01214b52d382f"]}
],
"outputs":[
{"addresses": ["884bae20ee442a1d53a1d44b1067af42f896e541"],
"value": 4200000000000000}
]}' https://api.blockcypher.com/v1/eth/main/txs/new?token=YOURTOKEN
{
"tx": {
"block_height": -1,
"block_index": 0,
"hash": "9403244c6bcfe4063e7fb33138b02881d7a72ccdbd9c04d9abdff2b432d67ada",
"addresses": [
"add42af7dd58b27e1e6ca5c4fdc01214b52d382f",
"884bae20ee442a1d53a1d44b1067af42f896e541"
],
"total": 4200000000000000,
"fees": 861000000000000,
"size": 44,
"gas_used": 0,
"gas_price": 41000000000,
"relayed_by": "",
"received": "2016-06-08T01:32:35.573921633Z",
"ver": 0,
"double_spend": false,
"vin_sz": 1,
"vout_sz": 1,
"inputs": [
{
"sequence": 0,
"addresses": [
"add42af7dd58b27e1e6ca5c4fdc01214b52d382f"
]
}
],
"outputs": [
{
"value": 4200000000000000,
"addresses": [
"884bae20ee442a1d53a1d44b1067af42f896e541"
]
}
]
},
"tosign": [
"a83f5bea598e0d217a03a2646d6c49edb2e99daf4537b2c09b008df76b77acec"
]
}
curl -s https://api.blockcypher.com/v1/eth/main/addrs/$YOURADDRESS/balance
// it should return this
{
"address": "738d145faabb1e00cf5a017588a9c0f998318012",
"total_received": 9762206505909057760,
"total_sent": 6916970312523512365,
"balance": 2845236193385545395,
"unconfirmed_balance": 0,
"final_balance": 2845236193385545395,
"n_tx": 702,
"unconfirmed_n_tx": 0,
"final_n_tx": 702
}
//but actually it will return this
{"error": "Address not found or unsupported: 8134d81b5bfc1f9143424bde04d9b969d77b6213"}
The address doesn't exist on the Ethereum block chain until it has a transaction associated with it
Without requiring funding an address, if anyone could embed addresses into the block chain, it would be a large DDoS vector.
cat publishGreeter.json
{
"solidity":
"contract greeter{
/* define variable greeting of the type string */
string greeting;
/* this runs when the contract is executed */
function greeter(string _greeting) public {
greeting = _greeting;
}
/* main function */
function greet() constant returns (string) {
return greeting;
}
}",
"params": ["Hello BlockCypher Test"]
}
Check solidity compilation via non-published test
curl -sd @testGreeter.json https://api.blockcypher.com/v1/eth/main/contracts?token=YOURTOKEN
[
{
"solidity":
"contract greeter{
/* define variable greeting of the type string */
string greeting;
/* this runs when the contract is executed */
function greeter(string _greeting) public {
greeting = _greeting;
}
/* main function */
function greet() constant returns (string) {
return greeting;
}
}",
"bin": "606060405260405161023e38038061023e8339810160405280510160008054600160a060020a031916331790558060016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10609f57805160ff19168380011785555b50608e9291505b8082111560cc57600081558301607d565b50505061016e806100d06000396000f35b828001600101855582156076579182015b82811115607657825182600050559160200191906001019060b0565b509056606060405260e060020a600035046341c0e1b58114610026578063cfae321714610068575b005b6100246000543373ffffffffffffffffffffffffffffffffffffffff908116911614156101375760005473ffffffffffffffffffffffffffffffffffffffff16ff5b6100c9600060609081526001805460a06020601f6002600019610100868816150201909416939093049283018190040281016040526080828152929190828280156101645780601f1061013957610100808354040283529160200191610164565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600f02600301f150905090810190601f1680156101295780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b565b820191906000526020600020905b81548152906001019060200180831161014757829003601f168201915b505050505090509056",
"abi": [
{
"constant": false,
"inputs": [],
"name": "kill",
"outputs": [],
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "greet",
"outputs": [
{
"name": "",
"type": "string"
}
],
"type": "function"
},
{
"inputs": [
{
"name": "_greeting",
"type": "string"
}
],
"type": "constructor"
}
]
}
]
Looking good! Now let's publish it to block chain
cat publishGreeter.json
{
"solidity":
"contract greeter{
/* define variable greeting of the type string */
string greeting;
/* this runs when the contract is executed */
function greeter(string _greeting) public {
greeting = _greeting;
}
/* main function */
function greet() constant returns (string) {
return greeting;
}
}",
"params": ["Hello BlockCypher Test"],
"publish": ["greeter"],
"private": "3ca40...",
"gas_limit": 500000
}
Now publish the greeter contract
curl -sd @testGreeter.json https://api.blockcypher.com/v1/eth/main/contracts?token=YOURTOKEN
[
{
"name": "greeter",
"solidity":
"contract greeter{
/* define variable greeting of the type string */
string greeting;
/* this runs when the contract is executed */
function greeter(string _greeting) public {
greeting = _greeting;
}
/* main function */
function greet() constant returns (string) {
return greeting;
}
}",
"bin": "606060405260405161023e38038061023e8339810160405280510160008054600160a060020a031916331790558060016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10609f57805160ff19168380011785555b50608e9291505b8082111560cc57600081558301607d565b50505061016e806100d06000396000f35b828001600101855582156076579182015b82811115607657825182600050559160200191906001019060b0565b509056606060405260e060020a600035046341c0e1b58114610026578063cfae321714610068575b005b6100246000543373ffffffffffffffffffffffffffffffffffffffff908116911614156101375760005473ffffffffffffffffffffffffffffffffffffffff16ff5b6100c9600060609081526001805460a06020601f6002600019610100868816150201909416939093049283018190040281016040526080828152929190828280156101645780601f1061013957610100808354040283529160200191610164565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600f02600301f150905090810190601f1680156101295780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b565b820191906000526020600020905b81548152906001019060200180831161014757829003601f168201915b505050505090509056",
"abi": [
{
"constant": false,
"inputs": [],
"name": "kill",
"outputs": [],
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "greet",
"outputs": [
{
"name": "",
"type": "string"
}
],
"type": "function"
},
{
"inputs": [
{
"name": "_greeting",
"type": "string"
}
],
"type": "constructor"
}
],
"gas_limit": 500000,
"creation_tx_hash": "61474003e56d67aba6bf148c5ec361e3a3c1ceea37fe3ace7d87759b399292f9",
"address": "0eb688e79698d645df015cf2e9db5a6fe16357f1",
"params": [
"Hello BlockCypher Test"
]
}
]
cat call.json
{
"private": "3ca40...",
"gas_limit": 20000
}
curl -d @call.json -s https://api.blockcypher.com/v1/eth/main/contracts/0eb688e79698d645df015cf2e9db5a6fe16357f1/greet?token=YOURTOKEN
{
"gas_limit": 20000,
"address": "0eb688e79698d645df015cf2e9db5a6fe16357f1",
"results": [
"Hello BlockCypher Test"
]
}
call the greet method, using your own private key and gas amount to fund
They won’t download the full blockchain and will create a private instance of the Ethereum network with its own blockchain, so are faster to use for development.
1.Start an Ethereum node (e.g. geth, testrpc or ethersim)
2.Compile your Solidity smart contract using solc
→ get back the binary
3.Deploy your compiled contract to the network.
→ get back the contract’s blockchain address and ABI (a JSON-ified representation of your compiled contract’s variables, events and methods that you can call)
4.Call stuff in the contract using web3.js’s JavaScript API to interact with it
var greeterSource = 'contract mortal { address owner; function mortal() { owner = msg.sender; } function kill() { if (msg.sender == owner) selfdestruct(owner); } } contract greeter is mortal { string greeting; function greeter(string _greeting) public { greeting = _greeting; } function greet() constant returns (string) { return greeting; } }'
var greeterCompiled = web3.eth.compile.solidity(greeterSource)
var _greeting = "Hello World!"
var greeterContract = web3.eth.contract(greeterCompiled.greeter.info.abiDefinition);
var greeter = greeterContract.new(_greeting,{from:web3.eth.accounts[0], data: greeterCompiled.greeter.code, gas: 300000}, function(e, contract){
if(!e) {
if(!contract.address) {
console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined...");
} else {
console.log("Contract mined! Address: " + contract.address);
console.log(contract);
}
}
})
Within less than a minute, you should have a log with the contract address, this means you've successfully deployed your contract.
And you will get two data:
var greeter = eth.contract(ABI).at(Address);
greeter.greet();
Business people must learn about decentralization, and ask if old processes can be replaced by decentralized ones, enabled by Ethereum