Bearcoin protocol implementation

A slimmed-down Bitcoin

With all the news that’s been going on around Bitcoin, I thought I would try my hand at explaining it. I’m choosing to do this now

Bearcoin was a cryptocurrency experiment created during weekend of Hack at Brown 2015. The goal was to be a simple explanatory tool for how basic blockchains work, as well as a prototype for a cryptocurrency that could be used at the Ratty (“That sounds silly” you say? Well we did name it BearCoin. I says right on the tin that you should be bearish about it).

Yes, this is just a modified hack@brown logo

(Like any cryptocurrency project, what’s important is that we have an impressive logo for our project to distract from the fact that it’s Bitcoin without Bitcoin’s original selling point)

The development of bearcoin followed a few main stages:

The first stage of development was the An unspent transaction output (UTXO) is basicallythe output of a transaction that a user receives and is able to spend in the future. This is true because, as the name suggests, it is the unspent output of a transaction. But what does this mean, exactly? An analogy will be helpful in making UTXO clear.

Each and every UTXO is like a single fiat coin or a single fiat bill. If you have 45incash,youmusthavemorethanonebillbecausetheresnosuchthingasafortyfivedollarbill.Sowhileyouhave45 in cash, you must have more than one bill because there’s no such thing as a forty-five dollar bill. So while you have45 dollars in your wallet, you may have any number of combination of bills— UTXO— sitting in your wallet.

Bearcoin started out as a centralized transaction authority that handled UTXOs (much like any centralized e-cash).

After this, data structures for the Blockchain blocks were created and a transaction verification process was added.

How do we run it

Bearcoin has a lovely script simulation.java, that you can run for similating transactions and visualizing getting a sense forthe network topology.

public class Simulation {
   public static void main(String[] args) {
      int numNodes = 100;
      double p_graph = Double.parseDouble(args[0]);
      double p_malicious = Double.parseDouble(args[1]);
      double p_txDistribution = Double.parseDouble(args[2]);
      int numRounds = Integer.parseInt(args[3]);

      Node[] nodes = new Node[numNodes];
      for (int i = 0; i < numNodes; i++) {
         if(Math.random() < p_malicious)
            nodes[i] = new MalDoNothing(p_graph, p_malicious, p_txDistribution, numRounds);
         else
            nodes[i] = new CompliantNode(p_graph, p_malicious, p_txDistribution, numRounds);
      }

      boolean[][] followees = new boolean[numNodes][numNodes];
      for (int i = 0; i < numNodes; i++) {
         for (int j = 0; j < numNodes; j++) {
            if (i == j) continue;
            if(Math.random() < p_graph) {
               followees[i][j] = true;
            }
         }
      }

      for (int i = 0; i < numNodes; i++)
         nodes[i].setFollowees(followees[i]);

      int numTx = 500;
      HashSet<Integer> validTxIds = new HashSet<Integer>();
      Random random = new Random();
      for (int i = 0; i < numTx; i++) {
         int r = random.nextInt();
         validTxIds.add(r);
      }

      for (int i = 0; i < numNodes; i++) {
         HashSet<Transaction> pendingTransactions = new HashSet<Transaction>();
         for(Integer txID : validTxIds) {
            if (Math.random() < p_txDistribution)
               pendingTransactions.add(new Transaction(txID));
         }
         nodes[i].setPendingTransaction(pendingTransactions);
      }

      for (int round = 0; round < numRounds; round++) { 

         HashMap<Integer, Set<Candidate>> allProposals = new HashMap<>();

         for (int i = 0; i < numNodes; i++) {
            Set<Transaction> proposals = nodes[i].sendToFollowers();
            for (Transaction tx : proposals) {
               if (!validTxIds.contains(tx.id))
                  continue;

               for (int j = 0; j < numNodes; j++) {
                  if(!followees[j][i]) continue;
                  if (!allProposals.containsKey(j)) {
                	  Set<Candidate> candidates = new HashSet<>();
                	  allProposals.put(j, candidates);
                  }    
                  Candidate candidate = new Candidate(tx, i);
                  allProposals.get(j).add(candidate);
               }
            }
         }

         for (int i = 0; i < numNodes; i++) {
            if (allProposals.containsKey(i))
               nodes[i].receiveFromFollowees(allProposals.get(i));
         }
      }
      
      for (int i = 0; i < numNodes; i++) {
         Set<Transaction> transactions = nodes[i].sendToFollowers();
         System.out.println("Transaction ids that Node " + i + " believes consensus on:");
         for (Transaction tx : transactions)
            System.out.println(tx.id);
         System.out.println();
         System.out.println();
      }
   }
}

Is that all it takes to bake a cryptocurrency?

Everything I showed right here is an incredibly simplistic overview of how cryptocurrencies work. Obviously I had to strip away many of the bells and whistles that Bitcoin has (like the SHA256 hashing), and that other coins like LiteCoin have

Ultimately, what’s needed for any decent cryptocurrency project (or for that matter even a fiat currency) is belief in the system itself. The reason Bitcoin can be used as a currency was because people like Lazlo Hanyecz bought two pizzas for 10000 Bitcoins, setting an actual value to it. People might poke fun at him for spending what today would be equivalent to $2,950,000 on two pizzas, but the reason they have so much value in the first place is because he spent those bitcoins on the pizzas.

Meanwhile Bearcoin is still very far from being used for pizzas at the Ratty.

So how did Bearcoin do at Hack@Brown?

You know those programming projects where you get so immersed in what you’re doing that you forget the oiginal goal?

Yeah, that’s pretty much what happened with BearCoin. I just missed the submission deadline. Considering how it was also a very basic project, I doubt it would have gotten as far as the projects that actually did win.

Seriously though, kudos to Carl Olsson, Aaron Gokaslan, Karthik Desingh, and Logan Barnes, you guys are awesome!

Still, this was an exciting challenge, and this was a fantastic time to learn much more in-depth how cryptocurrencies work by building one myself.

Subscribe to know whenever I post new content. I don't spam!


At least this isn't a full screen popup

That would be more annoying. Anyways, if you like what you're reading, consider subscribing to my newsletter! I'll notify you when I publish new posts - no spam.