Overview
This session used a small Python proof-of-work blockchain (pow_blockchain.py) as a workload for observing how the operating system manages CPU execution, processes, memory and system calls — the blockchain itself was not the subject. The class covered blockchain definitions (blocks, hashes, immutability, consensus, advantages/disadvantages), walked through the Python code (argparse, hashlib/SHA-256, multiprocessing), and then ran the program live under top at several difficulty/worker/block settings. The observations (CPU ~99%, load average, process IDs changing per round, memory untouched) were used to motivate identifying CPU-, memory- and storage-intensive code, performance testing as a quality gate, and the developer's options (optimize, add cores, limit usage, vertical scaling, auto-scaling). It sits in the course where the ISA/machine-code contract meets the OS's management of processes and resources: the program is the workload, the OS is what we are watching.
Learning outcomes
- Explain what a blockchain is (immutable append-only ledger, blocks, hash linking, consensus) and why it was chosen as an OS workload.
- Read the proof-of-work Python program and identify which parts are CPU-intensive, which create OS processes, and where system calls occur.
- Run the program with
python3 -uand interprettopoutput: CPU utilization, load average, memory/free/used/buffer cache/swap, process IDs, users. - Compute blocks in chain from blocks mined, and relate difficulty to the number of leading zeros in the target.
- Distinguish the identity hash used for linking from the public/private key hashes used for authentication, and explain that authentication is done by nodes, not blocks.
- Explain why a CPU-bound workload that runs sequentially causes latency, and name the developer's options and the testing levels (unit → functional/level 0 → performance/level 1 → security/level 2).
Core concepts
Blockchain as an immutable append-only ledger
Definition: A peer-to-peer network holding a shared immutable ledger; immutable = cannot modify, permanently recorded = can only append (cannot modify, cannot remove). All data is grouped into blocks with capacity limits; a filled block is closed and a new block is attached to the previous one, forming a chain and a historical record of all transactions. Why it matters: The append-only property is what the class later contrasts with normal mutable storage, and it is the source of the "immutability as a con" discussion. Connection: The chain structure (each block carrying the previous block's hash) is exactly what the code implements and what the live runs exercise.
Blocks, hashes and linking
Definition: Each block contains a unique code called a hash; each block is identified by its hash and by the hash of the previous block. The previous-block hash is what links blocks. Why it matters: The hash is the identity/reference of a block, not the shared content; the only way to retrieve data from a block is to identify it by its hash. Connection: In the code, the hash function takes index, timestamp, data, previous hash and nonce — the hash field itself is not included because it is the output of the calculation.
Immutability and permanence
Definition: Once a block is attached it is permanent — cannot be moved out of the chain or reordered. Analogy: stacking concrete blocks in a building — once placed/set, it is done forever; remove a block from the middle and the entire chain falls/breaks. Why it matters: A new block can only be appended after the current last block (block N becomes block O); it cannot be inserted between existing blocks (e.g., between H and J, or C and D). By default no removal, no deletion, no modification. Connection: The previous block provides its identity (hash) to the new block; no authentication is required from the previous block — it just says "I am connected to block N."
Consensus and validation
Definition: On a decentralized network, most computers on the network (nodes) must reach a consensus that the transaction/trade is real. Validation criteria, validation rules, acceptance criteria and acceptance rules are all established before the blockchain is created. Why it matters: Consensus can be random from the nodes of the cluster — it need not be every node. Example: if the mechanism says any three nodes consenting means the entire network consents, that is built in; with 250 nodes in the cluster, the mechanism may require any three nodes to agree. There is no single way of consensus; each type has a different methodology, technology and way of work. Connection: Named mechanisms: proof of work and proof of stake (details explicitly deferred — not covered in this session). Authentication is not done by blocks, it is done by nodes.
Two kinds of hashes: identity vs. authentication
Definition: (1) The identity hash used for linking is distinct from (2) the public/private key hashes used for authentication/validation. The second hash can be generated using different technologies, even third-party tools. Why it matters: Analogy: knowing the flat number (357) and phone number of a resident is not enough — you must provide identity at the security gate, and the person you are visiting must approve entry. The public key is input into the authentication system and mapped with the private key; the person who allows you is not the security but the person you came to meet. Authentication failing is not necessarily a bad thing (e.g., the person is not at home). Connection: SHA-256's way of working is not changed; technologies are used as-is to arrive at the outcome, not modified.
Advantages and disadvantages of blockchain
Advantages: (1) Immutability — data cannot be erased or replaced; prevents data tampering and manipulation. (2) Transparency — the network is decentralized, allowing any member to scrutinize the recorded data (example: 2 million nodes all have access to all the blocks in the chain — transcript says "2,000 blocks", see Flagged). (3) Security — transactions require validation from network members; combined with immutability and transparency, a block added with data is protected from third-party attacks, and there is no room for rogue administration activities (if that happens the chain breaks and a new chain must be created with all the blocks).
Disadvantages: (1) High cost of implementation — reduces cost for users but requires high cost for companies; proper planning and implementation are a must. (2) Performance — carries out more operations and is slower than traditional databases; inefficiency arises from several network users validating a single transaction. Analogy: a single if condition checking whether a variable is more than 10 is fast, but one million such if conditions will definitely slow down. (3) Immutability as a con — example: a transaction for $10, later the parties compromise at $9 — you cannot change $10 to $9; rewriting is incredibly time-consuming and very expensive. Hence in industry it is implemented where not much modification of data is required.
Connection: These pros/cons motivate why the class treats blockchain as a workload and why the lecturer expects it to remain niche.
Types, applications and history (as stated)
Types: Four, based on structure, characteristic and usage: public, private, hybrid, consortium (named only, not elaborated). Applications: financial markets, healthcare, decentralized finance, real estate, smart contracts. Financial services: Bitcoin and Ethereum are popular cryptocurrencies built on blockchain. Healthcare: Medicalchain — described as one of the first medical companies to implement blockchain to help facilitate storage and utilization of electronic medical records; used for a safer and faster way of managing data and tracking medical supplies. Supply chain: IBM Food Trust — a project to help improve the food supply chain. Real estate: Propy — a platform using blockchain to transform how people market, search for, buy, sell and invest in real estate. History: Concept first described by scientists Haber and Sternetta in 1991. A white paper published under the name Satoshi Nakamoto focusing on Bitcoin, a peer-to-peer electronic cash system, in 2018 (as spoken — see Flagged). The Bitcoin blockchain was made open source and released to the public in January 2009, making it possible to examine the code and reuse it.
The Python proof-of-work program
Definition: A small blockchain implementation with these parts:
- Imports: argparse (argument parsing), hashlib (cryptographic hashing, includes SHA-256; the proof-of-work algorithm depends on it), json (converts structured block data into a consistent serialized representation), multiprocessing as mp (creates and manages independent worker processes), plus os, time, and date/time.
- Which module creates OS processes: multiprocessing is what is responsible for creating the operating system processes when the Python program is executed.
- Target: production blockchain protocols can use more elaborate numeric targets, block headers or consensus, but for this lab the code uses a hex prefix.
- Genesis: the point where the blockchain operation starts; in the code, if Genesis is the previous hash, it returns false.
- Hash function: accepts the fields that contribute to the block's hash — index, timestamp (current timestamp), data (the actual data within the block), previous hash (hash of the previous block), and nonce. The dictionary represents the block fields to be hashed; the hash field itself is not included because it is the output of the calculation.
- mine_worker: each worker receives the block information, target difficulty, its own ID, the total number of workers, and a shared result queue Q. target = "0" * difficulty. The nonce is initialized to the worker ID, so the initial nonce differs between workers. An attempt counter records how many hashes this worker has calculated; a timer measures its elapsed mining time. The worker runs a while loop that calculates the hash for the current nonce and increments the counter.
- CPU-intensive portion: this loop is the main CPU-intensive portion; it repeatedly calculates without deliberation and performs a system call for each attempt — that is where it is CPU intensive.
- if condition in the worker: tests whether the hash satisfies the proof-of-work requirement.
- mine_block: coordinates the mining workers and assembles the resulting block; generates the timestamp, creates a queue for worker results, initializes the list to track the processes, and contains a for loop (also somewhat CPU intensive, but unlike a while loop).
- validate_chain: validates which chain and at what difficulty level. If target is 0 that is the initialization target, but it gets modified depending on the difficulty level supplied as input at execution. Validation has multiple stages: validate the genesis block first, then validate the subsequent blocks; it goes through the entire chain ensuring all blocks attached to the chain have valid data, and protects it.
- Main program: defines the main program and creates a command-line argument parser, so arguments can be given on the command line; the program is then run from its entry point.
- Code check: the code has an if condition checking difficulty must be between 1 and 6, and workers must be at least 1 (cannot be 0).
Why it matters: The lab parameters are (1) difficulty level, (2) block parameters, i.e. the hash, (3) the arguments. Framing: if we can identify which part of the program consumes more CPU and which requires more memory, it is easy for us to manage.
Connection: This is the workload whose behavior under top is the OS observation of the session.
Live Linux observation: pow_blockchain.py under top
Definition: Running the Python program and watching it with top in a second terminal. python3 -u — the -u option enables unbuffered standard output and standard error, helping progress messages appear promptly.
Why it matters: The runs show CPU-bound behavior (CPU up to 99%, memory untouched), sequential execution on a two-core machine, and process IDs changing between rounds (different processes/system calls created).
Connection: This is the concrete evidence behind the performance discussion and the testing-level discussion.
Performance implications and developer's options
Definition: If the system sends 300 records per second (or 3,000 records), you are expected to create 300 blocks (or 3,000 blocks) per second. In the deliberately constructed case, one block took 66 seconds; another completed in 1.9 seconds; delays of 3 seconds, 10 seconds, and up to 30 seconds and 50 seconds were built in on purpose (higher difficulty and higher number of blocks cause some blocks to slow down). Why it matters: An analyst watching the trend sees data delayed by seconds and misses the trend, making a wrong buy/sell decision — the decision-making system the design was meant for does not work. Developer's options (as stated): optimize the code; increase the number of cores in the CPU; or limit the program's usage. If blocks could be processed in parallel, a speedier response to the customer would be achieved; currently it runs sequentially because resources are not available. This behavior should have been caught in unit testing / performance testing. Scale concern: one block addition takes milliseconds; what if 3,000 blocks must be added? Bombay Stock Exchange can send 3,000 transactions in a second — "pretty much possible." Vertical scaling: in the cloud, when the program is known to be highly CPU sensitive, scale up CPU only for that moment so the customer is not impacted; a report later shows e.g. "we were using 12 cores of CPU, now we have increased it to 24", and you then address which part of the program consumes more cores. Auto-scaling: the "second option" — blindly increase infrastructure if auto-scaling is available (cloud only). It indirectly increases cost of operations: the more resources used, the more the cloud provider bills. Connection: This is the "why" behind identifying CPU/memory/storage-intensive code: if code accesses more data directly from storage it is storage-intensive and needs storage supporting that rate of access; if memory-intensive and not given appropriate memory it will hit the threshold and give alerts/issues. CPU and memory are the two critical ones; if these two can be managed, other things can be managed. This should be identified while writing code, or at least during performance testing.
Testing levels as quality gates
Definition: Functional testing = level 0; performance testing = level 1; security testing = level 2. All three must be qualified for the code to go into production. Unit testing is done before the code reaches the quality team ("pre-unit 0, pre-testing level 0"); what the lecturer performed in this session is described as a unit test.
Why it matters: In performance testing the quality team asks whether utilization is appropriate, whether resources are planned appropriately, and whether the code must be optimized or resources added. These calls are taken from this type of testing.
Connection: The live top runs are exactly this kind of evidence gathering.
Decentralization degree
Definition: There is no fixed minimum number of nodes required; response goes down depending on the degree of decentralization you want for security — the more nodes, the more secure. With three or four nodes it is centralized, so it is not really blockchain at that level. Why it matters: As of now the code is a single server — only one node. Connection: Distribution has two senses: (1) servers in different regions (e.g., AWS US and Australia) are part of the cluster maintaining the blockchain; (2) a block stored on one server may be followed by a block on another server in the same or a different data center — data is not stored sequentially on one server. The hash (created from the nonce) links blocks; each block has the previous hash so the system/network knows where the previous block is. Data is distributed — not like a single MySQL server holding all data. Blocks can be split between servers, and whichever server stores a particular block of data is the owner of that data; it still must go ahead with the consensus before the data gets added.
Formulas and maths
target = "0" * difficulty—targetis the hex prefix the hash must match;difficultyis the number of leading zeros required. Used to construct the proof-of-work target inmine_worker.- Blocks in chain = 1 (genesis block) + number of blocks mined —
1is the pre-existing genesis block; the second term is the number of blocks mined in the run. Used to predict the chain length after a run (mining 3 blocks gives 4 blocks in chain; mining 6 gives 7). - Difficulty = number of leading zeros prefixed to the hash —
difficultyis the count of leading zeros (difficulty 4 → 4 zeros; difficulty 6 → 6 zeros). Used to interpret the difficulty argument. - Load average > 1 is described as "100" (as stated in class) —
1is the threshold on the load-average scale as the lecturer uses it; the reading is compared against it. Used when readingtop's load average. - Nonce initialization: initial nonce = worker ID —
worker IDis the per-worker identifier passed tomine_worker; the initial nonce differs between workers. Used so workers start from different nonces.
Worked examples
- Participants: 153 joined at the start of waiting; later 242, then 241 attendees as of that moment.
- Immutability example: a transaction for $10; later the parties compromise at $9 — the change cannot be made; rewriting is incredibly time-consuming and very expensive.
- Transparency example: 2 million nodes in the network → all 2 million nodes have access to all the blocks in the chain (transcript says "2,000 blocks" — see Flagged).
- Performance analogy: one
ifcondition checking a variable > 10 is fast; one million suchifconditions (i.e., one million members each having to agree) will definitely slow down. - Difficulty example: difficulty 4 → target prefixed with 4 zeros; difficulty 5 or 6 → 5 or 6 zeros.
- Hash analogy (student answer): like shadow passwords, where each password is translated into a 128- or 256-equivalent shadow password — something similar; the hash is unique and can be attached to only one block.
- Distribution example: a data center in AWS region US and region Australia with virtual machines running there as part of the cluster that maintains the blockchain; servers in Australia and the US take part. A block stored on one server in Australia may be followed by a block stored on