Skip to content

Commit b8ff476

Browse files
committed
feat: enhance Polkadot smart contract documentation with dual virtual machine architecture overview
1 parent 79f3fe8 commit b8ff476

File tree

1 file changed

+36
-297
lines changed

1 file changed

+36
-297
lines changed
Lines changed: 36 additions & 297 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
---
2+
title: Dual Virtual Machine Stack
3+
description: Overview of Polkadot's dual virtual machine architecture, offering flexibility in choosing the right execution backend for developers.
4+
categories: Basics, Smart Contracts
5+
---
6+
17
# Dual Virtual Machine Stack
28

39
## Introduction
@@ -6,329 +12,62 @@ Polkadot's smart contract platform provides developers with a unique dual virtua
612

713
The dual-stack architecture consists of:
814

9-
- **REVM Backend**: Provides full Ethereum Virtual Machine compatibility, enabling unchanged Solidity contracts and familiar tooling
10-
- **PolkaVM (PVM) Backend**: A RISC-V-based virtual machine optimized for performance and enabling new use cases
15+
- **[REVM Backend](#revm-backend){target=\_blank}**: Provides full Ethereum Virtual Machine compatibility, enabling unchanged Solidity contracts and familiar tooling
16+
- **[PolkaVM Backend](#polkavm-backend){target=\_blank}**: A RISC-V-based virtual machine optimized for performance and enabling new use cases
1117

1218
Both backends share common infrastructure including RPC interfaces, tooling support, and precompiles, ensuring consistent developer experience while offering distinct performance characteristics.
1319

1420
```mermaid
1521
graph TD
16-
subgraph "Smart Contract Platform"
17-
subgraph "Dual VM Architecture"
18-
REVM["REVM Backend<br/>(EVM Compatible)"]
19-
PVM["PolkaVM Backend<br/>(RISC-V Based)"]
20-
end
21-
22-
subgraph "Shared Infrastructure"
23-
RPC["Ethereum JSON-RPC"]
24-
Tools["Developer Tooling<br/>(Hardhat, Foundry)"]
25-
Precompiles["Precompiles<br/>(Polkadot-specific features)"]
26-
end
27-
28-
REVM --> RPC
29-
PVM --> RPC
30-
RPC --> Tools
31-
REVM --> Precompiles
32-
PVM --> Precompiles
33-
end
34-
35-
Dev1["Ethereum Developers"] --> REVM
36-
Dev2["Performance-Focused Developers"] --> PVM
22+
Devs["Developers"] --> REVM["REVM (EVM-compatible)"]
23+
Devs --> PVM["PolkaVM (RISC-V)"]
24+
REVM --> Infra["Shared Infrastructure<br/>(Ethereum JSON-RPC, Tooling, Precompiles)"]
25+
PVM --> Infra
3726
```
3827

39-
## REVM Backend: EVM Compatibility
40-
41-
The REVM backend integrates a complete Ethereum Virtual Machine implementation, enabling Solidity contracts to run unchanged on Polkadot's smart contract platform.
42-
43-
### Key Features
44-
45-
- **Full EVM compatibility**: Execute unmodified Solidity contracts compiled to EVM bytecode
46-
- **Broad Solidity version support**: Compatible with multiple Solidity compiler versions, including older releases
47-
- **Familiar tooling**: Works seamlessly with Ethereum development tools like Hardhat and Foundry
48-
- **Standard bytecode**: Uses traditional EVM bytecode format, ensuring compatibility with existing tools and infrastructure
49-
50-
### Architecture
28+
## REVM Backend
5129

52-
```mermaid
53-
flowchart LR
54-
Dev[Developer] --> |Solidity<br>Source| Solc[Solidity Compiler]
55-
Solc --> |EVM<br>Bytecode| REVM[REVM Backend]
56-
REVM --> |Execution| Runtime[pallet_revive Runtime]
57-
Runtime --> Chain[Polkadot Chain]
58-
```
30+
The [REVM backend](https://github.com/bluealloy/revm){target=\_blank} integrates a complete Ethereum Virtual Machine implementation, enabling Solidity contracts to run unchanged on Polkadot's smart contract platform.
5931

60-
### Use Cases
32+
This allows developers to use their existing Ethereum tooling and infrastructure to build on Polkadot.
6133

6234
The REVM backend is ideal for:
6335

64-
- **Quick migration**: Projects porting existing Ethereum dApps without code modifications
36+
- Quick migration: Projects porting existing Ethereum dApps without code modifications
6537
- **Established tooling**: Teams leveraging mature Ethereum development infrastructure
6638
- **Third-party integrations**: Applications requiring standard EVM bytecode inspection
6739
- **Compatibility requirements**: Contracts needing exact EVM behavior for audited code
6840

69-
### Trade-offs
70-
71-
While REVM provides seamless compatibility, developers should consider:
72-
73-
- **Performance**: EVM's stack-based architecture may not match PVM's register-based efficiency
74-
- **Compute limits**: Bound by traditional EVM computational constraints
75-
- **Innovation path**: Limited to Ethereum's architectural decisions and evolution
76-
77-
## PolkaVM (PVM) Backend: Performance & Innovation
78-
79-
PolkaVM represents a next-generation virtual machine built on RISC-V architecture, optimized for high-performance smart contract execution while maintaining Solidity compatibility through the Revive compiler.
80-
81-
### Core Architecture Differences
82-
83-
PolkaVM introduces fundamental architectural improvements over the EVM:
84-
85-
```mermaid
86-
flowchart TB
87-
subgraph "EVM Architecture"
88-
EVMStack[Stack-Based Operations]
89-
EVM256[256-bit Word Size]
90-
EVMGas[Single Gas Metric]
91-
end
92-
93-
subgraph "PolkaVM Architecture"
94-
PVMReg[Register-Based Operations]
95-
PVM64[64-bit Word Size]
96-
PVMWeight[Multi-Dimensional Resources]
97-
end
98-
99-
EVMStack -.->|vs| PVMReg
100-
EVM256 -.->|vs| PVM64
101-
EVMGas -.->|vs| PVMWeight
102-
```
103-
104-
#### Register-Based Design
105-
106-
PolkaVM's RISC-V register-based approach provides several advantages:
107-
108-
- **Hardware alignment**: Efficient translation to modern CPU architectures through simple 1:1 mapping to x86-64
109-
- **Optimized execution**: Reduced compilation complexity via strategic register limitation
110-
- **Performance gains**: Direct register allocation eliminates stack manipulation overhead
111-
112-
#### 64-bit Word Size
113-
114-
Operating with native 64-bit words enables:
115-
116-
- **Hardware-supported operations**: Direct arithmetic without emulation overhead
117-
- **Compatibility preservation**: Maintains Solidity's 256-bit operations through YUL translation
118-
- **Mixed-language support**: Integration of performance-critical components in lower-level languages
119-
- **Reduced overhead**: Eliminates unnecessary precision costs for standard operations
120-
121-
#### Multi-Dimensional Resource Metering
122-
123-
Unlike EVM's single gas metric, PolkaVM meters three distinct resources:
124-
125-
| Resource | Purpose | Comparison to EVM |
126-
|:--------------------|:---------------------------------------------|:-------------------------|
127-
| **`ref_time`** | Computational time (closest to gas) | Similar to EVM gas |
128-
| **`proof_size`** | State proof size for validator verification | No EVM equivalent |
129-
| **`storage_deposit`** | State bloat management through deposits | Partially similar to gas |
130-
131-
All resources can be limited at the transaction level, and the Ethereum RPC proxy maps all three dimensions into the single gas dimension for seamless user experience.
132-
133-
### Compilation Process
134-
135-
PolkaVM contracts go through a sophisticated compilation pipeline:
136-
137-
```mermaid
138-
flowchart LR
139-
Dev[Developer] --> |Solidity<br>Source| Solc[solc Compiler]
140-
Solc --> |YUL IR| Revive[Revive Compiler]
141-
Revive --> |LLVM IR| LLVM[LLVM Optimizer]
142-
LLVM --> |RISC-V ELF| Linker[PVM Linker]
143-
Linker --> |PVM Blob<br>+ Metadata| Deploy[Deployment]
144-
```
145-
146-
The process integrates:
147-
148-
1. **Solc**: Standard Solidity compiler producing YUL intermediate representation
149-
2. **Revive Compiler**: Transforms YUL IR to LLVM IR for optimization
150-
3. **LLVM**: Industry-standard compiler infrastructure providing optimization passes
151-
4. **PVM Linker**: Produces final PolkaVM blob with metadata
152-
153-
### Execution Model
154-
155-
PolkaVM features dual-mode execution for optimal performance:
156-
157-
- **Interpreter**: Immediate code execution without compilation overhead, ideal for short-running contracts
158-
- **JIT Compiler** (planned): Advanced just-in-time compilation for compute-intensive workloads
159-
- **Lazy interpretation**: Eliminates upfront compilation costs for contracts with minimal execution
160-
161-
### Key Features
162-
163-
#### Performance Optimizations
41+
## PolkaVM Backend
16442

165-
- **Benchmark-based pricing**: Gas values reflect actual execution costs, making computation cheaper relative to I/O
166-
- **Efficient instruction set**: RISC-V instructions map directly to hardware operations
167-
- **Reduced overhead**: Register-based design eliminates stack manipulation costs
43+
[PolkaVM](https://github.com/paritytech/polkavm){target=\_blank} represents a next-generation virtual machine built on [RISC-V](https://en.wikipedia.org/wiki/RISC-V){target=\_blank} architecture, optimized for high-performance smart contract execution while maintaining Solidity compatibility through the Revive compiler.
16844

169-
#### Advanced Memory Management
45+
### Core PVM concepts
17046

171-
PolkaVM implements fixed memory limits per contract:
172-
173-
| Limit | Maximum |
174-
|:---------------------------------------------|:------------|
175-
| Call stack depth | 5 |
176-
| Event topics | 4 |
177-
| Event data payload size (including topics) | 416 bytes |
178-
| Storage value size | 416 bytes |
179-
| Transient storage variables | 128 uint |
180-
| Immutable variables | 16 uint |
181-
| Contract code blob size | ~100 KB |
182-
183-
This approach:
184-
185-
- Provides predictable resource usage for contract nesting calculations
186-
- Enables clear capacity planning for developers
187-
- Eliminates dynamic memory pricing complexity
188-
- May be enhanced with dynamic metering in future iterations
189-
190-
#### On-Chain Constructors
191-
192-
PolkaVM introduces an elegant alternative to runtime code modification:
193-
194-
- Enables contract instantiation without modifying bytecode
195-
- Requires code to be pre-uploaded before instantiation
196-
- Supports factory patterns through code hash references
197-
- Eliminates security risks from runtime code generation
198-
199-
### Use Cases
200-
201-
The PVM backend excels for:
202-
203-
- **Compute-intensive applications**: Games, simulations, complex algorithms
204-
- **Performance-critical contracts**: High-throughput DeFi protocols
205-
- **Innovation-focused projects**: Leveraging Polkadot-specific capabilities
206-
- **Long-term optimization**: Projects prioritizing execution efficiency
207-
208-
### Important Considerations
209-
210-
#### Bytecode Differences
211-
212-
- **Format**: RISC-V bytecode instead of EVM bytecode
213-
- **Inspection tools**: Standard EVM tools cannot parse PVM bytecode
214-
- **Code introspection**: Limited `EXTCODECOPY` support, replaced by on-chain constructors
215-
216-
#### Deployment Model
217-
218-
PVM follows a two-step deployment process:
219-
220-
1. **Code upload**: Contract bytecode must be uploaded to the chain first
221-
2. **Instantiation**: Contracts reference pre-uploaded code via hash
222-
223-
This affects:
224-
225-
- Factory contract patterns (must reference uploaded code hashes)
226-
- Dynamic contract creation (requires pre-uploaded dependencies)
227-
- Runtime code generation (not supported)
228-
229-
#### Gas Model Adaptations
230-
231-
- **Dynamic pricing**: Gas values are benchmark-based, not fixed
232-
- **Cross-contract calls**: Resource limits forwarded automatically, not manually specified
233-
- **Reentrancy protection**: Cannot rely on gas stipends; must implement explicit guards
47+
- **Architecture**: Register-based RISC-V VM with native 64-bit words. Solidity is supported via Revive/LLVM, offering lower overhead than the EVM's stack-based, 256-bit design.
48+
- **Resources (pricing)**: Dimensions like `ref_time` (compute), `proof_size` (state proof), `storage_deposit` (state bloat) are mapped to a single gas number at the RPC layer.
49+
- **Execution**: Interpreter at the time of writing; JIT planned for heavy workloads. Lazy interpretation avoids upfront compilation cost.
50+
- **Deployment**: Two-step flow. First upload code, and then instantiate by code hash. On-chain constructors replace runtime code generation (no dynamic codegen).
51+
- **Limits**: Fixed caps (stack depth, event topics/data, storage value size, transient/immutable vars, code size) for predictable nesting and capacity planning.
52+
- **Tooling/bytecode**: Ethereum JSON-RPC and common tooling work. PVM bytecode is RISC-V (EVM bytecode inspectors won't parse); limited `EXTCODECOPY`.
53+
- **Calls and safety**: Cross-contract calls forward resource limits automatically. Use explicit reentrancy guards; do not rely on gas stipends. Pricing is benchmark-based, making compute cheaper relative to I/O.
23454

23555
## Choosing the Right Backend
23656

23757
The dual-stack architecture allows developers to select the optimal backend based on their specific requirements:
23858

239-
### Choose REVM When:
240-
241-
- Migrating existing Ethereum contracts without modifications
242-
- Requiring exact EVM behavior for audited code
243-
- Using tools that inspect EVM bytecode
244-
- Prioritizing rapid deployment over optimization
245-
- Working with established Ethereum infrastructure
246-
247-
### Choose PolkaVM When:
248-
249-
- Building performance-critical applications
250-
- Requiring intensive computation (games, simulations)
251-
- Optimizing for lower execution costs
252-
- Leveraging Polkadot-specific features
253-
- Focusing on long-term platform innovation
254-
255-
### Shared Capabilities
256-
257-
Both backends provide:
258-
259-
- **Ethereum JSON-RPC**: Standard interface for wallets and dApps
260-
- **Development tooling**: Hardhat and Foundry compatibility
261-
- **Precompiles**: Access to Polkadot-specific functionality (XCM, etc.)
262-
- **18-decimal DOT**: Aligned with Ethereum conventions
263-
264-
```mermaid
265-
graph LR
266-
subgraph "Decision Factors"
267-
Compat[EVM Compatibility Required?]
268-
Perf[Performance Critical?]
269-
Tools[Existing EVM Tooling?]
270-
Compute[High Compute Needs?]
271-
end
272-
273-
Compat -->|Yes| REVM[Choose REVM]
274-
Compat -->|No| Perf
275-
Perf -->|Yes| PVM[Choose PolkaVM]
276-
Perf -->|No| Tools
277-
Tools -->|Yes| REVM
278-
Tools -->|No| Compute
279-
Compute -->|Yes| PVM
280-
Compute -->|No| Either[Either Backend Suitable]
281-
```
282-
283-
## Architecture Comparison Summary
284-
285-
| Feature | REVM Backend | PolkaVM Backend |
286-
|:-------------------------|:---------------------------------------|:---------------------------------------|
287-
| **Instruction Set** | Stack-based EVM | Register-based RISC-V |
288-
| **Bytecode Format** | Standard EVM bytecode | RISC-V format |
289-
| **Compiler** | Standard Solidity compiler | Revive compiler (Solidity support) |
290-
| **Performance** | Traditional EVM performance | Optimized via register-based design |
291-
| **Memory Model** | Dynamic with gas costs | Fixed limits per contract |
292-
| **Resource Metering** | Single gas metric | Multi-dimensional (ref_time, proof_size, storage_deposit) |
293-
| **Contract Deployment** | Single-step deployment | Two-step: upload then instantiate |
294-
| **Code Introspection** | Full EXTCODECOPY support | Limited, alternatives via constructors |
295-
| **Reentrancy Protection**| Gas stipends provide basic protection | Requires explicit guards |
296-
| **Tooling Compatibility**| Native EVM tool support | Requires Revive-aware tools |
297-
| **Solidity Versions** | All versions | Via-IR compilation mode |
298-
| **Innovation Path** | Follows Ethereum evolution | Polkadot-native optimizations |
299-
300-
## Future Development
301-
302-
Both backends continue to evolve:
303-
304-
### REVM Roadmap
305-
306-
- Enhanced gas mapping alignment with PVM
307-
- Ethereum block storage optimization
308-
- Expanded precompile library
309-
- Performance benchmarking against reference implementations
310-
311-
### PolkaVM Roadmap
312-
313-
- JIT compiler implementation for advanced optimization
314-
- Dynamic memory metering for deeper contract nesting
315-
- Additional language support beyond Solidity
316-
- Extended precompile ecosystem
317-
318-
### Shared Infrastructure
319-
320-
- Anvil-compatible local test nodes
321-
- Comprehensive test suites from major Ethereum applications
322-
- Differential testing against reference implementations
323-
- Stress testing and benchmarking tools
59+
- Choose REVM when:
32460

325-
## Getting Started
61+
- Migrating existing Ethereum contracts without modifications
62+
- Requiring exact EVM behavior for audited code
63+
- Using tools that inspect EVM bytecode
64+
- Prioritizing rapid deployment over optimization
65+
- Working with established Ethereum infrastructure
32666

327-
Developers can begin building on either backend:
67+
- Choose PolkaVM when:
32868

329-
1. **Development environment**: Set up Hardhat or Foundry with Polkadot smart contract platform
330-
2. **Backend selection**: Choose REVM for compatibility or PVM for performance
331-
3. **Local testing**: Use Anvil-compatible test nodes for both backends
332-
4. **Deployment**: Deploy to testnet (Kusama) before mainnet (Polkadot)
69+
- Building performance-critical applications
70+
- Requiring intensive computation
71+
- Optimizing for lower execution costs
33372

33473
The dual-stack architecture ensures developers have the flexibility to optimize for their specific use case while maintaining access to Polkadot's advanced capabilities through shared infrastructure and precompiles.

0 commit comments

Comments
 (0)