This document explores the implementation changes needed to support and test versioning of system contracts in the Hedera Smart Contract Service.
Currently, the handling of HTS, HAS and HSS system function calls follows similar pattern as described below:
- A particular descendant of the
SystemContractclass is identified by the contract address for processing the call. - A descendant of the
AbstractCallAttemptclass is created from the call data and other information available in the message frame. - The
AbstractCallAttemptdescendant is tested using theidentifyMethodagainst a list of class that implement theCallTranslatorinterface provided by dagger in order to determine which translator is responsible for processing the function call. - The
CallTranslatordescendant decodes and calls the necessary helper classes to either dispatch or process the call. - The
CallTranslatoralso keeps metadata about the system contract function in theSystemContractMethodclass.
The following changes are needed in order to support versioning of system contracts:
- Descendants of the
SystemContractclass will map to more than one contract address. The dagger provided map allows for the mapping of multiple address to a singleSystemContractclass. - Descendants of the
AbstractCallAttemptclass will be augmented with a system contract address field. Otherwise the current commonAbstractCallAttemptand relatedCallFactoryclass descendants will be used commonly by all system contracts of the same type. - The
SystemContractMethodclass metadata will be augmented with an enum set of supported system contract addresses which can include an item that can denote all addresses. - The
isMethodfunction support byCallTranslatordescendants will be augmented to check the system contract address field in theAbstractCallAttemptdescendant in order to determine a matching translator.
In order to organize and support multiple addresses for a system contract, a sensible package must be introduced.
Currently , all CallTranslator descendant classes and related helper classes are packaged together under the system contract package.
Functions supported under a new system contract address for the same system contract type will be placed in a new package with the name address_<system_contract_address>.
The packages at top level for a system contract will contain the base implementation which will be used if no override implementation of the function is available.
For example, the HTS system contract will have the following packages where a new address for the HTS system contract at address 0x16c is introduced and overrides the create function:
...
|--- hts
airdrops
allowance
associations
...
create
...
|--- address_0x16c
create
...
As with the Package Organization discussed above, bdd tests will also need to be organized in order to test variant
expectations for overridden functions in the system contract.
The path for looking up contract bin files uploading contract initcode will be augmented with an optional system contract address
to differentiate contracts using different system contract addresses.
Currently, the path used to locate contracts is hedera-node/test-clients/src/test/resources/contracts.
The path for the new system contract address will be hedera-node/test-clients/src/test/resources/contracts_<system_contract_address> if the optional
system contract address is given to the argument to the uploadInitCode utility function.
Similarly, an attribute will be added to the @Contract annotation to denote the system contract address for the contract.
New tests that that cover the new system contract address' behavior will be added:
- into new class files
- into existing class files using the @Nested annotation to group the tests together.
- into existing tests using the @ParameterizedTest annotation to test the same function with different system contract addresses. as appropriate in order to increase test readability and maintainability.
In order to ensure that the system contract are immutable from a user standpoint, each new version of a system contract needs to be scope in terms of expected behavior. A master feature flag for the version will be used to control release of the new system contract version to gate access as user deployed contract that are written to partially implemented system contract will break once behavior is altered.
The next system contract version for HTS will encompass the following scope:
- HIP-1028 - Metadata HIP
2. Updates to the
HederaTokenstruct with resultant cascading changes to theTokenInfo,FungibleTokenInfoandNonFungibleTokenInfostructs. - Update the HTS static functions to return a response code to indicate invalid input instead of reverting on incorrect input.