def callback(): blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) farmer = Farmer(address) header_date = formatdate(timeval=mktime(datetime.now().timetuple()), localtime=True, usegmt=True) header_authorization = blockchain.sign_unicode(wif, "lalala-wrong") farmer.authenticate(header_authorization, header_date)
def test_register(self): addr1 = '191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc' addr2 = '191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc9999ghjfghj99' addr3 = 'not valid address' # create Farmer objects farmer1 = Farmer(addr1) farmer2 = Farmer(addr2) farmer3 = Farmer(addr3) # test registration of first farmer self.assertFalse(farmer1.exists()) farmer1.register() self.assertTrue(farmer1.exists()) # test duplicate self.assertRaises(LookupError, farmer1.register) # these should not be inserted self.assertRaises(ValueError, farmer2.register) self.assertRaises(ValueError, farmer3.register) # double check they are not in the db self.assertFalse(farmer2.exists()) self.assertFalse(farmer3.exists())
def test_authentication_success(self): blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) farmer = Farmer(address) header_date = formatdate(timeval=mktime(datetime.now().timetuple()), localtime=True, usegmt=True) message = farmer.get_server_address() + " " + header_date header_authorization = blockchain.sign_unicode(wif, message) self.assertTrue(farmer.authenticate(header_authorization, header_date))
def test_ping_time_limit(self): farmer = Farmer(addresses["beta"]) farmer.register() register_time = farmer.last_seen time.sleep(2) farmer.ping() # should still be around 0 delta_seconds = int((farmer.last_seen - register_time).seconds) self.assertEqual(delta_seconds, 0)
def test_height(self): farmer = Farmer(addresses["gamma"]) farmer.register() # set height and check function output self.assertEqual(farmer.height, 0) self.assertEqual(farmer.set_height(5), 5) self.assertEqual(farmer.height, 5) # check the db object as well farmer2 = farmer.lookup() self.assertEqual(farmer2.height, 5)
def test_height(self): farmer = Farmer('191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc') farmer.register() # set height and check function output self.assertEqual(farmer.height, 0) self.assertEqual(farmer.set_height(5), 5) self.assertEqual(farmer.height, 5) # check the db object as well farmer2 = farmer.lookup() self.assertEqual(farmer2.height, 5)
def callback(): blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) farmer = Farmer(address) header_date = formatdate(timeval=mktime(datetime.now().timetuple()) , localtime=True, usegmt=True) message = farmer.get_server_address() + " " + header_date header_authorization = blockchain.sign_unicode(wif, message) headers = {"Date": None, "Authorization": header_authorization} farmer.authenticate(headers)
def test_register(self): # test success farmer1 = Farmer(addresses["alpha"]) self.assertFalse(farmer1.exists()) farmer1.register() self.assertTrue(farmer1.exists()) # test duplicate error self.assertRaises(LookupError, farmer1.register) def callback_a(): Farmer(addresses["omega"]) self.assertRaises(ValueError, callback_a)
def set_height(btc_addr, height): # create Farmer object to represent user user = Farmer(btc_addr) # attempt to set height try: user.set_height(height) return make_response("Height accepted.", 200) except ValueError: msg = "Invalid Bitcoin address." return make_response(msg, 400) except LookupError: msg = "Farmer not found." return make_response(msg, 404)
def callback(): blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) farmer = Farmer(address) timeout = farmer.get_server_authentication_timeout() date = datetime.now() - timedelta(seconds=timeout) header_date = formatdate(timeval=mktime(date.timetuple()), localtime=True, usegmt=True) message = farmer.get_server_address() + " " + header_date header_authorization = blockchain.sign_unicode(wif, message) farmer.authenticate(header_authorization, header_date)
def callback(): blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) farmer = Farmer(address) header_date = formatdate(timeval=mktime( datetime.now().timetuple()), localtime=True, usegmt=True) message = farmer.get_server_address() + " " + header_date header_authorization = blockchain.sign_unicode(wif, message) headers = {"Date": None, "Authorization": header_authorization} farmer.authenticate(headers)
def test_audit(self): farmer = Farmer('191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc') # test audit before registration self.assertRaises(LookupError, farmer.audit) # register farmer farmer.register() # get register time, and make sure the ping work register_time = farmer.last_seen farmer.audit() ping_time = farmer.last_seen self.assertTrue(register_time < ping_time)
def test_to_json(self): farmer = Farmer('191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc') farmer.register() farmer.ping() farmer.set_height(50) test_payload = u'{"height": 50, "btc_addr": "191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc", "last_seen": 0}' test_json = json.loads(test_payload) call_payload = json.loads(farmer.to_json()) self.assertEqual(test_json, call_payload)
def test_audit(self): farmer = Farmer(addresses["delta"]) # test audit before registration self.assertRaises(LookupError, farmer.audit) # register farmer farmer.register() # get register time, and make sure the ping work register_time = farmer.last_seen time.sleep(app.config["MAX_PING"] + 1) # ping faster than max_ping would be ignored farmer.audit() ping_time = farmer.last_seen self.assertTrue(register_time < ping_time)
def set_height(btc_addr, height): # create Farmer object to represent user user = Farmer(btc_addr) user.authenticate(request.headers.get("Authorization"), request.headers.get("Date")) # attempt to set height try: user.set_height(height) return make_response("Height accepted.", 200) except ValueError: msg = "Invalid Bitcoin address." return make_response(msg, 400) except LookupError: msg = "Farmer not found." return make_response(msg, 404)
def test_authentication_timeout_future_success(self): blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) farmer = Farmer(address) timeout = farmer.get_server_authentication_timeout() - 5 date = datetime.now() + timedelta(seconds=timeout) header_date = formatdate(timeval=mktime(date.timetuple()), localtime=True, usegmt=True) message = farmer.get_server_address() + " " + header_date header_authorization = blockchain.sign_unicode(wif, message) headers = {"Date": header_date, "Authorization": header_authorization} self.assertTrue(farmer.authenticate(headers))
def test_ping(self): farmer = Farmer(addresses["beta"]) # test ping before registration self.assertRaises(LookupError, farmer.ping) # register farmer farmer.register() # get register time, and make sure the ping work register_time = farmer.last_seen # ping faster than max_ping would be ignored time.sleep(app.config["MAX_PING"] + 1) farmer.ping() # update last seen ping_time = farmer.last_seen self.assertTrue(register_time < ping_time)
def register(btc_addr): # create Farmer object to represent user user = Farmer(btc_addr) # error template error_msg = "Registration Failed: {0}" # attempt to register the farmer/farming address try: user.register() return make_response("User registered.", 200) except ValueError: msg = "Invalid Bitcoin address." return make_response(error_msg.format(msg), 400) except LookupError: msg = "Address already is registered." return make_response(error_msg.format(msg), 409)
def register(btc_addr): # create Farmer object to represent user user = Farmer(btc_addr) # error template error_msg = "Registration Failed: {0}" # attempt to register the farmer/farming address try: user.register() return make_response("User registered.", 200) except ValueError: msg = "Invalid BTC Address." return make_response(error_msg.format(msg), 400) except LookupError: msg = "Address Already Is Registered." return make_response(error_msg.format(msg), 409)
def ping(btc_addr): # create Farmer object to represent user user = Farmer(btc_addr) # error template error_msg = "Ping Failed: {0}" # attempt to ping the farmer/farming address try: user.ping() return make_response("Ping accepted.", 200) except ValueError: msg = "Invalid Bitcoin address." return make_response(error_msg.format(msg), 400) except LookupError: msg = "Farmer not found." return make_response(error_msg.format(msg), 404)
def ping(btc_addr): # create Farmer object to represent user user = Farmer(btc_addr) # error template error_msg = "Ping Failed: {0}" # attempt to register the farmer/farming address try: user.ping() return make_response("Ping Accepted.", 200) except ValueError: msg = "Invalid BTC Address." return make_response(error_msg.format(msg), 400) except LookupError: msg = "Farmer not found." return make_response(error_msg.format(msg), 404)
def ping(btc_addr): # create Farmer object to represent user user = Farmer(btc_addr) user.authenticate(request.headers.get("Authorization"), request.headers.get("Date")) # error template error_msg = "Ping Failed: {0}" # attempt to ping the farmer/farming address try: user.ping() return make_response("Ping accepted.", 200) except ValueError: msg = "Invalid Bitcoin address." return make_response(error_msg.format(msg), 400) except LookupError: msg = "Farmer not found." return make_response(error_msg.format(msg), 404)
def test_to_json(self): farmer = Farmer(addresses["epsilon"]) farmer.register() farmer.ping() farmer.set_height(50) test_json = { "height": 50, "btc_addr": addresses["epsilon"], 'payout_addr': addresses["epsilon"], "last_seen": 0 } call_payload = json.loads(farmer.to_json()) self.assertEqual(test_json, call_payload)
def ping(btc_addr): logger.info("CALLED /api/ping/{0}".format(btc_addr)) error_msg = "Ping Failed: {0}" try: user = Farmer(btc_addr) def before_commit(): # lazy authentication user.authenticate(dict(request.headers)) user.ping(before_commit_callback=before_commit) return make_response("Ping accepted.", 200) except ValueError: msg = "Invalid Bitcoin address." logger.warning(msg) return make_response(error_msg.format(msg), 400) except LookupError: msg = "Farmer not found." logger.warning(msg) return make_response(error_msg.format(msg), 404) except storjcore.auth.AuthError: msg = "Invalid authentication headers." logger.warning(msg) return make_response(error_msg.format(msg), 401)
def set_height(btc_addr, height): logger.info("CALLED /api/height/{0}/{1}".format(btc_addr, height)) error_msg = "Set height failed: {0}" try: user = Farmer(btc_addr) user.authenticate(dict(request.headers)) if height < app.config["HEIGHT_LIMIT"]: user.set_height(height) return make_response("Height accepted.", 200) else: msg = "Height limit exceeded." logger.warning(msg) raise OverflowError(msg) except OverflowError: msg = "Height limit exceeded." logger.warning(msg) return make_response(msg, 413) except ValueError: msg = "Invalid Bitcoin address." logger.warning(msg) return make_response(msg, 400) except LookupError: msg = "Farmer not found." logger.warning(msg) return make_response(msg, 404) except storjcore.auth.AuthError: msg = "Invalid authentication headers." logger.warning(msg) return make_response(error_msg.format(msg), 401)
def test_to_json(self): btc_addr = self.btctxstore.get_address( self.btctxstore.get_key(self.btctxstore.create_wallet())) farmer = Farmer(btc_addr) farmer.register() farmer.ping() farmer.set_height(50) test_json = { "height": 50, "btc_addr": btc_addr, 'payout_addr': btc_addr, "last_seen": 0, "uptime": 100 } call_payload = json.loads(farmer.to_json()) self.assertEqual(test_json, call_payload)
def register(btc_addr): date = request.headers.get("Date") authorization = request.headers.get("Authorization") # create Farmer object to represent user user = Farmer(btc_addr) user.authenticate(request.headers.get("Authorization"), request.headers.get("Date")) # error template error_msg = "Registration Failed: {0}" # attempt to register the farmer/farming address try: user.register() return make_response("User registered.", 200) except ValueError: msg = "Invalid Bitcoin address." return make_response(error_msg.format(msg), 400) except LookupError: msg = "Address already is registered." return make_response(error_msg.format(msg), 409)
def test_ping_days(self): btc_addr = self.btctxstore.get_address( self.btctxstore.get_key(self.btctxstore.create_wallet())) farmer = Farmer(btc_addr) farmer.register() # ping to late -> 50% delta = timedelta(days=2) farmer.last_seen = datetime.utcnow() - delta farmer.reg_time = datetime.utcnow() - delta farmer.uptime = 86401 # 1 / 2 days farmer was online farmer.ping() test_json = { "height": 0, "btc_addr": btc_addr, 'payout_addr': btc_addr, "last_seen": 0, "uptime": 50 } call_payload = json.loads(farmer.to_json()) call_payload["uptime"] = round(call_payload["uptime"]) self.assertEqual(test_json, call_payload)
def test_height_25(self): btc_addr = self.btctxstore.get_address( self.btctxstore.get_key(self.btctxstore.create_wallet())) farmer = Farmer(btc_addr) farmer.register() #ping to late -> 50% delta = timedelta(minutes=(4 * app.config["ONLINE_TIME"])) farmer.last_seen = datetime.utcnow() - delta farmer.reg_time = datetime.utcnow() - delta farmer.set_height(25) test_json = { "height": 25, "btc_addr": btc_addr, 'payout_addr': btc_addr, "last_seen": 0, "uptime": 25 } call_payload = json.loads(farmer.to_json()) call_payload["uptime"] = round(call_payload["uptime"]) self.assertEqual(test_json, call_payload)
def test_ping_time_limit(self): btc_addr = self.btctxstore.get_address( self.btctxstore.get_key(self.btctxstore.create_wallet())) farmer = Farmer(btc_addr) farmer.register() register_time = farmer.last_seen time.sleep(2) farmer.ping() # should still be around 0 delta_seconds = int((farmer.last_seen - register_time).seconds) self.assertEqual(delta_seconds, 0)
def test_height(self): btc_addr = self.btctxstore.get_address( self.btctxstore.get_key(self.btctxstore.create_wallet())) farmer = Farmer(btc_addr) farmer.register() # set height and check function output self.assertEqual(farmer.height, 0) self.assertEqual(farmer.set_height(5), 5) self.assertEqual(farmer.height, 5) # check the db object as well farmer2 = farmer.lookup() self.assertEqual(farmer2.height, 5)
def set_height(btc_addr, height): # create Farmer object to represent user user = Farmer(btc_addr) user.authenticate(request.headers.get('Authorization'), request.headers.get('Date')) # attempt to set height try: user.set_height(height) return make_response("Height accepted.", 200) except ValueError: msg = "Invalid Bitcoin address." return make_response(msg, 400) except LookupError: msg = "Farmer not found." return make_response(msg, 404)
def test_register(self): # test success btc_addr = self.btctxstore.get_address( self.btctxstore.get_key(self.btctxstore.create_wallet())) farmer1 = Farmer(btc_addr) self.assertFalse(farmer1.exists()) farmer1.register() self.assertTrue(farmer1.exists()) # test duplicate error self.assertRaises(LookupError, farmer1.register) def callback_a(): Farmer(self.bad_addr) self.assertRaises(ValueError, callback_a)
def test_audit(self): farmer = Farmer(addresses["delta"]) # test audit before registration self.assertRaises(LookupError, farmer.audit) # register farmer farmer.register() # get register time, and make sure the ping work register_time = farmer.last_seen # ping faster than max_ping would be ignored time.sleep(app.config["MAX_PING"] + 1) farmer.audit() ping_time = farmer.last_seen self.assertTrue(register_time < ping_time)
def register_with_payout(btc_addr, payout_addr): logger.info("CALLED /api/register/{0}/{1}".format(btc_addr, payout_addr)) error_msg = "Registration Failed: {0}" try: user = Farmer(btc_addr) user.authenticate(dict(request.headers)) user.register(payout_addr) return make_response(user.to_json(), 200) except ValueError: msg = "Invalid Bitcoin address." logger.warning(msg) return make_response(error_msg.format(msg), 400) except LookupError: msg = "Address already is registered." logger.warning(msg) return make_response(error_msg.format(msg), 409) except storjcore.auth.AuthError: msg = "Invalid authentication headers." logger.warning(msg) return make_response(error_msg.format(msg), 401)
def test_ping(self): btc_addr = self.btctxstore.get_address( self.btctxstore.get_key(self.btctxstore.create_wallet())) farmer = Farmer(btc_addr) # test ping before registration self.assertRaises(LookupError, farmer.ping) # register farmer farmer.register() # get register time, and make sure the ping work register_time = farmer.last_seen # ping faster than max_ping would be ignored time.sleep(app.config["MAX_PING"] + 1) farmer.ping() # update last seen ping_time = farmer.last_seen self.assertTrue(register_time < ping_time)
def register(btc_addr): date = request.headers.get('Date') authorization = request.headers.get('Authorization') # create Farmer object to represent user user = Farmer(btc_addr) user.authenticate(request.headers.get('Authorization'), request.headers.get('Date')) # error template error_msg = "Registration Failed: {0}" # attempt to register the farmer/farming address try: user.register() return make_response("User registered.", 200) except ValueError: msg = "Invalid Bitcoin address." return make_response(error_msg.format(msg), 400) except LookupError: msg = "Address already is registered." return make_response(error_msg.format(msg), 409)
def callback_a(): Farmer(self.bad_addr)
def test_register(self): btc_addr = self.btctxstore.get_address( self.btctxstore.get_key(self.btctxstore.create_wallet())) farmer = Farmer(btc_addr) farmer.register() test_json = { "height": 0, "btc_addr": btc_addr, 'payout_addr': btc_addr, "last_seen": 0, "uptime": 100 } call_payload = json.loads(farmer.to_json()) call_payload["uptime"] = round(call_payload["uptime"]) self.assertEqual(test_json, call_payload) # reg_time = max online time -> 100% uptime delta = timedelta(minutes=app.config["ONLINE_TIME"]) farmer.last_seen = datetime.utcnow() - delta farmer.reg_time = datetime.utcnow() - delta test_json = { "height": 0, "btc_addr": btc_addr, 'payout_addr': btc_addr, "last_seen": delta.seconds, "uptime": 100 } call_payload = json.loads(farmer.to_json()) call_payload["uptime"] = round(call_payload["uptime"]) self.assertEqual(test_json, call_payload) # reg_time = 2 * max online time -> 50% uptime delta = timedelta(minutes=(2 * app.config["ONLINE_TIME"])) farmer.last_seen = datetime.utcnow() - delta farmer.reg_time = datetime.utcnow() - delta farmer.ping() test_json = { "height": 0, "btc_addr": btc_addr, 'payout_addr': btc_addr, "last_seen": 0, "uptime": 50 } call_payload = json.loads(farmer.to_json()) call_payload["uptime"] = round(call_payload["uptime"]) self.assertEqual(test_json, call_payload)
def test_repr(self): farmer = Farmer('191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc') ans = "<Farmer BTC Address: '191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc'>" self.assertEqual(repr(farmer), ans)