Test Environment Configuration for db: memdown()

When using Truffle directly, one can speed up the tests significantly by configuring Ganache to avoid disk I/O operations in file truffle-config.js:

const memdown = require('memdown');

module.exports = {
    ...
    networks: {
        ...
        development: {
            ...
            provider: ganache.provider({
                ...
                db: memdown()
            })
        }
    }
};

I am looking for a way to apply the same on your test environment.

Adding the above configuration in file test-environment.config.js does not seem to work.
An error is thrown in low-level file deferred-leveldown.js.

The Test Environment Configuration page does not explain how to do that.

Can you please explain how this can be achieved?

Thanks

1 Like

Hi @barakman,

It isn't possible currently, there are only a small number of supported options:

I have created an issue for this: https://github.com/OpenZeppelin/openzeppelin-test-environment/issues/145

The use-case is when you have a tremendously large test-suite (i.e., a ton of tests) which takes hours on end to complete.
In such scenario, you’d want your tests to run as fast as possible.
The db: memdown() option disables all disk read/write operations in ganache, turning your tests ballistically faster.
The downside (from my observation) is that you need to run your node process (NodeJS, not the Ganache node) configured with a higher amount of memory.

For example, prior to switching to OZ test environment (when we were using Truffle directly), we had in our package.json file:

"scripts": {
    "build": "node --max-old-space-size=4096 node_modules/truffle/build/cli.bundled.js compile",
    "test": "node --max-old-space-size=4096 node_modules/truffle/build/cli.bundled.js test",
    "coverage": "node --max-old-space-size=4096 node_modules/truffle/build/cli.bundled.js run coverage"
}

So supporting memdown on your side would need to be accompanied with --max-old-space-size=4096 in some way (or perhaps you can add that as yet another option in your test environment configuration file).

Thank you very much for your quick response, by the way.
Do I need to add all of this to the issue that you’ve opened on GitHub?

1 Like

Hi @barakman, it’s not possible to customize Test Environment in this way probably because the options that you specify under node in test-environment.config.js are serialized before sending them to the Ganache subprocess.

We would have to change the way we load the config file so it is loaded in the Ganache subprocess.

We won’t have time to work on this feature for now but I’m happy to guide you through it if you’re interested in contributing it.

1 Like

@frangio:

Hi, thank you very much.
Yes, I am interested (provided that it’s not going to be an extremely long task).

Here is my progress so far, up until I read your message (yes, I have realized that you’re serializing this object).

I can easily workaround this problem by editing file deferred-leveldown.js and adding the following line at the beginning of function DeferredLevelDOWN:

db.db = require('memdown')();

Of course, you can also declare const memdown = require('memdown'); at the beginning of the file and then add db.db = memdown(); at the beginning of the function, but that’s really just semantics.

I could add an npm-postinstall script on my end, to edit that file and “patch” it with the fix above, but this workaround is of course not guaranteed to be backward-compatible and work correctly on the next version of the OZ Test Environment.

So I’d be happy if you could guide me through on that one.
I suppose that your actual source code is in TypeScript (as I recall from previously contributing to one of your other packages - solidity-docgen), but that’s not an issue as far as I’m concerned.

Thanks again :slight_smile:

1 Like

Okay so here’s what I think we should do.

Instead of sending the entire config to the subprocess as in:

We should only send accountsConfig. The rest of the values in config.node should be obtained directly in setupServer by running await import('./config'), and merged with the accounts that were received:

1 Like

Thank you.

Applying that, I get an Exceeds block gas limit runtime error at the beginning of the test.

Here is my OZ Test Environment configuration file, which might be relevant to this issue:

const memdown = require('memdown');

module.exports = {
    accounts: {
        amount: 10,
        ether: 10000000000000000000
    },

    contracts: {
        type: 'truffle',
        defaultGas: 9500000,
        defaultGasPrice: 20000000000,
        artifactsDir: 'project/build/contracts'
    },

    node: {
        db: memdown(),
        gasLimit: 9500000,
        gasPrice: 20000000000
    }
};

Any idea what might I be doing wrong?

Side note: I had to leave coverage: config.coverage as part of the options object in order for it to compile, but I doubt that it makes any difference; I ensured that by removing this field in the corresponding compilation output (JS) file.

I also tried keeping the gasPrice field (in other words, I tried removing only the ...config.node field), but got the same results.

Thanks

1 Like

@frangio:

Update: with a little more effort and digging-in, I was able to solve this.
Can you please authorize me to submit a PR on this repo?
Thanks

@barakman You should fork the repository and push your changes to your own fork. You’ll be able to open a PR from there.

1 Like

GitHub does not give me the option of requesting review from specific users, so the PR is at https://github.com/OpenZeppelin/openzeppelin-test-environment/pull/146.

Also, I’m am not so sure how to extend the relevant test (I could not find any ganache-related tests).

Thanks

1 Like

@barakman Please try version 0.1.7 now and let us know if it goes well with the db option.

1 Like

It works!
Provided that I add db: memdown() in my configuration file, execution running time is reduced by x4 per it.
Thank you very much!!! :slight_smile:

2 Likes