def as_str(self) -> str: if len(self.data) % 2 != 0: raise UTXOError( 'Length of seqNo-amount pairs must be even: items={}'.format( len(self.data))) return ':'.join(self.data)
def as_output_list(self) -> List[Output]: if len(self.data) % 2 != 0: raise UTXOError('Length of seqNo-amount pairs must be even: items={}'.format(len(self.data))) rtn = [] for i in range(0, len(self.data), 2): try: seq_no = int(self.data[i]) amount = int(self.data[i + 1]) except ValueError: raise UTXOError("Invalid data -- not integers -- seq_no:{} amount:{}".format(self.data[i], self.data[i + 1])) rtn.append(Output(self.address, seq_no, amount)) return rtn
def add_amount(self, seq_no: int, amount: int): if not isinstance(seq_no, int) or not isinstance(amount, int): raise UTXOError("Adding invalid types -- seqNo:{} amount:{}".format(seq_no, amount)) logger.debug('adding seq_no: address {} - seq_no: {} - amount: {}'.format(self.address, seq_no, amount)) self.data.append(str(seq_no)) self.data.append(str(amount))
def __init__(self, address: str, data=None): self.address = address if data: if isinstance(data, (bytes, bytearray)): data = data.decode() if not isinstance(data, str): raise UTXOError("Items are not valid string -- '{}'".format(str(data))) split = data.split(self.DELIMITER) if not len(split) % 2 == 0: # test if even raise UTXOError("Stored seqNo-amount pairs is not even") self.data = split else: self.data = []
def remove_seq_no(self, seq_no: int): logger.debug( 'removing seq_no -- address:{} - seq_no: {} - data: {}'.format( self.address, seq_no, str(self.data))) seq_no_str = str(seq_no) # Shortens the passed list `seq_nos_amounts` by 2 items for i in range(0, len(self.data), 2): if self.data[i] == seq_no_str: break else: err_msg = "seq_no {} is not found is list of seq_nos_amounts for address -- current list: {}".format( seq_no_str, self.data) raise UTXONotFound(err_msg) if i < len(self.data) and (i + 1) < len(self.data): self.data.pop(i) # List has changed length, value at index `i+1` has moved to index `i` self.data.pop(i) else: raise UTXOError("Unable to remove seq_no from address")
def _is_valid_output(output: Output): if not isinstance(output, Output): raise UTXOError("Output is invalid object type")