-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathView and Pure Functions.srt
656 lines (492 loc) · 11.5 KB
/
View and Pure Functions.srt
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
1
00:00:00,410 --> 00:00:02,580
functions can be declared to have
2
00:00:02,580 --> 00:00:05,609
certain visibility like public private
3
00:00:05,609 --> 00:00:09,719
internal and external functions can also
4
00:00:09,719 --> 00:00:11,759
be declared to have certain behavior
5
00:00:11,759 --> 00:00:16,320
like boo peer and payable in this video
6
00:00:16,320 --> 00:00:18,900
we're gonna go over what beyou and peer
7
00:00:18,900 --> 00:00:22,859
functions are functions declared with
8
00:00:22,859 --> 00:00:24,990
the keyword view promised not to modify
9
00:00:24,990 --> 00:00:28,230
the state of the blockchain according to
10
00:00:28,230 --> 00:00:30,689
the solidity documentation the following
11
00:00:30,689 --> 00:00:32,549
are considered to modify the state of
12
00:00:32,549 --> 00:00:34,800
the blockchain writing to state
13
00:00:34,800 --> 00:00:39,090
variables emitting events also known as
14
00:00:39,090 --> 00:00:41,160
logging this is a topic for another
15
00:00:41,160 --> 00:00:44,450
video creating other contracts using
16
00:00:44,450 --> 00:00:47,070
self-destruct this is a function to call
17
00:00:47,070 --> 00:00:49,140
when you want to delete your contract
18
00:00:49,140 --> 00:00:51,390
from the blockchain sending either by
19
00:00:51,390 --> 00:00:53,489
calls in other words calling the
20
00:00:53,489 --> 00:00:55,350
function to send either to another
21
00:00:55,350 --> 00:00:58,140
address calling any function not marked
22
00:00:58,140 --> 00:01:00,780
as view or peer using low-level calls
23
00:01:00,780 --> 00:01:03,750
and using inline assembly that contains
24
00:01:03,750 --> 00:01:05,970
certain op codes so if a function is
25
00:01:05,970 --> 00:01:08,430
marked as Bude then that function cannot
26
00:01:08,430 --> 00:01:10,260
contain any code that does any of
27
00:01:10,260 --> 00:01:13,020
actions mentioned here let's go over
28
00:01:13,020 --> 00:01:15,780
some examples in remix first we will
29
00:01:15,780 --> 00:01:17,820
walk through an example of a ballad view
30
00:01:17,820 --> 00:01:20,130
function next we're going to try
31
00:01:20,130 --> 00:01:22,320
compiling the function declared as view
32
00:01:22,320 --> 00:01:24,479
but inside the function we're going to
33
00:01:24,479 --> 00:01:27,360
modify the state variable and we're also
34
00:01:27,360 --> 00:01:29,729
going to try to compile function again
35
00:01:29,729 --> 00:01:31,770
declare this view but inside the
36
00:01:31,770 --> 00:01:33,210
function we're going to call another
37
00:01:33,210 --> 00:01:35,700
function which is neither Beauty nor
38
00:01:35,700 --> 00:01:38,759
peer first let's see an example of AB
39
00:01:38,759 --> 00:01:41,009
you function that does keep the promise
40
00:01:41,009 --> 00:01:43,409
not to modify the state here we have a
41
00:01:43,409 --> 00:01:45,930
state variable named X initialized to
42
00:01:45,930 --> 00:01:48,450
the value 1 we also have a function
43
00:01:48,450 --> 00:01:51,810
named add to X add to X takes in an
44
00:01:51,810 --> 00:01:54,479
integer variable named Y as input and
45
00:01:54,479 --> 00:01:57,990
returns a sum of X plus y where X is the
46
00:01:57,990 --> 00:02:00,420
state variable this is a ballot view
47
00:02:00,420 --> 00:02:02,340
function since it reads the state
48
00:02:02,340 --> 00:02:05,820
variable X but does not update it we can
49
00:02:05,820 --> 00:02:07,740
verify that calling this function does
50
00:02:07,740 --> 00:02:10,739
not update the state variable X compile
51
00:02:10,739 --> 00:02:13,940
and deploy the contract
52
00:02:13,940 --> 00:02:16,820
next let's check the value of X it
53
00:02:16,820 --> 00:02:18,740
should be one which is what we
54
00:02:18,740 --> 00:02:20,960
initialized it to we'll call this
55
00:02:20,960 --> 00:02:23,450
function later again and verify that the
56
00:02:23,450 --> 00:02:25,940
state variable was not modified next
57
00:02:25,940 --> 00:02:28,970
call add to X with some input you'll get
58
00:02:28,970 --> 00:02:31,970
the sum of X plus your input at the
59
00:02:31,970 --> 00:02:34,130
bottom of the remix console you'll
60
00:02:34,130 --> 00:02:36,020
notice that calling this function did
61
00:02:36,020 --> 00:02:38,810
not create any transactions as long as
62
00:02:38,810 --> 00:02:40,370
the function does not create any
63
00:02:40,370 --> 00:02:43,250
transactions there is no way to change
64
00:02:43,250 --> 00:02:45,710
the state of the blockchain so calling
65
00:02:45,710 --> 00:02:48,710
add to X did not modify any state to be
66
00:02:48,710 --> 00:02:51,530
100% sure let's double check on the
67
00:02:51,530 --> 00:02:53,810
state variable X and make sure that it
68
00:02:53,810 --> 00:02:58,070
hasn't changed it's still 1 what would
69
00:02:58,070 --> 00:03:00,230
happen if we declare a function as view
70
00:03:00,230 --> 00:03:02,780
but inside the function modify the state
71
00:03:02,780 --> 00:03:05,630
variable let's give it a try create a
72
00:03:05,630 --> 00:03:08,360
function called update X and declare as
73
00:03:08,360 --> 00:03:11,120
view we're going to violate the promise
74
00:03:11,120 --> 00:03:13,640
not to modify the state inside the
75
00:03:13,640 --> 00:03:16,250
function update the state variable X by
76
00:03:16,250 --> 00:03:19,970
incrementing it and hit compile you'll
77
00:03:19,970 --> 00:03:22,520
see a compilation error like this this
78
00:03:22,520 --> 00:03:24,320
is a really good feature of solidity
79
00:03:24,320 --> 00:03:26,660
that it can detect potential state
80
00:03:26,660 --> 00:03:29,660
changes a function cannot be AB u
81
00:03:29,660 --> 00:03:31,490
function if it calls another function
82
00:03:31,490 --> 00:03:34,730
which is neither bill nor peer here we
83
00:03:34,730 --> 00:03:37,250
have a function called foo and it is not
84
00:03:37,250 --> 00:03:39,530
declared as view we also have a function
85
00:03:39,530 --> 00:03:41,780
called invalid view func which is
86
00:03:41,780 --> 00:03:44,300
declared as view inside the function
87
00:03:44,300 --> 00:03:46,760
invalid be func it's going to call
88
00:03:46,760 --> 00:03:49,010
another function foo which is neither
89
00:03:49,010 --> 00:03:52,010
Beauty nor peer if you try to compile
90
00:03:52,010 --> 00:03:53,810
this code you'll get a error like this
91
00:03:53,810 --> 00:03:56,150
stating that invalid view func
92
00:03:56,150 --> 00:03:58,459
contains code that potentially modifies
93
00:03:58,459 --> 00:04:00,790
the state
94
00:04:00,790 --> 00:04:03,610
view functions declare not to modify the
95
00:04:03,610 --> 00:04:05,890
state peer functions make a stronger
96
00:04:05,890 --> 00:04:08,349
promise peer functions promise not to
97
00:04:08,349 --> 00:04:11,530
modify state like B functions in
98
00:04:11,530 --> 00:04:13,870
addition they also promise not to read
99
00:04:13,870 --> 00:04:15,930
the state according to the solidity
100
00:04:15,930 --> 00:04:18,040
documentation the following are
101
00:04:18,040 --> 00:04:19,720
considered reading from the state
102
00:04:19,720 --> 00:04:22,570
reading from state variables accessing
103
00:04:22,570 --> 00:04:25,030
the ether balance urban address reading
104
00:04:25,030 --> 00:04:28,000
from special variables like blog TX and
105
00:04:28,000 --> 00:04:30,760
message calling any function not marked
106
00:04:30,760 --> 00:04:33,280
peer and using inline assembly
107
00:04:33,280 --> 00:04:36,160
containing certain op codes so a peer
108
00:04:36,160 --> 00:04:38,560
function cannot contain any code that
109
00:04:38,560 --> 00:04:40,780
does any of the actions mentioned here
110
00:04:40,780 --> 00:04:44,260
and it can also modify the state like a
111
00:04:44,260 --> 00:04:47,139
b function let's see some examples of
112
00:04:47,139 --> 00:04:50,139
peer function in remix first we'll go
113
00:04:50,139 --> 00:04:52,240
over an example of a ballot peer
114
00:04:52,240 --> 00:04:55,210
function next we'll see some examples of
115
00:04:55,210 --> 00:04:58,300
invalid peer functions here is an
116
00:04:58,300 --> 00:05:00,910
example of a ballot peer function here
117
00:05:00,910 --> 00:05:03,430
we have a function named ad ad is a
118
00:05:03,430 --> 00:05:06,070
function that takes two inputs I and J
119
00:05:06,070 --> 00:05:09,310
and returns I plus J this is a ballot
120
00:05:09,310 --> 00:05:11,410
peer function since it doesn't read any
121
00:05:11,410 --> 00:05:14,650
state and modify any state next let's
122
00:05:14,650 --> 00:05:16,599
see some examples of invalid peer
123
00:05:16,599 --> 00:05:19,539
functions revisit the add two function
124
00:05:19,539 --> 00:05:22,150
back at the top of the contract this is
125
00:05:22,150 --> 00:05:25,030
a ballad view function however since it
126
00:05:25,030 --> 00:05:27,190
reads from the state variable this
127
00:05:27,190 --> 00:05:30,010
cannot be a peer function let's declare
128
00:05:30,010 --> 00:05:33,700
as peer anyways and hit compile you'll
129
00:05:33,700 --> 00:05:35,830
see a similar error to the ones we got
130
00:05:35,830 --> 00:05:38,110
when we try to compile invalid view
131
00:05:38,110 --> 00:05:39,740
function
132
00:05:39,740 --> 00:05:42,470
similar to how AB you function can only
133
00:05:42,470 --> 00:05:45,530
call other view or peer functions appear
134
00:05:45,530 --> 00:05:47,750
functions can only call other peer
135
00:05:47,750 --> 00:05:50,900
function so a function declared as peer
136
00:05:50,900 --> 00:05:53,840
but if it calls a non peer fortune then
137
00:05:53,840 --> 00:05:56,330
it is invalid code let's see an example
138
00:05:56,330 --> 00:05:59,360
here we have a function foo which is
139
00:05:59,360 --> 00:06:02,300
neither Beauty nor peer and we also have
140
00:06:02,300 --> 00:06:05,150
a function invalid peer func declared as
141
00:06:05,150 --> 00:06:08,030
peer inside this function foo is called
142
00:06:08,030 --> 00:06:10,820
since foo is neither Beauty nor peer
143
00:06:10,820 --> 00:06:13,730
this contract will not compile the code
144
00:06:13,730 --> 00:06:16,160
becomes valid when you declare foo as a
145
00:06:16,160 --> 00:06:19,010
peer function so we've just seen that a
146
00:06:19,010 --> 00:06:21,110
peer function can only call other peer
147
00:06:21,110 --> 00:06:24,740
functions it's easy to forget to declare
148
00:06:24,740 --> 00:06:27,680
a function as B or peer the good news is
149
00:06:27,680 --> 00:06:29,870
that the solidity compiler is smart
150
00:06:29,870 --> 00:06:32,120
enough to warn you that you might have
151
00:06:32,120 --> 00:06:34,010
forgotten to put these keywords on your
152
00:06:34,010 --> 00:06:37,070
functions when you compile your contract
153
00:06:37,070 --> 00:06:39,740
and remix you'll get warning stating
154
00:06:39,740 --> 00:06:42,410
that functions can be declared as view
155
00:06:42,410 --> 00:06:45,110
or peer so don't forget to carefully
156
00:06:45,110 --> 00:06:47,210
check the warnings and fix your code if
157
00:06:47,210 --> 00:06:49,600
necessary
158
00:06:49,600 --> 00:06:51,940
in this video we cover beer and peer
159
00:06:51,940 --> 00:06:54,460
functions beautiful chains promise not
160
00:06:54,460 --> 00:06:57,010
to modify the state peer functions make
161
00:06:57,010 --> 00:06:59,020
a stronger promised and view functions
162
00:06:59,020 --> 00:07:01,450
they promise neither to modify the state
163
00:07:01,450 --> 00:07:04,450
nor read the state thank you so much for
164
00:07:04,450 --> 00:07:08,310
watching and I'll see you next time