First assignment on creating blockchain to understand the fundamentals of the technology, and to have a answers to the following questions:
- How does blockchian work?
- How does wallet work?
- What is Proof-of-Work?
- What is mining and how does it work?
The simple command-line application is written on JavaScript.
Why JavaScript? I have no idea :)
Each module in the folder modules
represent distinct entity:
-
block.model.js
- representation of Block that consist of single block's fields -
blockchain.model.js
- representation of Blockchain
which contains:- Array representation of blockchain
- Difficulty property to regulate the amount of computational work while mining the block
add(block)
method to push newly created baby-block (lol) into the chainlastBlockHash()
method to retrieve the hash of previous blockshow()
method to output the blockchain structure
-
merkletree.model.js
- implementation of Merkle Tree data structure to have a Root Hash header in the Block(checkblock.model.js
) that stores hashes of block's transactions data and to prevent data modification. Learn more about Merkle Trees here -
miner.model.js
- representation of miner, that mines new blocks. After creation, miner has two fields: blockchain and memory pool. Miner has 2 methods:mine()
method that receives a transaction data fromtransactionPool
or momory pool that stores list of pending transactions. Then, it creates a new block, calculates merkle tree root hash, calculates its nonce which is a part of Proof-of-Work mechanism, and ensembles the data into single block. After mining it adds new block usingblockchian.add(newBlock)
method (see inblockchain.model.js
).mineGenesisBlock()
method which called during instantiation of Miner class. After creation, blockchain is totally empty, and the first block of the chain without data and previous block's hash is called Genesis Block that serves as a "head" of chain. The method generates this one Genesis Block.
-
transaction.model.js
- representation of transaction between two wallets. It has:sign()
- method to sign and veify the transaction using the private key of transaction initiator's(owner's) walletgenerateData()
method to process the transaction data and convert it to byte array for further sign operationgetTransactionData()
method to retrieve the transaction data as an single object, and to send it to blockshow()
method to output the info about transaction
-
transactionPool.model.js
- representation of memory pool. Memory pool holds a list of pending transactions that been made before creation of new Block. Memory pool cathes all created transactions and gives them to Miner to add them into new block. In my implementation, mempool serves also as a place to save all the transactions(for convenience). Transaction Pool has the following properties and methods:transactions[]
is a list to hold a history of transactions.pendingTransactions[]
is a list to store pending transactions that are currently don't belong to any block. The list is cleared since new block is mined.add(transaction)
method that takes a newly created transaction as an input and verifies it. The verification of the transaction consists of signature check and available funds check. For signature check it just checks the signature of the block using transaction owner's public key(pretty straightforward, I know). Funds check iterates over transactions history and check if sender has a enough amount of available cash to send it. Then if it is all okay, it adds transaction both to pending transactions list and transaction history list.getFunds()
method is a helper just to update a users balance in tha application. Actually it is a duplicate of avalidateFunds()
. Yes, I know that it was a serious mistake to duplicate the same function, but it is what it is. See here why.prepareForMining()
method gets all pending transactions and merges all of them into one single list of objects and then clears a pending transactions list.showPending()
method outputs to stdout pending transactions, nothing more :)
-
user.model.js
- user entity to have more convenient interaction with transaction operations. User class created a user with its unique login to make it simplier to send funds and to have an understanding who is who(without logins, each user is just a hash, which is user's wallet address). -
wallet.model.js
- user's pseudocryptowallet that consists of:
There is another app.js
file outside of the models
folder that holds an Application itself. Basically, application is nothing more than just a class to create an instances of all of the modules mentioned above and to call appropriate methods and perform actions on blockchain depending on user's input.
main.js
is just a main.js
. Take it easy, bro :)
The deadlines were short, it was my first touch of blockchain and a lot of stuff were unclear in the process of creation(not an excuse, but reasonable argument, lol). Generally, I had no purpose to invent a wheel or make a clone of great Satoshi Nakamoto's work. I tried to got to know what the blockchain even is and I believe, somehow I figured it out. Project has a lot of mistakes and things that are out of bounds of any logic. The methods are mixed up and a lot of stuff that are exist in real blokcchain was cut off. Project don't include following features(it is what I know, inly God knows what is over there that I have no idea of their existence):
- No reward system
- No miner races
- No validation of blockchain
- No distribution across nodes of network
And a lot more...
The next project are gonna be much better! Thank you for your attention <3
You have to have NodeJS JavaScript runtime and
npm
package manager installed on your machine
# clone the repository
git clone https://github.com/1slam10/blockchain-assignment-1.git
# get into project folder
cd mini-blockchain
# install dependencies
npm install
# to start a script and run the "app"
npm start
Voila! Enjoy!