-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathethereum.html
618 lines (461 loc) · 23.7 KB
/
ethereum.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
<!doctype html>
<html lang="en">
<head>
<base target="_blank">
<meta charset="utf-8">
<title>CCC: Cryptocurrency Course slide set</title>
<meta name="description" content="A set of slides for a course on Cryptocurrency">
<meta name="author" content="Aaron Bloomfield">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="../slides/reveal.js/dist/reset.css">
<link rel="stylesheet" href="../slides/reveal.js/dist/reveal.css">
<link rel="stylesheet" href="../slides/reveal.js/dist/theme/black.css" id="theme">
<link rel="stylesheet" href="../slides/ccc.css">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="../slides/reveal.js/plugin/highlight/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? '../slides/reveal.js/css/print/pdf.scss' : '../slides/reveal.js/css/print/paper.scss';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="../slides/reveal.js/lib/js/html5shiv.js"></script>
<![endif]-->
<style>
.reveal li {
font-size:90%;
line-height:120%;
}
</style>
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section data-markdown><textarea>
# CS 4501
### Cryptocurrency
<p class='titlep'> </p>
<div class="titlesmall"><p>
<a href="http://www.cs.virginia.edu/~asb">Aaron Bloomfield</a> ([email protected])<br>
<a href="http://github.com/aaronbloomfield/ccc">@github</a> | <a href="index.html">↑</a> | <a href="?print-pdf"><img class="print" width="20" src="../slides/images/print-icon.png" style="top:0px;vertical-align:middle"></a>
</p></div>
<p class='titlep'> </p>
## Ethereum
<a href='https://coinmarketcap.com/currencies/ethereum/'><img src="../slides/images/logos/eth-coin-symbol.svg" class="cclogomed" style="float:none;margin-left:auto !important;margin-right:auto !important"></a>
</textarea></section>
<section data-markdown><textarea>
# Contents
[Introduction](#/intro)
[Patricia Trees](#/patricia)
</textarea></section>
<!-- ============================================================ -->
<section>
<section id="intro" data-markdown class="center"><textarea>
# Introduction
<img src="../slides/images/logos/btc-coin-symbol.svg" class="cclogosubtitle">
<img src="../slides/images/logos/eth-coin-symbol.svg" class="cclogosubtitle">
</textarea></section>
<section data-markdown data-separator="^\n\n\n"><textarea>
<!-- .slide: class="cclogo-slide" -->
## [![btc logo](../slides/images/logos/btc-coin-symbol.svg)](https://coinmarketcap.com/currencies/bitcoin/) Bitcoin
- Created in 2009
- Bitcoin script is not Turing-complete
- Really only for transactions
- Tokens possible, but difficult
- More complicated TXNs are not enforceable
<!-- .slide: class="right-float-img" -->
## History
<a href="https://en.wikipedia.org/wiki/Vitalik_Buterin"><img src="images/ethereum/VitalikButerinProfile.webp" style="padding:0"></a>
- Vitalik Buterin in 2013: Bitcoin can do so much more!
- Bitcoin devs: meh
- Vitalik: Fine. I'll do it myself
- Vitalik then created Ethereum (at age 20)
- Now a multi-billionaire
<!-- .slide: class="cclogo-slide" -->
## Definitions
- [![eth logo](../slides/images/logos/eth-coin-symbol.svg)](https://coinmarketcap.com/currencies/ethereum/) *Ethereum* is the system / blockchain / network
- *Ether* or *Eth* (symbol: ETH) is the currency <br clear='all'>
- A *smart contract* is a program on the Etherem blockchain
- Analogous to a Bitcoin script on the Bitcoin blockchain
- *Solidity* is one (of many) programming languages that compiles to Ethereum bytecode
- Others: Serpent (Python-like), Vyper (Python-like), Bamboo (Erlang-like), etc.
## ETH
- The currency of Ethereum
- Current value: see [here](https://coinmarketcap.com/currencies/ethereum/)
- A *wei* is $1/10^{18}$ ETH
- Amounts on the blockchain are speified as an integer number of wei
- A *gwei* (giga-wei) is $1/10^9$ ETH
- If 1 ETH is worth `$`1,250, then 1 gwei is worth `$`0.00000125
- Or 800,000 gwei to the dollar
- TXN fees are typically reported in gwei
<!-- .slide: class="big-table-small" -->
## ETH part synonyms ([ref](https://www.languagesandnumbers.com/articles/en/ethereum-ether-units/))
| Int'l system | Usual name | Effigy |
|------------|--------|---------|
| $10^{-18}$ : attoether | wei | Wei Dai |
| $10^{-15}$ : femtoether | kwei, ada | Ada Lovelace |
| $10^{-12}$ : picoether | mwei, babbage | Charles Babbage |
| $10^{-9}$ : nanoether | gwei, shannon | Claude Shannon |
| $10^{-6}$ : microether | szabo, micro | Nick Szabo |
| $10^{-3}$ : milliether | finney, milli | Harold Finney |
| $1$: ether | ether | |
| $10^{3}$: kiloether | kether, grand, einstein | Albert Einstein |
| $10^{6}$: megaether | mether | |
| $10^{9}$: gigaether | gether | |
| $10^{12}$: teraether | tether | |
## Change of Perception
- Think of the Ethereum blockchain as an append-only database...
- ...of current accounts, programs, and tokens (NFTs)
- We can easily (if slowly) search the blockchain for anything
- With proper indexing (hash tables!), we can do that in almost constant time
- Blockchains store *all* iterations of a thing (account balances, etc.)
- Our index only needs to store the most recent version
- And doesn't need to store spent txn outputs
## Blockchain data
- Some items are on the blockchain itself
- A smart contract, a TXN for an account, etc.
- Others can be inferred or computed from the blockchain
- The current balance of an account or a contract
- These are the current *state*, and not stored directly on the blockchain
- The Ethereum Virtual Machine (EVM) keeps track of these, so it's called the *EVM state*
- Yet others can be determined by "rewinding" the blockchain to a previous point in time
- How much balance an account had after a given block
- The result of a specific function call
## Tokens
- A token is a "thing" on the blockchain that represents an asset
- Perhaps a physical asset, like a deed for a car
- (the legal basis for this is unclear, at best)
- Perhaps a digital asset, like ownership of an image
- Perhaps a blockchain asset, like another cryptocurrency
- Ethereum implements them as smart contracts
- Other cryptocurrencies natively support them (Ravencoin)
- This is a whole [separate lecture](tokens.html#/), coming soon...
## Account addresses
- Keypairs use 256-bit ECDSA (with secp256k1)
- Hashing algorithm is SHA3 (aka Keccak-256)
- Given a 256-bit ECDSA (secp256k1) public/private key pair...
- Take the SHA3 (aka Keccak-256) hash of the public key
- Take the last 20 bytes (160 bits) / 40 digits
- Render that in hexadecimal, and prefix with '0x'
- Example: 0xb794f5ea0ba39494ce839613fffba74279579268
- Notably, this address does NOT change per transaction like in Bitcoin!
- This is called an "owned" address
- ... because a person owns it
</textarea></section>
<section data-markdown id="checksum" data-separator="^\n\n\n"><textarea>
## Address checksum
- Originally no built-in checksum like in Bitcoin
- [EIP-55](https://eips.ethereum.org/EIPS/eip-55) adds one
- Consider:
- Ethereum account address are 40 hex digits (20 bytes)
- We would expect $6/16 = 37.5$% of those digits to be the letters a-f
- We can represent them as capitals or lower case, allowing 1 bit of a checksum for each letter digit
- A 40 digit address would have, on average, about 15 lettered digits
- Benefit: older systems allow for mixed-case addresses, and are thus unaffected
</textarea></section>
<section data-markdown data-separator="^\n\n\n"><textarea>
## Address checksum
- Equivalent addresses:
- `0x858d8f4cf6e8de4e8eba54d853c7d36015e1f36c`
- `0x858d8F4cF6e8dE4e8Eba54D853C7D36015e1f36C`
- Remix, the Solidity compiler, will insist on the checksummed form
- Ethereum's 15 bit (on average) checksum is less than Bitcoin's 32 bits
- A mis-typed address will be missed in $1/(2^{15}) = 0.3$% of cases
- Bitcoin: missed in $1/(2^{32}) = 0.00000002$% of cases
- Algorithm: if the $i^{th}$ digit is a letter:
- Print it in uppercase if the $4 \ast i^{th}$ bit of the hash of the lowercase hexadecimal address is 1
- Otherwise print it in lowercase
## Checksum implementation
JavaScript implementation from [EIP-55](https://eips.ethereum.org/EIPS/eip-55):
```
function toChecksumAddress (address) {
address = address.toLowerCase().replace('0x', '')
var hash = createKeccakHash('keccak256').
update(address).digest('hex')
var ret = '0x'
for (var i = 0; i < address.length; i++) {
if (parseInt(hash[i], 16) >= 8) {
ret += address[i].toUpperCase()
} else {
ret += address[i]
}
}
return ret
}
```
## How to checksum
- Some things may require a checksummed address
- Solidity does, in some cases
- Also some aspects of web3.py
- Ways to checksum:
- Use https://ethsum.netlify.app
- Try it with 0x858d8f4cf6e8de4e8eba54d853c7d36015e1f36c
- Solidity will warn you and suggest the checksummed address
- Just copy-and-paste that into your code
- Web3.py has a function for this: `toChecksumAddress()`
- We'll see this later
## Account Nonce
- The *account nonce* is an incrementing counter associated with each account
- Starts at 0
- *Each* transaction uses the nonce and *then* increments it
- This is NOT stored in the account address
- But it is inferred (computed) from the blockchain
- With proper indexing, finding it is almost $\Theta(1)$
- (Assuming that a hash table lookup is almost $\Theta(1)$)
- There is also a (separate) nonce in the block header, like in Bitcoin
## Contract addresses
- Some transactions put a smart contract (program) onto the blockchain
- Those programs need a *contract address* to be called
- To generate the contract address:
- Take the (binary) account address of who is executing this transaction
- That could be an owned address or a contract address
- Append the nonce (in binary) to that
- Take the SHA3 (aka Keccak-256) hash of that
- Take the last 20 bytes (160 bits)
- Render that in hexadecimal, and prefix with '0x'
- With proper indexing, finding a contract by address is fast
## Byte code
- Take Bitcoin Script, and add just one opcode: JUMP
- Conditional jumps via OP_IF statements
- Suddenly it's Turing-complete (most likely)
- Ethereum adds a bunch more
- Conditional jumps, subroutine call and return, etc.
- But really only JUMP is needed for Turing-completeness
- See the full list [here](https://ethereum.org/en/developers/docs/evm/opcodes/)
- Better link: [here](https://github.com/wolflo/evm-opcodes)
## Solidity
- One (of many) programming languages used for Ethereum
- A Turing-complete programming language
- It has loops! Recursion! Function calls!
- And is object-oriented
- It has inheritance! Visibilities!
- A Solidity program is called a "smart contract"
- Each transaction (and program call) is evaluated on every one of the Ethereum Virtual Machines (EVMs)
- And there are a [lot of them](https://ethernodes.org/)
- But how to prevent an infinite loop in a smart contract?
- Bitcoin, which doesn't have loops, paid TXN fees based on transaction byte size
## Gas
- Each TXN has a maximum number of "computational steps" the smart contract is allowed to execute
- Gas price: how much to pay, in ETH, per computational step
- Gas limit (aka *start gas*): the maximum amount of gas to pay
- If your smart contract exceeds the number of gas steps you have paid for, the smart contract is not executed
- And you lose all your gas fees! [Here](https://ethplorer.io/tx/0x70fc74551a132a301e5b4a6c1c6dbbfe23d4a6479501b60c6468cd2f6ba0cf3b) is such a TXN
- You can see a [graph of historical gas prices](https://ycharts.com/indicators/ethereum_average_gas_price)
- Current block limit: see [here](https://etherscan.io/blocks) (in the "Gas Limit" column)
## Gas example
- Assume that you agree to pay 200 gwei per gas unit
- This is your gas price: $200 * 10^{-9}$ ETH
- And you agree to pay for $g$ gas units, say $g=21,000$
- This is your *startgas*
- Example to evaluate the transaction:
- Buy $g$ gas at 200 gwei; total of 200 * 21,000 gwei = 0.0042 ETH
- As the script executes, it deducts gas from the 21,000 amount
- At the end of execution, any extra gas is refunded
- Formally:
- Let $p$ be the gas price (200 gwei), $g$ be startgas (21k)
- Let $u$ be the gas actually used: $u \le g$
- The maximum transaction fee you are willing to pay is $g \ast p$
- The actual transaction fee is $u \ast p \le g \ast p$
## Gas examples
- A simple TXN might use 21,000 gas (the typical amount)
- [This one](https://ethplorer.io/tx/0xf42fde83749cd2aa91d4d436a959a8b9e151bf7d87b716aa5b51bde43c557ea3), for example
- Gas price was about 100 gwei (actually 105.702 gwei) for that txn
- Total gas used was 21,000
- Total fee was thus 21,000 gas steps $\ast$ 105.702 gwei/step = 0.002219732760483 ETH
- At $3,045.41 per ETH (at the time)...
- That was $6.76 for the TXN
- A complex TXN could use 1 million gas (~$322 at that price)
- See the [average ETH TXN fees](https://messari.io/asset/ethereum/chart/txn-fee-avg)
- That includes more complex TXNs!
- Also see the [average gas costs](https://ethgas.watch/)
- And [how much to pay for txn speed](https://etherscan.io/gastracker)
## Exact gas costs
- Solidity is compiled down into bytecode
- Each bytecode opcode costs a different amount of gas
- 2-10 gas for simple instructions, but up to 1,000 gas for complex instructions (such as hashes)
- See the (older) table [here](https://github.com/djrtwo/evm-opcode-gas-costs/blob/master/opcode-gas-costs_EIP-150_revision-1e18248_2017-04-12.csv)
- The exact gas costs are determined from formulas in the [Ethereum yellow paper](https://ethereum.github.io/yellowpaper/paper.pdf)
- The Ethereum Virtual Machine (EVM) will keep computing until it runs out of gas
## Exact gas costs
- The "standard" transaction fee is a startgas of 21,000
- This is for a typical transfer of ETH
- No computational code computed
- This "standard" is as of August 2021 (London upgrade)
- Nothing is refunded, even if you use less gas!
- You can offer less startgas, but miners probably will not include it in their blocks
- Consider the current gas costs [here](https://ethgas.watch/)
## Why so high for gas?
- Supply and demand
- Ethereum has a fixed block rate (12-15 seconds)
- Some transactions are quite large due to the large compiled contracts therein
- But everybody wants to use it, and it can't process the transactons fast enough
## Why pay high gas fees?
- Miners will only choose the most profitable TXNs
- There are always more TXNs to mine than can fit in a block
- If you pay to little for the gas, the miners will pass it by
- See the [average gas price](https://etherscan.io/chart/gasprice) over time
- You can add a "tip" to incentivize miners to include your block faster
- See [different gas fee times](https://etherscan.io/gastracker)
- 200 gwei for gas @ 3k USD / ETH for 21k gas is $12.60
- It turns out fee computation is more complex...
## Ethereum 2.0
- In September 2022 Ethereum changed over from proof-of-work (PoW) to proof-of-stake (PoS)
- Partly to increase scalability, partly to decrease transaction fees
- It's still a bit early to see how well that worked
- TXN fees have not really gone down as much as was hoped
- Although it will probably be successful
- Current gas prices seem to be stabilizing around 14 gwei
- If Ethereum costs `$`1,500, then a "standard" transaction (21k gas) costs about `$`0.45
- A large contract deployment (1M gas) costs about `$`20
## Messages
- We have two known types of transactions so far:
- Transfer of ether from one account to another
- Deployment of a smart contract
- A message is another type of transaction
- It's invoking a smart contract's method(s)
- It also consumes gas!
- That gas is consumed by the original call, regardless of any other sub-calls or recursive calls
- You call contract A with 1,000 startgas
- That call uses 600 gas
- Contract A calls contract B, which uses 300 gas
- You only get 100 gas back
<!-- .slide: class="right-float-img" -->
## Transaction fields
![](images/ethereum/graphs/ethereum.dot.1.svg)
- Nonce: the overall TXN number for the account
- gasPrice & gasLimit: how much gas
- to: the recipient account address
- value: how much ETH (in wei)
- v, r, s: ECDSA fields: $(r,s)$ is the signature and $v$ specifies how the tuple $(r,s)$ is encoded
- init or data: empty if just ETH xfer
- If a smart contract deployment: init is the smart contract hex code
- If a smart contract function call: data has the code to make the call
## Evaluating a TXN
1. Is TXN well-formed and valid? If not, error.
2. Fee starts at *start_gas* $\ast$ *gas_price*
- Determine sending address from the signature
- Subtract fee from sending account; if not enough, error
3. set *gas* = *start_gas*, and subtract for each step
4. Transfer TXN value from sender to receiver
- If receiver is a contract, execute that code (using gas)
5. Not enough gas to execute? Revert back to start, but keep the fees
6. Else refund unspent gas to sender, send gas consumed to miner
## Ethereum TXNs
- See them at [ethplorer.io](https://ethplorer.io/last) or [etherscan.io](https://etherscan.io/)
- [TXN calling a smart contract](https://ethplorer.io/tx/0x1dd8887f7c4283f17ae5d5490f6a0e26302e8e1908cb0a9b59fe709ff54ad850)
- [Issuing a WETH token](https://ethplorer.io/tx/0x56752aba0ed7e68265576a5205c9228eba926d0d339f83b517d3b520f6d8498d)
- [Contract for SHIB](https://ethplorer.io/address/0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce#chart=candlestick)
- [ETH trasnfer from 2miners](https://ethplorer.io/tx/0xf42fde83749cd2aa91d4d436a959a8b9e151bf7d87b716aa5b51bde43c557ea3)
## London upgrade: Gas fees
- Before the London upgrade (August 5, 2021), gas fees were computed as described
- Since London...
- There is a [base fee](https://ethereum.org/en/developers/docs/gas/#base-fee) -- a minimum amount to pay (21k)
- You can add a [priority fee](https://ethereum.org/en/developers/docs/gas/#priority-fee) to incentivize miners to mine your TXN
- This was when Ethereum was proof-of work
- The real Ethereum network is now proof-of-stake
- But the fees are still the same
- On our course blockchain, gas cost is set at 1 gwei
- Much less than the real Ethereum!
</textarea></section>
</section>
<!-- ============================================================ -->
<section>
<section id="patricia" data-markdown class="center"><textarea>
# Patricia Trees
</textarea></section>
<section data-markdown data-separator="^\n\n\n"><textarea>
## Recall: Merkle Trees
![](images/bitcoin/Hash_Tree.svg)
## Overview
- From the Ethereum white paper: a Patricia tree includes
> a modification to the Merkle tree concept that allows for
nodes to be inserted and deleted, and not just changed, efficiently.
## Trie
A *trie* is a $k$-ary prefix tree that holds (usually) strings
[![](images/ethereum/trees/Trie_example.svg)](https://en.wikipedia.org/wiki/Trie#/media/File:Trie_example.svg)
<!-- .slide: class="right-float-img" -->
## Tries
[![](images/ethereum/trees/Trie_example.svg)](https://en.wikipedia.org/wiki/Trie#/media/File:Trie_example.svg)
- The key is the characters on the transitions between nodes
- The values in the nodes just show the values via the path so far
- The value is the number by each node
- Some internal nodes may not have an associated value!
- Any node has a common prefix -- the path to the root
- Reference: Wikipedia's article on [Tries](https://en.wikipedia.org/wiki/Trie)
## Radix Tree
A space-optimized Trie
<a href='https://en.wikipedia.org/wiki/Radix_tree#/media/File:Patricia_trie.svg'><img src="images/ethereum/trees/Patricia_trie.svg" style="width:650px"></a>
- Any paths that can be combined, are
- Reference: Wikipedia's article on [Radix Trees](https://en.wikipedia.org/wiki/Radix_tree)
## Binary Trie
- Like a regular Trie, but any node can have at most 2 children
- Left child is a 0 bit, right child is a 1 bit
- Used to store binary values (hashes, etc.)
- Not great for dictionaries of ASCII characters
- We can store hashes of the nodes / children to make it like a Merkle tree
<!-- .slide: class="right-float-img" -->
## Example Binary Trie
[![](images/ethereum/tries/tries.dot.1.svg)](images/ethereum/tries/tries.dot.1.svg)
- We store the hex digits 3, 7, e, and f
- The bit transitions are on the edges
- In each node:
- The path so far is in curly brackets (not actually stored in the node)
- `h(L+R)` means hash of left child and right child
- Or hash of the concatenation if only one child
- The leaves show the hex values
<!-- .slide: class="img-max-height-500" -->
## Insert into a Binary Trie
[![](images/ethereum/tries/tries.dot.2.svg)](images/ethereum/tries/tries.dot.2.svg)
- Insertion of hex value b (1011) into the tree
- Notice that we had to add 3 nodes
<!-- .slide: class="img-max-height-500" -->
## Insert into a Binary Radix Tree
- What about the compact (radix) form of the trie?
[![](images/ethereum/tries/tries.dot.3.svg)](images/ethereum/tries/tries.dot.3.svg)
<!-- .slide: class="img-max-height-500" -->
## Insert into a Binary Radix Tree
- Insert hex b (1011) into the tree; still have to add 2 nodes
[![](images/ethereum/tries/tries.dot.4.svg)](images/ethereum/tries/tries.dot.4.svg)
## PATRICIA Trees
- Reference: [this wiki page](https://eth.wiki/fundamentals/patricia-tree) and the [Ethereum yellow paper](https://ethereum.github.io/yellowpaper/paper.pdf)
- PATRICIA stands for Practical Algorithm To Retrieve Information Coded In Alphanumeric ([original paper](http://dl.acm.org/citation.cfm?id=321481))
- Design goals:
- Any insert of a value creates exactly one new node
- Storage is compact as possible
- Has 3 types of nodes: extension node, branch node, and leaf node
- The image on the next slide is from [this stackoverflow post](https://ethereum.stackexchange.com/questions/6415/eli5-how-does-a-merkle-patricia-trie-tree-work), and [released under a CC BY-SA license](https://stackoverflow.com/help/licensing)
[![](images/ethereum/patricia.webp)](images/ethereum/patricia.webp)
</textarea></section>
</section>
<!-- ============================================================ -->
</div>
</div>
<script src='../slides/reveal.js/dist/reveal.js'></script><script src='../slides/reveal.js/plugin/zoom/zoom.js'></script><script src='../slides/reveal.js/plugin/notes/notes.js'></script><script src='../slides/reveal.js/plugin/search/search.js'></script><script src='../slides/reveal.js/plugin/markdown/markdown.js'></script><script src='../slides/reveal.js/plugin/highlight/highlight.js'></script><script src='../slides/reveal.js/plugin/math/math.js'></script>
<script src="../slides/settings.js"></script>
<script>
var vals = new Array();
// often changed variables
vals['btc_price'] = 65000;
// rarely changed variables
vals['btc_reward_btc'] = 6.25;
// computations; not changed
vals['btc_reward_usd'] = vals['btc_price'] * vals['btc_reward_btc'];
Reveal.addEventListener( 'update', function() { myupdate(); } );
function myupdate() {
for (var k in vals) {
if ( document.getElementById(k) ) {
document.getElementById(k).innerHTML = vals[k].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
}
}
</script>
</body>
</html>