Skip to main content

WebAssembly Smart Contracts

Overview​

ArthaChain employs WebAssembly (WASM) as its smart contract runtime environment, providing a secure, efficient, and language-agnostic approach to contract development. The WASM VM offers significant advantages over traditional blockchain VMs, including enhanced performance, formal verification capabilities, and quantum resistance.

Architecture​

The WASM smart contract system is implemented across several modules in blockchain_node/src/wasm/:

blockchain_node/src/wasm/
├── abi.rs # Contract ABI definitions
├── context.rs # Execution context
├── debug.rs # Debugging infrastructure
├── engine.rs # Core VM engine
├── executor.rs # Contract execution logic
├── gas.rs # Gas metering
├── host.rs # Host environment
├── host_functions.rs # Functions callable from contracts
├── mod.rs # Module entry point
├── rpc.rs # RPC interface
├── runtime.rs # Runtime environment
├── standards.rs # Contract standards
├── storage.rs # Contract storage
├── types.rs # Common type definitions
├── upgrade.rs # Contract upgrade mechanisms
├── verification.rs # Formal verification
└── vm.rs # VM implementation

WASM Virtual Machine​

The core VM implementation in vm.rs provides the execution environment for contracts:

pub struct WasmVm {
/// VM configuration
config: WasmVmConfig,
/// Cached modules
modules: HashMap<String, wasmer::Module>,
/// Wasmer store
store: wasmer::Store,
}

Key features include:

  • Memory-Safe Execution: Sandboxed runtime with strict memory boundaries (100 pages/6.4MB max)
  • Deterministic Execution: Guaranteed identical results across all validators
  • JIT Compilation: Just-in-time compilation for optimized performance
  • Bytecode Validation: Strict validation of contract bytecode
  • Metered Execution: Fine-grained gas metering for resource accounting

VM Configuration​

The VM is highly configurable through the WasmVmConfig structure:

pub struct WasmVmConfig {
/// Maximum memory pages (64KB each)
pub max_memory_pages: u32,
/// Maximum gas limit per execution
pub default_gas_limit: u64,
/// Timeout in milliseconds
pub execution_timeout_ms: u64,
/// Maximum WASM module size in bytes
pub max_module_size: usize,
/// Features enabled for WASM execution
pub features: WasmFeatures,
}

Default configuration values:

impl Default for WasmVmConfig {
fn default() -> Self {
Self {
max_memory_pages: 100, // 6.4MB (64KB per page)
default_gas_limit: 10_000_000,
execution_timeout_ms: 5000, // 5 seconds
max_module_size: 2 * 1024 * 1024, // 2MB
features: WasmFeatures::default(),
}
}
}

Contract Execution Environment​

The execution environment provides the contract with access to blockchain state and functions:

pub struct WasmEnv {
/// Storage access for the contract
pub storage: Arc<dyn Storage>,
/// Memory for the contract
pub memory: RefCell<Vec<u8>>,
/// Gas meter for metered execution
pub gas_meter: GasMeter,
/// Call context (caller, block info, etc.)
pub context: CallContext,
/// Contract address
pub contract_address: Address,
/// Caller address
pub caller: Address,
/// State access
pub state: Arc<State>,
/// Current caller
pub caller_str: String,
/// Current contract address
pub contract_address_str: String,
/// Value sent with call
pub value: u64,
/// Contract call data
pub call_data: Vec<u8>,
/// Execution logs
pub logs: Vec<String>,
}

Host Functions​

Contracts interact with the blockchain through host functions defined in host_functions.rs:

// Re-exported in mod.rs
pub use host_functions::{
crypto_verify, get_block_number, get_caller, storage_delete, storage_read, storage_write,
};

These functions provide capabilities like:

  • Storage Access: Read, write and delete contract state
  • Blockchain Context: Access to block information, transaction details
  • Cryptographic Operations: Hash computation, signature verification
  • Caller Information: Details about the transaction sender
  • Contract Interaction: Ability to call other contracts

Smart Contract Development​

Supported Languages​

The WASM VM supports multiple programming languages for contract development:

Rust​

Rust is the primary recommended language for ArthaChain smart contracts due to its memory safety guarantees, performance, and rich type system.

Example Rust Contract:

use artha_sdk::prelude::*;

#[artha_contract]
pub struct TokenContract {
total_supply: u64,
balances: Map<Address, u64>,
}

#[artha_methods]
impl TokenContract {
pub fn new(initial_supply: u64) -> Self {
let mut contract = Self {
total_supply: initial_supply,
balances: Map::new(),
};
let deployer = env::caller();
contract.balances.insert(deployer, initial_supply);
contract
}

pub fn transfer(&mut self, to: Address, amount: u64) -> Result<(), String> {
let from = env::caller();
let from_balance = self.balances.get(&from).unwrap_or(0);

if from_balance < amount {
return Err("Insufficient balance".to_string());
}

self.balances.insert(from, from_balance - amount);
let to_balance = self.balances.get(&to).unwrap_or(0);
self.balances.insert(to, to_balance + amount);

Ok(())
}

pub fn balance_of(&self, account: Address) -> u64 {
self.balances.get(&account).unwrap_or(0)
}
}

AssemblyScript​

AssemblyScript provides a TypeScript-like experience targeting WebAssembly.

C/C++​

For advanced use cases requiring maximum performance or specific optimizations.

Contract ABI​

Contracts expose their interface through a standardized ABI:

pub struct ContractABI {
/// Contract name
pub name: String,
/// Contract version
pub version: String,
/// Contract methods
pub methods: Vec<ABIMethod>,
/// Contract events
pub events: Vec<ABIEvent>,
/// Contract constructor
pub constructor: Option<ABIConstructor>,
}

Contract Execution​

Loading and Execution​

Contract execution follows a well-defined lifecycle:

  1. Loading: Contract bytecode is loaded and validated

    pub fn load_module(
    &mut self,
    contract_address: &str,
    bytecode: &[u8],
    ) -> Result<(), WasmError>
  2. Instantiation: An instance is created with the appropriate environment

    let instance = wasmer::Instance::new(module, &import_object)
    .map_err(|e| WasmError::InstantiationError(e.to_string()))?;
  3. Execution: The specified function is called with provided arguments

    pub fn execute(
    &self,
    contract_address: &str,
    env: WasmEnv,
    function: &str,
    args: &[wasmer::Value],
    ) -> Result<WasmExecutionResult, WasmError>

Gas Metering​

Execution is metered to ensure fair resource usage:

const GAS_PER_INSTRUCTION: u64 = 1;
const MAX_EXECUTION_STEPS: u64 = 10_000_000; // 10 million steps

Gas is charged for:

  • Instruction Execution: Basic computational operations
  • Memory Operations: Allocation, reads, and writes
  • Storage Access: Reading from and writing to persistent storage
  • Host Function Calls: Interactions with the blockchain

Security Features​

Memory Safety​

The VM enforces strict memory isolation:

// From vm.rs
// Add memory
let memory = wasmer::Memory::new(
&self.store,
wasmer::MemoryType::new(1, Some(self.config.max_memory_pages)),
)
.map_err(|e| WasmError::InstantiationError(e.to_string()))?;

Key security features:

  • Memory bounds checking
  • Stack depth limitations
  • Maximum module size enforcement
  • Execution timeout protection

Validation​

All WASM bytecode undergoes rigorous validation before execution:

// From vm.rs
let mut validator = Validator::new_with_features(self.config.features);

// Parse and validate the module
for payload in Parser::new(0).parse_all(bytecode) {
let payload = payload.map_err(|e| WasmError::ValidationError(e.to_string()))?;
validator
.payload(&payload)
.map_err(|e| WasmError::ValidationError(e.to_string()))?;
}

Formal Verification​

The system includes tools for formal verification of contracts:

// From mod.rs
pub use verification::{ContractVerifier, LivenessProperty, SafetyProperty, VerificationResult};

Verification capabilities include:

  • Safety Properties: Ensuring contracts behave as expected
  • Liveness Properties: Guaranteeing progress under fair conditions
  • Model Checking: Verifying against formally specified requirements

Contract Standards​

The system supports standardized interfaces for common contract types:

// From mod.rs
pub use standards::{
ContractStandard, GovernanceStandard, SecurityStandard, StandardRegistry, StandardType,
TokenStandard,
};

Available standards include:

  • Token Standards: Fungible and non-fungible token interfaces
  • Governance Standards: Decentralized governance protocols
  • Security Standards: Enhanced security compliance requirements

Upgradeability​

Contracts can be designed for upgradeability:

// From mod.rs
pub use upgrade::{ContractVersion, StorageLayout, UpgradeManager, UpgradePattern};

Upgrade patterns include:

  • Proxy Pattern: Delegating calls to implementation contracts
  • Diamond Pattern: Multi-facet upgradeable contracts
  • Storage Migration: Upgrading while preserving state

Debugging Tools​

The system includes comprehensive debugging capabilities:

// From mod.rs
pub use debug::{Breakpoint, DebugManager, DebugSession, StackFrame, StackTrace};

Debugging features include:

  • Breakpoints: Pausing execution at specific points
  • Stack Traces: Viewing call history
  • State Inspection: Examining contract memory and storage
  • Gas Profiling: Analyzing gas usage

AI Integration​

ArthaChain uniquely integrates AI capabilities with smart contracts:

Neural Network Interface​

Contracts can access pre-trained models for:

  • Fraud Detection: Identifying suspicious transaction patterns
  • Data Classification: Categorizing on-chain data
  • Anomaly Detection: Flagging unusual contract behaviors
  • Pattern Recognition: Identifying patterns in blockchain data

Model Integration​

The AI integration follows a strict versioning and verification pattern:

  1. Models are deployed to the blockchain with versioning
  2. Contracts specify required model versions
  3. Runtime ensures model compatibility and integrity
  4. Execution provides secure, metered model inference

Performance Characteristics​

The WASM VM is optimized for blockchain execution:

  • Execution Speed: Near-native performance through JIT compilation
  • Memory Efficiency: Minimal memory footprint with explicit allocation
  • Startup Time: Fast instantiation through module caching
  • Determinism: Guaranteed identical execution across all nodes
  • Scalability: Linear scaling with hardware capabilities

Conclusion​

ArthaChain's WebAssembly Smart Contract system provides a secure, flexible, and efficient environment for developing and executing blockchain applications. By leveraging the power of WASM with blockchain-specific optimizations, it enables complex decentralized applications with strong security guarantees.