-
Notifications
You must be signed in to change notification settings - Fork 9
/
Contract.py
67 lines (54 loc) · 1.92 KB
/
Contract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import hashlib
import binascii
import RandomIO
from dataserv.app import db, app
class Contract(db.Model):
id = db.Column(db.Integer, primary_key=True)
btc_addr = db.Column(db.String(35), unique=True)
contract_type = db.Column(db.Integer, default=0)
file_hash = db.Column(db.String(128))
byte_size = db.Column(db.Integer, default=0)
seed = db.Column(db.String(128))
def __init__(self):
pass
def to_json(self):
"""JSON dump of the contract object."""
contract_template = {
"btc_addr": self.btc_addr,
"contract_type": self.contract_type,
"file_hash": self.file_hash,
"byte_size": self.byte_size,
"seed": self.seed
}
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
self.contract_type = 0
# take in a seed, if not generate it ourselves
if seed is None:
seed = os.urandom(12)
self.seed = binascii.hexlify(seed).decode('ascii')
else:
self.seed = seed
# take in a byte_size, if not then get it from config
if byte_size is None:
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()