Skip to content

Commit dd0abe2

Browse files
iCoin update
1 parent f943c71 commit dd0abe2

File tree

6 files changed

+189
-0
lines changed

6 files changed

+189
-0
lines changed

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Akshay Bahadur
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

iCoin.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
from flask import Flask
2+
from flask import request
3+
import json
4+
import requests
5+
import hashlib as hasher
6+
import datetime as date
7+
node = Flask(__name__)
8+
9+
class CreateBlock:
10+
def __init__(self, index, timestamp, data, previous_hash):
11+
self.index = index
12+
self.timestamp = timestamp
13+
self.data = data
14+
self.previous_hash = previous_hash
15+
self.hash = self.create_hash()
16+
17+
def create_hash(self):
18+
sha = hasher.sha256()
19+
sha.update(str(self.index) + str(self.timestamp) + str(self.data) + str(self.previous_hash))
20+
return sha.hexdigest()
21+
22+
23+
def create_first_block():
24+
return CreateBlock(0, date.datetime.now(), {
25+
"proof-of-work": 8831*547,
26+
"transactions": None
27+
}, "0")
28+
29+
30+
miner_address = "frufhi55kefj-akshay-address-ndji45jjg6"
31+
blockchain = []
32+
blockchain.append(create_first_block())
33+
this_nodes_transactions = []
34+
35+
peer_nodes = []
36+
mining = True
37+
38+
@node.route('/request_transaction', methods=['POST'])
39+
def transaction():
40+
new_request_data = request.get_json()
41+
this_nodes_transactions.append(new_request_data)
42+
print "Transaction"
43+
print this_nodes_transactions
44+
print new_request_data
45+
print "FROM: {}".format(new_request_data['from'].encode('ascii','replace'))
46+
print "TO: {}".format(new_request_data['to'].encode('ascii','replace'))
47+
print "AMOUNT: {}\n".format(new_request_data['amount'])
48+
return "Transaction successful\n"
49+
50+
@node.route('/blocks', methods=['GET'])
51+
def get_blocks():
52+
chain_send = blockchain
53+
blocklist = ""
54+
for i in range(len(chain_send)):
55+
block = chain_send[i]
56+
block_index = str(block.index)
57+
block_timestamp = str(block.timestamp)
58+
block_data = str(block.data)
59+
block_hash = block.hash
60+
assembled = json.dumps({
61+
"index": block_index,
62+
"timestamp": block_timestamp,
63+
"data": block_data,
64+
"hash": block_hash
65+
})
66+
if blocklist == "":
67+
blocklist = assembled
68+
else:
69+
blocklist += assembled
70+
return blocklist
71+
72+
def find_new_chains():
73+
chains = []
74+
for node_url in peer_nodes:
75+
block = requests.get(node_url + "/blocks").content
76+
block = json.loads(block)
77+
chains.append(block)
78+
return chains
79+
80+
def consensus():
81+
chains = find_new_chains()
82+
longest_chain = blockchain
83+
for chain in chains:
84+
if len(longest_chain) < len(chain):
85+
longest_chain = chain
86+
blockchain = longest_chain
87+
88+
def proof_of_work(last_proof):
89+
flag = last_proof + 1
90+
proof_of_work_number=8831*547 #choose a combination 2 big prime number so that computational time is more
91+
while not (flag % proof_of_work_number == 0 and flag % last_proof == 0):
92+
flag += 1
93+
return flag
94+
95+
@node.route('/mine', methods = ['GET'])
96+
def mine():
97+
last_block = blockchain[len(blockchain) - 1]
98+
last_proof = last_block.data['proof-of-work']
99+
proof = proof_of_work(last_proof)
100+
this_nodes_transactions.append(
101+
{ "from": "System", "to": miner_address, "amount": 1 }
102+
)
103+
104+
new_block_data = {
105+
"proof-of-work": proof,
106+
"transactions": list(this_nodes_transactions)
107+
}
108+
new_block_index = last_block.index + 1
109+
new_block_timestamp = this_timestamp = date.datetime.now()
110+
last_block_hash = last_block.hash
111+
this_nodes_transactions[:] = []
112+
mined_block = CreateBlock(
113+
new_block_index,
114+
new_block_timestamp,
115+
new_block_data,
116+
last_block_hash
117+
)
118+
blockchain.append(mined_block)
119+
return json.dumps({
120+
"index": new_block_index,
121+
"timestamp": str(new_block_timestamp),
122+
"data": new_block_data,
123+
"hash": last_block_hash
124+
}) + "\n"
125+
126+
node.run()
127+

mine.PNG

41.9 KB
Loading

post.PNG

18.4 KB
Loading

readme.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## iCoin
2+
This code explains the working of Crypto-currency.
3+
4+
5+
### Code Requirements
6+
The example code is in Python ([version 2.7](https://www.python.org/download/releases/2.7/) or higher will work).
7+
1) import hashlib
8+
2) import flask
9+
10+
### Description
11+
12+
A cryptocurrency (or crypto currency) is a digital asset designed to work as a medium of exchange using cryptography to secure the transactions and to control the creation of additional units of the currency. Cryptocurrencies are classified as a subset of digital currencies and are also classified as a subset of alternative currencies and virtual currencies.
13+
14+
####Concepts
15+
16+
1) [Proof-of-Work Algorithm](https://en.wikipedia.org/wiki/Proof-of-work_system)
17+
2) [Concensus Algorithm](https://en.wikipedia.org/wiki/Consensus_(computer_science))
18+
19+
For more information, [see](https://en.wikipedia.org/wiki/Cryptocurrency)
20+
21+
###Steps
22+
23+
1) After you have set up the python code, you need to send a POST request of transferring iCoins to someone.
24+
25+
<img src="https://github.com/akshaybahadur21/iCoin-CryptoCurrency/blob/master/setup.png">
26+
27+
<img src="https://github.com/akshaybahadur21/iCoin-CryptoCurrency/blob/master/post.png">
28+
29+
2) Now mine the block for verification by send a GET request
30+
31+
<img src="https://github.com/akshaybahadur21/iCoin-CryptoCurrency/blob/master/mine.png">
32+
33+
3) You can check all the blocks by sending a get request to '/blocks'
34+
35+
36+
### Execution
37+
To run the code, type `python block_chain.py`
38+
39+
```
40+
python block_chain.py
41+
```

setup.PNG

13.4 KB
Loading

0 commit comments

Comments
 (0)