My notes on Ethereum
At the highest level the Deloitte executive briefings provide an excellent overview of implications of the idea of blockchains, bitcoin itself and other clones. There are many of them, here is one: [ https://dupress.deloitte.com/dup-us-en/industry/financial-services/evolution-of-blockchain-github-platform.html ]. Here are several others: https://dupress.deloitte.com/dup-us-en/tags/blockchain.html . To see the incredibly rapid change in the industry look here; [ https://thenextweb.com/hardfork/2017/11/09/deloitte-blockchain-26000-projects/ ].
Ethereum itself… Ethereum and Bitcoin are examples of blockchain based applications. Ethereum is more flexible in that it can run user defined contracts. There are many tutorials on getting up to speed on Ethereum [ https://medium.com/@mattcondon/getting-up-to-speed-on-ethereum-63ed28821bbe ]. Another great overview is this one : http://truffleframework.com/tutorials/ethereum-overview . Here’s another overview as well [ https://blockgeeks.com/guides/what-is-ethereum/ ]. Truffle has a good campfire story going over the history from scratch as well [ http://truffleframework.com/tutorials/ethereum-overview ].
Blockchains… A blockchain is an idea of having a shared public database which is maintained by all parties around the world. All parties keep a copy of all data. There are various techniques to synchronize the database state and to prevent bad actors from writing arbitrary state to the database. Many different organizations are looking at ways to make blockchains scale better since there are currently some scaling issues with respect to number of transactions that can happen per hour and with respect to storage (it can be expensive to store the entire blockchain and it may become necessary to just store fragments or hashes of a blockchain). There are many separate blockchains running concurrently in the world today. [ https://en.wikipedia.org/wiki/Blockchain ]. A blockchain acts as a public ledger of transactions in the case of both Ethereum and Bitcoin. There are many people trying to do a better job because of criticisms of the computational costs. See: [ https://blog.gridplus.io/money-vs-cryptocurrency-the-real-costs-part-1-33c09dfea671 ] . Also see: [ https://breathepublication.com/blockchain-just-became-obsolete-the-future-is-hashgraph-de4948609cbf ]. As well: [ https://www.rchain.coop/#home ]. However many of these so called improvements are not open source and are themselves unproven. Others are critical of attempts to reduce the costs of blockchain: [ http://www.truthcoin.info/blog/pow-cheapest/ ]. Here’s another critique of ethereum [ https://techcrunch.com/2017/10/03/the-new-age-of-icos-is-here-and-its-not-based-on-ethereum/ ].
History… It’s worth reading the original paper here which kick-started a lot of this revolution: [ https://bitcoin.org/bitcoin.pdf ]. Briefly here is some of the early history (of which many contributors are still active today):
- Merkle Tree R&D https://en.wikipedia.org/wiki/Merkle_tree
- “Pricing via Processing or Combatting Junk Mail”, Cynthia Dwork, Moni Naor, Adam Back, https://en.wikipedia.org/wiki/Hashcash, 1992
- BMoney, Wei Dai, http://lesswrong.com/lw/jgz/aalwa_ask_any_lesswronger_anything/ap3c, http://weidai.com, 1998
- Reusable proofs of work, Hal Finney, 2005
- “New advances in replicated database technology”, BitGold, Nick Szabo, 2008
- Bitcoin Whitepaper, https://bitcoin.org/bitcoin.pdf, Satoshi Nakamoto, October 31st 2008
- Bitcoin goes live, Satoshi Nakamoto, January 3rd 2009
- Bitcoin Foundation Launched, Gavin Andresen, September 12th 2012
- Ethereum, Vitalik Buterin, Mihai Alisie, Anthony Di Iorio, and Charles Hoskinson, https://github.com/ethereum/wiki/wiki/White-Paper, https://en.wikipedia.org/wiki/Ethereum, October 1st 2013
Wallets and accounts… Using the ethereum network you first make a private/public key pair which exists on the blockchain and which can hold some amount of ether. A wallet is a smart contract — often managing the flow of money. Note that bitcoin and ethereum are totally separate universes and they each implement things like currency differently [ see https://ethereumbuilders.gitbooks.io/guide/content/en/design_rationale.html ].
Communities… Ethereum has several communities that are worth joining especially if you are looking for people to talk to or developers. There are also many apps and many contracts that are visible. The practice is to make ethereum source code public for verifiability — many projects are hosted on github. Also see [ https://medium.com/blockchannel/where-can-i-join-the-ethereum-community-3aa5c795b1e5 ]. Of course forums such as http://reddit.com/r/ethereum should be known to all readers.
Existing apps… there are many ethereum apps coming out. Right now one promising one is Filecoin. There are many others. One collection of ethereum apps is here [ https://www.stateofthedapps.com/ ].
ICO’s… There are many ICO’s as well — see https://www.civic.com/ for an example of one family of related projects. There are turnkey ICO launching services now such as http://icobox.io . And there are teams that will build your ICO for you. See also [ https://blockgeeks.com/guides/what-is-an-initial-coin-offering/ ].
User facing UX dev tools… There are a variety of user facing applications for interfacing both with existing Ethereum apps and for creating your own. Like any database, the ethereum blockchain is not natively visible to the web, so some kind of wrapper is needed. Look at Metamask or Mist for examples here. For our purposes Meteor is a nice way to wrap up an ethereum app with a user interface [ https://github.com/ethereum/wiki/wiki/Dapp-using-Meteor ].
Core development tools… For actual development and programming of ethereum apps Truffle looks like the clear winner — it is basically a collection of tools not unlike a makefile or build system — see [ http://truffleframework.com/docs/ ] . There are also excellent tutorials [ http://truffleframework.com/tutorials/ ].
Programming theory..
- Here’s a reference on the ideas around programming distributed applications (considering both the ethereum back end and UX): [ https://dappsforbeginners.wordpress.com/tutorials/introduction-to-development-on-ethereum/ ].
- For basics see https://ethereumbuilders.gitbooks.io/guide/content/en/solidity_tutorials.html
- Here is another tutorial on going from zero to a full ethereum app https://medium.com/@petehumiston/build-your-first-ethereum-smart-contract-with-solidity-tutorial-94171d6b1c4b ].
- Here is a best of breed tutorial to programming smart contracts [ http://solidity.readthedocs.io/en/latest/introduction-to-smart-contracts.html ] — other chapters also dig deeper into implementing things like voting systems. Also look at [ http://solidity.readthedocs.io/en/develop/contracts.html ]
- Also see [ https://github.com/transmute-industries/awesome-smart-contracts ].
- As well [ https://medium.com/@k3no/ethereum-tokens-smart-contracts-3346b62d2a0 ]
Programming best practices...
- A good basic reference is here: https://ethereum.org/token .
- I was also impressed by the Truffle Boxes examples — especially their pet-shop example [ http://truffleframework.com/boxes/ ].
- This twitter clone is also useful [ https://ethereumdev.io/deploying-contract-embark-framework/ ]. There’s another one here as well, using a similar pattern: [ https://github.com/yep/eth-tweet ]
Programming ethereum core principles…
- There exists a database — the ethereum blockchain.
- Each entry in the database has an ether balance.
- Each entry in the database also represents either an external person or a contract.
- A contract refers to an instance on the blockchain — with state and methods. If you’re familiar with C++ a contract is similar to the live runtime instance of a class (not the class definition). It is similar in some respects to a prototype in the way it is discussed. It can have arbitrary state, arbitrary methods, public and private logic and suchlike. Contracts can directly communicate with each other because the database is fully visible to all contracts. There are no library services like a C++ programmer may be familiar with (although it is possible to write a library). There are no search or lookup services to find other contracts. The only way to know about anything else is to have it passed to a public contract function.
- Each database entry ether balance is affected only by contracts. [ http://solidity.readthedocs.io/en/develop/introduction-to-smart-contracts.html and http://solidity.readthedocs.io/en/develop/contracts.html ]
- A ubiquitous practice in Ethereum is to give people “tokens”. One way to do this is to physically make a new contract owned by that person — the contract is the token. Another way is to keep a balance sheet inside of a manager contract. [ https://github.com/OpenZeppelin/zeppelin-solidity/blob/master/contracts/token/MintableToken.sol ] . See also [ https://blog.zeppelin.solutions/how-to-create-token-and-initial-coin-offering-contracts-using-truffle-openzeppelin-1b7a5dae99b6 ].
Designing your app in Ethereum requires thinking about the contracts or agreements that exist between the parties. There are multiple separate layers of consideration. There are the core contracts defining the app (and any ether that is involved). There are dApp wrappers so users can interact with it on the web. And there is also likely a crowd-sale contract for handling your ICO if you have one, or however it is that you choose to distribute any tokens that represent access to the service you’re providing. You don’t need to use a token based approach at all of course.
Setting up an ethereum testbed…
It’s easy to do it privately, but I wanted to do a full blown test with other developers and visible to casual users; basically what could become a website. My approach was I spun up an instance of Ubuntu 17.10 on Google Cloud — giving it extra disk space since often disks are too small. There are several ways to set this up such that you have geth running with some initial amount of ethereum in your account (although mining is easy on your own instance as well). See [ https://github.com/ethereum/go-ethereum/wiki/Managing-your-accounts ]. Here’s what I did:
- Sshed into that box, elevated to root and installed nodejs, npm, truffle and ethereum — here are reasonable instructions for the last [ https://github.com/ethereum/go-ethereum/wiki/Installation-Instructions-for-Ubuntu ] — I used ‘apt install ethereum-unstable’ for 17.10.
- I wanted to setup my own ethereum instance with some default ether in it to play with. Using testrpc looks useful but I’d like to play more with geth. It’s slightly annoying to configure so I made a small script to do it:
- rm -R -f ~/.ethereum_private
- mkdir ~/.ethereum_private
- address1=$(geth — datadir ~/.ethereum_private — password <(echo whatawonderfulday)
- account new | sed ‘s/Address: {//’ | sed ‘s/}//’)
- echo “Made account $address1”cat > genesis.json <<- EOM
{ “config”: { “chainId”: 15, “homesteadBlock”: 0, “eip155Block”: 0, “eip158Block”: 0 }, “difficulty”: “400”, “gasLimit”: “2100000”, “alloc”: { “$address1”: { “balance”: “30000” } } }
EOM - geth — datadir ~/.ethereum_private init ./genesis.json
- geth — datadir ~/.ethereum_private — password <(echo whatawonderfulday) — unlock 0 — fast — cache 512 — ipcpath ~/geth.ipc — rpc — rpcport “8545” — rpcaddr “127.0.0.1” — rpccorsdomain “*” — port “30303” — rpcapi “personal,db,eth,net,web3” — mine console
Note that for metamask you may have to do the following magic incantation and wave a dead chicken over RPC with this [ http://metamask.consensyssupport.happyfox.com/kb/article/2-using-a-local-node ].
At this point (from the console) it’s easy to check your balance:
- eth.getBalance(eth.accounts[0])
And if you setup your firewall rules (https://console.cloud.google.com/networking/firewalls) or if you’re on EC2 etc do whatever is the right thing there — and expose port 8545 (to tcp4 not tcp6) — then you can do a remote curl query easily enough as well:
- curl -X POST — data ‘{“jsonrpc”:”2.0",”method”:”web3_clientVersion”,”params”:[],”id”:67}’ -H “Content-Type: application/json” http://256.0.0.0:8545
Or you can use geth to attach to geth…
- geth attach http://256.0.0.0:8545
Using testrpc instead…
- Metamask has a mnemonic incantation it can use…
- testrpc -a -m “candy maple cake sugar pudding cream honey rich smooth crumble sweet treat” — gasLimit 1111115141592
Using Truffle Examples…
- Basic process is “Truffle init” and then copy and paste one of the test examples like so [ https://blog.zeppelin.solutions/how-to-create-token-and-initial-coin-offering-contracts-using-truffle-openzeppelin-1b7a5dae99b6 ]
- I setup truffle.js to run on the local ethereum network for my tests : module.exports = { networks: { development: { host: “35.202.26.50”, port: 8545, gas: 4712388, network_id: “*” // Match any network id } }};
- There appear to be lots of hassles with actually running truffle to save contracts — gas limits are hard to predict [ https://www.codeooze.com/blockchain/ethereum-geth-private-blockchain/ ] and [ https://github.com/trufflesuite/truffle/issues/271 ] and [ https://medium.com/@gus_tavo_guim/using-truffle-to-create-and-deploy-smart-contracts-95d65df626a2 ]
- http://truffleframework.com/tutorials/pet-shop — this example actually has a front end in react.
- To run that front end at port 80 modify the bs-config like so { “port”:80, “host”:”35.202.26.50", “server”: { “baseDir”: [“./src”, “./build/contracts”] }}
- Make sure to expose the ports (see netstat -vnl) and ubuntu has a quirk of putting things on ipv6 ports if the host ip is not specified.
Building and pushing and poking at Contracts…
- SO MUCH POOR DOCUMENTATION ON THE WEB.
- OBSOLETE: http://hypernephelist.com/2016/12/13/compile-deploy-ethereum-smart-contract-web3-solc.html
- Still — a few handy incantations that can be run from the truffle console or as a raw piece of nodejs code on a server
- Truffle really is the easiest best way to build and manage contracts. I tend to end up using truffle migrate — reset out of paranoia.
- Truffle console operations to talk to a truffle visible contract is excellent.
- let accounts =0;
- web3.eth.getAccounts(function(err,res) { accounts = res; });
- let myresult = 0;
- MyContract.deployed().then(instance => instance.mySpecialMethod(accounts[0])).then(result => myresult= result)
- Some people may not be familiar with es6 notation — older notation is also totally fine:
- MyContract.deployed().then(instance => instance.mySpecialMethod(accounts[0],{from:account[0]})).then(function(result) { myresult= result })
- I personally had some kind of strange bug in this truffle handler : node_modules/truffle-contract/contract.js
- I circumvented it by hacking at Async to make it synchronous:
- Provider.prototype.sendAsync = function() { return this.provider.send.apply(this.provider, arguments); };
- There is a secret parameter that can be passed to a contract method that does any actual work such as changing state on the blockchain—hardly documented but nothing works without it — see {from:account[0]} .
- See this note: We passed an object as the third parameter to sendCoin. Note that the sendCoin function in our Solidity contract doesn’t have a third parameter. What you see above is a special object that can always be passed as the last parameter to a function that lets you edit specific details about the transaction. Here, we set the from address ensuring this transaction came from account_one. [ https://github.com/trufflesuite/truffle-contract ]
- http://truffleframework.com/tutorials/upgrading-from-truffle-2-to-3
- http://truffleframework.com/docs/getting_started/contracts
- https://medium.com/@mvmurthy/full-stack-hello-world-voting-ethereum-dapp-tutorial-part-1-40d2d0d807c2
- https://github.com/trufflesuite/truffle-artifactor < OBSOLETE!
The existing examples do work if they’re very carefully babysat. They’re a bit glitchy — don’t refresh, sensitive to state initialization, there are upper and lower gas limits that make it hard to test on anywhere that isn’t the testnet, require browser plugins (metamask) and CORS settings on the server to permit metamask to work… and it all feels fragile…. but it does seem possible to build web apps that can do transactions over the ethereum network.
Trying to think through how people actually will in the future interact with these services. It’s a bit of a different model than what I think we’re used to. Today the commitment to make a purchase is a somewhat heavy transaction. Users enter their credit card or paypal and the browser isn’t really thought of as a wallet per se — but rather just a kind of kiosk or gateway. I’m imagining the future of web based purchases or transactions that require a signature to be more like metamask — where some kind of identity or signing authority is attached to your browser and you use it often to certify transactions.
Once you’ve wrapped your head around the basics — the question becomes more what are the kinds of services that can be built around these ideas. And in fact basic token exchanges, acting as a way to fund projects early, while giving liquidity to participants, looks like a totally reasonable way to do all this.