def _mine(self, coin_json, difficulty, simplification, starter_length, starter_limit): data_dump = coins_io.coin_to_string(coin_json) starter = self._build_starter(starter_length) on = 0 while True: if self._stop_marker(): if _Debug: lg.out( _DebugLevel, 'coins_miner._mine STOPPED, stop marker returned True') return None check = starter + str(on) check += data_dump hexdigest = self._build_hash(check) if difficulty == self._get_hash_complexity(hexdigest, simplification): # SOLVED! break on += 1 if on > starter_limit: starter = self._build_starter(starter_length) on = 0 coin_json['miner'].update({ 'hash': hexdigest, 'starter': starter + str(on), 'mined': utime.utcnow_to_sec1970(), }) return coin_json
def build_json_message(data, message_id, message_time=None, sender=None, recipient=None, message_type=None, direction=None, conversation_id=None): if not sender: sender = my_id.getGlobalID(key_alias='master') if not recipient: recipient = my_id.getGlobalID(key_alias='master') if direction is None: if message_type in ['private_message', None, ]: direction = 'out' if sender == my_id.getGlobalID(key_alias='master') else 'in' else: direction = 'in' else: direction = direction.replace('incoming', 'in').replace('outgoing', 'out') new_json = { "payload": { "msg_type": message_type or "message", "message_id": strng.to_text(message_id), "time": message_time or utime.utcnow_to_sec1970(), "data": data, }, 'sender': { 'glob_id': sender, }, 'recipient': { 'glob_id': recipient, }, 'direction': direction, 'conversation_id': conversation_id, } return new_json
def work_on_data_with_known_difficulty(data, difficulty, simplification=2, starter_length=10, starter_limit=99999, stop_marker=None): data['miner'] = my_id.getLocalID() data_dump = json.dumps(data) starter = build_starter(starter_length) on = 0 while True: if stop_marker is not None and callable(stop_marker): if stop_marker(): return None check = starter + str(on) if data is not None: check += data_dump hexdigest = build_hash(check) if difficulty != get_hash_complexity(hexdigest, simplification): on += 1 if on > starter_limit: starter = build_starter(starter_length) on = 0 continue return { "starter": starter + str(on), "hash": hexdigest, "tm": utime.utcnow_to_sec1970(), "data": data, }
def storage_contract_open(customer_idurl, duration, amount, price=1.0, trustee=None): """ + type of resources: storage space (can be also : cpu, traffic, etc.) + amount: in megabytes + duration: seconds + customer: idurl of consumer + supplier: idurl of provider + trustee: idurl of trusted supporter + started: seconds since epoch + price: 1.0 by default """ return { "miner": { "prev": "", }, "payload": { "type": "storage", "amount": amount, "duration": duration, "customer": customer_idurl, "supplier": my_id.getLocalID(), "trustee": trustee, "started": utime.utcnow_to_sec1970(), "price": price, }, }
def build_json_message(data, message_id, message_time=None, sender=None, recipient=None, message_type=None, direction=None): """ """ if not sender: sender = my_id.getGlobalID(key_alias='master') if not recipient: recipient = my_id.getGlobalID(key_alias='master') new_json = { "payload": { "type": message_type or "message", "message_id": strng.to_text(message_id), "time": message_time or utime.utcnow_to_sec1970(), "data": data, }, 'sender': { 'glob_id': sender, }, 'recipient': { 'glob_id': recipient, }, 'direction': direction, } return new_json
def work_with_known_difficulty(coin_json, difficulty, simplification=2, starter_length=10, starter_limit=99999, stop_marker=None, prev_hash=''): coin_json['miner'] = { 'idurl': my_id.getIDURL().to_bin(), } # data_dump = json.dumps(data) data_dump = coins_io.coin_to_string(coin_json) starter = build_starter(starter_length) on = 0 while True: if stop_marker is not None and callable(stop_marker): if stop_marker(): return None check = starter + str(on) # if coin_json is not None: # check += data_dump hexdigest = build_hash(check) if difficulty == get_hash_complexity(hexdigest, simplification): break on += 1 if on > starter_limit: starter = build_starter(starter_length) on = 0 coin_json['miner'].update({ 'hash': hexdigest, 'prev': prev_hash, 'starter': starter + str(on), 'mined': utime.utcnow_to_sec1970(), }) return coin_json
def check_create_rename_key(new_public_key, new_key_id, new_local_key_id): conversation_type = None if new_key_id.startswith('group_'): conversation_type = 'group_message' elif new_key_id.startswith('person_'): conversation_type = 'personal_message' elif new_key_id.startswith('master$'): conversation_type = 'private_message' if conversation_type is None: return False sql = 'SELECT * FROM keys WHERE public_key=?' params = [new_public_key, ] found_public_keys = list(cur().execute(sql, params)) if found_public_keys: if len(found_public_keys) > 1: raise Exception('found multiple records for same public key: %r' % found_public_keys) key_id = found_public_keys[0][0] local_key_id = found_public_keys[0][1] changed = False if key_id != new_key_id: changed = True sql = 'UPDATE keys SET key_id=? WHERE public_key=?' params = [new_key_id, new_public_key, ] cur().execute(sql, params) db().commit() if _Debug: lg.args(_DebugLevel, sql=sql, params=params) if local_key_id != new_local_key_id: changed = True lg.warn('found new public key which is re-using already known key with different local key id: %r -> %r' % ( local_key_id, new_local_key_id, )) update_history_with_new_local_key_id(local_key_id, new_local_key_id) update_conversations_with_new_local_key_id(local_key_id, new_local_key_id) sql = 'UPDATE keys SET local_key_id=? WHERE public_key=?' params = [new_local_key_id, new_public_key, ] cur().execute(sql, params) db().commit() if _Debug: lg.args(_DebugLevel, sql=sql, params=params) return changed # new key will be registered in the "keys" table # also new conversation record can be created if that was a group type # unlike groups, private messages suppose to record a new conversation only when a first message appears and delivered between two sql = 'INSERT INTO keys (key_id, local_key_id, public_key) VALUES (?, ?, ?)' params = [new_key_id, new_local_key_id, new_public_key, ] cur().execute(sql, params) db().commit() if _Debug: lg.args(_DebugLevel, sql=sql, params=params) if conversation_type in ['group_message', 'personal_message', ]: update_conversation( sender_local_key_id=new_local_key_id, recipient_local_key_id=new_local_key_id, payload_type=MESSAGE_TYPES.get(conversation_type, 1), payload_time=utime.utcnow_to_sec1970(), payload_message_id='', ) return True
def publish_customer_supplier_relation(customer_idurl, supplier_idurl=None): if not supplier_idurl: supplier_idurl = my_id.getLocalID() new_data = { 'customer_idurl': customer_idurl, 'supplier_idurl': supplier_idurl, 'time': utime.utcnow_to_sec1970(), 'signature': '', # TODO: add signature and verification methods } return RelationsLookup(customer_idurl, new_data=new_data, publish=True).start()
def storage_contract_accept(prev_coin_json): """ """ return { "miner": { # populate previous hash from existing coin "prev": prev_coin_json['miner']['hash'], }, "payload": { "accepted": utime.utcnow_to_sec1970(), }, }
def doRetreiveCoins(self, arg): """ Action method. """ query = { "method": "get_all", "index": "time", "limit": self.download_limit, "start": utime.datetime_to_sec1970(self.download_offset), "end": utime.utcnow_to_sec1970(), } for idurl in self.connected_accountants: p2p_service.SendRetreiveCoin(idurl, query)
def doRetrieveCoins(self, arg): """ Action method. """ query = { 'method': 'get_many', 'index': 'time_mined', 'limit': self.download_limit, 'start': utime.datetime_to_sec1970(self.download_offset), 'end': utime.utcnow_to_sec1970(), } for idurl in self.connected_accountants: p2p_service.SendRetrieveCoin(idurl, query)
def storage_contract_continue(prev_coin_json, duration): """ """ return { "miner": { # populate previous hash from existing coin "prev": prev_coin_json['miner']['hash'], }, "payload": { "extended": utime.utcnow_to_sec1970(), "duration": duration, }, }
def prepare_broadcast_message(owner, payload): tm = utime.utcnow_to_sec1970() rnd = ''.join(random.choice(string.ascii_uppercase) for _ in range(4)) msgid = '%s:%s:%s' % (tm, rnd, owner) msg = [ ('owner', owner), ('started', tm), ('id', msgid), ('payload', payload), ] owner_sign = key.Sign(key.Hash(str(msg))) msg = {k: v for k, v in msg} msg['owner_sign'] = owner_sign return msg
def publish_customer_supplier_relation(customer_idurl, supplier_idurl=None): if not supplier_idurl: supplier_idurl = my_id.getLocalID() meta_info = contactsdb.get_customer_meta_info(customer_idurl) if _Debug: lg.out(_DebugLevel, 'dht_relations.publish_customer_supplier_relation: customer:%s supplier:%s meta:%s' % ( customer_idurl, supplier_idurl, meta_info, )) new_data = { 'customer_idurl': customer_idurl, 'supplier_idurl': supplier_idurl, 'ecc_map': meta_info.get('ecc_map', None), 'time': utime.utcnow_to_sec1970(), 'signature': '', # TODO: add signature and verification methods } return RelationsLookup(customer_idurl, new_data=new_data, publish=True).start()
def build_json_message(data, message_id, sender=None, recipient=None): """ """ if not sender: sender = my_id.getGlobalID(key_alias='master') if not recipient: recipient = my_id.getGlobalID(key_alias='master') new_json = { "payload": { "type": "message", "message_id": message_id, "time": utime.utcnow_to_sec1970(), "data": data, }, 'sender': { 'glob_id': sender, }, 'recipient': { 'glob_id': recipient, } } return new_json
def storage_contract_open(customer_idurl, duration, amount, price=1.0): """ + signer: idurl of this node. + partner: idurl of given customer + type: sold_storage (can be also : buy_cpu, sell_traffic, buy_hosting, etc.) + duration: seconds + amount: in megabytes + price: 1.0 by default """ coin = { "payload": { "type": "storage", "amount": amount, "price": price, "duration": duration, "customer": customer_idurl, "supplier": my_id.getLocalID(), "started": utime.utcnow_to_sec1970(), }, } return signed_coin(coin)
def _run(self, signed_coin, difficulty, simplification, starter_length, starter_limit): acoin = coins_io.get_coin_base(signed_coin) data_dump = coins_io.coin_to_string(acoin) starter = self._build_starter(starter_length) on = 0 while True: if self._stop_marker(): if _Debug: lg.out(_DebugLevel, "coins_miner._run STOPPED, stop marker returned True") return None check = starter + str(on) check += data_dump hexdigest = self._build_hash(check) if difficulty != self._get_hash_complexity(hexdigest, simplification): on += 1 if on > starter_limit: starter = self._build_starter(starter_length) on = 0 continue result = {"starter": starter + str(on), "hash": hexdigest, "mined": utime.utcnow_to_sec1970()} result.update(signed_coin) return result
def work_on_data_with_known_difficulty( data, difficulty, simplification=2, starter_length=10, starter_limit=99999, stop_marker=None ): data["miner"] = my_id.getLocalID() data_dump = json.dumps(data) starter = build_starter(starter_length) on = 0 while True: if stop_marker is not None and callable(stop_marker): if stop_marker(): return None check = starter + str(on) if data is not None: check += data_dump hexdigest = build_hash(check) if difficulty != get_hash_complexity(hexdigest, simplification): on += 1 if on > starter_limit: starter = build_starter(starter_length) on = 0 continue return {"starter": starter + str(on), "hash": hexdigest, "tm": utime.utcnow_to_sec1970(), "data": data}
def insert_message(data, message_id, message_time=None, sender=None, recipient=None, message_type=None, direction=None): """ Writes JSON message to the message database. """ payload_time = message_time or utime.utcnow_to_sec1970() payload_message_id = strng.to_text(message_id) payload_type = MESSAGE_TYPES.get(message_type, 1) if not sender: sender = my_id.getGlobalID(key_alias='master') if not recipient: recipient = my_id.getGlobalID(key_alias='master') if direction is None: if message_type in [ 'private_message', None, ]: direction = 'out' if sender == my_id.getGlobalID( key_alias='master') else 'in' else: direction = 'in' else: direction = direction.replace('incoming', 'in').replace('outgoing', 'out') if _Debug: lg.args(_DebugLevel, sender=sender, recipient=recipient, typ=payload_type, dir=direction, message_id=payload_message_id) recipient_local_key_id = my_keys.get_local_key_id(recipient) if payload_type in [ 3, 4, ]: sender_local_key_id = recipient_local_key_id else: sender_local_key_id = my_keys.get_local_key_id(sender) if sender_local_key_id is None or recipient_local_key_id is None: lg.err( 'failed to store message because local_key_id is not found, sender=%r recipient=%r' % ( sender_local_key_id, recipient_local_key_id, )) return None cur().execute( '''INSERT INTO history ( sender_local_key_id, sender_id, recipient_local_key_id, recipient_id, direction, payload_type, payload_time, payload_message_id, payload_body ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)''', ( sender_local_key_id, sender, recipient_local_key_id, recipient, 0 if direction == 'in' else 1, payload_type, payload_time, payload_message_id, data, )) db().commit() conversation_id = update_conversation(sender_local_key_id, recipient_local_key_id, payload_type, payload_time, payload_message_id) snap_id = '{}/{}'.format(conversation_id, payload_message_id) message_json = build_json_message( sender=sender, recipient=recipient, direction=direction, conversation_id=conversation_id, message_type=MESSAGE_TYPE_CODES.get(int(payload_type), 'private_message'), message_time=payload_time, message_id=payload_message_id, data=data, ) listeners.push_snapshot('message', snap_id=snap_id, created=payload_time, data=message_json) return message_json