def m_tree(p_list: list): if len(p_list) == 0: return b'' if len(p_list) == 1: return p_list[0] sub_tree = [] for i in chunks(p_list, 2): if len(i) == 2: sub_tree.append(sha3_hex(i[0] + i[1])) else: sub_tree.append(sha3_hex(i[0] + i[0])) return m_tree(sub_tree)
def set_code(self, address, code): validate_contract(address) code = validate_code(code) account = self._get_account(address) hashcode = sha3_hex(code) self._code_cache[hashcode] = code self._db.put(hashcode, code) self._set_account(address, account.copy(code=hashcode))
async def process_event(self, channel, body, envelope, properties): # TODO: sender in validators. how to check validators? obj = json.loads(body) height, sender, blk_hash, sig = obj confirm_set = [height, sender, blk_hash] confirm_hash = sha3_hex(','.join(confirm_set)) await verify(confirm_hash, sig.encode(), sender.encode()) self.storage[(int(height), sender.encode())] = blk_hash.encode()
def make_confirm_from_vote(height, aggregate_vote_blk, chain: BaseChain): if chain.is_validator: confirm_set = [str(height), chain.nodebase.decode(), aggregate_vote_blk.decode()] sig = chain.make_signature(sha3_hex(','.join(confirm_set))) confirm_set.append(sig.decode()) return confirm_set
def create_nodebase(public_key: bytes): """ create account address :param public_key: (bytes) public key :return: (bytes) account address >>> create_nodebase(b"~\xe2\x15(\x07\x02?l\xd8\x87...") >>> return b'gBx7dfae3...' """ address = sha3_hex(public_key)[-40:] prefix_address = b''.join((O_COIN_TYPE, address)) assert len(prefix_address) == ADDRESS_SIZE return prefix_address
def create_contract(node_base, nonce): """ create contract address :param node_base: (bytes) account address :param nonce: (int) account nonce :return: (bytes) contract address >>> create_contract(b'gBx7dfae3...', 45) >>> return b'gBca8fed...' """ address = sha3_hex(b''.join((node_base, str(nonce).encode())))[-40:] prefix_address = b''.join((CONTRACT_TYPE, address)) assert len(prefix_address) == ADDRESS_SIZE return prefix_address
def set_delegated(self, address, to, value): hash_key = sha3_hex(b''.join((address, to))) account = self._get_account(address) account_to = self._get_account(address) delegated = account.delegated delegated_stake = account_to.delegated if hash_key not in account.delegated: delegated.append(hash_key) if hash_key not in account_to.delegated: delegated.append(hash_key) self._set_account(address, account.copy(delegated=delegated)) self._set_account(to, account_to.copy(delegated=delegated_stake)) self.compute_balance(address, -1 * value) self.compute_stake_balance(to, value) self._set_delegated(hash_key, (address, to, value))
def pre_hash(self): setup = self.__slots__[:7] attr = self.serialize(setup) return sha3_hex(','.join(attr).encode())
def _set_node(self, node): raw_node = self.serialize(node) key = sha3_hex(raw_node) self.cache[key] = raw_node return key
def _set_root(self, node): raw_node = self.serialize(node) key = sha3_hex(raw_node) self.cache[key] = raw_node self.root = key return key
def get_trie_key(value: bytes): return sha3_hex(value).decode()