Detailed Architecture
Overview
OP_NET observes Bitcoin blocks and derives deterministic state according to its own ruleset, without requiring any modifications to Bitcoin's protocol. This is fundamentally different from metaprotocols (which rely on indexers that can disagree) and Layer 2s (which require bridges and trust assumptions).
OP_NET Four Layers
The OP_NET architecture is divided into four layers that work together to process transactions, execute smart contracts, and maintain state consistency across the network.
Layer 1: The Transaction Layer (OP_NET Nodes)
At its foundation, OP_NET utilizes Bitcoin transactions as its data transport mechanism. Deploying a smart contract or interacting with one involves creating specialized Bitcoin transactions that carry the necessary data.
Transaction Structure
OP_NET currently leverages Taproot's script-path spending capabilities to embed data directly within Bitcoin transactions. Future releases will extend support to additional transaction types, including SegWit and Legacy formats.
- Smart Contract Deployment: A transaction containing the smart contract's compiled WebAssembly bytecode is created and broadcast to the network.
- Smart Contract Interaction: A transaction containing the function call, parameters, and additional OP_NET-specific data is created and broadcast.
Since Bitcoin does not support native smart contract addresses, OP_NET generates deterministic addresses using the P2OP format. These addresses are derived from a combination of:
- The deployer's public key.
- A random salt.
- The smart contract bytecode hash.
This approach (similar to Ethereum's CREATE2 mechanism) ensures each smart contract has a unique, predictable address that cannot be exploited to redirect funds.
Layer 2: The Execution Layer (OP_VM)
Above the transaction layer is OP_VM, the virtual machine responsible for smart contract execution. When OP_NET nodes detect smart contract related transactions within Bitcoin blocks, these transactions are passed to OP_VM for processing.
OP_VM executes WebAssembly bytecode within an isolated environment. This design provides two key benefits:
- Language Flexibility: Smart contracts can be written in multiple languages (AssemblyScript, Rust, and others) that compile to WebAssembly.
- Deterministic Execution: The isolated environment ensures that every node executing the same smart contract with identical inputs produces exactly the same result.
OP_VM implements a gas metering system similar to Ethereum, where each operation has an associated cost. This prevents infinite loops and ensures users pay proportionally for the computational resources consumed. Unlike Ethereum, gas fees are paid in Bitcoin (satoshis) rather than a separate token.
Layer 3: The State Management Layer
Smart contracts require persistent storage for data such as token balances, smart contract variables, and other state information. OP_NET manages this through a sophisticated state system. Each smart contract can allocate storage slots consisting of 32 bytes keys mapped to 32 bytes values, with all state changes tracked through Merkle trees.
OP_NET nodes maintain state locally in databases, but the data remains provable and verifiable. The state root, a cryptographic summary of all smart contract states, is included in epoch solutions, creating an immutable record of the system's state at regular intervals.
Layer 4: The Epoch System
OP_NET uses an epoch-based checkpointing system rather than traditional blockchain consensus. Every 5 Bitcoin blocks form an epoch (approximately 50 minutes).
At each epoch boundary, miners compete to find the best SHA-1 near-collision with the epoch's checksum root. The winner must include an attestation about state from 4 epochs ago (~21 blocks deep in Bitcoin). This time-delay ensures the attested state is so deep it's effectively immutable.
Critical point: SHA-1 miners cannot decide consensus. They only compete for rewards. Given the same Bitcoin blocks, every OP_NET node derives the exact same state regardless of who mines the epoch. If nobody mines a valid solution, the genesis block hash becomes the epoch miner, ensuring consensus finality never stops.
Fork-Impossible Consensus
OP_NET forks are mathematically impossible, not just unlikely, but actually impossible. To understand why, consider how traditional blockchains handle competing histories.
Traditional Blockchains
In Bitcoin, two miners might find valid blocks simultaneously, creating temporary forks until one chain grows longer. This happens because these systems make decisions about events happening right now, where network latency and timing differences create ambiguity.
OP_NET Consenus
OP_NET completely sidesteps this problem by never making decisions about the present. When epoch 184446 ends at block 922234, miners compete to create the winning solution. But every valid solution must include an attestation about the state at the end of epoch 184442 (block 922214). By the time anyone can submit a solution, block 922214 is already 21 blocks deep in Bitcoin's history.
For two nodes to disagree about epoch 184446's winner, they would need to disagree about what happened at block 922234. But at 21 blocks deep, every honest node has exactly the same view of block 922214's contents and state. The historical reference point is so deeply buried that all nodes must agree on it. There's no ambiguity, no possibility of different views. OP_NET uses Bitcoin blocks and does not create blocks on its own. If Bitcoin reorgs, OP_NET reorgs, simple as that.
Even when two miners submit equally good solutions with the same number of SHA-1 matching bits, OP_NET's deterministic tie-breaking mechanism prevents any ambiguity. The system doesn't care who submitted first or which node saw what solution when. Instead, it uses a mathematical comparison of the miners' public keys, the transaction hash that submitted this solution, and the one numerically closer to zero wins. This means that given the same set of submissions, every node will independently arrive at exactly the same winner, regardless of the order they received the submissions or their network position.
This dual mechanism, time delayed attestations to immutable history plus deterministic tie breaking, transforms the consensus problem from "which chain should we follow?" to "what does Bitcoin say happened 20 blocks ago, and who had the best solution?" Both questions have exactly one answer across all honest nodes, making forks logically impossible rather than just unlikely.
Transaction Flow
The following sequence illustrates how these layers work together during a smart contract interaction:
- A user creates a transaction calling a smart contract function (such as swapping tokens on a DEX).
- The transaction is broadcast to the Bitcoin network and included in a block.
- OP_NET nodes detect the transaction.
- The transaction enters a deterministic ordering queue based on gas fees.
- Nodes execute the transaction and update states accordingly.
- State changes are recorded and included in the next epoch checkpoint.
Handling Bitcoin Reorganizations
While OP_NET's design makes internal forks impossible, it must still handle the reality that Bitcoin itself can experience reorganizations. When Bitcoin reorganizes, OP_NET needs to gracefully handle the state changes this implies.
Understanding how Bitcoin reorganizations affect OP_NET is essential. When Bitcoin reorganizes, blocks that OP_NET has already processed are replaced with different blocks. These new blocks may contain different transactions, in a different order, or may exclude some transactions entirely. OP_NET must therefore rewind its state to the point before the reorganization and replay the new chain of blocks.
Since reorganizations only affect recent epochs that have not been finalized, no epoch winners need to be changed.
The reorganization process works as follows:
- Detection: The OP_NET node detects a reorganization in Bitcoin by noticing that a previously confirmed block has been replaced.
- Identification: The node identifies the common ancestor, which is the last block that remains valid in both the old and new chains.
- Reversion: The node reverts its state to the common ancestor block.
- Reprocessing: The node processes the new blocks in sequential order.
Smart Contract Bytecode Storage
When deploying a smart contract, the bytecode is stored in Bitcoin transactions using Tapscript and OP_PUSHDATA opcodes. The P2OP format serves as the address type for OP_NET contracts but does not store the bytecode itself.
Smart contracts bytecode is compressed using ZLIB to minimize storage requirements, with a maximum bytecode size of 400KB. The compressed bytecode is embedded directly in the witness data of the Bitcoin transaction.