예제 #1
0
    def test_vote_passes_voting_period_runs_median_sets_new_rate_and_resets_properly(
            self):
        # 'stu', 'raghu', 'alex', 'monica', 'steve', 'tejas'

        self.client.submit(stamps, constructor_args={
            'initial_rate': 10000,
        })

        stamps_contract = self.client.get_contract('stamps')

        self.assertEqual(stamps_contract.S['rate'], 10000)

        env = {'now': Datetime._from_datetime(dt.today() + td(days=7))}

        stamps_contract.vote(vk='stu', obj=20000, environment=env)
        stamps_contract.vote(vk='raghu', obj=5000, environment=env)
        stamps_contract.vote(vk='alex', obj=12000, environment=env)
        stamps_contract.vote(vk='monica', obj=13000, environment=env)
        stamps_contract.vote(vk='steve', obj=7000, environment=env)

        self.assertEqual(stamps_contract.S['votes', 'stu'], 20000)
        self.assertEqual(stamps_contract.S['votes', 'raghu'], 5000)
        self.assertEqual(stamps_contract.S['votes', 'alex'], 12000)
        self.assertEqual(stamps_contract.S['votes', 'monica'], 13000)
        self.assertEqual(stamps_contract.S['votes', 'steve'], 7000)

        env = {'now': Datetime._from_datetime(dt.today() + td(days=14))}

        stamps_contract.vote(vk='tejas', obj=15000, environment=env)

        expected = 12500

        self.assertEqual(stamps_contract.S['rate'], expected)
예제 #2
0
    def test_voting_no_confidence_again_after_waiting_one_day_works(self):
        self.currency.approve(signer='stu', amount=10_000, to='elect_members')

        stu_bal = self.currency.balances['stu']

        env = {'now': Datetime._from_datetime(dt.today())}

        self.elect_members.vote_no_confidence(signer='stu',
                                              address='raghu',
                                              environment=env)

        env = {'now': Datetime._from_datetime(dt.today() + td(days=7))}

        self.elect_members.vote_no_confidence(signer='stu',
                                              address='raghu',
                                              environment=env)

        self.assertEqual(self.currency.balances['stu'], stu_bal - 2)
        self.assertEqual(
            self.elect_members.no_confidence_state['votes', 'raghu'], 2)

        self.assertEqual(self.currency.balances['blackhole'], 2)

        self.assertEqual(
            self.elect_members.no_confidence_state['last_voted', 'stu'],
            env['now'])
예제 #3
0
    def test_vote_doesnt_reach_consensus_after_voting_period_fails(self):
        self.client.submit(masternodes,
                           constructor_args={
                               'initial_masternodes':
                               ['a' * 64, 'b' * 64, 'c' * 64],
                           })

        mn_contract = self.client.get_contract('masternodes')

        env = {'now': Datetime._from_datetime(dt.today())}

        mn_contract.vote(vk='a' * 64,
                         obj=('introduce_motion', 3),
                         environment=env)

        env = {'now': Datetime._from_datetime(dt.today() + td(days=2))}

        mn_contract.vote(vk='a' * 64,
                         obj=('vote_on_motion', True),
                         environment=env)

        self.assertEqual(mn_contract.quick_read('S', 'current_motion'), 0)
        self.assertEqual(mn_contract.quick_read('S', 'master_in_question'),
                         None)
        self.assertEqual(mn_contract.quick_read('S', 'nays'), 0)
예제 #4
0
    def test_voting_again_after_waiting_one_day_works(self):
        # Give joe money
        self.currency.transfer(signer='stu', amount=100_000, to='joe')

        # Joe Allows Spending
        self.currency.approve(signer='joe', amount=100_000, to='elect_members')

        self.elect_members.register(signer='joe')

        self.currency.approve(signer='stu', amount=10_000, to='elect_members')

        stu_bal = self.currency.balances['stu']

        env = {'now': Datetime._from_datetime(dt.today())}

        self.elect_members.vote_candidate(signer='stu',
                                          address='joe',
                                          environment=env)

        env = {'now': Datetime._from_datetime(dt.today() + td(days=7))}

        self.elect_members.vote_candidate(signer='stu',
                                          address='joe',
                                          environment=env)

        self.assertEqual(self.currency.balances['stu'], stu_bal - 2)
        self.assertEqual(self.elect_members.candidate_state['votes', 'joe'], 2)

        self.assertEqual(self.currency.balances['blackhole'], 2)

        self.assertEqual(
            self.elect_members.candidate_state['last_voted', 'stu'],
            env['now'])
예제 #5
0
    def test_vote_finished_returns_average_sum(self):
        env = {'now': Datetime._from_datetime(dt.today() + td(days=7))}

        self.election_house.vote(policy='rewards',
                                 value=[20, 20, 20, 20, 20],
                                 environment=env,
                                 signer='v1')
        self.election_house.vote(policy='rewards',
                                 value=[25, 0, 50, 25, 0],
                                 environment=env,
                                 signer='v2')
        self.election_house.vote(policy='rewards',
                                 value=[25, 0, 0, 75, 0],
                                 environment=env,
                                 signer='v3')
        self.election_house.vote(policy='rewards',
                                 value=[100, 0, 0, 0, 0],
                                 environment=env,
                                 signer='v4')

        env = {'now': Datetime._from_datetime(dt.today() + td(days=8))}

        self.election_house.vote(policy='rewards',
                                 value=[100, 0, 0, 0, 0],
                                 environment=env,
                                 signer='v5')

        # Expected [176, 26, 76, 126] / 404 = [0.4375, 0.0625, 0.1875, 0.3125]

        self.assertEqual(
            self.election_house.current_value_for_policy(policy='rewards'),
            [0.425, 0.05, 0.175, 0.30, 0.05])
예제 #6
0
    def test_vote_after_window_reset_can_pass(self):
        self.upgrade.vote(signer=self.mn_wallets[0],
                          cilantro_branch_name='hello1',
                          contracting_branch_name='hello2',
                          pepper='123xyz')

        self.upgrade.vote(signer=self.mn_wallets[1])

        self.assertEqual(self.upgrade.upgrade_state['votes'], 2)

        env = {
            'now': Datetime._from_datetime(datetime.now() + timedelta(weeks=2))
        }

        # This will fail because it needs the keyword arguments
        with self.assertRaises(TypeError):
            self.upgrade.vote(signer=self.mn_wallets[2], environment=env)

        # This will pass
        self.upgrade.vote(signer=self.mn_wallets[2],
                          cilantro_branch_name='hello4',
                          contracting_branch_name='hello6',
                          pepper='693kdw',
                          environment=env)

        self.upgrade.vote(signer=self.mn_wallets[0], environment=env)
        self.upgrade.vote(signer=self.mn_wallets[1], environment=env)
        self.upgrade.vote(signer=self.dn_wallets[0], environment=env)

        self.assertTrue(self.upgrade.upgrade_state['consensus'])
예제 #7
0
    def test_invalid_signer_fails(self):
        env = {'now': Datetime._from_datetime(dt.today() + td(days=7))}

        with self.assertRaises(AssertionError):
            self.election_house.vote(policy='rewards',
                                     value=[1, 2, 3, 4, 5],
                                     environment=env)
    def test_voting_again_too_soon_throws_assertion_error(self):
        # Give joe money
        self.currency.transfer(signer='stu', amount=100_000, to='joe')

        # Joe Allows Spending
        self.currency.approve(signer='joe',
                              amount=100_000,
                              to='master_candidates')

        self.master_candidates.register(signer='joe')

        self.currency.approve(signer='stu',
                              amount=10_000,
                              to='master_candidates')

        env = {'now': Datetime._from_datetime(dt.today())}

        self.master_candidates.vote_candidate(signer='stu',
                                              address='joe',
                                              environment=env)

        with self.assertRaises(AssertionError):
            self.master_candidates.vote_candidate(signer='stu',
                                                  address='joe',
                                                  environment=env)
예제 #9
0
    def test_pop_top_deletes_bob_if_pop_is_top_masternode(self):
        self.currency.transfer(signer='stu', amount=100_000,
                               to='joe')  # Give joe money
        self.currency.approve(signer='joe', amount=100_000,
                              to='elect_members')  # Joe Allows Spending
        self.elect_members.register(signer='joe')  # Register Joe

        self.currency.transfer(signer='stu', amount=100_000,
                               to='bob')  # Give Bob money
        self.currency.approve(signer='bob', amount=100_000,
                              to='elect_members')  # Bob Allows Spending
        self.elect_members.register(signer='bob')  # Register Bob

        self.currency.approve(
            signer='stu', amount=10_000,
            to='elect_members')  # Stu approves spending to vote
        env = {'now': Datetime._from_datetime(dt.today())}
        self.elect_members.vote_candidate(signer='stu',
                                          address='bob',
                                          environment=env)  # Stu votes for Bob

        self.assertEqual(self.elect_members.top_member(),
                         'bob')  # bob is the current top spot

        self.assertIsNotNone(self.elect_members.candidate_state['votes',
                                                                'bob'])

        self.elect_members.pop_top(signer='masternodes')

        self.assertIsNone(self.elect_members.candidate_state['votes', 'bob'])
예제 #10
0
    def test_vote_on_motion_works_nays(self):
        self.client.submit(masternodes,
                           constructor_args={
                               'initial_masternodes':
                               ['a' * 64, 'b' * 64, 'c' * 64],
                               'initial_open_seats':
                               0
                           })

        mn_contract = self.client.get_contract('masternodes')

        env = {'now': Datetime._from_datetime(dt.today())}

        mn_contract.vote(vk='a' * 64,
                         obj=('introduce_motion', 3),
                         environment=env)

        mn_contract.vote(vk='b' * 64, obj=('vote_on_motion', False))

        self.assertEqual(mn_contract.quick_read('S', 'nays'), 1)
        self.assertEqual(mn_contract.quick_read('S', 'yays'), 0)
        self.assertEqual(
            mn_contract.quick_read(variable='S',
                                   key='positions',
                                   args=['b' * 64]), False)
예제 #11
0
    def test_last_masternode_returns_first_in_if_tie(self):
        self.currency.approve(signer='stu', amount=10_000, to='elect_members')

        env = {'now': Datetime._from_datetime(dt.today())}

        self.elect_members.vote_no_confidence(signer='stu',
                                              address='stux',
                                              environment=env)

        env = {'now': Datetime._from_datetime(dt.today() + td(days=7))}

        self.elect_members.vote_no_confidence(signer='stu',
                                              address='raghu',
                                              environment=env)

        self.assertEqual(self.elect_members.last_member(), 'stux')
예제 #12
0
    def test_vote_reaches_more_than_half_nays_fails(self):
        self.client.submit(masternodes,
                           constructor_args={
                               'initial_masternodes':
                               ['a' * 64, 'b' * 64, 'c' * 64],
                               'initial_open_seats':
                               0
                           })

        mn_contract = self.client.get_contract('masternodes')

        env = {'now': Datetime._from_datetime(dt.today())}

        mn_contract.vote(vk='a' * 64,
                         obj=('introduce_motion', 3),
                         environment=env)

        mn_contract.vote(vk='a' * 64, obj=('vote_on_motion', False))
        mn_contract.vote(vk='b' * 64, obj=('vote_on_motion', False))

        self.assertEqual(mn_contract.quick_read('S', 'open_seats'), 0)

        self.assertEqual(mn_contract.quick_read('S', 'current_motion'), 0)
        self.assertEqual(mn_contract.quick_read('S', 'master_in_question'),
                         None)
        self.assertEqual(mn_contract.quick_read('S', 'nays'), 0)
예제 #13
0
    def test_vote_for_someone_registered_deducts_tau_and_adds_vote(self):
        # Give joe money
        self.currency.transfer(signer='stu', amount=100_000, to='joe')

        # Joe Allows Spending
        self.currency.approve(signer='joe', amount=100_000, to='elect_members')

        self.elect_members.register(signer='joe')

        self.currency.approve(signer='stu', amount=10_000, to='elect_members')

        env = {'now': Datetime._from_datetime(dt.today())}

        stu_bal = self.currency.balances['stu']

        self.elect_members.vote_candidate(signer='stu',
                                          address='joe',
                                          environment=env)

        self.assertEqual(self.currency.balances['stu'], stu_bal - 1)
        self.assertEqual(self.elect_members.candidate_state['votes', 'joe'], 1)
        self.assertEqual(self.currency.balances['blackhole'], 1)
        self.assertEqual(
            self.elect_members.candidate_state['last_voted', 'stu'],
            env['now'])
    def test_top_masternode_returns_bob_if_joe_and_bob_registered_but_bob_has_votes(
            self):
        self.currency.transfer(signer='stu', amount=100_000,
                               to='joe')  # Give joe money
        self.currency.approve(signer='joe',
                              amount=100_000,
                              to='master_candidates')  # Joe Allows Spending
        self.master_candidates.register(signer='joe')  # Register Joe

        self.currency.transfer(signer='stu', amount=100_000,
                               to='bob')  # Give Bob money
        self.currency.approve(signer='bob',
                              amount=100_000,
                              to='master_candidates')  # Bob Allows Spending
        self.master_candidates.register(signer='bob')  # Register Bob

        self.currency.approve(
            signer='stu', amount=10_000,
            to='master_candidates')  # Stu approves spending to vote
        env = {'now': Datetime._from_datetime(dt.today())}
        self.master_candidates.vote_candidate(
            signer='stu', address='bob', environment=env)  # Stu votes for Bob

        self.assertEqual(self.master_candidates.top_masternode(),
                         'bob')  # bob is the current top spot
예제 #15
0
    def test_vote_list_not_ints_fails(self):
        env = {'now': Datetime._from_datetime(dt.today() + td(days=7))}

        with self.assertRaises(AssertionError):
            self.election_house.vote(policy='rewards',
                                     value=['a', 'b', 'c', 'd'],
                                     environment=env,
                                     signer='v2')
예제 #16
0
    def test_vote_list_negatives_fails(self):
        env = {'now': Datetime._from_datetime(dt.today() + td(days=7))}

        with self.assertRaises(AssertionError):
            self.election_house.vote(policy='rewards',
                                     value=[1, 2, 3, -1, 0],
                                     environment=env,
                                     signer='v4')
예제 #17
0
    def test_vote_list_sum_not_100_fails(self):
        env = {'now': Datetime._from_datetime(dt.today() + td(days=7))}

        with self.assertRaises(AssertionError):
            self.election_house.vote(policy='rewards',
                                     value=[99, 0, 0, 0, 0],
                                     environment=env,
                                     signer='v1')
예제 #18
0
    def test_election_is_over_if_more_than_max_time_past_first_vote(self):
        self.stamp_cost.vote(vk='a', obj=120)

        env = {'now': Datetime._from_datetime(dt.today() + td(days=7))}

        res = self.stamp_cost.run_private_function(f='election_is_over',
                                                   signer='stu',
                                                   environment=env)

        self.assertTrue(res)
def generate_environment(driver, timestamp, input_hash):
    now = Datetime._from_datetime(datetime.utcfromtimestamp(timestamp))

    return {
        'block_hash': driver.latest_block_hash,
        'block_num': driver.latest_block_num + 1,
        '__input_hash':
        input_hash,  # Used for deterministic entropy for random games
        'now': now
    }
예제 #20
0
    def set_contract(self, name, code, owner=None, overwrite=False, timestamp=Datetime._from_datetime(datetime.now()), developer=None):
        if self.get_contract(name) is None:
            code_obj = compile(code, '', 'exec')
            code_blob = marshal.dumps(code_obj)

            self.set_var(name, CODE_KEY, value=code)
            self.set_var(name, COMPILED_KEY, value=code_blob)
            self.set_var(name, OWNER_KEY, value=owner)
            self.set_var(name, TIME_KEY, value=timestamp)
            self.set_var(name, DEVELOPER_KEY, value=developer)
예제 #21
0
    def test_integration_into_election_house(self):
        self.client.submit(stamps,
                           constructor_args={
                               'initial_rate': 10000,
                           },
                           owner='election_house')

        self.election_house.register_policy(contract='stamps')

        env = {'now': Datetime._from_datetime(dt.today() + td(days=7))}

        self.election_house.vote(policy='stamps',
                                 value=20000,
                                 signer='stu',
                                 environment=env)
        self.election_house.vote(policy='stamps',
                                 value=5000,
                                 signer='raghu',
                                 environment=env)
        self.election_house.vote(policy='stamps',
                                 value=12000,
                                 signer='alex',
                                 environment=env)
        self.election_house.vote(policy='stamps',
                                 value=13000,
                                 signer='monica',
                                 environment=env)
        self.election_house.vote(policy='stamps',
                                 value=7000,
                                 signer='steve',
                                 environment=env)

        env = {'now': Datetime._from_datetime(dt.today() + td(days=14))}

        self.election_house.vote(policy='stamps',
                                 value=15000,
                                 signer='tejas',
                                 environment=env)

        self.assertEqual(
            self.election_house.current_value_for_policy(policy='stamps'),
            12500)
예제 #22
0
    def generate_environment(self, driver, timestamp, input_hash, bhash='0' * 64, num=1):
        now = Datetime._from_datetime(
            datetime.utcfromtimestamp(timestamp)
        )

        return {
            'block_hash': bhash,
            'block_num': num,
            '__input_hash': input_hash,  # Used for deterministic entropy for random games
            'now': now
        }
예제 #23
0
    def test_voting_again_after_waiting_one_day_works(self):
        self.pending_masters.register(signer='joe')

        self.currency.approve(signer='stu',
                              amount=10_000,
                              to='pending_masters')

        env = {'now': Datetime._from_datetime(dt.today())}

        self.pending_masters.vote(signer='stu', address='joe', environment=env)

        env = {'now': Datetime._from_datetime(dt.today() + td(days=7))}

        self.pending_masters.vote(signer='stu', address='joe', environment=env)

        self.assertEqual(self.currency.balances['stu'], 999998)
        self.assertEqual(self.pending_masters.Q.get()['joe'], 2)
        self.assertEqual(self.currency.balances['blackhole'], 2)
        self.assertEqual(self.pending_masters.S['last_voted', 'stu'],
                         env['now'])
예제 #24
0
    def test_vote_list_sum_100_succeeds(self):
        env = {'now': Datetime._from_datetime(dt.today() + td(days=7))}

        self.election_house.vote(policy='rewards',
                                 value=[20, 20, 20, 20, 20],
                                 environment=env,
                                 signer='v2')
        self.assertEqual(self.rewards.S['current_votes', 'masternodes'], 20)
        self.assertEqual(self.rewards.S['current_votes', 'delegates'], 20)
        self.assertEqual(self.rewards.S['current_votes', 'blackhole'], 20)
        self.assertEqual(self.rewards.S['current_votes', 'foundation'], 20)
        self.assertEqual(self.rewards.S['current_votes', 'developer'], 20)
    def test_last_masternode_returns_least_popular_if_multiple_votes(self):
        self.currency.approve(signer='stu',
                              amount=10_000,
                              to='master_candidates')

        env = {'now': Datetime._from_datetime(dt.today())}
        self.master_candidates.vote_no_confidence(signer='stu',
                                                  address='stux',
                                                  environment=env)

        env = {'now': Datetime._from_datetime(dt.today() + td(days=7))}
        self.master_candidates.vote_no_confidence(signer='stu',
                                                  address='raghu',
                                                  environment=env)

        env = {'now': Datetime._from_datetime(dt.today() + td(days=14))}
        self.master_candidates.vote_no_confidence(signer='stu',
                                                  address='stux',
                                                  environment=env)

        self.assertEqual(self.master_candidates.last_masternode(), 'stux')
예제 #26
0
    def test_vote_starts_election_if_time_to(self):
        self.client.submit(stamps, constructor_args={
            'initial_rate': 10000,
        })

        stamps_contract = self.client.get_contract('stamps')
        env = {'now': Datetime._from_datetime(dt.today() + td(days=7))}
        stamps_contract.vote(vk='stu', obj=20000, environment=env)

        self.assertEqual(stamps_contract.S['election_start_time'], env['now'])
        self.assertEqual(stamps_contract.S['in_election'], True)
        self.assertEqual(stamps_contract.S['votes', 'stu'], 20000)
예제 #27
0
    def test_last_masternode_returns_masternode_with_most_votes_if_none_in_relinquished(
            self):
        self.currency.approve(
            signer='stu', amount=10_000,
            to='elect_members')  # Stu approves spending to vote
        env = {'now': Datetime._from_datetime(dt.today())}
        self.elect_members.vote_no_confidence(
            signer='stu', address='raghu',
            environment=env)  # Stu votes for Bob

        self.assertEqual(self.elect_members.last_member(),
                         'raghu')  # bob is the current top spot
예제 #28
0
    def test_vote_no_motion_fails(self):
        self.submit_members(constructor_args={
            'initial_members': ['a' * 64, 'b' * 64, 'c' * 64],
        })

        mn_contract = self.client.get_contract('masternodes')

        env = {'now': Datetime._from_datetime(dt.today())}

        with self.assertRaises(AssertionError):
            mn_contract.vote(vk='a' * 64,
                             obj=('vote_on_motion', False),
                             environment=env)
예제 #29
0
    def test_vote_if_started_correct_votes_tally_properly(self):
        self.client.submit(stamps, constructor_args={
            'initial_rate': 10000,
        })

        stamps_contract = self.client.get_contract('stamps')

        env = {'now': Datetime._from_datetime(dt.today() + td(days=7))}

        stamps_contract.vote(vk='stu', obj=20000, environment=env)
        stamps_contract.vote(vk='raghu', obj=15000, environment=env)

        self.assertEqual(stamps_contract.S['votes', 'raghu'], 15000)
예제 #30
0
    def test_voting_no_confidence_again_too_soon_throws_assertion_error(self):
        self.currency.approve(signer='stu', amount=10_000, to='elect_members')

        env = {'now': Datetime._from_datetime(dt.today())}

        self.elect_members.vote_no_confidence(signer='stu',
                                              address='raghu',
                                              environment=env)

        with self.assertRaises(AssertionError):
            self.elect_members.vote_no_confidence(signer='stu',
                                                  address='raghu',
                                                  environment=env)