-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
38 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,27 @@ | ||
import random | ||
from dataserv.Farmer import sha256 | ||
|
||
|
||
class Challenge: | ||
def seed_list(self, height): | ||
def __init__(self, btc_addr, height): | ||
self.btc_addr = btc_addr | ||
self.height = height | ||
|
||
self.seeds = self.gen_seeds() | ||
self.challenge = None | ||
|
||
def gen_seeds(self): | ||
"""Generate a list of seeds for challenges based on the Bitcoin address.""" | ||
seeds = [] | ||
last_seed = sha256(self.btc_addr) | ||
for i in range(height+1): | ||
for i in range(self.height): | ||
seeds.append(last_seed) | ||
last_seed =sha256(last_seed) | ||
return seeds | ||
|
||
def gen_challenge(self): | ||
def pick_seed(self): | ||
"""Generate a random challenge for the Farmer.""" | ||
iter_seed = random.randrange(0, len(app.config['SEED_HEIGHT'])) | ||
seeds = self.seed_list(app.config['SEED_HEIGHT']) | ||
return random.choice(self.seeds) | ||
|
||
def get_seeds(self): | ||
return self.seeds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import unittest | ||
from dataserv.Challenge import Challenge | ||
|
||
class ChallengeTest(unittest.TestCase): | ||
|
||
def test_seeds(self): | ||
addr = '191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc' | ||
chal = Challenge(addr, 100) | ||
|
||
iter0 = '66357e60899acae95ce1e31def3d7b32a73d34b2f12ece73cdca025a26e17e32' | ||
iter3 = '96a6cb8668775a1751d7cf1d59cd0a8d3bc67e8ed5e6f5fce0376d5e04284071' | ||
|
||
seeds = chal.get_seeds() | ||
self.assertEqual(len(seeds), 100) | ||
self.assertEqual(iter0, seeds[0]) | ||
self.assertEqual(iter3, seeds[3]) | ||
|
||
def test_challenge(self): | ||
addr = '191GVvAaTRxLmz3rW3nU5jAV1rF186VxQc' | ||
chal = Challenge(addr, 100) | ||
self.assertTrue(chal.pick_seed() in chal.get_seeds()) |