A land management system using Proof of Stake consensus based blockchain
ID | Name |
---|---|
2019A7PS0164H |
Nandan H R |
2019A7PS0033H |
T V Chandra Vamsi |
2019A7PS0158H |
Vraj Ketan Gandhi |
Python 3.10
pip
The root folder consists of:
blockchain
,network
andutils
directories- Two python files,
main.py
anddemo.py
. requirements.txt
Running main.py
gives a command line interface to execute your own commands
in the network.
demo.py
contains a sample test case containing three nodes and covering all possible operations that can be performed in the network.
- From the root directory, run
pip install -r requirements.txt
- To execute
demo.py
, runpython demo.py
- To use the command line interface, execute
main.py
with the commandpython main.py <file_name>
where<file_name>
is optional and contains the state of the blockchain network.blockchain.net
contains the sample state of the network fromdemo.py
. - After starting the
main.py
program, typehelp
and hit enter to get a list of commands that can be performed in the network.
A Blockchain is a distributed ledger containing immutable transactions. It is a set of blocks cryptographically linked to each other. Each block contains a number of transactions. Each transaction is a transfer of value in the blockchain
This blockchain is used to track ownerships and transfers of lands
The blockchain supports 4 types of transactions
- Receive Coins
When a new node is connected, it is the amount of coins provided to it by the network ( specified by the user during node creation)
- Land Declaration
The transaction that a user uses to register a new land under their name
- Land Transfer
The transaction that a user uses to transfer ownership of a land they own to another user
- Stake Increase The transaction used by a user to increase their stake in the network
A validator is selected from the set of nodes in the network. This is done by using the coinage of the nodes.
Stake
- The amount of coins staked by a node in the networkAge
- The number of blocks since the last block minted by a nodeCoinage
- The product of stake and age A node is randomly chosen as a validator (Weighted by their coinages)
The validator node validates the transactions and puts the validated transaction into a block. This is called minting or forging a block. The block is then broadcasted to all nodes who then add it to their respective blockchains.
The structure of a block is given as
Block Header
- ID: The height of the block
- Timestamp: The timestamp when the block was minted
- Prev. block hash: The hash of the previous block
- Merkle Root: The merkle root of the transactions in the block
- Validator: The ID of the validator of the block
Block Data
- The list of transactions validated in this block
This folder holds the code implementation for the representation of the blockchain network. Network is essentially a simulation of a P2P environment.
The Network
class represents a blockchain network. It manages all communications between nodes.
Function | Definition |
---|---|
connectNode() |
Connects a new node to the network |
start() |
Starts the blockchain network which listens to the user inputs |
run() |
Run a specified command on the network |
nodeExists() |
A helper function to check if a given node (or for atleast one node) exists on the network |
handle() |
Handle user commands |
printCommands() |
Displays all the available commands |
broadcastTransaction() |
Broadcasts the new transaction to all nodes |
broadcastBlock() |
Broadcasts the new minted block to all nodes |
getValidator() |
This contains the implementation for the PoS consensus. The probability of a validator being selected is directly dependent on the stake the node holds in the blockchain. The validator mints the new block.This function will return the validator node's ID. |
mint() |
This function calls the validator on all the transactions in the transaction pool |
validate() |
This function actually validates all the transactions passed to it and returns a boolean based on whether the transaction is valid |
addBlock() |
Once all the transactions are validated, this function will mint and return the new block. The broadcastBlock() function will then broadcast the block to all nodes. |
This file contains the implementation of all the commands pertaining to operating a node as well as the Proof of Stake consensus algorithm
Function | Definition |
---|---|
registerCoins() |
Inititates a transaction to set the node's initial balance |
registerLand() |
Initates a new transaction to register a new land under the node |
buyLand() |
Initiates a new transaction for buying a land |
sellLand() |
Initiates a new transaction for selling a land |
stake() |
Initiate a new transaction to increase the stake of a node |
addTransaction() |
Adds a transaction to the transaction pool |
getValidator() |
This contains the implementation for the PoS consensus. The probability of a validator being selected is directly dependent on the stake the node holds in the blockchain. The validator mints the new block.This function will return the validator node's ID. |
mint() |
The validator chosen validates all transactions and mints a block |
validate() |
This function validates all the transactions passed to it and returns a boolean based on whether the transaction is valid |
addBlock() |
Adds a block to the transaction and empties the transaction pool |
This folder contains the blockchain implementation of the code
This file contains implementation for the block of the blockchain
Function | Definition |
---|---|
hashBlock() |
Hashes a given block using the SHA256 algorithm |
genesis() |
Generates the genesis block |
createBlock() |
Creates and returns a new block |
serialize() |
Serializes the block |
The Blockchain class is used to represent a blockchain which is a series of cryptographically linked blocks.
The Blockchain is the single source of truth for all data in a distributed network.
Function | Definition |
---|---|
addBlock() |
Appends a block to the blockchain |
getLength() |
Returns the length of the blockchain |
getTransaction() |
Returns transaction based on transaction ID |
getLandHistory() |
Gets history of the buyers and sellers of the land |
getLandOwner() |
Returns the landowner of the land ID given |
getLandOwners() |
Returns a list of all the lands and their owners |
getBlockFromHeight() |
Returns a block based on the block height |
getLastBlock() |
Returns the last block of the blockchain |
getStakes() |
Returns a list of the stakes of all nodes |
getAges() |
Get the coin ages of all nodes |
getBalance() |
Get wallet balance of a node |
getAllBalances() |
Get wallet balances of all nodes |
The Transaction class represents a transaction in the blockchain. A transaction is a transfer of value in a blockchain.
Function | Definition |
---|---|
newRCTransaction() |
Create a new Receive Coins transaction |
newLDTransaction() |
Create a new Land Declaration transaction |
newLTTransaction() |
Create a new Land Transfer transaction |
newSTTransaction() |
Create a new Stake Increase transaction |
generateTransaction() |
Generates a transaction in the correct structure |
serialize() |
Serializes the transaction object |
A MerkleTree is a tree in which every leaf node is a hash of a data block and every inner node is the hash of its child nodes. This class provides a function called getMerkleRoot()
to get the Merkle Root given a list of transactions