Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The code in this gist isn't as succinct as I'd like it to be. Please bare with me and ask plenty of questions that you may have about it.
from flask import Flask
from flask import request
import json
import requests
import hashlib as hasher
import datetime as date
node = Flask(__name__)
# Define what a Snakecoin block is
class Block:
def __init__(self, index, timestamp, data, previous_hash):
self.index = index
self.timestamp = timestamp
self.data = data
self.previous_hash = previous_hash
self.hash = self.hash_block()
def hash_block(self):
sha = hasher.sha256()
sha.update(str(self.index) + str(self.timestamp) + str(self.data) + str(self.previous_hash))
return sha.hexdigest()
# Generate genesis block
def create_genesis_block():
# Manually construct a block with
# index zero and arbitrary previous hash
return Block(0, date.datetime.now(), {
"proof-of-work": 9,
"transactions": None
}, "0")
# A completely random address of the owner of this node
miner_address = "q3nf394hjg-random-miner-address-34nf3i4nflkn3oi"
# This node's blockchain copy
blockchain = []
blockchain.append(create_genesis_block())
# Store the transactions that
# this node has in a list
this_nodes_transactions = []
# Store the url data of every
# other node in the network
# so that we can communicate
# with them
peer_nodes = []
# A variable to deciding if we're mining or not
mining = True
@node.route('/txion', methods=['POST'])
def transaction():
# On each new POST request,
# we extract the transaction data
new_txion = request.get_json()
# Then we add the transaction to our list
this_nodes_transactions.append(new_txion)
# Because the transaction was successfully
# submitted, we log it to our console
print "New transaction"
print "FROM: {}".format(new_txion['from'].encode('ascii','replace'))
print "TO: {}".format(new_txion['to'].encode('ascii','replace'))
print "AMOUNT: {}\n".format(new_txion['amount'])
# Then we let the client know it worked out
return "Transaction submission successful\n"
@node.route('/blocks', methods=['GET'])
def get_blocks():
chain_to_send = blockchain
# Convert our blocks into dictionaries
# so we can send them as json objects later
for i in range(len(chain_to_send)):
block = chain_to_send[i]
block_index = str(block.index)
block_timestamp = str(block.timestamp)
block_data = str(block.data)
block_hash = block.hash
chain_to_send[i] = {
"index": block_index,
"timestamp": block_timestamp,
"data": block_data,
"hash": block_hash
}
chain_to_send = json.dumps(chain_to_send)
return chain_to_send
def find_new_chains():
# Get the blockchains of every
# other node
other_chains = []
for node_url in peer_nodes:
# Get their chains using a GET request
block = requests.get(node_url + "/blocks").content
# Convert the JSON object to a Python dictionary
block = json.loads(block)
# Add it to our list
other_chains.append(block)
return other_chains
def consensus():
# Get the blocks from other nodes
other_chains = find_new_chains()
# If our chain isn't longest,
# then we store the longest chain
longest_chain = blockchain
for chain in other_chains:
if len(longest_chain) < len(chain):
longest_chain = chain
# If the longest chain isn't ours,
# then we stop mining and set
# our chain to the longest one
blockchain = longest_chain
def proof_of_work(last_proof):
# Create a variable that we will use to find
# our next proof of work
incrementor = last_proof + 1
# Keep incrementing the incrementor until
# it's equal to a number divisible by 9
# and the proof of work of the previous
# block in the chain
while not (incrementor % 9 == 0 and incrementor % last_proof == 0):
incrementor += 1
# Once that number is found,
# we can return it as a proof
# of our work
return incrementor
@node.route('/mine', methods = ['GET'])
def mine():
# Get the last proof of work
last_block = blockchain[len(blockchain) - 1]
last_proof = last_block.data['proof-of-work']
# Find the proof of work for
# the current block being mined
# Note: The program will hang here until a new
# proof of work is found
proof = proof_of_work(last_proof)
# Once we find a valid proof of work,
# we know we can mine a block so
# we reward the miner by adding a transaction
this_nodes_transactions.append(
{ "from": "network", "to": miner_address, "amount": 1 }
)
# Now we can gather the data needed
# to create the new block
new_block_data = {
"proof-of-work": proof,
"transactions": list(this_nodes_transactions)
}
new_block_index = last_block.index + 1
new_block_timestamp = this_timestamp = date.datetime.now()
last_block_hash = last_block.hash
# Empty transaction list
this_nodes_transactions[:] = []
# Now create the
# new block!
mined_block = Block(
new_block_index,
new_block_timestamp,
new_block_data,
last_block_hash
)
blockchain.append(mined_block)
# Let the client know we mined a block
return json.dumps({
"index": new_block_index,
"timestamp": str(new_block_timestamp),
"data": new_block_data,
"hash": last_block_hash
}) + "\n"
node.run()
@sam-irl

This comment has been minimized.

Show comment
Hide comment
@sam-irl

sam-irl Jul 24, 2017

Update: solved. Below is my working get_blocks()

@node.route('/blocks', methods=['GET'])
def get_blocks():
  chain_to_send = blockchain
  blocklist = ""
  for i in range(len(chain_to_send)):
    block = chain_to_send[i]
    block_index = str(block.index)
    block_timestamp = str(block.timestamp)
    block_data = str(block.data)
    block_hash = block.hash
    assembled = json.dumps({
    "index": block_index,
    "timestamp": block_timestamp,
    "data": block_data,
    "hash": block_hash
    })
    if blocklist == "":
      blocklist = assembled
    else:
      blocklist += assembled
  return blocklist

I can make one HTTP POST/GET before an error is thrown. Code unedited.

Run in terminal:

sam@SAM-SURFACE:~/snakecoin$ curl localhost:5000/blocks
[{"index": "0", "data": "{'transactions': None, 'proof-of-work': 9}", "hash": "4581e66df1e9ad5727f52aba796c6f102e26eaa6be1531a24408d6d7d1723f6d", "timestamp": "2017-07-24 06:15:16.424693"}]
sam@SAM-SURFACE:~/snakecoin$ curl localhost:5000/blocks
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request.  Either the server is overloaded or there is an error in the application.</p>

Output:

sam@SAM-SURFACE:~/snakecoin$ python snakecoin.py
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [24/Jul/2017 06:15:33] "GET /blocks HTTP/1.1" 200 -
[2017-07-24 06:15:34,877] ERROR in app: Exception on /blocks [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "snakecoin.py", line 71, in get_blocks
    block_index = str(block.index)
AttributeError: 'dict' object has no attribute 'index'
127.0.0.1 - - [24/Jul/2017 06:15:34] "GET /blocks HTTP/1.1" 500 -

sam-irl commented Jul 24, 2017

Update: solved. Below is my working get_blocks()

@node.route('/blocks', methods=['GET'])
def get_blocks():
  chain_to_send = blockchain
  blocklist = ""
  for i in range(len(chain_to_send)):
    block = chain_to_send[i]
    block_index = str(block.index)
    block_timestamp = str(block.timestamp)
    block_data = str(block.data)
    block_hash = block.hash
    assembled = json.dumps({
    "index": block_index,
    "timestamp": block_timestamp,
    "data": block_data,
    "hash": block_hash
    })
    if blocklist == "":
      blocklist = assembled
    else:
      blocklist += assembled
  return blocklist

I can make one HTTP POST/GET before an error is thrown. Code unedited.

Run in terminal:

sam@SAM-SURFACE:~/snakecoin$ curl localhost:5000/blocks
[{"index": "0", "data": "{'transactions': None, 'proof-of-work': 9}", "hash": "4581e66df1e9ad5727f52aba796c6f102e26eaa6be1531a24408d6d7d1723f6d", "timestamp": "2017-07-24 06:15:16.424693"}]
sam@SAM-SURFACE:~/snakecoin$ curl localhost:5000/blocks
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request.  Either the server is overloaded or there is an error in the application.</p>

Output:

sam@SAM-SURFACE:~/snakecoin$ python snakecoin.py
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [24/Jul/2017 06:15:33] "GET /blocks HTTP/1.1" 200 -
[2017-07-24 06:15:34,877] ERROR in app: Exception on /blocks [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "snakecoin.py", line 71, in get_blocks
    block_index = str(block.index)
AttributeError: 'dict' object has no attribute 'index'
127.0.0.1 - - [24/Jul/2017 06:15:34] "GET /blocks HTTP/1.1" 500 -
@wowkin2

This comment has been minimized.

Show comment
Hide comment
@wowkin2

wowkin2 Aug 8, 2017

Why don't you use PEP8 (at least 4 space indentation) and don't write Python 3 compatible code (print as function)?

wowkin2 commented Aug 8, 2017

Why don't you use PEP8 (at least 4 space indentation) and don't write Python 3 compatible code (print as function)?

@zacanger

This comment has been minimized.

Show comment
Hide comment
@zacanger

zacanger Aug 14, 2017

For anyone interested, as a learning exercise I ported this to JS: https://github.com/zacanger/tiny-blockchain

For anyone interested, as a learning exercise I ported this to JS: https://github.com/zacanger/tiny-blockchain

@lq08025107

This comment has been minimized.

Show comment
Hide comment
@lq08025107

lq08025107 Aug 17, 2017

your consensus function was not used

your consensus function was not used

@zacanger

This comment has been minimized.

Show comment
Hide comment
@zacanger

zacanger Aug 21, 2017

@lq08025107 that should probably be at the top of the GET /blocks route, shouldn't it?

@lq08025107 that should probably be at the top of the GET /blocks route, shouldn't it?

@tryjude

This comment has been minimized.

Show comment
Hide comment
@tryjude

tryjude Aug 25, 2017

The peer_nodes is an empty list... how does it get populated?

tryjude commented Aug 25, 2017

The peer_nodes is an empty list... how does it get populated?

@zzz6519003

This comment has been minimized.

Show comment
Hide comment
@zzz6519003

zzz6519003 Aug 30, 2017

mining is not used

zzz6519003 commented Aug 30, 2017

mining is not used

@zzz6519003

This comment has been minimized.

Show comment
Hide comment
@zzz6519003

zzz6519003 Aug 30, 2017

"your consensus function was not used" + 1

"your consensus function was not used" + 1

@drewrice2

This comment has been minimized.

Show comment
Hide comment
@drewrice2

drewrice2 Sep 7, 2017

i'm working on a reimplementation of aunyks's great work. feel free to check out my repo simple_blockchain. it's far from perfect right now... would love some criticism or help!

i plan to implement some of the features core to the original blockchain system, outlined in the bitcoin whitepaper.

so far, i've included the nonce calculation and hashing, with a variable for difficulty. i still need to work on transaction broadcasting to other nodes, transaction verification, etc.

i'm working on a reimplementation of aunyks's great work. feel free to check out my repo simple_blockchain. it's far from perfect right now... would love some criticism or help!

i plan to implement some of the features core to the original blockchain system, outlined in the bitcoin whitepaper.

so far, i've included the nonce calculation and hashing, with a variable for difficulty. i still need to work on transaction broadcasting to other nodes, transaction verification, etc.

@dmoney

This comment has been minimized.

Show comment
Hide comment
@dmoney

dmoney Sep 20, 2017

In consensus(), it seems to me that it should verify the other chains' proof of work before accepting them. This seems to work as a verifier:

def verify_proof_of_work(proof, last_proof):
    return (proof > last_proof
        and proof % 9 == 0
        and proof % last_proof == 0)

However, it seems you can avoid doing the work by just multiplying the last proof by 9.

Edit: I see @drewrice2 uses a different proof function, based on leading 0's in a sha256 hash value, as mentioned in the whitepaper: https://github.com/drewrice2/simple_blockchain/blob/master/code/node.py

dmoney commented Sep 20, 2017

In consensus(), it seems to me that it should verify the other chains' proof of work before accepting them. This seems to work as a verifier:

def verify_proof_of_work(proof, last_proof):
    return (proof > last_proof
        and proof % 9 == 0
        and proof % last_proof == 0)

However, it seems you can avoid doing the work by just multiplying the last proof by 9.

Edit: I see @drewrice2 uses a different proof function, based on leading 0's in a sha256 hash value, as mentioned in the whitepaper: https://github.com/drewrice2/simple_blockchain/blob/master/code/node.py

@davidgengler

This comment has been minimized.

Show comment
Hide comment
@davidgengler

davidgengler Oct 2, 2017

Using my Windows machine, it took me a bit to figure out the curl vs. Invoke-Web stuff. If you're trying to run the program and send a transaction but it's giving you grief about content types, this might help:

Invoke-RestMethod "http://localh ost:5000/txion" -ContentType 'application/json' -Method Post -Body '{"from": "asd fa", "to":"asdfas", "amount": 3}'

Using my Windows machine, it took me a bit to figure out the curl vs. Invoke-Web stuff. If you're trying to run the program and send a transaction but it's giving you grief about content types, this might help:

Invoke-RestMethod "http://localh ost:5000/txion" -ContentType 'application/json' -Method Post -Body '{"from": "asd fa", "to":"asdfas", "amount": 3}'

@jmutuku95

This comment has been minimized.

Show comment
Hide comment
@jmutuku95

jmutuku95 Oct 13, 2017

so how is the peer_nodes populated and how do we make use of the consensus function?

so how is the peer_nodes populated and how do we make use of the consensus function?

@iandesj

This comment has been minimized.

Show comment
Hide comment
@iandesj

iandesj Nov 15, 2017

Is there like a central authority that takes care of peer_nodes?

iandesj commented Nov 15, 2017

Is there like a central authority that takes care of peer_nodes?

@bigjonroberts

This comment has been minimized.

Show comment
Hide comment
@bigjonroberts

bigjonroberts Dec 9, 2017

@iandesj:
here's some information on how bitcoin bootstraps to find peers:
https://bitcoin.stackexchange.com/questions/2027/how-does-the-bitcoin-client-make-the-initial-connection-to-the-bitcoin-network
that seems like overkill for a small POC like this.

bigjonroberts commented Dec 9, 2017

@iandesj:
here's some information on how bitcoin bootstraps to find peers:
https://bitcoin.stackexchange.com/questions/2027/how-does-the-bitcoin-client-make-the-initial-connection-to-the-bitcoin-network
that seems like overkill for a small POC like this.

@benrhine

This comment has been minimized.

Show comment
Hide comment
@benrhine

benrhine Dec 15, 2017

I am currently work on a port of this to Groovy using the Spark web server. Most of the code is ported and should be fully functional shortly.

Groovy Version:
https://github.com/benrhine/GroovySnakeCoinExpanded

I am currently work on a port of this to Groovy using the Spark web server. Most of the code is ported and should be fully functional shortly.

Groovy Version:
https://github.com/benrhine/GroovySnakeCoinExpanded

@cosme12

This comment has been minimized.

Show comment
Hide comment
@cosme12

cosme12 Jan 13, 2018

Hey @aunyks don't leave use wondering how this fantastic tutorial continues and how wallets work. Thanks again and keep it up!

cosme12 commented Jan 13, 2018

Hey @aunyks don't leave use wondering how this fantastic tutorial continues and how wallets work. Thanks again and keep it up!

@dpino

This comment has been minimized.

Show comment
Hide comment
@dpino

dpino Jan 14, 2018

The consensus function synchronizes the blockchain of the current node to the longest blockchain available across the network of peers. It should be called in '/blocks' before populating the list of blocks to be returned, thus the node makes sure its blockchain is the same as all the other peers.

About how to populate the list of peers, it would be possible to define a new REST command '/add_peer' that adds a peer address running in a host:port. It would be necessary as well to be able to pass a port number when running the server, so two or more instances in the same host don't collide. I implemented those changes in a new version of the script. Check it out here: https://gist.github.com/dpino/07a0e90d559a959eb73fecf58a3fea92

dpino commented Jan 14, 2018

The consensus function synchronizes the blockchain of the current node to the longest blockchain available across the network of peers. It should be called in '/blocks' before populating the list of blocks to be returned, thus the node makes sure its blockchain is the same as all the other peers.

About how to populate the list of peers, it would be possible to define a new REST command '/add_peer' that adds a peer address running in a host:port. It would be necessary as well to be able to pass a port number when running the server, so two or more instances in the same host don't collide. I implemented those changes in a new version of the script. Check it out here: https://gist.github.com/dpino/07a0e90d559a959eb73fecf58a3fea92

@saint1729

This comment has been minimized.

Show comment
Hide comment
@saint1729

saint1729 Jan 27, 2018

Hi everyone,

Few basic questions.

If I host the above source on my machine and you host it on your machine, will this still be like we are using the same blockchain to mine SnakeCoin?

Or is there a way that nodes have to be connected before you start mining? How do we ensure that there is a limit on number of coins that can be mined? How can we increase the level of difficulty of mining as the number of coins mined gets increased?

Hi everyone,

Few basic questions.

If I host the above source on my machine and you host it on your machine, will this still be like we are using the same blockchain to mine SnakeCoin?

Or is there a way that nodes have to be connected before you start mining? How do we ensure that there is a limit on number of coins that can be mined? How can we increase the level of difficulty of mining as the number of coins mined gets increased?

@drewrice2

This comment has been minimized.

Show comment
Hide comment
@drewrice2

drewrice2 Jan 28, 2018

@saint1729 this implementation doesn't cover transaction broadcasting, which would allow other nodes to mine the same transactions.

additionally, a coin issuance hard cap isn't covered here.

increasing the difficulty of the mining just means increasing the difficulty of the proof of work algorithm. this implementation doesn't include a viable proof of work algo.

drewrice2 commented Jan 28, 2018

@saint1729 this implementation doesn't cover transaction broadcasting, which would allow other nodes to mine the same transactions.

additionally, a coin issuance hard cap isn't covered here.

increasing the difficulty of the mining just means increasing the difficulty of the proof of work algorithm. this implementation doesn't include a viable proof of work algo.

@saint1729

This comment has been minimized.

Show comment
Hide comment
@saint1729

saint1729 Jan 31, 2018

@drewrice2 I see. I found a good visual explanation here below.

https://github.com/anders94/blockchain-demo

Are there any similar full fledged basic .java or .py implementations?

@drewrice2 I see. I found a good visual explanation here below.

https://github.com/anders94/blockchain-demo

Are there any similar full fledged basic .java or .py implementations?

@djalmabright

This comment has been minimized.

Show comment
Hide comment
@djalmabright

djalmabright Feb 4, 2018

I can't fix this error in:
line 20, in hash_block
sha.update(str(self.index) + str(self.timestamp) + str(self.data) + str(self.previous_hash))
TypeError: Unicode-objects must be encoded before hashing. ?

I can't fix this error in:
line 20, in hash_block
sha.update(str(self.index) + str(self.timestamp) + str(self.data) + str(self.previous_hash))
TypeError: Unicode-objects must be encoded before hashing. ?

@paulvijaykumarm

This comment has been minimized.

Show comment
Hide comment
@paulvijaykumarm

paulvijaykumarm Feb 8, 2018

@drewrice2 is there a possible solution to perform transactions between two computers with the above code?
If not how can we make transactions over a network? Please let me know. I am able to run the localhost:5000/blocks and locakhost:5000/mine but the curl command doesn't work - I have everything set up on a windows machine.

C:\curl "localhost:5000/txion" -H "Contect-Type: application/json" -d '{"from": "akjflw", "to":"fjlakdj", "amount": 3}'

<title>404 Not Found</title>

Not Found

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

curl: (6) Could not resolve host: akjflw, curl: (3) Port number ended with 'f' curl: (6) Could not resolve host: amount curl: (3) [globbing] unmatched close brace/bracket in column 2

@drewrice2 is there a possible solution to perform transactions between two computers with the above code?
If not how can we make transactions over a network? Please let me know. I am able to run the localhost:5000/blocks and locakhost:5000/mine but the curl command doesn't work - I have everything set up on a windows machine.

C:\curl "localhost:5000/txion" -H "Contect-Type: application/json" -d '{"from": "akjflw", "to":"fjlakdj", "amount": 3}'

<title>404 Not Found</title>

Not Found

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

curl: (6) Could not resolve host: akjflw, curl: (3) Port number ended with 'f' curl: (6) Could not resolve host: amount curl: (3) [globbing] unmatched close brace/bracket in column 2
@nroble

This comment has been minimized.

Show comment
Hide comment
@nroble

nroble Feb 8, 2018

@djalmabright try sha.update(( str(self.index) + str(self.timestamp) + str(self.data) + str(self.previous_hash)).encode('utf-8') )

nroble commented Feb 8, 2018

@djalmabright try sha.update(( str(self.index) + str(self.timestamp) + str(self.data) + str(self.previous_hash)).encode('utf-8') )

@spicyramen

This comment has been minimized.

Show comment
Hide comment
@spicyramen

spicyramen Feb 17, 2018

def hash_block(self):
    """

    :return:
    """
    sha = hasher.sha256()
    sha.update(self.attributes())
    return sha.hexdigest()

def attributes(self):
    return '%s%s%s%s' % (self.index, self.timestamp, self.data, self.previous_hash)

spicyramen commented Feb 17, 2018

def hash_block(self):
    """

    :return:
    """
    sha = hasher.sha256()
    sha.update(self.attributes())
    return sha.hexdigest()

def attributes(self):
    return '%s%s%s%s' % (self.index, self.timestamp, self.data, self.previous_hash)
@hamidabuisa

This comment has been minimized.

Show comment
Hide comment
@hamidabuisa

hamidabuisa Feb 19, 2018

Any thoughts on this error?

C:\Users\HI>curl "localhost:5000/txion" \

<title>405 Method Not Allowed</title>

Method Not Allowed

The method is not allowed for the requested URL.

curl: (6) Could not resolve host: \

Any thoughts on this error?

C:\Users\HI>curl "localhost:5000/txion" \

<title>405 Method Not Allowed</title>

Method Not Allowed

The method is not allowed for the requested URL.

curl: (6) Could not resolve host: \
@DarkNTNT84

This comment has been minimized.

Show comment
Hide comment
@DarkNTNT84

DarkNTNT84 Feb 23, 2018

@paulvijaykumarm, @hamidabuisa
Hi,

When I used the command : curl "localhost:5000/txion" -H "Contect-Type: application/json" -d '{"from": "akjflw", "to":"fjlakdj", "amount": 3}'
The error appeared.

I changed the command and it worked:
image

image
image

DarkNTNT84 commented Feb 23, 2018

@paulvijaykumarm, @hamidabuisa
Hi,

When I used the command : curl "localhost:5000/txion" -H "Contect-Type: application/json" -d '{"from": "akjflw", "to":"fjlakdj", "amount": 3}'
The error appeared.

I changed the command and it worked:
image

image
image

@makeworld-the-better-one

This comment has been minimized.

Show comment
Hide comment
@makeworld-the-better-one

makeworld-the-better-one Feb 24, 2018

Where is the next_block function mentioned in part 1? is it replaced with mining?

makeworld-the-better-one commented Feb 24, 2018

Where is the next_block function mentioned in part 1? is it replaced with mining?

@DarkNTNT84

This comment has been minimized.

Show comment
Hide comment
@DarkNTNT84

DarkNTNT84 Feb 24, 2018

@Cole128,

When you run the command "curl localhost:5000/mine", the new block will be added. In this code, the function "mine" in charge the adding new block.

DarkNTNT84 commented Feb 24, 2018

@Cole128,

When you run the command "curl localhost:5000/mine", the new block will be added. In this code, the function "mine" in charge the adding new block.

@makeworld-the-better-one

This comment has been minimized.

Show comment
Hide comment
@makeworld-the-better-one

makeworld-the-better-one Feb 24, 2018

@DarkNTNT84 thanks, I will actually not have to implement mining, which is why I was looking for the next block function. Thank you!

@DarkNTNT84 thanks, I will actually not have to implement mining, which is why I was looking for the next block function. Thank you!

@makeworld-the-better-one

This comment has been minimized.

Show comment
Hide comment
@makeworld-the-better-one

makeworld-the-better-one Feb 26, 2018

This code doesn't broadcast transactions to all nodes as mentioned in the bitcoin whitepaper, so each block would only contain the transactions for the user and miner of that node, instead of combining transactions from multiple nodes. /transaction should have a GET method which would supply other nodes will all the transactions this node has, which would then be added to this_node_transactions.

This code doesn't broadcast transactions to all nodes as mentioned in the bitcoin whitepaper, so each block would only contain the transactions for the user and miner of that node, instead of combining transactions from multiple nodes. /transaction should have a GET method which would supply other nodes will all the transactions this node has, which would then be added to this_node_transactions.

@moliata

This comment has been minimized.

Show comment
Hide comment
@moliata

moliata Jun 1, 2018

To fix Internal Server Error aka "dict" object has no attribute "index", simply change chain_to_send = blockchain to chain_to_send = blockchain[:]. This will copy the blockchain array. So you wouldn't change the actual blockchain to JSON.

moliata commented Jun 1, 2018

To fix Internal Server Error aka "dict" object has no attribute "index", simply change chain_to_send = blockchain to chain_to_send = blockchain[:]. This will copy the blockchain array. So you wouldn't change the actual blockchain to JSON.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment