Skip to main content

Aspect

Aspect introduces a dynamic mechanism to implement system-level features on blockchain platforms. Essentially, an Aspect is akin to a system extension, executed in WebAssembly (WASM), which can be bound to any smart contract. This capability provides flexibility to augment the target contract's capabilities or to monitor its activities, initiating specific actions when predefined conditions are met.

What is Aspect?

The power of Aspect is rooted in WebAssembly (WASM). On Artela, we've developed a dedicated WASM runtime, named Aspect-Runtime, specifically for executing Aspects on the platform. The Aspect Runtime enables interactions between the Aspect and the core blockchain modules through a set of predefined host APIs. These APIs empower the Aspect with various abilities, such as reading the blockchain state, initiating calls to smart contracts, managing its own state, and more.

For those with a background in web development, the concept of "middleware" may be familiar. For newcomers, let's explore this concept further.

Interceptor in Web Framework

Interceptor is code that can be integrated into a web server to extend its functionalities. Imagine using interceptor to add authentication or logging features to a web server, as shown below:

Interceptor

When an HTTP request is received, the web server's interceptor (or in some framework may be named as middleware) mechanism allows developers to create modules that process this incoming request, either before or after its main handling. For example, an authentication interceptor might validate a user's credentials before the request reaches the core logic. This design pattern decentralizes functionalities like authentication and logging, ensuring a more modular approach.

This interceptor setup also supports a shared context, enabling communication between different middlewares or route handlers. For instance, after authenticating a user, the relevant interceptor can store user data in the shared context. Subsequent interceptor or route handlers can then access this data without reloading it.

Aspect in Artela

Drawing parallels, Aspect can be seen as a smart contract's interceptor. Imagine developing a decentralized exchange on Artela and integrating Aspects:

Aspect

Just as interceptor enhance web frameworks, Aspects can also enhance your dApps. They enable modularization and foster inter-module communication via a shared context.

How Does Aspect Work?

Extension Layer

In Artela, we provide a built-in extension layer that sits in-between the application layer (which consists of smart contracts) and the base layer (core modules of a blockchain, e.g., consensus, mempool, P2P networking). This extension layer allows developers to develop chain-level extensions to customized transaction processing workflow to a specific smart contract.

Layers

Aspect Core

The Aspect Core is a system module that manages the lifecycles of Aspects, including deployment, execution, binding, upgrading, and destruction. It is also exposed as a system contract deployed at 0x0000000000000000000000000000000000A27E14, which can be invoked with an EVM contract call. The implementation of Aspect Core is written in native code to reduce the overhead of VM bootstrap.

Aspect Core

Aspect Lifecycle

The lifecycle of an Aspect is managed by Aspect Core. It consists of the following stages:

  • Compilation: The Aspect is compiled into WASM bytecode by the Aspect Compiler.
  • Deployment: The compiled WASM bytecode can be deployed to the blockchain and initialized with certain properties.
  • Binding: To be triggered at join points, the Aspect must be bound to a Smart Contract. This binding process is done by calling the bind method of Aspect Core.
  • Execution: The execution of an Aspect can be triggered either at predefined join points or by making a direct call to the Aspect's operation interface.

For more details about the lifecycle of an Aspect, please refer to the documentation here.

What Can Aspect Do?

Insert a 'Just-in-Time' Call

A Just-in-Time (JIT) call is a type of call that can be inserted into the current EVM call stack at Contract Call Level Join Points. For a detailed explanation of the JIT call concept, please refer to the documentation here.

Sharing information with other Aspects and Smart Contracts

Although Aspect and Smart Contract execution environments are distinct (WASM vs. EVM), they are not completely isolated. Information can be shared between Aspects and Smart Contracts in with Aspect Context. Aspect Context is essentially a transient storage whose lifecycle is only current transaction. The Aspect Context can be used to enables two-way communication between Aspect and Smart Contract.

For more details on this topic, please read the documentation here.

Query from EVM Smart Contract

Aspect can make read-only calls to EVM smart contract with latest state (historical state currently is not allowed). In some cases, like if you are building a price feed oracle Aspect, you can utilize this feature to query the latest price from the oracle contract and save it to the Aspect Context and share it with Smart Contract. For more details on this topic, please read the documentation here.

State Change Tracing

Aspects can trace the changes of a Smart Contract's state. For instance, an Aspect can check if the committing state of a Smart Contract is as expected, based on the input parameters.

This tracing is facilitated by extra opcodes and IR methods generated by the ASOLC compiler:

ASOLC

For more information on this topic, please refer to the ASOLC documentation here.

Maintain State Like Smart Contracts

Similar to a smart contract, Aspect is also stateful. Each Aspect maintain its own state that will be persisted in the world state. Currently the Aspect only provides a simple key-value storage, a better storage binding will provided in a later version. To persist data into the blockchain world state, you can use the following way:

// read & write state
let state = sys.aspect.mutableState.get<string>('state-key');
let unwrapped = state.unwrap();
state.set('state-value');

Processing Calls

An Aspect can also operate independently. It can process external transactions or calls with its bytes-in-bytes-out operation method:

class AspectTest implements IAspectOperation {
operation(input: OperationInput): Uint8Array {
// handle incoming request and echo it back
return data;
}
}
note

Please note the operation method is only a bytes-in-bytes-out entrypoint, if there is any sensitive data in the Aspect, make sure you are making the authentication correctly.

More to know

The initial version of Artela Aspect is built using AssemblyScript (a subset of TypeScript, with strict typing). To facilitate easier development of Aspects, we've also created the Aspect Tooling, a library and toolset for developers to interact with our lower-level host APIs. For more details, you can check out our repos.