The AVM

The AElf blockchain aims to support multiple virtual machines and programming languages.

The Interface

At the heart of AElf’s smart contract execution lies the ISmartContractBridgeContext interface. This interface defines the API between smart contracts and the host environment. Here’s how it works:

Smart Contract Development

Developers can code smart contracts in any language as long as there’s an SDK that utilizes the ISmartContractBridgeContext interface. This approach allows for language diversity and encourages innovation.

Host Environment

The actual execution of smart contracts occurs within the host environment. The host implements the IHostSmartContractBridgeContext interface, which extends ISmartContractBridgeContext. Through this interface, smart contracts interact with the blockchain, access contract states, and fire events.

Figure 10.0. Contract Context

The C# Virtual Machine (CLR)

In the AElf blockchain ecosystem, C# stands as the default language for crafting smart contracts. During runtime, a specialised version of Common Language Runtime (CLR) executes these C# smart contracts. AElf's runtime, based on the CLR, is aptly named the AElf Virtual Machine. Notably, AElf holds the distinction of being the pioneering blockchain platform to employ this smart contract execution engine.

The Smart Contract Executive

Each deployed smart contract becomes an instance encapsulated within an IExecutive object. The ApplyAsync method of the IExecutive interface handles contract execution. The separation of contract instances ensures efficient management and execution.

The following code snippet depicts the core construct.

public interface ISmartContractExecutiveService
{
    Task<IExecutive> GetExecutiveAsync(IChainContext chainContext, Address address);
}

public interface IExecutive
{
    Task ApplyAsync(ITransactionContext transactionContext);
}

Ensuring Safety: Constraints in the Execution Environment

Safety Measures: The smart contract execution environment isn’t identical to the CLR due to safety considerations. AElf introduces specific constraints to prevent malicious or resource-intensive behavior:

  • Floating Operations: Disallowed to maintain determinism.

  • Whitelisted Libraries: Only approved libraries are allowed.

  • Array Size Limit: Arrays are restricted to 40KB.

  • Conditional Branches Limit: A maximum number of conditional branches.

  • Method Calls Limit: A maximum number of method calls.

You can refer to our repo for a more complete and detailed explanation of these constraints.

The AElf Virtual Machine combines flexibility, safety, and performance, making it a powerful platform for smart contract development. As AElf continues to evolve, its virtual machine will play a pivotal role in shaping the future of decentralized applications.

Last updated