def _grade_challenge(self, contract_name, user_address, contract_address, timeout=180): """ Deploys solidity contract :param contract_name: name of the challenge / the contract we're going to deploy :param user_address: end-user address that asked for this challenge :param timeout: how long to wait for things to happen on-chain - in seconds :return: contract_address - address of this newly spawned contract """ with self._lock: self._status = "started" contract_number = util.get_contract_number(contract_name) with self._lock: self._status = "in-progress" self._deployed_address = contract_address util.mark_grading(user_address, contract_number) if (util.contract_exists(contract_name)): contract_helper = util.get_icontract(user_address, contract_name, contract_address=contract_address) hacked = contract_helper.has_been_hacked() if hacked: util.mark_finished(user_address, contract_name) else: util.mark_in_progress(user_address, contract_number) with self._lock: self._status = "graded" return contract_address
def test_mark_finished(self): # Setup path = constants.DB_PATH + self.MOCK_USER + "/" + self.MOCK_CHALLENGE + '.done' if os.path.exists(path): os.remove(path) util.exists(self.MOCK_USER) # Validate assert not os.path.exists(path) util.mark_finished(self.MOCK_USER, self.MOCK_CHALLENGE) assert os.path.exists(path) # Cleanup if os.path.exists(path): os.remove(path)
def test_get_status(self): ns = ("Not Started", "red") dep = ("Deployed / Unfinished", "black", self.MOCK_CONTRACT_ADDRESS) fin = ("Done!", "green", self.MOCK_CONTRACT_ADDRESS) with run.app.app_context(): stat = util.get_status(self.MOCK_USER, 1) self.assertTupleEqual(stat, ns) util.exists(self.MOCK_USER) stat = util.get_status(self.MOCK_USER, 1) self.assertTupleEqual(stat, ns) util.write_address(self.MOCK_USER, 1, self.MOCK_CONTRACT_ADDRESS) stat = util.get_status(self.MOCK_USER, 1) self.assertTupleEqual(stat, dep) util.mark_finished(self.MOCK_USER, "01_naive_programmer") stat = util.get_status(self.MOCK_USER, 1) self.assertTupleEqual(stat, fin)
def test_util_get_status(self): # Setup path = constants.DB_PATH + self.MOCK_USER + "/" + self.MOCK_CHALLENGE if os.path.exists(path): os.remove(path) # Validate assert util.get_status(self.MOCK_USER, self.MOCK_CHALLENGE)[0] == "Not Started" util.write_address(self.MOCK_USER, self.MOCK_CHALLENGE, self.MOCK_CONTRACT_ADDRESS) assert util.get_status(self.MOCK_USER, self.MOCK_CHALLENGE)[0] == "Deployed / Unfinished" util.mark_finished(self.MOCK_USER, self.MOCK_CHALLENGE) assert util.get_status(self.MOCK_USER, self.MOCK_CHALLENGE)[0] == "Done!" # Cleanup if os.path.exists(path): os.remove(path)
def update(address, contract): contract_addr = util.get_status(address, contract)[2].strip() checks = json.loads( open("challenges/" + contract + ".json").read().strip()).get( "post_check", []) contract_bal = ethereum.EasyWeb3().balance(contract_addr) for check in checks: print 50000000000000000, contract_bal, int(check["balance_lt"]) print type(int(check["balance_lt"])), type(contract_bal) if "balance_lt" in check: if int(check["balance_lt"]) <= int(contract_bal): print 50000000000000000, contract_bal, int(check["balance_lt"]) return redirect(request.referrer) #return redirect("/dashboard?address=" + address) util.mark_finished(address, contract) return redirect(request.referrer)
def test_mark_in_progress(self): with run.app.app_context(): util.write_address(self.MOCK_USER, 1, self.MOCK_CONTRACT_ADDRESS) util.mark_finished(self.MOCK_USER, "01_naive_programmer") util.mark_in_progress(self.MOCK_USER, 1) conn = sqlite3.connect(constants.DB_PATH) cur = conn.cursor() resp = cur.execute("SELECT * FROM htctable") val = list(resp)[0] # userid useraddress score c1state c1depoyaddr c2state c2deployaddr c3state c3deployaddr c4state c4deployaddr self.assertTupleEqual(val, ( 1, # userid self.MOCK_USER, # useraddress 1, # score 0, # c0state None, # c0deployaddr 0, # c0finished 1, # c1state self.MOCK_CONTRACT_ADDRESS, # c1deployaddr 1, # c1finished 0, # c2state None, # c2deployaddr 0, # c2finished 0, # c3state None, #c3deployaddr 0, # c3finished 0, # c4state None, # c4deployaddr 0, # c4finished 0, # c5state None, # c5deployaddr 0, # c5finished 0, # c6state None, # c6deployaddr 0, # c6finished 0, #c7state None, # c7deployaddr 0 # c7finished ) ) conn.close()
def test_get_ranking_from_db(self): with run.app.app_context(): util.write_address(self.MOCK_USER, 1, self.MOCK_CONTRACT_ADDRESS) util.mark_finished(self.MOCK_USER, "01_naive_programmer") util.write_address(self.VALID_ADDRESSES[0], 2, self.VALID_ADDRESSES[1]) util.mark_finished(self.VALID_ADDRESSES[0], "02_coinflip") util.write_address(self.VALID_ADDRESSES[0], 3, self.VALID_ADDRESSES[2]) util.mark_finished(self.VALID_ADDRESSES[0], "03_ERC20") util.write_address(self.VALID_ADDRESSES[3], 5, self.VALID_ADDRESSES[4]) util.mark_finished(self.VALID_ADDRESSES[3], "05_bad_ico") ranking = util.get_ranking_from_db() self.assertListEqual( [ (self.VALID_ADDRESSES[0], 6), (self.VALID_ADDRESSES[3], 3), (self.MOCK_USER, 1), ], ranking)
def test_malicious_mark_finished(self): with run.app.app_context(): conn = sqlite3.connect(constants.DB_PATH) cur = conn.cursor() util.write_address(self.MOCK_USER, 1, self.MOCK_CONTRACT_ADDRESS) resp = cur.execute("SELECT * FROM htctable") val = list(resp)[0] self.assertTupleEqual(val, ( 1, # userid self.MOCK_USER, # useraddress 0, # score 0, # c0state None, # c0deployaddr 0, # c0finished constants.STATE_DEPLOYED_IN_PROGRESS, # c1state self.MOCK_CONTRACT_ADDRESS, # c1deployaddr 0, # c1finished 0, # c2state None, # c2deployaddr 0, # c2finished 0, # c3state None, #c3deployaddr 0, # c3finished 0, # c4state None, # c4deployaddr 0, # c4finished 0, # c5state None, # c5deployaddr 0, # c5finished 0, # c6state None, # c6deployaddr 0, # c6finished 0, #c7state None, # c7deployaddr 0 # c7finished ) ) util.mark_in_progress(self.MOCK_USER, 1) resp = cur.execute("SELECT * FROM htctable") val = list(resp)[0] self.assertTupleEqual(val, ( 1, # userid self.MOCK_USER, # useraddress 0, # score 0, # c0state None, # c0deployaddr 0, # c0finished constants.STATE_DEPLOYED_IN_PROGRESS, # c1state self.MOCK_CONTRACT_ADDRESS, # c1deployaddr 0, # c1finished 0, # c2state None, # c2deployaddr 0, # c2finished 0, # c3state None, #c3deployaddr 0, # c3finished 0, # c4state None, # c4deployaddr 0, # c4finished 0, # c5state None, # c5deployaddr 0, # c5finished 0, #c6state None, # c6deployaddr 0, # c6finished 0, #c7state None, # c7deployaddr 0 # c7finished ) ) util.mark_finished(self.MOCK_USER, "01_naive_programmer") resp = cur.execute("SELECT * FROM htctable") val = list(resp)[0] self.assertTupleEqual(val, ( 1, # userid self.MOCK_USER, # useraddress 1, # score 0, # c0state None, # c0deployaddr 0, # c0finished constants.STATE_FINISHED, # c1state self.MOCK_CONTRACT_ADDRESS, # c1deployaddr 1, # c1finished 0, # c2state None, # c2deployaddr 0, # c2finished 0, # c3state None, #c3deployaddr 0, # c3finished 0, # c4state None, # c4deployaddr 0, # c4finished 0, # c5state None, # c5deployaddr 0, # c5finished 0, #c6state None, # c6deployaddr 0, # c6finished 0, #c7state None, # c7deployaddr 0 # c7finished ) ) util.mark_finished(self.MOCK_USER, "01_naive_programmer") resp = cur.execute("SELECT * FROM htctable") val = list(resp)[0] self.assertTupleEqual(val, ( 1, # userid self.MOCK_USER, # useraddress 1, # score 0, # c0state None, # c0deployaddr 0, # c0finished constants.STATE_FINISHED, # c1state self.MOCK_CONTRACT_ADDRESS, # c1deployaddr 1, # c1finished 0, # c2state None, # c2deployaddr 0, # c2finished 0, # c3state None, #c3deployaddr 0, # c3finished 0, # c4state None, # c4deployaddr 0, # c4finished 0, # c5state None, # c5deployaddr 0, # c5finished 0, #c6state None, # c6deployaddr 0, # c6finished 0, #c7state None, # c7deployaddr 0 # c7finished ) ) util.write_address(self.MOCK_USER, 1, self.VALID_ADDRESSES[1]) util.mark_in_progress(self.MOCK_USER, 1) resp = cur.execute("SELECT * FROM htctable") val = list(resp)[0] self.assertTupleEqual(val, ( 1, # userid self.MOCK_USER, # useraddress 1, # score 0, # c0state None, # c0deployaddr 0, # c0finished constants.STATE_DEPLOYED_IN_PROGRESS, # c1state self.VALID_ADDRESSES[1], # c1deployaddr 1, # c1finished 0, # c2state None, # c2deployaddr 0, # c2finished 0, # c3state None, #c3deployaddr 0, # c3finished 0, # c4state None, # c4deployaddr 0, # c4finished 0, # c5state None, # c5deployaddr 0, # c5finished 0, #c6state None, # c6deployaddr 0, # c6finished 0, #c7state None, # c7deployaddr 0 # c7finished ) ) util.mark_grading(self.MOCK_USER, 1) resp = cur.execute("SELECT * FROM htctable") val = list(resp)[0] self.assertTupleEqual(val, ( 1, # userid self.MOCK_USER, # useraddress 1, # score 0, # c0state None, # c0deployaddr 0, # c0finished constants.STATE_GRADING, # c1state self.VALID_ADDRESSES[1], # c1deployaddr 1, # c1finished 0, # c2state None, # c2deployaddr 0, # c2finished 0, # c3state None, #c3deployaddr 0, # c3finished 0, # c4state None, # c4deployaddr 0, # c4finished 0, # c5state None, # c5deployaddr 0, # c5finished 0, #c6state None, # c6deployaddr 0, # c6finished 0, #c7state None, # c7deployaddr 0 # c7finished ) ) util.mark_finished(self.MOCK_USER, "01_naive_programmer") resp = cur.execute("SELECT * FROM htctable") val = list(resp)[0] self.assertTupleEqual(val, ( 1, # userid self.MOCK_USER, # useraddress 1, # score 0, # c0state None, # c0deployaddr 0, # c0finished constants.STATE_FINISHED, # c1state self.VALID_ADDRESSES[1], # c1deployaddr 1, # c1finished 0, # c2state None, # c2deployaddr 0, # c2finished 0, # c3state None, #c3deployaddr 0, # c3finished 0, # c4state None, # c4deployaddr 0, # c4finished 0, # c5state None, # c5deployaddr 0, # c5finished 0, #c6state None, # c6deployaddr 0, # c6finished 0, #c7state None, # c7deployaddr 0 # c7finished ) ) conn.close()
def bombed(self): if not self.preparing: self.dead = True if self.isMe(): util.mark_finished()