-
Notifications
You must be signed in to change notification settings - Fork 0
PVM
prasad-kumkar edited this page May 5, 2025
·
1 revision
The jam.pvm module implements the Polkadot Virtual Machine (PVM), which provides the execution environment for smart contracts and services in Tessera.
The PVM implementation consists of several key components:
The core VM for executing WebAssembly (Wasm) code:
- Wasm Runtime: Execute WebAssembly bytecode
- Memory Management: Manage memory allocation and access
- Instruction Execution: Execute Wasm instructions
- Stack Machine: Implement the Wasm stack machine
Host functions provide the interface between the VM and the node:
- Storage Access: Read and write state storage
- Cryptographic Operations: Access cryptographic primitives
- Environmental Information: Access chain and block information
- Logging and Debugging: Emit logs and debug information
The contract interface manages interaction with smart contracts:
- Contract Deployment: Deploy new contracts to the chain
- Contract Execution: Execute contract methods
- Contract Upgrades: Handle contract upgrades and migrations
- Contract State: Manage contract state
from jam.pvm import PVM, ContractExecutor
# Initialize PVM
pvm = PVM()
# Deploy a contract
contract_code = load_wasm_code("contract.wasm")
contract_address = pvm.deploy_contract(contract_code, constructor_args=[42])
# Execute a contract method
result = pvm.execute_contract(
contract_address,
method="transfer",
args=["0xalice", "0xbob", 100]
)
# Use the contract executor for more options
executor = ContractExecutor(pvm)
result = executor.execute(
contract_address,
method="transfer",
args=["0xalice", "0xbob", 100],
gas_limit=1000000,
value=0
)The PVM manages the full lifecycle of a contract:
- Compilation: Compile high-level language to Wasm
- Deployment: Deploy Wasm bytecode to the chain
- Initialization: Initialize contract state
- Execution: Execute contract methods
- Upgrades: Upgrade contract code when needed
- Termination: Terminate and remove contracts
The PVM includes a gas system to limit computation:
- Gas Metering: Track resource usage during execution
- Gas Limits: Enforce maximum gas usage
- Gas Pricing: Calculate gas costs for operations
# Execute with gas limit
result = pvm.execute_contract(
contract_address,
method="complex_calculation",
args=[input_data],
gas_limit=1000000
)
# Check gas usage
print(f"Gas used: {result.gas_used}")The PVM provides detailed error handling:
from jam.pvm import PVMError, ContractError
try:
result = pvm.execute_contract(
contract_address,
method="transfer",
args=["0xalice", "0xbob", 100]
)
except ContractError as e:
# Contract-specific error
print(f"Contract error: {e}")
except PVMError as e:
# General PVM error
print(f"PVM error: {e}")The PVM includes precompiled contracts for common operations:
- Cryptography: Elliptic curve operations, hashing
- Verification: Signature verification
- Data Structures: Merkle tree operations
The module provides tools for contract development:
from jam.pvm import ContractTester
# Create a contract tester
tester = ContractTester(contract_code)
# Test a method
result = tester.test_method(
method="transfer",
args=["0xalice", "0xbob", 100],
initial_state={"balances": {"0xalice": 1000, "0xbob": 500}}
)
# Assert on the result
assert result.success
assert result.return_value == True
assert result.state["balances"]["0xalice"] == 900
assert result.state["balances"]["0xbob"] == 600