The Grammar · Contracts Talking to Contracts · Week 9 · Checkpoint 19
What to Read Choosing
Five hundred and thirty two lines, of which you will read about twenty on purpose. The skill here is not reading faster. It is picking three functions out of a long file, running a real number through one of them, and leaving the rest closed knowing why.
15 steps~28 min3 nodes for your map
01 · Five hundred and thirty two lines
This file is five hundred and thirty two lines long. Nobody reads it top to bottom, including the people who wrote it. What you are looking at is the first thirty seven lines, and the panel will never show you more than a window at a time, because a window is how the file is actually used.
So this checkpoint is not about reading faster. It is about choosing. You will open three functions completely, meet a handful more as names only, walk past one stretch of the file on purpose, and finish able to say which is which.
02 · The part you always read whole
The top of a file is the one part you do read whole, because it is short and it says what you are holding. A licence, a version ceiling, four imports, and on line 14 the word `library`, which you have met: no stored values, no constructor, nothing sitting at an address.
Line 15 is the attaching statement again, `using SafeCast for *`, pointed at every type rather than one. Four imports mean four more files you are not opening today, and noticing that without opening them is already the method working.
03 · Reading the declarations, not the bodies
Here is a stretch from the middle of the file with the bodies dimmed. What is lit is five declarations, and a declaration is a complete sentence by itself: a name, the values it takes, the values it hands back. You can read all five of these in about ten seconds.
That is the first pass, and it is all the first pass is. You are building an index, not an understanding. The names tell you what kind of thing lives here, and full attention is spent later, on whichever one turns out to be the function you need.
You open a file of five hundred and thirty two lines because you need one thing out of it. What is the first pass?
04 · Names that arrive in pairs
Four of the lit lines are two names, each written twice. `parseUint` on lines 159 and 171, `tryParseUint` on lines 182 and 192. You have seen this pairing in another file: one of them stops the transaction when the work cannot be done, the other hands you a bool and leaves the decision to you.
Line 173 is the revert inside the first. Line 197 returns `false` rather than stopping. The prefix on a name is doing as much work as the name, and the same pair repeats further down for signed integers, for hexadecimal, and for addresses.
05 · The word that makes a function readable alone
Line 171 carries `pure`, and here is that word in its natural home. A `view` function may read stored values without writing them. A `pure` function may not even read them. Everything it works with arrives in the parentheses, and everything it produces leaves through the return.
That has one consequence worth the whole checkpoint. A pure function can be settled by reading it and nothing else. No other line of this file, no stored value, no call made earlier can change what lines 172 to 174 do with a given input.
06 · What the library keeps, and what it refuses
A library holds no state, so lines 17 to 22 are `constant`, fixed when the file is compiled. Line 17 is sixteen characters, `0123456789abcdef`, and you will watch it get indexed shortly. Line 18 is twenty, the byte count of an address. Lines 19 to 22 belong to a function you are not opening.
Then three errors, declared once and thrown by name. Line 27 carries a value and a length, so a caller learns both numbers. Line 32 carries nothing, because a bad character has nothing useful to hand back. The one on line 27 is the one you are going to reach.
07 · One name, three declarations
Three declarations on this board carry the same name. Line 72 takes a `uint256`. Line 81 takes a `uint256` and a `uint256`. Line 100 takes an `address`. Solidity allows that, and it chooses between them by the number and the types of the arguments written at the call site.
There is a fourth further down at line 131 taking a `bytes memory`, which you can note and leave closed. The name is not the function. When a call reaches a name declared four times, the argument list is the only thing deciding which body runs.
08 · Following one call to its declaration
Line 100 is the shortest of the three. Its body is a single line: widen the address into a `uint256` by hand, and call a `toHexString` again, this time with a second argument. That second argument is `ADDRESS_LENGTH`, the constant you read on line 18 of the file, which is twenty.
So a call carrying one address lands here and immediately becomes a call carrying two arguments, one of which you never wrote. Work out which of the three declarations line 101 reaches, and what width it is asking that declaration for, before reading any further.
09 · Opening the one you chose
This is the function you picked, and from here you read every line of it. Line 81 takes a value and a length, returns a `string memory`, and is `pure`. Line 82 copies the value into a local, because the original is wanted again later and the loop is about to destroy this one.
Line 83 asks for a buffer of `2 * length + 2` bytes: two characters for every byte of output, plus two for the prefix. Lines 84 and 85 write that prefix immediately, a `0` and an `x`, and the rest of the buffer has nothing in it yet.
10 · Three lines, run backwards
The loop is three lines and it runs from the back of the buffer toward the front. Line 86 starts `i` at the last position and brings it down, stopping while `i > 1` so the two prefix characters are never overwritten. Lines 87 and 88 do all of the actual work.
Line 87 picks one character out of the sixteen on line 17 of the file and writes it. Line 88 shifts the local value right by four bits. Tap each line and read what it is taking off the number, then you will run a real value through the three of them.
11 · One value, all the way through
Call it with a value of 255 and a length of 1. Line 83 makes a buffer of four bytes. Lines 84 and 85 fill the first two with `0` and `x`. Line 86 starts `i` at three, and 255 is eight ones in binary, so its lowest four bits are 1111, which is fifteen.
Line 87 writes `HEX_DIGITS[15]`, an `f`, at position three. Line 88 leaves fifteen behind. The second pass writes another `f` at position two and leaves zero, `i` drops to one, and the loop ends. Line 93 hands back `0xff`. That is what reading a pure function completely gets you.
12 · The line that decides it did not fit
Once the loop ends, `localValue` holds whatever the shifting could not consume. If the buffer was wide enough the number reached zero and line 90 is false. If it was not, part of the number is still sitting there and has quietly gone nowhere, which is the one outcome this function refuses.
Line 91 reverts with the error declared on line 27, carrying the original value and the width you asked for. Call it with 256 and a length of 1 and that is what you get: two zero characters written, a one left over, and a stop.
13 · Recognising a stretch and walking past it
Near the end of the file sits this. Line 513 is a declaration you can read without help: private, `pure`, takes a buffer and an offset, hands back a `bytes32`. Then line 515 opens an `assembly` block, and what is inside it is a different language with different rules.
You have met that shape before and you are not descending into it here. The comment on lines 507 to 512 says what the function is for, the declaration says what goes in and comes out, and that is enough to carry on elsewhere. Stopping on purpose is a reading skill.
14 · What three functions bought you
Count what you have actually read. Three functions completely: lines 171 to 175, lines 81 to 94, and the three line one at 100 to 102. Seven more you have seen as a declaration and nothing else. The remaining twenty one in this file you have not looked at, and that is the right number.
That is about twenty lines of function body out of five hundred and thirty two, and they were the right twenty rather than a random twenty. Say precisely what reading them buys you, and just as precisely what it does not, before the checkpoint closes.
15 · Three functions, on purpose
Two words did most of the work. `pure`, which says a function reads nothing outside its own arguments and is therefore the one kind of function you can finish reading. And a name declared more than once, where the argument list rather than the name decides which body runs.
The third thing is not a keyword. You chose what to read. Five hundred and thirty two lines, three functions opened, one stretch of a different language recognised and left alone, and an honest sentence at the end about what you now know.
BANK_DBowner: the bank
you2,400
what the app is actually showing you
BANK_DBowner: the bank ✍
you2,400their pen
you hold a claim. they hold the pen.
your digital life
BANK · you2,400the bank ✍
INSTAGRAM · you2.1M followersMeta ✍
STEAM · you134 gamesValve ✍
AIRLINE · you58,200 milesthe airline ✍
four tables. zero pens that are yours.
BANK_DBowner: the bank ✍
you2,400
DENIED ✗
try both pens
PLATFORM_DBowner: the platform ✍
her · 8 years2,000,000 followers
one automated decision away
your row stands on all three
FTX_DBowner: FTX ✍
you5 BTC
the row stayed. the backing did not.
CARD_DBowner: your bank ✍
TV you never bought−1,100
fraud reversal+1,100 ✓
someone holds the pen, so someone can fix it
?_DBowner: nobody
youstill yours?
can a table exist that nobody owns?
?_DBowner: ̶n̶o̶b̶o̶d̶y̶
you100
no owner, no pen, no trust?
keeper 1
you100
keeper 2
you100
keeper 3
you100
keeper 4
you100
keeper 5
you100
no THE copy, only copies.
keeper 2
you100
keeper 3
you100
keeper 4
you100
keeper 5
you100
your copy
you100
five copies. one of them is yours.
one attacker, ten thousand faces.
writing costs watts. faking voters buys nothing.
proof of work, burn energy to vote.
rewrite one line, break every lock after it.
the price buys trustlessness. the office already has trust.