Class AvmRule

  • All Implemented Interfaces:
    org.junit.rules.TestRule

    public final class AvmRule
    extends java.lang.Object
    implements org.junit.rules.TestRule
    TestRule to handle the boilerplate operations of testing with an embedded avm.

    If declared with @Rule annotation, the kernel and avm are instantiated for each for each test.

    Otherwise, when declared with @ClassRule annotation, the kernel and avm are instantiated once for the test class.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  AvmRule.ResultWrapper
      An abstract container of the result of running a transaction or call on AvmRule.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      org.aion.avm.core.AvmImpl avm  
      org.aion.kernel.TestingState kernel  
    • Constructor Summary

      Constructors 
      Constructor Description
      AvmRule​(boolean debugMode)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      org.junit.runners.model.Statement apply​(org.junit.runners.model.Statement statement, org.junit.runner.Description description)  
      AvmRule.ResultWrapper balanceTransfer​(Address from, Address to, java.math.BigInteger value, long energyLimit, long energyPrice)
      Makes a balance transfer.
      AvmRule.ResultWrapper call​(Address from, Address dappAddress, java.math.BigInteger value, byte[] transactionData)
      Makes a call transaction to the Dapp.
      AvmRule.ResultWrapper call​(Address from, Address dappAddress, java.math.BigInteger value, byte[] transactionData, long energyLimit, long energyPrice)
      Makes a call transaction to the Dapp.
      AvmRule.ResultWrapper deploy​(Address from, java.math.BigInteger value, byte[] dappBytes)
      Deploys the Dapp.
      AvmRule.ResultWrapper deploy​(Address from, java.math.BigInteger value, byte[] dappBytes, long energyLimit, long energyPrice)
      Deploys the Dapp.
      void disableAutomaticBlockGeneration()
      Disables automatic generation of blocks for each transaction
      byte[] getDappBytes​(java.lang.Class<?> mainClass, byte[] arguments, int abiVersion, java.lang.Class<?>... otherClasses)
      Retrieves bytes corresponding to the in-memory representation of Dapp jar.
      byte[] getDappBytes​(java.lang.Class<?> mainClass, byte[] arguments, java.lang.Class<?>... otherClasses)
      Retrieves bytes corresponding to the in-memory representation of Dapp jar.
      byte[] getDappBytesWithoutOptimization​(java.lang.Class<?> mainClass, byte[] arguments, java.lang.Class<?>... otherClasses)
      Retrieves bytes corresponding to the in-memory representation of Dapp jar.
      Address getPreminedAccount()  
      Address getRandomAddress​(java.math.BigInteger initialBalance)
      Creates an account with an initial balance in the kernel
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • kernel

        public org.aion.kernel.TestingState kernel
      • avm

        public org.aion.avm.core.AvmImpl avm
    • Constructor Detail

      • AvmRule

        public AvmRule​(boolean debugMode)
        Parameters:
        debugMode - enable/disable the debugging features
    • Method Detail

      • apply

        public org.junit.runners.model.Statement apply​(org.junit.runners.model.Statement statement,
                                                       org.junit.runner.Description description)
        Specified by:
        apply in interface org.junit.rules.TestRule
      • getDappBytes

        public byte[] getDappBytes​(java.lang.Class<?> mainClass,
                                   byte[] arguments,
                                   java.lang.Class<?>... otherClasses)
        Retrieves bytes corresponding to the in-memory representation of Dapp jar. This uses the base ABI compiler version.
        Parameters:
        mainClass - Main class of the Dapp to include and list in manifest (can be null).
        arguments - Constructor arguments
        otherClasses - Other classes to include (main is already included).
        Returns:
        Byte array corresponding to the optimized deployable Dapp jar and arguments, where unreachable classes and methods are removed from the jar
      • getDappBytes

        public byte[] getDappBytes​(java.lang.Class<?> mainClass,
                                   byte[] arguments,
                                   int abiVersion,
                                   java.lang.Class<?>... otherClasses)
        Retrieves bytes corresponding to the in-memory representation of Dapp jar.
        Parameters:
        mainClass - Main class of the Dapp to include and list in manifest (can be null).
        arguments - Constructor arguments
        abiVersion - Version of ABI compiler to use
        otherClasses - Other classes to include (main is already included).
        Returns:
        Byte array corresponding to the optimized deployable Dapp jar and arguments, where unreachable classes and methods are removed from the jar
      • getDappBytesWithoutOptimization

        public byte[] getDappBytesWithoutOptimization​(java.lang.Class<?> mainClass,
                                                      byte[] arguments,
                                                      java.lang.Class<?>... otherClasses)
        Retrieves bytes corresponding to the in-memory representation of Dapp jar. This is always using the latest ABI version.
        Parameters:
        mainClass - Main class of the Dapp to include and list in manifest (can be null).
        arguments - Constructor arguments
        otherClasses - Other classes to include (main is already included).
        Returns:
        Byte array corresponding to the deployable Dapp jar and arguments.
      • deploy

        public AvmRule.ResultWrapper deploy​(Address from,
                                            java.math.BigInteger value,
                                            byte[] dappBytes,
                                            long energyLimit,
                                            long energyPrice)
        Deploys the Dapp.
        Parameters:
        from - Address of the deployer account
        value - Value to transfer to the Dapp
        dappBytes - Byte array corresponding to the Dapp jar
        energyLimit - Maximum energy to be used for deployment
        energyPrice - Energy price to be used for deployment
        Returns:
        Result of the operation
      • deploy

        public AvmRule.ResultWrapper deploy​(Address from,
                                            java.math.BigInteger value,
                                            byte[] dappBytes)
        Deploys the Dapp. Energy limit is set to the maximum allowed in the kernel for deploying contracts
        Parameters:
        from - Address of the deployer account
        value - Value to transfer to the Dapp
        dappBytes - Byte array corresponding to the Dapp jar
        Returns:
        Result of the operation
      • call

        public AvmRule.ResultWrapper call​(Address from,
                                          Address dappAddress,
                                          java.math.BigInteger value,
                                          byte[] transactionData,
                                          long energyLimit,
                                          long energyPrice)
        Makes a call transaction to the Dapp.
        Parameters:
        from - Address of the account calling the Dapp
        dappAddress - Address of the Dapp
        value - Value to transfer with the call
        transactionData - The encoded byte array that contains the method descriptor, argument descriptor and encoded arguments, according the Aion ABI format.
        energyLimit - Maximum energy to be used for the call
        energyPrice - Energy price to be used for the call
        Returns:
        Result of the operation
      • call

        public AvmRule.ResultWrapper call​(Address from,
                                          Address dappAddress,
                                          java.math.BigInteger value,
                                          byte[] transactionData)
        Makes a call transaction to the Dapp. Energy limit is set to the maximum allowed in the kernel for a call transaction
        Parameters:
        from - Address of the account calling the Dapp
        dappAddress - Address of the Dapp
        value - Value to transfer with the call
        transactionData - The encoded byte array that contains the method descriptor, argument descriptor and encoded arguments, according the Aion ABI format.
        Returns:
        Result of the operation
      • balanceTransfer

        public AvmRule.ResultWrapper balanceTransfer​(Address from,
                                                     Address to,
                                                     java.math.BigInteger value,
                                                     long energyLimit,
                                                     long energyPrice)
        Makes a balance transfer.
        Parameters:
        from - Address of sender
        to - Address of the receiver
        value - Transfer amount
        energyLimit - Maximum energy to be used for the call
        energyPrice - Energy price to be used for the transfer
        Returns:
        Result of the operation
      • getRandomAddress

        public Address getRandomAddress​(java.math.BigInteger initialBalance)
        Creates an account with an initial balance in the kernel
        Parameters:
        initialBalance - Initial balance of the created account
        Returns:
        Address of the newly created account
      • getPreminedAccount

        public Address getPreminedAccount()
        Returns:
        Address of the account with initial (pre-mined) balance in the kernel
      • disableAutomaticBlockGeneration

        public void disableAutomaticBlockGeneration()
        Disables automatic generation of blocks for each transaction