MiniEVM is a lightweight implementation of the Ethereum Virtual Machine (EVM) written in Go. It provides a minimal, yet functional environment for executing EVM bytecode, making it ideal for educational purposes, testing, and research. While not intended for production use, it implements core EVM features including stack operations, memory management, and gas metering.
- π Overview
- β¨ Features
- ποΈ Architecture
- π Installation
- π» Usage
- π Project Structure
- π Implementation Details
- β½ Gas Costs
- π€ Contributing
- π£οΈ Roadmap
- π License
- Basic EVM execution environment
- Stack-based operation handling (push/pop)
- Memory management
- Storage operations
- Gas metering and management
- Basic arithmetic operations (ADD, SUB, MUL, DIV)
- Program counter management
- State management and execution flow control
- Complete implementation of all EVM opcodes
- Contract creation and deployment
- Event logging system
- Memory expansion and gas calculation
- Advanced arithmetic operations (ADDMOD, MULMOD, EXP)
- Bitwise operations
- Jump operations with proper validation
- Exception handling mechanisms
- Contract calling mechanisms (CALL, DELEGATECALL, STATICCALL)
- State reverting capabilities
- SHA3 hashing operations
- Block information access
- Extended testing framework
The MiniEVM is built with a modular architecture consisting of several key components:
-
State Management (
state.go)- Maintains execution context
- Handles program counter
- Manages gas consumption
- Controls execution flow
-
Stack (
types.go)- Implementation:
Stackstruct - Maximum depth: 1024 items
- Basic operations: Push, Pop
- Stack overflow protection
- Implementation:
-
Memory (
types.go)- Dynamic memory model
- 32-byte word alignment
- Automatic expansion
- Bounds checking
-
Storage (
types.go)- Key-value storage model
- Persistent state storage
- 32-byte keys and values
-
Operation Codes (
opcodes.go)- Complete EVM opcode definitions
- Grouped by functionality
- Includes gas costs
# Clone the repository
git clone https://github.com/blockfuselabs/evm.git
# Change to project directory
cd evm
# Install dependencies
go mod download
# Build the project
go build ./cmd/clipackage main
import (
"fmt"
"github.com/blockfuselabs/evm/types"
)
func main() {
// Create new EVM instance with initial parameters
evm := types.NewEvmState(
"0x1234...", // sender address
[]byte{0x60, 0x03, 0x60, 0x02, 0x01, 0x00}, // bytecode
1000, // gas limit
100, // value
[]uint8{}, // calldata
)
// Execute the bytecode
evm.Run()
// Print results
fmt.Printf("Remaining Gas: %d\nStack: %v\n", evm.Gas, evm.Stack.Data)
}// Example of executing custom bytecode
bytecode := []byte{
types.PUSH1, 0x03, // Push 3 onto stack
types.PUSH1, 0x02, // Push 2 onto stack
types.ADD, // Add top two stack items
types.STOP, // Stop execution
}
evm := types.NewEvmState("sender", bytecode, 1000, 0, nil)
evm.Run()evm/
βββ bin/ # Compiled binaries
βββ cmd/
β βββ cli/
β βββ main.go # CLI entry point
βββ types/
βββ types.go # Core type definitions
βββ methods.go # EVM operations implementation
βββ opcodes.go # Opcode definitions
βββ state.go # State management
The stack implementation follows EVM specifications:
- Maximum depth of 1024 items
- Each item is 32 bytes
- Automatic overflow checking
- Panic on stack overflow
func (s *Stack) Push(data byte) {
if len(s.Data) >= int(MaximumDepth) {
panic("stack overflow")
}
s.Data = append(s.Data, data)
}Memory is managed in 32-byte words:
- Dynamic expansion
- Gas cost calculation for expansion
- Automatic alignment
- Bounds checking on access
Gas costs are implemented according to the Ethereum Yellow Paper:
- Basic operations: 3 gas
- Memory expansion: dynamic cost
- Storage operations: high cost
- Complex operations: variable cost
We welcome contributions! Please follow these steps:
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to your branch
- Create a Pull Request
Please ensure your code:
- Follows Go best practices
- Includes tests
- Is properly documented
- Passes all existing tests
- Complete implementation of arithmetic operations
- Add comprehensive testing suite
- Implement memory expansion with proper gas calculation
- Add contract creation capabilities
- Implement call operations
- Add event logging system
- Implement remaining EVM opcodes
- Add advanced debugging features
- Performance optimizations
This project is licensed under the MIT License - see the LICENSE file for details.