Skip to content

Commit

Permalink
Removed Contract Testing Code from Farmer
Browse files Browse the repository at this point in the history
  • Loading branch information
super3 committed Jun 24, 2015
1 parent 4d4dabb commit 4aadf1c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
19 changes: 18 additions & 1 deletion dataserv/Contract.py
Expand Up @@ -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
Expand All @@ -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()
23 changes: 4 additions & 19 deletions 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


Expand Down Expand Up @@ -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()
3 changes: 2 additions & 1 deletion dataserv/config.py
Expand Up @@ -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
1 change: 1 addition & 0 deletions tests/test_App.py
Expand Up @@ -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'
Expand Down
3 changes: 2 additions & 1 deletion 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):
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 4aadf1c

Please sign in to comment.