def add_key(self, target, value, height): assert len(target) == KEYLENGTH path = self.get_path(target, new=True) if path is True: return #print "add key: target", target.encode('hex'), "path", map(lambda x: x.encode('hex'), path) parent = path[-1] parent_node = self.get_node(parent) n = len(parent) c = target[n] if parent_node.has(c): h, v = parent_node.get(c) skip = self.get_skip(parent + c) child = parent + c + skip assert not target.startswith(child) prefix = self.common_prefix(child, target) index = len(prefix) if len(child) == KEYLENGTH: # if it's a leaf, get hash and value of new_key from parent d = Node.from_dict({ target[index]: (None, 0), child[index]: (h, v) }) else: # if it is not a leaf, update its hash because skip_string changed child_node = self.get_node(child) h, v = child_node.get_hash(child, prefix) d = Node.from_dict({ target[index]: (None, 0), child[index]: (h, v) }) self.set_skip(prefix + target[index], target[index + 1:]) self.set_skip(prefix + child[index], child[index + 1:]) self.put_node(prefix, d) path.append(prefix) self.parents[child] = prefix # update parent skip new_skip = prefix[n + 1:] self.set_skip(parent + c, new_skip) parent_node.set(c, None, 0) self.put_node(parent, parent_node) else: # add new letter to parent skip = target[n + 1:] self.set_skip(parent + c, skip) parent_node.set(c, None, 0) self.put_node(parent, parent_node) # write the new leaf s = (int_to_hex8(value) + int_to_hex4(height)).decode('hex') self.db_utxo.put(target, s) # the hash of a leaf is the txid _hash = target[20:52] self.update_node_hash(target, path, _hash, value)
def add_key(self, target, value, height): assert len(target) == KEYLENGTH path = self.get_path(target, new=True) if path is True: return #print "add key: target", target.encode('hex'), "path", map(lambda x: x.encode('hex'), path) parent = path[-1] parent_node = self.get_node(parent) n = len(parent) c = target[n] if parent_node.has(c): h, v = parent_node.get(c) skip = self.get_skip(parent + c) child = parent + c + skip assert not target.startswith(child) prefix = self.common_prefix(child, target) index = len(prefix) if len(child) == KEYLENGTH: # if it's a leaf, get hash and value of new_key from parent d = Node.from_dict({ target[index]: (None, 0), child[index]: (h, v) }) else: # if it is not a leaf, update its hash because skip_string changed child_node = self.get_node(child) h, v = child_node.get_hash(child, prefix) d = Node.from_dict({ target[index]: (None, 0), child[index]: (h, v) }) self.set_skip(prefix + target[index], target[index+1:]) self.set_skip(prefix + child[index], child[index+1:]) self.put_node(prefix, d) path.append(prefix) self.parents[child] = prefix # update parent skip new_skip = prefix[n+1:] self.set_skip(parent+c, new_skip) parent_node.set(c, None, 0) self.put_node(parent, parent_node) else: # add new letter to parent skip = target[n+1:] self.set_skip(parent+c, skip) parent_node.set(c, None, 0) self.put_node(parent, parent_node) # write the new leaf s = (int_to_hex8(value) + int_to_hex4(height)).decode('hex') self.db_utxo.put(target, s) # the hash of a leaf is the txid _hash = target[20:52] self.update_node_hash(target, path, _hash, value)