Every Ante Test needs to implement the IAnteTest.sol interface to work with Ante. To simplify the test writing process, we've provided an abstract classAnteTest.sol that any Ante Test can inherit.
// SPDX-License-Identifier: GPL-3.0-only// ┏━━━┓━━━━━┏┓━━━━━━━━━┏━━━┓━━━━━━━━━━━━━━━━━━━━━━━// ┃┏━┓┃━━━━┏┛┗┓━━━━━━━━┃┏━━┛━━━━━━━━━━━━━━━━━━━━━━━// ┃┗━┛┃┏━┓━┗┓┏┛┏━━┓━━━━┃┗━━┓┏┓┏━┓━┏━━┓━┏━┓━┏━━┓┏━━┓// ┃┏━┓┃┃┏┓┓━┃┃━┃┏┓┃━━━━┃┏━━┛┣┫┃┏┓┓┗━┓┃━┃┏┓┓┃┏━┛┃┏┓┃// ┃┃ ┃┃┃┃┃┃━┃┗┓┃┃━┫━┏┓━┃┃━━━┃┃┃┃┃┃┃┗┛┗┓┃┃┃┃┃┗━┓┃┃━┫// ┗┛ ┗┛┗┛┗┛━┗━┛┗━━┛━┗┛━┗┛━━━┗┛┗┛┗┛┗━━━┛┗┛┗┛┗━━┛┗━━┛// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━pragmasolidity ^0.8.0;/// @title The interface for the Ante V0.6 Ante Test/// @notice The Ante V0.6 Ante Test wraps test logic for verifying fundamental invariants of a protocolinterface IAnteTest {/// @notice Emitted when the test author is changed/// @param previousAuthor The address of the previous author/// @param newAuthor The address of the new authoreventTestAuthorChanged(addressindexed previousAuthor, addressindexed newAuthor);/// @notice Function containing the logic to set the AnteTest state and call checkTestPasses/// @param _state The encoded data required to set the test state/// @return A single bool indicating if the Ante Test passes/failsfunctionsetStateAndCheckTestPasses(bytesmemory_state) externalreturns (bool);/// @notice Function containing test logic to inspect the protocol invariant/// @dev This should usually return True/// @return A single bool indicating if the Ante Test passes/failsfunctioncheckTestPasses() externalreturns (bool);/// @notice Returns the author of the Ante Test/// @dev This overrides the auto-generated getter for testAuthor as a public var/// @return The address of the test authorfunctiontestAuthor() externalviewreturns (address);/// @notice Sets the author of the Ante Test/// @dev This can only be called by the current author, which is the deployer initially/// @param _testAuthor The address of the test authorfunctionsetTestAuthor(address_testAuthor) external;/// @notice Returns the name of the protocol the Ante Test is testing/// @dev This overrides the auto-generated getter for protocolName as a public var/// @return The name of the protocol in string formatfunctionprotocolName() externalviewreturns (stringmemory);/// @notice Returns a single address in the testedContracts array/// @dev This overrides the auto-generated getter for testedContracts [] as a public var/// @param i The array index of the address to return/// @return The address of the i-th element in the list of tested contractsfunctiontestedContracts(uint256 i) externalviewreturns (address);/// @notice Returns the name of the Ante Test/// @dev This overrides the auto-generated getter for testName as a public var/// @return The name of the Ante Test in string formatfunctiontestName() externalviewreturns (stringmemory);/// @notice Returns a string of comma delimited types used for setting the AnteTest state/// @return The types of the state variablesfunctiongetStateTypes() externalpurereturns (stringmemory);/// @notice Returns a string of comma delimited names used for setting the AnteTest state/// @return The names of the state variablesfunctiongetStateNames() externalpurereturns (stringmemory);}
IAnteTest.sol as of 2023-01-19
All Ante Tests must follow the interface outlined in IAnteTest.sol to work with Ante. The interface outlines 8 main components:
setStateAndCheckTestPasses(bytes): this wrapper function takes a state parameter, updates the Ante Test's internal state and calls checkTestPasses() in a single transaction. (Not all tests need state, most don't!)
checkTestPasses(): the core function that checks if the invariant holds. This is expected to return True. A False return value indicates a failure. NB: Reverts are not considered test failures.
testAuthor: this overrides the auto-generated getter and allows testAuthor to be used as a public variable.
setTestAuthor: this allows the current author to set the testAuthor to a different address.
protocolName: this overrides the auto-generated getter and allows the optional parameter protocolName to be used as a public variable.
testedContracts: this overrides the auto-generated getter for the optional public variable testedContracts.
testName: this overrides the auto-generated getter and allows testName to be used as a public variable.
getStateTypes: this returns a string of comma-delimited types for Ante Tests that need state. This defines the expected format of the data encoded in the bytes parameter passed in to setStateAndCheckTestPasses(bytes)
getStateNames: this returns a string of comma-delimited names for Ante Tests that need state. These names should describe what state values the test expects and are displayed as input labels to the test verifier in the Ante app. NB: There must a name label for each corresponding type.
To aid in working with the IAnteTest.sol interface, we've created an AnteTest.solabstract class that any Ante Test can inherit.
// SPDX-License-Identifier: GPL-3.0-only// ┏━━━┓━━━━━┏┓━━━━━━━━━┏━━━┓━━━━━━━━━━━━━━━━━━━━━━━// ┃┏━┓┃━━━━┏┛┗┓━━━━━━━━┃┏━━┛━━━━━━━━━━━━━━━━━━━━━━━// ┃┗━┛┃┏━┓━┗┓┏┛┏━━┓━━━━┃┗━━┓┏┓┏━┓━┏━━┓━┏━┓━┏━━┓┏━━┓// ┃┏━┓┃┃┏┓┓━┃┃━┃┏┓┃━━━━┃┏━━┛┣┫┃┏┓┓┗━┓┃━┃┏┓┓┃┏━┛┃┏┓┃// ┃┃ ┃┃┃┃┃┃━┃┗┓┃┃━┫━┏┓━┃┃━━━┃┃┃┃┃┃┃┗┛┗┓┃┃┃┃┃┗━┓┃┃━┫// ┗┛ ┗┛┗┛┗┛━┗━┛┗━━┛━┗┛━┗┛━━━┗┛┗┛┗┛┗━━━┛┗┛┗┛┗━━┛┗━━┛// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━pragmasolidity ^0.8.0;import"./interfaces/IAnteTest.sol";/// @title Ante V0.6 Ante Test smart contract/// @notice Abstract inheritable contract that supplies syntactic sugar for writing Ante Tests/// @dev Usage: contract YourAnteTest is AnteTest("String descriptor of test") { ... }abstractcontractAnteTestisIAnteTest {/// @inheritdoc IAnteTestaddresspublicoverride testAuthor;/// @inheritdoc IAnteTeststringpublicoverride testName;/// @inheritdoc IAnteTeststringpublicoverride protocolName;/// @inheritdoc IAnteTestaddress[] publicoverride testedContracts;/// @dev testedContracts and protocolName are optional parameters which should/// be set in the constructor of your AnteTest/// @param _testName The name of the Ante Testconstructor(stringmemory_testName) { testAuthor = msg.sender; testName = _testName; }/// @inheritdoc IAnteTestfunctionsetStateAndCheckTestPasses(bytesmemory_state) externaloverridereturns (bool) {if (_state.length >0) {_setState(_state); }returncheckTestPasses(); }/// @notice Returns the testedContracts array of addresses/// @return The list of tested contracts as an array of addressesfunctiongetTestedContracts() externalviewreturns (address[] memory) {return testedContracts; }/// @inheritdoc IAnteTestfunctionsetTestAuthor(address_testAuthor) external {require(msg.sender == testAuthor,"Only the current testAuthor can set a new test author");require(_testAuthor !=address(0),"ANTE: Test author cannot be the zero address");address previousAuthor = testAuthor; testAuthor = _testAuthor;emitTestAuthorChanged(previousAuthor, _testAuthor); }/// @inheritdoc IAnteTestfunctiongetStateTypes() externalpurevirtualoverridereturns (stringmemory) {return""; }/// @inheritdoc IAnteTestfunctiongetStateNames() externalpurevirtualoverridereturns (stringmemory) {return""; }/// @inheritdoc IAnteTestfunctioncheckTestPasses() publicvirtualoverridereturns (bool passes) {}/// @notice Function containing the logic to set the AnteTest statefunction_setState(bytesmemory) internalvirtual {}}
AnteTest.sol as of 2023-01-28
The AnteTest.sol is an abstract contract that once inherited, can be used to write any Ante Test. Just pass in a descriptive name for the test, designated by _testName. The optional parameters protocolName and testedContracts can be set in the constructor of your Ante Test.
Deriving from AnteTest.sol
You can use it by declaring your Ante Test like the following:
import"@antefinance/contracts/interfaces/AnteTest.sol";contractAnteNewProtocolTestisAnteTest("Stringdescriptoroftest") {// create Ante Test variables & insert logic here// you can omit your constructor if not defining these optional parametersconstructor () { protocolName ="My Protocol"; testedContracts = [0x000000000000000000000000000000000000fEeD]; }functioncheckTestPasses() publicviewoverridereturns (bool) {// insert logic here to check the My Protocol invariant }}
Stateful tests
When writing an Ante Test that needs internal state values, you can write:
import"@antefinance/contracts/interfaces/AnteTest.sol";contractAnteNewProtocolTestWithStateisAnteTest("Stringdescriptoroftest") {// declare any variables needed to test the invariant herestringpublic myStringStateVariable;uintpublic myUintStateVariable;// you can omit your constructor if not defining these optional parametersconstructor () { protocolName ="My Protocol"; testedContracts = [0x000000000000000000000000000000000000fEeD]; }// decode the encoded bytes and set the state variablesfunction_setState(bytesmemory_state) internaloverride { (string _myStringStateVariable,uint _myUintStateVariable) = abi.decode( _state, (string,uint) ); myStringStateVariable = _myStringStateVariable; myUintStateVariable = _myUintStateVariable; }functioncheckTestPasses() publicviewoverridereturns (bool) {// insert logic here to check the My Protocol invariant }// tell verifiers which encoded types they need to pass infunctiongetStateTypes() externalpureoverridereturns (stringmemory) {return"string,uint"; }// tell verifiers what state variable names they need to set values forfunctiongetStateNames() externalpureoverridereturns (stringmemory) {return"myStringStateVariable,myUintStateVariable"; }}
We'll see in the next section, how to use this abstract contract in some Ante Test examples.