Skip to content
Open

Mine #92

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ verify_ssl = true

[dev-packages]
flake8 = "*"
autopep8 = "*"
pylint = "*"

[packages]
requests = ">=2.20.0"
Expand Down
115 changes: 105 additions & 10 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions basic_block_gp/blank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# block chain note?
# holds info for ledgers of data/ transactions etc..
# allows us once given data structure making it unable to be broken down easily and adapted as each step needs the previous step's info as part of the next block.j

# like a linked list... each block has a relationship to next block...

# EACH BLOCK

# NEEDS TO STORE:
# INDEX
# TIME STAMP-WHEN CREATED
# WHAT IT'S STORING- AKA .VALUE OR TRANSACTION
# PROOF= a number... an important one... "proof of work" is proof that we spent the time doing the labor of mining the blocks... hash with a certain amount of zeros at the start.
# "block string" + "proof (aka some number)" and take that and hash it... making the proof of work = 000A1L6.....
# THE HASH OF THE PREVIOUS BLOCK
# NEEDS THE ITEMS WITH 3# MOST


# the longest chain is the winner and its a race to get there
63 changes: 51 additions & 12 deletions basic_block_gp/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def __init__(self):
self.current_transactions = []

# Create the genesis block
# created different since it has no previous block to pull data for the proof
self.new_block(previous_hash=1, proof=100)

def new_block(self, proof, previous_hash=None):
Expand All @@ -32,12 +33,19 @@ def new_block(self, proof, previous_hash=None):

block = {
# TODO
'index': len(self.chain) + 1,
'timestamp': time(),
'transactions': self.current_transactions,
'proof': proof,
'previous_hash': previous_hash or self.hash(self.last_block),
}

# Reset the current list of transactions
self.current_transactions = []
# Append the chain to the block
self.chain.append(block)
# Return the new block
pass
return block

def hash(self, block):
"""
Expand All @@ -46,7 +54,6 @@ def hash(self, block):
:param block": <dict> Block
"return": <str>
"""

# Use json.dumps to convert json into a string
# Use hashlib.sha256 to create a hash
# It requires a `bytes-like` object, which is what
Expand All @@ -56,17 +63,21 @@ def hash(self, block):
# or we'll have inconsistent hashes

# TODO: Create the block_string
string_object = json.dumps(block, sort_keys=True)
# sort_keys = True will allow the dict to be ordered the same way each time.... big deal for this usage
block_string = string_object.encode()

# TODO: Hash this string using sha256
raw_hash = hashlib.sha256(block_string)

# By itself, the sha256 function returns the hash in a raw string
# that will likely include escaped characters.
# This can be hard to read, but .hexdigest() converts the
# hash to a string of hexadecimal characters, which is
# easier to work with and understand

hash_string = raw_hash.hexdigest()
# TODO: Return the hashed block string in hexadecimal format
pass
return hash_string

@property
def last_block(self):
Expand All @@ -80,9 +91,14 @@ def proof_of_work(self, block):
in an effort to find a number that is a valid proof
:return: A valid proof for the provided block
"""
# TODO
pass
# return proof
block_string = json.dumps(block, sort_keys=True)

proof = 0

while self.valid_proof(block_string, proof) == False:
proof += 1

return proof

@staticmethod
def valid_proof(block_string, proof):
Expand All @@ -97,8 +113,14 @@ def valid_proof(block_string, proof):
:return: True if the resulting hash is a valid proof, False otherwise
"""
# TODO
pass
# return True or False
print(f"is {proof} valid?")
guess = block_string + str(proof)
guess = guess.encode()

hash_value = hashlib.sha256(guess).hexdigest()
print(hash_value)

return hash_value[:3] == '000'


# Instantiate our Node
Expand All @@ -111,14 +133,30 @@ def valid_proof(block_string, proof):
blockchain = Blockchain()


@app.route('/', methods=['GET'])
def hello_world():
response = {
'text': 'hello World'
}
return jsonify(response), 200


@app.route('/mine', methods=['GET'])
def mine():
# Run the proof of work algorithm to get the next proof
# Run the proof of work algorithm to get the next proof'

print("we shall now mine a block!")

proof = blockchain.proof_of_work(blockchain.last_block)

print(f"after a long process... here it is... {proof}")

# Forge the new Block by adding it to the chain with the proof

new_block = blockchain.new_block(proof)

response = {
# TODO: Send a JSON response with the new block
'block': new_block,
}

return jsonify(response), 200
Expand All @@ -127,7 +165,8 @@ def mine():
@app.route('/chain', methods=['GET'])
def full_chain():
response = {
# TODO: Return the chain and its current length
'len': len(blockchain.chain),
'chain': blockchain.chain
}
return jsonify(response), 200

Expand Down
23 changes: 23 additions & 0 deletions basic_wallet_p/frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
Loading