MrTsRex / Bitcoin_Implementation

This project implements a bitcoin network in Elixir as described in the white paper written by Satoshi Nakamoto.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bitcoin Implementations

Group Memebers

  • Sandesh Joshi - 4419-5831
  • Susmitha Are - 3655-4439

Implementation

We have implemented the bitcoin network following the Bitcoin white paper by Satoshi Nakamoto. We have used genserver for implementation where each node/user is a genserver. The state of the genserver maintains the public key, private key and the ledger in a tuple. The first value in the state is the private key, second is public key and the third value is a list which is the ledger containing chain of blocks(blockchain). Our implementation takes input from the user for the network size(number of nodes/users) and the number of miners(who are also the part of the network). Each node has their own wallet(public/private key pair) and their own copy of the ledger/blockchain. The genesis block is created and added to the ledger where each node get 100 coins with generated transaction. Just like in the paper, the users as well as miners have their own wallets but miners generate proof of work while users don't. We create transactions randomly and check whether the transaction is valid. If it's valid, it is broadcasted on the network and the miners receive it and make a block out of them and start mining. The proof of work requirement is only two leading 0's in the hash because we wanted the answers to be generated in milliseconds as required in the project. Once the block is mined by miner, it is broadcasted on the network. If some other miner has already mined the block and broadcasted the block with same transaction and has been added by the user to it's ledger, this new block will be discarded and will be considered as orphan. The entire network is fully distributed and we have achieved parallelism.

What is working?

  • Network creation: A bitcoin network is being successfully created with nodes having their own wallets and a copy of ledger.
  • Genesis Block creation: Genesis block is created with intial transactions to all the users and it is being added to the ledger.
  • Transactions: Sender and reciever are randomly selected and a random amount of bitcoins are selected for transaction.
  • Verifying digital Signature for transaction: In order to verify the authenticity of the sender, digital signature is created by encrypting transaction with sender private key. Reciever will decrypt the signature and compare the result with transaction. If they are same then the transaction is authentic otherwise it is invalid.
  • Verifying Transactions: Once the transaction is broadcasted, all the nodes verify whether the transaction is valid or not. In order to be valid, the transaction amount should be greater than the difference between total amount received and sent by the user.
  • Block creation: A miner receives transaction and creates a block with the block storing list of transactions, their hash and previous block hash (The last two values can be considered as the block header).
  • Verify Block: The blocks broadcasted by the miners are verified by the users/nodes in the network by first checking the nonce and the proof of work. Then checking whether the block with the same transactions are already in the ledger by comparing the previous hash of the last block in the ledger and the previous block hash of the received block.
  • Proof of work: The proof of work is generated by the miners by generating nonces and appending them to the hash of the transactions and then hashing it again until the desired hash is found. Once found, the nonce and the block is broadcasted on the network.

Largest network

We are able to create a bitcoin/blockchain network with 10,000 nodes/users.

Test Cases

We performed unit test for:

  • Verifying Public/private key pairs: This test verifies the generation of public and private key pairs.
  • Generation of digital signature: Given a transaction and private key, the test verifies whether a digital signature is being generated or not.
  • Verification of digital signature: Given a transaction, digital signature and public key, the test verifies whether the deprypted signature matches the transaction or not.
  • Generation of nonce: This test verifies the generation of nonce.
  • Generation of Proof of work: Given a hashed transaction, this test verifies whether the proof of work is properly being caluclated by the miner.
  • Verification of Proof of Work: When the miner broadcasts his nonce found for the proof of work, all the other users verify whether the genrated nonce is correct or not.
  • Creating Nodes: This test verifies whether the nodes are properly created or not.
  • Creating Wallet: This test verifies whether every user has his public/private key pair in his wallet or not.
  • Intializing transactions: This test verifies whether we're able to give bitcoins to all the users.
  • Creating block: This test verifies the creation of block with previous block hash, this block hash and transactions.
  • Generating random transaction: This test verifies whether random sender is able to send random amount bitcoins to random reciever.
  • Verifying authenticity of transaction: This test verifies whether the sender is authentic sender or not.
  • Verifying whether the transaction is valid or not: This test verifies whether the sender has enough money to perform the transaction. We performed acceptance testing for:
  • Mining: Which tests complete functionality from creating a transaction to adding it to the ledger.

Bonus

We have implemented the bonus section as well. The extra features we added are:

  • Incentive to the miners: The miners get 25 coin incentive for every block they mine and it gets added to the ledger.
  • Digital signature: Each transaction is sent with the digital signature which are created using the ECDSA algorithm with elliptic curve secp256k1.
  • Bitcoin public address: We have also written function to create bitcoin public address along with the public keys that we are using.

Compliation and Running

Run the project and the test cases using the following commands. Compile the program using "mix escript.build"

Running for Windows:

Run the program using "escript bitcoin numNodes numMiners". For example: escript bitcoin 10 2 Run the test cases using "mix test"

Running for Linux and Mac:

Run the program using "./bitcoin numNodes numMiners". For example: ./bitcoin 20 4 Run the test cases using "mix test"

About

This project implements a bitcoin network in Elixir as described in the white paper written by Satoshi Nakamoto.


Languages

Language:Elixir 100.0%