Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Description of the project.


Enhanced Python Blockchain Project This project implements a basic blockchain with several enhanced features using Python:

  1. Core Blockchain: Blocks, chain structure, hashing (SHA-256).
  2. Transactions: Simple sender/recipient/amount structure
  3. Proof-of-Work (PoW): Basic nonce-finding algorithm.
  4. Dynamic Difficulty Adjustment: Adjusts mining difficulty based on block times to maintain a target interval.
  5. ECDSA Signatures: Transactions are signed using ECDSA (secp256k1 curve via cryptography library) for authenticity and integrity. Signatures are verified before adding transactions to the pool or blocks.
  6. Wallet: Basic wallet class for managing ECDSA key pairs (generation, conceptual loading/saving - INSECURE STORAGE), signing transactions, and calculating balances by scanning the chain.

P2P Networking (Flask): Uses Flask to create API endpoints for node communication: Node registration (/nodes/register) Transaction submission and broadcasting (/transactions/new, /transactions/receive) Mining new blocks (/mine) Block broadcasting (/blocks/receive) Chain retrieval (/chain) Consensus resolution (longest valid chain rule via /nodes/resolve) Conceptual Proof-of-Stake: Includes explanations of PoS principles (not implemented in code).


Setup


Clone the repository:

git clone <your-repo-url> cd enhanced_crypto_project

Create and activate a virtual environment (recommended):

python -m venv.venv
source.venv/bin/activate  # On Windows use `.venv\Scripts\activate`

Install dependencies:

pip install -r requirements.txt

 

Running Nodes Open multiple terminals. In each terminal, run a node on a different port:

Terminal 1 (Node 1):

python node_runner.py -p 5000 -i node1

Terminal 2 (Node 2):

python node_runner.py -p 5001 -i node2

Terminal 3 (Node 3):

python node_runner.py -p 5002 -i node3

Interacting with Nodes (Example using cURL) Register Nodes with Each Other:

Register Node 2 and Node 3 with Node 1:

curl -X POST -H "Content-Type: application/json" -d '{"nodes": ["[http://127.0.0.1:5001](http://127.0.0.1:5001)", "[http://127.0.0.1:5002](http://127.0.0.1:5002)"]}' [http://127.0.0.1:5000/nodes/register](http://127.0.0.1:5000/nodes/register)

Register Node 1 and Node 3 with Node 2:

curl -X POST -H "Content-Type: application/json" -d '{"nodes": ["[http://127.0.0.1:5000](http://127.0.0.1:5000)", "[http://127.0.0.1:5002](http://127.0.0.1:5002)"]}' [http://127.0.0.1:5001/nodes/register](http://127.0.0.1:5001/nodes/register)

Register Node 1 and Node 2 with Node 3:

curl -X POST -H "Content-Type: application/json" -d '{"nodes": ["[http://127.0.0.1:5000](http://127.0.0.1:5000)", "[http://127.0.0.1:5001](http://127.0.0.1:5001)"]}' [http://127.0.0.1:5002/nodes/register](http://127.0.0.1:5002/nodes/register)

Create a Transaction (Requires knowing recipient's public key PEM):

You'll need to get the public key PEM string from the wallet_nodeX.pub file (or node output) of the recipient node. The sending node's wallet will sign this automatically. You need to submit it to the sending node's API. (Manual transaction creation via API is complex due to signing. Usually done via a client app interacting with the wallet). A simplified client interaction would be needed to properly use the wallet's signing function before POSTing to /transactions/new. Mine a Block (e.g., on Node 1):

curl [http://127.0.0.1:5000/mine](http://127.0.0.1:5000/mine)
(This will include any pending transactions and the mining reward)

Check the Chain (e.g., on Node 2):

curl [http://127.0.0.1:5001/chain](http://127.0.0.1:5001/chain)

Resolve Conflicts (e.g., trigger on Node 3):

curl [http://127.0.0.1:5002/nodes/resolve](http://127.0.0.1:5002/nodes/resolve)

(This node will query its peers and adopt the longest valid chain)

Check Wallet Balance (e.g., on Node 1):

curl [http://127.0.0.1:5000/wallet/balance](http://127.0.0.1:5000/wallet/balance)

*********************************** GitHub Repository Setup Instructions ********************************

Here are the minimal Git commands to create a GitHub repository and push your local project code:

  1. Navigate to your project directory: Open your terminal or command prompt and change to the root directory of your project (enhanced_crypto_project/).

    cd /path/to/your/enhanced_crypto_project
  2. Initialize a local Git repository: This creates a hidden .git folder to track your project's history. The -b main sets the default branch name to main.[29, 30]

    git init -b main
  3. Add all project files to staging: This prepares all the files in your project directory (respecting .gitignore) to be included in the first commit.[29, 31, 30, 32, 33, 34]

    git add.
  4. Commit the files locally: This saves the staged changes to your local repository history with a message.[29, 31, 30, 32, 33, 34]

    git commit -m "Initial commit: Enhanced Python blockchain project"
  5. Create a new repository on GitHub: Go to GitHub.com, log in, and create a new, empty repository. Give it a name (e.g., enhanced-python-blockchain). Do not initialize it with a README, license, or .gitignore file on GitHub itself, as you've already created these locally.[29, 30, 35, 32, 33, 36]

  6. Copy the repository URL: On your new GitHub repository page, find and copy the repository URL. It will look something like https://github.com/YourUsername/enhanced-python-blockchain.git (HTTPS) or git@github.com:YourUsername/enhanced-python-blockchain.git (SSH).

  7. Link your local repository to the remote GitHub repository: Replace <YOUR_GITHUB_REPOSITORY_URL> with the URL you just copied.[29, 31, 30, 35, 32, 33, 36, 34] origin is the standard name for the remote connection.

    git remote add origin <YOUR_GITHUB_REPOSITORY_URL>
  8. Push your local code to GitHub: This uploads your committed files to the main branch on GitHub. The -u flag sets up tracking, so future pushes can use git push.[29, 31, 30, 35, 32, 33, 36, 34]

    git push -u origin main

Next Steps/Further Learning

This enhanced project serves as a robust foundation for further exploration. Potential next steps could include:

Implementing a more efficient state management model (e.g., UTXO or an account-based model) to replace full-chain balance scanning. Developing more sophisticated P2P communication protocols (e.g., gossip protocols for propagation). Exploring and implementing more advanced consensus algorithms (e.g., different PoS variations, Practical Byzantine Fault Tolerance - PBFT). Adding basic smart contract capabilities. Implementing secure key storage and management practices. Building a more comprehensive command-line or graphical user interface for wallet interaction. Conducting performance analysis and optimization of the network and consensus layers.

Conceptual Explanation of Proof-of-Stake (PoS)

As requested, here's a conceptual overview of Proof-of-Stake, which is not implemented in the code above:

  • Core Idea: Instead of miners expending computational power (PoW), PoS selects validators to create new blocks based on the amount of cryptocurrency they "stake" or lock up as collateral.[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  • Validator Selection: Users lock a certain amount of the network's native currency to become eligible validators.[1, 2, 3, 5, 13, 10, 11, 12] The network protocol then chooses validators to propose or attest to new blocks. Common selection methods include:
    • Stake-Weighted Randomness: Validators with a larger stake have a higher probability of being selected.[14, 2, 15, 3, 5, 8, 16, 11, 17]
    • Other factors: Some systems might incorporate coin age, validator reputation, or committee-based selection.[2, 5, 6, 13]
    • Delegated PoS (DPoS): Token holders vote for a limited number of delegates who perform validation.[5, 6, 7, 17]
  • Block Creation & Validation:
    1. Proposal: A selected validator proposes a new block containing transactions.[18, 13, 10, 11]
    2. Attestation/Voting: Other validators (or a committee) check the block's validity and vote/attest to it.[1, 2, 18, 13, 10, 11]
    3. Finalization: Once enough attestations are gathered (e.g., a two-thirds majority), the block is considered confirmed and added to the chain. PoS often allows for faster block finality than PoW.[18, 5, 11]
  • Economic Incentives & Security:
    • Rewards: Validators earn rewards, typically from transaction fees (and sometimes controlled inflation), for honest participation.[1, 2, 3, 5, 13, 16, 10, 19]
    • Slashing: Validators risk losing a portion or all of their staked collateral ("slashing") if they act maliciously (e.g., proposing invalid blocks, double-signing) or are consistently offline.[1, 2, 3, 18, 13, 10, 11, 12, 20] This economic penalty is the primary security mechanism, making attacks costly.[2, 3, 13, 10, 20]
  • PoS vs. PoW Differences:
    • Energy: PoS is vastly more energy-efficient as it doesn't require intensive computation.[14, 2, 6, 21, 22, 8, 9, 13, 16, 10, 23, 11, 24, 19, 25]
    • Hardware: PoS requires standard computing hardware, removing the need for specialized ASICs.[14, 6, 22, 7, 9, 13, 10, 23, 24, 19, 25]
    • Security Model: PoW security relies on the cost of computation; PoS security relies on the economic value of the staked assets.[2, 3, 22, 9, 13, 16, 26, 10, 11, 24, 20]
    • Centralization Risks: PoW risks mining pool/hardware centralization; PoS risks wealth concentration and staking pool centralization.[14, 27, 28, 22, 8, 13, 26, 23, 24, 12, 17]
    • Scalability: PoS generally allows for higher transaction throughput.[15, 3, 28, 8, 13, 23, 19, 25]

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages