3. Testing
We provide a comprehensive testing suite for Substreams modules. The testing suite facilitates end-to-end testing, ensuring your Substreams modules function as expected. For unit tests, please use standard Rust unit testing practices.
What is tested?
The testing suite runs Tycho Indexer with your provided Substream implementation for a specific block range and verifies that the end state matches the expected state specified on the testing YAML file. This confirms that your substreams package is indexable and outputs what you're expecting.
Next it simulates transactions using Tycho Simulation engine. This will verify that the that all necessary data was indexed as well as the functionality of the provided SwapAdapter
contract.
It is important to understand that the simulation engine runs entirely offchain and only has access to the data and contracts you index (token contracts are mocked and need not be indexed).
Test Configuration
Inside your substreams directory you'll need an integration_test.tycho.yaml file. This test template file already outlines everything you need, however for clarity some test configs are expanded upon here:
1. skip_balance_check
skip_balance_check
By default this should be false. During testing the balances reported for the component are verified by comparing them to the onchain balances of the Component.id. This should be set to false if:
the Component.id does not correlate to a contract address
balances are not stored on the component's contract (i.e. they're stored on a vault)
If this skip is set to true, it is required that you comment the reason why.
2. initialized_accounts
initialized_accounts
Set to a list of addresses of contracts that are required during simulation, but their creation is not indexed within the test block range. Leave empty if not required.
It is important to note that this config is used during testing only. It is expected that the accounts listed here are still properly initialised by your substreams package. This configuration only eliminates the need to include historical blocks containing the initialisation events in your test data. This is useful for ensuring tests are targeted and quick to run.
The initialized_accounts
config can be used on 2 levels in the test configuration file:
global: the accounts listed here are used for all tests in this suite
test level: the accounts listed here are scoped to that test only
5. expected_components
expected_components
A list of the components whose creation you are testing, including all component data (tokens, static attributes etc). You do not need to include all components created within your test block range, only the ones you wish to focus on in the test.
4. skip_simulation
skip_simulation
By default this should be set to false and should only be set to true temporarily if you want to isolate testing the indexing phase only, or for extenuating circumstances (such as you are testing indexing a pool type that simulation does not support yet). If set to true, it is required to comment the reason why.
Troubleshooting
Slow tests
At the most, an integration test is expected to take 5-10 minutes to run. If the tests take noticeably longer than that, there are a few key things you can look into:
ensure you have no infinite loops within your code.
ensure you are using a small block range for your test, ideally below 1000 blocks. The blocks you include in your test need to cover only the creation of the component you are testing and optionally extend to blocks with changes for that component that you want the test to cover. To help keep the test block range small, it might be useful for you to look into the initialized_accounts config.
make sure you are not indexing tokens. Token contracts tend to use a lot of storage, so are slow to fetch historical data for. Instead they are mocked on the simulation engine and do not need to be explicitly indexed. Exceptions include if they have unique behavior, such as can act as both a token and a pool, or rebasing tokens that provide a
getRate
method.
Note: substreams uses cache to improve the speed of subsequent runs of the same module. The first run of a test will always be slower than subsequent runs (unless you adjust the substreams module).
Account not initialised
There are 2 main causes for this error:
your substream package is not indexing a contract that is needed for simulations
your test starts at a block that is later than the block the contract is created on. To fix this, add the missing contract to the initialized_accounts test config.
Debugging
For enhanced debugging, running the testing module with the --tycho-logs flag is recommended. It will enable Tycho-indexer logs.
Last updated