def test_trie(): txs = [] for i in range(10): sender = "a" + str(i) receivers = [str(i)] amounts = [str(i + 100)] fee = 100 * i txs.append(Transaction(sender, receivers, amounts, fee)) txs = [tx.hash() for tx in txs] trie = Trie(txs) assert trie.size() == 10 trie_root = trie.root() assert trie_root == "f12419db9e522427fa0fddff624b8d024becb4749fc75018dec2512048a21492"
def test_trie(): txs = [] for i in range(10): sender = "a" + str(i) receivers = [str(i)] amounts = [str(i + 100)] nonce = i fee = 100 * i txs.append(Transaction(sender, receivers, amounts, nonce, fee)) txs = [tx.hash() for tx in txs] trie = Trie(txs) assert trie.size() == 10 trie_root = trie.root() assert trie_root == "819208b7314e74647711cb43a22e518fea7d67541bd691ef7914a368883b43c6"
def __init__(self, prev_main_hash: str, prev_shard_hash: str, miner: str, tx_list: List[Transaction]): self.prev_main_hash = prev_main_hash self.prev_shard_hash = prev_shard_hash self.height = 0 # TODO self.miner = miner self.time = int(time.time() * 1000) self.miner_signature = "" self.miner_nonce = 0 if not tx_list or len(tx_list) == 0: self.tx_list = [] else: self.tx_list = tx_list self.trie = Trie([tx.hash() for tx in self.tx_list]) self.tx_count = self.trie.size() self.tx_merkle_root = self.trie.root() loggerutil.debug("created ShardBlock: " + self.string())
def __init__(self, prev_hash: str, transactions: List[str], miner: str, difficulty: str, nonce: str): self.prev_hash = prev_hash self.transactions = transactions self.miner = miner try: self.height = block_database.get_current_height() + 1 except: self.height = 0 self.difficulty = difficulty self.nonce = nonce self.reward = "00" if not transactions or len(transactions) == 0: self.transactions = [] self.tx_count = 0 self.tx_merkle_root = stringutil.empty_root else: trie = Trie(transactions) self.tx_count = trie.size() self.tx_merkle_root = trie.root() self.time = int(time.time() * 1000.0) loggerutil.debug("created block: " + self.string())
def __init__(self, prev_hash: str, miner: str, difficulty: str, vote_list: List[Vote], shard_list: List[ShardBlock], vtx_list: List[VotingTokenTransaction]): self.prev_hash = prev_hash self.height = 0 # TODO self.miner = miner self.time = int(time.time() * 1000) self.difficulty = difficulty self.nonce = "".join("0" * 16) # init with nonce = 0 if not vote_list or len(vote_list) == 0: self.vote_list = [] else: self.vote_list = vote_list self.vote_trie = Trie([v.hash() for v in self.vote_list]) self.vote_count = self.vote_trie.size() self.vote_merkle_root = self.vote_trie.root() if not shard_list or len(shard_list) == 0: self.shard_list = [] else: self.shard_list = shard_list self.shard_trie = Trie([s.hash() for s in self.shard_list]) self.shard_count = self.shard_trie.size() self.shard_merkle_root = self.shard_trie.root() if not vtx_list or len(vtx_list) == 0: self.vtx_list = [] else: self.vtx_list = vtx_list self.vtx_trie = Trie([vtx.hash() for vtx in self.vtx_list]) self.vtx_count = self.vtx_trie.size() self.vtx_merkle_root = self.vtx_trie.root() self.next_shard_producers = [] loggerutil.debug("created MainBlock: " + self.string())
class ShardBlock(object): """a class representing a shard/sub block in the neutro Blockchain""" fields = [("prev_main_hash", str), ("prev_shard_hash", str), ("height", int), ("time", int), ("miner", str), ("miner_signature", str), ("miner_nonce", str), ("tx_merkle_root", str), ("tx_list", str), ("tx_count", int)] def __init__(self, prev_main_hash: str, prev_shard_hash: str, miner: str, tx_list: List[Transaction]): self.prev_main_hash = prev_main_hash self.prev_shard_hash = prev_shard_hash self.height = 0 # TODO self.miner = miner self.time = int(time.time() * 1000) self.miner_signature = "" self.miner_nonce = 0 if not tx_list or len(tx_list) == 0: self.tx_list = [] else: self.tx_list = tx_list self.trie = Trie([tx.hash() for tx in self.tx_list]) self.tx_count = self.trie.size() self.tx_merkle_root = self.trie.root() loggerutil.debug("created ShardBlock: " + self.string()) def __str__(self) -> str: """returns a json-string of itself without the transaction list""" return self.string() def __hash__(self) -> int: """returns an int as hash of this object""" return int(self.hash(), 16) def __eq__(self, other) -> bool: """ checks for equality this is not 100% correct because theoretically 2 diferent entities could have the same hash, but in that case the whole concept of Blockchain breaks, so this is of no relevance for now """ if not isinstance(other, ShardBlock): return False return self.hash() == other.hash() def string(self, with_transaction_list=False) -> str: """ returns a json-string of itself with_transaction_list is there to store and boradcast a shard-block containing all information """ ret = {} for f in self.fields: ret.update({f[0]: getattr(self, f[0])}) if not with_transaction_list: ret.pop("tx_list", None) else: ret["tx_list"] = [tx.json() for tx in ret["tx_list"]] return stringutil.dict_to_string(ret) def hash(self) -> str: """returns a hex string of the hash of this object""" return hashutil.hash_string(self.string()) def json(self) -> str: """returns a json dict of this object""" return json.loads(self.string(True)) def get_tx_root(self) -> str: """returns the tx_merkle_root of this block""" return self.tx_merkle_root def get_heigth(self) -> int: """returns the height of this block""" return self.height
def test_trie_only_one_tx(): trie = Trie(["00af"]) assert trie.size() == 1 assert trie.root() == "a16a3ddd11708de93ce0cfd391fa9ecf9f55e0524fa76740affd6c41af8ac588"
def test_trie_empty(): trie = Trie() assert trie.size() == 0 assert trie.root() == "f1534392279bddbf9d43dde8701cb5be14b82f76ec6607bf8d6ad557f60f304e"
class MainBlock(object): """class representing a block""" fields = [ ("prev_hash", str), ("height", int), ("time", int), ("miner", str), ("difficulty", str), # TODO ("reward", str), ("nonce", str), ("vote_merkle_root", str), ("vote_list", List[Vote]), ("vote_count", int), ("shard_merkle_root", str), ("shard_list", List[ShardBlock]), ("shard_count", str), ("vtx_merkle_root", str), ("vtx_list", List[VotingTokenTransaction]), ("vtx_count", int), ("next_shard_producers", List[str]) ] def __init__(self, prev_hash: str, miner: str, difficulty: str, vote_list: List[Vote], shard_list: List[ShardBlock], vtx_list: List[VotingTokenTransaction]): self.prev_hash = prev_hash self.height = 0 # TODO self.miner = miner self.time = int(time.time() * 1000) self.difficulty = difficulty self.nonce = "".join("0" * 16) # init with nonce = 0 if not vote_list or len(vote_list) == 0: self.vote_list = [] else: self.vote_list = vote_list self.vote_trie = Trie([v.hash() for v in self.vote_list]) self.vote_count = self.vote_trie.size() self.vote_merkle_root = self.vote_trie.root() if not shard_list or len(shard_list) == 0: self.shard_list = [] else: self.shard_list = shard_list self.shard_trie = Trie([s.hash() for s in self.shard_list]) self.shard_count = self.shard_trie.size() self.shard_merkle_root = self.shard_trie.root() if not vtx_list or len(vtx_list) == 0: self.vtx_list = [] else: self.vtx_list = vtx_list self.vtx_trie = Trie([vtx.hash() for vtx in self.vtx_list]) self.vtx_count = self.vtx_trie.size() self.vtx_merkle_root = self.vtx_trie.root() self.next_shard_producers = [] loggerutil.debug("created MainBlock: " + self.string()) def __str__(self) -> str: """returns a json-string of itself excluding all lists""" return self.string() def __hash__(self) -> int: """returns an int as hash of this object""" return int(self.hash(), 16) def __eq__(self, other) -> bool: """ checks for equality this is not 100% correct because theoretically 2 diferent entities could have the same hash, but in that case the whole concept of Blockchain breaks, so this is of no relevance for now """ if not isinstance(other, MainBlock): return False return self.hash() == other.hash() def string(self, with_vote_list=False, with_shard_list=False, with_vtx_list=False) -> str: """ returns a json-string of itself with_x_list is there to store and broadcast a main-block containing all information """ ret = {} for f in self.fields: ret.update({f[0]: getattr(self, f[0])}) if not with_vote_list: ret.pop("vote_list") else: ret["vote_list"] = [v.json() for v in ret["vote_list"]] if not with_shard_list: ret.pop("shard_list") else: ret["shard_list"] = [s.json() for s in ret["shard_list"]] if not with_vtx_list: ret.pop("vtx_list") else: ret["vtx_list"] = [vtx.json() for vtx in ret["vtx_list"]] return stringutil.dict_to_string(ret) def hash(self) -> str: """returns a hex string of the hash of this object""" return hashutil.hash_string(self.string()) def json(self) -> str: """returns a json dict of this object""" return json.loads(self.string()) def get_prev_hash(self) -> str: return self.prev_hash def get_miner(self) -> str: return self.miner def get_vote_root(self) -> str: return self.vote_merkle_root def get_shard_root(self) -> str: return self.shard_merkle_root def get_vtx_root(self) -> str: return self.vtx_merkle_root