-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0dc70bd
commit c7e1049
Showing
3 changed files
with
119 additions
and
35 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
test_data = [{ | ||
"nonce": "631412", | ||
"index": "0", | ||
"hash": "000002f9c703dc80340c08462a0d6acdac9d0e10eb4190f6e57af6bb0850d03c", | ||
"timestamp": "1508895381", | ||
"prev_hash": "", | ||
"data": "First block data" | ||
}, { | ||
"nonce": "1225518", | ||
"index": "1", | ||
"hash": "00000c575050241e0a4df1acd7e6fb90cc1f599e2cc2908ec8225e10915006cc", | ||
"timestamp": "1508895386", | ||
"prev_hash": | ||
"000002f9c703dc80340c08462a0d6acdac9d0e10eb4190f6e57af6bb0850d03c", | ||
"data": "I block #1" | ||
}, { | ||
"nonce": "1315081", | ||
"index": "2", | ||
"hash": "000003cf81f6b17e60ef1e3d8d24793450aecaf65cbe95086a29c1e48a5043b1", | ||
"timestamp": "1508895393", | ||
"prev_hash": | ||
"00000c575050241e0a4df1acd7e6fb90cc1f599e2cc2908ec8225e10915006cc", | ||
"data": "I block #2" | ||
}, { | ||
"nonce": "24959", | ||
"index": "3", | ||
"hash": "00000221653e89d7b04704d4690abcf83fdb144106bb0453683c8183253fabad", | ||
"timestamp": "1508895777", | ||
"prev_hash": | ||
"000003cf81f6b17e60ef1e3d8d24793450aecaf65cbe95086a29c1e48a5043b1", | ||
"data": "I block #3" | ||
}, { | ||
"nonce": "46053", | ||
"index": "3", | ||
"hash": "000000257df186344486c2c3c1ebaa159e812ca1c5c29947651672e2588efe1e", | ||
"timestamp": "1508961173", | ||
"prev_hash": | ||
"000003cf81f6b17e60ef1e3d8d24793450aecaf65cbe95086a29c1e48a5043b1", | ||
"data": "I block #3" | ||
}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,80 @@ | ||
from app.models.block import Block | ||
from datetime import datetime | ||
import os | ||
from app.managers.common import get_data_directory, calculate_hash | ||
from app.managers.block import save_block | ||
|
||
|
||
def initialize_block(): | ||
# index zero and arbitrary previous hash | ||
block_data = {} | ||
block_data['index'] = 0 | ||
block_data['timestamp'] = datetime.now() | ||
block_data['data'] = 'First block data' | ||
block_data['prev_hash'] = None | ||
block_data['hash'] = calculate_hash( | ||
0, None, block_data['data'], block_data['timestamp'], 0 | ||
) | ||
block = Block(block_data) | ||
return block | ||
|
||
|
||
def save_to_file(): | ||
# blockchain_dir is the directory where the each block is stored locally | ||
# in the file system. | ||
# If the folder does not exist, need to create it. | ||
directory = get_data_directory() | ||
if not os.path.exists(directory): | ||
# make chaindata dir | ||
os.mkdir(directory) | ||
# if the folder is empty, initialise the blockchain. Else add to it.to | ||
# TODO: Add to an existing chain | ||
# TODO: Save block to a file | ||
if os.listdir(directory) == []: | ||
# create first block | ||
first_block = initialize_block() | ||
save_block(first_block) | ||
from app.models.chain import Chain | ||
from static.test import test_data | ||
|
||
block_zero_dir = test_data[0] | ||
block_one_dir = test_data[1] | ||
block_two_dir = test_data[2] | ||
block_three_dir = test_data[3] | ||
block_three_later_in_time_dir = test_data[4] | ||
|
||
########################### | ||
# | ||
# Block time | ||
# | ||
########################### | ||
|
||
block_zero = Block(**block_zero_dir) | ||
another_block_zero = Block(**block_zero_dir) | ||
assert block_zero.is_valid() | ||
assert block_zero == another_block_zero | ||
assert not block_zero != another_block_zero | ||
|
||
block_one = Block(**block_one_dir) | ||
another_block_one = Block(**block_one_dir) | ||
assert block_one.is_valid() | ||
assert block_one == another_block_one | ||
assert not block_one != another_block_one | ||
|
||
block_two = Block(**block_two_dir) | ||
another_block_two = Block(**block_two_dir) | ||
assert block_two.is_valid() | ||
assert block_two == another_block_two | ||
assert not block_two != another_block_two | ||
|
||
block_three = Block(**block_three_dir) | ||
another_block_three = Block(**block_three_dir) | ||
assert block_three.is_valid() | ||
assert block_three == another_block_three | ||
assert not block_three != another_block_three | ||
|
||
##################################### | ||
# | ||
# Bringing Chains into play | ||
# | ||
##################################### | ||
blockchain = Chain([block_zero, block_one, block_two]) | ||
assert blockchain.is_valid() | ||
assert len(blockchain) == 3 | ||
|
||
empty_chain = Chain([]) | ||
assert len(empty_chain) == 0 | ||
empty_chain.add_block(block_zero) | ||
assert len(empty_chain) == 1 | ||
empty_chain = Chain([]) | ||
assert len(empty_chain) == 0 | ||
|
||
another_blockchain = Chain([block_zero, block_one, block_two]) | ||
assert another_blockchain.is_valid() | ||
assert len(another_blockchain) == 3 | ||
|
||
print another_blockchain, blockchain | ||
|
||
assert blockchain == another_blockchain | ||
assert not blockchain != another_blockchain | ||
assert blockchain <= another_blockchain | ||
assert blockchain >= another_blockchain | ||
assert not blockchain > another_blockchain | ||
assert not another_blockchain < blockchain | ||
|
||
blockchain.add_block(block_three) | ||
assert blockchain.is_valid() | ||
assert len(blockchain) == 4 | ||
assert not blockchain == another_blockchain | ||
assert blockchain != another_blockchain | ||
assert not blockchain <= another_blockchain | ||
assert blockchain >= another_blockchain | ||
assert blockchain > another_blockchain | ||
assert another_blockchain < blockchain | ||
|
||
blockchain.save() |