A Blockchain Primer › Module 4 › Lesson 4.3

Lesson 4.3: Running Programs on a Blockchain — Smart Contracts, DApps, and Oracles

  • #1. The Core Question
  • #2. An Everyday Analogy
  • #3. Core Concepts Explained
  • #4. Diagrams
  • #5. Key Takeaways
  • #6. Quiz

The blockchains we've covered so far mostly manage transfers and ledgers. But if all you can do is jot a few lines in a transaction's memo field, how could a blockchain support thousands of different applications? This lesson looks at how a blockchain becomes a "world computer" that can run programs, and how on-chain programs connect to the real world through oracles.

1. The Core Question

Blockchains started out just recording transactions. What does it take to run more complex logic on-chain? Where do programs live, who runs them, and how do you keep one program from freezing the whole system? And how do programs read the real world off the chain?

This lesson answers four questions: What is a smart contract? What are DApps and the world computer? Why do we need "gas"? And how do oracles bring off-chain data onto the chain?

The short version: A blockchain stretches transaction data into programs that can run, with every node running the same code. DApps are built from smart contracts, and oracles hook real-world data up to the chain.

2. An Everyday Analogy

Sending Instructions in a Transfer Memo

Picture yourself somewhere with no way to communicate, and the only way to reach home is by international bank transfer. In the "purpose of payment" field on the transfer form, you write:

  • "Please pick me up at the airport."
  • "Please wheel out the mower and cut the grass."
  • "Please clean the house until it's clean, or until the $100 runs out."

The first two are simple messages. The third has a condition, a budget, and a rule for when to stop. A friend who follows these rules is really carrying out a "self-executing contract."

That's exactly what a smart contract is: a program written into transaction data. It doesn't just say "transfer this much money." It spells out "under what conditions, by what rules, do what."

Temperature Insurance

Now take an on-chain insurance policy: if the average temperature in a certain place on a certain day goes above 20°C, it pays out automatically. The smart contract doesn't know the temperature. It needs an oracle to check the weather data and write "today's temperature" onto the chain. Then the contract pays out based on that number.

If the oracle reports bad data, the contract runs on bad data. The problem isn't the contract's logic. It's the point where the data comes in.

3. Core Concepts Explained

3.1 From Transaction Data to Executable Programs

Transactions already have a data field that can hold memos or extra information. A programmable blockchain builds on that: a transaction can carry not just data but program code, or it can call a program that's already deployed on-chain.

To run programs, a blockchain needs at least:

  • A programming language to write programs in.
  • A way to put programs onto the blockchain.
  • Interfaces for calling programs, passing inputs, getting results, and reading and writing data.
  • An environment to run programs in (usually a virtual machine).
  • Space to store program code and program state.

All of this has to happen in a purely distributed system, without leaning on a central server.

3.2 What a Smart Contract Is

A smart contract is a computer program stored and run on a blockchain.

It's called a "contract," but it isn't necessarily a legal contract. It's more like a self-contained piece of code that can describe things like:

  • Paying rent on a schedule.
  • Borrowing and paying back.
  • Settling complicated bets.
  • Paying out insurance when damage or a specific event happens.

A smart contract's rules are written in code, and nodes carry them out. Its "smartness" comes from program logic, not human judgment.

3.3 Turing Complete: Powerful but Dangerous

A Turing complete programming language gives programmers control over everything the processor can do, with almost no limits on what they can express. For smart contracts, that means you can write very complex logic.

But Turing completeness is also dangerous, because a program can contain an infinite loop. Since every node has to run the same smart contract, an infinite loop would run forever on every node and drag the whole system down.

The loop might be written on purpose, by mistake, or through a mix of errors nobody saw coming. The blockchain doesn't care why. It only cares about the result.

3.4 Gas: The Emergency Brake for Turing Completeness

The fix is to give smart contracts "gas."

Every instruction a program runs burns a little gas. When the gas runs out, the program has to stop, and it can't keep tying up the nodes' computing power. To run again, someone outside has to refuel it.

That keeps the expressive power of Turing completeness while giving the system an emergency brake. No matter how complex a program is, it can only run on a limited amount of gas.

3.5 DApps: Applications Built from Smart Contracts

A DApp is a distributed application that runs on a programmable blockchain. A DApp usually has several smart contracts, each handling one subtask.

A simple application might have just one contract, but complex applications often take many contracts working together. The user sees one application. Underneath, a set of programs is running on-chain.

3.6 The World Computer and Web3

To the user, a DApp looks like a program running on their own computer, or like an ordinary website, but with a few differences:

  • It's available around the clock.
  • You can reach it from anywhere in the world.
  • Its results are consistent.
  • It doesn't depend on a single server, so it's hard to shut down or censor.

That's why people often call a programmable blockchain a "world computer." The on-chain programs are the applications, the network of nodes is the hardware, and the blockchain technology suite is the operating system.

DApps run on a distributed blockchain with no single host, which is why they're often called Web3, or "the next-generation internet." But Web3 isn't completely free of centers either. Users still depend on wallets, node services, network infrastructure, and on-chain governance. What changes is how applications are hosted and run. Not every center disappears.

3.7 Wallets and Tokens

A common misconception is that "coins are stored in the wallet." In fact, a wallet has no coins in it. Ownership of coins is recorded on the blockchain. The wallet holds your private keys and helps you connect to nodes, check balances, and create and sign transactions. A wallet is like a browser: a browser doesn't contain the whole internet, and a wallet doesn't contain the whole blockchain.

A blockchain's native cryptocurrency takes part in consensus and paying fees. It's part of the system. Smart contracts can also create their own tokens, but those tokens are usually just applications on the chain and don't take part in the underlying consensus.

3.8 What Smart Contracts Still Lack: Off-Chain Data

Smart contracts can read on-chain data, but by default they can't directly read the internet, sensors, or real-world events. They need a "bridge" to bring outside data in.

That bridge is the oracle.

3.9 What an Oracle Is

A blockchain oracle is a third-party service that feeds off-chain information to smart contracts, acting as a bridge between the blockchain and the outside world.

It doesn't change on-chain consensus, and it doesn't make decisions for smart contracts. It just brings outside data in, or sends on-chain results out.

3.10 Oracle Types and Directions

By data source:

  • Software oracles: pull data from internet sources like online databases, servers, websites, and APIs.
  • Hardware oracles: collect data from sensors, IoT devices, barcode/QR scanners, RFID tags, and so on.
  • Human oracles: people or organizations with specialized expertise research and verify information, then write it in.
  • Computation oracles: run computations off-chain and hand the result to the smart contract.

By direction:

  • Inbound oracles: bring outside information into smart contracts.
  • Outbound oracles: send information from smart contracts to the outside world, for example to trigger a physical device.

3.11 Centralized and Decentralized Oracles

A centralized oracle is controlled by a single party, which is the smart contract's only source of information. The problem is a single point of failure: if that party makes a mistake, gets attacked, or gets bribed, the contract gets bad data. That goes against the blockchain principle of "no central single point of failure."

A decentralized oracle (also called a consensus oracle) uses a distributed architecture and its own consensus algorithm. It doesn't rely on any single source of information, which makes the data more reliable.

3.12 The Trust Problems of Oracles

Blockchain consensus assumes that lots of digital witnesses can see the same on-chain history. But real-world events happen at a specific time and place, and few people can see them firsthand. Even decentralized oracles have a hard time producing consistent, unambiguous observations and descriptions of real events. Errors, biases, and different interpretations in real-world data flow onto the chain through the oracle.

Oracles can also lack transparency, be expensive to run, produce results that are hard to verify, depend on a few data providers, and be easy to manipulate or attack. When you design an oracle, think about whether the data sources are reliable, whether you need several independent sources to check each other, how errors get caught and fixed, and whether the oracle itself could turn into a new centralized bottleneck.

3.13 Oracles and Cross-Chain Bridges Are Not the Same Thing

An oracle connects a blockchain to the real world. A cross-chain bridge connects two separate blockchains so that assets or information can move between them. Both deal with "outside information," but what's on the other end is different.

4. Diagrams

Figure 4-3-1: Programmable Blockchains, DApps, and the World Computer

Multiple smart contracts form a DApp; the network of nodes forms the world computerMultiple smart contracts form a DApp; the network of nodes forms the world computer
Figure 4-3-1 Multiple smart contracts form a DApp; the network of nodes forms the world computer
Click to enlarge

Figure 4-3-2: The Oracle as a Bridge Between On-Chain and Off-Chain

Inbound brings external data into contracts; outbound sends contract results outsideInbound brings external data into contracts; outbound sends contract results outside
Figure 4-3-2 Inbound brings external data into contracts; outbound sends contract results outside
Click to enlarge

5. Key Takeaways

  • A programmable blockchain lets transactions carry or call programs, not just transfers.
  • A smart contract is a program stored on-chain and run by nodes. It isn't a legal contract.
  • Turing completeness gives you a lot of expressive power but brings the risk of infinite loops. Gas puts a cost on running code, and the program stops when the gas runs out.
  • A DApp is a distributed application on a programmable blockchain, usually built from several smart contracts.
  • World computer = on-chain programs + the network of nodes + the blockchain technology suite. Web3 lowers the risk of relying on one host but still depends on infrastructure.
  • Wallets don't store coins. They hold private keys and connect you to the blockchain.
  • An oracle is a third-party service that feeds off-chain information to smart contracts.
  • Oracles by source: software, hardware, human, and computation. By direction: inbound and outbound.
  • A centralized oracle has a single point of failure. A decentralized oracle uses multiple sources and consensus to be more reliable.
  • Oracles bring the real world's errors, biases, and trust problems onto the chain. A cross-chain bridge connects two blockchains.

6. Quiz

Part 1: Single Choice (1–4)

Question 1

What's the most accurate definition of a smart contract?

A. A paper contract that a lawyer has to sign
B. A computer program stored and run on a blockchain
C. A kind of central server
D. A kind of encrypted image

Show answer

Answer: B

Explanation: A smart contract is an on-chain program with state, run by nodes. It isn't a legal contract.

Question 2

What does "gas" do in a programmable blockchain?

A. It puts a cost on running a program and stops the program when the gas runs out
B. It makes private keys longer
C. It makes blocks smaller
D. It makes every transaction free

Show answer

Answer: A

Explanation: Gas puts a cost on every instruction. When it runs out, the program stops, so it can't run forever.

Question 3

Reading a temperature from a sensor and passing it to a smart contract is what kind of oracle?

A. A hardware oracle, inbound direction
B. A software oracle, outbound direction
C. A human oracle, outbound direction
D. A computation oracle, inbound direction

Show answer

Answer: A

Explanation: A sensor is a hardware source, and the data flows from outside onto the chain, which is the inbound direction.

Question 4

What's the biggest problem with a centralized oracle?

A. It's a single point of failure that may clash with the blockchain's distributed principle
B. It can't provide any data
C. It's always slower than a decentralized oracle
D. It can't read software data

Show answer

Answer: A

Explanation: A centralized oracle is controlled by a single party, has a single point of failure, and clashes with the distributed principle.

Part 2: Multiple Choice (5–7)

Question 5

At a minimum, which components does a programmable blockchain need?

A. A programming language
B. A way to deploy and call programs
C. An execution environment
D. Storage for programs and state

Show answer

Answer: A, B, C, D

Explanation: The language, deploying and calling programs, the execution environment, and storage are all basic parts of a programmable blockchain.

Question 6

By data source, how can oracles be classified?

A. Software oracles
B. Hardware oracles
C. Human oracles
D. Computation oracles

Show answer

Answer: A, B, C, D

Explanation: All four are oracle data source types.

Question 7

Which statements about the trust problems of oracles are correct?

A. Real-world data can carry errors, biases, and different interpretations
B. Centralized oracles have a single point of failure
C. Oracles may depend on a few data providers
D. Oracles make on-chain systems completely immune to the real world

Show answer

Answer: A, B, C

Explanation: Real-world data is uncertain, centralized designs have a single point of failure, and relying on a few providers can happen. D is wrong because oracles bring real-world problems onto the chain.

Part 3: True or False (8–9)

Question 8

True or false: by default, smart contracts can directly read the internet and real-world sensor data.

Show answer

Answer: False

Explanation: By default, smart contracts can't read off-chain data. They need mechanisms like oracles.

Question 9

True or false: an outbound oracle can send on-chain results to the outside world, for example to trigger a physical device.

Show answer

Answer: True

Explanation: An outbound oracle sends on-chain information to the outside world and can trigger physical devices or actions.

Part 4: Scenario Analysis (10)

Question 10

A team wants to build an app on-chain that splits revenue automatically: when a payment comes in, it gets divided among several accounts by fixed percentages. They plan to write a smart contract but are worried about bugs or infinite loops.

Answer the following:

  1. How would a smart contract handle the automatic split?
  2. How does the gas mechanism keep the program from dragging the system down?
  3. What off-chain capabilities or safety measures does this app still need?
Show answer

Answer:

  1. When a payment comes in, the contract works out each account's share using the preset percentages and makes the transfers or updates the books.
  2. Every instruction burns gas. When the gas runs out, the program stops, so an infinite loop can't tie up every node.
  3. It needs off-chain payment confirmation, account address checks, access control, error handling, and auditing. If it depends on real-world data, it also needs an oracle.