-
Notifications
You must be signed in to change notification settings - Fork 12
/
blockchainproject_d.py
57 lines (39 loc) · 2.32 KB
/
blockchainproject_d.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-
"""BlockchainProject_D.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/17FaQ8UIUZxzJWPrwJWC1W8l6fxECSu5k
Block chain
Block chain: In simple words, Blockchain can be defined as a chain of the block that contains information.
"""
#Project Starts:
"""we will be using "hash function " to create fingerprints for each transactions , the hash function will link each out block chain to other block . To make this easier to use, we’ll define a helper function to wrap the python hash function that we’re using"""
import hashlib,json,sys
def hashMe(msg=""):
# For convenience, this is a helper function that wraps our hashing algorithm
if type(msg)!=str:
msg = json.dumps(msg,sort_keys=True) # If we don't sort keys, we can't guarantee repeatability!
if sys.version_info.major == 2:
return unicode(hashlib.sha256(msg).hexdigest(),'utf-8')
else:
return hashlib.sha256(str(msg).encode('utf-8')).hexdigest()
"""Next, we want to create a function to generate exchanges between vinny and kinny. We’ll indicate withdrawals with negative numbers, and deposits with positive numbers. We’ll construct our transactions to always be between the two users of our system, and make sure that the deposit is the same magnitude as the withdrawal- i.e. that we’re neither creating nor destroying money."""
import random
random.seed(0)
def makeTransaction(maxValue=3):
# This will create valid transactions in the range of (1,maxValue)
sign = int(random.getrandbits(1))*2 - 1 # This will randomly choose -1 or 1
amount = random.randint(1,maxValue)
vinnyPays = sign * amount
kinnyPays = -1 * vinnyPays
# By construction, this will always return transactions that respect the conservation of tokens.
# However, note that we have not done anything to check whether these overdraft an account
return {u'Vinny':vinnyPays,u'kinny':kinnyPays}
"""lets create transcations
"""
txnBuffer = [makeTransaction() for i in range(30)]
# WAIT WAIT Project Code is longer and detailed explained.
# Mail me to get Full Project Code at [ vatshayan007@gmail.com ]
"""# Mail me at vatshayan007@gail.com for Project Files.
## Project Files will include Project Code, Project PPT. Report and documentations
"""