60Blockchain & DLT (testing-oriented)
What you will master here
- Blockchain vs DLT — the distinction
- Bitcoin essentials: transactions, wallets, keys, mining
- Ethereum: EVM, smart contracts, gas
- Hyperledger Fabric — permissioned blockchain
- Token standards: ERC-20, ERC-721 (NFTs), ERC-1155
- How SDETs test blockchain applications
60.1 Blockchain vs DLT
Distributed Ledger Technology (DLT) = umbrella term: any database replicated across multiple nodes with no central authority. Blockchain = one specific type of DLT where transactions are grouped into blocks, each cryptographically linked to the previous via a hash — forming an immutable chain.
All blockchains are DLTs. Not all DLTs are blockchains (e.g. Hashgraph uses a "DAG" structure instead of blocks).
| Property | Blockchain | Other DLT |
|---|---|---|
| Data structure | Linked blocks | DAG, Merkle tree, etc. |
| Consensus | PoW, PoS, PBFT | Varies |
| Examples | Bitcoin, Ethereum, Hyperledger Fabric | IOTA (Tangle), Hedera (Hashgraph) |
60.2 Core security mechanisms
- Cryptographic hashing (SHA-256 in Bitcoin) — each block contains a hash of the previous. Change one transaction → its block hash changes → all subsequent hashes invalid.
- Public-key cryptography — private key signs transactions; public key (derived) verifies. Possession of private key = ownership of funds.
- Consensus — how nodes agree on the next block. PoW (Bitcoin — compute hashes), PoS (Ethereum 2 — stake tokens), PBFT (Hyperledger — Byzantine fault tolerance), etc.
- Decentralisation — no single point of failure / trust.
60.3 Bitcoin — the basics
- Created by pseudonymous Satoshi Nakamoto, 2009.
- Wallets hold private keys (HD wallets derive many keys from one seed).
- Addresses are derived from public keys (hashed for compactness).
- Transactions are UTXO-based — each spend consumes prior outputs and creates new ones.
- Mining — nodes compete to solve a PoW puzzle; winner adds the next block and earns the block reward + fees.
- Block time ~10 minutes.
- Forensics — addresses are pseudonymous, not anonymous. Companies (Chainalysis) trace flows.
60.4 Ethereum & the EVM
- Smart contract platform — programs run on every node.
- EVM (Ethereum Virtual Machine) — sandboxed execution environment.
- Gas — each operation costs gas; user pays gas fee × gas price. Prevents infinite loops.
- Account model (not UTXO) — externally owned (EOA) vs contract accounts.
- Solidity — most common contract language.
- Moved to Proof-of-Stake (PoS) in 2022 (The Merge).
A tiny smart contract
// Solidity (Java-ish syntax — using java highlighter for clarity)
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 private value;
event ValueChanged(uint256 newValue);
function set(uint256 newValue) public {
value = newValue;
emit ValueChanged(newValue);
}
function get() public view returns (uint256) {
return value;
}
}
60.5 Hyperledger Fabric — permissioned blockchain
- Open-source, Linux Foundation project
- Permissioned — known participants, not anonymous
- No mining — uses PBFT-style consensus
- Channels — private sub-ledgers between subsets of participants
- Chaincode — Hyperledger's term for smart contracts (Go, Java, JS)
- Use cases: supply chain, trade finance, healthcare records
60.6 Token standards
| Standard | What | Examples |
|---|---|---|
| ERC-20 | Fungible tokens — interchangeable units | USDT, DAI, most stablecoins, governance tokens |
| ERC-721 | Non-fungible tokens (NFTs) — unique items | Art, collectibles (CryptoPunks, BAYC) |
| ERC-1155 | Multi-token (mix of fungible + non-fungible in one contract) | Game items, semi-fungible |
| ERC-4626 | Tokenised vaults | DeFi yield vaults |
| ERC-4337 | Account abstraction (smart wallets) | Modern wallet UX |
60.7 Cryptocurrencies vs tokens
- Cryptocurrency = native asset of a blockchain (BTC on Bitcoin, ETH on Ethereum). Used to pay gas / transaction fees.
- Token = asset issued by a smart contract on an existing blockchain (USDT on Ethereum). Doesn't have its own chain.
60.8 What SDETs actually test in blockchain apps
Layer 1: dApp UI (Playwright)
test('connect wallet flow', async ({ page, context }) => {
/* Inject mock wallet — most teams use a mocked window.ethereum */
await context.addInitScript(() => {
(window as any).ethereum = {
isMetaMask: true,
request: async ({ method }: any) => {
if (method === 'eth_requestAccounts') return ['0xAbc...123'];
if (method === 'eth_chainId') return '0x1';
},
on: () => {},
};
});
await page.goto('/');
await page.getByRole('button', { name: 'Connect Wallet' }).click();
await expect(page.getByText(/0xAbc...123/)).toBeVisible();
});
Layer 2: Smart contract (Hardhat / Foundry / Truffle)
// Hardhat test (TypeScript)
import { ethers } from 'hardhat';
import { expect } from 'chai';
describe('SimpleStorage', () => {
it('stores and reads a value', async () => {
const Factory = await ethers.getContractFactory('SimpleStorage');
const contract = await Factory.deploy();
await contract.set(42);
expect(await contract.get()).to.equal(42);
});
it('emits ValueChanged event', async () => {
const c = await (await ethers.getContractFactory('SimpleStorage')).deploy();
await expect(c.set(7)).to.emit(c, 'ValueChanged').withArgs(7);
});
});
Layer 3: Indexer / API
Most dApps run a backend indexer (The Graph, custom) that reads blockchain events and serves a normal REST/GraphQL API. Test that like any API — schema, status codes, latency. Just need a known on-chain state to query against.
60.9 Special challenges
- Non-deterministic timing — block confirmation takes seconds to minutes. Test on a local devnet (Hardhat / Anvil) where blocks are mined instantly.
- Gas costs — assert gas usage per transaction stays within budget (regression detector).
- Forks — fork mainnet at a specific block for realistic state.
- Reorgs — transient state can change. Wait for N confirmations before asserting.
- Security — smart contracts are immutable post-deploy. Bugs = lost funds. Pre-deploy audits (Trail of Bits, OpenZeppelin), invariant tests (Foundry / Echidna), formal verification.
Module 36 — Blockchain Q&A
Blockchain vs DLT?
DLT is the umbrella — any distributed ledger. Blockchain is a specific DLT structure where transactions are grouped into blocks chained by cryptographic hash. Other DLTs use DAGs or other structures.
What's the EVM?
Ethereum Virtual Machine — sandboxed bytecode execution environment that runs smart contracts identically on every Ethereum node. Code compiled (e.g. Solidity → EVM bytecode) and run; deterministic.
ERC-20 vs ERC-721?
ERC-20: fungible tokens (1 USDT = 1 USDT, interchangeable). ERC-721: non-fungible tokens (NFTs) — each token id is unique, not interchangeable.
How would you test a smart contract?
Local devnet (Hardhat / Anvil) for instant block mining. Unit tests in TS via ethers.js + Mocha/Chai or Foundry's Rust-like tests. Cover: state changes, events emitted, gas usage, revert conditions, edge cases. Add invariant tests (Foundry / Echidna) to fuzz inputs.
How would you test a dApp UI?
Playwright with a mocked
window.ethereum — inject it via addInitScript. Stub responses to eth_requestAccounts, eth_sendTransaction, etc. Test connect, sign, switch network flows without a real wallet.What's gas?
A measure of computational effort. Each EVM operation consumes gas. Users pay gas × gas-price (in ETH) to execute. Prevents infinite loops; rewards miners/validators.
Hyperledger Fabric vs Ethereum?
Fabric is permissioned (known participants), uses PBFT consensus, no native token, business-focused. Ethereum is permissionless (anyone can join), uses PoS, has ETH as native currency, focuses on open smart-contract platforms.
What's the difference between a coin and a token?
Coin = native asset of its own blockchain (BTC, ETH). Token = asset issued by a contract on someone else's blockchain (USDT on Ethereum). Tokens depend on the host chain's security.
Why are smart contracts hard to fix?
Once deployed they're immutable. To "fix" you deploy a new version and migrate state. Upgradeable patterns (proxy contracts) exist but add complexity. Hence the emphasis on pre-deploy audits and formal verification.
How does mining differ from staking?
Mining (PoW): nodes solve a hash puzzle; first to solve wins the right to add a block. Energy-intensive. Staking (PoS): validators put tokens as collateral; protocol picks a validator pseudo-randomly weighted by stake. Slashing for misbehaviour. Vastly less energy.
What is forensics on a blockchain?
Tracing money flows across addresses to identify the parties involved. Bitcoin addresses are pseudonymous, not anonymous — every transaction is public. Firms like Chainalysis link addresses to exchanges and (often) to real-world identities.