def test_UInt256_comparison(self): u1 = UInt256("100") u2 = UInt256("101") u3 = UInt256("101") # test comparisons with UInt256 self.assertGreater(u2, u1) self.assertLess(u1, u2) self.assertGreaterEqual(u2, u1) self.assertGreaterEqual(u3, u1) self.assertGreaterEqual(u3, u2) self.assertGreaterEqual(u2, u3) self.assertLessEqual(u1, u2) self.assertLessEqual(u1, u3) self.assertLessEqual(u3, u2) self.assertLessEqual(u2, u3) self.assertEqual(u2, u3) # comparison with int self.assertGreater(u2, 100) self.assertLess(u1, 101) self.assertGreaterEqual(u2, 100) self.assertGreaterEqual(u3, 100) self.assertGreaterEqual(u3, 101) self.assertLessEqual(u1, 101) self.assertLessEqual(u3, 101) self.assertEqual(u1, 100)
def dict2bid(m): msg = Offer() msg.model = Multihash() msg.model.multihash = m['model'] msg.objective = Multihash() msg.objective.multihash = m['objective'] msg.token = Address() msg.token.address = m['token'] msg.cost = UInt256() msg.cost.uint256 = str(m['cost']) msg.validator = Address() msg.validator.address = m['validator'] msg.lighthouse = Address() msg.lighthouse.address = m['lighthouse'] msg.lighthouseFee = UInt256() msg.lighthouseFee.uint256 = str(m['lighthouseFee']) msg.deadline = UInt256() msg.deadline.uint256 = str(m['deadline']) msg.sender = Address() msg.sender.address = m['sender'] msg.signature = unhexlify(m['signature'].encode('utf-8')) return msg
def test_UInt256_arithmetic(self): u1 = UInt256("100") u2 = UInt256("101") u1 += 1 self.assertEqual(u1, u2) u1 = UInt256("100") u3 = u1 + 1 self.assertEqual(u3, u2) u3 = u1 + UInt256("1") self.assertEqual(u3, u2)
def create_demand(fields: dict) -> Demand: rospy.loginfo("Creating an demand...") demand = Demand() demand.model = Multihash(fields["model"]["multihash"]) demand.objective = Multihash(fields["objective"]["multihash"]) demand.token = Address(fields["token"]["address"]) demand.cost = UInt256(fields["cost"]["uint256"]) demand.lighthouse = Address(fields["lighthouse"]["address"]) demand.validator = Address(fields["validator"]["address"]) demand.validatorFee = UInt256(fields["validatorFee"]["uint256"]) demand.deadline = UInt256(fields["deadline"]["uint256"]) rospy.loginfo(demand) return demand
def create_offer(fields: dict) -> Offer: rospy.loginfo("Creating an offer...") offer = Offer() offer.model = Multihash(fields["model"]["multihash"]) offer.objective = Multihash(fields["objective"]["multihash"]) offer.token = Address(fields["token"]["address"]) offer.cost = UInt256(fields["cost"]["uint256"]) offer.lighthouse = Address(fields["lighthouse"]["address"]) offer.validator = Address(fields["validator"]["address"]) offer.lighthouseFee = UInt256(fields["lighthouseFee"]["uint256"]) offer.deadline = UInt256(fields["deadline"]["uint256"]) rospy.loginfo(offer) return offer
def make_offer(self, demand: Demand): rospy.loginfo('Making offer...') offer = Offer() offer.model = Multihash(self.MODEL) offer.objective = demand.objective offer.token = demand.token offer.cost = demand.cost offer.lighthouse = demand.lighthouse offer.validator = demand.validator offer.lighthouseFee = UInt256("0") offer.deadline = UInt256() offer.deadline.uint256 = self.make_deadline() self.signing_offer.publish(offer) rospy.loginfo(offer)
def make_demand(objective: str, cost: int) -> Demand: rospy.loginfo('Making demand...') demand = Demand() demand.model = Multihash(MODEL) demand.objective = Multihash(objective) demand.token = Address(TOKEN) demand.cost = UInt256(str(cost)) demand.lighthouse = Address(LIGHTHOUSE) demand.validator = Address(VALIDATOR) demand.validatorFee = UInt256('0') demand.deadline = UInt256() demand.deadline.uint256 = make_deadline() demand.nonce = UInt256('0') rospy.loginfo(demand) return demand
def make_offer(self, incoming): rospy.loginfo('Making offer...') offer = Offer() offer.model = incoming.model offer.objective = incoming.objective offer.token = incoming.token offer.cost = incoming.cost offer.lighthouse = Address() offer.lighthouse.address = rospy.get_param('~lighthouse') offer.validator = incoming.validator offer.lighthouseFee = UInt256() offer.lighthouseFee.uint256 = '0' offer.deadline = UInt256() offer.deadline.uint256 = self.make_deadline() self.signing_offer.publish(offer) rospy.loginfo(offer)
def getValidAddedOrderFeedback(): f = AddedOrderFeedback() f.order = unhexlify(validAddedOrderFeedbackDict['order'].encode('utf-8')) f.accepted = UInt256() f.accepted.uint256 = str(validAddedOrderFeedbackDict['accepted']) f.signature = unhexlify(validAddedOrderFeedbackDict['signature'].encode('utf-8')) return f
def get_nonce_by_address(address): try: nonce = get_initialized_factory().call().nonceOf(address.address) return UInt256(nonce) except Exception as e: rospy.logerr("Failed to get nonce by address %s with exception: %s", address.address, e) try: self.__factory_initialization_lock.acquire() self.factory = None finally: self.__factory_initialization_lock.release()
def send_offer(self, demand: Demand, price: UInt256): offer = Offer() offer.model = Multihash(self.MODEL) offer.objective = demand.objective offer.token = Address(self.TOKEN) offer.cost = price offer.lighthouse = demand.lighthouse offer.validator = demand.validator offer.lighthouseFee = UInt256("0") offer.deadline = self.make_deadline() self.signing_offer.publish(offer) rospy.loginfo(offer)
def getValidAsk(): a = Demand() a.model = Multihash() a.model.multihash = validAskDict['model'] a.objective = Multihash() a.objective.multihash = validAskDict['objective'] a.token = Address() a.token.address = validAskDict['token'] a.cost = UInt256() a.cost.uint256 = str(validAskDict['cost']) a.lighthouse = Address() a.lighthouse.address = validAskDict['lighthouse'] a.validator = Address() a.validator.address = validAskDict['validator'] a.validatorFee = UInt256() a.validatorFee.uint256 = str(validAskDict['validatorFee']) a.deadline = UInt256() a.deadline.uint256 = str(validAskDict['deadline']) a.sender = Address() a.sender.address = validAskDict['sender'] a.signature = unhexlify(validAskDict['signature'].encode('utf-8')) return a
def getValidBid(): b = Offer() b.model = Multihash() b.model.multihash = validBidDict['model'] b.objective = Multihash() b.objective.multihash = validBidDict['objective'] b.token = Address() b.token.address = validBidDict['token'] b.cost = UInt256() b.cost.uint256 = str(validBidDict['cost']) b.validator = Address() b.validator.address = validBidDict['validator'] b.lighthouse = Address() b.lighthouse.address = validBidDict['lighthouse'] b.lighthouseFee = UInt256() b.lighthouseFee.uint256 = str(validBidDict['lighthouseFee']) b.deadline = UInt256() b.deadline.uint256 = str(validBidDict['deadline']) b.sender = Address() b.sender.address = validAskDict['sender'] b.signature = unhexlify(validBidDict['signature'].encode('utf-8')) return b
def calc_price(self, demand: Demand) -> UInt256: objective = IpfsRosBag(multihash=demand.objective) stl_address = objective.messages["/file"].data stl_path = self.__ipfs_download(stl_address) name = self.octopi.upload_file(stl_path) gcode_analysis = self.octopi.get_gcode_analysis(name) estimated_time = int(gcode_analysis["estimatedPrintTime"]) production_cost = self.__calc_production_cost(estimated_time) material_cost = 0 # TODO total_cost_tokens = production_cost + material_cost return UInt256(str(total_cost_tokens))
def test_offer_hash(self): test_nonce_value = UInt256("0") rospy.logwarn( "test_offer_hash: %s", hexlify( robonomicsMessageUtils.offer_hash(messageValidator.dict2bid( testMessages.validBidDict), nonce=test_nonce_value))) self.assertEqual( bytearray.fromhex( '50f82ac8c4905d14a65632915fa21cf3726c720fd9650d3b5ac134ba2ebb9da2' ), robonomicsMessageUtils.offer_hash(messageValidator.dict2bid( testMessages.validBidDict), nonce=test_nonce_value))
def make_demand(self, objective: Multihash, cost: UInt256): rospy.loginfo('Making demand...') demand = Demand() demand.model = Multihash() demand.model.multihash = self.MODEL demand.objective = objective demand.token = Address() demand.token.address = self.TOKEN demand.cost = cost demand.lighthouse = Address() demand.lighthouse.address = self.LIGHTHOUSE demand.validator = Address() demand.validator.address = '0x0000000000000000000000000000000000000000' demand.validatorFee = UInt256() demand.validatorFee.uint256 = '0' demand.deadline = self.get_deadline() self.signing_demand.publish(demand) rospy.loginfo(demand)
def sign_demand(msg): msg.sender = Address(self.__account.address) if messageValidator.isDemandFieldsCorrect(msg): msg = robonomicsMessageUtils.convert_msg_ens_names_to_addresses( msg, web3=self.web3) current_nonce = get_nonce_by_address(msg.sender) message_hash = robonomicsMessageUtils.demand_hash( msg, current_nonce) signed_hash = self.__account.signHash( defunct_hash_message(message_hash)) msg.signature = signed_hash.signature msg.nonce = UInt256(uint256=str(current_nonce.uint256)) rospy.loginfo('askhash: %s signature: %s', binascii.hexlify(message_hash), binascii.hexlify(msg.signature)) self.signed_demand.publish(msg) else: rospy.logerr( "Signing demand error: msg %s is not valid Demand message", msg)
def make_deadline(self) -> UInt256: lifetime = int(rospy.get_param('~order_lifetime')) deadline = rospy.ServiceProxy('/eth/current_block', BlockNumber)().number + lifetime return UInt256(str(deadline))
def test_UInt256_default_value(self): u = UInt256() print(u) self.assertEqual(0, int(u.uint256))
def __init__(self): rospy.init_node('blockchainization_agent') self.model = rospy.get_param('~model') self.token = rospy.get_param('~token') self.bid_lifetime = rospy.get_param('~bid_lifetime') self._web3 = Web3(HTTPProvider(rospy.get_param('~web3_http_provider'))) rospy.loginfo('Connecting to ERC20 node...') rospy.wait_for_service('accounts') self.accounts = rospy.ServiceProxy('accounts', Accounts)(AccountsRequest()) rospy.loginfo(str(self.accounts)) # agent's ethereum addresses if rospy.get_param('~approve', 'no') == 'yes': self.liability_factory = rospy.get_param('~liability_factory') rospy.loginfo('Making approvement to liabilities factory %s.', self.liability_factory) rospy.wait_for_service('approve') msg = ApproveRequest( spender=Address(address=self.liability_factory), value=UInt256( uint256=rospy.get_param('~approve_value', "10000"))) tx = rospy.ServiceProxy('approve', Approve)(msg) rospy.loginfo('Approved on tx: %s.', tx) else: rospy.loginfo('Launching without approve on liabilities factory.') self._signing_bid = rospy.Publisher('liability/infochan/signing/bid', Bid, queue_size=128) def on_incoming_ask(incoming_ask): rospy.loginfo('Incoming ask: ' + str(incoming_ask)) if incoming_ask.model == self.model and incoming_ask.token == self.token: rospy.loginfo('For my model and token.') self.make_bid(incoming_ask) else: rospy.loginfo('Not fits, skip.') rospy.Subscriber('liability/infochan/incoming/ask', Ask, on_incoming_ask) rospy.Subscriber('/agent/objective/duration', Duration, self.start_process) rospy.loginfo('Connecting to observer node...') rospy.wait_for_service('/observer/journals') self.get_journals = rospy.ServiceProxy('/observer/journals', GetJournals) self.result_topics = dict() self.result_topics['journals'] = rospy.Publisher('~result/journals', String, queue_size=1000) self.result_topics['sha256'] = rospy.Publisher('~result/sha256', String, queue_size=1000) rospy.loginfo('Connecting to liability node...') self.finish_allowed = False rospy.Service('~allow_finish', EmptySrv, self.allow_finish) rospy.wait_for_service('liability/finish') self._finish_liability = rospy.ServiceProxy('liability/finish', EmptySrv) rospy.loginfo('Node ' + rospy.get_name() + ' started.')
def get_deadline(self) -> UInt256: lifetime = int(200) deadline = rospy.ServiceProxy('/eth/current_block', BlockNumber)().number + lifetime return UInt256(str(deadline))
# ROS import rospy # Robonomics communication from robonomics_msgs.msg import Offer, Demand from ethereum_common.msg import Address, UInt256 from ethereum_common.srv import Accounts, BlockNumber, Approve, ApproveRequest from ipfs_common.msg import Multihash if __name__ == '__main__': rospy.init_node('demand_publisher') rospy.loginfo('Launching...') rospy.wait_for_service('/eth/current_block') rospy.wait_for_service('/eth/accounts') rospy.wait_for_service('/eth/approve') accounts = rospy.ServiceProxy('/eth/accounts', Accounts)() rospy.loginfo(str(accounts)) # AIRA ethereum addresses # Building the request. Specify spender address (factory address) and amount of tokens # our own address is derived from robonomics_comm settings req = ApproveRequest(spender=Address(address='0x7e384AD1FE06747594a6102EE5b377b273DC1225'), value=UInt256(uint256='100000000000000000')) # calling the service approve = rospy.ServiceProxy('/eth/approve', Approve)(req) rospy.loginfo(approve) rospy.loginfo('Complete.')
def __init__(self): rospy.init_node('agent') self.model = rospy.get_param('~model') self.token = rospy.get_param('~token') self.bid_lifetime = rospy.get_param('~bid_lifetime') self.web3 = Web3(HTTPProvider(rospy.get_param('~web3_http_provider'))) rospy.wait_for_service('accounts') self.account = str( rospy.ServiceProxy('accounts', Accounts)(AccountsRequest())) rospy.loginfo('Account: ' + self.account) if rospy.get_param('~approve') == 'y': rospy.wait_for_service('approve') factory = Address( address="0x44CFBcb1Ca0d3df0925dDA3354E955d38d78ad6B") msg = ApproveRequest(spender=factory, value=UInt256(uint256="1")) tx = rospy.ServiceProxy('approve', Approve)(msg) rospy.loginfo('Approved in: ' + str(tx)) self.signing_bid = rospy.Publisher('liability/infochan/signing/bid', Bid, queue_size=128) def on_incoming_ask(incoming_ask): rospy.loginfo('Incoming ask: ' + str(incoming_ask)) if incoming_ask.model == self.model and incoming_ask.token == self.token: rospy.loginfo('For my model and token.') self.make_bid(incoming_ask) else: rospy.loginfo('Not fits, skip.') rospy.Subscriber('liability/infochan/incoming/ask', Ask, on_incoming_ask) def on_email(msg): self.current_job['email'] = msg.data rospy.loginfo('Email: ' + msg.data) rospy.Subscriber('~objective/email', String, on_email) def on_droneid(msg): self.current_job['droneid'] = msg.data self.current_job['success'] = True rospy.loginfo('Drone ID: ' + msg.data) rospy.Subscriber('~objective/droneid', String, on_droneid) self.result_topics = dict() self.result_topics['droneid'] = rospy.Publisher('~result/droneid', String, queue_size=10) self.result_topics['email'] = rospy.Publisher('~result/email', String, queue_size=10) self.result_topics['success'] = rospy.Publisher('~result/success', Bool, queue_size=10) rospy.wait_for_service('liability/finish') self.finish = rospy.ServiceProxy('liability/finish', Empty) threading.Thread(target=self.process, daemon=True).start() rospy.loginfo('Node ' + rospy.get_name() + ' started.')
# -*- coding: utf-8 -*- import rosbag from ethereum_common.msg import UInt256 emission_factor = 0.430 # gramm CO2 emitted by one Watt * hour of energy consumption co2_on_vcu = 1_000_000 # gramm CO2 consumption represented by one VCU bag = rosbag.Bag('./mybag.bag', 'w') topic = '/packet_size' msg = UInt256(uint256=str(co2_on_vcu/emission_factor)) try: bag.write(topic, msg) finally: bag.close()
def strToUInt256(s): return UInt256(str(s))
# Standart, System and Third Party # ROS import rospy # Robonomics communication from robonomics_msgs.msg import Offer, Demand from ethereum_common.msg import Address, UInt256 from ethereum_common.srv import Accounts, BlockNumber, Approve, ApproveRequest from ipfs_common.msg import Multihash if __name__ == '__main__': rospy.init_node('demand_publisher') rospy.loginfo('Launching...') rospy.wait_for_service('/eth/current_block') rospy.wait_for_service('/eth/accounts') rospy.wait_for_service('/eth/approve') accounts = rospy.ServiceProxy('/eth/accounts', Accounts)() rospy.loginfo(str(accounts)) # AIRA ethereum addresses req = ApproveRequest( spender=Address(address='0x7e384AD1FE06747594a6102EE5b377b273DC1225'), value=UInt256(uint256='100000000000000000')) approve = rospy.ServiceProxy('/eth/approve', Approve)(req) rospy.loginfo(approve) rospy.loginfo('Complete.')
deadline = str( rospy.ServiceProxy('/eth/current_block', BlockNumber)().number + int(lifetime)) rospy.loginfo('Making demand...') # Demand message consists of the following fields demand = Demand() demand.model = Multihash() demand.model.multihash = model demand.objective = Multihash() demand.objective.multihash = objective demand.lighthouse = Address() demand.lighthouse.address = '0xD40AC7F1e5401e03D00F5aeC1779D8e5Af4CF9f1' demand.token = Address() demand.token.address = token demand.cost = UInt256() demand.cost.uint256 = str(price) demand.validatorFee = UInt256() demand.validatorFee.uint256 = '0' demand.validator = Address() demand.validator.address = '0x0000000000000000000000000000000000000000' demand.deadline = UInt256() demand.deadline.uint256 = deadline # We ask robonomics_comm to publish the demand message by publishing the message to the ros topic signing_demand.publish(demand) rospy.loginfo(demand) rospy.loginfo('Complete.')