learn-blockchain-with-phoebe / Practitioner session 5 of 10
Learn Blockchain with Phoebe · Practitioner session 5 of 10

The cost of a rewrite

Two halves are waiting to be put together. Session 3 showed that an attacker who edits block 2 has to redo block 2 and everything after it, and that at difficulty 0 redoing it costs nothing at all. Session 4 attached a price to one block and showed that the price is a lottery draw with a known mean and a wild spread. Tonight both arrive at once. Rewriting block 2 of the six block register means re-mining blocks 2, 3, 4 and 5, which is four blocks, which is four separate draws. Run it twice and you will not get the same answer.

🟠 Working depth Engineers and data people Anyone pricing a threat model 45 min
0-4 · Welcome 4-16 · Four blocks 16-28 · Four draws 28-45 · Build-along and Q&A
Part 0

Putting the tamper and the price together

A rewrite has two quantities in it and they are usually mixed up. One is how many blocks have to be redone, which is set by where in the register the edit sits and is not random at all. The other is what one block costs to redo, which is set by the difficulty and is random every single time. Tonight the first is counted, the second is drawn live, and the two are kept apart on purpose.

Live = presented in the 45 minutes Self-study = the full depth, after class 🔍 Every diagram zooms

★ What you walk out with tonight. The count: an edit at block 2 of a six block register forces four blocks to be re-mined. The shape of the price: four independent draws, so the total swings far harder than the per block mean suggests. And the reason depth matters, which is the argument underneath every confirmation count you have ever seen quoted.

Self-studyThe words that will keep coming back4 min▶
  • Reforge. Re-mine a block and every block after it, so the register becomes internally consistent again. CHAIN.reforge(chain, index, difficulty).
  • attempts. The field reforge returns alongside the rebuilt chain. It is the total across every block it re-mined, not a per block figure.
  • Draw. One block's search for a qualifying nonce. Independent of the last one, with no memory and no progress.
  • Depth. How many blocks sit on top of the entry you want to change. The count of blocks to redo is exactly that number plus the block itself.
  • Confirmation. One block added on top of the block holding your entry. Counting them is counting depth.
  • Finality. The point at which a rewrite stops being worth worrying about. A probability that strengthens, not a line that is crossed.
  • Mean. An average over many independent blocks, quoted with its trial count. A single run is not one.
  • Spread. How far individual runs sit from the mean. At difficulty 3 the measured range ran from 40 attempts to 17,343.
Part 1 · covers the count

Four blocks, and the count is not the random part

12 min live

Edit block 2 and block 2's own hash changes. Block 3 records the old one, so block 3 has to be rebuilt, which changes block 3's hash, so block 4 has to be rebuilt, and so on to the end. Blocks 0 and 1 are untouched because nothing above them moved. The register has six blocks, the edit is at index 2, and that makes the count 4.

Edit block 2, and the damage runs to the end of the register Nothing below an edit is affected. Everything above it is. 0 untouched nothing above it moved 1 untouched no work to redo 2 entry edited its own hash no longer matches 3 prev broken records the hash block 2 used to have 4 prev broken block 3 moved underneath it 5 prev broken the last block in the register 4 BLOCKS TO RE-MINE · 4 INDEPENDENT LOTTERY DRAWS The count is arithmetic. Only the price is a lottery. Six blocks in the register, the edit at index 2, so blocks 2, 3, 4 and 5 have to be produced again. Each of the four pays a full draw at the chosen difficulty. None of them gets a discount for being late in the queue.
🔍 Click to zoom - blocks 0 and 1 survive untouched because an edit propagates upward through the prev field, never downward.
Live tamper then reforge, running in this page. Block 2's entry is edited and blocks 2 to 5 are re-mined for real.

...

...
LivePress it again, and again4 min▶

The first total means almost nothing on its own. Press the same button a second time at the same difficulty and watch the total land somewhere else entirely, sometimes by a factor of several. Same register, same edit, same difficulty, same four blocks. Four fresh draws.

That is the correct mental model of a rewrite cost, and it is why a single measured figure is not a number you should quote to anybody. It is one sample of a quantity with an enormous tail.

LiveWhere the randomness in the box comes from3 min▶

Worth being exact about, because it is easy to mistake for a trick. The nonce search itself is not random: it starts at zero and counts upward, so searching the identical bytes twice finds the identical nonce after the identical number of attempts. Re-running a fixed model gives a fixed answer, which is the same reproducibility that lets session 2 print hashes in a diagram.

What varies between runs is the block being searched. An attacker forging blocks 2 to 5 writes their own timestamps into them, and the box does exactly that, stamping a different second on each forged block each time you press it. Different bytes, different search, an independent draw per block. That is what a real block does anyway, since no two blocks ever carry the same contents.

LiveWhat reforge actually does, step by step3 min▶

Starting at the index you give it, reforge copies the chain, then walks forward. For each block it sets prev to the hash the block below it now has, searches for a nonce that satisfies the difficulty, stores the new nonce and the new hash, and adds that block's attempts to a running total. When it reaches the end it hands back the rebuilt chain and one number.

Note the order. Block 3 cannot be mined until block 2 is finished, because block 3's prev is an input to block 3's hash. The four draws are independent in their outcomes and strictly sequential in their execution, and that distinction does real work in session 8.

Self-studyWhy the rebuilt register verifies cleanly3 min▶

Run CHAIN.verify() on the reforged chain and it returns ok. Every hash recomputes, every link matches, and the entry at block 2 now says whatever the attacker wanted it to say. Nothing inside the register objects.

This is the uncomfortable half of proof of work and it is worth being blunt about it. Work does not make a rewrite detectable. Session 3 already made it detectable, with no work at all. Work makes a rewrite expensive, and expensive is a completely different property from visible. A reader holding only the rewritten register sees a perfect chain.

Part 2 · covers the price

Four draws, not four times the mean

12 min live

Session 4 measured what one block costs at each difficulty, as a mean across many independent blocks, with the trial count beside it. Those are the figures below and they are per block. They are also the only cost figures on this page, because the total for four blocks is not a figure at all: it is whatever your four draws happen to come to, and the live box above is where you get yours.

What one block costs, measured. Per block, never per rewrite. Each row is a mean over many independent blocks, with its trial count beside it. DIFFICULTY TRIALS MEAN ATTEMPTS FOR ONE BLOCK LOWEST SEEN HIGHEST SEEN 1 500 16 1 101 2 300 277 1 2,147 3 120 4,115 40 17,343 4 150 74,190 149 339,789 Every figure above is for one block. A rewrite from block 2 is four of these. Four means multiplied together would describe a tidy run that nobody is ever going to have. At difficulty 3 a single block already ranged from 40 attempts to 17,343, and a rewrite draws that dice four times over. The number worth having is the one your own run produced, next to the one it produced the time before.
🔍 Click to zoom - the mean column is the only column with a stable meaning. The lowest and highest columns are there to stop you trusting it too far.
LiveWhy four draws are wilder than one, and tamer at the same time4 min▶

Both things are true and they sound contradictory. In absolute terms four draws swing further than one, because one unlucky block is enough to blow the total out on its own and there are now four chances of that happening. In relative terms four draws are steadier than one, because a very short draw and a very long draw can land in the same total and partly cancel.

So a four block rewrite is a little more predictable as a proportion of itself and a lot less predictable as a raw count. Neither of those makes any single run a cost estimate. Run the box several times at difficulty 2 and write the totals down before you believe anything about them.

LiveEach extra zero is a step change, not a step3 min▶

Read the mean column downward: 16, then 277, then 4,115, then 74,190. Each row is roughly sixteen times the one above it, because one more required leading zero divides the qualifying hashes by sixteen. The difficulty dial is not a slider with linear notches, it is an exponent.

That is what makes difficulty a usable defence at all. A modest increase in what one block costs honest writers is a huge increase in what four blocks cost an attacker, and both sides feel it, which is the trade the next card is about.

Self-studyWhere the disagreement really is4 min▶

One camp holds that this cost is the security. The register is safe to the extent that redoing it is expensive, the expense is measurable, and a threat model can be written with the measurement in it. On that reading, work is the thing that turns a detectable edit into an edit nobody will attempt, and everything else is commentary.

The other camp holds that cost is the wrong unit entirely. Honest writers pay the cost continuously, for as long as the register is in use; an attacker pays it once, for the short window they care about. A quantity that one side pays forever and the other side pays once is not a fair comparison, and calling it security smuggles in an assumption about the attacker's budget that nobody has checked.

Both camps agree completely about the arithmetic on this page and disagree about what the arithmetic licenses you to claim. It is worth knowing which one you are talking to before agreeing that a chain is secure, because they mean different things by the word.

Part 3 · covers depth

The deeper it is buried, the dearer it is to move

7 min live

The count of blocks to redo is the depth of the entry plus one. Change the last block and you redo one. Change block 2 of six and you redo four. Change block 0 and you redo all six. Nothing about the entry matters, only how much has been written on top of it since.

Same register, same difficulty, three different targets Amber is a block that has to be produced again. THE ENTRY SOMEBODY WANTS CHANGED WHAT THAT COSTS Block 4, two from the end 0 1 2 3 4 5 2 blocks, 2 draws Block 2, in the middle 0 1 2 3 4 5 4 blocks, 4 draws Block 0, the genesis entry 0 1 2 3 4 5 6 blocks, 6 draws Cost grows with depth, and that is the whole reason confirmations get counted. An entry with four blocks written on top of it costs four draws to move. The same entry with forty on top costs forty. Which is why finality here is a probability that keeps strengthening, rather than a moment that arrives and settles it.
🔍 Click to zoom - waiting is the only thing a reader can do that makes an old entry harder to move, and it works because somebody else keeps adding blocks.
LiveA confirmation is just another block on top4 min▶

When a register is described as waiting for six confirmations before an entry is acted on, that is this arithmetic being used as a policy. Six confirmations means six blocks have been written on top, so a rewrite now has seven draws to win instead of one, and each of those draws is the full price at the current difficulty.

The choice of six is not a property of the mathematics. It is somebody deciding how much residual risk they will carry, on what they are willing to lose, and reasonable people pick different numbers for different consignments. Treat any confirmation count you are quoted as a risk appetite, not a fact.

LiveWhy it is a probability and never a certainty3 min▶

A draw with an enormous tail can always come in short. Difficulty 3 has a mean of 4,115 attempts and it has produced a block in 40. There is no depth at which the chance of a lucky sequence becomes zero, only depths at which it becomes small enough that nobody reasonable would plan around it.

So the honest sentence is that an entry gets harder to move every time a block lands on it, without limit and without ever arriving anywhere. If somebody tells you a record on a chain is final, the useful question is what depth they mean and what they are willing to lose if they are unlucky.

Self-studyDepth does nothing for a register nobody is extending3 min▶

Everything in this part assumes blocks keep arriving. Depth is accumulated by other people writing, and an entry in a register that has been quiet for a year is exactly as deep as it was a year ago. The protection did not grow; it stopped growing the moment the writing stopped.

Worth checking against whatever you are being sold. A private register with one writer and a handful of entries a month accumulates depth very slowly, so the cost argument that makes this structure interesting is doing almost no work there. The tamper evidence from session 3 still holds, and it holds at difficulty 0 for free.

Part 4 · covers the limit

What this prices, and what it says nothing about

5 min live

Everything measured tonight is one machine, working alone, redoing four blocks in sequence. That is a real and useful quantity. It is also not the threat most people mean when they ask whether a chain can be rewritten, and pretending otherwise is the commonest overclaim in the subject.

The scope of tonight's number, stated plainly What the live run prices One machine, working alone. Four blocks re-mined one after another, in order. One full draw each, at the difficulty you chose. Attempts counted by the engine, not estimated. A real quantity, and a narrow one. What it says nothing about An attacker with more machines than everyone else. Draws running alongside each other, not in sequence. A register outpaced while it is still being written. Two versions of history competing to be the longer one. No number on this page measures any of that. Tonight: a single worker redoing a fixed number of blocks, priced honestly. Who wins when two versions of the register are being extended at the same time is session 8, and it is a different argument.
🔍 Click to zoom - the left panel is what the engine can measure. The right panel is what the engine cannot see, which is why it is named rather than guessed at.
Part 5 · build-along

Price it yourself

14 min hands on

Console open on this page. Build the register at a difficulty, tamper it, reforge it, and read the total. Then do the whole thing again and compare.

Consolevar ENTRIES = [ "Harbourgate opens the register", "Consignment 41 signed over to Pell and Sons", "Consignment 41 inspected, seal intact", "Consignment 42 signed over to Ardwick Cold Store", "Consignment 41 released to the buyer", "Consignment 42 inspected, seal broken" ]; var chain = CHAIN.makeChain(ENTRIES, { difficulty: 2 }); var bent = CHAIN.tamper(chain, 2, "Consignment 41 inspected, seal BROKEN"); CHAIN.verify(bent).problems // the attacker writes their own timestamps into the blocks they forge function restamp(c) { var d = c.map(function (b) { var o = {}; for (var k in b) o[k] = b[k]; return o; }); for (var i = 2; i < d.length; i++) d[i].timestamp += Math.floor(Math.random() * 600); return d; } var r = CHAIN.reforge(restamp(bent), 2, 2); r.attempts CHAIN.verify(r.chain).ok r.chain.length - 2

Check the hash before you price anything

Run CHAIN.selfTest().ok. If the published test vectors do not pass in your browser, every attempt count you are about to read is meaningless, and that is worth four seconds.

Count the blocks before you mine them

Print r.chain.length - 2 and confirm it is 4. Then say why it is 4 without using the word hash, using only the words register, position and end.

Prove to yourself that the search is not random

Run CHAIN.reforge(bent, 2, 2).attempts twice, with no restamp. Identical both times, because the same bytes get the same search. Reproducibility is a property of the model, exactly as in session 2, and it is why the restamp is there.

Read a total, then throw it away

Now run CHAIN.reforge(restamp(bent), 2, 2).attempts twice. Different blocks, different searches, two independent totals. Write both down. Anyone quoting one of them as the cost of a rewrite has shown you they did not run it twice.

Do it five times at difficulty 2

Loop the restamped reforge five times and collect the totals. Look at the lowest and the highest. Compare that gap with the per block figures in the table above, where difficulty 2 has a mean of 277 attempts over 300 trials and a single block ranged from 1 to 2,147.

Move the target and watch the count move

Reforge from index 4 instead of 2. Confirm the rebuilt chain still verifies and that only two blocks were touched. Then reforge from index 0 and confirm all six were. The count is the only part of tonight you can predict.

Raise the difficulty one notch and feel the exponent

Rebuild at difficulty 3 and reforge again. It will take noticeably longer, and the mean per block moves from 277 to 4,115. Say out loud what a fifth zero would do to the same four blocks, given the pattern in the mean column.

The habit to keep. When somebody quotes you the cost of rewriting a register, ask two questions in this order. How many blocks, which is arithmetic they should be able to answer exactly. And how many runs is that figure averaged over, which is where most quoted costs quietly fall apart.

Check yourself

Three questions

An attacker edits block 2 of the six block Harbourgate register. How many blocks must be re-mined, and is that number random?

An edit propagates upward through the prev field and never downward, so blocks 0 and 1 are untouched. Blocks 2, 3, 4 and 5 all have to be produced again, which is 4. That count comes from the position of the edit and the length of the register, and it is the one quantity tonight that is not a lottery.

At difficulty 3 one block has a mean of 4,115 attempts over 120 trials. What can you say about the cost of a four block rewrite at that difficulty?

A single block at difficulty 3 was measured at 40 attempts at one extreme and 17,343 at the other, a factor of more than 400 around the mean. Drawing that four times gives a total nobody can hand you in advance. The per block mean with its trial count is a real figure; a rewrite total from one run is a sample, and should be labelled as one.

Why are confirmations counted at all, and what does a high count actually give you?

Depth is the count of blocks on top, and the count of blocks to redo is that depth plus one. Nothing re-checks the entry and nothing locks. A draw with a long tail can always come in short, so waiting buys a probability that keeps strengthening and never arrives at a guarantee. Any specific confirmation count is a risk appetite somebody chose.

Before session 6

Homework

Twenty five minutes, turning tonight's arithmetic into a sentence you would sign.

Run the restamped reforge ten times and keep the list

Difficulty 2, index 2, ten totals. Write down the lowest and the highest you saw. Bring the gap to session 6, because it is the cleanest evidence you will get that a single measurement of anything in this subject is not a measurement.

Find the depth in a register you rely on

Take a log or audit trail from your own work and ask how much gets written after a given row before anybody acts on it. That is your depth. Then ask what it costs anybody to rewrite from there today, and whether the honest answer is nothing at all.

Covered tonight

What this session covers

✓The count of blocks a rewrite forcesAn edit at block 2 of six means blocks 2, 3, 4 and 5, which is 4, and the count is arithmetic rather than chance.
✓reforge driven live, and its total attemptstamper then reforge in this page, with the total read off the engine and a run log so two runs can be compared.
✓Four independent draws, not four times a meanPer block means of 16, 277, 4,115 and 74,190 over 500, 300, 120 and 150 trials, with the measured extremes beside them.
✓Depth, confirmations and probabilistic finalityBlocks to redo equal depth plus one, so waiting raises the cost without ever reaching a guarantee.
◐An attacker with more machines than everyone elseNamed tonight as the limit of the measurement. Session 8.
◐Forks and the longest chain ruleImplied by two versions of the register being extended at once. Session 8 works it through.
○Who is allowed to write an entry in the first placeNothing tonight asks. Signatures and identity are session 7.
○Energy, hardware and what any of this costs to runAttempts are the unit here. Turning attempts into anything else is outside these sessions.
Take this with you

Cheat sheet

The count. Edit at index i in a register of n blocks means n minus i blocks to re-mine. Here, 4.
The call. CHAIN.reforge(chain, 2, d) returns the rebuilt chain and one total attempts.
The shape. Four independent draws. Any single total is a sample, never a cost.
Per block, measured. 16 over 500 trials, 277 over 300, 4,115 over 120, 74,190 over 150.
The spread. One block at difficulty 3 ran from 40 attempts to 17,343. Four draws of that.
Depth. Blocks to redo equal depth plus one, which is exactly what a confirmation count counts.
Finality. A probability that strengthens with every block. Not a moment, and never zero risk.
The limit. All of this is one machine alone. More machines than everyone else is session 8.