-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath---creating-a-private-ethereum-blockchain-and-using-it-as-a-model-09e64e0a368195b557bf.js.map
1 lines (1 loc) · 104 KB
/
path---creating-a-private-ethereum-blockchain-and-using-it-as-a-model-09e64e0a368195b557bf.js.map
1
{"version":3,"sources":["webpack:///path---creating-a-private-ethereum-blockchain-and-using-it-as-a-model-09e64e0a368195b557bf.js","webpack:///./.cache/json/creating-a-private-ethereum-blockchain-and-using-it-as-a-model.json"],"names":["webpackJsonp","529","module","exports","data","site","siteMetadata","title","markdownRemark","id","html","frontmatter","date","author","bio","avatar","childImageSharp","resolutions","tracedSVG","width","height","src","srcSet","pathContext","slug","previous","fields","next"],"mappings":"AAAAA,cAAc,gBAERC,IACA,SAAUC,EAAQC,GCHxBD,EAAAC,SAAkBC,MAAQC,MAAQC,cAAgBC,MAAA,4BAAmCC,gBAAmBC,GAAA,kJAAAC,KAAA;AAAms6CC,aAAgtEJ,MAAA,iEAAAK,KAAA,gBAAAC,QAA0GJ,GAAA,cAAAK,IAAA,kFAAAC,QAAqHC,iBAAmBC,aAAeC,UAAA,ubAAAC,MAAA,GAAAC,OAAA,GAAAC,IAAA,6DAAAC,OAAA,uEAA4mBC,aAAgBC,KAAA,iEAAAC,UAAoFC,QAAUF,KAAA,uDAA6Db,aAAgBJ,MAAA,wDAA+DoB,MAASD,QAAUF,KAAA,kDAAwDb,aAAgBJ,MAAA","file":"path---creating-a-private-ethereum-blockchain-and-using-it-as-a-model-09e64e0a368195b557bf.js","sourcesContent":["webpackJsonp([3348540548791],{\n\n/***/ 529:\n/***/ (function(module, exports) {\n\n\tmodule.exports = {\"data\":{\"site\":{\"siteMetadata\":{\"title\":\"Engineering at MindLink\"}},\"markdownRemark\":{\"id\":\"/Applications/AzureDevOpsAgent/_work/13/s/src/pages/18-06-06-private-ethereum-blockchain-as-a-model/index.md absPath of file >>> MarkdownRemark\",\"html\":\"<p>Recently there’s been a lot of fascination surrounding blockchains, and how they can be used to improve long-standing industry technologies. Because of this, we’ve been investigating the applications of blockchains in messaging platforms. I’ve developed a blockchain-based messaging web-app which treats the blockchain as the main data model to gain some hands-on experience working with a blockchain.</p>\\n<p>In this post I’ll detail my process and rationale behind creating a private Ethereum blockchain and a web-app which interacts with it, with the hopes of guiding anyone who’d like to try it for themselves. No Ethereum experience necessary!</p>\\n<p><a href=\\\"https://github.com/mindlink/sample-blockchain-messenger\\\">You can check out my proof-of-concept web-app here</a>, which contains the finished web-app and a simple readme with instructions for running the Ethereum network.</p>\\n<h2>Creating a private Ethereum network</h2>\\n<p>The first thing that’s needed to start playing around with Ethereum is a way to leverage the Ethereum protocol, which gives access to the tools needed to interact with Ethereum networks. There are C++ and Python implementations of the protocol, but I’ll be using Geth (the Go implementation) as it can be interacted with through the CLI, making it quick and easy to use. Even though I’m using Geth, any implementation can be used as they should provide similar capabilities.</p>\\n<p>Before we can create the Ethereum network, we’ll first need to create its blockchain (starting with a genesis block).</p>\\n<h3>Creating the genesis block</h3>\\n<p>A blockchain is a data structure which builds upon itself over time, which means that it needs an initial structure to build upon - the genesis block! This block is defined by a genesis state file, which describes both the block’s parameters and some parameters which affect the operation of the Ethereum network.</p>\\n<p>I initialized the genesis block using a state file named <code class=\\\"language-text\\\">genesis.json</code> in my working directory. Here’s what my <code class=\\\"language-text\\\">genesis.json</code> file contains:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-json\\\"><code class=\\\"language-json\\\"><span class=\\\"token punctuation\\\">{</span>\\n <span class=\\\"token property\\\">\\\"config\\\"</span><span class=\\\"token operator\\\">:</span> <span class=\\\"token punctuation\\\">{</span> \\n <span class=\\\"token property\\\">\\\"chainId\\\"</span><span class=\\\"token operator\\\">:</span> <span class=\\\"token number\\\">731</span><span class=\\\"token punctuation\\\">,</span> \\n <span class=\\\"token property\\\">\\\"homesteadBlock\\\"</span><span class=\\\"token operator\\\">:</span> <span class=\\\"token number\\\">0</span>\\n <span class=\\\"token punctuation\\\">}</span><span class=\\\"token punctuation\\\">,</span>\\n <span class=\\\"token property\\\">\\\"difficulty\\\"</span><span class=\\\"token operator\\\">:</span> <span class=\\\"token string\\\">\\\"0x400\\\"</span><span class=\\\"token punctuation\\\">,</span>\\n <span class=\\\"token property\\\">\\\"gasLimit\\\"</span><span class=\\\"token operator\\\">:</span> <span class=\\\"token string\\\">\\\"0x8000000\\\"</span><span class=\\\"token punctuation\\\">,</span>\\n <span class=\\\"token property\\\">\\\"alloc\\\"</span><span class=\\\"token operator\\\">:</span> <span class=\\\"token punctuation\\\">{</span><span class=\\\"token punctuation\\\">}</span>\\n<span class=\\\"token punctuation\\\">}</span></code></pre>\\n </div>\\n<p>This is a fairly minimal genesis state file, as I’ve only defined the parameters that I need for my particular setup. I’ll give some explanation on what each parameter does:</p>\\n<ul>\\n<li>\\n<p><code class=\\\"language-text\\\">config</code>: An object containing the configuration of Ethereum. It doesn’t configure anything specific to the genesis block itself, but it defines how the Ethereum network will run.</p>\\n<ul>\\n<li><code class=\\\"language-text\\\">chainId</code>: Identifies which blockchain Ethereum will be using. There are a few different important chain IDs, like the ID of the mainnet’s blockchain (1). I chose a random 3 digit ID.</li>\\n<li><code class=\\\"language-text\\\">homesteadBlock: 0</code>: Ensures that we’ll be using the latest release of Ethereum, homestead.</li>\\n</ul>\\n</li>\\n<li><code class=\\\"language-text\\\">difficulty</code>: Determines how difficult it is for a block to be mined. The value is the reciprocal of the probability that mining the block succeeds (in hexadecimal). The value I chose (0x400, 1024 in denary) is fairly low, as quicker mining means that transactions can be committed to the blockchain with less latency.</li>\\n<li><code class=\\\"language-text\\\">gasLimit</code>: The maximum number of computations any following block can support (in hexadecimal), chosen as an arbitrarily high number that shouldn’t be reached.</li>\\n<li><code class=\\\"language-text\\\">alloc</code>: An object allowing you to allocate Ether to specific accounts. I didn’t pre-allocate any Ether, as my blockchain was set up to have no transaction fees and my transactions won’t contain any Ether.</li>\\n</ul>\\n<h3>Initializing the blockchain</h3>\\n<p>Once you have a genesis state file, setting up the blockchain is easy-peasy. I ran the following command in my working directory to create the blockchain based on my genesis state file in a directory named <code class=\\\"language-text\\\">blockchain</code>:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-bash\\\"><code class=\\\"language-bash\\\">geth -datadir ./blockchain init ./genesis.json</code></pre>\\n </div>\\n<p>If all goes well, the output looks something like this:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-bash\\\"><code class=\\\"language-bash\\\">INFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:12:14<span class=\\\"token punctuation\\\">]</span> Maximum peer count ETH<span class=\\\"token operator\\\">=</span>25 LES<span class=\\\"token operator\\\">=</span>0 total<span class=\\\"token operator\\\">=</span>25\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:12:14<span class=\\\"token punctuation\\\">]</span> Allocated cache and <span class=\\\"token function\\\">file</span> handles database<span class=\\\"token operator\\\">=</span><span class=\\\"token operator\\\"><</span>my-working-dir<span class=\\\"token operator\\\">></span>\\\\blockchain\\\\geth\\\\chaindata cache<span class=\\\"token operator\\\">=</span>16 handles<span class=\\\"token operator\\\">=</span>16\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:12:14<span class=\\\"token punctuation\\\">]</span> Writing custom genesis block\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:12:14<span class=\\\"token punctuation\\\">]</span> Persisted trie from memory database nodes<span class=\\\"token operator\\\">=</span>0 size<span class=\\\"token operator\\\">=</span>0.00B time<span class=\\\"token operator\\\">=</span>0s gcnodes<span class=\\\"token operator\\\">=</span>0 gcsize<span class=\\\"token operator\\\">=</span>0.00B gctime<span class=\\\"token operator\\\">=</span>0s livenodes<span class=\\\"token operator\\\">=</span>1 livesize<span class=\\\"token operator\\\">=</span>0.00B\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:12:14<span class=\\\"token punctuation\\\">]</span> Successfully wrote genesis state database<span class=\\\"token operator\\\">=</span>chaindata hash<span class=\\\"token operator\\\">=</span>d1a12d…4c8725\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:12:14<span class=\\\"token punctuation\\\">]</span> Allocated cache and <span class=\\\"token function\\\">file</span> handles database<span class=\\\"token operator\\\">=</span><span class=\\\"token operator\\\"><</span>my-working-dir<span class=\\\"token operator\\\">></span>\\\\blockchain\\\\geth\\\\lightchaindata cache<span class=\\\"token operator\\\">=</span>16 handles<span class=\\\"token operator\\\">=</span>16\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:12:15<span class=\\\"token punctuation\\\">]</span> Writing custom genesis block\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:12:15<span class=\\\"token punctuation\\\">]</span> Persisted trie from memory database nodes<span class=\\\"token operator\\\">=</span>0 size<span class=\\\"token operator\\\">=</span>0.00B time<span class=\\\"token operator\\\">=</span>0s gcnodes<span class=\\\"token operator\\\">=</span>0 gcsize<span class=\\\"token operator\\\">=</span>0.00B gctime<span class=\\\"token operator\\\">=</span>0s livenodes<span class=\\\"token operator\\\">=</span>1 livesize<span class=\\\"token operator\\\">=</span>0.00B\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:12:15<span class=\\\"token punctuation\\\">]</span> Successfully wrote genesis state database<span class=\\\"token operator\\\">=</span>lightchaindata hash<span class=\\\"token operator\\\">=</span>d1a12d…4c8725</code></pre>\\n </div>\\n<h3>Creating the network</h3>\\n<p>After creating the blockchain data, running a private network for the blockchain is simple thanks to Geth. Here’s the command I used to initialize my private Ethereum network (replacing <device name> with my device’s name):</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-bash\\\"><code class=\\\"language-bash\\\">geth --datadir ./blockchain --networkid 7314 --ws --wsorigins <span class=\\\"token string\\\">\\\"http://localhost:8080,http://<device name>\\\"</span> --wsapi<span class=\\\"token operator\\\">=</span><span class=\\\"token string\\\">\\\"eth,web3,personal,miner\\\"</span> --gasprice 0</code></pre>\\n </div>\\n<p>I’ll explain why I passed those specific parameters below (<a href=\\\"https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options\\\">see here for a full list of viable options</a>):</p>\\n<ul>\\n<li><code class=\\\"language-text\\\">datadir</code>: The directory containing the blockchain data (created by initializing the blockchain).</li>\\n<li><code class=\\\"language-text\\\">networkid</code>: Identifies the Ethereum network, I chose a random 4 digit ID to minimise clashing with other networks (<a href=\\\"https://ethereum.stackexchange.com/questions/17051/how-to-select-a-network-id-or-is-there-a-list-of-network-ids/17101#17101\\\">see here for a list of public network IDs</a>).</li>\\n<li><code class=\\\"language-text\\\">ws</code>: Enables the WS-RPC server, which allows (insecure) websocket connections to be opened with this server. I’ll be using websockets to communicate between the web-app and the blockchain as it can run easily in a browser. To use secure websockets, you’d need a proxy node or similar which responds to secure websocket connections and forwards requests, as Ethereum doesn’t provide support for secure websockets.</li>\\n<li><code class=\\\"language-text\\\">wsorigins</code>: A list of origins from which to accept websocket requests. I’ll be serving my web-app on port 8080, so requests from that port needed to be accepted or else the app wouldn’t be able to communicate with the network. My device’s name also needs to be accepted so that I can connect to the network through the Geth console. Passing <code class=\\\"language-text\\\">*</code> as a parameter will allow any connection, but this is less secure as it allows anyone access to the network.</li>\\n<li><code class=\\\"language-text\\\">wsapi</code>: A list of APIs to allow over websocket communication. I need <code class=\\\"language-text\\\">web3</code> to expose the Ethereum protocol to my web-app, <code class=\\\"language-text\\\">eth</code> to manage transactions, <code class=\\\"language-text\\\">personal</code> to manage accounts, and <code class=\\\"language-text\\\">miner</code> to mine transactions.</li>\\n<li><code class=\\\"language-text\\\">gasprice</code>: The minimum gas price of a transaction. Transaction fees are calculated as <code class=\\\"language-text\\\">gasprice * gaslimit</code> and paid in Ether. As I want no transaction fees on my transactions, I set the gas price to 0 to allow fee-free transactions to be sent/mined.</li>\\n</ul>\\n<p>With this, the private Ethereum network is running and transactions can be sent and mined to the blockchain! When running successfully, your ouput should look something like this:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-bash\\\"><code class=\\\"language-bash\\\">INFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Maximum peer count ETH<span class=\\\"token operator\\\">=</span>25 LES<span class=\\\"token operator\\\">=</span>0 total<span class=\\\"token operator\\\">=</span>25\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Starting peer-to-peer node instance<span class=\\\"token operator\\\">=</span>Geth/v1.8.10-stable-eae63c51/windows-amd64/go1.10.2\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Allocated cache and <span class=\\\"token function\\\">file</span> handles database<span class=\\\"token operator\\\">=</span><span class=\\\"token operator\\\"><</span>my-working-dir<span class=\\\"token operator\\\">></span>\\\\blockchain\\\\geth\\\\chaindata cache<span class=\\\"token operator\\\">=</span>768 handles<span class=\\\"token operator\\\">=</span>1024\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Initialised chain configuration config<span class=\\\"token operator\\\">=</span><span class=\\\"token string\\\">\\\"{ChainID: 731 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: <nil> EIP158: <nil> Byzantium: <nil> Constantinople: <nil> Engine: unknown}\\\"</span>\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Disk storage enabled <span class=\\\"token keyword\\\">for</span> ethash caches dir<span class=\\\"token operator\\\">=</span><span class=\\\"token operator\\\"><</span>my-working-dir<span class=\\\"token operator\\\">></span>\\\\blockchain\\\\geth\\\\ethash count<span class=\\\"token operator\\\">=</span>3\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Disk storage enabled <span class=\\\"token keyword\\\">for</span> ethash DAGs dir<span class=\\\"token operator\\\">=</span>C:\\\\AppData\\\\Ethash count<span class=\\\"token operator\\\">=</span>2\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Initialising Ethereum protocol versions<span class=\\\"token operator\\\">=</span><span class=\\\"token string\\\">\\\"[63 62]\\\"</span> network<span class=\\\"token operator\\\">=</span>7314\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Loaded <span class=\\\"token function\\\">most</span> recent local header number<span class=\\\"token operator\\\">=</span>0 hash<span class=\\\"token operator\\\">=</span>d1a12d…4c8725 td<span class=\\\"token operator\\\">=</span>1024\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Loaded <span class=\\\"token function\\\">most</span> recent local full block number<span class=\\\"token operator\\\">=</span>0 hash<span class=\\\"token operator\\\">=</span>d1a12d…4c8725 td<span class=\\\"token operator\\\">=</span>1024\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Loaded <span class=\\\"token function\\\">most</span> recent local fast block number<span class=\\\"token operator\\\">=</span>0 hash<span class=\\\"token operator\\\">=</span>d1a12d…4c8725 td<span class=\\\"token operator\\\">=</span>1024\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Regenerated local transaction journal transactions<span class=\\\"token operator\\\">=</span>0 accounts<span class=\\\"token operator\\\">=</span>0\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Starting P2P networking\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:57<span class=\\\"token punctuation\\\">]</span> UDP listener up self<span class=\\\"token operator\\\">=</span>enode://939c12fbde8d140e55800327c8b5fcd71a81a32647b95a89840580f67628cf4a650974af1c28588c1df476400cbaa6d514b23d0d14919f04ece8da52e6f7fcf5@<span class=\\\"token punctuation\\\">[</span>::<span class=\\\"token punctuation\\\">]</span>:30303\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:57<span class=\\\"token punctuation\\\">]</span> RLPx listener up self<span class=\\\"token operator\\\">=</span>enode://939c12fbde8d140e55800327c8b5fcd71a81a32647b95a89840580f67628cf4a650974af1c28588c1df476400cbaa6d514b23d0d14919f04ece8da52e6f7fcf5@<span class=\\\"token punctuation\\\">[</span>::<span class=\\\"token punctuation\\\">]</span>:30303\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:57<span class=\\\"token punctuation\\\">]</span> IPC endpoint opened url<span class=\\\"token operator\\\">=</span>\\\\\\\\.\\\\pipe\\\\geth.ipc\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:57<span class=\\\"token punctuation\\\">]</span> WebSocket endpoint opened url<span class=\\\"token operator\\\">=</span>ws://127.0.0.1:8546</code></pre>\\n </div>\\n<h3>Interacting with the network</h3>\\n<p>With the server running, the network should be accessible through Geth. You can attach a Geth console to the network with the following command:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-bash\\\"><code class=\\\"language-bash\\\">geth attach ws://localhost:8546</code></pre>\\n </div>\\n<p>This attaches to the network through the websocket served at port 8546 (the default port for WS-RPC) and provides a javascript console for interacting with it.</p>\\n<p>If you see the error message: <code class=\\\"language-text\\\">Fatal: Unable to attach to remote geth: bad status</code> accompanied with the following warning in the network output: <code class=\\\"language-text\\\">WARN [06-19|13:58:58] origin 'http://<device name>' not allowed on WS-RPC interface</code>, then you need to pass the given device name to the <code class=\\\"language-text\\\">-wsorigins</code> option when running your network.</p>\\n<p>Now that we have a console attached to the network, we’ll need to create an account before we can do certain things on the network (like mining and sending transactions). This can be done with the function <code class=\\\"language-text\\\">personal.newAccount()</code>, which prompts twice for a passphrase and then outputs the new account’s address (which acts like an ID for our purposes). The address you are allocated should look something like this: <code class=\\\"language-text\\\">0xedfc0cea37e9ed3bc7927a81f0dcd393bc57c0e6</code>.</p>\\n<h3>Sending and mining a transaction</h3>\\n<p>To test that the network is set up correctly, we’ll send a dummy transaction and mine it. First, your account needs to be unlocked so that you can send a transaction from it. Calling the function <code class=\\\"language-text\\\">personal.unlockAccount("<your account address>")</code> and entering the passphrase when prompted will unlock the given account, otherwise an error will be thrown.</p>\\n<p>Next, we’ll send a transaction. The function <code class=\\\"language-text\\\">ethereum.sendTransaction({from: "<address>"})</code> will send a transaction on the network from a given address. For the full details of what the configuration object can hold <a href=\\\"https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendtransaction\\\">see here</a>, but for now you only need to provide the required <code class=\\\"language-text\\\">from</code> field to send a basic transaction. The ouput from the function is the transaction hash (i.e. all of the transaction data hashed together).</p>\\n<p>After sending a transaction, you can check that it’s visible to the network by calling <code class=\\\"language-text\\\">eth.getBlock("pending")</code>, which outputs the <code class=\\\"language-text\\\">pending</code> block (a virtual block containing transactions which haven’t been committed to the blockchain). If the transactions list present in the block contains the dummy transaction’s hash, that means the transaction is waiting to be mined. You can mine the transaction by calling <code class=\\\"language-text\\\">miner.start(<number-of-threads>)</code> (but remember to call <code class=\\\"language-text\\\">miner.stop()</code> unless you want to be mining forever). While mining, the network output should look like this:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-bash\\\"><code class=\\\"language-bash\\\">INFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>14:34:39<span class=\\\"token punctuation\\\">]</span> Starting mining operation\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>14:34:39<span class=\\\"token punctuation\\\">]</span> Commit new mining work number<span class=\\\"token operator\\\">=</span>1 txs<span class=\\\"token operator\\\">=</span>1 uncles<span class=\\\"token operator\\\">=</span>0 elapsed<span class=\\\"token operator\\\">=</span>0s\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>14:34:43<span class=\\\"token punctuation\\\">]</span> Successfully sealed new block number<span class=\\\"token operator\\\">=</span>1 hash<span class=\\\"token operator\\\">=</span>0d5b6c…978671\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>14:34:43<span class=\\\"token punctuation\\\">]</span> 🔨 mined potential block number<span class=\\\"token operator\\\">=</span>1 hash<span class=\\\"token operator\\\">=</span>0d5b6c…978671\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>14:34:43<span class=\\\"token punctuation\\\">]</span> Commit new mining work number<span class=\\\"token operator\\\">=</span>2 txs<span class=\\\"token operator\\\">=</span>0 uncles<span class=\\\"token operator\\\">=</span>0 elapsed<span class=\\\"token operator\\\">=</span>0s\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>14:34:43<span class=\\\"token punctuation\\\">]</span> Successfully sealed new block number<span class=\\\"token operator\\\">=</span>2 hash<span class=\\\"token operator\\\">=</span>62f2bd…351578\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>14:34:43<span class=\\\"token punctuation\\\">]</span> 🔨 mined potential block number<span class=\\\"token operator\\\">=</span>2 hash<span class=\\\"token operator\\\">=</span>62f2bd…351578**</code></pre>\\n </div>\\n<p>After mining for a (very) short while, the transaction should be committed to the blockchain. You can check individual blocks with <code class=\\\"language-text\\\">eth.getBlock(<block-number>)</code> to find the transaction manually (it should be in block 1 if you didn’t mine until after sending the transaction), or you can loop through the possible block numbers to find any non-empty <code class=\\\"language-text\\\">transactions</code> array. If all has gone well, you should find the same transaction hash you saw before in one of the blocks’ <code class=\\\"language-text\\\">transactions</code> arrays. This means that your network is set up correctly!</p>\\n<h3>Creating a lazy miner</h3>\\n<p>The web-app we’ll be making will use transactions as a vehicle for data, so we’ll need to mine new blocks whenever transactions are sent or the data will never be stored on the blockchain. We don’t really want to run a miner non-stop as this is a waste of energy, so I’ve written the script <code class=\\\"language-text\\\">lazyMine.js</code> to subscribe to updates to the set of pending transactions and mine them when necessary:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-js\\\"><code class=\\\"language-js\\\"><span class=\\\"token keyword\\\">function</span> <span class=\\\"token function\\\">checkPendingTransactions</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token punctuation\\\">)</span> <span class=\\\"token punctuation\\\">{</span>\\n <span class=\\\"token keyword\\\">if</span> <span class=\\\"token punctuation\\\">(</span>eth<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">getBlock</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token string\\\">\\\"pending\\\"</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">.</span>transactions<span class=\\\"token punctuation\\\">.</span>length <span class=\\\"token operator\\\">></span> <span class=\\\"token number\\\">0</span><span class=\\\"token punctuation\\\">)</span> <span class=\\\"token punctuation\\\">{</span>\\n <span class=\\\"token keyword\\\">if</span> <span class=\\\"token punctuation\\\">(</span>eth<span class=\\\"token punctuation\\\">.</span>mining<span class=\\\"token punctuation\\\">)</span> <span class=\\\"token keyword\\\">return</span><span class=\\\"token punctuation\\\">;</span>\\n\\n console<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">log</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token string\\\">\\\"Mining pending transactions...\\\\n\\\"</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span>\\n miner<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">start</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token number\\\">1</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span>\\n <span class=\\\"token punctuation\\\">}</span> <span class=\\\"token keyword\\\">else</span> <span class=\\\"token punctuation\\\">{</span>\\n <span class=\\\"token keyword\\\">if</span> <span class=\\\"token punctuation\\\">(</span><span class=\\\"token operator\\\">!</span>eth<span class=\\\"token punctuation\\\">.</span>mining<span class=\\\"token punctuation\\\">)</span> <span class=\\\"token keyword\\\">return</span><span class=\\\"token punctuation\\\">;</span>\\n\\n miner<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">stop</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span>\\n console<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">log</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token string\\\">\\\"Mining stopped.\\\\n\\\"</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span>\\n <span class=\\\"token punctuation\\\">}</span>\\n<span class=\\\"token punctuation\\\">}</span>\\n\\neth<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">filter</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token string\\\">\\\"latest\\\"</span><span class=\\\"token punctuation\\\">,</span> checkPendingTransactions<span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span>\\neth<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">filter</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token string\\\">\\\"pending\\\"</span><span class=\\\"token punctuation\\\">,</span> checkPendingTransactions<span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span>\\n<span class=\\\"token function\\\">checkPendingTransactions</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span></code></pre>\\n </div>\\n<p>I’ll break down how this script works now:</p>\\n<ul>\\n<li>\\n<p>The function <code class=\\\"language-text\\\">checkPendingTransactions</code> checks if any transactions are pending, and then:</p>\\n<ul>\\n<li>If transactions are pending and we’re not already mining, it starts mining on 1 thread.</li>\\n<li>If no transactions are pending and we’re mining, it stops mining.</li>\\n</ul>\\n</li>\\n<li>\\n<p><code class=\\\"language-text\\\">eth.filter</code> causes the given function to be called on the specified block (<code class=\\\"language-text\\\">latest</code> is the latest block added to the blockchain, <code class=\\\"language-text\\\">pending</code> is the virtual block of non-committed transactions) when that block is updated.</p>\\n<ul>\\n<li>When the <code class=\\\"language-text\\\">latest</code> block updates it means that new transactions may have been mined, so we may have to stop mining if no pending transactions are left.</li>\\n<li>When the <code class=\\\"language-text\\\">pending</code> block updates it means that new transactions may have been sent, so we may have to start mining.</li>\\n</ul>\\n</li>\\n<li>We then call <code class=\\\"language-text\\\">checkPendingTransactions</code> to mine any non-committed transactions when the script is first run.</li>\\n</ul>\\n<p>We can run this script in a Geth console attached to the network using the following command:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-bash\\\"><code class=\\\"language-bash\\\">geth attach ws://localhost:8546 --preload ./lazyMine.js</code></pre>\\n </div>\\n<p>You can test if the script is working correctly with a similar method to how we tested that the network was set up correctly. I recommend leaving this script running in the background for now, as it won’t mine anything until transactions are actually sent.</p>\\n<p>Now that we have our blockchain and Ethereum network fully set up, it’s time to make the web-app that’ll communicate with it!</p>\\n<h2>Creating the web-app</h2>\\n<p>Since we’re interested in messaging here at MindLink, I’ll be building an MVC messaging app which uses the blockchain as a model. I won’t explain all of the nitty-gritty implementation details behind the app, but I’ll explain everything you should need to interact with the Ethereum network. If you want to see the full codebase for my web-app you can access it <a href=\\\"https://github.com/mindlink/sample-blockchain-messenger\\\">here</a>.</p>\\n<h3>Server and packaging</h3>\\n<p>I used <a href=\\\"https://webpack.js.org/\\\">webpack</a> to package my web-app, mainly as we use it for development of MindLink and so anything I create in webpack is potentially compatible with our systems. It also has some convenient features/plugins, like <a href=\\\"https://webpack.js.org/configuration/dev-server/\\\">webpack-dev-server</a>, which will help speed up development of the app.</p>\\n<h3>Web-app overview</h3>\\n<p>I’ve written a very simple messaging web-app, which allows messages to be represented by individual transactions on the blockchain. In order to stay focused on Ethereum, the web-app is a barebones html/javascript implementation with packages needed to run the server in webpack and nothing more. The application has the following features:</p>\\n<ul>\\n<li>It can unlock accounts with a given account’s address & passphrase</li>\\n<li>It subscribes to blockchain updates (i.e. new transactions)</li>\\n<li>It can list all of the accounts on the network</li>\\n<li>It displays new transactions as messages</li>\\n<li>It can send messages in the form of transactions</li>\\n</ul>\\n<p>When the web-app is first accessed, the user must provide their account’s credentials, which will be used to unlock the account (or create a new one). The account address will also be used as the <code class=\\\"language-text\\\">from</code> field when sending messages. <em>Make sure not to input any important credentials, as they are not encrypted at this stage!</em> After this, the user is greeted with a basic messaging application, containing a message list and a message input, as shown in the screenshot below.</p>\\n<p><img src=\\\"./messaging-app-screenshot.png\\\" alt=\\\"The messaging web-app\\\"></p>\\n<h3>Interacting with Ethereum in Javascript</h3>\\n<p>In order to access the Ethereum protocol in Javascript, I’m using <a href=\\\"https://github.com/ethereum/web3.js/tree/1.0\\\">Web3</a> - a package which provides a Javascript implementation for Ethereum. Thankfully it’s very easy to set it up! You can import the package through <code class=\\\"language-text\\\">npm</code> (or in my case, <code class=\\\"language-text\\\">yarn</code>) and then import and instantiate it wherever you need to use it, like so:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-js\\\"><code class=\\\"language-js\\\"><span class=\\\"token keyword\\\">import</span> Web3 <span class=\\\"token keyword\\\">from</span> <span class=\\\"token string\\\">\\\"web3\\\"</span><span class=\\\"token punctuation\\\">;</span>\\n\\n<span class=\\\"token keyword\\\">const</span> web3 <span class=\\\"token operator\\\">=</span> <span class=\\\"token keyword\\\">new</span> <span class=\\\"token class-name\\\">Web3</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token string\\\">\\\"ws://localhost:8546\\\"</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span></code></pre>\\n </div>\\n<p>This instantiates a new Web3 object, which you can use to interact with the Ethereum network through the given websocket. We can call methods on <code class=\\\"language-text\\\">web3</code> to interact with the network.</p>\\n<p>If you’re not using websockets, you can find out what arguments to pass the Web3 constructor <a href=\\\"https://web3js.readthedocs.io/en/1.0/include_package-core.html#providers\\\">here</a>. The reason I decided on using websockets is that my application code is run entirely on the client’s browser, which means that we can’t use IPC as it requires the NPM <code class=\\\"language-text\\\">net</code> package (which isn’t browser compatible).</p>\\n<h3>Ethereum account management with Web3</h3>\\n<p>In order to send transactions, we’ll need to unlock an account first (just like when we sent a transaction from the Geth console). To do this with Web3, you’ll need to obtain the user’s credentials and then pass them to the following function:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-js\\\"><code class=\\\"language-js\\\">web3<span class=\\\"token punctuation\\\">.</span>eth<span class=\\\"token punctuation\\\">.</span>personal<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">unlockAccount</span><span class=\\\"token punctuation\\\">(</span>address<span class=\\\"token punctuation\\\">,</span> passphrase<span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span></code></pre>\\n </div>\\n<p>This is an asynchronous function (as are any which require a response from the network), so if you need the result before continuing then make sure to use <code class=\\\"language-text\\\">await</code> beforehand. The function will throw an error if the wrong credentials are given, so you should ideally have some error handling in place if your credentials are input by the user.</p>\\n<p>If you don’t already have an account set up (or you just want to create a new account), you can do this with Web3 as well! First you’ll need to obtain a passphrase, and then you can pass it onto the following async function (which returns the new account address if successful):</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-js\\\"><code class=\\\"language-js\\\">web3<span class=\\\"token punctuation\\\">.</span>eth<span class=\\\"token punctuation\\\">.</span>personal<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">newAccount</span><span class=\\\"token punctuation\\\">(</span>passphrase<span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span></code></pre>\\n </div>\\n<p>It can also be useful to get a list of all accounts on the Ethereum network, allowing users to select addresses when needed instead of typing them (as they’re not human-readable). In my messaging web-app, I used this to allow the user to select the recipient of their message from a drop-down box. You can get a list of accounts in the Ethereum network with the following async function:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-js\\\"><code class=\\\"language-js\\\">web3<span class=\\\"token punctuation\\\">.</span>eth<span class=\\\"token punctuation\\\">.</span>personal<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">getAccounts</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span></code></pre>\\n </div>\\n<h3>Sending transactions with Web3</h3>\\n<p>Sending transactions with Web3 is quick and easy! First you’ll need to construct a transaction object, which contains the fields used by the transaction (<a href=\\\"https://web3js.readthedocs.io/en/1.0/web3-eth.html#sendtransaction\\\">see here for the full list of available fields</a>). The only required field is the <code class=\\\"language-text\\\">from</code> field, but I’ll go through all of the fields I used in my web-app to treat a transaction as a message-sending mechanism:</p>\\n<ul>\\n<li><code class=\\\"language-text\\\">from</code>: The address the transaction is sent from (must be unlocked). I use the account the user provided for this, and treat it as the message’s sender field.</li>\\n<li><code class=\\\"language-text\\\">to</code>: The address the transaction is sent to. I use this as the message’s recipient field, and obtain the value from a drop-down box (populated by all account addresses in the network) which the user can select.</li>\\n<li><code class=\\\"language-text\\\">value</code>: The amount of Ether transferred for the transaction. I set this value to 0 so that all messaging can be effectively free (and therefore unlimited).</li>\\n<li><code class=\\\"language-text\\\">data</code>: This field can take any hexadecimal string, which is great for sending additional data!</li>\\n</ul>\\n<p>To send my messages in the <code class=\\\"language-text\\\">data</code> field, I first create an object with a <code class=\\\"language-text\\\">message</code> field (which contains the message text). This is useful as if I wanted to send any other data along with the message text, I could simply add another field. I then use <code class=\\\"language-text\\\">JSON.stringify</code> to convert the object into a string, and then <code class=\\\"language-text\\\">web3.utils.asciiToHex</code> to convert the string to hexadecimal. This final value is what I pass in to the transaction’s <code class=\\\"language-text\\\">data</code> field.</p>\\n<p>Once you have your transaction object, you can send the transaction with the following asynchronous function:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-js\\\"><code class=\\\"language-js\\\">web3<span class=\\\"token punctuation\\\">.</span>eth<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">sendTransaction</span><span class=\\\"token punctuation\\\">(</span>transaction<span class=\\\"token punctuation\\\">)</span></code></pre>\\n </div>\\n<p>This returns the transaction hash upon completion. The transaction will have to be mined after this, so it won’t appear on the blockchain for a little while. If you still have the lazy miner running then it should appear quickly with no outside intervention. How convenient!</p>\\n<h3>Receiving transactions with Web3</h3>\\n<p>Receiving transactions requires a little more work than sending them. There’s no function to list all transactions, so instead we’ll have to make our own! In order to get all the transactions from the blockchain, we’ll have to iterate over each block and process the transactions array they contain. Here’s the method I use to process transactions (which I’ll explain below):</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-js\\\"><code class=\\\"language-js\\\"><span class=\\\"token keyword\\\">async</span> <span class=\\\"token keyword\\\">function</span> <span class=\\\"token function\\\">getMessages</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token punctuation\\\">)</span> <span class=\\\"token punctuation\\\">{</span>\\n <span class=\\\"token keyword\\\">let</span> startBlockNumber <span class=\\\"token operator\\\">=</span> lastBlockRead <span class=\\\"token operator\\\">+</span> <span class=\\\"token number\\\">1</span><span class=\\\"token punctuation\\\">;</span>\\n <span class=\\\"token keyword\\\">let</span> endBlockNumber <span class=\\\"token operator\\\">=</span> <span class=\\\"token keyword\\\">await</span> web3<span class=\\\"token punctuation\\\">.</span>eth<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">getBlockNumber</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span>\\n\\n <span class=\\\"token keyword\\\">for</span> <span class=\\\"token punctuation\\\">(</span><span class=\\\"token keyword\\\">var</span> i <span class=\\\"token operator\\\">=</span> startBlockNumber<span class=\\\"token punctuation\\\">;</span> i <span class=\\\"token operator\\\"><=</span> endBlockNumber<span class=\\\"token punctuation\\\">;</span> i<span class=\\\"token operator\\\">++</span><span class=\\\"token punctuation\\\">)</span> <span class=\\\"token punctuation\\\">{</span>\\n <span class=\\\"token keyword\\\">var</span> block <span class=\\\"token operator\\\">=</span> <span class=\\\"token keyword\\\">await</span> web3<span class=\\\"token punctuation\\\">.</span>eth<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">getBlock</span><span class=\\\"token punctuation\\\">(</span>i<span class=\\\"token punctuation\\\">,</span> <span class=\\\"token boolean\\\">true</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span>\\n <span class=\\\"token keyword\\\">if</span> <span class=\\\"token punctuation\\\">(</span>block <span class=\\\"token operator\\\">&&</span> block<span class=\\\"token punctuation\\\">.</span>transactions <span class=\\\"token operator\\\">!==</span> <span class=\\\"token keyword\\\">null</span><span class=\\\"token punctuation\\\">)</span> <span class=\\\"token punctuation\\\">{</span>\\n <span class=\\\"token keyword\\\">for</span> <span class=\\\"token punctuation\\\">(</span><span class=\\\"token keyword\\\">let</span> tx <span class=\\\"token keyword\\\">of</span> block<span class=\\\"token punctuation\\\">.</span>transactions<span class=\\\"token punctuation\\\">)</span> <span class=\\\"token punctuation\\\">{</span>\\n <span class=\\\"token function\\\">handleTransaction</span><span class=\\\"token punctuation\\\">(</span>tx<span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span>\\n <span class=\\\"token punctuation\\\">}</span>\\n lastBlockRead <span class=\\\"token operator\\\">=</span> block<span class=\\\"token punctuation\\\">.</span>number<span class=\\\"token punctuation\\\">;</span>\\n <span class=\\\"token punctuation\\\">}</span>\\n <span class=\\\"token punctuation\\\">}</span>\\n<span class=\\\"token punctuation\\\">}</span></code></pre>\\n </div>\\n<p>Firstly, the function checks which block number it should start from (using a variable <code class=\\\"language-text\\\">lastBlockRead</code>). This is useful as there’s no point re-processing blocks you’ve already processed, so it’s good to keep track of where you got up to. The variable is initialized at -1 (as the first block number is 0). The function then checks where it can read up to using <code class=\\\"language-text\\\">web3.eth.getBlockNumber</code>, which returns the highest block number on the blockchain. You could also read blocks up until a block is undefined, and use that as a loop termination condition.</p>\\n<p>The function then iterates over the blocks in the blockchain, getting each one with <code class=\\\"language-text\\\">web3.eth.getBlock(i, true)</code>. The first parameter to <code class=\\\"language-text\\\">getBlock</code> is the block number, and the second is a boolean which determines whether the transactions array should contain transaction objects (<code class=\\\"language-text\\\">true</code>) or hashes (<code class=\\\"language-text\\\">false</code>). We need the transaction objects to process the data they store, so we must pass in <code class=\\\"language-text\\\">true</code>. Once we have the block, the function simply loops through the available transactions and processes them.</p>\\n<p>This is great for manually processing all unread transactions, but what if we want to process transactions as they occur? For this, we’ll need to subscribe to certain blockchain events, so that this function is called whenever a new block is committed. To do this we can use the following asynchronous function:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-js\\\"><code class=\\\"language-js\\\">web3<span class=\\\"token punctuation\\\">.</span>eth<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">subscribe</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token string\\\">\\\"newBlockHeaders\\\"</span><span class=\\\"token punctuation\\\">,</span> callback<span class=\\\"token punctuation\\\">)</span></code></pre>\\n </div>\\n<p>This function takes a string and a callback function. The string defines what kind of events we are subscribing to - <code class=\\\"language-text\\\">"newBlockHeaders"</code> means the callback is called whenever new blocks are committed (<a href=\\\"https://web3js.readthedocs.io/en/1.0/web3-eth-subscribe.html\\\">see here for the list of event types</a>). The callback is called whenever an event of the given kind occurs, and is passed a block header object or an error. I pass <code class=\\\"language-text\\\">getMessages</code> as my callback function so that the list of messages updates whenever a new block is committed.</p>\\n<p>With that, you should have all you need to send and receive transactions from a web-app! If you’d like to see my blockchain-integrated web-app, <a href=\\\"https://github.com/mindlink/sample-blockchain-messenger\\\">click here</a>.</p>\\n<h2>Conclusions</h2>\\n<p>As you can see, once you have an Ethereum network and blockchain set up it’s surprisingly easy to interact with it from a web-app. Everything you need to use the blockchain as a model is provided through Web3, as you can commit and read back data in the form of transactions. This makes it quite easy to use and quick to set up.</p>\\n<p>When it comes to the efficacy of an Ethereum blockchain as a data models, there are some obvious problems. The main issue is latency, as any transactions being sent to the blockchain must be mined first, which is not particularly quick in comparison to more conventional data stores.</p>\\n<p>Another big issue I found was that all data on the blockchain is freely available to anyone with access to the network, and so any user (with or without an account) could read any other user’s data easily. While there are ways to work around this restriction, there are methods built into conventional data stores which handle this issue for you, so this makes using a blockchain as a store look much less appetizing.</p>\\n<p>I’ve yet to find any practical benefits to using the blockchain as a model. While there are unique systems like mining/transaction fees and smart contracts, I’m not sure that they provide any tangible benefit that couldn’t be replicated on other types of model.</p>\\n<p>Overall, while this was a worthwhile proof-of-concept to gain experience working with Ethereum, it seems like there aren’t many reasons to use the blockchain as a model in the first place.</p>\",\"frontmatter\":{\"title\":\"Creating a private Ethereum blockchain and using it as a model\",\"date\":\"June 06, 2018\",\"author\":{\"id\":\"Daniel Dean\",\"bio\":\"Software Engineer Intern at MindLink. Worth at least 70% of a regular employee!\",\"avatar\":{\"childImageSharp\":{\"resolutions\":{\"tracedSVG\":\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='60' height='60' viewBox='0 0 60 60' version='1'%3E%3Crect width='100%25' height='100%25' fill='%23f6f2f8'/%3E%3Cpath d='M16 2c0 9 1 15 7 21l3 4c0 2-1 4-3 4-5 0-13 4-17 7-3 4-3 4-4 11 0 11-2 11 29 11 30 0 28 0 28-11-1-6-1-7-4-11-4-3-12-7-17-7-2 0-3-2-3-4l3-4c6-6 7-12 7-21 0-2 0-2-14-2-15 0-15 0-15 2M0 51l1 3v-7l-1 4' fill='%23e0d6eb' fill-rule='evenodd'/%3E%3C/svg%3E\",\"width\":40,\"height\":40,\"src\":\"/static/default-788e71c2bb40c6a516ea06e9a06387eb-bad24.png\",\"srcSet\":\"/static/default-788e71c2bb40c6a516ea06e9a06387eb-bad24.png 1x\"}}}}}}},\"pathContext\":{\"slug\":\"creating-a-private-ethereum-blockchain-and-using-it-as-a-model\",\"previous\":{\"fields\":{\"slug\":\"custom-content-in-a-skype-for-business-conversation\"},\"frontmatter\":{\"title\":\"Custom content in a Skype for Business conversation\"}},\"next\":{\"fields\":{\"slug\":\"end-to-end-encryption-with-the-signal-protocol\"},\"frontmatter\":{\"title\":\"End-To-End Encryption with the Signal Protocol\"}}}}\n\n/***/ })\n\n});\n\n\n// WEBPACK FOOTER //\n// path---creating-a-private-ethereum-blockchain-and-using-it-as-a-model-09e64e0a368195b557bf.js","module.exports = {\"data\":{\"site\":{\"siteMetadata\":{\"title\":\"Engineering at MindLink\"}},\"markdownRemark\":{\"id\":\"/Applications/AzureDevOpsAgent/_work/13/s/src/pages/18-06-06-private-ethereum-blockchain-as-a-model/index.md absPath of file >>> MarkdownRemark\",\"html\":\"<p>Recently there’s been a lot of fascination surrounding blockchains, and how they can be used to improve long-standing industry technologies. Because of this, we’ve been investigating the applications of blockchains in messaging platforms. I’ve developed a blockchain-based messaging web-app which treats the blockchain as the main data model to gain some hands-on experience working with a blockchain.</p>\\n<p>In this post I’ll detail my process and rationale behind creating a private Ethereum blockchain and a web-app which interacts with it, with the hopes of guiding anyone who’d like to try it for themselves. No Ethereum experience necessary!</p>\\n<p><a href=\\\"https://github.com/mindlink/sample-blockchain-messenger\\\">You can check out my proof-of-concept web-app here</a>, which contains the finished web-app and a simple readme with instructions for running the Ethereum network.</p>\\n<h2>Creating a private Ethereum network</h2>\\n<p>The first thing that’s needed to start playing around with Ethereum is a way to leverage the Ethereum protocol, which gives access to the tools needed to interact with Ethereum networks. There are C++ and Python implementations of the protocol, but I’ll be using Geth (the Go implementation) as it can be interacted with through the CLI, making it quick and easy to use. Even though I’m using Geth, any implementation can be used as they should provide similar capabilities.</p>\\n<p>Before we can create the Ethereum network, we’ll first need to create its blockchain (starting with a genesis block).</p>\\n<h3>Creating the genesis block</h3>\\n<p>A blockchain is a data structure which builds upon itself over time, which means that it needs an initial structure to build upon - the genesis block! This block is defined by a genesis state file, which describes both the block’s parameters and some parameters which affect the operation of the Ethereum network.</p>\\n<p>I initialized the genesis block using a state file named <code class=\\\"language-text\\\">genesis.json</code> in my working directory. Here’s what my <code class=\\\"language-text\\\">genesis.json</code> file contains:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-json\\\"><code class=\\\"language-json\\\"><span class=\\\"token punctuation\\\">{</span>\\n <span class=\\\"token property\\\">\\\"config\\\"</span><span class=\\\"token operator\\\">:</span> <span class=\\\"token punctuation\\\">{</span> \\n <span class=\\\"token property\\\">\\\"chainId\\\"</span><span class=\\\"token operator\\\">:</span> <span class=\\\"token number\\\">731</span><span class=\\\"token punctuation\\\">,</span> \\n <span class=\\\"token property\\\">\\\"homesteadBlock\\\"</span><span class=\\\"token operator\\\">:</span> <span class=\\\"token number\\\">0</span>\\n <span class=\\\"token punctuation\\\">}</span><span class=\\\"token punctuation\\\">,</span>\\n <span class=\\\"token property\\\">\\\"difficulty\\\"</span><span class=\\\"token operator\\\">:</span> <span class=\\\"token string\\\">\\\"0x400\\\"</span><span class=\\\"token punctuation\\\">,</span>\\n <span class=\\\"token property\\\">\\\"gasLimit\\\"</span><span class=\\\"token operator\\\">:</span> <span class=\\\"token string\\\">\\\"0x8000000\\\"</span><span class=\\\"token punctuation\\\">,</span>\\n <span class=\\\"token property\\\">\\\"alloc\\\"</span><span class=\\\"token operator\\\">:</span> <span class=\\\"token punctuation\\\">{</span><span class=\\\"token punctuation\\\">}</span>\\n<span class=\\\"token punctuation\\\">}</span></code></pre>\\n </div>\\n<p>This is a fairly minimal genesis state file, as I’ve only defined the parameters that I need for my particular setup. I’ll give some explanation on what each parameter does:</p>\\n<ul>\\n<li>\\n<p><code class=\\\"language-text\\\">config</code>: An object containing the configuration of Ethereum. It doesn’t configure anything specific to the genesis block itself, but it defines how the Ethereum network will run.</p>\\n<ul>\\n<li><code class=\\\"language-text\\\">chainId</code>: Identifies which blockchain Ethereum will be using. There are a few different important chain IDs, like the ID of the mainnet’s blockchain (1). I chose a random 3 digit ID.</li>\\n<li><code class=\\\"language-text\\\">homesteadBlock: 0</code>: Ensures that we’ll be using the latest release of Ethereum, homestead.</li>\\n</ul>\\n</li>\\n<li><code class=\\\"language-text\\\">difficulty</code>: Determines how difficult it is for a block to be mined. The value is the reciprocal of the probability that mining the block succeeds (in hexadecimal). The value I chose (0x400, 1024 in denary) is fairly low, as quicker mining means that transactions can be committed to the blockchain with less latency.</li>\\n<li><code class=\\\"language-text\\\">gasLimit</code>: The maximum number of computations any following block can support (in hexadecimal), chosen as an arbitrarily high number that shouldn’t be reached.</li>\\n<li><code class=\\\"language-text\\\">alloc</code>: An object allowing you to allocate Ether to specific accounts. I didn’t pre-allocate any Ether, as my blockchain was set up to have no transaction fees and my transactions won’t contain any Ether.</li>\\n</ul>\\n<h3>Initializing the blockchain</h3>\\n<p>Once you have a genesis state file, setting up the blockchain is easy-peasy. I ran the following command in my working directory to create the blockchain based on my genesis state file in a directory named <code class=\\\"language-text\\\">blockchain</code>:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-bash\\\"><code class=\\\"language-bash\\\">geth -datadir ./blockchain init ./genesis.json</code></pre>\\n </div>\\n<p>If all goes well, the output looks something like this:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-bash\\\"><code class=\\\"language-bash\\\">INFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:12:14<span class=\\\"token punctuation\\\">]</span> Maximum peer count ETH<span class=\\\"token operator\\\">=</span>25 LES<span class=\\\"token operator\\\">=</span>0 total<span class=\\\"token operator\\\">=</span>25\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:12:14<span class=\\\"token punctuation\\\">]</span> Allocated cache and <span class=\\\"token function\\\">file</span> handles database<span class=\\\"token operator\\\">=</span><span class=\\\"token operator\\\"><</span>my-working-dir<span class=\\\"token operator\\\">></span>\\\\blockchain\\\\geth\\\\chaindata cache<span class=\\\"token operator\\\">=</span>16 handles<span class=\\\"token operator\\\">=</span>16\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:12:14<span class=\\\"token punctuation\\\">]</span> Writing custom genesis block\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:12:14<span class=\\\"token punctuation\\\">]</span> Persisted trie from memory database nodes<span class=\\\"token operator\\\">=</span>0 size<span class=\\\"token operator\\\">=</span>0.00B time<span class=\\\"token operator\\\">=</span>0s gcnodes<span class=\\\"token operator\\\">=</span>0 gcsize<span class=\\\"token operator\\\">=</span>0.00B gctime<span class=\\\"token operator\\\">=</span>0s livenodes<span class=\\\"token operator\\\">=</span>1 livesize<span class=\\\"token operator\\\">=</span>0.00B\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:12:14<span class=\\\"token punctuation\\\">]</span> Successfully wrote genesis state database<span class=\\\"token operator\\\">=</span>chaindata hash<span class=\\\"token operator\\\">=</span>d1a12d…4c8725\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:12:14<span class=\\\"token punctuation\\\">]</span> Allocated cache and <span class=\\\"token function\\\">file</span> handles database<span class=\\\"token operator\\\">=</span><span class=\\\"token operator\\\"><</span>my-working-dir<span class=\\\"token operator\\\">></span>\\\\blockchain\\\\geth\\\\lightchaindata cache<span class=\\\"token operator\\\">=</span>16 handles<span class=\\\"token operator\\\">=</span>16\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:12:15<span class=\\\"token punctuation\\\">]</span> Writing custom genesis block\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:12:15<span class=\\\"token punctuation\\\">]</span> Persisted trie from memory database nodes<span class=\\\"token operator\\\">=</span>0 size<span class=\\\"token operator\\\">=</span>0.00B time<span class=\\\"token operator\\\">=</span>0s gcnodes<span class=\\\"token operator\\\">=</span>0 gcsize<span class=\\\"token operator\\\">=</span>0.00B gctime<span class=\\\"token operator\\\">=</span>0s livenodes<span class=\\\"token operator\\\">=</span>1 livesize<span class=\\\"token operator\\\">=</span>0.00B\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:12:15<span class=\\\"token punctuation\\\">]</span> Successfully wrote genesis state database<span class=\\\"token operator\\\">=</span>lightchaindata hash<span class=\\\"token operator\\\">=</span>d1a12d…4c8725</code></pre>\\n </div>\\n<h3>Creating the network</h3>\\n<p>After creating the blockchain data, running a private network for the blockchain is simple thanks to Geth. Here’s the command I used to initialize my private Ethereum network (replacing <device name> with my device’s name):</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-bash\\\"><code class=\\\"language-bash\\\">geth --datadir ./blockchain --networkid 7314 --ws --wsorigins <span class=\\\"token string\\\">\\\"http://localhost:8080,http://<device name>\\\"</span> --wsapi<span class=\\\"token operator\\\">=</span><span class=\\\"token string\\\">\\\"eth,web3,personal,miner\\\"</span> --gasprice 0</code></pre>\\n </div>\\n<p>I’ll explain why I passed those specific parameters below (<a href=\\\"https://github.com/ethereum/go-ethereum/wiki/Command-Line-Options\\\">see here for a full list of viable options</a>):</p>\\n<ul>\\n<li><code class=\\\"language-text\\\">datadir</code>: The directory containing the blockchain data (created by initializing the blockchain).</li>\\n<li><code class=\\\"language-text\\\">networkid</code>: Identifies the Ethereum network, I chose a random 4 digit ID to minimise clashing with other networks (<a href=\\\"https://ethereum.stackexchange.com/questions/17051/how-to-select-a-network-id-or-is-there-a-list-of-network-ids/17101#17101\\\">see here for a list of public network IDs</a>).</li>\\n<li><code class=\\\"language-text\\\">ws</code>: Enables the WS-RPC server, which allows (insecure) websocket connections to be opened with this server. I’ll be using websockets to communicate between the web-app and the blockchain as it can run easily in a browser. To use secure websockets, you’d need a proxy node or similar which responds to secure websocket connections and forwards requests, as Ethereum doesn’t provide support for secure websockets.</li>\\n<li><code class=\\\"language-text\\\">wsorigins</code>: A list of origins from which to accept websocket requests. I’ll be serving my web-app on port 8080, so requests from that port needed to be accepted or else the app wouldn’t be able to communicate with the network. My device’s name also needs to be accepted so that I can connect to the network through the Geth console. Passing <code class=\\\"language-text\\\">*</code> as a parameter will allow any connection, but this is less secure as it allows anyone access to the network.</li>\\n<li><code class=\\\"language-text\\\">wsapi</code>: A list of APIs to allow over websocket communication. I need <code class=\\\"language-text\\\">web3</code> to expose the Ethereum protocol to my web-app, <code class=\\\"language-text\\\">eth</code> to manage transactions, <code class=\\\"language-text\\\">personal</code> to manage accounts, and <code class=\\\"language-text\\\">miner</code> to mine transactions.</li>\\n<li><code class=\\\"language-text\\\">gasprice</code>: The minimum gas price of a transaction. Transaction fees are calculated as <code class=\\\"language-text\\\">gasprice * gaslimit</code> and paid in Ether. As I want no transaction fees on my transactions, I set the gas price to 0 to allow fee-free transactions to be sent/mined.</li>\\n</ul>\\n<p>With this, the private Ethereum network is running and transactions can be sent and mined to the blockchain! When running successfully, your ouput should look something like this:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-bash\\\"><code class=\\\"language-bash\\\">INFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Maximum peer count ETH<span class=\\\"token operator\\\">=</span>25 LES<span class=\\\"token operator\\\">=</span>0 total<span class=\\\"token operator\\\">=</span>25\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Starting peer-to-peer node instance<span class=\\\"token operator\\\">=</span>Geth/v1.8.10-stable-eae63c51/windows-amd64/go1.10.2\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Allocated cache and <span class=\\\"token function\\\">file</span> handles database<span class=\\\"token operator\\\">=</span><span class=\\\"token operator\\\"><</span>my-working-dir<span class=\\\"token operator\\\">></span>\\\\blockchain\\\\geth\\\\chaindata cache<span class=\\\"token operator\\\">=</span>768 handles<span class=\\\"token operator\\\">=</span>1024\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Initialised chain configuration config<span class=\\\"token operator\\\">=</span><span class=\\\"token string\\\">\\\"{ChainID: 731 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: <nil> EIP158: <nil> Byzantium: <nil> Constantinople: <nil> Engine: unknown}\\\"</span>\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Disk storage enabled <span class=\\\"token keyword\\\">for</span> ethash caches dir<span class=\\\"token operator\\\">=</span><span class=\\\"token operator\\\"><</span>my-working-dir<span class=\\\"token operator\\\">></span>\\\\blockchain\\\\geth\\\\ethash count<span class=\\\"token operator\\\">=</span>3\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Disk storage enabled <span class=\\\"token keyword\\\">for</span> ethash DAGs dir<span class=\\\"token operator\\\">=</span>C:\\\\AppData\\\\Ethash count<span class=\\\"token operator\\\">=</span>2\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Initialising Ethereum protocol versions<span class=\\\"token operator\\\">=</span><span class=\\\"token string\\\">\\\"[63 62]\\\"</span> network<span class=\\\"token operator\\\">=</span>7314\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Loaded <span class=\\\"token function\\\">most</span> recent local header number<span class=\\\"token operator\\\">=</span>0 hash<span class=\\\"token operator\\\">=</span>d1a12d…4c8725 td<span class=\\\"token operator\\\">=</span>1024\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Loaded <span class=\\\"token function\\\">most</span> recent local full block number<span class=\\\"token operator\\\">=</span>0 hash<span class=\\\"token operator\\\">=</span>d1a12d…4c8725 td<span class=\\\"token operator\\\">=</span>1024\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Loaded <span class=\\\"token function\\\">most</span> recent local fast block number<span class=\\\"token operator\\\">=</span>0 hash<span class=\\\"token operator\\\">=</span>d1a12d…4c8725 td<span class=\\\"token operator\\\">=</span>1024\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Regenerated local transaction journal transactions<span class=\\\"token operator\\\">=</span>0 accounts<span class=\\\"token operator\\\">=</span>0\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:55<span class=\\\"token punctuation\\\">]</span> Starting P2P networking\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:57<span class=\\\"token punctuation\\\">]</span> UDP listener up self<span class=\\\"token operator\\\">=</span>enode://939c12fbde8d140e55800327c8b5fcd71a81a32647b95a89840580f67628cf4a650974af1c28588c1df476400cbaa6d514b23d0d14919f04ece8da52e6f7fcf5@<span class=\\\"token punctuation\\\">[</span>::<span class=\\\"token punctuation\\\">]</span>:30303\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:57<span class=\\\"token punctuation\\\">]</span> RLPx listener up self<span class=\\\"token operator\\\">=</span>enode://939c12fbde8d140e55800327c8b5fcd71a81a32647b95a89840580f67628cf4a650974af1c28588c1df476400cbaa6d514b23d0d14919f04ece8da52e6f7fcf5@<span class=\\\"token punctuation\\\">[</span>::<span class=\\\"token punctuation\\\">]</span>:30303\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:57<span class=\\\"token punctuation\\\">]</span> IPC endpoint opened url<span class=\\\"token operator\\\">=</span>\\\\\\\\.\\\\pipe\\\\geth.ipc\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>13:45:57<span class=\\\"token punctuation\\\">]</span> WebSocket endpoint opened url<span class=\\\"token operator\\\">=</span>ws://127.0.0.1:8546</code></pre>\\n </div>\\n<h3>Interacting with the network</h3>\\n<p>With the server running, the network should be accessible through Geth. You can attach a Geth console to the network with the following command:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-bash\\\"><code class=\\\"language-bash\\\">geth attach ws://localhost:8546</code></pre>\\n </div>\\n<p>This attaches to the network through the websocket served at port 8546 (the default port for WS-RPC) and provides a javascript console for interacting with it.</p>\\n<p>If you see the error message: <code class=\\\"language-text\\\">Fatal: Unable to attach to remote geth: bad status</code> accompanied with the following warning in the network output: <code class=\\\"language-text\\\">WARN [06-19|13:58:58] origin 'http://<device name>' not allowed on WS-RPC interface</code>, then you need to pass the given device name to the <code class=\\\"language-text\\\">-wsorigins</code> option when running your network.</p>\\n<p>Now that we have a console attached to the network, we’ll need to create an account before we can do certain things on the network (like mining and sending transactions). This can be done with the function <code class=\\\"language-text\\\">personal.newAccount()</code>, which prompts twice for a passphrase and then outputs the new account’s address (which acts like an ID for our purposes). The address you are allocated should look something like this: <code class=\\\"language-text\\\">0xedfc0cea37e9ed3bc7927a81f0dcd393bc57c0e6</code>.</p>\\n<h3>Sending and mining a transaction</h3>\\n<p>To test that the network is set up correctly, we’ll send a dummy transaction and mine it. First, your account needs to be unlocked so that you can send a transaction from it. Calling the function <code class=\\\"language-text\\\">personal.unlockAccount("<your account address>")</code> and entering the passphrase when prompted will unlock the given account, otherwise an error will be thrown.</p>\\n<p>Next, we’ll send a transaction. The function <code class=\\\"language-text\\\">ethereum.sendTransaction({from: "<address>"})</code> will send a transaction on the network from a given address. For the full details of what the configuration object can hold <a href=\\\"https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendtransaction\\\">see here</a>, but for now you only need to provide the required <code class=\\\"language-text\\\">from</code> field to send a basic transaction. The ouput from the function is the transaction hash (i.e. all of the transaction data hashed together).</p>\\n<p>After sending a transaction, you can check that it’s visible to the network by calling <code class=\\\"language-text\\\">eth.getBlock("pending")</code>, which outputs the <code class=\\\"language-text\\\">pending</code> block (a virtual block containing transactions which haven’t been committed to the blockchain). If the transactions list present in the block contains the dummy transaction’s hash, that means the transaction is waiting to be mined. You can mine the transaction by calling <code class=\\\"language-text\\\">miner.start(<number-of-threads>)</code> (but remember to call <code class=\\\"language-text\\\">miner.stop()</code> unless you want to be mining forever). While mining, the network output should look like this:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-bash\\\"><code class=\\\"language-bash\\\">INFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>14:34:39<span class=\\\"token punctuation\\\">]</span> Starting mining operation\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>14:34:39<span class=\\\"token punctuation\\\">]</span> Commit new mining work number<span class=\\\"token operator\\\">=</span>1 txs<span class=\\\"token operator\\\">=</span>1 uncles<span class=\\\"token operator\\\">=</span>0 elapsed<span class=\\\"token operator\\\">=</span>0s\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>14:34:43<span class=\\\"token punctuation\\\">]</span> Successfully sealed new block number<span class=\\\"token operator\\\">=</span>1 hash<span class=\\\"token operator\\\">=</span>0d5b6c…978671\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>14:34:43<span class=\\\"token punctuation\\\">]</span> 🔨 mined potential block number<span class=\\\"token operator\\\">=</span>1 hash<span class=\\\"token operator\\\">=</span>0d5b6c…978671\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>14:34:43<span class=\\\"token punctuation\\\">]</span> Commit new mining work number<span class=\\\"token operator\\\">=</span>2 txs<span class=\\\"token operator\\\">=</span>0 uncles<span class=\\\"token operator\\\">=</span>0 elapsed<span class=\\\"token operator\\\">=</span>0s\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>14:34:43<span class=\\\"token punctuation\\\">]</span> Successfully sealed new block number<span class=\\\"token operator\\\">=</span>2 hash<span class=\\\"token operator\\\">=</span>62f2bd…351578\\nINFO <span class=\\\"token punctuation\\\">[</span>06-19<span class=\\\"token operator\\\">|</span>14:34:43<span class=\\\"token punctuation\\\">]</span> 🔨 mined potential block number<span class=\\\"token operator\\\">=</span>2 hash<span class=\\\"token operator\\\">=</span>62f2bd…351578**</code></pre>\\n </div>\\n<p>After mining for a (very) short while, the transaction should be committed to the blockchain. You can check individual blocks with <code class=\\\"language-text\\\">eth.getBlock(<block-number>)</code> to find the transaction manually (it should be in block 1 if you didn’t mine until after sending the transaction), or you can loop through the possible block numbers to find any non-empty <code class=\\\"language-text\\\">transactions</code> array. If all has gone well, you should find the same transaction hash you saw before in one of the blocks’ <code class=\\\"language-text\\\">transactions</code> arrays. This means that your network is set up correctly!</p>\\n<h3>Creating a lazy miner</h3>\\n<p>The web-app we’ll be making will use transactions as a vehicle for data, so we’ll need to mine new blocks whenever transactions are sent or the data will never be stored on the blockchain. We don’t really want to run a miner non-stop as this is a waste of energy, so I’ve written the script <code class=\\\"language-text\\\">lazyMine.js</code> to subscribe to updates to the set of pending transactions and mine them when necessary:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-js\\\"><code class=\\\"language-js\\\"><span class=\\\"token keyword\\\">function</span> <span class=\\\"token function\\\">checkPendingTransactions</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token punctuation\\\">)</span> <span class=\\\"token punctuation\\\">{</span>\\n <span class=\\\"token keyword\\\">if</span> <span class=\\\"token punctuation\\\">(</span>eth<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">getBlock</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token string\\\">\\\"pending\\\"</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">.</span>transactions<span class=\\\"token punctuation\\\">.</span>length <span class=\\\"token operator\\\">></span> <span class=\\\"token number\\\">0</span><span class=\\\"token punctuation\\\">)</span> <span class=\\\"token punctuation\\\">{</span>\\n <span class=\\\"token keyword\\\">if</span> <span class=\\\"token punctuation\\\">(</span>eth<span class=\\\"token punctuation\\\">.</span>mining<span class=\\\"token punctuation\\\">)</span> <span class=\\\"token keyword\\\">return</span><span class=\\\"token punctuation\\\">;</span>\\n\\n console<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">log</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token string\\\">\\\"Mining pending transactions...\\\\n\\\"</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span>\\n miner<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">start</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token number\\\">1</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span>\\n <span class=\\\"token punctuation\\\">}</span> <span class=\\\"token keyword\\\">else</span> <span class=\\\"token punctuation\\\">{</span>\\n <span class=\\\"token keyword\\\">if</span> <span class=\\\"token punctuation\\\">(</span><span class=\\\"token operator\\\">!</span>eth<span class=\\\"token punctuation\\\">.</span>mining<span class=\\\"token punctuation\\\">)</span> <span class=\\\"token keyword\\\">return</span><span class=\\\"token punctuation\\\">;</span>\\n\\n miner<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">stop</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span>\\n console<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">log</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token string\\\">\\\"Mining stopped.\\\\n\\\"</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span>\\n <span class=\\\"token punctuation\\\">}</span>\\n<span class=\\\"token punctuation\\\">}</span>\\n\\neth<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">filter</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token string\\\">\\\"latest\\\"</span><span class=\\\"token punctuation\\\">,</span> checkPendingTransactions<span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span>\\neth<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">filter</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token string\\\">\\\"pending\\\"</span><span class=\\\"token punctuation\\\">,</span> checkPendingTransactions<span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span>\\n<span class=\\\"token function\\\">checkPendingTransactions</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span></code></pre>\\n </div>\\n<p>I’ll break down how this script works now:</p>\\n<ul>\\n<li>\\n<p>The function <code class=\\\"language-text\\\">checkPendingTransactions</code> checks if any transactions are pending, and then:</p>\\n<ul>\\n<li>If transactions are pending and we’re not already mining, it starts mining on 1 thread.</li>\\n<li>If no transactions are pending and we’re mining, it stops mining.</li>\\n</ul>\\n</li>\\n<li>\\n<p><code class=\\\"language-text\\\">eth.filter</code> causes the given function to be called on the specified block (<code class=\\\"language-text\\\">latest</code> is the latest block added to the blockchain, <code class=\\\"language-text\\\">pending</code> is the virtual block of non-committed transactions) when that block is updated.</p>\\n<ul>\\n<li>When the <code class=\\\"language-text\\\">latest</code> block updates it means that new transactions may have been mined, so we may have to stop mining if no pending transactions are left.</li>\\n<li>When the <code class=\\\"language-text\\\">pending</code> block updates it means that new transactions may have been sent, so we may have to start mining.</li>\\n</ul>\\n</li>\\n<li>We then call <code class=\\\"language-text\\\">checkPendingTransactions</code> to mine any non-committed transactions when the script is first run.</li>\\n</ul>\\n<p>We can run this script in a Geth console attached to the network using the following command:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-bash\\\"><code class=\\\"language-bash\\\">geth attach ws://localhost:8546 --preload ./lazyMine.js</code></pre>\\n </div>\\n<p>You can test if the script is working correctly with a similar method to how we tested that the network was set up correctly. I recommend leaving this script running in the background for now, as it won’t mine anything until transactions are actually sent.</p>\\n<p>Now that we have our blockchain and Ethereum network fully set up, it’s time to make the web-app that’ll communicate with it!</p>\\n<h2>Creating the web-app</h2>\\n<p>Since we’re interested in messaging here at MindLink, I’ll be building an MVC messaging app which uses the blockchain as a model. I won’t explain all of the nitty-gritty implementation details behind the app, but I’ll explain everything you should need to interact with the Ethereum network. If you want to see the full codebase for my web-app you can access it <a href=\\\"https://github.com/mindlink/sample-blockchain-messenger\\\">here</a>.</p>\\n<h3>Server and packaging</h3>\\n<p>I used <a href=\\\"https://webpack.js.org/\\\">webpack</a> to package my web-app, mainly as we use it for development of MindLink and so anything I create in webpack is potentially compatible with our systems. It also has some convenient features/plugins, like <a href=\\\"https://webpack.js.org/configuration/dev-server/\\\">webpack-dev-server</a>, which will help speed up development of the app.</p>\\n<h3>Web-app overview</h3>\\n<p>I’ve written a very simple messaging web-app, which allows messages to be represented by individual transactions on the blockchain. In order to stay focused on Ethereum, the web-app is a barebones html/javascript implementation with packages needed to run the server in webpack and nothing more. The application has the following features:</p>\\n<ul>\\n<li>It can unlock accounts with a given account’s address & passphrase</li>\\n<li>It subscribes to blockchain updates (i.e. new transactions)</li>\\n<li>It can list all of the accounts on the network</li>\\n<li>It displays new transactions as messages</li>\\n<li>It can send messages in the form of transactions</li>\\n</ul>\\n<p>When the web-app is first accessed, the user must provide their account’s credentials, which will be used to unlock the account (or create a new one). The account address will also be used as the <code class=\\\"language-text\\\">from</code> field when sending messages. <em>Make sure not to input any important credentials, as they are not encrypted at this stage!</em> After this, the user is greeted with a basic messaging application, containing a message list and a message input, as shown in the screenshot below.</p>\\n<p><img src=\\\"./messaging-app-screenshot.png\\\" alt=\\\"The messaging web-app\\\"></p>\\n<h3>Interacting with Ethereum in Javascript</h3>\\n<p>In order to access the Ethereum protocol in Javascript, I’m using <a href=\\\"https://github.com/ethereum/web3.js/tree/1.0\\\">Web3</a> - a package which provides a Javascript implementation for Ethereum. Thankfully it’s very easy to set it up! You can import the package through <code class=\\\"language-text\\\">npm</code> (or in my case, <code class=\\\"language-text\\\">yarn</code>) and then import and instantiate it wherever you need to use it, like so:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-js\\\"><code class=\\\"language-js\\\"><span class=\\\"token keyword\\\">import</span> Web3 <span class=\\\"token keyword\\\">from</span> <span class=\\\"token string\\\">\\\"web3\\\"</span><span class=\\\"token punctuation\\\">;</span>\\n\\n<span class=\\\"token keyword\\\">const</span> web3 <span class=\\\"token operator\\\">=</span> <span class=\\\"token keyword\\\">new</span> <span class=\\\"token class-name\\\">Web3</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token string\\\">\\\"ws://localhost:8546\\\"</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span></code></pre>\\n </div>\\n<p>This instantiates a new Web3 object, which you can use to interact with the Ethereum network through the given websocket. We can call methods on <code class=\\\"language-text\\\">web3</code> to interact with the network.</p>\\n<p>If you’re not using websockets, you can find out what arguments to pass the Web3 constructor <a href=\\\"https://web3js.readthedocs.io/en/1.0/include_package-core.html#providers\\\">here</a>. The reason I decided on using websockets is that my application code is run entirely on the client’s browser, which means that we can’t use IPC as it requires the NPM <code class=\\\"language-text\\\">net</code> package (which isn’t browser compatible).</p>\\n<h3>Ethereum account management with Web3</h3>\\n<p>In order to send transactions, we’ll need to unlock an account first (just like when we sent a transaction from the Geth console). To do this with Web3, you’ll need to obtain the user’s credentials and then pass them to the following function:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-js\\\"><code class=\\\"language-js\\\">web3<span class=\\\"token punctuation\\\">.</span>eth<span class=\\\"token punctuation\\\">.</span>personal<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">unlockAccount</span><span class=\\\"token punctuation\\\">(</span>address<span class=\\\"token punctuation\\\">,</span> passphrase<span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span></code></pre>\\n </div>\\n<p>This is an asynchronous function (as are any which require a response from the network), so if you need the result before continuing then make sure to use <code class=\\\"language-text\\\">await</code> beforehand. The function will throw an error if the wrong credentials are given, so you should ideally have some error handling in place if your credentials are input by the user.</p>\\n<p>If you don’t already have an account set up (or you just want to create a new account), you can do this with Web3 as well! First you’ll need to obtain a passphrase, and then you can pass it onto the following async function (which returns the new account address if successful):</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-js\\\"><code class=\\\"language-js\\\">web3<span class=\\\"token punctuation\\\">.</span>eth<span class=\\\"token punctuation\\\">.</span>personal<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">newAccount</span><span class=\\\"token punctuation\\\">(</span>passphrase<span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span></code></pre>\\n </div>\\n<p>It can also be useful to get a list of all accounts on the Ethereum network, allowing users to select addresses when needed instead of typing them (as they’re not human-readable). In my messaging web-app, I used this to allow the user to select the recipient of their message from a drop-down box. You can get a list of accounts in the Ethereum network with the following async function:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-js\\\"><code class=\\\"language-js\\\">web3<span class=\\\"token punctuation\\\">.</span>eth<span class=\\\"token punctuation\\\">.</span>personal<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">getAccounts</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span></code></pre>\\n </div>\\n<h3>Sending transactions with Web3</h3>\\n<p>Sending transactions with Web3 is quick and easy! First you’ll need to construct a transaction object, which contains the fields used by the transaction (<a href=\\\"https://web3js.readthedocs.io/en/1.0/web3-eth.html#sendtransaction\\\">see here for the full list of available fields</a>). The only required field is the <code class=\\\"language-text\\\">from</code> field, but I’ll go through all of the fields I used in my web-app to treat a transaction as a message-sending mechanism:</p>\\n<ul>\\n<li><code class=\\\"language-text\\\">from</code>: The address the transaction is sent from (must be unlocked). I use the account the user provided for this, and treat it as the message’s sender field.</li>\\n<li><code class=\\\"language-text\\\">to</code>: The address the transaction is sent to. I use this as the message’s recipient field, and obtain the value from a drop-down box (populated by all account addresses in the network) which the user can select.</li>\\n<li><code class=\\\"language-text\\\">value</code>: The amount of Ether transferred for the transaction. I set this value to 0 so that all messaging can be effectively free (and therefore unlimited).</li>\\n<li><code class=\\\"language-text\\\">data</code>: This field can take any hexadecimal string, which is great for sending additional data!</li>\\n</ul>\\n<p>To send my messages in the <code class=\\\"language-text\\\">data</code> field, I first create an object with a <code class=\\\"language-text\\\">message</code> field (which contains the message text). This is useful as if I wanted to send any other data along with the message text, I could simply add another field. I then use <code class=\\\"language-text\\\">JSON.stringify</code> to convert the object into a string, and then <code class=\\\"language-text\\\">web3.utils.asciiToHex</code> to convert the string to hexadecimal. This final value is what I pass in to the transaction’s <code class=\\\"language-text\\\">data</code> field.</p>\\n<p>Once you have your transaction object, you can send the transaction with the following asynchronous function:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-js\\\"><code class=\\\"language-js\\\">web3<span class=\\\"token punctuation\\\">.</span>eth<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">sendTransaction</span><span class=\\\"token punctuation\\\">(</span>transaction<span class=\\\"token punctuation\\\">)</span></code></pre>\\n </div>\\n<p>This returns the transaction hash upon completion. The transaction will have to be mined after this, so it won’t appear on the blockchain for a little while. If you still have the lazy miner running then it should appear quickly with no outside intervention. How convenient!</p>\\n<h3>Receiving transactions with Web3</h3>\\n<p>Receiving transactions requires a little more work than sending them. There’s no function to list all transactions, so instead we’ll have to make our own! In order to get all the transactions from the blockchain, we’ll have to iterate over each block and process the transactions array they contain. Here’s the method I use to process transactions (which I’ll explain below):</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-js\\\"><code class=\\\"language-js\\\"><span class=\\\"token keyword\\\">async</span> <span class=\\\"token keyword\\\">function</span> <span class=\\\"token function\\\">getMessages</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token punctuation\\\">)</span> <span class=\\\"token punctuation\\\">{</span>\\n <span class=\\\"token keyword\\\">let</span> startBlockNumber <span class=\\\"token operator\\\">=</span> lastBlockRead <span class=\\\"token operator\\\">+</span> <span class=\\\"token number\\\">1</span><span class=\\\"token punctuation\\\">;</span>\\n <span class=\\\"token keyword\\\">let</span> endBlockNumber <span class=\\\"token operator\\\">=</span> <span class=\\\"token keyword\\\">await</span> web3<span class=\\\"token punctuation\\\">.</span>eth<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">getBlockNumber</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span>\\n\\n <span class=\\\"token keyword\\\">for</span> <span class=\\\"token punctuation\\\">(</span><span class=\\\"token keyword\\\">var</span> i <span class=\\\"token operator\\\">=</span> startBlockNumber<span class=\\\"token punctuation\\\">;</span> i <span class=\\\"token operator\\\"><=</span> endBlockNumber<span class=\\\"token punctuation\\\">;</span> i<span class=\\\"token operator\\\">++</span><span class=\\\"token punctuation\\\">)</span> <span class=\\\"token punctuation\\\">{</span>\\n <span class=\\\"token keyword\\\">var</span> block <span class=\\\"token operator\\\">=</span> <span class=\\\"token keyword\\\">await</span> web3<span class=\\\"token punctuation\\\">.</span>eth<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">getBlock</span><span class=\\\"token punctuation\\\">(</span>i<span class=\\\"token punctuation\\\">,</span> <span class=\\\"token boolean\\\">true</span><span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span>\\n <span class=\\\"token keyword\\\">if</span> <span class=\\\"token punctuation\\\">(</span>block <span class=\\\"token operator\\\">&&</span> block<span class=\\\"token punctuation\\\">.</span>transactions <span class=\\\"token operator\\\">!==</span> <span class=\\\"token keyword\\\">null</span><span class=\\\"token punctuation\\\">)</span> <span class=\\\"token punctuation\\\">{</span>\\n <span class=\\\"token keyword\\\">for</span> <span class=\\\"token punctuation\\\">(</span><span class=\\\"token keyword\\\">let</span> tx <span class=\\\"token keyword\\\">of</span> block<span class=\\\"token punctuation\\\">.</span>transactions<span class=\\\"token punctuation\\\">)</span> <span class=\\\"token punctuation\\\">{</span>\\n <span class=\\\"token function\\\">handleTransaction</span><span class=\\\"token punctuation\\\">(</span>tx<span class=\\\"token punctuation\\\">)</span><span class=\\\"token punctuation\\\">;</span>\\n <span class=\\\"token punctuation\\\">}</span>\\n lastBlockRead <span class=\\\"token operator\\\">=</span> block<span class=\\\"token punctuation\\\">.</span>number<span class=\\\"token punctuation\\\">;</span>\\n <span class=\\\"token punctuation\\\">}</span>\\n <span class=\\\"token punctuation\\\">}</span>\\n<span class=\\\"token punctuation\\\">}</span></code></pre>\\n </div>\\n<p>Firstly, the function checks which block number it should start from (using a variable <code class=\\\"language-text\\\">lastBlockRead</code>). This is useful as there’s no point re-processing blocks you’ve already processed, so it’s good to keep track of where you got up to. The variable is initialized at -1 (as the first block number is 0). The function then checks where it can read up to using <code class=\\\"language-text\\\">web3.eth.getBlockNumber</code>, which returns the highest block number on the blockchain. You could also read blocks up until a block is undefined, and use that as a loop termination condition.</p>\\n<p>The function then iterates over the blocks in the blockchain, getting each one with <code class=\\\"language-text\\\">web3.eth.getBlock(i, true)</code>. The first parameter to <code class=\\\"language-text\\\">getBlock</code> is the block number, and the second is a boolean which determines whether the transactions array should contain transaction objects (<code class=\\\"language-text\\\">true</code>) or hashes (<code class=\\\"language-text\\\">false</code>). We need the transaction objects to process the data they store, so we must pass in <code class=\\\"language-text\\\">true</code>. Once we have the block, the function simply loops through the available transactions and processes them.</p>\\n<p>This is great for manually processing all unread transactions, but what if we want to process transactions as they occur? For this, we’ll need to subscribe to certain blockchain events, so that this function is called whenever a new block is committed. To do this we can use the following asynchronous function:</p>\\n<div class=\\\"gatsby-highlight\\\">\\n <pre class=\\\"language-js\\\"><code class=\\\"language-js\\\">web3<span class=\\\"token punctuation\\\">.</span>eth<span class=\\\"token punctuation\\\">.</span><span class=\\\"token function\\\">subscribe</span><span class=\\\"token punctuation\\\">(</span><span class=\\\"token string\\\">\\\"newBlockHeaders\\\"</span><span class=\\\"token punctuation\\\">,</span> callback<span class=\\\"token punctuation\\\">)</span></code></pre>\\n </div>\\n<p>This function takes a string and a callback function. The string defines what kind of events we are subscribing to - <code class=\\\"language-text\\\">"newBlockHeaders"</code> means the callback is called whenever new blocks are committed (<a href=\\\"https://web3js.readthedocs.io/en/1.0/web3-eth-subscribe.html\\\">see here for the list of event types</a>). The callback is called whenever an event of the given kind occurs, and is passed a block header object or an error. I pass <code class=\\\"language-text\\\">getMessages</code> as my callback function so that the list of messages updates whenever a new block is committed.</p>\\n<p>With that, you should have all you need to send and receive transactions from a web-app! If you’d like to see my blockchain-integrated web-app, <a href=\\\"https://github.com/mindlink/sample-blockchain-messenger\\\">click here</a>.</p>\\n<h2>Conclusions</h2>\\n<p>As you can see, once you have an Ethereum network and blockchain set up it’s surprisingly easy to interact with it from a web-app. Everything you need to use the blockchain as a model is provided through Web3, as you can commit and read back data in the form of transactions. This makes it quite easy to use and quick to set up.</p>\\n<p>When it comes to the efficacy of an Ethereum blockchain as a data models, there are some obvious problems. The main issue is latency, as any transactions being sent to the blockchain must be mined first, which is not particularly quick in comparison to more conventional data stores.</p>\\n<p>Another big issue I found was that all data on the blockchain is freely available to anyone with access to the network, and so any user (with or without an account) could read any other user’s data easily. While there are ways to work around this restriction, there are methods built into conventional data stores which handle this issue for you, so this makes using a blockchain as a store look much less appetizing.</p>\\n<p>I’ve yet to find any practical benefits to using the blockchain as a model. While there are unique systems like mining/transaction fees and smart contracts, I’m not sure that they provide any tangible benefit that couldn’t be replicated on other types of model.</p>\\n<p>Overall, while this was a worthwhile proof-of-concept to gain experience working with Ethereum, it seems like there aren’t many reasons to use the blockchain as a model in the first place.</p>\",\"frontmatter\":{\"title\":\"Creating a private Ethereum blockchain and using it as a model\",\"date\":\"June 06, 2018\",\"author\":{\"id\":\"Daniel Dean\",\"bio\":\"Software Engineer Intern at MindLink. Worth at least 70% of a regular employee!\",\"avatar\":{\"childImageSharp\":{\"resolutions\":{\"tracedSVG\":\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='60' height='60' viewBox='0 0 60 60' version='1'%3E%3Crect width='100%25' height='100%25' fill='%23f6f2f8'/%3E%3Cpath d='M16 2c0 9 1 15 7 21l3 4c0 2-1 4-3 4-5 0-13 4-17 7-3 4-3 4-4 11 0 11-2 11 29 11 30 0 28 0 28-11-1-6-1-7-4-11-4-3-12-7-17-7-2 0-3-2-3-4l3-4c6-6 7-12 7-21 0-2 0-2-14-2-15 0-15 0-15 2M0 51l1 3v-7l-1 4' fill='%23e0d6eb' fill-rule='evenodd'/%3E%3C/svg%3E\",\"width\":40,\"height\":40,\"src\":\"/static/default-788e71c2bb40c6a516ea06e9a06387eb-bad24.png\",\"srcSet\":\"/static/default-788e71c2bb40c6a516ea06e9a06387eb-bad24.png 1x\"}}}}}}},\"pathContext\":{\"slug\":\"creating-a-private-ethereum-blockchain-and-using-it-as-a-model\",\"previous\":{\"fields\":{\"slug\":\"custom-content-in-a-skype-for-business-conversation\"},\"frontmatter\":{\"title\":\"Custom content in a Skype for Business conversation\"}},\"next\":{\"fields\":{\"slug\":\"end-to-end-encryption-with-the-signal-protocol\"},\"frontmatter\":{\"title\":\"End-To-End Encryption with the Signal Protocol\"}}}}\n\n\n//////////////////\n// WEBPACK FOOTER\n// ./~/json-loader!./.cache/json/creating-a-private-ethereum-blockchain-and-using-it-as-a-model.json\n// module id = 529\n// module chunks = 3348540548791"],"sourceRoot":""}