Blockchain Assignment
Blockchain Assignment
$ cd ganache
$ npm install
Q.3) Write a Simple Python program to create a Block class that contains
index, timestamp, and previous hash. Connect the blocks to create a
Blockchain.
# For timestamp
import datetime
# To store data
# in our blockchain
import JSON
class Blockchain:
return new_proof
previous_proof = previous_block['proof']
proof = block['proof']
hash_operation = hashlib.sha256(
str(proof*2 - previous_proof*2).encode()).hexdigest()
if hash_operation[:5] != '00000':
return False
previous_block = block
block_index += 1
return True
if valid:
response = {'message': 'The Blockchain is valid.'}
else:
response = {'message': 'The Blockchain is not valid.'}
return jsonify(response), 200
{
"chain":[{"index":1,
"previous_hash":"0",
"proof":1,
"timestamp":"2020-06-01 22:47:05.915000"},{"index":2,
"previous_hash":"2d83a826f87415edb31b7e12b35949b9dbf702aee7e383cba
b119456847b957c",
"proof":533,
"timestamp":"2020-06-01 22:47:59.309000"}],
"length":2
}
Output(valid):
Load Comments
WHAT'S NEW
Q.5) Write a Simple Smart Contract for Bank with withdraw and deposit
functionality.
// SPDX-License-Identifier: MIT
if(msg.value==0){
userAccount[msg.sender]=0;
userExists[msg.sender]=true;
userAccount[msg.sender] = msg.value;
userExists[msg.sender] = true;
userAccount[msg.sender]=userAccount[msg.sender]-amount;
msg.sender.transfer(amount);
userAccount[msg.sender]=userAccount[msg.sender]-amount;
userAccount[userAddress]=userAccount[userAddress]+amount;
userAccount[msg.sender]=userAccount[msg.sender]-amount;
toAddress.transfer(amount);
return userAccount[msg.sender];
return userExists[msg.sender];
}
Q.6) Write a Smart Contract for storing and retrieving information of
Degree Certificates.
No time was spent correcting errors that can occur when filling out
documentation by hand.
There's no need to worry about information being tampered with for personal
gain because there's no third party engaged and
Encrypted transaction logs are exchanged among participants.
Security
Savings
A smart contract is a sort of program that encodes business logic and operates
on a dedicated virtual machine embedded in a blockchain or other distributed
ledger.
Step 1: Business teams collaborate with developers to define their criteria for
the smart contract's desired behavior in response to certain events or
circumstances.
Step 2: Conditions such as payment authorization, shipment receipt, or a utility
meter reading threshold are examples of simple events.
Step 3: More complex operations, such as determining the value of a derivative
financial instrument, or automatically releasing an insurance payment, might
be encoded using more sophisticated logic.
Step 4: The developers then use a smart contract writing platform to create
and test the logic. After the application is written, it is sent to a separate team
for security testing.
Step 5: An internal expert or a company that specializes in vetting smart
contract security could be used.
Step 6: The contract is then deployed on an existing blockchain or other
distributed ledger infrastructure once it has been authorized.
Step 7: The smart contract is configured to listen for event updates from an
"oracle," which is effectively a cryptographically secure streaming data source,
once it has been deployed.
Step 8: Once it obtains the necessary combination of events from one or more
oracles, the smart contract executes.
Let's consider a real-life scenario in which smart contracts are used. Rachel is
at the airport, and her flight is delayed. AXA, an insurance company, provides
flight delay insurance utilizing Ethereum smart contracts. This insurance
compensates Rachel in such a case. How? The smart contract is linked to the
database recording flight status. The smart contract is created based on terms
and conditions.
The condition set for the insurance policy is a delay of two hours or more.
Based on the code, the smart contract holds AXA's money until that certain
condition is met. The smart contract is submitted to the nodes on EMV (a
runtime compiler to execute the smart contract code) for evaluation. All the
nodes on the network executing the code must come to the same result. That
result is recorded on the distributed ledger. If the flight is delayed in excess of
two hours, the smart contract self-executes, and Rachel is compensated. Smart
contracts are immutable; no one may alter the agreement.