forked from ethereum/beaconrunner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
br2050.html
1899 lines (1458 loc) · 113 KB
/
br2050.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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head><meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Beacon Runner 2050: Agent-based model of PoS Ethereum</title>
<meta property="og:title" content="Beacon Runner 2050: Agent-based model of PoS Ethereum" />
<meta property="og:url" content="https://ethereum.github.io/beaconrunner/notebooks/beaconrunner2050/br2050.html" />
<meta property="og:image" content="https://ethereum.github.io/rig/static/rig.png" />
<meta property="og:description" content="A demo of ABM for protocol analysis" />
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Beacon Runner 2050: Agent-based model of PoS Ethereum">
<meta name="twitter:description" content="A demo of ABM for protocol analysis">
<meta name="twitter:image" content="https://ethereum.github.io/rig/static/rig.png">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/underscore-umd-min.js" type="text/javascript"></script>
<script src="https://ethereum.github.io/rig/static/react.development.js"></script>
<script src="https://ethereum.github.io/rig/static/react-dom.development.js"></script>
<script src="https://ethereum.github.io/rig/static/component-library.js"></script>
<script src="https://ethereum.github.io/rig/static/header.js"></script>
<script src="https://ethereum.github.io/rig/static/footer.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.10/require.min.js"></script>
<style type="text/css">
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: var(--jp-cell-editor-active-background) }
.highlight { background: var(--jp-cell-editor-background); color: var(--jp-mirror-editor-variable-color) }
.highlight .c { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment */
.highlight .err { color: var(--jp-mirror-editor-error-color) } /* Error */
.highlight .k { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword */
.highlight .o { color: var(--jp-mirror-editor-operator-color); font-weight: bold } /* Operator */
.highlight .p { color: var(--jp-mirror-editor-punctuation-color) } /* Punctuation */
.highlight .ch { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Hashbang */
.highlight .cm { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Multiline */
.highlight .cp { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Preproc */
.highlight .cpf { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.PreprocFile */
.highlight .c1 { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Single */
.highlight .cs { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Special */
.highlight .kc { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Pseudo */
.highlight .kr { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Type */
.highlight .m { color: var(--jp-mirror-editor-number-color) } /* Literal.Number */
.highlight .s { color: var(--jp-mirror-editor-string-color) } /* Literal.String */
.highlight .ow { color: var(--jp-mirror-editor-operator-color); font-weight: bold } /* Operator.Word */
.highlight .w { color: var(--jp-mirror-editor-variable-color) } /* Text.Whitespace */
.highlight .mb { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Bin */
.highlight .mf { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Float */
.highlight .mh { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Hex */
.highlight .mi { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Integer */
.highlight .mo { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Oct */
.highlight .sa { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Affix */
.highlight .sb { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Backtick */
.highlight .sc { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Char */
.highlight .dl { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Delimiter */
.highlight .sd { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Doc */
.highlight .s2 { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Double */
.highlight .se { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Escape */
.highlight .sh { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Heredoc */
.highlight .si { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Interpol */
.highlight .sx { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Other */
.highlight .sr { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Regex */
.highlight .s1 { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Single */
.highlight .ss { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Symbol */
.highlight .il { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Integer.Long */
</style>
<link rel="stylesheet" type="text/css" href="https://ethereum.github.io/rig/static/jupyter.css"/>
<link rel="stylesheet" type="text/css" href="https://ethereum.github.io/rig/static/theme-light.css"/>
<style type="text/css">
a.anchor-link {
display: none;
}
.highlight {
margin: 0.4em;
}
/* Input area styling */
.jp-InputArea {
overflow: hidden;
}
.jp-InputArea-editor {
overflow: hidden;
}
@media print {
body {
margin: 0;
}
}
</style>
<!-- Load mathjax -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-MML-AM_CHTML-full,Safe"> </script>
<!-- MathJax configuration -->
<script type="text/x-mathjax-config">
init_mathjax = function() {
if (window.MathJax) {
// MathJax loaded
MathJax.Hub.Config({
TeX: {
equationNumbers: {
autoNumber: "AMS",
useLabelIds: true
}
},
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
processEscapes: true,
processEnvironments: true
},
displayAlign: 'center',
CommonHTML: {
linebreaks: {
automatic: true
}
},
"HTML-CSS": {
linebreaks: {
automatic: true
}
}
});
MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
}
}
init_mathjax();
</script>
<!-- End of mathjax configuration -->
<link rel="stylesheet" type="text/css" href="https://ethereum.github.io/rig/static/index.css"/>
</head>
<body class="jp-Notebook" data-jp-theme-light="true" data-jp-theme-name="JupyterLab Light">
<div id="header"></div>
<script>
ReactDOM.render(
e(Header, null),
document.querySelector("#header")
);
const hamburger = document.querySelector(".hamburger");
const navMenu = document.querySelector(".nav-menu");
function mobileMenu() {
hamburger.classList.toggle("active");
navMenu.classList.toggle("active");
}
function closeMenu() {
hamburger.classList.remove("active");
navMenu.classList.remove("active");
}
hamburger.addEventListener("click", mobileMenu);
const navLink = document.querySelectorAll(".nav-link");
navLink.forEach(n => n.addEventListener("click", closeMenu));
</script>
<div class="article-container">
<div class="document-container">
<div class="title-container">
<div class="title">
Beacon Runner 2050: An agent-based model of eth2
</div>
<div class="sub-title">
</div>
</div>
<div id="toc-author-block">
<div id="authors"></div>
<div id="toc"></div>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<blockquote><p>Note: All code used in this notebook is contained in the <a href="https://github.com/barnabemonnot/beaconrunner/tree/master/notebooks/beaconrunner2050">notebooks/beaconrunner2050</a> folder of the Beacon runner repo, and does not use current eth2.0 specs. Most of the content here remains applicable.</p>
</blockquote>
<h2 id="TL;DR">TL;DR<a class="anchor-link" href="#TL;DR">¶</a></h2><ul>
<li>We improve upon our second <a href="../beaconrunner2049/br2049.html"><em>Beacon Runner 2049</em></a>, an economics-focused simulation environment for eth2.</li>
<li>Validators are placed on an <a href="https://decentralizedthoughts.github.io/2019-06-01-2019-5-31-models/">asynchronous</a> P2P network, with each validator storing their current view of the chain, incoming blocks and attestations in a <code>Store</code> object, as defined in the <a href="https://github.com/ethereum/eth2.0-specs/blob/dev/specs/phase0/fork-choice.md#store">specs</a>.</li>
<li>Validator behaviours are fully modular and can be plugged into the simulation as long as they follow a simple API, to produce rich agent-based models. Try it yourself!</li>
</ul>
<hr>
<p>We want to understand how validator behaviours map to rewards, penalties and chain outcomes. Ideally, validators who are rational are also honest, i.e., they run the eth2 protocol the way it "should" be run. But apart from how incentives are designed, there is no guarantee that this will indeed the case. And as we will see, honesty may not always have a unique instantiation.</p>
<p>In this notebook, we improve upon the <a href="../beaconrunner/br.html">first</a> and <a href="../beaconrunner2049/br2049.html">second</a> Beacon Runners by introducing a more full-fledged simulation environment. If you haven't read the previous two, that's OK! Let's catch up now.</p>
<h2 id="Previously,-on..."><em>Previously, on...</em><a class="anchor-link" href="#Previously,-on...">¶</a></h2><p>In the <a href="../beaconrunner/br.html">first notebook</a>, we introduced the possibility of "wrapping" the current specs in a <a href="https://github.com/BlockScience/cadCAD">cadCAD</a> simulation environment. We defined simple <em>strategies</em> for validators that allowed them to produce blocks and attest too. The implementation was centralised in the sense that all validators shared a common view of the chain at all times -- a situation akin to being on a network with <em>perfect information</em> and <em>zero latency</em>.</p>
<p>The natural next step was to relax this assumption, and allow different views of the chain to coexist. In the simplest case, these views have an empty intersection: this is the case when the network is perfectly <em>partitioned</em>, and each side of the partition works independently. <a href="../beaconrunner2049/br2049.html">We explored in the second notebook</a> how the <em>inactivity leak</em>, which decreases the stake of inactive validators, eventually allows for finalisation to resume. But what if this intersection is not empty? In other words, what if some validators see both sides of the partition? More generally, what if each validator has their own view of the chain, governed by the messages they have received from other validators on the network?</p>
<p>These are the conditions we explore here. They are sufficient to represent a realistic p2p network, where validators receive updates from each other after some (random) delay. We'll reuse the network model introduced in the previous notebook, reintroduced in the next section with a brief introduction to the validator API.</p>
<h2 id="Getting-started">Getting started<a class="anchor-link" href="#Getting-started">¶</a></h2><p><a href="../beaconrunner2049/br2049.html">Once again</a>, we import the specs loaded with a custom configuration file, <code>fast</code>, where epochs are only 4 slots long (for demonstration purposes).</p>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell ">
<details>
<summary>Open code input</summary>
<div class="jp-Cell-inputWrapper">
<div class="jp-InputArea jp-Cell-inputArea">
<div></div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="CodeMirror cm-s-jupyter">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">specs</span>
<span class="kn">import</span> <span class="nn">importlib</span>
<span class="kn">from</span> <span class="nn">eth2spec.config.config_util</span> <span class="kn">import</span> <span class="n">prepare_config</span>
<span class="kn">from</span> <span class="nn">eth2spec.utils.ssz.ssz_impl</span> <span class="kn">import</span> <span class="n">hash_tree_root</span>
<span class="n">prepare_config</span><span class="p">(</span><span class="s2">"."</span><span class="p">,</span> <span class="s2">"fast.yaml"</span><span class="p">)</span>
<span class="n">importlib</span><span class="o">.</span><span class="n">reload</span><span class="p">(</span><span class="n">specs</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</details>
<div class="jp-Cell-outputWrapper">
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div></div>
<div class="jp-RenderedText jp-OutputArea-output jp-OutputArea-executeResult" data-mime-type="text/plain">
<pre><module 'specs' from '/Users/barnabe/Documents/Research/Projects/beaconrunner/notebooks/beaconrunner2050/specs.py'></pre>
</div>
</div>
</div>
</div>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<p>We import our network library, seen in <a href="https://github.com/ethereum/beaconrunner/tree/master/notebooks/beaconrunner2050/network.py">network.py</a>, as well as a library of helper functions for our Beacon Runners, <a href="https://github.com/ethereum/beaconrunner/tree/master/notebooks/beaconrunner2050/brlib.py">brlib.py</a>. Open them up! The code is not that scary.</p>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs ">
<details>
<summary>Open code input</summary>
<div class="jp-Cell-inputWrapper">
<div class="jp-InputArea jp-Cell-inputArea">
<div></div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="CodeMirror cm-s-jupyter">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">network</span> <span class="k">as</span> <span class="nn">nt</span>
<span class="kn">import</span> <span class="nn">brlib</span>
</pre></div>
</div>
</div>
</div>
</div>
</details>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<p>Now on to the new stuff. We've moved <code>honest_attest</code> and <code>honest_propose</code> to a new <a href="https://github.com/ethereum/beaconrunner/tree/master/notebooks/beaconrunner2050/validatorlib.py">validatorlib.py</a> file. This file also defines a very important class, the <code>BRValidator</code>, intended to be an abstract superclass to custom validator implementations. <code>BRValidator</code> comes packaged with a <code>Store</code>, a nifty little helper class defined in the <a href="https://github.com/ethereum/eth2.0-specs/blob/dev/specs/phase0/fork-choice.md#store">specs</a> and a bunch more logic to record past actions and current parameters. We'll get to them in a short while.</p>
<p>We intend <code>BRValidator</code> to be an abstract superclass, meaning that though it is not supposed to be instantiated, it is friendly to inheritance. Subclasses of <code>BRValidator</code> inherit its attributes and methods, and are themselves intended to follow a simple API. Subclasses of <code>BRValidator</code> must expose a <code>propose()</code> and an <code>attest()</code> method which return, respectively, a block or an attestation when queried (or <code>None</code> when they are shy and don't want to return anything yet). We provide an example implementation in <a href="https://github.com/ethereum/beaconrunner/tree/master/notebooks/beaconrunner2050/ASAPValidator.py">ASAPValidator.py</a>, a very nice validator who always proposes and attests as soon as they can, and honestly too.</p>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs ">
<details>
<summary>Open code input</summary>
<div class="jp-Cell-inputWrapper">
<div class="jp-InputArea jp-Cell-inputArea">
<div></div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="CodeMirror cm-s-jupyter">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">validatorlib</span> <span class="k">as</span> <span class="nn">vlib</span>
<span class="kn">from</span> <span class="nn">ASAPValidator</span> <span class="kn">import</span> <span class="o">*</span>
</pre></div>
</div>
</div>
</div>
</div>
</details>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<p>Let's talk about cadCAD once more. Our simulations are now stochastic, since the latency of the network means that some updates are random. cadCAD makes it easy to organise and run any number of instances as well as define the steps that take place in each instance. But our simulation state is pretty large: there are <em>n</em> validators and for each validator, a certain amount of data to keep track of, including chain states and current unincluded blocks and attestations. So we are using <a href="https://github.com/cadCAD-edu/radCAD">radCAD</a>, a cadCAD extension which supports disabling deep copies of the state at each step, in addition to performance improvements.</p>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<p>We can now import the radCAD classes and methods we'll use here.</p>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs ">
<details>
<summary>Open code input</summary>
<div class="jp-Cell-inputWrapper">
<div class="jp-InputArea jp-Cell-inputArea">
<div></div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="CodeMirror cm-s-jupyter">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">radcad</span> <span class="kn">import</span> <span class="n">Model</span><span class="p">,</span> <span class="n">Simulation</span><span class="p">,</span> <span class="n">Experiment</span>
<span class="kn">from</span> <span class="nn">radcad.engine</span> <span class="kn">import</span> <span class="n">Engine</span><span class="p">,</span> <span class="n">Backend</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
</pre></div>
</div>
</div>
</div>
</div>
</details>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<p>Are we all set? It seems so!</p>
<h2 id="Discovering-the-validator-and-network-APIs">Discovering the validator and network APIs<a class="anchor-link" href="#Discovering-the-validator-and-network-APIs">¶</a></h2><p>We'll start slow, as we have done in previous notebooks, before moving to a bigger simulation. We loaded a specs configuration with 4 slots per epoch, so we'll instantiate 4 <code>ASAPValidator</code>s, one to attest in each slot.</p>
<h3 id="Genesis">Genesis<a class="anchor-link" href="#Genesis">¶</a></h3><p>First, we obtain a genesis state with 4 deposits registered. Second, we instantiate our validators from this state. A <code>Store</code> is created in each of them that records the genesis state root and a couple other things. Finally we ask our validators to skip the genesis block -- it is a special block at slot 0 that no one is supposed to suggest. The first block from a validator is expected at slot 1.</p>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs ">
<details>
<summary>Open code input</summary>
<div class="jp-Cell-inputWrapper">
<div class="jp-InputArea jp-Cell-inputArea">
<div></div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="CodeMirror cm-s-jupyter">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">genesis_state</span> <span class="o">=</span> <span class="n">brlib</span><span class="o">.</span><span class="n">get_genesis_state</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="n">seed</span><span class="o">=</span><span class="s2">"riggerati"</span><span class="p">)</span>
<span class="n">validators</span> <span class="o">=</span> <span class="p">[</span><span class="n">ASAPValidator</span><span class="p">(</span><span class="n">genesis_state</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">)]</span>
<span class="n">brlib</span><span class="o">.</span><span class="n">skip_genesis_block</span><span class="p">(</span><span class="n">validators</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</details>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<p>Note that the current store time is exactly <code>SECONDS_PER_SLOT</code> ahead of <code>genesis_time</code> (in our configuration, and the current canonical specs, 12 seconds). We've fast-forwarded beyond the first block at 0 to the start of slot 1.</p>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell ">
<details>
<summary>Open code input</summary>
<div class="jp-Cell-inputWrapper">
<div class="jp-InputArea jp-Cell-inputArea">
<div></div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="CodeMirror cm-s-jupyter">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="s2">"Genesis time ="</span><span class="p">,</span> <span class="n">validators</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">store</span><span class="o">.</span><span class="n">genesis_time</span><span class="p">,</span> <span class="s2">"seconds"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Store time ="</span><span class="p">,</span> <span class="n">validators</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">store</span><span class="o">.</span><span class="n">time</span><span class="p">,</span> <span class="s2">"seconds"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Current slot ="</span><span class="p">,</span> <span class="n">validators</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">slot</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</details>
<div class="jp-Cell-outputWrapper">
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain">
<pre>Genesis time = 1578182400 seconds
Store time = 1578182412 seconds
Current slot = 1
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<p>Let's now reuse the network we had in <a href="../beaconrunner2049/br2049.html">the second notebook</a>. The four validators are arranged along a chain, <code>0</code> peering with <code>1</code>, <code>1</code> peering with <code>2</code>, and <code>2</code> peering with <code>3</code>. We create information sets (who peers with who) to represent the chain.</p>
<p><img src="img/infosets.jpeg" alt=""></p>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs ">
<details>
<summary>Open code input</summary>
<div class="jp-Cell-inputWrapper">
<div class="jp-InputArea jp-Cell-inputArea">
<div></div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="CodeMirror cm-s-jupyter">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">set_a</span> <span class="o">=</span> <span class="n">nt</span><span class="o">.</span><span class="n">NetworkSet</span><span class="p">(</span><span class="n">validators</span><span class="o">=</span><span class="nb">list</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">]))</span>
<span class="n">set_b</span> <span class="o">=</span> <span class="n">nt</span><span class="o">.</span><span class="n">NetworkSet</span><span class="p">(</span><span class="n">validators</span><span class="o">=</span><span class="nb">list</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">]))</span>
<span class="n">set_c</span> <span class="o">=</span> <span class="n">nt</span><span class="o">.</span><span class="n">NetworkSet</span><span class="p">(</span><span class="n">validators</span><span class="o">=</span><span class="nb">list</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]))</span>
<span class="n">net</span> <span class="o">=</span> <span class="n">nt</span><span class="o">.</span><span class="n">Network</span><span class="p">(</span><span class="n">validators</span> <span class="o">=</span> <span class="n">validators</span><span class="p">,</span> <span class="n">sets</span> <span class="o">=</span> <span class="nb">list</span><span class="p">([</span><span class="n">set_a</span><span class="p">,</span> <span class="n">set_b</span><span class="p">,</span> <span class="n">set_c</span><span class="p">]))</span>
</pre></div>
</div>
</div>
</div>
</div>
</details>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<h3 id="Proposer-duties">Proposer duties<a class="anchor-link" href="#Proposer-duties">¶</a></h3><p>When we instantiate new validators, as we have done with <code>ASAPValidator(genesis_state, validator_index)</code>, their constructor preloads a few things. First, each validator checks their proposer duties for all slots of the current epoch.</p>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell ">
<details>
<summary>Open code input</summary>
<div class="jp-Cell-inputWrapper">
<div class="jp-InputArea jp-Cell-inputArea">
<div></div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="CodeMirror cm-s-jupyter">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">proposer_views</span> <span class="o">=</span> <span class="p">[(</span><span class="n">validator_index</span><span class="p">,</span> <span class="n">validator</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">current_proposer_duties</span><span class="p">)</span> \
<span class="k">for</span> <span class="n">validator_index</span><span class="p">,</span> <span class="n">validator</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">net</span><span class="o">.</span><span class="n">validators</span><span class="p">)]</span>
<span class="n">proposer_views</span>
</pre></div>
</div>
</div>
</div>
</div>
</details>
<div class="jp-Cell-outputWrapper">
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div></div>
<div class="jp-RenderedText jp-OutputArea-output jp-OutputArea-executeResult" data-mime-type="text/plain">
<pre>[(0, [False, False, False, False]),
(1, [True, True, False, False]),
(2, [False, False, False, True]),
(3, [False, False, True, False])]</pre>
</div>
</div>
</div>
</div>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<p>The array above shows for each validator index (0, 1, 2, 3) whether they are expected to propose a block in either of the 4 slots. Notice that the randomness means the same validator could be called twice in an epoch. This is distinct from attestation duties, where each validator is expected to attest once, and only once, in each epoch.</p>
<p>Since we are at slot 1, we see that validator 1 is expected to propose here. Let's ping them by calling their <code>propose</code> method, which expects a dictionary of "known items": blocks and attestations communicated over the network which may not yet have been included in the chain.</p>
<p>Let's take a brief look at the <code>propose</code> method of the ASAP validator:</p>
<div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">propose</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">known_items</span><span class="p">)</span> <span class="o">-></span> <span class="n">Optional</span><span class="p">[</span><span class="n">specs</span><span class="o">.</span><span class="n">SignedBeaconBlock</span><span class="p">]:</span>
<span class="c1"># Not supposed to propose for current slot</span>
<span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">current_proposer_duties</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">slot</span> <span class="o">%</span> <span class="n">specs</span><span class="o">.</span><span class="n">SLOTS_PER_EPOCH</span><span class="p">]:</span>
<span class="k">return</span> <span class="kc">None</span>
<span class="c1"># Already proposed for this slot</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">last_slot_proposed</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">slot</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">None</span>
<span class="c1"># honest propose</span>
<span class="k">return</span> <span class="n">vlib</span><span class="o">.</span><span class="n">honest_propose</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">known_items</span><span class="p">)</span>
</pre></div>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<p>Each validator has a <code>data</code> attribute, its "internals", maintained and updated by the <code>BRValidator</code> class. By exposing the current slot, whether the validator is supposed to propose (in <code>current_proposer_duties</code>) and whether the validator proposed something already (<code>last_slot_proposed</code>), one can build fairly sophisticated strategies already.</p>
<p>Validator 1 is the first to do anything here, so we'll leave the <code>known_items</code> attributes empty.</p>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell ">
<details>
<summary>Open code input</summary>
<div class="jp-Cell-inputWrapper">
<div class="jp-InputArea jp-Cell-inputArea">
<div></div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="CodeMirror cm-s-jupyter">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">block</span> <span class="o">=</span> <span class="n">net</span><span class="o">.</span><span class="n">validators</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">propose</span><span class="p">({</span> <span class="s2">"attestations"</span><span class="p">:</span> <span class="p">[],</span> <span class="s2">"blocks"</span><span class="p">:</span> <span class="p">[]</span> <span class="p">})</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"There are"</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">block</span><span class="o">.</span><span class="n">message</span><span class="o">.</span><span class="n">body</span><span class="o">.</span><span class="n">attestations</span><span class="p">),</span> <span class="s2">"attestations in the block"</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</details>
<div class="jp-Cell-outputWrapper">
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain">
<pre>1 proposes block for slot 1
There are 0 attestations in the block
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<p>Unsurprisingly, the new block produced does not contain any attestations.</p>
<p>Now validator 1 communicates its block to the information sets it belongs to. Since it belongs to $\{ 0, 1 \}$ and $\{ 1, 2 \}$, validators 0 and 2 receive the block at this point.</p>
<p><img src="img/propose.jpeg" alt=""></p>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs ">
<details>
<summary>Open code input</summary>
<div class="jp-Cell-inputWrapper">
<div class="jp-InputArea jp-Cell-inputArea">
<div></div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="CodeMirror cm-s-jupyter">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">nt</span><span class="o">.</span><span class="n">disseminate_block</span><span class="p">(</span><span class="n">net</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">block</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</details>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<p>In addition to the <code>data</code> attribute, our validators maintain a <code>store</code> which records current beacon chain states. We can access the blocks in those states or the states themselves from the hash of the block. Let's check if validators 0 and 3 have recorded the current block.</p>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell ">
<details>
<summary>Open code input</summary>
<div class="jp-Cell-inputWrapper">
<div class="jp-InputArea jp-Cell-inputArea">
<div></div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="CodeMirror cm-s-jupyter">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">block_root</span> <span class="o">=</span> <span class="n">hash_tree_root</span><span class="p">(</span><span class="n">block</span><span class="o">.</span><span class="n">message</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">net</span><span class="o">.</span><span class="n">validators</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">store</span><span class="o">.</span><span class="n">blocks</span><span class="p">[</span><span class="n">block_root</span><span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"0: there is a block"</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"0: no block"</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">net</span><span class="o">.</span><span class="n">validators</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span><span class="o">.</span><span class="n">store</span><span class="o">.</span><span class="n">blocks</span><span class="p">[</span><span class="n">block_root</span><span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"3: there is a block"</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"3: no block"</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</details>
<div class="jp-Cell-outputWrapper">
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain">
<pre>0: there is a block
3: no block
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<p>This confirms that validator 3 has not seen the block yet. In the next network update, triggered when <code>update_network</code> is called on the current <code>network</code> object, validator 2 communicates the block to validator 3. But let's not do that just yet, and instead fast-forward a little more to slot number 2.</p>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell ">
<details>
<summary>Open code input</summary>
<div class="jp-Cell-inputWrapper">
<div class="jp-InputArea jp-Cell-inputArea">
<div></div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="CodeMirror cm-s-jupyter">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">for</span> <span class="n">validator</span> <span class="ow">in</span> <span class="n">net</span><span class="o">.</span><span class="n">validators</span><span class="p">:</span>
<span class="n">validator</span><span class="o">.</span><span class="n">forward_by</span><span class="p">(</span><span class="n">specs</span><span class="o">.</span><span class="n">SECONDS_PER_SLOT</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Validator 0 says this is slot number"</span><span class="p">,</span> <span class="n">net</span><span class="o">.</span><span class="n">validators</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">slot</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</details>
<div class="jp-Cell-outputWrapper">
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain">
<pre>Validator 0 says this is slot number 2
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<h3 id="Attester-duties">Attester duties<a class="anchor-link" href="#Attester-duties">¶</a></h3><p>Let's check who is expected to attest at slot 2. Our <code>BRValidator</code> superclass records the slot of the current epoch where validators are expected to attest (their <em>committee slot</em>), in the <code>current_attest_slot</code> attribute of their <code>data</code>. In general, computing attester or proposer duties is expensive, so we try to cache it when we can and recompute it only when necessary.</p>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell ">
<details>
<summary>Open code input</summary>
<div class="jp-Cell-inputWrapper">
<div class="jp-InputArea jp-Cell-inputArea">
<div></div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="CodeMirror cm-s-jupyter">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">committee_slots</span> <span class="o">=</span> <span class="p">[</span><span class="n">validator</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">current_attest_slot</span> <span class="k">for</span> <span class="n">validator</span> <span class="ow">in</span> <span class="n">net</span><span class="o">.</span><span class="n">validators</span><span class="p">]</span>
<span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span> <span class="s2">"validator_index"</span><span class="p">:</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="s2">"committee_slot"</span><span class="p">:</span> <span class="n">committee_slots</span><span class="p">})</span>
</pre></div>
</div>
</div>
</div>
</div>
</details>
<div class="jp-Cell-outputWrapper">
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div></div>
<div class="jp-RenderedHTMLCommon jp-RenderedHTML jp-OutputArea-output jp-OutputArea-executeResult" data-mime-type="text/html">
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>validator_index</th>
<th>committee_slot</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td>0</td>
</tr>
<tr>
<th>1</th>
<td>1</td>
<td>1</td>
</tr>
<tr>
<th>2</th>
<td>2</td>
<td>2</td>
</tr>
<tr>
<th>3</th>
<td>3</td>
<td>3</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<p>According to the schedule here, at slot 2, validator 2 is expected to attest. Let's check what items they currently know about.</p>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell ">
<details>
<summary>Open code input</summary>
<div class="jp-Cell-inputWrapper">
<div class="jp-InputArea jp-Cell-inputArea">
<div></div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="CodeMirror cm-s-jupyter">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">known_items</span> <span class="o">=</span> <span class="n">nt</span><span class="o">.</span><span class="n">knowledge_set</span><span class="p">(</span><span class="n">net</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Validator 2 knows"</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">known_items</span><span class="p">[</span><span class="s2">"blocks"</span><span class="p">]),</span> <span class="s2">"block"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"This block was proposed by validator"</span><span class="p">,</span> <span class="n">known_items</span><span class="p">[</span><span class="s2">"blocks"</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">item</span><span class="o">.</span><span class="n">message</span><span class="o">.</span><span class="n">proposer_index</span><span class="p">,</span>
<span class="s2">"in slot"</span><span class="p">,</span> <span class="n">known_items</span><span class="p">[</span><span class="s2">"blocks"</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">item</span><span class="o">.</span><span class="n">message</span><span class="o">.</span><span class="n">slot</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</details>
<div class="jp-Cell-outputWrapper">
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain">
<pre>Validator 2 knows 1 block
This block was proposed by validator 1 in slot 1
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<p>Validator 2 knows about the block that validator 1 sent in slot 1! All is well here. Validator 2's attestation will set this block as the current head of the chain and the heat goes on.</p>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell ">
<details>
<summary>Open code input</summary>
<div class="jp-Cell-inputWrapper">
<div class="jp-InputArea jp-Cell-inputArea">
<div></div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="CodeMirror cm-s-jupyter">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">attestation</span> <span class="o">=</span> <span class="n">net</span><span class="o">.</span><span class="n">validators</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">attest</span><span class="p">(</span><span class="n">known_items</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">attestation</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</details>
<div class="jp-Cell-outputWrapper">
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain">
<pre>None
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<p>Woah, what happened here? Validator 2 refused to attest.</p>
<p>Let's back up a bit and see why. Validator 2 is expected to attest during slot 2. Honest validators however are supposed to leave a bit of time for block proposers to communicate their blocks. We are indeed in slot 2, but we are early into it, at the very start. Meanwhile, slots last for about 12 seconds and validators are only expected to attest a third of the way into the slot, i.e., 4 seconds in. This leaves 4 seconds for the block proposer of slot 2 to produce their block and communicate it (in reality, a bit more since producers can start producing before the end of the previous slot, at the risk of missing incoming attestations).</p>
<p>We can also look at the <code>attest</code> code in <code>ASAPValidator</code> to see this:</p>
<div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">attest</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">known_items</span><span class="p">)</span> <span class="o">-></span> <span class="n">Optional</span><span class="p">[</span><span class="n">specs</span><span class="o">.</span><span class="n">Attestation</span><span class="p">]:</span>
<span class="c1"># Not the moment to attest</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">current_attest_slot</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">slot</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">None</span>
<span class="c1"># Too early in the slot</span>
<span class="k">if</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">store</span><span class="o">.</span><span class="n">time</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">store</span><span class="o">.</span><span class="n">genesis_time</span><span class="p">)</span> <span class="o">%</span> <span class="n">specs</span><span class="o">.</span><span class="n">SECONDS_PER_SLOT</span> <span class="o"><</span> <span class="mi">4</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">None</span>
<span class="c1"># Already attested for this slot</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">last_slot_attested</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">slot</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">None</span>
<span class="c1"># honest attest</span>
<span class="k">return</span> <span class="n">vlib</span><span class="o">.</span><span class="n">honest_attest</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">known_items</span><span class="p">)</span>
</pre></div>
<p>Alright. Let's assume that no one wants to propose anything yet for this slot. We'll forward everyone by 4 seconds and see if validator 2 is ready to attest then.</p>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell jp-mod-noOutputs ">
<details>
<summary>Open code input</summary>
<div class="jp-Cell-inputWrapper">
<div class="jp-InputArea jp-Cell-inputArea">
<div></div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="CodeMirror cm-s-jupyter">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">for</span> <span class="n">validator</span> <span class="ow">in</span> <span class="n">net</span><span class="o">.</span><span class="n">validators</span><span class="p">:</span>
<span class="n">validator</span><span class="o">.</span><span class="n">forward_by</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</details>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell ">
<details>
<summary>Open code input</summary>
<div class="jp-Cell-inputWrapper">
<div class="jp-InputArea jp-Cell-inputArea">
<div></div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="CodeMirror cm-s-jupyter">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="s2">"Validator 2 says this is slot number"</span><span class="p">,</span> <span class="n">net</span><span class="o">.</span><span class="n">validators</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">slot</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Time is now"</span><span class="p">,</span> <span class="n">net</span><span class="o">.</span><span class="n">validators</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">store</span><span class="o">.</span><span class="n">time</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"We are now"</span><span class="p">,</span>
<span class="p">(</span><span class="n">net</span><span class="o">.</span><span class="n">validators</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">store</span><span class="o">.</span><span class="n">time</span> <span class="o">-</span> <span class="n">net</span><span class="o">.</span><span class="n">validators</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">store</span><span class="o">.</span><span class="n">genesis_time</span><span class="p">)</span> <span class="o">%</span> <span class="n">specs</span><span class="o">.</span><span class="n">SECONDS_PER_SLOT</span><span class="p">,</span> <span class="s2">"seconds into the slot"</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</details>
<div class="jp-Cell-outputWrapper">
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain">
<pre>Validator 2 says this is slot number 2
Time is now 1578182428
We are now 4 seconds into the slot
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<p>Ready to attest now?</p>
</div><div class="jp-Cell jp-CodeCell jp-Notebook-cell ">
<details>
<summary>Open code input</summary>
<div class="jp-Cell-inputWrapper">
<div class="jp-InputArea jp-Cell-inputArea">
<div></div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="CodeMirror cm-s-jupyter">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">attestation</span> <span class="o">=</span> <span class="n">net</span><span class="o">.</span><span class="n">validators</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">attest</span><span class="p">(</span><span class="n">known_items</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">attestation</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</details>
<div class="jp-Cell-outputWrapper">
<div class="jp-OutputArea jp-Cell-outputArea">
<div class="jp-OutputArea-child">
<div></div>
<div class="jp-RenderedText jp-OutputArea-output" data-mime-type="text/plain">
<pre>Attestation(Container)
aggregation_bits: SpecialBitlistView = Bitlist[2048](1 bits: 1)
data: AttestationData = AttestationData(Container)
slot: Slot = 2
index: CommitteeIndex = 0
beacon_block_root: Root = 0x204c3c90f89e27ef085dfbd38e81c07036e637d4742d5d76705a6133243204b3
source: Checkpoint = Checkpoint(Container)
epoch: Epoch = 0
root: Root = 0x0000000000000000000000000000000000000000000000000000000000000000
target: Checkpoint = Checkpoint(Container)
epoch: Epoch = 0
root: Root = 0x838fb6c5f9b6e8ae5005b16fed7da017452a8bd9d7ed2eda6434b69db123485c
signature: BLSSignature = 0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="jp-RenderedHTMLCommon jp-RenderedMarkdown jp-MarkdownOutput " data-mime-type="text/markdown">
<p>Yes! Validator 2 returned a well-formed attestation. ASAP validators are in a hurry, sure, but not in such a hurry that they would attest too early in the slot.</p>
<p><em>What are the dangers of attesting too early?</em> Simply put, validators are rewarded for attesting to the correct head of the chain from the perspective of their committee slot. Suppose a validator is expected to attest for slot 2. They send their attestation out before receiving the block for slot 2. In their attestation, they attested for the head of the chain they knew about, i.e., the block at slot 1. Later on, the block for slot 2 is included in the canonical chain. The head in the attestation is now incorrect!</p>
<p><img src="img/earlyattest.jpeg" alt=""></p>
<p><em>But shouldn't a validator attest as late as possible then?</em> We are dipping our toes in the waters of game theory here. I like it. Maybe! Though attesting too late means that the reward obtained for being included early decreases, and if you are <em>really</em> too late, like an epoch late, then you cannot be included at all. So pick your poison here.</p>
<p>We'll define (or you can try it yourself!) a different validator behaviour, attesting <em>as soon as a block is received in the slot, or slightly before the end of the slot if no block comes in</em>. This is much unlike the current validator, who attests four seconds in no matter what. Let's look at this in <a href="../thunderdome/thunderdome.html">a different notebook</a>!</p>