{"id":19124,"date":"2026-04-08T21:43:22","date_gmt":"2026-04-08T21:43:22","guid":{"rendered":"https:\/\/cryptoted.net\/index.php\/2026\/04\/08\/advanced-contract-programming-example-schellingcoin\/"},"modified":"2026-04-08T21:43:22","modified_gmt":"2026-04-08T21:43:22","slug":"advanced-contract-programming-example-schellingcoin","status":"publish","type":"post","link":"https:\/\/cryptoted.net\/index.php\/2026\/04\/08\/advanced-contract-programming-example-schellingcoin\/","title":{"rendered":"Advanced Contract Programming Example: SchellingCoin"},"content":{"rendered":"<p> <br \/>\n<\/p>\n<div id=\"\">\n<p class=\"chakra-text css-gi02ar\">Writing effective decentralized applications in Ethereum is at the same time easy and hard. The easy part we all know: rather than needing to create your own blockchain, manage complicated database code, deal with networking and NAT traversal, or any of the other complexities involving writing a peer-to-peer app from scratch, you can write code in a simple, high-level programming language like Serpent or Mutan (or LLL if you prefer mucking around a bit lower-level), with the simplicity of a toy scripting language but the power and security of a full blockchain backing it up. An entire implementation of a basic name registry can be done in two lines of code that encompass the essential logic of the program: <span class=\"chakra-text css-ons8vw\">if not contract.storage[msg.data[0]]: contract.storage[msg.data[0]] = msg.data[1]<\/span>. Use the zeroth data item in the message as a key and the first as a value; if the key is not yet taken then set the key to the desired value. A phone book that you can add entries to, but where entries, once made, cannot be changed. However, there is also a hard part: decentralized applications are likely to involve logic that is fundamentally complex, and there is no way that any simplifications to the programming environment can ever remove that fact (however, libraries built on top of the programming language might alleviate specific issues). Furthermore, any dapps doing anything truly interesting is likely to involve cryptographic protocols and economics, and we all know how complex those are.<\/p>\n<p class=\"chakra-text css-gi02ar\">The purpose of this article will be to go through a contract that is an important component of a fully decentralized cryptoeconomic ecosystem: a decentralized oracle. The oracle will be implemented using the SchellingCoin protocol, described <a class=\"chakra-link css-vezwxf\" href=\"https:\/\/blog.ethereum.org\/2014\/03\/28\/schellingcoin-a-minimal-trust-universal-data-feed\">in a previous blog post<\/a>. The core idea behind the protocol is that everyone &#8220;votes&#8221; on a particular value (in this case, we&#8217;ll use wei per US cent as an example, as that will end up very useful in financial contracts), and everyone who submitted a vote that is between the 25th and 75 percentile (ie. close to median) receives a reward. The median is taken to be the &#8220;true value&#8221;. In order to increase security, each round is done via a two-step commitment protocol: in the first phase, everyone selects a value <span class=\"chakra-text css-ons8vw\">P<\/span> which is the value they will be voting for, and submits <span class=\"chakra-text css-ons8vw\">H = sha3([msg.sender, P])<\/span> to the contract, and in the second phase everyone submits the <span class=\"chakra-text css-ons8vw\">P<\/span> that they selected and the contract accepts only those values that match the previously provided hash. Rewarding and evaluation is then done at the end.<\/p>\n<p class=\"chakra-text css-gi02ar\">The reason why it works is this. During the first phase, everyone is so to speak &#8220;in the dark&#8221;; they do not know what the others will be submitting, seeing perhaps only hashes of other votes. The only information they have is that they are supposed to be submitting the price of a US cent in wei. Thus, knowing only that the only value that other people&#8217;s answers are going to be biased towards is the actual wei\/UScent, the rational choice to vote for in order to maximize one&#8217;s chance of being near-median is the wei\/UScent itself. Hence, it&#8217;s in everyone&#8217;s best interests to come together and all provide their best estimate of the wei\/UScent price. An interesting philosophical point is that this is also the same way that proof-of-work blockchains work, except that in that case what you are voting on is the time order of transactions instead of some particular numeric value; this moderately strongly suggests that this protocol is likely to be viable at least for some applications.<\/p>\n<p class=\"chakra-text css-gi02ar\">Of course, in reality various kinds of special scenarios and attacks are possible, and the fact that the price of any asset is quite often controlled by a small number of centralized exchanges makes things more difficult. For example, one imaginable failure mode is that if there is a market share split between the BTC\/USD on Bitstamp, Bitfinex and MtGox, and MtGox is the most popular exchange, then the incentives might drive all the votes to aggregate around the GOX-BTC\/USD price specifically, and at that point it is entirely unclear what would happen when MtGox gets hacked and the price on that exchange alone, and not the others, falls to $100. Everyone may well end up following their individual incentives and sticking to each other to the protocol&#8217;s collective doom. How to deal with these situations and whether or not they are even significant is an entirely empirical issue; it is hard to say what the real world will do beforehand.<\/p>\n<p class=\"chakra-text css-gi02ar\">Formalizing the protocol, we have the following:<\/p>\n<ol role=\"list\" class=\"css-vgl4zd\">\n<li class=\"css-0\">Every set of N blocks (here, we set N = 100) constitutes a separate &#8220;epoch&#8221;. We define the epoch number as <span class=\"chakra-text css-ons8vw\">floor(block.number \/ 100)<\/span>, and we define the block number modulo 100 to be the &#8220;residual&#8221;.<\/li>\n<li class=\"css-0\">If the residual is less than 50, then anyone can submit a transaction with any value V and hash <span class=\"chakra-text css-ons8vw\">H = sha3([msg.sender, R, P])<\/span>, where <span class=\"chakra-text css-ons8vw\">P<\/span> is their estimate of the price of 1 US cent in wei (remember, 1 wei = 10<sup>-18<\/sup> ether, and 1 cent = 10<sup>-2<\/sup> USD) and <span class=\"chakra-text css-ons8vw\">R<\/span> is a random number.<\/li>\n<li class=\"css-0\">If the residual is greater than 50, then anyone who submitted a hash can submit <span class=\"chakra-text css-ons8vw\">P<\/span>, and the contract will check if <span class=\"chakra-text css-ons8vw\">sha3([msg.sender, P])<\/span> matches the hash.<\/li>\n<li class=\"css-0\">At the end of the epoch (or, more precisely, at the point of the first &#8220;ping&#8221; during the next epoch), everyone who submitted a value for <span class=\"chakra-text css-ons8vw\">P<\/span> between the 25th and 75th percentile, weighted by deposit, gets their deposit back plus a small reward, everyone else gets their deposit minus a small penalty, and the median value is taken to be the true UScent\/wei price. Everyone who failed to submit a valid value for <span class=\"chakra-text css-ons8vw\">P<\/span> gets their deposit back minus a small penalty.<\/li>\n<\/ol>\n<p>Note that there are possible optimizations to the protocol; for example, one might introduce a feature that allows anyone with a particular <\/p>\n<pre style=\"color:white;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none;padding:1em;margin:0.5em 0;overflow:auto;background:#011627\"><code class=\"language-bash\" style=\"color:#d6deeb;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none\"><span>P<\/span><\/code><\/pre>\n<p> value to steal the deposit from whoever submitted the hash, making it impractical to share one&#8217;s <\/p>\n<pre style=\"color:white;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none;padding:1em;margin:0.5em 0;overflow:auto;background:#011627\"><code class=\"language-bash\" style=\"color:#d6deeb;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none\"><span>P<\/span><\/code><\/pre>\n<p> to try to influence people&#8217;s votes before residual 50 hits and the second phase starts. However, to keep this example from getting too complicated we will not do this; additionally, I personally am skeptical of &#8220;forced private data revelation&#8221; strategies in general because I predict that many of them will become useless with the eventual advent of <a target=\"_blank\" rel=\"noopener\" class=\"chakra-link css-vezwxf\" href=\"http:\/\/eprint.iacr.org\/2013\/507.pdf\">generalized zero-knowledge proofs<\/a>, <a target=\"_blank\" rel=\"noopener\" class=\"chakra-link css-vezwxf\" href=\"http:\/\/en.wikipedia.org\/wiki\/Homomorphic_encryption#Fully_homomorphic_encryption\">fully homomorphic encryption<\/a> and <a target=\"_blank\" rel=\"noopener\" class=\"chakra-link css-vezwxf\" href=\"http:\/\/bitcoinmagazine.com\/10055\/cryptographic-code-obfuscation-decentralized-autonomous-organizations-huge-leap-forward\/\">obfuscation<\/a>. For example, one might imagine an attacker beating such a scheme by supplying a zero-knowledge proof that their <\/p>\n<pre style=\"color:white;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none;padding:1em;margin:0.5em 0;overflow:auto;background:#011627\"><code class=\"language-bash\" style=\"color:#d6deeb;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none\"><span>P<\/span><\/code><\/pre>\n<p> value is within a particular 10<sup>15<\/sup> wei-wide range, giving enough information to give users a target but not enough to practically locate the exact value of <\/p>\n<pre style=\"color:white;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none;padding:1em;margin:0.5em 0;overflow:auto;background:#011627\"><code class=\"language-bash\" style=\"color:#d6deeb;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none\"><span>P<\/span><\/code><\/pre>\n<p>. Given these concerns, and given the desire for simplicity, for now the simple two-round protocol with no bells-and-whistles is best.<\/p>\n<p class=\"chakra-text css-gi02ar\">Before we start coding SchellingCoin itself, there is one other contract that we will need to create: a sorting function. The only way to calculate the median of a list of numbers and determine who is in a particular percentile range is to sort the list, so we will want a generalized function to do that. For added utility, we will make our sorting function generic: we will sort pairs instead of integers. Thus, for examples, <span class=\"chakra-text css-ons8vw\">[30, 1, 90, 2, 70, 3, 50, 4]<\/span> would become <span class=\"chakra-text css-ons8vw\">[ 30, 1, 50, 4, 70, 3, 90, 2 ]<\/span>. Using this function, one can sort a list containing any kind of object simply by making an array of pairs where the first number is the key to sort by and the second number is a pointer to the object in parent memory or storage. Here&#8217;s <a target=\"_blank\" rel=\"noopener\" class=\"chakra-link css-vezwxf\" href=\"https:\/\/github.com\/ethereum\/serpent\/blob\/master\/examples\/quicksort_pairs.se\">the code<\/a>:<\/p>\n<div class=\"chakra-stack css-1jx0in4\">\n<pre><pre style=\"color:white;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none;padding:1em;margin:0.5em 0;overflow:auto;background:#011627\"><code class=\"language-bash\" style=\"color:#d6deeb;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none\"><span class=\"token\" style=\"color:rgb(127, 219, 202)\">if<\/span><span> msg.datasize <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">==<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>:\n<\/span><span>    return<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span>else:\n<span>    low <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> array<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>msg.datasize<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><span>    lsz <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>\n<\/span><span>    high <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> array<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>msg.datasize<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><span>    hsz <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>\n<\/span><span>    i <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span>\n<\/span><span>    <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">while<\/span><span> i <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\"><span> msg.datasize:\n<\/span><span>        <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">if<\/span><span> msg.data<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\"><span> msg.data<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>:\n<\/span><span>            low<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>lsz<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> msg.data<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>\n<\/span><span>            low<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>lsz + <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> msg.data<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i + <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>\n<\/span><span>            lsz <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">+=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span>\n<\/span>        else:\n<span>            high<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>hsz<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> msg.data<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>\n<\/span><span>            high<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>hsz + <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> msg.data<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i + <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>\n<\/span><span>            hsz <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">+=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span>\n<\/span><span>        i <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> i + <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span>\n<\/span><span>    low <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> call<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>contract.address, low, lsz, lsz<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><span>    high <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> call<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>contract.address, high, hsz, hsz<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><span>    o <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> array<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>msg.datasize<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><span>    i <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>\n<\/span><span>    <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">while<\/span><span> i <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\"><span> lsz:\n<\/span><span>        o<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> low<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>\n<\/span><span>        i <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">+=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span>\n<\/span><span>    o<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>lsz<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> msg.data<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>\n<\/span><span>    o<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>lsz + <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> msg.data<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>\n<\/span><span>    j <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>\n<\/span><span>    <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">while<\/span><span> j <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\"><span> hsz:\n<\/span><span>        o<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>lsz + <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span> + j<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> high<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>j<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>\n<\/span><span>        j <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">+=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span>\n<\/span><span>    return<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>o, msg.datasize<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><\/span><\/span><\/span><\/span><\/code><\/pre>\n<\/div>\n<p class=\"chakra-text css-gi02ar\">Computer students may recognize this as a quicksort implementation; the idea is that we first split the list into two, with one half containing everything less than the first item and the other half containing everything greater, then we recursively sort the first and second lists (the recursion terminates eventually, since eventually the sub-lists will have zero or one items, in which case we just return those values directly), and finally we concatenate <span class=\"chakra-text css-ons8vw\">output = sorted_less_than_list + first item + sorted_greater_than_list<\/span> and return that array. Now, putting that into &#8220;quicksort_pairs.se&#8221;, let&#8217;s build the code <a target=\"_blank\" rel=\"noopener\" class=\"chakra-link css-vezwxf\" href=\"https:\/\/github.com\/ethereum\/serpent\/blob\/master\/examples\/schellingcoin.se\">for the actual SchellingCoin<\/a>. Feel free to go to the github to see the code all in one piece; here, we will go through it a few lines at a time.<\/p>\n<p class=\"chakra-text css-gi02ar\">First, some initialization code:<\/p>\n<div class=\"chakra-stack css-1jx0in4\">\n<pre><pre style=\"color:white;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none;padding:1em;margin:0.5em 0;overflow:auto;background:#011627\"><code class=\"language-bash\" style=\"color:#d6deeb;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none\"><span>init:\n<\/span><span>    contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> block.number\n<\/span><span>    contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">3<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> create<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span class=\"token\" style=\"color:rgb(173, 219, 103)\">'quicksort_pairs.se'<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span>\n<!-- -->code:\n<span>    HASHES <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span>^160\n<\/span><span>    VALUES <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span>^170\n<\/span><\/code><\/pre>\n<\/div>\n<p class=\"chakra-text css-gi02ar\">The first code block sets contract storage index 0 to the current block number at initialization time, and then creates a quicksort contract and saves that in storage index 3. Note that theoretically you would want to just create the quicksort contract once and refer to it by address; we&#8217;re just doing an inline create for simplicity and to show the feature. In the code we start off by declaring two variables to serve as pseudo-constants; HASHES = 2<sup>160<\/sup> as the pointer for where we store hashes, and VALUES = 2<sup>170<\/sup> as the pointer for where we store values from the second phase.<\/p>\n<p class=\"chakra-text css-gi02ar\">Now, from here let&#8217;s skip to the bottom half of the code, because that turns out to be more convenient and it&#8217;s the code that actually gets run &#8220;first&#8221; over the course of the contract&#8217;s lifetime.<\/p>\n<div class=\"chakra-stack css-1jx0in4\">\n<pre><pre style=\"color:white;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none;padding:1em;margin:0.5em 0;overflow:auto;background:#011627\"><code class=\"language-bash\" style=\"color:#d6deeb;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none\"><span class=\"token\" style=\"color:rgb(99, 119, 119);font-style:italic\"># Hash submission<\/span><span>\n<\/span><span\/><span class=\"token\" style=\"color:rgb(127, 219, 202)\">if<\/span><span> msg.data<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">==<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span>:\n<\/span><span>    <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">if<\/span><span> block.number % <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">100<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\"><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">50<\/span><span>:\n<\/span><span>        cur <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>\n<\/span><span>        pos <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> HASHES + cur * <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">3<\/span><span>\n<\/span><span>        contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>pos<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> msg.data<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>\n<\/span><span>        contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>pos + <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> msg.value\n<\/span><span>        contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>pos + <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> msg.sender\n<\/span><span>        contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> cur + <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span>\n<\/span><span>        return<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>cur<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><span\/><span class=\"token\" style=\"color:rgb(99, 119, 119);font-style:italic\"># Value submission<\/span><span>\n<\/span><span\/><span class=\"token\" style=\"color:rgb(127, 219, 202)\">elif<\/span><span> msg.data<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">==<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span>:\n<\/span><span>    <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">if<\/span><span> sha3<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>msg.sender, msg.data<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">3<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>, msg.data<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">==<\/span><span> contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>HASHES + msg.data<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> * <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">3<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>:\n<\/span><span>        contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>VALUES + msg.data<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> msg.data<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>\n<\/span><span>        return<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><span\/><span class=\"token\" style=\"color:rgb(99, 119, 119);font-style:italic\"># Balance request<\/span><span>\n<\/span><span\/><span class=\"token\" style=\"color:rgb(127, 219, 202)\">elif<\/span><span> msg.data<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">==<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">3<\/span><span>:\n<\/span><span>    return<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>contract.balance<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><span\/><span class=\"token\" style=\"color:rgb(99, 119, 119);font-style:italic\"># Value request<\/span><span>\n<\/span>else:\n<span>    return<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><\/span><\/code><\/pre>\n<\/div>\n<p class=\"chakra-text css-gi02ar\">The first important paradigm that we see here is using <span class=\"chakra-text css-ons8vw\">msg.data[0]<\/span> to refer to a &#8220;message type&#8221;; messages with zeroth data item 1 are hash submissions, 2 are value submissions, 3 are balance requests and 4 are requests for the current UScent\/wei price. This is a standard interface that you will likely see across very many contracts. The first clause, the one for submitting hashes, is somewhat involved, so let us break it down step by step. The primary purpose here is to allow people to submit hashes, and record submissions in storage. To that end, the contract is storing the data sequentially in storage starting at index 2<sup>160<\/sup>. We need to store three pieces of data &#8211; the actual hash, the size of the accompanying deposit, and the sender address, for each hash, so we do that. We also use storage index 1 to store how many hashes have already been submitted. Thus, if two hashes have been submitted, storage will look something like this:<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/blog.ethereum.org\/images\/posts\/2014\/07\/strip.png\" class=\"chakra-image css-hw6q2r\"\/><\/p>\n<p class=\"chakra-text css-gi02ar\">The precise instructions in the clause are:<\/p>\n<ol role=\"list\" class=\"css-vgl4zd\">\n<li class=\"css-0\">Proceed only if the residual is less than 50.<\/li>\n<li class=\"css-0\">Set the variable <span class=\"chakra-text css-ons8vw\">cur<\/span> to storage index 1, where we are going to be storing the number of hashes that have already been submitted<\/li>\n<li class=\"css-0\">Set the variable <span class=\"chakra-text css-ons8vw\">pos<\/span> to the index in storage in which we will be putting the new hash<\/li>\n<li class=\"css-0\">Save the hash (supplied as the first data item), the sender address and the value in storage<\/li>\n<li class=\"css-0\">Set the new number of hashes to <span class=\"chakra-text css-ons8vw\">cur + 1<\/span><\/li>\n<li class=\"css-0\">Return the index of the hash supplied<\/li>\n<\/ol>\n<p>Technically, if the only users of SchellingCoin are people, step 5 is unnecessary; although the index will be necessary in a later step, a smart client could potentially simply scan the <\/p>\n<pre style=\"color:white;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none;padding:1em;margin:0.5em 0;overflow:auto;background:#011627\"><code class=\"language-bash\" style=\"color:#d6deeb;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none\"><span>cur<\/span><\/code><\/pre>\n<p> variable immediately after the transaction, removing the need for the opcodes needed to handle the return. However, since we expect that in Ethereum we will have plenty of instances of contracts using other contracts, we will provide the return value as a habit of good machine interface.<\/p>\n<p class=\"chakra-text css-gi02ar\">The next clause is for submitting values. Here, we ask for two data items as input: the index where the hash was saved during the first step of the protocol (this is the return value of the previous clause), and the actual value. We then hash the sender and value together, and if the hash matches then we save the result in another place in contract storage; an alternative approach is to use one single starting storage location and simply have four slots per hash instead of three. We return 1 is successful, and nothing for a failure. The third and fourth clauses are simply trivial data requests; the third is a balance check, and the fourth returns the contract&#8217;s current view of the price.<\/p>\n<p class=\"chakra-text css-gi02ar\">That&#8217;s all for the interface side of the contract; however, the one part that we still need to do is the part that actually aggregates the votes. We&#8217;ll break that up into parts. First, we have:<\/p>\n<div class=\"chakra-stack css-1jx0in4\">\n<pre><pre style=\"color:white;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none;padding:1em;margin:0.5em 0;overflow:auto;background:#011627\"><code class=\"language-bash\" style=\"color:#d6deeb;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none\"><span>HASHES <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span>^160\n<\/span><span>VALUES <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span>^170\n<\/span><span\/><span class=\"token\" style=\"color:rgb(127, 219, 202)\">if<\/span><span> block.number \/ <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">100<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">&gt;<\/span><span> contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> \/ <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">100<\/span><span>:\n<\/span><span>    <\/span><span class=\"token\" style=\"color:rgb(99, 119, 119);font-style:italic\"># Sort all hashes<\/span><span>\n<\/span><span>    N <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>\n<\/span><span>    o <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> array<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>N<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><span>    i <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>\n<\/span><span>    j <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>\n<\/span><span>    <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">while<\/span><span> i <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\"><span> N:\n<\/span><span>        <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">if<\/span><span> contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>VALUES + i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>:\n<\/span><span>            o<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>j<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>VALUES + i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>\n<\/span><span>            o<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>j + <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> i\n<\/span><span>            j <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">+=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span>\n<\/span><span>        i <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">+=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span>\n<\/span><span>    values <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> call<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">3<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>, o, j, j<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><\/span><\/code><\/pre>\n<\/div>\n<p class=\"chakra-text css-gi02ar\">First, we use storage index 0 to store the last accessed epoch, and we check if the current epoch is higher than the last accessed epoch. If it is, then that signals the start of a new epoch, so we need to process all the votes and clear the contract for the next epoch. We start off by copying the values that have been submitted to an array (values that have not been submitted, ie. zeroes, are not put into this array). We keep two running counters, <span class=\"chakra-text css-ons8vw\">i<\/span> and <span class=\"chakra-text css-ons8vw\">j<\/span>; the counter <span class=\"chakra-text css-ons8vw\">i<\/span> runs through all value slots, but the counter <span class=\"chakra-text css-ons8vw\">j<\/span> counts only the value slots that have something inside them. Note that the array that we produce is of the form <span class=\"chakra-text css-ons8vw\">[ val1, index1, val2, index2 &#8230; ]<\/span>, where <span class=\"chakra-text css-ons8vw\">index1<\/span> etc are the indices of the associated values in the original values array in contract storage, thus for example, the following values would lead to the following array:<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/blog.ethereum.org\/images\/posts\/2014\/06\/conversion.png\" class=\"chakra-image css-hw6q2r\"\/><\/p>\n<p class=\"chakra-text css-gi02ar\">Then, we send that array through the quicksort contract, which sorts data pairs in the array. After the sort, we end up with:<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"https:\/\/blog.ethereum.org\/images\/posts\/2014\/06\/conversion2.png\" class=\"chakra-image css-hw6q2r\"\/><\/p>\n<p class=\"chakra-text css-gi02ar\">Now, what we have is a sorted list of all the values that people have submitted, alongside pointers to where the associated metadata is stored in cold storage. The next part of the code will handle three things simultaneously. First, it will compute the total amount that has been deposited; this is useful in figuring out the median. Second, we will make two arrays to represent deposits and their associated addresses, and we will remove that data from the contract. Finally, we will 99.9% refund anyone who did not submit a value. Theoretically, we could make it a 70% refund or a 0% refund, but that might make the contract too risky for people to throw their life savings in (which is actually what we want in a proof-of-stake-weighted system; the more ether is thrown in by legitimate users the harder it is for an attacker to muster enough funds to launch an attack). here&#8217;s the code; feel free to understand each line yourself:<\/p>\n<div class=\"chakra-stack css-1jx0in4\">\n<pre><pre style=\"color:white;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none;padding:1em;margin:0.5em 0;overflow:auto;background:#011627\"><code class=\"language-bash\" style=\"color:#d6deeb;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none\"><span>    <\/span><span class=\"token\" style=\"color:rgb(99, 119, 119);font-style:italic\"># Calculate total deposit, refund non-submitters and<\/span><span>\n<\/span><span>    <\/span><span class=\"token\" style=\"color:rgb(99, 119, 119);font-style:italic\"># cleanup<\/span><span>\n<\/span>\n<span>    deposits <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> array<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>j \/ <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><span>    addresses <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> array<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>j \/ <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span>\n<span>    i <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>\n<\/span><span>    total_deposit <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>\n<\/span><span>    <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">while<\/span><span> i <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\"><span> j \/ <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span>:\n<\/span><span>        base_index <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> HASHES + values<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i * <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span> + <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> * <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">3<\/span><span>\n<\/span><span>        contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>base_index<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>\n<\/span><span>        deposits<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>base_index + <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>\n<\/span><span>        contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>base_index + <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>\n<\/span><span>        addresses<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>base_index + <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>\n<\/span><span>        contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>base_index + <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>\n<\/span><span>        <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">if<\/span><span> contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>VALUES + values<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i * <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span> + <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>:\n<\/span><span>            total_deposit <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">+=<\/span><span> deposits<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>\n<\/span>        else:\n<span>            send<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>addresses<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>, deposits<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> * <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">999<\/span><span> \/ <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1000<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><span>        i <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">+=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span>\n<\/span><\/span><\/code><\/pre>\n<\/div>\n<p class=\"chakra-text css-gi02ar\">Now, we come to the last part of the code, the part the computes the median and rewards people. According to the specification, we need to reward everyone between the 25th and 75th percentile, and take the median (ie. 50th percentile) as the truth. To actually do this, we needed to first sort the data; now that the data is sorted, however, it&#8217;s as simple as maintaining a running counter of &#8220;total deposited value of everything in the list up to this point&#8221;. If that value is between 25% and 75% of the total deposit, then we send a reward slightly greater than what they sent in, otherwise we send a slightly smaller reward. Here is the code:<\/p>\n<div class=\"chakra-stack css-1jx0in4\">\n<pre><pre style=\"color:white;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none;padding:1em;margin:0.5em 0;overflow:auto;background:#011627\"><code class=\"language-bash\" style=\"color:#d6deeb;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none\"><span>    inverse_profit_ratio <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> total_deposit \/ <\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>contract.balance \/ <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1000<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span> + <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span>\n<\/span><span>    <\/span><span class=\"token\" style=\"color:rgb(99, 119, 119);font-style:italic\"># Reward everyone<\/span><span>\n<\/span><span>    i <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>\n<\/span><span>    running_deposit_sum <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>\n<\/span><span>    halfway_passed <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>\n<\/span><span>    <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">while<\/span><span> i <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\"><span> j \/ <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span>:\n<\/span><span>        new_deposit_sum <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> running_deposit_sum + deposits<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>\n<\/span><span>        <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">if<\/span><span> new_deposit_sum <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">&gt;<\/span><span> total_deposit \/ <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">4<\/span><span> and running_deposit_sum <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\"><span> total_deposit * <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">3<\/span><span> \/ <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">4<\/span><span>:\n<\/span><span>            send<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>addresses<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>, deposits<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> + deposits<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> \/ inverse_profit_ratio * <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">3<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span>        else:\n<span>            send<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>addresses<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>, deposits<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> - deposits<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> \/ inverse_profit_ratio<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span>\n<span>        <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">if<\/span><span> not halfway_passed and new_deposit_sum <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">&gt;<\/span><span> total_deposit \/ <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span>:\n<\/span><span>            contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>VALUES + i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>\n<\/span><span>            halfway_passed <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span>\n<\/span><span>        contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>VALUES + i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>\n<\/span><span>        running_deposit_sum <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> new_deposit_sum\n<\/span><span>        i <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">+=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span>\n<\/span><span>    contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> block.number\n<\/span><span>    contract.storage<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>\n<\/span><\/span><\/span><\/code><\/pre>\n<\/div>\n<p class=\"chakra-text css-gi02ar\">At the same time, you can see we also zero out the values in contract storage, and we update the epoch and reset the number of hashes to zero. The first value that we calculate, the &#8220;inverse profit ratio&#8221;, is basically the inverse of the &#8220;interest rate&#8221; you get on your deposit; if <span class=\"chakra-text css-ons8vw\">inverse_profit_ratio = 33333<\/span>, and you submitted 1000000 wei, then you get 1000090 wei back if you are close to the median and 999970 if you are not (ie. your expected return is 1000030 wei). Note that although this amount is tiny, it happens per hundred blocks, so really it is quite large. And that&#8217;s all there is to it. If you want to test, then try running the following Python script:<\/p>\n<div class=\"chakra-stack css-1jx0in4\">\n<pre><pre style=\"color:white;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none;padding:1em;margin:0.5em 0;overflow:auto;background:#011627\"><code class=\"language-bash\" style=\"color:#d6deeb;font-family:Consolas, Monaco, &quot;Andale Mono&quot;, &quot;Ubuntu Mono&quot;, monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;font-size:1em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none\"><span class=\"token\" style=\"color:rgb(130, 170, 255)\">import<\/span><span> pyethereum\n<\/span><span>t <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> pyethereum.tester\n<\/span><span>s <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> t.state<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><span>s.mine<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">123<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><span>c <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> s.contract<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span class=\"token\" style=\"color:rgb(173, 219, 103)\">'schellingcoin.se'<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><span>c2 <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> s.contract<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span class=\"token\" style=\"color:rgb(173, 219, 103)\">'schellinghelper.se'<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><span>vals <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">125<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">200<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">126<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">900<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">127<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">500<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">128<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">300<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>,\n<\/span><span>        <\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">133<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">300<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">135<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">150<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">135<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">150<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>\n<\/span><span>s.send<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>t.k9, c, <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">10<\/span><span>**15<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><span>print <\/span><span class=\"token\" style=\"color:rgb(173, 219, 103)\">\"Submitting hashes\"<\/span><span>\n<\/span><span\/><span class=\"token\" style=\"color:rgb(127, 219, 202)\">for<\/span><span> i, <\/span><span class=\"token\" style=\"color:rgb(130, 170, 255)\">v<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">in<\/span><span> enumerate<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>vals<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>:\n<\/span><span>    print s.send<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>t.keys<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>, c, v<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">1<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span> + s.send<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>t.keys<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>, c2, <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>v<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">12378971241241<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">))<\/span><span>\n<\/span><span>s.mine<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">50<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><span>print <\/span><span class=\"token\" style=\"color:rgb(173, 219, 103)\">\"Submitting vals\"<\/span><span>\n<\/span><span\/><span class=\"token\" style=\"color:rgb(127, 219, 202)\">for<\/span><span> i, <\/span><span class=\"token\" style=\"color:rgb(130, 170, 255)\">v<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">in<\/span><span> enumerate<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>vals<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>:\n<\/span><span>    <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">if<\/span><span> i <\/span><span class=\"token\" style=\"color:rgb(127, 219, 202)\">!=<\/span><span> <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">5<\/span><span>:\n<\/span><span>        print s.send<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>t.keys<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>, c, <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span>, i, v<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">12378971241241<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span>    else:\n<span>        print s.send<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>t.keys<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span>i<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span>, c, <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">2<\/span><span>, i, <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">4<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><span>print <\/span><span class=\"token\" style=\"color:rgb(173, 219, 103)\">\"Final check\"<\/span><span>\n<\/span><span>s.mine<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">50<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><span>print s.send<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">(<\/span><span>t.k9, c, <\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">0<\/span><span>, <\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">[<\/span><span class=\"token\" style=\"color:rgb(247, 140, 108)\">4<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">]<\/span><span class=\"token\" style=\"color:rgb(199, 146, 234)\">)<\/span><span>\n<\/span><\/code><\/pre>\n<\/div>\n<p class=\"chakra-text css-gi02ar\">Before running the script, be sure to fill the &#8216;schellinghelper.se&#8217; file with <span class=\"chakra-text css-ons8vw\">return(sha3([msg.sender, msg.data[0], msg.data[1]], 3))<\/span>; here, we&#8217;re just being lazy and using Serpent itself to help us put the hash together; in reality, this should definitely be done off-chain. If you do that, and run the script, the last value printed by the contract should return 127.<\/p>\n<p class=\"chakra-text css-gi02ar\">Note that this contract as it stands is not really scalable by itself; at 1000+ users, whoever supplies the first transaction at the start of each epoch would need to pay a very large amount of gas. The way to fix this economically is of course to reward the submitter of the transaction, and take a flat fee off every participant to pay for the reward. Also, however, the interest rate per epoch is tiny, so it may already not be worth it for users to participate unless they have a signigicant amount of cash, and the flat fee may make this problem even worse.<\/p>\n<p class=\"chakra-text css-gi02ar\">To allow people to participate with small amounts of ether, the simplest solution is to create a &#8220;stake pool&#8221; where people put their ether into a contract for the long term, and then the pool votes together, randomly selecting a participant weighted by stake to supply the value to vote for in each epoch. This would reduce the load from two transactions per user per epoch to three transactions per pool per epoch (eg. 1 pool = 1000 users) plus one transaction per user to deposit\/withdraw. Note that, unlike Bitcoin mining pools, this stake pool is completely decentralized and blockchain-based, so it introduces at most very small centralization risks. However, this is an instructive example to show how a single contract or DAO may end up leading to an entire ecosystem of infrastructure working on the blockchain with contracts talking to each other; a specialized SchellingCoin blockchain would not be able to invent pooling mechanisms after the fact and integrate them so efficiently.<\/p>\n<p class=\"chakra-text css-gi02ar\">As far as applications go, the most immediate one is contracts for difference, and eventually a decentralized cryptographic US dollar; if you want to see an attempt at such a contract see <a target=\"_blank\" rel=\"noopener\" class=\"chakra-link css-vezwxf\" href=\"https:\/\/github.com\/ethereum\/serpent\/blob\/master\/examples\/schellingdollar.se\">here<\/a>, although that code is almost certainly vulnerable to market manipulation attacks (buy a very large amount of USD inside the system, then buy USD on the market to move the price 0.5%, then sell the USD inside the system for a quick 0.3% profit). The core idea behind the decentralized crypto-dollar is simple: have a bank with two currencies, USD and ether (or rather, UScent and wei), with the ability to have a positive or negative quantity of dollars, and manipulate the interest rate on dollar deposits in order to keep the contract&#8217;s net dollar exposure always close to zero so that the contract does not have any net obligations in currencies that it does not have the ability to hold. A simpler approach would simply be to have an expanding-supply currency that adjusts its supply function to target the USD, but that is problematic because there is no protection if the value falls too much. These kinds of applications, however, will likely take quite a long time (in crypto terms; quite fast in traditional finance terms of course) to get built.<\/p>\n<\/div>\n<p><br \/>\n<br \/><a href=\"https:\/\/blog.ethereum.org\/en\/2014\/06\/30\/advanced-contract-programming-example-schellingcoin\">Source link <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Writing effective decentralized applications in Ethereum is at the same time easy and hard. The easy part we all know: rather than needing to create your own blockchain, manage complicated database code, deal with networking and NAT traversal, or any of the other complexities involving writing a peer-to-peer app from scratch, you can write code [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":18498,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"tdm_status":"","tdm_grid_status":"","footnotes":""},"categories":[24],"tags":[],"kronos_expire_date":[],"class_list":["post-19124","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ethereum"],"_links":{"self":[{"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/posts\/19124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/comments?post=19124"}],"version-history":[{"count":0,"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/posts\/19124\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/media\/18498"}],"wp:attachment":[{"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/media?parent=19124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/categories?post=19124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/tags?post=19124"},{"taxonomy":"kronos_expire_date","embeddable":true,"href":"https:\/\/cryptoted.net\/index.php\/wp-json\/wp\/v2\/kronos_expire_date?post=19124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}