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

Smart contracts, mechanically

Harbourgate wants one thing automated. When an inspection entry records that a seal was intact, the consignment should be released without anybody deciding again. Write that as a rule, file it in the register, and every participant will run it and reach the same answer. Tonight is what that arrangement actually is, the single requirement that shapes every odd thing about how such rules are written, and the three guarantees people assume it carries and it does not.

🟠 Working depth Engineers and data people Anyone being sold automation on a register 45 min
0-4 · Welcome 4-16 · The mechanism 16-32 · What it does not guarantee 32-45 · Build-along and Q&A
Part 0

The name does most of the damage

Two words, both wrong. It is not a contract in the legal sense, because nothing in it binds anybody and nothing in it knows what anybody intended. It is not smart, because it does exactly what it was written to do and has no opinion about whether that is sensible. Say both of those out loud before anything else, because almost every disappointment in this area is somebody taking the name at face value.

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

★ What you walk out with tonight. One sentence for the mechanism: code stored in the register, which every participant runs against the same inputs and therefore agrees on the output of. One requirement that explains the rest: same inputs, same output, on every machine. And three limits stated plainly enough to say to somebody selling you the idea.

LiveWithout the hype4 min▶

A rule is filed in the register the same way an entry about a consignment is filed. It is hashed with everything else, it is linked to what came before it, and anybody holding a copy can see which version of it is there. When something happens that the rule cares about, every participant runs the rule against the register they hold. Because they hold the same register and run the same rule, they produce the same output, and there is nothing left to argue about.

That is the whole of it. The interesting property is not that code runs, which is unremarkable. It is that a rule nobody can quietly swap out produces a result nobody has to take on trust, because everybody computed it themselves.

Real world

Tokens, dapps and the wider stack built on top of this idea belong to learn-web3-with-phoebe, and none of the mechanism tonight depends on any of it.

Self-studyThe words that will keep coming back4 min▶
  • Deterministic. Same inputs, same output, every time, on every machine. The single requirement the whole design is built around.
  • State. What the rule is allowed to read: the register, and nothing else.
  • Oracle. Whatever puts outside information into the register so a rule can react to it. A person typing, or a sensor reporting. Session a5.
  • Append only. A rule adds entries. It does not replace them, so a rule that behaved wrongly leaves its wrong output in place.
  • Immutability. The property that makes an old entry trustworthy, and the same property that makes a mistake permanent.
  • Code is the agreement. The position that what the rule does is what was agreed, with no appeal above it. Part 4, argued both ways.
  • Upgradeability. Whatever arrangement lets a filed rule be replaced later. Every such arrangement gives somebody the power to change the rule, which is the thing filing it was meant to prevent.
Part 1 · covers the mechanism

Code in the register, run by everybody

12 min live

Nothing about the storage is special. The rule is an entry. What is special is what happens next: every participant runs it for themselves, rather than asking anybody what the answer was.

A rule is an entry. Agreement comes from everybody running it, not from anybody reporting it. An entry in the register Filed like any other line, hashed and linked with it Every participant runs it Same inputs, same order The same output Nothing left to disagree about WHAT EACH PARTICIPANT DOES, WITHOUT ASKING ANYBODY Participant one Reads the same entries, runs the same rule Participant two Reads the same entries, runs the same rule Participant three Reads the same entries, runs the same rule It is not a contract, and it is not smart. It is a rule stored where everybody can see it, run by everybody, with nothing left to take on trust. The difference from an ordinary stored procedure is not the code. It is that no single party can quietly replace it, and every party can check which version ran.
🔍 Click to zoom - the rule is stored the way an entry is stored. Everything unusual is in who runs it and how many of them do.
LiveWhy everybody running it is the point3 min▶

Ordinary automation runs in one place and reports its answer. Everyone downstream believes the report, because checking would mean holding the same data and the same code, which they do not. The report is a trust relationship wearing the clothes of a computation.

Here the computation is repeated by everybody who cares. There is no report to believe. A participant who thinks the output is wrong recomputes it, and either they are wrong or the others are, and the disagreement is settled by arithmetic rather than by seniority.

Self-studyWhat the rule is allowed to see3 min▶

A rule reads the register and nothing else. That is not a restriction somebody added for safety, it is the only thing every participant demonstrably holds in common. Anything outside it is something one machine has and another does not, and the moment a rule depends on such a thing the participants stop agreeing.

The practical consequence is that any fact the rule needs must already be an entry. Somebody has to have written it down first, which is where Part 3 starts.

Part 2 · covers determinism

One requirement, and the strangeness follows from it

8 min live

Most lists of rules about writing these programs read as arbitrary. They are not. Every one of them is the same requirement applied to a different temptation: every machine must reach the same result from the same inputs, or the agreement that made the whole thing worth doing collapses.

Every machine must reach the same result from the same inputs. Everything else is a consequence. RULED OUT WHY THE AGREEMENT BREAKS IF YOU DO IT Reaching out to the network Asking anything outside the register Two machines ask a moment apart and get two answers, so they compute two outputs and there is no agreement left. The clock and the dice A wall clock reading, a fresh random draw Neither is shared, so neither can be agreed. A value nobody else can reproduce is a value nobody else can check. Arithmetic that drifts Any operation whose last digit depends on the machine One digit of disagreement is disagreement. There is no close enough in a system built on exact comparison. The strangeness is not a house style. Each restriction is this one requirement, meeting a different temptation to reach outside the register. Determinism is why a rule can be trusted to have run the same way on a machine you have never seen.
🔍 Click to zoom - learn the requirement and you can derive the rules. Learn the rules and you will be surprised by the next one.
LiveRandomness is the sharpest case3 min▶

A rule that needs to pick something at random cannot simply draw a number, because every participant would draw a different one and the register would fork into as many versions as there are participants. Anything used as randomness has to be a value they all already hold, which means it is not secret from anybody who holds the register, including whoever stands to gain from the outcome.

That is a genuinely hard problem, and it is hard for a reason that is now obvious rather than mysterious: shared and unpredictable are close to opposites when everybody has the same copy.

Self-studyTime, which people assume is free3 min▶

A rule cannot ask what time it is. Each participant's clock says something slightly different, and slightly different is enough. What a rule can use is a value recorded in the register, which is a claim somebody made about the time rather than the time itself.

So a deadline enforced by such a rule is enforced against a written claim, not against the world. That is a smaller thing than it sounds like when somebody says the deadline is enforced automatically, and it is the same shape as the limit in Part 3.

Part 3 · covers the limits

Three things it does not guarantee

10 min live

These are the session. Each one is assumed by somebody buying this idea, none of them is delivered by it, and all three failures verify perfectly clean, because from inside the system nothing has gone wrong.

Three assumptions people bring, and what is actually on offer WHAT PEOPLE HEAR WHAT IS ACTUALLY TRUE WHERE THE ANSWER LIVES The code is correct Limit one It runs exactly as written, bugs included. A mistake is kept as firmly as a rule, and permanence applies to both of them. Review and testing, before the rule is filed. It knows the seal held Limit two It knows an entry says so. Somebody put that entry there, and the rule has no way of seeing past it. The point of entry, as in any register. It is legally binding Limit three Running is not agreeing. Nothing in the mechanism decides what anybody meant, or what to do when the result surprises them. Contracts, jurisdiction and people. All three failures verify clean. The structure reports no problem, because from inside the system there is no problem. That is it working.
🔍 Click to zoom - none of these three is a defect to be fixed. They are the edges of what the mechanism was ever able to claim.
LiveA bug is as permanent as a feature4 min▶

Take the property that makes an old entry trustworthy and apply it to a rule. Nobody can change the rule after the fact, which is exactly what you wanted when you imagined somebody tampering with it. It is exactly what you did not want the moment you notice the rule says intact where it should have said broken.

The mechanism draws no distinction between those two cases, because a distinction would require knowing what was intended, and that is precisely the thing it has no access to. Correctness has to be bought before filing, with review and testing, by the ordinary unglamorous means. There is no arrangement that keeps a rule beyond reach and also lets you fix it.

LiveThe oracle problem, with money or goods attached4 min▶

Session a5 established that whatever a register knows about the world, a person or a sensor put there, at unchanged trustworthiness. A rule that reacts automatically makes that limit sharper rather than softer. Before, a false entry sat in a register and somebody might notice before acting on it. Now the entry is the action: write the sentence and the consignment moves.

So automation raises the value of lying at the point of entry, and every defence against that is in the physical world. Two signatures instead of one, a photograph hashed alongside the entry, a second sensor that would have to fail in the same direction at the same moment. None of them is a chain technique.

Self-studyWhat happens when nobody intended the result4 min▶

Suppose a rule does something every party agrees was not what they meant. The register holds a valid output, produced correctly by a rule everybody ran. There is no error to report and nothing to verify differently. The question of what should happen now is a question the chain has no answer to, and never claimed to.

It goes where such questions went before: to whatever written agreement exists outside the register, to whoever has jurisdiction over the parties, and to what the parties are willing to do voluntarily. Any organisation relying on automatic rules should know which of those it would be reaching for, before it needs one.

Part 4 · covers the open argument

Code is the agreement: feature, or bug

6 min live

This one is not settled and is not going to be settled here. It is worth knowing both readings well enough to state either, because which one the person opposite you holds will predict most of what else they think.

Serious people hold both of these. Neither side is missing the mechanism. HELD AS A FEATURE Ambiguity in an ordinary agreement is expensive. It is argued over, lawyered over and settled late, by whoever can afford to keep arguing longest. A rule that simply runs removes all of that. Everybody knows the outcome before committing, and nobody can reinterpret it afterwards. HELD AS A BUG Ambiguity is where judgement lives. Every serious agreement has a way to pause, to appeal, to say letter and intent came apart. A rule that simply runs has no such door. When the result is one nobody wanted, there is no mechanism to reach for, only the result. They agree on what happens. They disagree on what a missing escape hatch is worth. Which makes it a question about your own exposure and your own counterparties, not a question about chains.
🔍 Click to zoom - the disagreement is about how often the letter and the intent come apart in your line of work. That is empirical, and it is about you.
Self-studyThe escape hatch that eats itself4 min▶

The obvious compromise is to file the rule and keep a way to replace it if it turns out wrong. Notice what that does. Whoever holds the power to replace the rule now holds the power to decide outcomes, which is the position the arrangement was built to remove. Participants are back to trusting a party, and the rule is a strong default rather than a guarantee.

That is not an argument against it. Plenty of operations would rather have a strong default with a named owner than an unstoppable rule. It is an argument for saying which one you have bought, because the two are sold with the same vocabulary and they are not the same product.

Part 5 · covers the worked case

The Harbourgate seal rule, traced

6 min live

Take the rule seriously and follow it end to end. Two entries of the Harbourgate register are already the input and the output: block 2 records the inspection, block 4 records the release. The question is what putting a rule between them buys, and what it leaves exactly where it was.

Release the consignment when an inspection entry records the seal intact THE INPUT IT READS Consignment 41 inspected, seal intact f325e5d9 THE RULE If the seal is recorded intact, append the release. THE OUTPUT IT WRITES Consignment 41 released to the buyer e5526e6e The first box is the one step nobody can verify from inside the system. Everything after it is arithmetic anybody can redo. That entry is a person's word, arriving as every entry does. WHAT THIS BUYS No argument about whether the rule was applied. It cannot be applied to one consignment and withheld from another without everybody seeing. WHAT IT DOES NOT BUY No assurance the inspector opened the door. The rule cannot tell an inspection from a sentence about one, since only the sentence is in here. Automatic application of a rule is a real gain, and it is worth buying. It is a different thing from assurance about the world, and the two are sold in the same sentence.
🔍 Click to zoom - block 2 and block 4 of the register are the real input and the real output. The rule only removes the step in the middle where a person decided.
Live makeChain, tamper and verify, running in this page. The engine hashes, links and checks. It does not execute the rule.

...

...
LiveTwo ways the same sentence can be false3 min▶

Press the second button and the inspection line is changed after the fact. Verification reports a mismatch at that block immediately, which is the case the structure was built for and it handles it loudly.

Now consider the other case. The false line was filed at the time, by the person entitled to file it. Nothing to detect, nothing to report, and the rule runs on it. The register is in perfect health and the consignment is gone. Only one of these two is a chain problem, and it is the less likely one.

Part 6 · build-along

Write the rule, file the inputs, find the step nobody can check

13 min hands on

Console open on this page. Nothing here executes the rule, and nothing pretends to. You write the rule as deterministic pseudocode, file the entries it would read using the real engine, verify them, and then say out loud which step the structure cannot support.

Pseudocodeon each new entry E in the register: if E reads "Consignment 41 inspected, seal intact" and the register already holds "Consignment 41 signed over to Pell and Sons" and the register holds no release for consignment 41: append "Consignment 41 released to the buyer" reads: the register, and nothing else does not read: a clock, a network, a random draw
ConsoleCHAIN.selfTest().ok var RULE = "Rule filed: release consignment 41 once an inspection entry records the seal intact"; var entries = [ "Harbourgate opens the register", RULE, "Consignment 41 signed over to Pell and Sons", "Consignment 41 inspected, seal intact", "Consignment 41 released to the buyer" ]; var chain = CHAIN.makeChain(entries); CHAIN.verify(chain).ok chain.map(function (b) { return b.index + " " + b.hash.slice(0, 16); }) var edited = CHAIN.tamper(chain, 3, "Consignment 41 inspected, seal broken"); CHAIN.verify(edited).problems

Check the hash first

Run CHAIN.selfTest().ok. Four seconds, and everything below is worth nothing without it.

Write the rule so two strangers get the same answer

Rewrite the pseudocode above for a rule of your own. Then hand it to somebody and ask them to find an input on which the two of you would disagree. Every disagreement they find is a place the rule reached outside the register.

Hunt your own non-determinism

Go back through your rule and mark every point where you wanted a time, a lookup, or a choice. Write beside each one what a second machine would have got instead. Three is a normal score.

File the inputs as ordinary entries

Build the chain above and confirm verify(chain).ok is true. Note that the rule itself is entry 1 and is hashed and linked exactly like the consignment lines around it. There is nothing special about how it is stored.

Name the step nobody can verify

Five entries. Point at the one whose truth cannot be established from inside the register, and say who would have to be trusted for it instead. Then say what you would put in place around that person, given that no amount of chain helps.

Break it the easy way and the hard way

Run the tamper line and read what verify reports. Then describe, in one sentence, the version of the same falsehood that verification would never report, and which of the two you would actually expect to face.

Write the bug you would not be able to fix

Take one rule your organisation runs today and invert a single condition in it. Write what would happen for a week. Then write what you would do if that rule could not be edited, only appended to.

The habit to keep. When somebody says a rule runs automatically on a register, ask two questions. What does it read, and who wrote that. The first answer tells you whether it can be agreed on. The second tells you what it is actually worth.

Check yourself

Three questions

Why can a rule of this kind not ask the network for a value while it is running?

Every restriction of this kind comes from one requirement: same inputs, same output, on every machine. A value that is not already in the register is not shared, and anything not shared cannot be agreed. Speed is not the issue and storage is not the issue.

A rule releases a consignment when an inspection entry records the seal intact. The inspector never opened the door and filed the entry anyway. What does the system report?

Nothing has gone wrong from inside the system. The entry was filed by somebody entitled to file it, the rule read it and did what it was written to do, and verification has nothing to object to. This is the oracle problem from session a5 with a consignment attached to the outcome.

Which statement about "the code is the agreement" is honest?

The two camps agree completely about what the mechanism does and disagree about how much a missing escape hatch costs, which depends on how often the letter and the intent come apart in your own operation. That is an empirical question about you, not a question about chains.

Before session 10

Homework

Twenty minutes, on one rule your organisation already runs without a human deciding each time.

Write down what it reads

List every input that rule depends on, and beside each one write who or what produced it. Circle any input that came from a person typing or a sensor reporting. Those are the ones automation makes more valuable to falsify.

Write down what you would do if it could not be changed

Assume the rule is filed permanently and can only be added to, never edited. Say in two sentences how you would handle the week it turns out to be wrong. Bring both answers to session 10, where the engine runs end to end.

Covered tonight

What this session covers

✓The mechanism, stripped of the nameCode stored in the register, run by every participant against the same inputs, so the output is agreed rather than reported.
✓Determinism as the design constraintNo network mid run, no clock, no fresh randomness, no arithmetic that drifts. One requirement, three consequences.
✓The three non-guaranteesIt does not make the code correct, it knows nothing about the world, and it binds nobody by itself.
✓Code is the agreement, both sidesRemoving costly ambiguity against removing a necessary escape hatch. Left open, because it is open.
✓The Harbourgate seal rule, tracedFrom the inspection entry at f325e5d96bee6a1d to the release at e5526e6e0bbc1aed, and the step in between that nobody can check.
◐Tokens, dapps and the wider stackNamed here as what gets built on the mechanism, and handed to learn-web3-with-phoebe.
◐Running the whole engine end to endBuild, tamper, mine, reforge and prove together. Session 10.
○Named languages, platforms and execution environmentsNone of the mechanism depends on which one, and they move faster than a course can.
○Key management and who may file a ruleSession 7 for identity; the security and access control courses for the discipline itself.
○Anything about currency, price or marketsNot taught here, and nothing on this page is investment advice.
Take this with you

Cheat sheet

The one sentence. Code stored in the register, run by everybody, so the output is agreed rather than reported.
The name. Not a contract, and not smart. Both halves mislead, and most disappointment starts there.
The requirement. Same inputs, same output, on every machine. Everything odd follows from it.
Ruled out. Network calls mid run, the wall clock, fresh randomness, arithmetic that drifts.
Limit one. It runs as written, bugs included, and a bug is as permanent as a rule.
Limit two. It knows only what an entry says. Somebody put that entry there.
Limit three. Running is not agreeing. What was intended is a question it cannot hold.
The open argument. Removing ambiguity, or removing the escape hatch. Both held by serious people.
The two questions. What does the rule read, and who wrote that.