From 4aadf1c7679e3378397c1de969c62534f3ca9e8c Mon Sep 17 00:00:00 2001 From: Shawn Wilkinson Date: Wed, 24 Jun 2015 13:04:32 -0400 Subject: [PATCH] Removed Contract Testing Code from Farmer --- dataserv/Contract.py | 19 ++++++++++++++++++- dataserv/Farmer.py | 23 ++++------------------- dataserv/config.py | 3 ++- tests/test_App.py | 1 + tests/test_Contract.py | 3 ++- 5 files changed, 27 insertions(+), 22 deletions(-) diff --git a/dataserv/Contract.py b/dataserv/Contract.py index 9b95361..1e2963b 100644 --- a/dataserv/Contract.py +++ b/dataserv/Contract.py @@ -28,6 +28,17 @@ def to_json(self): return contract_template + def below_limit(self, limit=None): + current_size = 0 + contracts = Contract.query.filter_by(btc_addr=self.btc_addr) + for single_contract in contracts: + current_size += single_contract.byte_size + + if limit is None: + return current_size < app.config["BYTE_FARMER_MAX"] + else: + return current_size < limit + def new_contract(self, btc_addr, seed=None, byte_size=None): """Build a new contract.""" self.btc_addr = btc_addr @@ -42,9 +53,15 @@ def new_contract(self, btc_addr, seed=None, byte_size=None): # take in a byte_size, if not then get it from config if byte_size is None: - self.byte_size = app.config["SHARD_SIZE"] + self.byte_size = app.config["BYTE_SIZE"] else: self.byte_size = byte_size gen_file = RandomIO.RandomIO(seed).read(self.byte_size) self.file_hash = hashlib.sha256(gen_file).hexdigest() + + self.save() + + def save(self): + db.session.add(self) + db.session.commit() \ No newline at end of file diff --git a/dataserv/Farmer.py b/dataserv/Farmer.py index 08fa766..026412a 100644 --- a/dataserv/Farmer.py +++ b/dataserv/Farmer.py @@ -1,10 +1,8 @@ -import os import hashlib -import binascii -import RandomIO from dataserv.app import db from datetime import datetime from sqlalchemy import DateTime +from dataserv.Contract import Contract from dataserv.Validator import is_btc_address @@ -99,19 +97,6 @@ def audit(self): def new_contract(self, hexseed=None): farmer = self.lookup() - seed = os.urandom(12) - hexseed = binascii.hexlify(seed).decode('ascii') - filesize = 10*1024*1024 - print('Pair {0}: Generating hash for {1} bytes file with seed {2}...'.format(0, filesize, hexseed)) - myhash = hashlib.sha256(RandomIO.RandomIO(seed).read(filesize)).hexdigest() - print('{0} {1}\n'.format(hexseed, myhash)) - - contract_template = { - "btc_addr": self.btc_addr, - "contract_type": 0, - "file_hash": myhash, - "byte_size": filesize, - "seed": hexseed - } - - return contract_template + con = Contract() + con.new_contract(self.btc_addr) + return con.to_json() \ No newline at end of file diff --git a/dataserv/config.py b/dataserv/config.py index ee0915f..3ee6668 100644 --- a/dataserv/config.py +++ b/dataserv/config.py @@ -2,4 +2,5 @@ ONLINE_TIME = 15 # minutes DATA_DIR = 'data/' -SHARD_SIZE = 10*1024*1024 # 10 MB +BYTE_SIZE = 10*1024*1024 # 10 MB +BYTE_FARMER_MAX = 30*1024*1024 # 30 MB diff --git a/tests/test_App.py b/tests/test_App.py index f324ffd..bdc695a 100644 --- a/tests/test_App.py +++ b/tests/test_App.py @@ -122,6 +122,7 @@ def test_new_contract(self): # check type 0 contracts self.assertEqual(json_data["btc_addr"], addr) self.assertEqual(json_data["contract_type"], 0) + self.assertEqual(json_data["byte_size"], app.config["BYTE_SIZE"]) def test_new_contract_fail(self): addr1 = '191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc' diff --git a/tests/test_Contract.py b/tests/test_Contract.py index b0a5f13..4928a4c 100644 --- a/tests/test_Contract.py +++ b/tests/test_Contract.py @@ -1,5 +1,5 @@ import unittest -from dataserv.app import db +from dataserv.app import db, app from dataserv.Contract import Contract class ContractTest(unittest.TestCase): @@ -28,6 +28,7 @@ def test_new_contract2(self): my_contract.new_contract(addr) self.assertEqual(my_contract.btc_addr, addr) self.assertEqual(my_contract.contract_type, 0) + self.assertEqual(my_contract.byte_size, app.config["BYTE_SIZE"]) def test_new_contract_json(self): addr = '191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc'