Lab Assignment 1
Lab Assignment 1
Lab Assignment 1
AIM: To study implementation of Blockchain Technology.
LO1: Develop and test smart contract on local blockchain.
Theory:
Introduction
It demonstrates two key aspects of blockchain technology:
1. Smart Contract Development: A Solidity-based smart contract for transferring funds
between accounts on the Ethereum blockchain.
2. Blockchain Implementation: A simple blockchain implementation using Python to
understand the underlying concepts of blockchain technology.
1. Deployment:
o The contract was deployed, and the deployer’s address was initialized with a
balance of 25000.
2. Deposit:
o The deposit function was called with an amount of 1000. The deployer’s
balance increased to 26000.
3. Transfer:
o The transfer function was called to send 500 to a recipient address. The
deployer’s balance decreased to 25500, and the recipient’s balance increased
to 500.
4. Balance Check:
o The getBalance function was used to verify the balances of both the deployer
and the recipient.
o Key methods:
▪ new_block: Creates a new block and adds it to the chain.
▪ new_transaction: Adds a new transaction to the list of pending
transactions.
▪ proof_of_work: Implements a simple proof-of-work algorithm.
▪ valid_proof: Validates the proof-of-work.
2. Flask API:
o Provides endpoints to interact with the blockchain:
▪ /mine: Mines a new block.
▪ /transactions/new: Adds a new transaction.
▪ /chain: Returns the full blockchain.
import hashlib
import json
from time import time
from uuid import uuid4
from flask import Flask, jsonify, request
class Blockchain:
def __init__(self):
self.chain = []
self.current_transactions = []
self.new_block(previous_hash='1', proof=100) # Genesis block
}
self.current_transactions = []
self.chain.append(block)
return block
@staticmethod
def hash(block):
block_string = json.dumps(block, sort_keys=True).encode()
return hashlib.sha256(block_string).hexdigest()
@property
def last_block(self):
return self.chain[-1]
@staticmethod
def valid_proof(last_proof, proof):
PRASAD AROTE B21 ROLL NO - 77
guess = f'{last_proof}{proof}'.encode()
guess_hash = hashlib.sha256(guess).hexdigest()
return guess_hash[:4] == "0000"
app = Flask(__name__)
node_identifier = str(uuid4()).replace('-', '')
blockchain = Blockchain()
@app.route('/mine', methods=['GET'])
def mine():
last_block = blockchain.last_block
last_proof = last_block['proof']
proof = blockchain.proof_of_work(last_proof)
blockchain.new_transaction(
sender="0",
recipient=node_identifier,
amount=1,
)
block = blockchain.new_block(proof)
response = {
'message': "New Block Forged",
'index': block['index'],
'transactions': block['transactions'],
'proof': block['proof'],
'previous_hash': block['previous_hash'],
}
return jsonify(response), 200
PRASAD AROTE B21 ROLL NO - 77
@app.route('/transactions/new', methods=['POST'])
def new_transaction():
values = request.get_json()
required = ['sender', 'recipient', 'amount']
if not all(k in values for k in required):
return 'Missing values', 400
@app.route('/chain', methods=['GET'])
def full_chain():
response = {
'chain': blockchain.chain,
'length': len(blockchain.chain),
}
return jsonify(response), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)