logo

Class Structure

Restrictions for Simplified Code Checks#

Inheritance Rules#

  • Only one inheritance is allowed from ContractBase, generated by the contract plugin as a nested type in ContractContainer.
  • Only one inheritance is allowed from CSharpSmartContract.
  • Only one inheritance is allowed from ContractState.
  • The type inherited from ContractState should be the element type of the CSharpSmartContract generic instance type.
  • If these rules are not followed, code deployment will fail.

    Field Usage Limitations#

    In Contract Implementation Class#

  • Initial values for non-readonly, non-constant fields are not allowed (for both static and non-static fields). This is because their value will reset to 0 or null after the first execution, losing the initial value.
  • Allowed:

    1
    class MyContract : MyContractBase
    2
    {
    3
    int test;
    4
    static const int test = 2;
    5
    }

    Not Allowed:

    1
    class MyContract : MyContractBase
    2
    {
    3
    ! int test = 2;
    4
    }
    1
    class MyContract : MyContractBase
    2
    {
    3
    int test;
    4
    5
    public MyContract
    6
    {
    7
    ! test = 2;
    8
    }
    9
    }
  • Only primitive types or the following types are allowed for readonly/constant fields:
  • In Non-Contract Classes (Classes not inheriting from ContractBase<T>)#

  • Initial values for non-readonly, non-constant static fields are not allowed. They reset to 0 or null after the first execution, losing the initial value.
  • Allowed:

    1
    class AnyClass
    2
    {
    3
    static int test;
    4
    }

    Not Allowed:

    1
    class AnyClass
    2
    {
    3
    ! static int test = 2;
    4
    }
    1
    class AnyClass
    2
    {
    3
    static int test;
    4
    5
    public AnyClass
    6
    {
    7
    ! test = 2;
    8
    }
    9
    }
  • Exception: Fields with FileDescriptor types are allowed due to protobuf-generated code. These fields don’t have a readonly modifier and write access to them is allowed only from the constructor of the declaring type.
  • Allowed:

    1
    public class TestType
    2
    {
    3
    private static FileDescriptor test;
    4
    5
    public class TestType
    6
    {
    7
    test = ...
    8
    }
    9
    }

    Not Allowed:

    1
    public class TestType
    2
    {
    3
    private static FileDescriptor test;
    4
    5
    public TestType
    6
    {
    7
    test = ...
    8
    }
    9
    10
    ! public void SetFromSomeWhereElse(FileDescriptor input)
    11
    ! {
    12
    ! test = input;
    13
    ! }
    14
    }
  • Accessing test fields is restricted to the declaring type’s constructor only.
  • Only the following types are allowed for readonly/constant static fields:
  • Note: T can only be a primitive type.

  • Exception: If a type has a readonly field of the same type as itself, it is only allowed if the type has no instance fields (to support LINQ-related generated types).
  • Allowed:

    1
    public class TestType
    2
    {
    3
    private static readonly TestType test;
    4
    5
    private static int i;
    6
    }

    Not Allowed:

    1
    public class TestType
    2
    {
    3
    private static readonly TestType test;
    4
    5
    ! private int i;
    6
    }

    In Contract State#

    In contract state, only the following types are allowed:

    Primitive Types

  • BoolState
  • Int32State
  • UInt32State
  • Int64State
  • UInt64State
  • StringState
  • BytesState
  • Complex Types
  • Complex Types

  • SingletonState<T>
  • ReadonlyState<T>
  • MappedState<T, T>
  • MappedState<T, T, T>
  • MappedState<T, T, T, T>
  • MappedState<T, T, T, T, T>
  • MethodReference<T, T>
  • ProtobufState<T>
  • ContractReferenceState
  • Edited on: 15 July 2024 03:32:22 GMT+0