def set_message_done(address, channel_name): message = Session.query(MessageDatabase).filter(MessageDatabase.address == address).\ filter(MessageDatabase.channel_name == channel_name).all() if message: message[0].state = "done" Session.commit() return None
def add_channel_to_database(self, sender, receiver, state, sender_deposit, receiver_deposit, open_block_number, settle_timeout, sender_deposit_cache, receiver_deposit_cache, contract_address="", contract_hash="", tx_id="", start_block_number=0): channel_state = ChannelDatabase( receiver=receiver, sender=sender, channel_name=self.channelname, state=state.value, sender_deposit=sender_deposit, receiver_deposit=receiver_deposit, open_block_number=open_block_number, settle_timeout=settle_timeout, sender_deposit_cache=sender_deposit_cache, receiver_deposit_cache=receiver_deposit_cache, start_block_number=start_block_number, contract_address=contract_address, contract_hash=contract_hash) try: Session.add(channel_state) Session.commit() except: raise ChannelDBAddFail return None
def delete_address(self, address): try: Session.query(ChannelAddrDataBase).filter( ChannelAddrDataBase.address == address).delete() Session.commit() except: raise return None
def push_message(address, type, message, channel_name): m = MessageDatabase(address=address, type=type, message=message, channel_name=channel_name, state="pending") Session.add(m) Session.commit()
def query_channel_name(sender, receiver): channel1 = Session.query(ChannelDatabase).filter(ChannelDatabase.sender == sender). \ filter(ChannelDatabase.receiver == receiver).filter(ChannelDatabase.state !=5).all() channel2 = Session.query(ChannelDatabase). \ filter(ChannelDatabase.sender == receiver). \ filter(ChannelDatabase.receiver == sender).filter(ChannelDatabase.state !=5).all() channel = channel1 if channel1 else channel2 return channel[0].channel_name if channel else None
def delete_channel_in_database(self): try: Session.query(ChannelDatabase).filter( ChannelDatabase.channel_name == self.channelname).delete() Session.commit() except: raise return None
def pull_message(address): message_info = {} messages = Session.query(MessageDatabase).filter(MessageDatabase.address == address). \ filter(MessageDatabase.state == "pending").all() for m in messages: message_info.setdefault("channel_name", m.channel_name) message_info.setdefault("raw_tx", m.message) Session.commit() return message_info
def update_receiver_signature(self, signature): try: ch = Session.query(ChannelDatabase).filter( ChannelDatabase.channel_name == self.channelname).one() ch.receiversignature = signature Session.commit() return True except: raise ChannelDBUpdateFail
def update_txid(self, tx_id): try: ch = Session.query(ChannelDatabase).filter( ChannelDatabase.channel_name == self.channelname).one() ch.tx_id = tx_id Session.commit() return True except: raise ChannelDBUpdateFail
def update_channel_state(self, state): try: ch = Session.query(ChannelDatabase).filter( ChannelDatabase.channel_name == self.channelname).one() ch.state = state.value Session.commit() return True except: raise ChannelDBUpdateFail
def update_channel_to_database(self, **kwargs): self.qeury_channel() try: for key, value in kwargs.items(): setattr(self.match, key, value) Session.commit() except: raise ChannelDBUpdateFail return None
def update_channel_deposit(self, sender_deposit, receiver_deposit): try: ch = Session.query(ChannelDatabase).filter( ChannelDatabase.channel_name == self.channelname).one() ch.sender_deposit = sender_deposit ch.receiver_deposit = receiver_deposit Session.commit() return True except: raise ChannelDBUpdateFail
def update_address(self, address, ip, port, public_key="NULL"): try: Session.merge( ChannelAddrDataBase(address=address, ip=ip, port=port, public_key=public_key)) Session.commit() except: raise ChannelDBUpdateFail return None
def query_channel_from_address(address, role="both"): if role not in ("both", "sender", "receiver"): raise QureyRoleNotCorrect if role == "sender": return Session.query(ChannelDatabase).filter( ChannelDatabase.sender == address).all() elif role == "receiver": return Session.query(ChannelDatabase).filter( ChannelDatabase.receiver == address).all() else: result = Session.query(ChannelDatabase).filter( ChannelDatabase.sender == address).all() result.extend( Session.query(ChannelDatabase).filter( ChannelDatabase.receiver == address).all()) return result
def receiver_signature(self): try: ch = Session.query(ChannelDatabase).filter( ChannelDatabase.channel_name == self.channelname).one() return ch.receiversignature except: return None
def qeury_channel(self): try: self.match = Session.query(ChannelDatabase).filter( ChannelDatabase.channel_name == self.channelname).one() return True except: return False
def query_address(self, address): try: result = Session.query(ChannelAddrDataBase).filter( ChannelAddrDataBase.address == address).one() except: return None return result
def get_publickey(address): try: result = Session.query(ChannelAddrDataBase).filter( ChannelAddrDataBase.address == address).one() except: return None return result.public_key
def add_address(self, address, ip="NULL", port="NULL", public_key="NULL"): try: if self.query_address(address): print("query_address get %s" % address) Session.merge( ChannelAddrDataBase(address=address, ip=ip, port=port, public_key=public_key)) else: Session.add( ChannelAddrDataBase(address=address, ip=ip, port=port, public_key=public_key)) Session.commit() except: raise ChannelDBAddFail return None