Ejemplo n.º 1
0
    def test_variables_for_contract_passes_election_house(self):
        code = '''
# Convenience
I = importlib

#Policies
policies = Hash()

# Policy interface
policy_interface = [
    I.Func('vote', args=('vk', 'obj')),
    I.Func('current_value')
]

@export
def register_policy(contract: str):
    if policies[contract] is None:
        # Attempt to import the contract to make sure it is already submitted
        p = I.import_module(contract)

        # Assert ownership is election_house and interface is correct
        assert I.owner_of(p) == ctx.this, \
            'Election house must control the policy contract!'

        assert I.enforce_interface(p, policy_interface), \
            'Policy contract does not follow the correct interface'

        policies[contract] = True
    else:
        raise Exception('Policy already registered')

@export
def current_value_for_policy(policy: str):
    assert policies.get(policy) is not None, 'Invalid policy.'
    p = I.import_module(policy)

    return p.current_value()

@export
def vote(policy: str, value: Any):
    # Verify policy has been registered
    assert policies.get(policy) is not None, 'Invalid policy.'
    p = I.import_module(policy)

    p.vote(vk=ctx.caller, obj=value)
    '''

        compiled = self.compiler.parse_to_code(code)

        got = parser.variables_for_contract(compiled)

        expected = {
            'variables': [],
            'hashes': ['policies']
        }

        self.assertDictEqual(got, expected)
Ejemplo n.º 2
0
    async def get_variables(self, request, contract):
        contract_code = self.client.raw_driver.get_contract(contract)

        if contract_code is None:
            return response.json(
                {'error': '{} does not exist'.format(contract)},
                status=404,
                headers={'Access-Control-Allow-Origin': '*'})

        variables = parser.variables_for_contract(contract_code)

        return response.json(variables,
                             headers={'Access-Control-Allow-Origin': '*'})
Ejemplo n.º 3
0
    def test_variables_for_contract_multiple_hashes(self):
        code = '''
h1 = Hash()
h2 = Hash()
h3 = Hash()

@export
def something():
   return 1
        '''

        compiled = self.compiler.parse_to_code(code)

        got = parser.variables_for_contract(compiled)

        expected = {'variables': [], 'hashes': ['h1', 'h2', 'h3']}

        self.assertDictEqual(got, expected)
Ejemplo n.º 4
0
    def test_variables_for_contract_multiple_variables(self):
        code = '''
v1 = Variable()
v2 = Variable()
v3 = Variable()

@export
def something():
   return 1
        '''

        compiled = self.compiler.parse_to_code(code)

        got = parser.variables_for_contract(compiled)

        expected = {'variables': ['v1', 'v2', 'v3'], 'hashes': []}

        self.assertDictEqual(got, expected)