Liteminer - Your Crypto Mining Crew

Many cryptocurrencies use a distributed consensus protocol to ensure security and authority. In this project I implemented a peer-to-peer networking and proof of work mechanism. In the case of cryptocurrencies, nodes are miners which may drop in and out of the network for various reasons: a miner may crash (or reboot), or a new miner may be added. These changes to the network happen in an unscheduled and unpredictable manner and special care must therefore be taken to ensure that no information is lost.

Implementation

* Due to ethical consideration, this repo will remain private util the end of Spring 2023 semester

Motivated by the hash function within boundaries. By distributing the calculation workload, the eventual proof-of-work is ensured by the key value with largest hash. Since the system required multi-threading and parallel computing, we used Atomic and mutex locks to prevent race condition. Atomic vars are used to protect simple types such as bool, int, string, while mutexes are used to protect maps, slices, and customized structs.

Proof of Work

In cryptocurrencies like Bitcoin, blocks which contain a collection of transactions are constantly created and validated by network participants that perform a computationally intensive task. This is often referred to as proof of work. A common proof of work scheme involves finding a number referred to as a nonce that when concatenated to a string and hashed results in a new string that has a certain property. In Bitcoin, the proof of work entails finding the nonce that, when concatenated with the transaction data and hashed, results in a string that starts with a series of zeros.

The fundamental insight is that doing the work to find such a nonce is computationally expensive and requires a lot of resources; therefore, in order to “cheat” the system, one needs to have more resources than all other participants combined.

Miner

In a cryptocurrency network, we refer to a miner as a worker that attempts to verify transactions by performing proof of work. Given a financial transaction encoded as a string M and an unsigned integer N, a miner will need to find the nonce between 0 and N, inclusive, that, when concatenated with M and hashed, results in the lowest hash value.

Pool

A pool is a collection of multiple miners and is responsible for distributing the work amongst its miners. It will also handle top-level logic and communications for a client. A pool and its corresponding miners have the following properties:

  • The pool must take client requests and shard these requests (distribute the work) across all of its miners.
  • The pool must keep track of the miners under its control and allow miners to come and go at will.
  • The pool must aggregate miner responses and return the proof of work to the client.

In a centralized system, this process can take a considerable amount of time. In a distributed system, you will be able to divide up the work, but you must take care when dealing with faulty miners (i.e., miners that drop in and out). Our pool takes a channel to marking the waiting mining tasks and to prevent the conflict of concurrent accessing by its FIFO characteristic. When a node is lost, the task will be return to the queue and wait for a new miner to grab. Only when alsl tasks in the channel are taken and all nonces are collect will the pool return a final proof-of-work. The miner will remain alive and waiting for the next mining task from the pool.

本作品采用 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议 (CC BY-NC-ND 4.0) 进行许可。