Skip to content

Instantly share code, notes, and snippets.

@hidehiro98
Last active September 23, 2020 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hidehiro98/99c52dffbfb10d9f9cc7451ee856d4c5 to your computer and use it in GitHub Desktop.
Save hidehiro98/99c52dffbfb10d9f9cc7451ee856d4c5 to your computer and use it in GitHub Desktop.
ブロックチェーンを作ることで学ぶ 〜ブロックチェーンがどのように動いているのか学ぶ最速の方法は作ってみることだ〜 ref: https://qiita.com/hidehiro98/items/841ece65d896aeaa8a2a
...
@app.route('/nodes/register', methods=['POST'])
def register_node():
values = request.get_json()
nodes = values.get('nodes')
if nodes is None:
return "Error: 有効ではないノードのリストです", 400
for node in nodes:
blockchain.register_node(node)
response = {
'message': '新しいノードが追加されました',
'total_nodes': list(blockchain.nodes),
}
return jsonify(response), 201
@app.route('/nodes/resolve', methods=['GET'])
def consensus():
replaced = blockchain.resolve_conflicts()
if replaced:
response = {
'message': 'チェーンが置き換えられました',
'new_chain': blockchain.chain
}
else:
response = {
'message': 'チェーンが確認されました',
'chain': blockchain.chain
}
return jsonify(response), 200
...
pip install Flask==0.12.2 requests==2.18.4
block = {
'index': 1,
'timestamp': 1506057125.900785,
'transactions': [
{
'sender': "8527147fe1f5426f9dd545de4b27ee00",
'recipient': "a77f5cdfa2934df3954a5c7c7da5df1f",
'amount': 5,
}
],
'proof': 324984774000,
'previous_hash': "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
}
$ python blockchain.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
$ python blockchain.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
$ curl -X POST -H "Content-Type: application/json" -d '{
"sender": "d4ee26eee15148ee92c6cd394edd974e",
"recipient": "someone-other-address",
"amount": 5
}' "http://localhost:5000/transactions/new"
{
"chain": [
{
"index": 1,
"previous_hash": 1,
"proof": 100,
"timestamp": 1506280650.770839,
"transactions": []
},
{
"index": 2,
"previous_hash": "c099bc...bfb7",
"proof": 35293,
"timestamp": 1506280664.717925,
"transactions": [
{
"amount": 1,
"recipient": "8bbcb347e0634905b0cac7955bae152b",
"sender": "0"
}
]
},
{
"index": 3,
"previous_hash": "eff91a...10f2",
"proof": 35089,
"timestamp": 1506280666.1086972,
"transactions": [
{
"amount": 1,
"recipient": "8bbcb347e0634905b0cac7955bae152b",
"sender": "0"
}
]
}
],
"length": 3
}
$ curl -X POST -H "Content-Type: application/json" -d '{
"nodes": ["http://localhost:5001"]
}' "http://localhost:5000/nodes/register"
$ curl -X POST -H "Content-Type: application/json" -d '{
"nodes": ["http://localhost:5001"]
}' "http://localhost:5000/nodes/register"
$ curl "http://localhost:5001/mine"
$ curl "http://localhost:5000/nodes/resolve"
block = {
'index': 1,
'timestamp': 1506057125.900785,
'transactions': [
{
'sender': "8527147fe1f5426f9dd545de4b27ee00",
'recipient': "a77f5cdfa2934df3954a5c7c7da5df1f",
'amount': 5,
}
],
'proof': 324984774000,
'previous_hash': "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
}
from hashlib import sha256
x = 5
y = 0 # まだこのyがどの数字であるべきかはわからない
while sha256(f'{x*y}'.encode()).hexdigest()[-1] != "0":
y += 1
print(f'The solution is y = {y}')
from hashlib import sha256
x = 5
y = 0 # まだこのyがどの数字であるべきかはわからない
while sha256(f'{x*y}'.encode()).hexdigest()[-1] != "0":
y += 1
print(f'The solution is y = {y}')
>>> sha256(f'{5*21}'.encode()).hexdigest()
'1253e9373e781b7500266caa55150e08e210bc8cd8cc70d89985e3600155e860'
{
"sender": "my address",
"recipient": "someone else's address",
"amount": 5
}
{
"sender": "my address",
"recipient": "someone else's address",
"amount": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment