def regist_channel(sender_addr, receiver_addr, asset_type,deposit, open_blockchain): """ This func is to register channel :param sender_addr: String, the sender address :param receiver_addr: String, receiver's address :param asset_type: String, asset type should be symbol name , asset id should be configured in the configure.json :param deposit: String, depoist number in block chain :param open_blockchain: Open block chain number :return: Dictionary, {"channel_name":channel_name, "trad_info":raw_tans} """ channel = Channel(sender_addr,receiver_addr) if channel.find_channel(): return {"error": "channel already exist"} else: raw_tans,state = blockchain.NewTransection(asset_type, sender_addr, Contract_addr, int(deposit)) if state: channel_name = channel.create(sender_deposit=int(deposit),reciever_deposit=0, open_block_number=int(open_blockchain),settle_timeout=10) return {"channel_name": channel_name, "trad_info": raw_tans} else: return {"channel_name":None, "trad_info":raw_tans}
def update_deposit(address, channel_name, asset_type, value): """ :param address: String , address :param channel_name: String, channel name :param asset_type: String, asset type :param value: String , the deposit number :return: {"channel_name": channel.channel_name, "trad_info": raw_tans} """ sender, receiver = split_channel_name(channel_name) channel = Channel(sender, receiver) if channel.sender == address: if channel.state_in_database != State.OPEN: return {"channel_name": channel.channel_name, "trad_info": "Channel exist but in state %s" % str(State(channel.state_in_database))} else: channel.update_channel_to_database(sender_deposit_cache=float(value)) raw_tans ,state= blockchain.NewTransection(asset_type, address, Contract_addr, value) if state: channel.update_channel_state(state = State.UPDATING) elif channel.receiver == address: channel.update_channel_to_database(receiver_deposit_cache=float(value)) raw_tans, state = blockchain.NewTransection(asset_type, address, Contract_addr, value) if state: channel.update_channel_state(state=State.UPDATING) else: return {"error":"channel name not match the address"} return {"channel_name": channel.channel_name, "trad_info": raw_tans}
def get_channel_state(address): """ :param sender_addr: :param receiver_addr: :return: """ channel_infos = [] channels = get_channelnames_via_address(address) for channel in channels: ch = Channel(channel.sender, channel.receiver) channel_detail = [{ "address": ch.sender, "deposit": ch.sender_deposit, "balance": ch.get_address_balance(ch.sender) }, { "address": ch.receiver, "deposit": ch.receiver_deposit, "balance": ch.get_address_balance(ch.receiver) }] channel_info = { "channel_name": ch.channel_name, "channel_state": str(State(ch.stateinDB)), "open_block": ch.open_block_number, "tx_info": channel_detail } channel_infos.append(channel_info) return channel_infos
def test03_sender_to_receiver(self): self.channel.sender_to_receiver(5) channel_info = self.channel.read_channel() channel = Channel.from_dict(channel_info[-1]) self.assertEqual(channel.balance, self.deposit - 5) self.channel.sender_to_receiver(5) channel_info = self.channel.read_channel() channel = Channel.from_dict(channel_info[-1]) self.assertEqual(channel.balance, self.deposit - 10)
def close_channel(sender_addr, receiver_addr, channel_name): """ :param sender_addr: String, the sender address :param receiver_addr: String, receiver's address :param channel_name: String, channel name :return: """ sender, receiver = split_channel_name(channel_name) ch = Channel(sender, receiver) return ch.settle_balance_onchain()
def close_channel(sender_addr, receiver_addr,channel_name): """ :param sender_addr: String, the sender address :param receiver_addr: String, receiver's address :param channel_name: String, channel name :return: """ sender, receiver = split_channel_name(channel_name) ch = Channel(sender, receiver) ch.update_channel_state(State.SETTLING) return ch.settle_banlace_onblockchain()
def depoistout(address, value): channels = get_channelnames_via_address(address) success_channel = [] for channel in channels: if channel.state == State.SETTLING.value: sender, receiver = split_channel_name(channel.channel_name) ch = Channel(sender, receiver) ch.close() success_channel.append(channel.channel_name) else: continue
def close_channel(sender_addr, receiver_addr, channel_name): """ :param sender_addr: :param receiver_addr: :param channel_name: :return: """ sender, receiver = split_channel_name(channel_name) ch = Channel(sender, receiver) return ch.close()
def setUpClass(cls): cls.sender = "0x123456789" cls.receiver = "0x34567890" cls.deposit = 100 cls.open_block_number = 100 cls.channel = Channel(cls.sender, cls.receiver, cls.deposit, cls.open_block_number)
def sender_to_receiver(sender_addr, receiver_addr, channel_name, asset_type, count): """ :param sender_addr: String, the sender address :param receiver_addr: String, receiver's address :param channel_name: String, channel name :param asset_type: String, asset type should be symbol name , asset id should be configured in the configure.json :param count: String, depoist number in block chain :return: """ sender,receiver = split_channel_name(channel_name) ch = Channel(sender,receiver) if sender_addr == ch.sender and receiver_addr == ch.receiver: return ch.sender_to_receiver(float(count)) elif receiver_addr == ch.sender and sender_addr == ch.receiver: return ch.receiver_to_sender(float(count)) else: return {"error":"Address and Channelname not match"}
def sender_to_receiver(sender_addr, receiver_addr, channel_name, asset_type, count): """ :param sender_addr: :param receiver_addr: :param channel_name: :param asset_type: :param count: :return: """ sender, receiver = split_channel_name(channel_name) ch = Channel(sender, receiver) if sender_addr == ch.sender and receiver_addr == ch.receiver: return ch.sender_to_receiver(int(count)) elif receiver_addr == ch.sender and sender_addr == ch.receiver: return ch.receiver_to_sender(int(count)) else: return {"error": "Address and Channelname not match"}
def send_raw_transaction(sender_address, channel_name, hex): """ :param sender_address: :param channel_name: :param hex: :return: """ blockchain.send_raw_transection(hex) sender, receiver = split_channel_name(channel_name) ch = Channel(sender, receiver) sender_deposit = ch.sender_deposit receiver_deposit = ch.receiver_deposit sender_cache = ch.sender_deposit_cache receiver_cache = ch.receiver_deposit_cache ch.update_channel_deposit(sender_deposit=sender_deposit + sender_cache, receiver_deposit=receiver_deposit + receiver_cache) return ch.set_channel_open()
def get_channel_state(address): """ :param sender_addr: String, the sender address :param receiver_addr: String, receiver's address :return: Dictionary, the chnnnel information """ channel_infos =[] channels = get_channelnames_via_address(address) for channel in channels: channel_detail =[] ch = Channel(channel.sender, channel.receiver) sender_info = {"address": ch.sender, "deposit": ch.sender_deposit, "balance": ch.get_address_balance(ch.sender) if ch.get_address_balance(ch.sender) else 0} recevier_info = {"address": ch.receiver, "deposit": ch.receiver_deposit, "balance": ch.get_address_balance(ch.receiver) if ch.get_address_balance(ch.receiver) else 0} if ch.sender == address: channel_detail.append(sender_info) channel_detail.append(recevier_info) else: channel_detail.append(recevier_info) channel_detail.append(sender_info) channel_info = { "channel_name": ch.channel_name, "channel_state": str(State(ch.stateinDB)), "open_block":ch.open_block_number, "tx_info": channel_detail } channel_infos.append(channel_info) return channel_infos
def update_deposit(address, channel_name, asset_type, value): """ :param address: :param channel_name: :param asset_type: :param value: :return: """ sender, receiver = split_channel_name(channel_name) channel = Channel(sender, receiver) if channel.sender == address: if channel.state_in_database != State.OPEN: return { "channel_name": channel.channel_name, "trad_info": "Channel exist but in state %s" % str(State(channel.state_in_database)) } else: raw_tans = blockchain.NewTransection(asset_type, address, Contract_addr, int(value)) channel.update_channel_to_database(sender_deposit_cache=int(value)) elif channel.receiver == address: raw_tans = blockchain.NewTransection(asset_type, address, Contract_addr, int(value)) channel.update_channel_to_database(receiver_deposit_cache=int(value)) else: return {"error": "channel name not match the address"} return {"channel_name": channel.channel_name, "trad_info": raw_tans}
def regist_channel(sender_addr, receiver_addr, asset_type, deposit, open_blockchain): """ :param sender_addr: :param receiver_addr: :param asset_type: :param deposit: :param open_blockchain: :return: """ channel = Channel(sender_addr, receiver_addr) if channel.find_channel(): return {"error": "channel already exist"} else: channel_name = channel.create(sender_deposit=int(deposit), reciever_deposit=0, open_block_number=int(open_blockchain), settle_timeout=10) raw_tans = blockchain.NewTransection(asset_type, sender_addr, Contract_addr, int(deposit)) return {"channel_name": channel_name, "trad_info": raw_tans}
def depoist_out(address, value): channel_name = get_channelnames_via_address(address) if channel_name: sender, receiver = split_channel_name(channel_name) ch = Channel(sender, receiver) ch.delete_channle_in_database() ch.delete_channel() else: return None return address
def depoist_in(address, value): channel_name = get_channelnames_via_address(address) if channel_name: sender, receiver = split_channel_name(channel_name) ch = Channel(sender, receiver) if address == sender and ch.stateinDB == State.OPENING and value == ch.sender_deposit_cache: ch.update_channel_state(State.OPEN) elif address == receiver and ch.stateinDB == State.OPENING and value == ch.receiver_deposit_cache: ch.update_channel_state(State.OPEN) else: return None else: return None return address
def depositin(address, value): channels = get_channelnames_via_address(address) success_channel = [] for channel in channels: print(channel.channel_name) sender, receiver = split_channel_name(channel.channel_name) ch = Channel(sender, receiver) # if address == sender and value == ch.sender_deposit_cache: if address == sender: ch.set_channel_open() success_channel.append(channel.channel_name) # elif address == receiver and value == ch.receiver_deposit_cache: elif address == receiver: ch.set_channel_open() success_channel.append(channel.channel_name) else: continue
def _get_channel_states_info(address): channel_infos = [] channels = get_channelnames_via_address(address) for channel in channels: channel_detail = [] ch = Channel(channel.sender, channel.receiver) sender_info = { "address": ch.sender, "deposit": ch.sender_deposit, "balance": ch.get_address_balance(ch.sender) if ch.get_address_balance(ch.sender) else 0 } recevier_info = { "address": ch.receiver, "deposit": ch.receiver_deposit, "balance": ch.get_address_balance(ch.receiver) if ch.get_address_balance(ch.receiver) else 0 } if ch.sender == address: channel_detail.append(sender_info) channel_detail.append(recevier_info) else: channel_detail.append(recevier_info) channel_detail.append(sender_info) channel_info = { "channel_name": ch.channel_name, "channel_state": str(State(ch.stateinDB)), "open_block": ch.open_block_number, "contract_address": ch.contract_address, "tx_info": channel_detail } channel_infos.append(channel_info) return channel_infos
def regist_channel(sender_addr, receiver_addr, asset_type, deposit, open_blockchain): """ This func is to register channel :param sender_addr: String, the sender address :param receiver_addr: String, receiver's address :param asset_type: String, asset type should be symbol name , asset id should be configured in the configure.json :param deposit: String, depoist number in block chain :param open_blockchain: Open block chain number :return: Dictionary, {"channel_name":channel_name, "trad_info":raw_tans} """ channel = Channel(sender_addr, receiver_addr) logger.info('start to register channel') if channel.qeury_channel(): return {"error": "channel already exist"} else: channel_name = channel.create(sender_deposit=int(deposit), open_block_number=int(open_blockchain), settle_timeout=10) if not channel_name: return { "channel_name": None, "contract_address": None, "trad_info": "on pub key or not register pub key" } channel.qeury_channel() raw_tans, tx_id, state = blockchain.deposit_transaction( asset_type, sender_addr, channel.contract_address, int(deposit)) if state: channel.update_channel_to_database(tx_id=tx_id, state=State.OPENING) return { "channel_name": channel_name, "contract_address": channel.contract_address, "trad_info": raw_tans } else: channel.delete_channel() channel.delete_channel_in_database() return { "channel_name": None, "contract_address": None, "trad_info": raw_tans }
def get_history(channel_name, index, count): sender, receiver = split_channel_name(channel_name) ch = Channel(sender, receiver) result = ch.read_channel() return result[int(index):int(index) + int(count)]