-
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
1a24d02
commit 30aa5a3
Showing
5 changed files
with
156 additions
and
22 deletions.
There are no files selected for viewing
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
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,21 @@ | ||
def getter_setter_gen(name, type_): | ||
def getter(self): | ||
return getattr(self, "__" + name) | ||
|
||
def setter(self, value): | ||
if not isinstance(value, type_): | ||
raise TypeError( | ||
"%s attribute must be set to an instance of %s" % (name, type_) | ||
) | ||
setattr(self, "__" + name, value) | ||
return property(getter, setter) | ||
|
||
|
||
def auto_attr_check(cls): | ||
new_dct = {} | ||
for key, value in cls.__dict__.items(): | ||
if isinstance(value, type): | ||
value = getter_setter_gen(key, value) | ||
new_dct[key] = value | ||
# Creates a new class, using the modified dictionary as the class dict: | ||
return type(cls)(cls.__name__, cls.__bases__, new_dct) |
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
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,78 @@ | ||
from block import Block | ||
|
||
|
||
class Chain: | ||
def __init__(self, blocks): | ||
self.blocks = blocks | ||
|
||
def is_valid(self): | ||
''' | ||
Is a valid blockchain if | ||
1) Each block is indexed one after the other | ||
2) Each block's prev hash is the hash of the prev block | ||
3) The block's hash is valid for the number of zeros | ||
''' | ||
for index, cur_block in enumerate(self.blocks[1:]): | ||
prev_block = self.blocks[index] | ||
if prev_block.index + 1 != cur_block.index: | ||
return False | ||
if not cur_block.is_valid(): | ||
return False | ||
if prev_block.hash != cur_block.prev_hash: | ||
return False | ||
return True | ||
|
||
def save(self): | ||
''' | ||
Save the blockchain to the file system | ||
''' | ||
for block in self.blocks: | ||
block.save() | ||
|
||
def find_block_by_hash(self, b_hash): | ||
for block in self.blocks: | ||
if block.hash == b_hash: | ||
return block | ||
return None | ||
|
||
def find_block_by_index(self, index): | ||
if len(self) <= index: | ||
return self.blocks[index] | ||
else: | ||
return False | ||
|
||
def __len__(self): | ||
return len(self.blocks) | ||
|
||
def __gt__(self, other): | ||
return len(self) > len(other) | ||
|
||
def __ge__(self, other): | ||
return self.__eq__(other) or self.__gt__(other) | ||
|
||
def __eq__(self, other): | ||
if len(self) != len(other): | ||
return False | ||
for self_block, other_block in zip(self.blocks, other.blocks): | ||
if self_block != other_block: | ||
return False | ||
return True | ||
|
||
def max_index(self): | ||
''' | ||
We're assuming a valid chain. Might change later | ||
''' | ||
return self.blocks[-1].index | ||
|
||
def add_block(self, new_block): | ||
''' | ||
Put the new block into the index that the block is asking. | ||
That is, if the index is of one that currently exists, the new block | ||
would take it's place. Then we want to see if that block is valid. | ||
If it isn't, then we ditch the new block and return False. | ||
''' | ||
self.blocks.append(new_block) | ||
return True | ||
|
||
def block_list_dict(self): | ||
return [b.to_dict() for b in self.blocks] |
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,2 +1,3 @@ | ||
blockchain_dir = 'data' | ||
NUM_ZEROS = 4 | ||
broadcast_blockchain_dir = 'broadcast' | ||
num_zeores = 5 |