Example #1
0
def test_migrated_chain_fixture(project_dir, write_project_file, request,
                                MATH):
    write_project_file('contracts/Math.sol', MATH['source'])
    write_project_file('migrations/__init__.py')

    class TestMigration(Migration):
        migration_id = '0001_initial'
        dependencies = []

        operations = [
            DeployContract('Math'),
        ]

        compiled_contracts = {
            'Math': {
                'code': MATH['code'],
                'code_runtime': MATH['code_runtime'],
                'abi': MATH['abi'],
            },
        }

    with open('migrations/0001_initial.py', 'w') as migration_file:
        write_migration(migration_file, TestMigration)

    project = request.getfuncargvalue('project')
    assert len(project.migrations) == 1

    unmigrated_chain = request.getfuncargvalue('unmigrated_chain')

    registrar = unmigrated_chain.registrar
    assert not registrar.call().exists('contract/Math')

    chain = request.getfuncargvalue('chain')

    assert registrar.call().exists('contract/Math')
Example #2
0
def migration_0003(tester_chain, migration_0002, MATH):
    project = tester_chain.project
    migrations_dir = project.migrations_dir

    class Migration0003(Migration):
        migration_id = '0003_deploy_alternate_math'
        dependencies = ['0002_deploy_v2_math']
        operations = [
            DeployContract('Math', contract_registrar_name='FancyMath'),
        ]
        compiled_contracts = {
            'Math': MATH,
        }

    migration_filename = os.path.join(migrations_dir,
                                      '0003_deploy_alternate_math.py')

    with open(migration_filename, 'w') as migration_0003_file:
        write_migration(migration_0003_file, Migration0003)

    assert len(project.migrations) == 3

    migrations_to_execute = get_migration_classes_for_execution(
        project.migrations,
        tester_chain,
    )

    assert len(migrations_to_execute) == 1

    the_migration_instance = migrations_to_execute[0]
    the_migration_instance.execute()

    return Migration0003
Example #3
0
def migration_0001(tester_chain, MATH):
    project = tester_chain.project
    migrations_dir = project.migrations_dir

    class Migration0001(Migration):
        migration_id = '0001_deploy_v1_math'
        dependencies = []
        operations = [
            DeployContract('Math'),
        ]
        compiled_contracts = {
            'Math': MATH,
        }

    migration_filename = os.path.join(migrations_dir, '0001_deploy_v1_math.py')

    with open(migration_filename, 'w') as migration_0001_file:
        write_migration(migration_0001_file, Migration0001)

    assert len(project.migrations) == 1

    migrations_to_execute = get_migration_classes_for_execution(
        project.migrations,
        tester_chain,
    )

    assert len(migrations_to_execute) == 1

    the_migration_instance = migrations_to_execute[0]
    the_migration_instance.execute()

    return Migration0001
def test_migrated_chain_fixture(project_dir, write_project_file, request,
                                MATH):
    write_project_file('contracts/Math.sol', MATH['source'])
    write_project_file('migrations/__init__.py')

    class TestMigration(Migration):
        migration_id = '0001_initial'
        dependencies = []

        operations = [
            DeployContract('Math'),
        ]

        compiled_contracts = {
            'Math': {
                'code': MATH['code'],
                'code_runtime': MATH['code_runtime'],
                'abi': MATH['abi'],
            },
        }

    with open('migrations/0001_initial.py', 'w') as migration_file:
        write_migration(migration_file, TestMigration)

    project = request.getfuncargvalue('project')
    assert len(project.migrations) == 1

    unmigrated_chain = request.getfuncargvalue('unmigrated_chain')

    registrar = unmigrated_chain.registrar
    assert not registrar.call().exists('contract/Math')

    chain = request.getfuncargvalue('chain')

    assert registrar.call().exists('contract/Math')
def migration_0001(testrpc_chain, MATH):
    project = testrpc_chain.project
    migrations_dir = project.migrations_dir

    class Migration0001(Migration):
        migration_id = '0001_deploy_v1_math'
        dependencies = []
        operations = [
            DeployContract('Math'),
        ]
        compiled_contracts = {
            'Math': MATH,
        }
    migration_filename = os.path.join(migrations_dir, '0001_deploy_v1_math.py')

    with open(migration_filename, 'w') as migration_0001_file:
        write_migration(migration_0001_file, Migration0001)

    assert len(project.migrations) == 1

    migrations_to_execute = get_migration_classes_for_execution(
        project.migrations,
        testrpc_chain,
    )

    assert len(migrations_to_execute) == 1

    the_migration_instance = migrations_to_execute[0]
    the_migration_instance.execute()

    return Migration0001
def migration_0003(testrpc_chain, migration_0002, MATH):
    project = testrpc_chain.project
    migrations_dir = project.migrations_dir

    class Migration0003(Migration):
        migration_id = '0003_deploy_alternate_math'
        dependencies = ['0002_deploy_v2_math']
        operations = [
            DeployContract('Math', contract_registrar_name='FancyMath'),
        ]
        compiled_contracts = {
            'Math': MATH,
        }
    migration_filename = os.path.join(migrations_dir, '0003_deploy_alternate_math.py')

    with open(migration_filename, 'w') as migration_0003_file:
        write_migration(migration_0003_file, Migration0003)

    assert len(project.migrations) == 3

    migrations_to_execute = get_migration_classes_for_execution(
        project.migrations,
        testrpc_chain,
    )

    assert len(migrations_to_execute) == 1

    the_migration_instance = migrations_to_execute[0]
    the_migration_instance.execute()

    return Migration0003
Example #7
0
def make_migration(ctx, migration_name):
    """
    Generate an empty migration.
    """
    project = ctx.obj['PROJECT']

    next_migration_number = get_next_migration_number(project.project_dir)

    if migration_name is None:
        if next_migration_number == 1:
            migration_name = 'initial'
        else:
            migration_name = 'auto'

    migration_id = "{num:04d}_{migration_name}".format(
        num=next_migration_number,
        migration_name=migration_name,
    )

    migration_filename = "{migration_id}.py".format(migration_id=migration_id)

    if not is_valid_migration_filename(migration_filename):
        ctx.fail(
            "Migration filenames may only contain letters, numbers, and "
            "underscores: {0!r}".format(migration_filename)
        )

    migration_file_path = os.path.join(project.migrations_dir, migration_filename)
    if os.path.exists(migration_file_path):
        ctx.fail((
            "Unexpectedly found duplicate migration name: {0}".format(
                os.path.abspath(migration_file_path),
            )
        ))

    compiled_contracts = project.compiled_contracts

    new_migration_dict = {
        'migration_id': migration_id,
        'operations': [],
        'dependencies': [],
        'compiled_contracts': compiled_contracts,
    }

    if project.migrations:
        new_migration_dict['dependencies'].append(
            project.migrations[-1].migration_id,
        )

    new_migration = type('Migration', (Migration,), new_migration_dict)

    with open(migration_file_path, 'w') as file_obj:
        write_migration(file_obj, new_migration)

    click.echo(
        "Wrote new migration to: {0}".format(os.path.relpath(
            migration_file_path, project.project_dir,
        ))
    )
def make_migration(ctx, migration_name):
    """
    Generate an empty migration.
    """
    project = ctx.obj['PROJECT']

    next_migration_number = get_next_migration_number(project.project_dir)

    if migration_name is None:
        if next_migration_number == 1:
            migration_name = 'initial'
        else:
            migration_name = 'auto'

    migration_id = "{num:04d}_{migration_name}".format(
        num=next_migration_number,
        migration_name=migration_name,
    )

    migration_filename = "{migration_id}.py".format(migration_id=migration_id)

    if not is_valid_migration_filename(migration_filename):
        ctx.fail("Migration filenames may only contain letters, numbers, and "
                 "underscores: {0!r}".format(migration_filename))

    migration_file_path = os.path.join(project.migrations_dir,
                                       migration_filename)
    if os.path.exists(migration_file_path):
        ctx.fail(("Unexpectedly found duplicate migration name: {0}".format(
            os.path.abspath(migration_file_path), )))

    compiled_contracts = project.compiled_contracts

    new_migration_dict = {
        'migration_id': migration_id,
        'operations': [],
        'dependencies': [],
        'compiled_contracts': compiled_contracts,
    }

    if project.migrations:
        new_migration_dict['dependencies'].append(
            project.migrations[-1].migration_id, )

    new_migration = type('Migration', (Migration, ), new_migration_dict)

    with open(migration_file_path, 'w') as file_obj:
        write_migration(file_obj, new_migration)

    click.echo("Wrote new migration to: {0}".format(
        os.path.relpath(
            migration_file_path,
            project.project_dir,
        )))
def test_serialize_empty_migration():
    class TestMigration(Migration):

        migration_id = "0001_initial"
        dependencies = []
        operations = []
        compiled_contracts = {
            "ContractA": {"abi": [], "code": "0x1234", "code_runtime": "0x1234"},
            "ContractB": {"abi": [], "code": "0x5678", "code_runtime": "0x5678"},
        }

    f = StringIO()
    write_migration(f, TestMigration)
    assert f.getvalue() == EXPECTED_EMPTY_MIGRATION_CONTENT
Example #10
0
def test_serialize_empty_migration():
    class TestMigration(Migration):

        migration_id = '0001_initial'
        dependencies = []
        operations = []
        compiled_contracts = {
            'ContractA': {
                'abi': [],
                'code': '0x1234',
                'code_runtime': '0x1234',
            },
            'ContractB': {
                'abi': [],
                'code': '0x5678',
                'code_runtime': '0x5678',
            },
        }

    f = StringIO()
    write_migration(f, TestMigration)
    assert f.getvalue() == EXPECTED_EMPTY_MIGRATION_CONTENT
Example #11
0
def test_load_project_migrations(project_dir, MATH):
    migrations_dir = get_migrations_dir(project_dir)
    migration_0001_file_path = os.path.join(migrations_dir, '0001_initial.py')
    migration_0002_file_path = os.path.join(migrations_dir,
                                            '0002_the_second_migration.py')
    migration_0003_file_path = os.path.join(migrations_dir,
                                            '0003_the_third_migration.py')

    class MigrationA(Migration):
        migration_id = '0001_initial'
        dependencies = []
        operations = []
        compiled_contracts = {
            'Math': MATH,
        }

    class MigrationB(Migration):
        migration_id = '0002_the_second_migration'
        dependencies = ['0001_initial']
        operations = []
        compiled_contracts = {
            'Math': MATH,
        }

    class MigrationC(Migration):
        migration_id = '0003_the_third_migration'
        dependencies = ['0002_the_second_migration']
        operations = []
        compiled_contracts = {
            'Math': MATH,
        }

    with open(migration_0001_file_path, 'w') as migration_0001_file:
        write_migration(migration_0001_file, MigrationA)

    with open(migration_0002_file_path, 'w') as migration_0002_file:
        write_migration(migration_0002_file, MigrationB)

    with open(migration_0003_file_path, 'w') as migration_0003_file:
        write_migration(migration_0003_file, MigrationC)

    migration_classes = load_project_migrations(project_dir)

    assert len(migration_classes) == 3
    actual_ids = {m.migration_id for m in migration_classes}

    expected_ids = {
        '0001_initial', '0002_the_second_migration', '0003_the_third_migration'
    }

    assert actual_ids == expected_ids
Example #12
0
def test_load_project_migrations(project_dir, MATH):
    migrations_dir = get_migrations_dir(project_dir)
    migration_0001_file_path = os.path.join(migrations_dir, '0001_initial.py')
    migration_0002_file_path = os.path.join(migrations_dir, '0002_the_second_migration.py')
    migration_0003_file_path = os.path.join(migrations_dir, '0003_the_third_migration.py')

    class MigrationA(Migration):
        migration_id = '0001_initial'
        dependencies = []
        operations = []
        compiled_contracts = {
            'Math': MATH,
        }

    class MigrationB(Migration):
        migration_id = '0002_the_second_migration'
        dependencies = ['0001_initial']
        operations = []
        compiled_contracts = {
            'Math': MATH,
        }

    class MigrationC(Migration):
        migration_id = '0003_the_third_migration'
        dependencies = ['0002_the_second_migration']
        operations = []
        compiled_contracts = {
            'Math': MATH,
        }

    with open(migration_0001_file_path, 'w') as migration_0001_file:
        write_migration(migration_0001_file, MigrationA)

    with open(migration_0002_file_path, 'w') as migration_0002_file:
        write_migration(migration_0002_file, MigrationB)

    with open(migration_0003_file_path, 'w') as migration_0003_file:
        write_migration(migration_0003_file, MigrationC)

    migration_classes = load_project_migrations(project_dir)

    assert len(migration_classes) == 3
    actual_ids = {m.migration_id for m in migration_classes}

    expected_ids = {'0001_initial', '0002_the_second_migration', '0003_the_third_migration'}

    assert actual_ids == expected_ids
Example #13
0
def test_migrate_cmd(project_dir, write_project_file, MATH):
    class MigrationA(migrations.Migration):
        migration_id = '0001_deploy_math'
        dependencies = []

        operations = [
            migrations.DeployContract('Math'),
        ]

        compiled_contracts = {
            'Math': MATH,
        }

    class MigrationB(migrations.Migration):
        migration_id = '0002_increment'
        dependencies = ['0001_deploy_math']

        operations = [
            migrations.TransactContract(
                contract_name='Math',
                method_name='increment',
                arguments=[3],
                contract_address=migrations.Address.defer(key='contract/Math'),
                timeout=30,
            ),
        ]

        compiled_contracts = {
            'Math': MATH,
        }


    write_project_file('contracts/Math.sol', MATH['source'])
    write_project_file('migrations/__init__.py')

    migration_0001_path = os.path.join(
        project_dir, 'migrations', '0001_deploy_math.py',
    )
    with open(migration_0001_path, 'w') as migration_0001:
        write_migration(migration_0001, MigrationA)

    migration_0002_path = os.path.join(
        project_dir, 'migrations', '0002_increment.py',
    )
    with open(migration_0002_path, 'w') as migration_0002:
        write_migration(migration_0002, MigrationB)

    write_project_file('populus.ini', '[chain:local_a]')

    project = Project()

    # sanity
    assert len(project.migrations) == 2

    with project.get_chain('local_a') as chain:
        chain.wait.for_unlock(chain.web3.eth.coinbase, timeout=30)
        project.config.set('chain:local_a', 'deploy_from', chain.web3.eth.coinbase)
        RegistrarFactory = get_registrar(web3=chain.web3)
        deploy_transaction_hash = RegistrarFactory.deploy()
        registrar_address = chain.wait.for_contract_address(deploy_transaction_hash, timeout=60)
        project.config.set('chain:local_a', 'registrar', registrar_address)
        project.write_config()

    runner = CliRunner()
    result = runner.invoke(main, ['migrate', 'local_a'])

    assert result.exit_code == 0, result.output + str(result.exception)

    with project.get_chain('local_a') as chain:
        registrar = chain.registrar
        math_address = registrar.call().getAddress('contract/Math')
        Math = chain.contract_factories.Math(address=math_address)
        assert Math.call().counter() == 3
def test_migrate_cmd(project_dir, write_project_file, MATH):
    class MigrationA(migrations.Migration):
        migration_id = '0001_deploy_math'
        dependencies = []

        operations = [
            migrations.DeployContract('Math'),
        ]

        compiled_contracts = {
            'Math': MATH,
        }

    class MigrationB(migrations.Migration):
        migration_id = '0002_increment'
        dependencies = ['0001_deploy_math']

        operations = [
            migrations.TransactContract(
                contract_name='Math',
                method_name='increment',
                arguments=[3],
                contract_address=migrations.Address.defer(key='contract/Math'),
                timeout=30,
            ),
        ]

        compiled_contracts = {
            'Math': MATH,
        }

    write_project_file('contracts/Math.sol', MATH['source'])
    write_project_file('migrations/__init__.py')

    migration_0001_path = os.path.join(
        project_dir,
        'migrations',
        '0001_deploy_math.py',
    )
    with open(migration_0001_path, 'w') as migration_0001:
        write_migration(migration_0001, MigrationA)

    migration_0002_path = os.path.join(
        project_dir,
        'migrations',
        '0002_increment.py',
    )
    with open(migration_0002_path, 'w') as migration_0002:
        write_migration(migration_0002, MigrationB)

    write_project_file('populus.ini', '[chain:local_a]')

    project = Project()

    # sanity
    assert len(project.migrations) == 2

    with project.get_chain('local_a') as chain:
        chain.wait.for_unlock(chain.web3.eth.coinbase, timeout=30)
        project.config.set('chain:local_a', 'deploy_from',
                           chain.web3.eth.coinbase)
        RegistrarFactory = get_registrar(web3=chain.web3)
        deploy_transaction_hash = RegistrarFactory.deploy()
        registrar_address = chain.wait.for_contract_address(
            deploy_transaction_hash, timeout=60)
        project.config.set('chain:local_a', 'registrar', registrar_address)
        project.write_config()

    runner = CliRunner()
    result = runner.invoke(main, ['migrate', 'local_a'])

    assert result.exit_code == 0, result.output + str(result.exception)

    with project.get_chain('local_a') as chain:
        registrar = chain.registrar
        math_address = registrar.call().getAddress('contract/Math')
        Math = chain.contract_factories.Math(address=math_address)
        assert Math.call().counter() == 3