-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsearch.xml
More file actions
1460 lines (1371 loc) · 202 KB
/
search.xml
File metadata and controls
1460 lines (1371 loc) · 202 KB
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
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>React Testing</title>
<url>/2021/01/13/React-Testing/</url>
<content><![CDATA[<h2 id="描述"><a href="#描述" class="headerlink" title="描述"></a>描述</h2><h4 id="Tools"><a href="#Tools" class="headerlink" title="Tools"></a>Tools</h4><ul>
<li><a href="https://facebook.github.io/jest/" target="_blank" rel="noopener">Jest</a></li>
<li><a href="https://testing-library.com/react" target="_blank" rel="noopener">React Testing Library</a></li>
</ul>
<h4 id="总步骤"><a href="#总步骤" class="headerlink" title="总步骤"></a>总步骤</h4><ol>
<li>通过<code>render</code>等渲染函数,渲染一个组件</li>
<li><code>Queries</code>等查询、获取组件</li>
<li><code>Expect</code>断言渲染结果、状态值</li>
</ol>
<a id="more"></a>
<h4 id="一些行为"><a href="#一些行为" class="headerlink" title="一些行为"></a>一些行为</h4><ul>
<li>对模块、函数、时间进行Mock</li>
<li><code>beforeEach</code>等函数对环境进行设置、清除</li>
<li><code>act</code>等函数等待渲染完成,以便对确定完成的结果进行断言</li>
<li><code>fireEvent</code>发送时间,如点击事件</li>
</ul>
<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><ol>
<li><a href="https://reactjs.org/docs/testing-recipes.html" target="_blank" rel="noopener">https://reactjs.org/docs/testing-recipes.html</a></li>
<li><a href="https://testing-library.com/" target="_blank" rel="noopener">https://testing-library.com/</a></li>
</ol>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
<tags>
<tag>React</tag>
</tags>
</entry>
<entry>
<title>Hooks Redux</title>
<url>/2021/01/13/Hooks-Redux/</url>
<content><![CDATA[<h2 id="Top-Level-File"><a href="#Top-Level-File" class="headerlink" title="Top Level File"></a>Top Level File</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">import { Provider } from 'react-redux';</span><br><span class="line">import store from '<path of store file>';</span><br><span class="line"></span><br><span class="line">export type RootState = ReturnType<typeof store.getState>;</span><br><span class="line"></span><br><span class="line"><Provider store={store}></span><br><span class="line"> <App /></span><br><span class="line"></Provider></span><br></pre></td></tr></table></figure>
<a id="more"></a>
<h2 id="Store-File"><a href="#Store-File" class="headerlink" title="Store File"></a>Store File</h2><figure class="highlight ts"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> reduxSliceOne <span class="keyword">from</span> <span class="string">'<redux slice file one>'</span>;</span><br><span class="line"><span class="keyword">import</span> reduxSliceTwo <span class="keyword">from</span> <span class="string">'<redux slice file two>'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> configureStore({</span><br><span class="line"> reducer: {</span><br><span class="line"> reduxOne: reduxSliceOne,</span><br><span class="line"> reduxTwo: reduxSliceTwo,</span><br><span class="line"> },</span><br><span class="line">});</span><br></pre></td></tr></table></figure>
<h2 id="Redux-Slice-File-One"><a href="#Redux-Slice-File-One" class="headerlink" title="Redux Slice File One"></a>Redux Slice File One</h2><figure class="highlight ts"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> { createSlice, PayloadAction, createAsyncThunk } <span class="keyword">from</span> <span class="string">'@reduxjs/toolkit'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> { RootState } <span class="keyword">from</span> <span class="string">'<path of top level file>'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">type</span> DataState = <span class="built_in">string</span>; <span class="comment">// What do u want</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">interface</span> ReduxSliceOneState {</span><br><span class="line"> data: DataState,</span><br><span class="line"> status: <span class="string">'idle'</span> | <span class="string">'loading'</span> | <span class="string">'succeeded'</span> | <span class="string">'failed'</span>,</span><br><span class="line"> error: <span class="built_in">string</span> | <span class="literal">null</span> | <span class="literal">undefined</span>,</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> initialState = {</span><br><span class="line"> data: <span class="string">'Hey Man ~'</span>,</span><br><span class="line"> status: <span class="string">'idle'</span>,</span><br><span class="line"> error: <span class="literal">null</span>,</span><br><span class="line">} <span class="keyword">as</span> ReduxSliceOneState;</span><br><span class="line"></span><br><span class="line"><span class="keyword">interface</span> fetchParams {</span><br><span class="line"> id: <span class="built_in">number</span>,</span><br><span class="line"> age: <span class="built_in">number</span>,</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// Async Update</span></span><br><span class="line"><span class="keyword">const</span> fetchReduxOneData = createAsyncThunk(</span><br><span class="line"> <span class="string">'redux/one'</span>, <span class="comment">// What do u want</span></span><br><span class="line"> <span class="keyword">async</span> (params: fetchParams) => {</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Just for example</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">await</span> fetch(params);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// Or:</span></span><br><span class="line"> <span class="comment">// return new Promise<DataState>((resolve, rejects) => {</span></span><br><span class="line"> <span class="comment">// const client = new Client('<url>');</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// client.on('error', async (err) => {</span></span><br><span class="line"> <span class="comment">// rejects(err);</span></span><br><span class="line"> <span class="comment">// });</span></span><br><span class="line"> <span class="comment">// client.on('close', () => {});</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// client.on("open", async () => {</span></span><br><span class="line"> <span class="comment">// try {</span></span><br><span class="line"> <span class="comment">// const data = await client.fetch(params);</span></span><br><span class="line"> <span class="comment">// resolve(data);</span></span><br><span class="line"> <span class="comment">// } catch (err) {</span></span><br><span class="line"> <span class="comment">// rejects(err)</span></span><br><span class="line"> <span class="comment">// } finally {</span></span><br><span class="line"> <span class="comment">// client.close();</span></span><br><span class="line"> <span class="comment">// }</span></span><br><span class="line"> <span class="comment">// });</span></span><br><span class="line"> <span class="comment">// });</span></span><br><span class="line"> }</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> slice = createSlice({</span><br><span class="line"> name: <span class="string">'reduxOne'</span>,</span><br><span class="line"> initialState: initialState,</span><br><span class="line"> reducers: {</span><br><span class="line"> <span class="comment">// Sync Update</span></span><br><span class="line"> updateData(state, action: PayloadAction<DataState>) {</span><br><span class="line"> state.data = action.payload;</span><br><span class="line"> },</span><br><span class="line"> },</span><br><span class="line"> extraReducers: <span class="function"><span class="params">builder</span> =></span> {</span><br><span class="line"> builder.addCase(fetchReduxOneData.pending, <span class="function">(<span class="params">state</span>) =></span> {</span><br><span class="line"> state.status = <span class="string">'loading'</span>;</span><br><span class="line"> });</span><br><span class="line"> builder.addCase(fetchReduxOneData.rejected, <span class="function">(<span class="params">state, action</span>) =></span> {</span><br><span class="line"> state.status = <span class="string">'failed'</span>;</span><br><span class="line"> state.error = action.error.message || <span class="string">'Something Error ...'</span>;</span><br><span class="line"> });</span><br><span class="line"> builder.addCase(fetchReduxOneData.fulfilled, <span class="function">(<span class="params">state, action</span>) =></span> {</span><br><span class="line"> state.status = <span class="string">'succeeded'</span>;</span><br><span class="line"> state.data = action.payload;</span><br><span class="line"> });</span><br><span class="line"> },</span><br><span class="line">});</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> { fetchReduxOneData };</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> { updateData } = slice.actions;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> selectReduxOneData = <span class="function">(<span class="params">state: RootState</span>) =></span> state.reduxOne; <span class="comment">// Related: store</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> slice.reducer;</span><br></pre></td></tr></table></figure>
<h2 id="Companent"><a href="#Companent" class="headerlink" title="Companent"></a>Companent</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">import { FC, useEffect } from 'react';</span><br><span class="line">import { useSelector, useDispatch } from 'react-redux';</span><br><span class="line">import { fetchReduxOneData, updateData, selectReduxOneData } from '<redux slice file one>'</span><br><span class="line"></span><br><span class="line">const Home: FC = () => {</span><br><span class="line"> const dispatch = useDispatch();</span><br><span class="line"> const reduxOneData = useSelector(selectReduxOneData);</span><br><span class="line"></span><br><span class="line"> useEffect(() => {</span><br><span class="line"> if (reduxOneData.status==='failed') {</span><br><span class="line"> alert(reduxOneData.error);</span><br><span class="line"> }</span><br><span class="line"> }, [reduxOneData]);</span><br><span class="line"></span><br><span class="line"> const handleClickUpdate = () => {</span><br><span class="line"> dispatch(updateData('Hey Man Too ~'));</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> const handleClickUpdateAsync = () => {</span><br><span class="line"> dispatch(fetchReduxOneData({ id: 1, age: 2 }));</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> return (</span><br><span class="line"> <div></span><br><span class="line"> {reduxOneData.status === 'loading' ? 'loading ...' : reduxOneData.data}</span><br><span class="line"> <button onClick={handleClickUpdate}>Update</button></span><br><span class="line"> <button onClick={handleClickUpdateAsync}>Async Update</button></span><br><span class="line"> </div></span><br><span class="line"> )</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">export default App;</span><br></pre></td></tr></table></figure>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
<tags>
<tag>Redux</tag>
</tags>
</entry>
<entry>
<title>jsx react-jsx error</title>
<url>/2020/12/23/jsx-react-jsx-error/</url>
<content><![CDATA[<h1 id="背景"><a href="#背景" class="headerlink" title="背景"></a>背景</h1><p>通过<code>yarn create react-app antd-demo-ts --template typescript</code>创建项目,使用VSCode打开项目提示错误:</p>
<a id="more"></a>
<p><img src="/imgss/react-jsx-error-01.png" alt="png"><br><img src="/imgss/react-jsx-error-02.png" alt="png"></p>
<h1 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h1><p>VSCode使用的Typescript版本问题</p>
<h1 id="解决"><a href="#解决" class="headerlink" title="解决"></a>解决</h1><p>VSCode选择使用工作区的版本</p>
<p><img src="/imgss/react-jsx-error-03.png" alt="png"><br><img src="/imgss/react-jsx-error-04.png" alt="png"><br><img src="/imgss/react-jsx-error-05.png" alt="png"><br><img src="/imgss/react-jsx-error-06.png" alt="png"></p>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
</entry>
<entry>
<title>react+typescript+jest踩坑</title>
<url>/2020/04/11/react-typescript-jest%E8%B8%A9%E5%9D%91/</url>
<content><![CDATA[<h2 id="使用Typescript和一些配置"><a href="#使用Typescript和一些配置" class="headerlink" title="使用Typescript和一些配置"></a>使用Typescript和一些配置</h2><h4 id="package-json"><a href="#package-json" class="headerlink" title="package.json"></a>package.json</h4><p><code>babelConfig</code> 用于告诉 ts-jest 是否使用 babel 。false不使用,true会查找 babel 配置文件,如 babel.config.json ,也可以配置某个具体的值。</p>
<p><code>identity-obj-proxy</code>用于jest mock css module</p>
<p>tsx 使用 babel-jest transform 遇到无法解析,后来改为使用 ts-jest 能解决问题</p>
<a id="more"></a>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line">{</span><br><span class="line"> <span class="attr">"scripts"</span>: {</span><br><span class="line"> <span class="attr">"dev"</span>: <span class="string">"webpack-dev-server --open"</span>,</span><br><span class="line"> <span class="attr">"test"</span>: <span class="string">"jest --no-cache"</span></span><br><span class="line"> },</span><br><span class="line"> <span class="attr">"jest"</span>: {</span><br><span class="line"> <span class="attr">"globals"</span>: {</span><br><span class="line"> <span class="attr">"ts-jest"</span>: {</span><br><span class="line"> <span class="attr">"babelConfig"</span>: <span class="literal">true</span></span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">"transform"</span>: {</span><br><span class="line"> <span class="attr">"^.+\\.jsx?$"</span>: <span class="string">"babel-jest"</span>,</span><br><span class="line"> <span class="attr">"^.+\\.tsx?$"</span>: <span class="string">"ts-jest"</span></span><br><span class="line"> },</span><br><span class="line"> <span class="attr">"transformIgnorePatterns"</span>: [</span><br><span class="line"> <span class="string">"<rootDir>/node_modules/?!(react-checkbox-group)"</span></span><br><span class="line"> ],</span><br><span class="line"> <span class="attr">"moduleNameMapper"</span>: {</span><br><span class="line"> <span class="attr">"\\.(css|less)$"</span>: <span class="string">"identity-obj-proxy"</span></span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> <span class="attr">"dependencies"</span>: {</span><br><span class="line"> <span class="attr">"jest"</span>: <span class="string">"25.1.0"</span>,</span><br><span class="line"> <span class="attr">"react"</span>: <span class="string">"16.9.0"</span>,</span><br><span class="line"> <span class="attr">"react-checkbox-group"</span>: <span class="string">"^5.0.2"</span>,</span><br><span class="line"> <span class="attr">"react-dom"</span>: <span class="string">"16.9.0"</span></span><br><span class="line"> },</span><br><span class="line"> <span class="attr">"devDependencies"</span>: {</span><br><span class="line"> <span class="attr">"@babel/core"</span>: <span class="string">"^7.9.0"</span>,</span><br><span class="line"> <span class="attr">"@babel/preset-env"</span>: <span class="string">"^7.9.5"</span>,</span><br><span class="line"> <span class="attr">"@types/jest"</span>: <span class="string">"25.1.1"</span>,</span><br><span class="line"> <span class="attr">"@types/node"</span>: <span class="string">"12.7.2"</span>,</span><br><span class="line"> <span class="attr">"@types/pretty"</span>: <span class="string">"^2.0.0"</span>,</span><br><span class="line"> <span class="attr">"@types/react"</span>: <span class="string">"16.9.2"</span>,</span><br><span class="line"> <span class="attr">"@types/react-dom"</span>: <span class="string">"16.9.0"</span>,</span><br><span class="line"> <span class="attr">"@types/react-test-renderer"</span>: <span class="string">"^16.9.2"</span>,</span><br><span class="line"> <span class="attr">"babel-jest"</span>: <span class="string">"^25.3.0"</span>,</span><br><span class="line"> <span class="attr">"css-loader"</span>: <span class="string">"3.2.0"</span>,</span><br><span class="line"> <span class="attr">"html-webpack-plugin"</span>: <span class="string">"3.2.0"</span>,</span><br><span class="line"> <span class="attr">"identity-obj-proxy"</span>: <span class="string">"^3.0.0"</span>,</span><br><span class="line"> <span class="attr">"react-test-renderer"</span>: <span class="string">"^16.13.1"</span>,</span><br><span class="line"> <span class="attr">"source-map-loader"</span>: <span class="string">"0.2.4"</span>,</span><br><span class="line"> <span class="attr">"style-loader"</span>: <span class="string">"1.0.0"</span>,</span><br><span class="line"> <span class="attr">"ts-jest"</span>: <span class="string">"^25.3.1"</span>,</span><br><span class="line"> <span class="attr">"ts-loader"</span>: <span class="string">"6.0.4"</span>,</span><br><span class="line"> <span class="attr">"typescript"</span>: <span class="string">"3.6.2"</span>,</span><br><span class="line"> <span class="attr">"webpack"</span>: <span class="string">"4.39.3"</span>,</span><br><span class="line"> <span class="attr">"webpack-cli"</span>: <span class="string">"3.3.7"</span>,</span><br><span class="line"> <span class="attr">"webpack-dev-server"</span>: <span class="string">"3.8.0"</span></span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="Jest-Enable-ES6"><a href="#Jest-Enable-ES6" class="headerlink" title="Jest Enable ES6"></a>Jest Enable ES6</h2><p>Jest是Node环境,不能识别<code>import/export</code>等ES6语法,可以通过安装Babel解决</p>
<ul>
<li>在项目根目录下建立<code>babel.config.json</code>文件,<strong>特别地,不要用<code>.babelrc</code></strong></li>
<li>填入:</li>
</ul>
<figure class="highlight json"><table><tr><td class="code"><pre><span class="line">{</span><br><span class="line"> <span class="attr">"presets"</span>: [</span><br><span class="line"> [<span class="string">"@babel/preset-env"</span>, {<span class="attr">"targets"</span>: {<span class="attr">"node"</span>: <span class="string">"current"</span>}}]</span><br><span class="line"> ]</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li>不要忘记:</li>
</ul>
<figure class="highlight shell"><table><tr><td class="code"><pre><span class="line">npm install @babel/preset-env --save-dev</span><br></pre></td></tr></table></figure>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
</entry>
<entry>
<title>《滚雪球--巴菲特和他的财富人生》</title>
<url>/2020/02/15/%E3%80%8A%E6%BB%9A%E9%9B%AA%E7%90%83-%E5%B7%B4%E8%8F%B2%E7%89%B9%E5%92%8C%E4%BB%96%E7%9A%84%E8%B4%A2%E5%AF%8C%E4%BA%BA%E7%94%9F%E3%80%8B/</url>
<content><![CDATA[<h2 id="巴拉巴拉"><a href="#巴拉巴拉" class="headerlink" title="巴拉巴拉"></a>巴拉巴拉</h2><p>这是一本巴菲特的传记,但不是那种作者连李嘉诚都没见过就写出的李嘉诚是怎么白手起家的传记。这本书很多是由巴菲特亲自口述,以及作者亲身观察巴菲特的工作,生活,他的朋友等,最后整理出来的。</p>
<a id="more"></a>
<p>书中描述了巴菲特的家族情况,以时间线的形式,从巴菲特出生,童年,到后来的股神。</p>
<p>没读这本书之前,以为巴菲特家族是多么显赫,家庭是多么幸福,股神是多么的牛逼。但是读完之后就会发现,股神也是凡人。他的家族的人不是皇侯将相,他父亲做的小小的官也不如意,他怕他母亲和她聊不来。而巴菲特自己本身,小时候也不是特别自信不敢和女孩子说话,甚至有点内向家里热闹的时候就一个人躲到家里工作的地方一个人工作。唯一会让他滔滔不绝是讨论股票的时候。甚至除了投资,他的生活也需要别人照顾。</p>
<p>巴菲特非常“吝啬”,很在意钱,也没打算会给多少钱他的子女,所以他的子女不是像富二代那样每天体验生活。这也许和他觉得,今天的一美元一年后可以复利到十美元,所以才会十分吝啬钱。他没有把很多钱留给子女,而是把绝大部分捐献到盖茨夫妇的基金。</p>
<p>通过这本书看到了一个凡人版的股神,就和很多追星的一样,明星也是人,也是要吃喝拉撒的。</p>
<h2 id="封面"><a href="#封面" class="headerlink" title="封面"></a>封面</h2><p><img src="/imgss/gun_xue_qiu.jpg" alt="封面"></p>
]]></content>
<categories>
<category>电影阅读</category>
</categories>
<tags>
<tag>读书</tag>
</tags>
</entry>
<entry>
<title>为什么使用reflect</title>
<url>/2020/01/09/%E4%B8%BA%E4%BB%80%E4%B9%88%E4%BD%BF%E7%94%A8reflect/</url>
<content><![CDATA[<h2 id="前情"><a href="#前情" class="headerlink" title="前情"></a>前情</h2><p>Golang是一门静态语言,也就决定了一般情况下不能在程序运行中改变其行为。好在Golang也是一门面向反射的语言,通过反射机制,可以弥补这部分不足。</p>
<a id="more"></a>
<h2 id="反射"><a href="#反射" class="headerlink" title="反射"></a>反射</h2><p>借用维基百科的一段话:</p>
<blockquote>
<p>在计算机学中,反射(英语:reflection)是指计算机程序在运行时(runtime)可以访问、检测和修改它本身状态或行为的一种能力。用比喻来说,反射就是程序在运行的时候能够“观察”并且修改自己的行为。</p>
</blockquote>
<h2 id="Go中的反射"><a href="#Go中的反射" class="headerlink" title="Go中的反射"></a>Go中的反射</h2><p><code>reflect</code>是Go的反射包。一个对象包含的最主要信息就是名称和值(key, value),知道了名称和值,那么等于知道了对象的所有。</p>
<p><code>reflect</code>中有两个重要的对象:</p>
<ul>
<li>type Value struct</li>
<li>type Type interface</li>
</ul>
<p>我们平时用到的<code>reflect</code>基本上都是围绕<code>Type</code>和<code>Value</code>。<code>reflect</code>提供可以获得这两个对象的方法:</p>
<ul>
<li>func TypeOf(i interface{}) Type</li>
<li>func ValueOf(i interface{}) Value</li>
</ul>
<p>得到<code>Value</code>和<code>Type</code>实例后,<code>Value</code>和<code>Type</code>提供很多方法供我们操作。</p>
<p>举例来说,我们可以通过<code>reflect</code>获取运行中某个变量是struct、interface还是func等类型,也可以获取变量的字段和值。</p>
<p>总之,通过<code>reflect</code>我们可以获取一个对象的一切,如果是struct,我们可以知道它的所有字段和值;如果是func,我们可以知道参数列表,然后构造一个函数,然后运行。</p>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
<tags>
<tag>Golang</tag>
</tags>
</entry>
<entry>
<title>《正见》</title>
<url>/2020/01/08/%E3%80%8A%E6%AD%A3%E8%A7%81%E3%80%8B/</url>
<content><![CDATA[<h2 id="说些什么"><a href="#说些什么" class="headerlink" title="说些什么"></a>说些什么</h2><p>大佬分享的一本书,也许大概可能是自己境界不够,看得有点绕,看完有点云里雾里。</p>
<a id="more"></a>
<h2 id="封面"><a href="#封面" class="headerlink" title="封面"></a>封面</h2><p><img src="/imgss/fengmian_zhengjian.jpg" alt="封面"></p>
]]></content>
<categories>
<category>电影阅读</category>
</categories>
<tags>
<tag>读书</tag>
</tags>
</entry>
<entry>
<title>为什么要用Context(上下文)</title>
<url>/2020/01/01/Golang-Context/</url>
<content><![CDATA[<h2 id="写在前面"><a href="#写在前面" class="headerlink" title="写在前面"></a>写在前面</h2><p>相比在参数中传递用于控制流程的自定义管道变量,<code>Context</code>可以更方便地串联、管理多个<code>Goroutine</code>,在大型项目中发挥着重要的作用。</p>
<a id="more"></a>
<h2 id="New-Context"><a href="#New-Context" class="headerlink" title="New Context"></a>New Context</h2><h4 id="WithCancel"><a href="#WithCancel" class="headerlink" title="WithCancel"></a>WithCancel</h4><figure class="highlight go"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">WithCancel</span><span class="params">(parent Context)</span> <span class="params">(ctx Context, cancel CancelFunc)</span></span></span><br></pre></td></tr></table></figure>
<p>返回一个携带一个新的<code>Done</code>管道的<code>parent</code>副本,<code>parent</code>的<code>cancel</code>被执行或<code>parent</code>的<code>Done</code>被<code>close</code>的时候,副本的<code>Done</code>会被<code>close</code>。</p>
<figure class="highlight go"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line"> stoping := <span class="built_in">make</span>(<span class="keyword">chan</span> <span class="keyword">bool</span>)</span><br><span class="line"> ctx, cancel := context.WithCancel(context.Background())</span><br><span class="line"></span><br><span class="line"> <span class="keyword">go</span> check(ctx, stoping)</span><br><span class="line"> time.Sleep(<span class="number">3</span> * time.Second)</span><br><span class="line"></span><br><span class="line"> fmt.Println(<span class="string">"after 3s"</span>)</span><br><span class="line"> cancel()</span><br><span class="line"></span><br><span class="line"> <-stoping</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">check</span><span class="params">(ctx context.Context, stoping <span class="keyword">chan</span> <span class="keyword">bool</span>)</span></span> {</span><br><span class="line"> fmt.Println(<span class="string">"[check] start..."</span>)</span><br><span class="line"> <-ctx.Done()</span><br><span class="line"> fmt.Println(<span class="string">"[check] end..."</span>)</span><br><span class="line"> stoping <- <span class="literal">true</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>输出:</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">[check] start...</span><br><span class="line">after 3s</span><br><span class="line">[check] end...</span><br></pre></td></tr></table></figure>
<h4 id="WithDeadline"><a href="#WithDeadline" class="headerlink" title="WithDeadline"></a>WithDeadline</h4><figure class="highlight go"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">WithDeadline</span><span class="params">(parent Context, deadline time.Time)</span> <span class="params">(Context, CancelFunc)</span></span></span><br></pre></td></tr></table></figure>
<p>副本<code>deadline</code>或<code>cancel</code>的时候,或<code>parent</code>的<code>Done</code>被<code>close</code>的时候,副本的<code>Done</code>将<code>close</code></p>
<h4 id="WithTimeout"><a href="#WithTimeout" class="headerlink" title="WithTimeout"></a>WithTimeout</h4><figure class="highlight go"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">WithTimeout</span><span class="params">(parent Context, timeout time.Duration)</span> <span class="params">(Context, CancelFunc)</span></span></span><br></pre></td></tr></table></figure>
<p>返回<code>WithDeadline(parent, time.Now().Add(timeout))</code>。</p>
<figure class="highlight go"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line"> ctx, _ := context.WithTimeout(context.Background(), <span class="number">2</span>*time.Second)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">go</span> check(ctx, <span class="number">1</span>*time.Second)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">select</span> {</span><br><span class="line"> <span class="keyword">case</span> <-ctx.Done():</span><br><span class="line"> fmt.Println(<span class="string">"[main] "</span>, ctx.Err())</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">check</span><span class="params">(ctx context.Context, timeout time.Duration)</span></span> {</span><br><span class="line"> <span class="keyword">select</span> {</span><br><span class="line"> <span class="keyword">case</span> <-ctx.Done():</span><br><span class="line"> fmt.Println(<span class="string">"[check] "</span>, ctx.Err())</span><br><span class="line"> <span class="keyword">case</span> <-time.After(timeout):</span><br><span class="line"> fmt.Println(<span class="string">"[check] timeout"</span>)</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>输出:</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">[check] timeout</span><br><span class="line">[main] context deadline exceeded</span><br></pre></td></tr></table></figure>
<h4 id="WithValue"><a href="#WithValue" class="headerlink" title="WithValue"></a>WithValue</h4><figure class="highlight go"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">WithValue</span><span class="params">(parent Context, key <span class="keyword">interface</span>{}, val <span class="keyword">interface</span>{})</span> <span class="title">Context</span></span></span><br></pre></td></tr></table></figure>
<figure class="highlight go"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> {</span><br><span class="line"> stoping := <span class="built_in">make</span>(<span class="keyword">chan</span> <span class="keyword">bool</span>)</span><br><span class="line"> ctx := context.WithValue(context.Background(), <span class="string">"name"</span>, <span class="string">"Jack"</span>)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">go</span> check(ctx, stoping)</span><br><span class="line"> <-stoping</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">check</span><span class="params">(ctx context.Context, stoping <span class="keyword">chan</span> <span class="keyword">bool</span>)</span></span> {</span><br><span class="line"> fmt.Println(<span class="string">"[check] Hi"</span>, ctx.Value(<span class="string">"name"</span>))</span><br><span class="line"> stoping <- <span class="literal">true</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>输出:</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">[check] Hi Jack</span><br></pre></td></tr></table></figure>
<h4 id="Background"><a href="#Background" class="headerlink" title="Background"></a>Background</h4><p><code>Background returns a non-nil, empty Context. It is never canceled, has no values, and has no deadline</code>。这个一般在<code>main</code>函数、初始化、测试和顶层使用,然后通过它往后派生。</p>
<h4 id="TODO"><a href="#TODO" class="headerlink" title="TODO"></a>TODO</h4><p>几乎是<code>Background</code>的别名般的存在,但相比<code>Background</code>,一些静态分析工具可以通过<code>TODO</code>分析<code>Context</code>有没有正确地传递。一般在对<code>Context</code>的使用还不清晰的地方使用。</p>
<h2 id="Note"><a href="#Note" class="headerlink" title="Note"></a>Note</h2><ul>
<li><code>Context</code>被<code>cancel</code>的时候,关联的资源也会被释放</li>
<li>不要在<code>struct</code>中存储<code>Context</code>实例,明确地通过函数参数传递,并且一般作为第一个参数</li>
<li>即使函数运行,也不要传递<code>nil Context</code>,如果不确定传递的<code>Context</code>将来有什么用处,可以传递一个<code>context.TODO</code></li>
<li>通过<code>Context</code>携带参数一般只用于请求域数据,可选参数应该明确通过函数参数传递</li>
<li>同一个<code>Context</code>可运行于不同<code>Goroutines</code>中的函数,<code>Context</code>可以在多个<code>Goroutines</code>间同步</li>
</ul>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
<tags>
<tag>Golang</tag>
</tags>
</entry>
<entry>
<title>C++碎碎念</title>
<url>/2019/11/25/C-%E7%A2%8E%E7%A2%8E%E5%BF%B5/</url>
<content><![CDATA[<h3 id="头文件中ifndef-define-endif的作用"><a href="#头文件中ifndef-define-endif的作用" class="headerlink" title="头文件中ifndef/define/endif的作用"></a>头文件中<code>ifndef/define/endif</code>的作用</h3><p>保证即使头文件被包含多次,也只定义一次,起到预编译保护作用。</p>
<a id="more"></a>
<h3 id="extern-“C”"><a href="#extern-“C”" class="headerlink" title="extern “C”"></a><code>extern “C”</code></h3><p>程序中的变量或函数,经<code>C</code>和<code>C++</code>编译器编译后后,符号不一样,会导致编译后的<code>C</code>代码在符号库中找不到。<code>extern “C”</code>告诉编译器这是<code>C</code>代码,按照<code>C</code>的方式编译。<br>所以,<code>extern “C”</code>的目的是解决符号匹配问题,实现<code>C</code>和<code>C++</code>混合编程。</p>
<h3 id="const和-define"><a href="#const和-define" class="headerlink" title="const和#define"></a><code>const</code>和<code>#define</code></h3><ul>
<li>都可以定义常量,<code>const</code>用途更广,比如修饰函数返回值和参数</li>
<li><code>const</code>有数据类型,编译器可以对其进行类型安全检查,对<code>#define</code>只会进行字符替换</li>
</ul>
<h3 id="sizeof与strlen"><a href="#sizeof与strlen" class="headerlink" title="sizeof与strlen"></a><code>sizeof</code>与<code>strlen</code></h3><ul>
<li><code>sizeof</code>是运算符,<code>strlen</code>是函数</li>
<li><code>sizeof</code>可以用类型做参数,<code>strlen</code>只能用<code>\0</code>结尾的<code>char *</code></li>
</ul>
<h3 id="指针和引用"><a href="#指针和引用" class="headerlink" title="指针和引用"></a>指针和引用</h3><ul>
<li>定义指针时可以不初始化;定义引用时必须初始化</li>
<li>指针赋值是把指针指向另一个对象;引用赋值是修改引用绑定的对象的值<br>在底层,引用变量由指针按照指针常量的方式实现,所以<code>int i = 2;int* const gi = &i;</code>和<code>int i = 2;int &gi = i;</code>是一致的。</li>
</ul>
<h3 id="空指针、野指针和悬垂指针"><a href="#空指针、野指针和悬垂指针" class="headerlink" title="空指针、野指针和悬垂指针"></a>空指针、野指针和悬垂指针</h3><ul>
<li>空指针:指向地址为空的指针(NULL指针)。可以被多次<code>delete</code></li>
<li>野指针:指向垃圾内存的指针。产生原因是创建时未初始化,它将会随意指向一个内存地址</li>
<li>悬垂指针:动态创建的对象被释放或回收了,但是指向该对象的指针未做任何修改,仍旧指向已经回收的内存地址</li>
</ul>
<h3 id="malloc-free和new-delete"><a href="#malloc-free和new-delete" class="headerlink" title="malloc/free和new/delete"></a><code>malloc/free</code>和<code>new/delete</code></h3><ul>
<li><code>malloc/free</code>是<code>C/C++</code>标准库函数,<code>new/delete</code>是<code>C++</code>运算符</li>
<li>申请的是堆里面的内存空间</li>
<li><code>malloc</code>不会做初始化,<code>new</code>有默认的初始化同时可以指定初始化。对于类类型而言,对象在创建的时候要自动执行构造函数,消亡之前要调用析构函数,<code>malloc/free</code>不能满足要求。<code>malloc/free</code>是库函数而不是运算符,不在编译器控制之内,不能把执行构造函数和析构函数的任务强加给它,因此,<code>C++</code>还需要<code>malloc/free</code></li>
</ul>
<h3 id="pragma-once和-ifndef-define-endif"><a href="#pragma-once和-ifndef-define-endif" class="headerlink" title="#pragma once和#ifndef/#define/#endif"></a><code>#pragma once</code>和<code>#ifndef/#define/#endif</code></h3><ul>
<li><code>#pragma once</code>是编译相关,某些编译器可能不能用,移植性差一些;<code>#ifndef/#define/#endif</code>是语言相关</li>
<li><code>#ifndef/#define/#endif</code>依赖于宏名字不能冲突,这不光可以保证同一个文件不会被包含多次,也能保证内容完全相同的两个文件不会被不小心同时包含;缺点是如果不同头文件的宏名不小心“撞车”,可能会导致头文件明明存在,编译器却说找不到声明的状况</li>
<li><code>#pragma once</code>由编译器提供保证同一个文件不会被包含多次。“同一个文件”是指物理上的一个文件,而不是指内容相同的两个文件,所以对应的缺点就是如果某个头文件有多份拷贝,将不能保证他们不被重复包含。当然,相比宏名碰撞引发的“找不到声明”的问题,重复包含更容易被发现并修正</li>
</ul>
<h3 id="NULL和nullptr"><a href="#NULL和nullptr" class="headerlink" title="NULL和nullptr"></a><code>NULL</code>和<code>nullptr</code></h3><p>在<code>C</code>中,<code>NULL</code>通常定义为<code>#define NULL ((void *)0)</code>,<code>void *</code>可以隐式转换成其他类型<br>而<code>C++</code>是强类型的,<code>void *</code>不能隐式转换成其他指针类型,所以通常情况下编译器在头文件中会这样定义<code>NULL</code>:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#ifdef __cplusplus</span><br><span class="line">#define NULL 0</span><br><span class="line">#else</span><br><span class="line">#define NULL ((void *)0)</span><br><span class="line">#endif</span><br></pre></td></tr></table></figure>
<p>所以<code>C++</code>中用<code>0</code>表示空指针。不过还是有缺陷不完美,处理重载函数的时候会出现问题。于是<code>C++11</code>引入了<code>nullptr</code>表示空指针</p>
<h3 id="智能指针"><a href="#智能指针" class="headerlink" title="智能指针"></a>智能指针</h3><p>最主要是为了解决内存释放问题。对于一个大型项目,我们在这里申请了一块内存,而在离这里很远的各个地方需要我们释放这块内存的时候,我们常常忘记这个操作,而且有时发现问题还很难排查出来。智能指针的目的就是让程序自动销毁这块内存,而不是靠程序员手动销毁。(未完待续)</p>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
</entry>
<entry>
<title>《简单实现自己的监听器模式》</title>
<url>/2019/11/25/%E3%80%8A%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%B7%B1%E7%9A%84%E7%9B%91%E5%90%AC%E5%99%A8%E6%A8%A1%E5%BC%8F%E3%80%8B/</url>
<content><![CDATA[<p>实现监听器模式,不使用<code>EventEmitter</code></p>
<a id="more"></a>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">class MyEventEmitter {</span><br><span class="line"> constructor() {</span><br><span class="line"> this.handles = Object.create(null);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> on(evt, cb) {</span><br><span class="line"> if (typeof cb !== 'function') {</span><br><span class="line"> console.log('cb must be a function');</span><br><span class="line"> return;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> if (!this.handles[evt]) {</span><br><span class="line"> this.handles[evt] = [];</span><br><span class="line"> }</span><br><span class="line"> this.handles[evt].push(cb);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> emit(evt, ...args) {</span><br><span class="line"> if (!!this.handles[evt]) {</span><br><span class="line"> this.handles[evt].forEach((item) => {</span><br><span class="line"> Reflect.apply(item, this, args);</span><br><span class="line"> });</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">const emitter = new MyEventEmitter();</span><br><span class="line">emitter.on('say', (name) => {</span><br><span class="line"> console.log('Hello, ', name);</span><br><span class="line">});</span><br><span class="line"></span><br><span class="line">setTimeout(() => {</span><br><span class="line"> emitter.emit('say', 'Jay');</span><br><span class="line">}, 1500);</span><br></pre></td></tr></table></figure>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
</entry>
<entry>
<title>Object.create(null)和{}</title>
<url>/2019/11/25/Object-create-null-%E5%92%8C/</url>
<content><![CDATA[<p><code>let a = {}</code>和<code>let b = Object.create(null)</code>有什么区别呢?</p>
<a id="more"></a>
<p>在浏览器开发者工具查看<code>console.log(a)</code>和<code>console.log(b)</code>可以看到,<code>a</code>继承了<code>Object</code>的一些东西(基于原型链),而<code>b</code>是干净的空的。</p>
<p>所以<code>b</code>的方式不用担心自己定义属性的话会和原型链上的冲突;另外某些循环中,不需要去遍历原型链上的属性,可以提升一丢丢性能。<br>当然,这么做另一方面是丢掉了原型链上提供的所有属性或功能。</p>
<p>参考:<a href="https://juejin.im/post/5acd8ced6fb9a028d444ee4e" target="_blank" rel="noopener">https://juejin.im/post/5acd8ced6fb9a028d444ee4e</a></p>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
</entry>
<entry>
<title>js实现继承</title>
<url>/2019/11/24/js%E5%AE%9E%E7%8E%B0%E7%BB%A7%E6%89%BF/</url>
<content><![CDATA[<h2 id="一、js继承问题"><a href="#一、js继承问题" class="headerlink" title="一、js继承问题"></a>一、<code>js</code>继承问题</h2><p><code>es6</code>引入了<code>class</code>和<code>extends</code>,从而可以像<code>c++</code>那样理解继承,但本质上这只是一个语法糖。</p>
<a id="more"></a>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">class Parent {</span><br><span class="line"> constructor(name) {</span><br><span class="line"> this.name = name;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line">class Children extends Parent {</span><br><span class="line"> constructor(name) {</span><br><span class="line"> super(name); // 子类没有自己的this,不进行super,得不到继承自父类的this,将出错</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line">const children = new Children("children");</span><br><span class="line">console.log(children.name); // children</span><br></pre></td></tr></table></figure>
<h2 id="二、自己如何实现继承"><a href="#二、自己如何实现继承" class="headerlink" title="二、自己如何实现继承"></a>二、自己如何实现继承</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">function Animal(color) {</span><br><span class="line"> this.name = "animal";</span><br><span class="line"> this.color = color;</span><br><span class="line"> this.type = ['pic', 'cat'];</span><br><span class="line">}</span><br><span class="line">Animal.prototype.sayName = function() {</span><br><span class="line"> console.log(`Hey, I am ${this.name}`);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h4 id="1、继承方式1:原型链"><a href="#1、继承方式1:原型链" class="headerlink" title="1、继承方式1:原型链"></a>1、继承方式1:原型链</h4><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">function Dog() {}</span><br><span class="line">Dog.prototype = new Animal("white");</span><br><span class="line">const dog = new Dog();</span><br><span class="line">console.log(dog.name, ":", dog.color); // animal : white</span><br></pre></td></tr></table></figure>
<p>此种方式缺陷1:引用缺陷</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">const dog1 = new Dog();</span><br><span class="line">dog1.type.push('dog');</span><br><span class="line">const dog2 = new Dog();</span><br><span class="line">console.log(dog2.type); // [ 'pic', 'cat', 'dog' ]</span><br></pre></td></tr></table></figure>
<p>此种方式缺陷2:对于继承来的属性,无法为不同的实例初始化为不同的值</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">function Cat() {}</span><br><span class="line">Cat.prototype = new Animal("black"); // 所有的Cat实例初始化时都是black色</span><br></pre></td></tr></table></figure>
<h4 id="2、继承方式2:借用构造函数-也叫经典继承"><a href="#2、继承方式2:借用构造函数-也叫经典继承" class="headerlink" title="2、继承方式2:借用构造函数(也叫经典继承)"></a>2、继承方式2:借用构造函数(也叫经典继承)</h4><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">function Pic(color) {</span><br><span class="line"> Animal.apply(this, arguments);</span><br><span class="line">}</span><br><span class="line">const pic = new Pic("white");</span><br><span class="line">console.log(pic.name, ":", pic.color); // animal : white</span><br></pre></td></tr></table></figure>
<p>此种方式缺陷:无法获取父类通过原型绑定的方法</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">pic.sayName(); // TypeError: pic.sayName is not a function</span><br></pre></td></tr></table></figure>
<h4 id="3、继承方式3:组合继承-就是组合上面两种,可以避免上面的缺陷。氮素,会调用两次父类构造函数"><a href="#3、继承方式3:组合继承-就是组合上面两种,可以避免上面的缺陷。氮素,会调用两次父类构造函数" class="headerlink" title="3、继承方式3:组合继承(就是组合上面两种,可以避免上面的缺陷。氮素,会调用两次父类构造函数)"></a>3、继承方式3:组合继承(就是组合上面两种,可以避免上面的缺陷。氮素,会调用两次父类构造函数)</h4><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">function Monkey(color) {</span><br><span class="line"> Animal.apply(this, arguments);</span><br><span class="line">}</span><br><span class="line">Monkey.prototype = new Animal();</span><br><span class="line">const monkey = new Monkey("gray");</span><br><span class="line">console.log(monkey.name, ":", monkey.color); // animal : gray</span><br><span class="line">monkey.sayName(); // Hey, I am animal</span><br></pre></td></tr></table></figure>
<h4 id="4、继承方式4:寄生组合继承-避免调用两次父类构造函数的问题"><a href="#4、继承方式4:寄生组合继承-避免调用两次父类构造函数的问题" class="headerlink" title="4、继承方式4:寄生组合继承(避免调用两次父类构造函数的问题)"></a>4、继承方式4:寄生组合继承(避免调用两次父类构造函数的问题)</h4><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">function Tiger(color) {</span><br><span class="line"> Animal.apply(this, arguments);</span><br><span class="line">}</span><br><span class="line">Tiger.prototype = Object.create(Animal.prototype); // 使用Object.create进行浅拷贝,少了一次调用父类的构造函数</span><br><span class="line">Tiger.prototype.constructor = Tiger; // 上面浅拷贝后Tiger.prototype的constructor被重写了,所以要修复一下</span><br></pre></td></tr></table></figure>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
</entry>
<entry>
<title>js的bind函数</title>
<url>/2019/11/23/js%E7%9A%84bind%E5%87%BD%E6%95%B0/</url>
<content><![CDATA[<h2 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h2><p>由<code>Function.prototype.bind()</code>,所以<code>bind()</code>是函数或方法的一个属性。<code>bind()</code>将创建一个新的函数,调用<code>bind()</code>的时候,新函数的<code>this</code>被<code>bind()</code>的第一个参数指定,<code>bind()</code>的其余参数将作为新函数的参数供调用时使用。</p>
<a id="more"></a>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">const obj = {</span><br><span class="line"> x: 12,</span><br><span class="line"> getX: function() { return this.x; },</span><br><span class="line"> setX: function(x) { this.x = x; }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">console.log(obj.getX()); // 12</span><br><span class="line"></span><br><span class="line">const unboundGetX = obj.getX;</span><br><span class="line">// console.log(unboundGetX()); // TypeError: Cannot read property 'x' of undefined</span><br><span class="line"></span><br><span class="line">const boundGetX = unboundGetX.bind(obj);</span><br><span class="line">console.log(boundGetX()); // 12</span><br><span class="line"></span><br><span class="line">const boundSetX = obj.setX.bind(obj, 89);</span><br><span class="line">boundSetX(); // 这时传不传参数不影响obj.x=89的事实</span><br><span class="line">console.log(boundGetX()); // 89</span><br><span class="line"></span><br><span class="line">const boundSetX2 = obj.setX.bind(obj);</span><br><span class="line">boundSetX2(999); // 这时如果不传参数obj.x将是undefined</span><br><span class="line">console.log(boundGetX()); // 999</span><br></pre></td></tr></table></figure>
<h2 id="实现自己的bind"><a href="#实现自己的bind" class="headerlink" title="实现自己的bind()"></a>实现自己的<code>bind()</code></h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">const obj2 = {</span><br><span class="line"> name: "this is obj2"</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">function foo(color) {</span><br><span class="line"> console.log(color);</span><br><span class="line"> return this.name;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">const bindFoo = foo.bind(obj2, "black");</span><br><span class="line">// black</span><br><span class="line">// this is obj2</span><br><span class="line">console.log(bindFoo());</span><br><span class="line"></span><br><span class="line">// 构造函数时</span><br><span class="line">const nbfoo = new bindFoo(); // black,传入的参数依然生效</span><br><span class="line">console.log(nbfoo.name); // undefined,传入的obj2失效,this指向的是nbfoo而不是obj2了</span><br><span class="line"></span><br><span class="line">// TODO: new bindFoo()的时候,this指向的是new出来的而不是obj2,这个效果</span><br><span class="line">Function.prototype.mybind = function(ctx) {</span><br><span class="line"> const fn = this;</span><br><span class="line"> const args = Array.prototype.slice.call(arguments, 1); // 去掉第0个</span><br><span class="line"> return function() {</span><br><span class="line"> return fn.apply(ctx, args);</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">const mybindFoo = foo.mybind(obj2, "white");</span><br><span class="line">// white</span><br><span class="line">// this is obj2</span><br><span class="line">console.log(mybindFoo());</span><br></pre></td></tr></table></figure>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
</entry>
<entry>
<title>js的with关键字</title>
<url>/2019/11/23/js%E7%9A%84with%E5%85%B3%E9%94%AE%E5%AD%97/</url>
<content><![CDATA[<h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">let obj = {</span><br><span class="line"> a: 'aaa',</span><br><span class="line"> b: 'bbb',</span><br><span class="line"> c: 'ccc'</span><br><span class="line">}</span><br><span class="line">console.log(obj.a, obj.b, obj.c);</span><br><span class="line"></span><br><span class="line">// 使用 with</span><br><span class="line">with (obj) {</span><br><span class="line"> console.log(a, b, c);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<p><code>with</code>用来改变作用域,上面的<code>a, b, c</code>首先会被认为是局部变量,然后会查<code>obj</code>中有没有同名的,有就指向该同名属性</p>
<h2 id="然而"><a href="#然而" class="headerlink" title="然而"></a>然而</h2><p>一般是不推荐使用<code>with</code>的,而且<code>strict</code>模式下是不能使用的,为什么呢?主要是性能问题,以及语意不明容易出现<code>bug</code>。</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">let obj = {</span><br><span class="line"> a: 'aaa',</span><br><span class="line"> b: 'bbb',</span><br><span class="line"> c: 'ccc'</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">function fn1() {</span><br><span class="line"> console.time("without");</span><br><span class="line"> for (let i = 0; i < 10000; i++) {</span><br><span class="line"> let ta = obj.a;</span><br><span class="line"> let tb = obj.b;</span><br><span class="line"> let tc = obj.c;</span><br><span class="line"> }</span><br><span class="line"> console.timeEnd("without");</span><br><span class="line">}</span><br><span class="line">function fn2() {</span><br><span class="line"> console.time("with");</span><br><span class="line"> for (let i = 0; i < 10000; i++) {</span><br><span class="line"> with (obj) {</span><br><span class="line"> let ta = a;</span><br><span class="line"> let tb = b;</span><br><span class="line"> let tc = c;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> console.timeEnd("with");</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">fn1(); // 0.343ms</span><br><span class="line">fn2(); // 6.114ms</span><br></pre></td></tr></table></figure>
<p>因为<code>js</code>代码执行有一个编译阶段,使用<code>with</code>后<code>js</code>引擎不知道这些<code>a, b, c</code>是<code>obj</code>上的属性,无法对它们进行静态分析,就无法对这段代码进行优化;另外,一些<code>js</code>压缩工具也无法对这段代码进行压缩。这些是影响其性能的主要原因。</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">let objName = {</span><br><span class="line"> name: "apple"</span><br><span class="line">}</span><br><span class="line">let objColor = {</span><br><span class="line"> color: "white"</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">function foo(obj) {</span><br><span class="line"> with (obj) {</span><br><span class="line"> name = "dog"</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">// console.log(name); // 此时将出错,ReferenceError: name is not defined</span><br><span class="line"></span><br><span class="line">console.log(objName.name); // apple</span><br><span class="line">foo(objName);</span><br><span class="line">console.log(objName.name); // dog</span><br><span class="line"></span><br><span class="line">console.log(objColor.color); // white</span><br><span class="line">foo(objColor);</span><br><span class="line">console.log(objColor.color); // white</span><br><span class="line"></span><br><span class="line">console.log(objColor.name); // undefined</span><br><span class="line">console.log(name); // dog</span><br></pre></td></tr></table></figure>
<p>上面这段代码很容易出错和难以调试。</p>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
</entry>
<entry>
<title>JS的call方法</title>
<url>/2019/11/23/JS%E7%9A%84call%E6%96%B9%E6%B3%95/</url>
<content><![CDATA[<h2 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h2><p><code>foo.call(this, args...)</code>,指明<code>foo</code>函数或方法的<code>this</code>和参数,然后运行<code>foo</code>。</p>
<a id="more"></a>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">function Animal(name) {</span><br><span class="line"> console.log(`animal's name is ${name}, color is ${this.color}`);</span><br><span class="line">}</span><br><span class="line">function Dog() {</span><br><span class="line"> this.color = "dog's color is white";</span><br><span class="line"> Animal.call(this, "dog");</span><br><span class="line">}</span><br><span class="line">new Dog(); // 打印 "animal's name is dog, color is dog's color is white"</span><br></pre></td></tr></table></figure>
<h2 id="为自己打call"><a href="#为自己打call" class="headerlink" title="为自己打call"></a>为自己打<code>call</code></h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">Function.prototype.mycall = function(ctx) {</span><br><span class="line"> // ctx也就是传过来的this(同arguments[0]),传null则为window(nodejs可以用global)</span><br><span class="line"> ctx = ctx || (typeof window === "undefined" ? global : window);</span><br><span class="line"> ctx.foo = this; // 这个this是调用mycall的那个函数或方法</span><br><span class="line"></span><br><span class="line"> let args = [];</span><br><span class="line"> for (let i = 1; i < arguments.length; i++) {</span><br><span class="line"> args.push(arguments[i]);</span><br><span class="line"> }</span><br><span class="line"> ctx.foo(...args);</span><br><span class="line"> delete ctx.foo;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">function Animal2(name) {</span><br><span class="line"> console.log(`animal's name is ${name}, color is ${this.color}`);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">function Dog2() {</span><br><span class="line"> this.color = "black";</span><br><span class="line"> Animal2.mycall(this, "dogdog");</span><br><span class="line">}</span><br><span class="line">new Dog2(); // 打印 "animal's name is dogdog, color is black"</span><br><span class="line"></span><br><span class="line">let Cat = {</span><br><span class="line"> color: "white"</span><br><span class="line">}</span><br><span class="line">Animal2.mycall(Cat, "catcat"); // 打印 "animal's name is catcat, color is white"</span><br></pre></td></tr></table></figure>
<h2 id="apply呢"><a href="#apply呢" class="headerlink" title="apply呢"></a><code>apply</code>呢</h2><p>上面的<code>call</code>,调用的时候,需要对应每个参数,而<code>apply</code>一共只有两个参数,第一个和<code>call</code>是一样的,而第二个参数传的是参数数组,这用于不确定参数时很方便。</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">function fn(arg1, arg2) {</span><br><span class="line"> console.log.call(console, arg1, arg2);</span><br><span class="line">}</span><br><span class="line">fn("what", 666); // 打印 "what 666"</span><br><span class="line"></span><br><span class="line">function fn2() {</span><br><span class="line"> console.log.apply(console, arguments);</span><br><span class="line">}</span><br><span class="line">fn2(12, "haha", 1234); // 打印 "12 haha 1234"</span><br><span class="line">fn2(12, "haha", 1234, "what", "why"); // 打印 "12 haha 1234 what why"</span><br></pre></td></tr></table></figure>
<h2 id="总的"><a href="#总的" class="headerlink" title="总的"></a>总的</h2><p>总的来说,<code>call</code>和<code>apply</code>都是为了动态改变<code>this</code>的指向。</p>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
</entry>
<entry>
<title>JS原型和原型链</title>
<url>/2019/11/22/JS%E5%8E%9F%E5%9E%8B%E5%92%8C%E5%8E%9F%E5%9E%8B%E9%93%BE/</url>
<content><![CDATA[<h2 id="背景"><a href="#背景" class="headerlink" title="背景"></a>背景</h2><p>有时代码敲得再多,项目做的再多,面试时还是会被一些简单的问题难倒</p>
<a id="more"></a>
<h2 id="JS原型和原型链"><a href="#JS原型和原型链" class="headerlink" title="JS原型和原型链"></a>JS原型和原型链</h2><p>读取某个属性,首先会在对象里面看看有没有,没有就会去对象的原型里面看看,以此直到最上层的<code>Object</code>对象的原型</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">function Animal() {</span><br><span class="line"> // 实例化后属于实例的属性</span><br><span class="line"> // 如果这里不定义,则和 let animal = new Animal(); animal.name = "I am an animal" 是一样的</span><br><span class="line"> this.name = "I am an animal";</span><br><span class="line">}</span><br><span class="line">Animal.prototype.name = "Hey, this is an animal"; // 对象原型的属性</span><br><span class="line"></span><br><span class="line">let animal = new Animal();</span><br><span class="line">console.log(animal.name); // 打印 "I am an animal"</span><br><span class="line"></span><br><span class="line">delete animal.name;</span><br><span class="line">console.log(animal.name); // 打印 "Hey, this is an animal"</span><br><span class="line"></span><br><span class="line">console.log(animal.__proto__ === Animal.prototype); // true</span><br><span class="line">console.log(Animal.prototype.__proto__ === Object.prototype); // true Animal的上一层是Object</span><br><span class="line">console.log(Animal.prototype.constructor === Animal); // true</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">// 一般地</span><br><span class="line">let obj = new Object();</span><br><span class="line">console.log(obj.__proto__ === Object.prototype); // true</span><br><span class="line">console.log(Object.prototype.__proto__ === null); // true 原型链顶层</span><br><span class="line">console.log(Object.prototype.constructor === Object); // true</span><br></pre></td></tr></table></figure>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
</entry>
<entry>
<title>《伟大的博弈》读后</title>
<url>/2019/11/15/%E3%80%8A%E4%BC%9F%E5%A4%A7%E7%9A%84%E5%8D%9A%E5%BC%88%E3%80%8B%E8%AF%BB%E5%90%8E/</url>
<content><![CDATA[<p>《伟大的博弈》讲的是华尔街的历史,讲述了华尔街从无到有,讲述了华尔街经历的战乱、灾难、成长,讲述了资本市场的腥风血雨、跌宕起伏。长投学堂的小熊老师说看完这本书,你对我们A股股市的波动就不会感到惊讶,因为和华尔街相比,A股的波动真的算不上波动了。</p>
<a id="more"></a>
<p>读完这本书后,我感觉有点喜欢上读历史了,这真的是本很有趣的书。</p>
]]></content>
<categories>
<category>电影阅读</category>
</categories>
<tags>
<tag>读书</tag>
</tags>
</entry>
<entry>
<title>《聪明的投资者》读后</title>
<url>/2019/11/10/%E3%80%8A%E8%81%AA%E6%98%8E%E7%9A%84%E6%8A%95%E8%B5%84%E8%80%85%E3%80%8B%E8%AF%BB%E5%90%8E/</url>
<content><![CDATA[<p>《聪明的投资者》是巴菲特的老师格雷厄姆出的一本书,巴菲特据说很少向别人推荐什么书,可能是很多书不能入他老人家的法眼吧,却唯独这一本是他非常推崇的。这本书也是长投学堂的小熊老师说读了很多次的书。</p>
<a id="more"></a>
<p>这本书讲了积极性投资者和保守型投资者的投资策略,股票和债券的投资组合,安全边际等等投资理财知识。读完这本书的感觉就是,书里面说的很多人应该都懂,也许还要多读几遍,满满领会。</p>
]]></content>
<categories>
<category>电影阅读</category>
</categories>
<tags>
<tag>读书</tag>
</tags>
</entry>
<entry>
<title>scatter交易missing required accounts</title>
<url>/2019/02/18/scatter%E4%BA%A4%E6%98%93missing-required-accounts/</url>
<content><![CDATA[<h2 id="概述:"><a href="#概述:" class="headerlink" title="概述:"></a>概述:</h2><p>使用<code>scatter js sdk</code>发起一笔交易,请求<code>scatter</code>确认时,出现如下错误提示:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">Missing required accounts, repull the identity</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<h2 id="解决:"><a href="#解决:" class="headerlink" title="解决:"></a>解决:</h2><p>需要添加权限选项<code>{ authorization: [account.name@account.authority] }</code>,比如 EOS 转账交易:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">eos.contract('eosio.token').then(contract => {</span><br><span class="line"> contract.transfer(</span><br><span class="line"> 'fromaccount1',</span><br><span class="line"> 'toaccount111',</span><br><span class="line"> '1.0000 EOS',</span><br><span class="line"> 'test',</span><br><span class="line"> { authorization: ['fromaccount1@active'] }</span><br><span class="line"> );</span><br><span class="line">});</span><br></pre></td></tr></table></figure>
]]></content>
<tags>
<tag>eosio</tag>
</tags>
</entry>
<entry>
<title>eosio.token的transfer的memo长度</title>
<url>/2019/02/18/eosio-token%E7%9A%84transfer%E7%9A%84memo%E9%95%BF%E5%BA%A6/</url>
<content><![CDATA[<h2 id="概述:"><a href="#概述:" class="headerlink" title="概述:"></a>概述:</h2><p>平时我们进行 EOS 币转账的时候,<code>memo</code>最多可以填多少个汉字、多少个英文字符呢?</p>
<a id="more"></a>
<p>官方<code>eosio.token</code>合约的<code>transfer</code>方法,<code>memo</code>被断言为长度小于等于<code>256</code>(<code>eosio_assert( memo.size() <= 256, "memo has more than 256 bytes" )</code>),那么作为一名“专业”码农应该觉得可以输入纯英文字符最多<code>256</code>个,纯汉字减半,然鹅在链上跑的时候,并不是酱紫的。</p>
<h2 id="敲黑板:"><a href="#敲黑板:" class="headerlink" title="敲黑板:"></a>敲黑板:</h2><p>在链上跑的时候,纯汉字最多可以输入<code>85</code>个,纯英文字符可以输入<code>258</code>个。每个汉字占用<code>length</code>为<code>3</code>。</p>
]]></content>
<tags>
<tag>eosio</tag>
</tags>
</entry>
<entry>
<title>transaction declares authority错误</title>
<url>/2019/02/18/transaction-declares-authority%E9%94%99%E8%AF%AF/</url>
<content><![CDATA[<h2 id="概述:"><a href="#概述:" class="headerlink" title="概述:"></a>概述:</h2><p>push action 或者 transaction 的时候,出现类似以下错误:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">transaction declares authority '{"actor":"myaccount111","permission":"active"}', but does not have signatures for it under a provided delay of 0 ms, provided permissions [{"actor":"myaccount111","permission":"eosio.code"}], and provided keys []</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<h2 id="原因:"><a href="#原因:" class="headerlink" title="原因:"></a>原因:</h2><p>检查部署<code>myaccount111</code>的合约代码,看看是不是使用了内联 action 或 defer ,这需要你的<code>myaccount111</code>具有<code>eosio.code</code>权限</p>
<h2 id="解决:"><a href="#解决:" class="headerlink" title="解决:"></a>解决:</h2><p>给<code>myaccount111</code>赋予<code>eosio.code</code>权限</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">// 这里 EOS5RZcioUTeXNWCUehqtSHpzcXEj4auyRQiD3qwsjCQjWqmH8Qgs 是你 myaccount111 的公钥</span><br><span class="line">$: cleos set account permission myaccount111 active '{"threshold":1, "keys":[{"key":"EOS5RZcioUTeXNWCUehqtSHpzcXEj4auyRQiD3qwsjCQjWqmH8Qgs", "weight":1}], "accounts": [{"permission":{"actor":"myaccount111","permission":"eosio.code"},"weight":1}]}' owner -p myaccount111</span><br></pre></td></tr></table></figure>
]]></content>
<tags>
<tag>eosio</tag>
</tags>
</entry>
<entry>
<title>节点rpc无法访问</title>
<url>/2019/02/18/%E8%8A%82%E7%82%B9rpc%E6%97%A0%E6%B3%95%E8%AE%BF%E9%97%AE/</url>
<content><![CDATA[<h2 id="概述:"><a href="#概述:" class="headerlink" title="概述:"></a>概述:</h2><p>你通过<code>config.ini</code>配置了<code>http-server-address = 0.0.0.0:8888</code>,并且配置了插件<code>plugin = eosio::http_plugin</code>,假设你的 server 的公开 ip 为<code>12.34.56.78</code>,氮素<code>cleos -u http://12.34.56.78:8888</code>仍然无法访问你的节点 rpc。</p>
<a id="more"></a>
<h2 id="一些问题:"><a href="#一些问题:" class="headerlink" title="一些问题:"></a>一些问题:</h2><ul>
<li>看看你的 server 安全组有没有允许<code>8888</code>端口的访问</li>
<li>在<code>config.ini</code>中配置<code>http-alias = 12.34.56.78:8888</code>试试看</li>
<li>然后重启 nodeos</li>
</ul>
]]></content>
<tags>
<tag>eosio</tag>
</tags>
</entry>
<entry>
<title>eos合约action命名</title>
<url>/2019/02/18/eos%E5%90%88%E7%BA%A6action%E5%91%BD%E5%90%8D/</url>
<content><![CDATA[<h2 id="概述:"><a href="#概述:" class="headerlink" title="概述:"></a>概述:</h2><p>EOS 中,合约的<code>action</code>对应合约 C++ 代码中的一个函数,如果你认为这个函数可以像传统 C++ 编码那样命名,那就错了。因为对应合约的<code>action</code>,所以有一些规则限制。</p>
<a id="more"></a>
<h2 id="出错:"><a href="#出错:" class="headerlink" title="出错:"></a>出错:</h2><p>如果编译或执行合约的某个<code>action</code>,出现以下提示,说明<code>action</code>对应的函数命名不合规范:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">Name should be less than 13 characters and only contains the following symbol .12345abcdefghijklmnopqrstuvwxyz</span><br></pre></td></tr></table></figure>
<h2 id="规范:"><a href="#规范:" class="headerlink" title="规范:"></a>规范:</h2><p>从<code>出错</code>提示知道,<code>action</code>对应的函数名,长度不能超过<code>12</code>个字符,并且只能由<code>.12345abcdefghijklmnopqrstuvwxyz</code>这些字符组成。</p>
]]></content>
<tags>
<tag>eosio</tag>
</tags>
</entry>
<entry>
<title>config.ini中的signature-provider</title>
<url>/2019/02/18/config-ini%E4%B8%AD%E7%9A%84signature-provider/</url>
<content><![CDATA[<h2 id="概述:"><a href="#概述:" class="headerlink" title="概述:"></a>概述:</h2><p>如果你要部署一个 EOSIO 的出块节点,你可能会关心这个问题。如果不是,或者你只是部署一个普通节点,那你不用care这里说的。</p>
<a id="more"></a>
<h2 id="signature-provider应该填哪对秘钥:"><a href="#signature-provider应该填哪对秘钥:" class="headerlink" title="signature-provider应该填哪对秘钥:"></a><code>signature-provider</code>应该填哪对秘钥:</h2><p>在节点的配置文件<code>config.ini</code>中,<code>signature-provider</code>应该填哪一对秘钥呢?可能你会和刚开始的我一样,觉得应该填的是出块账号对应的 owner 或 active 的 key ,但其实这是不建议的,因为涉及到安全问题。<br>其实你完全可以通过 <code>cleos create key --to-console</code> 出来一对<code>key</code> ,只要你注册出块节点的时候使用这个<code>key</code>,那么<code>signature-provider</code>你就可以使用这对<code>key</code>。<br>即,只要保证<code>signature-provider</code>用的是注册时的就行,至于注册你用哪个,你爱用哪个用哪个。</p>
]]></content>
<tags>
<tag>eosio</tag>
</tags>
</entry>
<entry>
<title>eosio计算ram价格</title>
<url>/2019/02/18/eosio%E8%AE%A1%E7%AE%97ram%E4%BB%B7%E6%A0%BC/</url>
<content><![CDATA[<h2 id="首先获取多索引表rammarket的数据:"><a href="#首先获取多索引表rammarket的数据:" class="headerlink" title="首先获取多索引表rammarket的数据:"></a>首先获取多索引表<code>rammarket</code>的数据:</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$: cleos -u https://nodes.get-scatter.com:443 get table eosio eosio rammarket</span><br><span class="line">{</span><br><span class="line"> "rows": [{</span><br><span class="line"> "supply": "10000000000.0000 RAMCORE",</span><br><span class="line"> "base": {</span><br><span class="line"> "balance": "70910488246 RAM",</span><br><span class="line"> "weight": "0.50000000000000000"</span><br><span class="line"> },</span><br><span class="line"> "quote": {</span><br><span class="line"> "balance": "1722420.0326 EOS",</span><br><span class="line"> "weight": "0.50000000000000000"</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> ],</span><br><span class="line"> "more": false</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<h2 id="那么n-KB的内存价格为:"><a href="#那么n-KB的内存价格为:" class="headerlink" title="那么n KB的内存价格为:"></a>那么<code>n KB</code>的内存价格为:</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">n KB内存需要的EOS = ( n * quote.balance ) / ( n + base.balance / 1024 )</span><br></pre></td></tr></table></figure>
]]></content>
<tags>
<tag>eosio</tag>
</tags>
</entry>
<entry>
<title>eosio获取账号actions</title>
<url>/2019/02/18/eosio%E8%8E%B7%E5%8F%96%E8%B4%A6%E5%8F%B7actions/</url>
<content><![CDATA[<h2 id="概述:"><a href="#概述:" class="headerlink" title="概述:"></a>概述:</h2><p>有时候我们使用别人的 rpc 地址,<code>get actions</code>的时候返回的是空:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$: cleos -u https://别人的rpc.io get actions myaccount111</span><br><span class="line"># seq when contract::action => receiver trx id... args</span><br><span class="line">================================================================================================================</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<p>这是因为,你使用的这个别人的 rpc 地址,他的 nodeos 没有打开<code>filter-on</code>配置,或者他只开了他需要监听的账号和 action ,这个时候,你就要另找 rpc 地址了。</p>
<h2 id="配置自己的节点:"><a href="#配置自己的节点:" class="headerlink" title="配置自己的节点:"></a>配置自己的节点:</h2><p>如果你有自己的节点,你想用自己节点的 rpc 查找 actions ,应该怎么配置呢?<br>当然,你可以在 <code>config.ini</code> 配置文件中,设置 <code>filter-on = *</code> ,这样会记录全网的所有账号,以及所有的 actions ,但是你要认识到这需要很大的服务器物理内存。<br>如果你只需要记录账号<code>myaccount111</code>的<code>transfer</code>,你只用这样配置就好了:<code>filter-on = myaccount111::transfer</code>。</p>
]]></content>
<tags>
<tag>eosio</tag>
</tags>
</entry>
<entry>
<title>screen</title>
<url>/2019/02/06/screen/</url>
<content><![CDATA[<h2 id="简介:"><a href="#简介:" class="headerlink" title="简介:"></a>简介:</h2><p>当我们远程登录服务器,正在执行某些操作,比如正在编译源代码到60%,如果因为网络等各种原因断开了与服务器的链接,那么编译过程可能会被中断,我们重新链接服务器时需要从从头进行编译。<br>如果执行编译前使用了<code>screen</code>,链接断开时编译工作不会中断,重连后登录之前的<code>screen</code>会话,就可以回到当时的环境。</p>
<a id="more"></a>
<h2 id="使用:"><a href="#使用:" class="headerlink" title="使用:"></a>使用:</h2><ul>
<li>screen -S XXX:创建名为XXX的后台会话</li>
<li>screnn -ls:查看所有后台会话</li>
<li>screen -r XXX:恢复名为XXX的后台会话</li>
</ul>
<h2 id="参考:"><a href="#参考:" class="headerlink" title="参考:"></a>参考:</h2><p><a href="http://www.cnblogs.com/mchina/archive/2013/01/30/2880680.html" target="_blank" rel="noopener">http://www.cnblogs.com/mchina/archive/2013/01/30/2880680.html</a></p>
]]></content>
</entry>
<entry>
<title>EOSIO笔记</title>
<url>/2019/02/02/EOSIO%E7%AC%94%E8%AE%B0/</url>
<content><![CDATA[<h2 id="action"><a href="#action" class="headerlink" title="action"></a>action</h2><ul>
<li>对应的 function 返回值为 void</li>
<li>命名长度不能超过12位</li>
<li>命名须由 <code>.12345abcdefghijklmnopqrstuvwxyz</code> 构成</li>
</ul>
]]></content>
<tags>
<tag>eosio</tag>
</tags>
</entry>
<entry>
<title>HomeBrew</title>
<url>/2019/02/02/HomeBrew/</url>
<content><![CDATA[<h2 id="HomeBrew:"><a href="#HomeBrew:" class="headerlink" title="HomeBrew:"></a>HomeBrew:</h2><p>macOS下的<code>homebrew</code>相当于Linux下的<code>apt-get</code>,即包管理器</p>
<a id="more"></a>
<h2 id="包卸载:"><a href="#包卸载:" class="headerlink" title="包卸载:"></a>包卸载:</h2><p>这里以卸载 <code>boost</code> 为例:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$: brew unlink boost</span><br><span class="line">$: brew unpin boost</span><br><span class="line">$: brew uninstall boost</span><br></pre></td></tr></table></figure>
<h2 id="包升级:"><a href="#包升级:" class="headerlink" title="包升级:"></a>包升级:</h2><p>这里以升级 <code>mongodb</code> 为例</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$: brew upgrade mongodb</span><br></pre></td></tr></table></figure>
<h2 id="查看服务:"><a href="#查看服务:" class="headerlink" title="查看服务:"></a>查看服务:</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$: brew services list</span><br></pre></td></tr></table></figure>
<h2 id="启动、停止、重启服务:"><a href="#启动、停止、重启服务:" class="headerlink" title="启动、停止、重启服务:"></a>启动、停止、重启服务:</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$: brew services start/stop/restart mongodb</span><br></pre></td></tr></table></figure>
]]></content>
<tags>
<tag>macOS</tag>
<tag>HomeBrew</tag>
</tags>
</entry>
<entry>
<title>创建一个react native项目</title>
<url>/2018/12/08/%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AAreact-native%E9%A1%B9%E7%9B%AE/</url>
<content><![CDATA[<h2 id="一、打开模拟器(当然也可以使用真机)"><a href="#一、打开模拟器(当然也可以使用真机)" class="headerlink" title="一、打开模拟器(当然也可以使用真机)"></a>一、打开模拟器(当然也可以使用真机)</h2><p>打开任意一个,或者新建一个Android Studio项目,然后启动模拟器。</p>
<a id="more"></a>
<h2 id="二、新建工程"><a href="#二、新建工程" class="headerlink" title="二、新建工程"></a>二、新建工程</h2><p><code>$: react-native init demo</code></p>
<h2 id="三、启动工程"><a href="#三、启动工程" class="headerlink" title="三、启动工程"></a>三、启动工程</h2><p><code>$: react-native run-android</code></p>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
<tags>
<tag>React</tag>
</tags>
</entry>
<entry>
<title>EOSIO通过插件存储账号余额到Mongo</title>
<url>/2018/12/03/EOSIO%E9%80%9A%E8%BF%87%E6%8F%92%E4%BB%B6%E5%AD%98%E5%82%A8%E8%B4%A6%E5%8F%B7%E4%BD%99%E9%A2%9D%E5%88%B0Mongo/</url>
<content><![CDATA[<h2 id="通过mongo-db-plugin存储账号余额到mongodb"><a href="#通过mongo-db-plugin存储账号余额到mongodb" class="headerlink" title="通过mongo_db_plugin存储账号余额到mongodb"></a>通过mongo_db_plugin存储账号余额到mongodb</h2><p>我们这里以存储<code>eosio.token</code>的<code>EOS</code>余额为例,思路是在发生<code>transfer</code>的时候,我们捕获该<code>action</code>,然后更新双方的余额。</p>
<a id="more"></a>
<p>首先,在<code>class mongo_db_plugin_impl</code>中声明我们需要的变量和函数</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"> static const std::string accounts_col;</span><br><span class="line"> static const std::string pub_keys_col;</span><br><span class="line"> static const std::string account_controls_col;</span><br><span class="line">+</span><br><span class="line">+ // for get currency balance</span><br><span class="line">+ mongocxx::collection _currency_balance; // mongo集合的handle</span><br><span class="line">+ static const std::string currency_balance_col; // 将存放mongo集合的名称</span><br><span class="line">+ chain_plugin* chain_plug_handle; // chain_plugin 中 get_currency_balance 可以帮助我们获取余额信息</span><br><span class="line">+ void update_currency_balance( const chain::action_trace& ); // 我们的操作放到这里进行</span><br></pre></td></tr></table></figure>
<p>给集合取名为<code>currency_balance</code></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"> const std::string mongo_db_plugin_impl::account_controls_col = "account_controls";</span><br><span class="line">+const std::string mongo_db_plugin_impl::currency_balance_col = "currency_balance"; // mongo中集合将显示的是这个</span><br></pre></td></tr></table></figure>
<p>在<code>mongo_db_plugin_impl::consume_blocks</code>中初始化<code>handle</code></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"> _account_controls = mongo_conn[db_name][account_controls_col];</span><br><span class="line">+ _currency_balance = mongo_conn[db_name][currency_balance_col];</span><br></pre></td></tr></table></figure>
<p>在<code>mongo_db_plugin_impl::init()</code>中给集合定义一个索引</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"> account_controls.create_index( bsoncxx::from_json( R"xxx({ "controlling_account" : 1 })xxx" ));</span><br><span class="line"></span><br><span class="line">+ // currency_balance index</span><br><span class="line">+ auto currency_balance = mongo_conn[db_name][currency_balance_col];</span><br><span class="line">+ currency_balance.create_index( bsoncxx::from_json( R"xxx({ "name" : 1 })xxx" )); // name字段索引为升序</span><br><span class="line">+ currency_balance.create_index( bsoncxx::from_json( R"xxx({ "balance" : -1 })xxx" )); // balance字段索引为降序</span><br><span class="line">+</span><br><span class="line"> } catch (...) {</span><br><span class="line"> handle_mongo_exception( "create indexes", __LINE__ );</span><br></pre></td></tr></table></figure>
<p>在<code>mongo_db_plugin::plugin_initialize</code>中实例化<code>chain_plug_handle</code></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"> auto& chain = chain_plug->chain();</span><br><span class="line"> my->chain_id.emplace( chain.get_chain_id());</span><br><span class="line"></span><br><span class="line">+ // 给 chain_plug_handle 实例化</span><br><span class="line">+ my->chain_plug_handle = chain_plug;</span><br><span class="line">+</span><br><span class="line"> my->accepted_block_connection.emplace( chain.accepted_block.connect( [&]( const chain::block_state_ptr& bs ) {</span><br><span class="line"> my->accepted_block( bs );</span><br><span class="line"> } ));</span><br></pre></td></tr></table></figure>
<p>定义我们的<code>update_currency_balance</code>函数</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">+void mongo_db_plugin_impl::update_currency_balance( const chain::action_trace& atrace ) {</span><br><span class="line">+ using namespace bsoncxx::types;</span><br><span class="line">+ using bsoncxx::builder::basic::kvp;</span><br><span class="line">+ using bsoncxx::builder::basic::make_document;</span><br><span class="line">+</span><br><span class="line">+ auto receiver = atrace.receipt.receiver;</span><br><span class="line">+ if ( atrace.act.account == name("eosio.token") && atrace.act.name == name("transfer") ) {</span><br><span class="line">+ chain_apis::read_only::get_currency_balance_params params = chain_apis::read_only::get_currency_balance_params {</span><br><span class="line">+ .code = name("eosio.token"),</span><br><span class="line">+ .account = receiver,</span><br><span class="line">+ .symbol = "EOS",</span><br><span class="line">+ };</span><br><span class="line">+</span><br><span class="line">+ chain_apis::read_only ro_api = chain_plug_handle->get_read_only_api();</span><br><span class="line">+ vector<asset> asserts = ro_api.get_currency_balance( params );</span><br><span class="line">+ if ( !asserts.empty() ) {</span><br><span class="line">+ asset balance = asserts.at(0);</span><br><span class="line">+ auto now = std::chrono::duration_cast<std::chrono::milliseconds>(</span><br><span class="line">+ std::chrono::microseconds{fc::time_point::now().time_since_epoch().count()} );</span><br><span class="line">+</span><br><span class="line">+ // ilog( "${a}'s balance: ${b}", ("a", atrace.receipt.receiver.to_string())("b", balance.to_string()) );</span><br><span class="line">+</span><br><span class="line">+ mongocxx::options::update update_opts{};</span><br><span class="line">+ update_opts.upsert( true );</span><br><span class="line">+</span><br><span class="line">+ const double balance_real = balance.to_real();</span><br><span class="line">+ const string receiver_str = receiver.to_string();</span><br><span class="line">+</span><br><span class="line">+ auto update = make_document(</span><br><span class="line">+ kvp( "$set", make_document( kvp( "name", receiver_str),</span><br><span class="line">+ kvp( "balance", balance_real),</span><br><span class="line">+ kvp( "createdAt", b_date{now} ))</span><br><span class="line">+ )</span><br><span class="line">+ );</span><br><span class="line">+</span><br><span class="line">+ try {</span><br><span class="line">+ if( !_currency_balance.update_one( make_document( kvp( "name", receiver_str )), update.view(), update_opts )) {</span><br><span class="line">+ EOS_ASSERT( false, chain::mongo_db_update_fail, "Failed to insert account ${n}", ("n", receiver));</span><br><span class="line">+ }</span><br><span class="line">+ } catch (...) {</span><br><span class="line">+ handle_mongo_exception( "update_currency", __LINE__ );</span><br><span class="line">+ }</span><br><span class="line">+ }</span><br><span class="line">+ }</span><br><span class="line">+}</span><br><span class="line">+</span><br><span class="line"> mongo_db_plugin_impl::mongo_db_plugin_impl()</span><br><span class="line"> {</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
<p><code>mongodb-wipe</code>的时候,应该删除集合,在<code>mongo_db_plugin_impl::wipe_database</code>添加如下</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"> auto pub_keys = mongo_conn[db_name][pub_keys_col];</span><br><span class="line"> auto account_controls = mongo_conn[db_name][account_controls_col];</span><br><span class="line">+ auto currency_balance = mongo_conn[db_name][currency_balance_col];</span><br><span class="line"></span><br><span class="line"> pub_keys.drop();</span><br><span class="line"> account_controls.drop();</span><br><span class="line">+ currency_balance.drop();</span><br><span class="line"> ilog("done wipe_database");</span><br></pre></td></tr></table></figure>
<p>准备好了之后,重新<code>./eosio_build.sh</code>,然后使用该<code>nodeos</code>重启节点(当然重启节点前需要配置好<code>mongo</code>插件,并且<code>sudo mongod</code>启动了<code>mongo</code>服务)。<br>通过<code>shell</code>查看<code>mongo</code>结果:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ mongo</span><br><span class="line"></span><br><span class="line">> show dbs</span><br><span class="line">EOS 0.093GB</span><br><span class="line">...</span><br><span class="line"></span><br><span class="line">> use EOS</span><br><span class="line">switched to db EOS</span><br><span class="line"></span><br><span class="line">> show collections</span><br><span class="line">account_controls</span><br><span class="line">accounts</span><br><span class="line">action_traces</span><br><span class="line">block_states</span><br><span class="line">blocks</span><br><span class="line">currency_balance</span><br><span class="line">pub_keys</span><br><span class="line">transaction_traces</span><br><span class="line">transactions</span><br><span class="line">// 通过上面show collections可以看到我们自定义的currency_balance集合了</span><br><span class="line"></span><br><span class="line">// 按照balance降序显示</span><br><span class="line">> db.currency_balance.find().sort({balance:-1}).pretty()</span><br><span class="line">{</span><br><span class="line"> "_id" : ObjectId("5bf684f44549fee6d16a0888"),</span><br><span class="line"> "name" : "eosio.stake",</span><br><span class="line"> "balance" : 240020654.8,</span><br><span class="line"> "createdAt" : ISODate("2018-11-22T10:52:39.140Z")</span><br><span class="line">}</span><br><span class="line">{</span><br><span class="line"> "_id" : ObjectId("5bf684f64549fee6d16a2964"),</span><br><span class="line"> "name" : "eosio.faucet",</span><br><span class="line"> "balance" : 199999730,</span><br><span class="line"> "createdAt" : ISODate("2018-11-22T10:51:18.982Z")</span><br><span class="line">}</span><br><span class="line">...</span><br></pre></td></tr></table></figure>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
<tags>
<tag>eosio</tag>
</tags>
</entry>
<entry>
<title>EOSIO的Mongo插件的使用</title>
<url>/2018/12/03/EOSIO%E7%9A%84Mongo%E6%8F%92%E4%BB%B6%E7%9A%84%E4%BD%BF%E7%94%A8/</url>
<content><![CDATA[<h2 id="eosio提供了mongo插件,实现把数据导到mongo中,从而可以更加灵活的操作数据。"><a href="#eosio提供了mongo插件,实现把数据导到mongo中,从而可以更加灵活的操作数据。" class="headerlink" title="eosio提供了mongo插件,实现把数据导到mongo中,从而可以更加灵活的操作数据。"></a>eosio提供了mongo插件,实现把数据导到mongo中,从而可以更加灵活的操作数据。</h2><h2 id="配置(config-ini)"><a href="#配置(config-ini)" class="headerlink" title="配置(config.ini)"></a>配置(config.ini)</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">plugin = eosio::mongo_db_plugin</span><br><span class="line">mongodb-uri = mongodb://localhost:27017</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<h2 id="本地启动mongo"><a href="#本地启动mongo" class="headerlink" title="本地启动mongo"></a>本地启动mongo</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ sudo mongod</span><br></pre></td></tr></table></figure>
<h2 id="重启nodeos"><a href="#重启nodeos" class="headerlink" title="重启nodeos"></a>重启nodeos</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ nodeos --replay-blockchain --hard-replay-blockchain --mongodb-wipe</span><br></pre></td></tr></table></figure>
<h2 id="mongo中查询"><a href="#mongo中查询" class="headerlink" title="mongo中查询"></a>mongo中查询</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ mongo</span><br><span class="line">> show dbs</span><br><span class="line">EOS 0.372GB</span><br><span class="line">...</span><br><span class="line"></span><br><span class="line">> use EOS</span><br><span class="line">switched to db EOS</span><br><span class="line"></span><br><span class="line">> show tables</span><br><span class="line">account_controls</span><br><span class="line">accounts</span><br><span class="line">action_traces</span><br><span class="line">block_states</span><br><span class="line">blocks</span><br><span class="line">pub_keys</span><br><span class="line">transaction_traces</span><br><span class="line">transactions</span><br><span class="line"></span><br><span class="line">> db.accounts.find().pretty()</span><br><span class="line">{</span><br><span class="line"> "_id" : ObjectId("5bf3cfbf4f6371c75cdc8890"),</span><br><span class="line"> "name" : "superoneiobp",</span><br><span class="line"> "createdAt" : ISODate("2018-11-20T09:11:27.309Z")</span><br><span class="line">}</span><br><span class="line">...</span><br></pre></td></tr></table></figure>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
<tags>
<tag>eosio</tag>
</tags>
</entry>
<entry>
<title>MongoDB简单使用</title>
<url>/2018/12/03/MongoDB%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8/</url>
<content><![CDATA[<h2 id="MongoDB简介"><a href="#MongoDB简介" class="headerlink" title="MongoDB简介"></a>MongoDB简介</h2><p>MongoDB是一个基于分布式文件存储,介于关系型和非关系型的数据库。<br>今天我们通过第三方平台(如Google等)可以很轻易地访问和抓取用户数据。用户的个人信息、社交网络、地理位置,用户生成的数据和用户操作日子已经成倍的增加。我们如果要对这些数据挖掘,那SQL数据库已经不适合这些应用了,NoSQL(即No Only SQL,MongoDB就是这种类型的数据库之一)数据库的发展却能很好的处理这些大数据。</p>
<a id="more"></a>
<p>MongoDB将数据存储为一个文档,数据结构有键值对(key=>value)组成,类似于JSON对象,MongoDB中叫BSON(即序列化的二进制JSON)。</p>
<h2 id="MacOS平台安装MongoDB"><a href="#MacOS平台安装MongoDB" class="headerlink" title="MacOS平台安装MongoDB"></a>MacOS平台安装MongoDB</h2><p>官网提供了安装包:<a href="https://www.mongodb.com/download-center#community" target="_blank" rel="noopener">https://www.mongodb.com/download-center#community</a>;<br>也可以通过brew进行安装:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ sudo brew install mongodb</span><br><span class="line"></span><br><span class="line">// 安装支持 TLS/SSL 命令</span><br><span class="line">$ sudo brew install mongodb --with-openssl</span><br><span class="line"></span><br><span class="line">// 安装最新开发版本</span><br><span class="line">$sudo brew install mongodb --devel</span><br></pre></td></tr></table></figure>
<h2 id="MongoDB操作"><a href="#MongoDB操作" class="headerlink" title="MongoDB操作"></a>MongoDB操作</h2><h4 id="运行"><a href="#运行" class="headerlink" title="运行"></a>运行</h4><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">// 创建一个数据库存储目录</span><br><span class="line">$ sudo mkdir -p /data/db</span><br><span class="line"></span><br><span class="line">// 启动 mongodb,默认数据库目录即为 /data/db</span><br><span class="line">$ sudo mongod</span><br><span class="line"></span><br><span class="line">// 打开另一个终端连接刚启动的mongod服务</span><br><span class="line">$ mongo</span><br></pre></td></tr></table></figure>
<h4 id="操作-启动mongo连接数据库服务后"><a href="#操作-启动mongo连接数据库服务后" class="headerlink" title="操作(启动mongo连接数据库服务后)"></a>操作(启动mongo连接数据库服务后)</h4><h5 id="显示数据库"><a href="#显示数据库" class="headerlink" title="显示数据库"></a>显示数据库</h5><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">// 显示所有数据库列表</span><br><span class="line">> show dbs</span><br><span class="line"></span><br><span class="line">// 显示当前的数据库或集合</span><br><span class="line">> db</span><br></pre></td></tr></table></figure>
<h5 id="使用mytest数据库,如果不存在则新建"><a href="#使用mytest数据库,如果不存在则新建" class="headerlink" title="使用mytest数据库,如果不存在则新建"></a>使用mytest数据库,如果不存在则新建</h5><p><code>> use mytest</code></p>
<h5 id="删除mytest数据库"><a href="#删除mytest数据库" class="headerlink" title="删除mytest数据库"></a>删除mytest数据库</h5><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">> use mytest</span><br><span class="line">> db.dropDatabase()</span><br></pre></td></tr></table></figure>
<h5 id="创建集合"><a href="#创建集合" class="headerlink" title="创建集合"></a>创建集合</h5><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">// 在test数据库上创建一个叫kk的集合</span><br><span class="line">> use test</span><br><span class="line">> db.createCollection("kk")</span><br><span class="line"></span><br><span class="line">// 或者插入数据的时候,也会自动创建集合</span><br><span class="line">> db.kk.insert({"name" : "hhily"})</span><br></pre></td></tr></table></figure>
<h5 id="查看集合"><a href="#查看集合" class="headerlink" title="查看集合"></a>查看集合</h5><p><code>> show collections</code></p>
<h5 id="删除集合"><a href="#删除集合" class="headerlink" title="删除集合"></a>删除集合</h5><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">// 删除test数据库中叫kk的集合</span><br><span class="line">> use test</span><br><span class="line">> db.kk.drop()</span><br></pre></td></tr></table></figure>
<h5 id="插入文档"><a href="#插入文档" class="headerlink" title="插入文档"></a>插入文档</h5><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">// 在runoob数据库col集合中插入一条文档</span><br><span class="line">> use runoob</span><br><span class="line">> db.col.insert({title: 'MongoDB 教程', </span><br><span class="line"> description: 'MongoDB 是一个 Nosql 数据库',</span><br><span class="line"> by: '菜鸟教程',</span><br><span class="line"> url: 'http://www.runoob.com',</span><br><span class="line"> tags: ['mongodb', 'database', 'NoSQL'],</span><br><span class="line"> likes: 100</span><br><span class="line">})</span><br><span class="line">// 上面的插入中,如果col集合不存在,将新建</span><br></pre></td></tr></table></figure>
<h5 id="查询文档"><a href="#查询文档" class="headerlink" title="查询文档"></a>查询文档</h5><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">> db.col.find().pretty()</span><br><span class="line">// or</span><br><span class="line">> db.col.findOne().pretty()</span><br></pre></td></tr></table></figure>
<h5 id="更新文档"><a href="#更新文档" class="headerlink" title="更新文档"></a>更新文档</h5><p><code>> db.col.updateOne({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}})</code></p>
<h5 id="删除文档"><a href="#删除文档" class="headerlink" title="删除文档"></a>删除文档</h5><p><code>db.col.deleteMany({'title':'MongoDB 教程'})</code></p>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
</entry>
<entry>
<title>eosio的multi_index</title>
<url>/2018/06/05/eosio%E7%9A%84multi-index/</url>
<content><{</span><br><span class="line"> eosio::print("Liking: ", address.name.c_str(), "\n");</span><br><span class="line"> address.liked++;</span><br><span class="line"> });</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h3 id="六-工具函数"><a href="#六-工具函数" class="headerlink" title="(六). 工具函数"></a>(六). 工具函数</h3><ol>
<li>available_primary_key<br>返回一个可用(未使用)的主键值,用于主键严格自增的表,它不会被设置为自定义值<br><code>uint64_t available_primary_key() const</code></li>
</ol>
<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><ul>
<li><a href="https://bihu.com/article/328856" target="_blank" rel="noopener">https://bihu.com/article/328856</a></li>
<li><a href="https://bihu.com/article/334710" target="_blank" rel="noopener">https://bihu.com/article/334710</a></li>
</ul>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
<tags>
<tag>Blockchain</tag>
</tags>
</entry>
<entry>
<title>说说eosio.token合约</title>
<url>/2018/05/15/%E8%AF%B4%E8%AF%B4eosio-token%E5%90%88%E7%BA%A6/</url>
<content><![CDATA[<h1 id="一、eosio-token合约的请用"><a href="#一、eosio-token合约的请用" class="headerlink" title="一、eosio.token合约的请用"></a>一、eosio.token合约的请用</h1><ul>
<li>使用账户 <em>hily</em> 部署系统的 <em>eosio.token</em> 合约<blockquote>
<p>$ cleos set contract hily build/contracts/eosio.token -p hily</p>
</blockquote>
</li>
</ul>
<a id="more"></a>
<ul>
<li><p>使用账户 <em>hily</em> 创建 <em>HH</em> 币,发行总量100000个</p>
<blockquote>
<p>$ cleos push action hily create ‘[“hily”,”100000.0000 HH”,1,1,1]’ -p hily</p>
</blockquote>
</li>
<li><p>向账户 <em>hily</em> 空投100个 <em>HH</em></p>
<blockquote>
<p>$ cleos push action hily issue ‘[“hily”,”100.0000 HH”,”memo”]’ -p hily</p>
</blockquote>
</li>
<li><p>查询账户 <em>hily</em> 的余额</p>
<blockquote>
<p>$ cleos get table hily hily accounts</p>
</blockquote>
</li>
<li><p>从账户 <em>hily</em> 转账10 <em>HH</em> 给 <em>jay</em></p>
<blockquote>
<p>$ cleos push action hily transfer ‘[“hily”,”jay”,”10.0000 HH”,”memo”]’ -p hily</p>
</blockquote>
</li>
<li><p>查看账户 <em>jay</em> 的余额</p>
<blockquote>
<p>$ cleos get table hily jay accounts</p>
</blockquote>
</li>
<li><p>使用 <em>hily</em> 部署 <em>token</em> 合约后,可以像上面一样, <em>hily</em> 自己创建一个 <em>HH</em> 币,同时账户 <em>jay</em> 也可以创建他的 <em>JAY</em> 币</p>
<blockquote>
<p>$ cleos push action hily create ‘[“jay”,”10000.0000 JAY”,1,1,1]’ -p hily</p>
</blockquote>
</li>
<li><p>空投 <em>jay</em> 的 <em>JAY</em> 币到 <em>jack</em> 时,要注意命令中各个账户的使用</p>
<blockquote>
<p>$ cleos push action hily issue ‘[“jack”,”20.0000 JAY”,”memo”]’ -p jay</p>
</blockquote>
</li>
<li><p>这时查看 <em>jack</em> 账户时,可以看到拥有的 <em>JAY</em> 币余额</p>
<blockquote>
<p>$ cleos get table hily jack accounts</p>
</blockquote>
</li>
<li><p>如果 <em>jack</em> 除了拥有 <em>JAY</em> 币外,还有拥有其他币,则上面的命令的结果将会显示 <em>jack</em> 不同的币的余额。</p>
</li>
</ul>
<h1 id="二、问题"><a href="#二、问题" class="headerlink" title="二、问题"></a>二、问题</h1><ul>
<li>比如上面我们将会发行10000个 <em>JAY</em> 币,并且空投了20个到 <em>jack</em> 账户,那么我们如何获得 <em>JAY</em> 币的剩余发行量(10000 - 20 = 9980)?</li>
</ul>
<h1 id="三、eosio-token源码分析"><a href="#三、eosio-token源码分析" class="headerlink" title="三、eosio.token源码分析"></a>三、eosio.token源码分析</h1><p>构造函数:使用 <em>hily</em> 账户部署 <em>token</em> 合约时( <code>cleos set contract hily build/contracts/eosio.token -p hily</code> ), <em>hily</em> 将传给 <em>self</em> ,而该 <em>self</em> 传到父类 <em>contract</em> 中时,将会存到到父类的 <em>_self</em> 属性中。</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">token( account_name self ):contract(self){}</span><br></pre></td></tr></table></figure>
<hr>
<p>create函数:创建加密币,对应到这个命令 <code>cleos push action hily create '["hily","100000.0000 HH",1,1,1]' -p hily</code> 。</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">void token::create( account_name issuer,</span><br><span class="line"> asset maximum_supply,</span><br><span class="line"> uint8_t issuer_can_freeze,</span><br><span class="line"> uint8_t issuer_can_recall,</span><br><span class="line"> uint8_t issuer_can_whitelist )</span><br><span class="line">{</span><br><span class="line"> // 这里的 _self 就是部署合约时的 hily ,所以上面的 create 命令需要加上 "-p hily"</span><br><span class="line"> require_auth( _self );</span><br><span class="line"></span><br><span class="line"> auto sym = maximum_supply.symbol;</span><br><span class="line"> eosio_assert( sym.is_valid(), "invalid symbol name" ); // 这里检查代币符合是否合法,比如上面的"HH"</span><br><span class="line"> eosio_assert( maximum_supply.is_valid(), "invalid supply");</span><br><span class="line"> eosio_assert( maximum_supply.amount > 0, "max-supply must be positive"); // 这里检查代币的数量必须大于0,比如上面是100000.0000</span><br><span class="line"></span><br><span class="line"> stats statstable( _self, sym.name() );</span><br><span class="line"> auto existing = statstable.find( sym.name() ); // 根据代币的符号,比如上面命令中的"HH",在合约中进行查找</span><br><span class="line"> eosio_assert( existing == statstable.end(), "token with symbol already exists" ); // 保证在这个合约中之前没有创建过这样的代币("HH")</span><br><span class="line"></span><br><span class="line"> // 把参数存放到statstable中</span><br><span class="line"> statstable.emplace( _self, [&]( auto& s ) {</span><br><span class="line"> s.supply.symbol = maximum_supply.symbol;</span><br><span class="line"> s.max_supply = maximum_supply;</span><br><span class="line"> s.issuer = issuer;</span><br><span class="line"> s.can_freeze = issuer_can_freeze;</span><br><span class="line"> s.can_recall = issuer_can_recall;</span><br><span class="line"> s.can_whitelist = issuer_can_whitelist;</span><br><span class="line"> });</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<hr>
<p>issue:空投或者发币<code>cleos push action hily issue ‘[“jack“,”20.0000 JAY”,”memo”]’ -p jay</code></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">void token::issue( account_name to, asset quantity, string memo )</span><br><span class="line">{</span><br><span class="line"> print( "issue" );</span><br><span class="line"> auto sym = quantity.symbol;</span><br><span class="line"> eosio_assert( sym.is_valid(), "invalid symbol name" ); // 首先确保正确的代币</span><br><span class="line"></span><br><span class="line"> auto sym_name = sym.name();</span><br><span class="line"> stats statstable( _self, sym_name );</span><br><span class="line"> auto existing = statstable.find( sym_name );</span><br><span class="line"> eosio_assert( existing != statstable.end(), "token with symbol does not exist, create token before issue" ); // 确保该代币,比如"JAY",是存在的</span><br><span class="line"> const auto& st = *existing;</span><br><span class="line"></span><br><span class="line"> require_auth( st.issuer ); // 通过代币"JAY"找到对应的发行者是"jay",所以上面的issue需要加上"-p jay"</span><br><span class="line"> eosio_assert( quantity.is_valid(), "invalid quantity" );</span><br><span class="line"> eosio_assert( quantity.amount > 0, "must issue positive quantity" );</span><br><span class="line"></span><br><span class="line"> eosio_assert( quantity.symbol == st.supply.symbol, "symbol precision mismatch" );</span><br><span class="line"> eosio_assert( quantity.amount <= st.max_supply.amount - st.supply.amount, "quantity exceeds available supply"); // 确保空投的数量不能超过剩余的可发行量</span><br><span class="line"></span><br><span class="line"> statstable.modify( st, 0, [&]( auto& s ) {</span><br><span class="line"> s.supply += quantity;</span><br><span class="line"> });</span><br><span class="line"></span><br><span class="line"> // 这里有点意思,你会发现在这里jay账户会增加20 JAY,虽然上面的命令是空投20 JAY到jack账户</span><br><span class="line"> add_balance( st.issuer, quantity, st, st.issuer );</span><br><span class="line"></span><br><span class="line"> // 到这里的时候,因为to是jack,st.issue是jay,所以不相等</span><br><span class="line"> if( to != st.issuer ) {</span><br><span class="line"> // 这里call transfer的时候,就会先call sub_balance然后call sub_balance</span><br><span class="line"> // 实作就是把jay账户减去20(因为上面加了20到jay,而实际需要加到jack的),然后把jack账户增加20 JAY</span><br><span class="line"> SEND_INLINE_ACTION( *this, transfer, {st.issuer,N(active)}, {st.issuer, to, quantity, memo} );</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<hr>
<p>transfer:转账<code>cleos push action hily transfer ‘[“jay”,”jack”,”10.0000 HH”,”memo”]’ -p jay</code></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">void token::transfer( account_name from,</span><br><span class="line"> account_name to,</span><br><span class="line"> asset quantity,</span><br><span class="line"> string /*memo*/ )</span><br><span class="line">{</span><br><span class="line"> print( "transfer from ", eosio::name{from}, " to ", eosio::name{to}, " ", quantity, "\n" );</span><br><span class="line"> eosio_assert( from != to, "cannot transfer to self" ); // 杜绝自己给自己转账,每个人都这么干将会造成EOS拥堵</span><br><span class="line"> require_auth( from ); // 在这里就可以知道上面的命令为什么加上"-p jay"了</span><br><span class="line"> eosio_assert( is_account( to ), "to account does not exist"); // 检查账户合法性</span><br><span class="line"> auto sym = quantity.symbol.name();</span><br><span class="line"> stats statstable( _self, sym );</span><br><span class="line"> const auto& st = statstable.get( sym );</span><br><span class="line"></span><br><span class="line"> require_recipient( from );</span><br><span class="line"> require_recipient( to );</span><br><span class="line"></span><br><span class="line"> // 检查转账的数量和代币正常性</span><br><span class="line"> eosio_assert( quantity.is_valid(), "invalid quantity" );</span><br><span class="line"> eosio_assert( quantity.amount > 0, "must transfer positive quantity" );</span><br><span class="line"> eosio_assert( quantity.symbol == st.supply.symbol, "symbol precision mismatch" );</span><br><span class="line"></span><br><span class="line"> // 从from账户中减去指定的币额并加到to账户</span><br><span class="line"> sub_balance( from, quantity, st );</span><br><span class="line"> add_balance( to, quantity, st, from );</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
<tags>
<tag>Blockchain</tag>
</tags>
</entry>
<entry>
<title>nodeos与config.ini</title>
<url>/2018/05/08/nodeos%E4%B8%8Econfig-ini/</url>
<content><![CDATA[<p>可能我们会通过这样的命令启动eos节点:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ nodeos -e -p eosio --plugin eosio::wallet_api_plugin --plugin eosio::chain_api_plugin --plugin eosio::account_history_api_plugin</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<p>如果不想每次启动都输入这么多参数,则可以通过配置对应的config.ini文件:</p>
<ul>
<li>“-e”对应的修改config.ini文件中<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"># Enable block production, even if the chain is stale. (eosio::producer_plugin)</span><br><span class="line">enable-stale-production = true</span><br></pre></td></tr></table></figure></li>
<li>“-p eosio”对应<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"># ID of producer controlled by this node (e.g. inita; may specify multiple times) (eosio::producer_plugin)</span><br><span class="line">producer-name = eosio</span><br></pre></td></tr></table></figure></li>
<li>“–plugin eosio::wallet_api_plugin –plugin eosio::chain_api_plugin –plugin eosio::account_history_api_plugin”对应<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"># Plugin(s) to enable, may be specified multiple times</span><br><span class="line">plugin = eosio::producer_plugin</span><br><span class="line">plugin = eosio::wallet_api_plugin</span><br><span class="line">plugin = eosio::chain_api_plugin</span><br><span class="line">plugin = eosio::account_history_api_plugin</span><br><span class="line">plugin = eosio::http_plugin</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>配置好config文件,以后启动节点只需要nodeos命令就好了,不需要参数:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ nodeos</span><br></pre></td></tr></table></figure>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
<tags>
<tag>Blockchain</tag>
</tags>
</entry>
<entry>
<title>合约编写部署和执行</title>
<url>/2018/05/07/%E5%90%88%E7%BA%A6%E7%BC%96%E5%86%99%E9%83%A8%E7%BD%B2%E5%92%8C%E6%89%A7%E8%A1%8C/</url>
<content><![CDATA[<p>假设你已经安装了EOSIO并且启动了nodeos。</p>
<h2 id="创建钱包"><a href="#创建钱包" class="headerlink" title="创建钱包"></a>创建钱包</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ cleos wallet create -n mywallet</span><br><span class="line">Creating wallet: mywallet</span><br><span class="line">Save password to use in the future to unlock this wallet.</span><br><span class="line">Without password imported keys will not be retrievable.</span><br><span class="line">"PW5KTrGBhmMeDAZxM69PgZxcHXaT2aaebFRWriZq5PDshPUaowpJF"</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<p>使用钱包之前,需要解锁钱包:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ cleos wallet unlock --password PW5KTrGBhmMeDAZxM69PgZxcHXaT2aaebFRWriZq5PDshPUaowpJF</span><br><span class="line">Unlocked: default</span><br></pre></td></tr></table></figure>
<p>上面的命令你的密码将会记录到shell历史中,更安全的,你可以通过下面的方法解锁:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ cleos wallet unlock</span><br><span class="line">password:</span><br></pre></td></tr></table></figure>
<p>你不使用它的时候,锁定它是一个良好的习惯:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ cleos wallet lock</span><br><span class="line">Locked: default</span><br></pre></td></tr></table></figure>
<h2 id="Bios关键的系统合约"><a href="#Bios关键的系统合约" class="headerlink" title="Bios关键的系统合约"></a>Bios关键的系统合约</h2><p>EOS相当于一个操作系统,而Bios就是这个系统的引导。进入到EOSIO的source code root目录下:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ cleos set contract eosio build/contracts/eosio.bios -p eosio</span><br><span class="line">Reading WAST...</span><br><span class="line">Assembling WASM...</span><br><span class="line">Publishing contract...</span><br><span class="line">executed transaction: 414cf0dc7740d22474992779b2416b0eabdbc91522c16521307dd682051af083 4068 bytes 10000 cycles</span><br><span class="line"># eosio <= eosio::setcode {"account":"eosio","vmtype":0,"vmversion":0,"code":"0061736d0100000001ab011960037f7e7f0060057f7e7e7e...</span><br><span class="line"># eosio <= eosio::setabi {"account":"eosio","abi":{"types":[],"structs":[{"name":"set_account_limits","base":"","fields":[{"n...</span><br></pre></td></tr></table></figure>
<h2 id="创建账户"><a href="#创建账户" class="headerlink" title="创建账户"></a>创建账户</h2><p>首先需要为账户创建一个key:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ cleos create key</span><br><span class="line">Private key: 5JiconQnk5WTpAbX5zcfkgXHhuki6Yy2U2zYKQmL5ovLUKprANN</span><br><span class="line">Public key: EOS6oPBCtWDzeRBxFMHZebxTgHFFybYqurt8GLwMSHabEkZk4tYku</span><br></pre></td></tr></table></figure>
<p>然后把key导入钱包:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ cleos wallet import 5JiconQnk5WTpAbX5zcfkgXHhuki6Yy2U2zYKQmL5ovLUKprANN -n mywallet</span><br><span class="line">imported private key for: EOS6oPBCtWDzeRBxFMHZebxTgHFFybYqurt8GLwMSHabEkZk4tYku</span><br></pre></td></tr></table></figure>
<p>通过该钱包创建两个账户 <em>tester1</em> 和 <em>tester2</em> :</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ cleos create account eosio tester1 EOS6oPBCtWDzeRBxFMHZebxTgHFFybYqurt8GLwMSHabEkZk4tYku EOS6oPBCtWDzeRBxFMHZebxTgHFFybYqurt8GLwMSHabEkZk4tYku</span><br><span class="line">executed transaction: 49b5aad108995ce096a840f2537376ae4cfadc57e06af7b1f435ae4e291991d7 352 bytes 102400 cycles</span><br><span class="line"># eosio <= eosio::newaccount {"creator":"eosio","name":"tester1","owner":{"threshold":1,"keys":[{"key":"EOS6oPBCtWDzeRBxFMHZebxTg...</span><br></pre></td></tr></table></figure>
<p>同理,把”tester1”改为”tester2”即可创建 <em>tester2</em> 账户。</p>
<h2 id="一个简单的合约"><a href="#一个简单的合约" class="headerlink" title="一个简单的合约"></a>一个简单的合约</h2><p>建立一个文件夹命名为hello,并且建立一个hello.cpp文件:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ mkdir hello</span><br><span class="line">$ cd hello</span><br><span class="line">$ touch hello.cpp</span><br></pre></td></tr></table></figure>
<p>hello.cpp中输入以下内容:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#include <eosiolib/eosio.hpp></span><br><span class="line">#include <eosiolib/print.hpp></span><br><span class="line">using namespace eosio;</span><br><span class="line"></span><br><span class="line">class hello : public eosio::contract {</span><br><span class="line"> public:</span><br><span class="line"> using contract::contract;</span><br><span class="line"></span><br><span class="line"> /// @abi action</span><br><span class="line"> void hi( account_name user ) {</span><br><span class="line"> print( "Hello, ", name{user} );</span><br><span class="line"> }</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line">EOSIO_ABI( hello, (hi) )</span><br></pre></td></tr></table></figure>
<p>编译出wast和abi文件,过程中可能会出现warning,可以暂时不需要理会:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ eosiocpp -o hello.wast hello.cpp</span><br><span class="line">$ eosiocpp -g hello.abi hello.cpp</span><br></pre></td></tr></table></figure>
<p>接下来通过下面命令部署合约:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ cleos set contract tester1 ../hello -p tester1</span><br></pre></td></tr></table></figure>
<p>现在,我们试着运行一下合约:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">$ cleos push action tester1 hi '["tester2"]' -p tester2</span><br><span class="line">executed transaction: 28d92256c8ffd8b0255be324e4596b7c745f50f85722d0c4400471bc184b9a16 244 bytes 1000 cycles</span><br><span class="line"># tester1 <= tester1::hi {"user":"tester2"}</span><br><span class="line">>> Hello, tester2</span><br></pre></td></tr></table></figure>
<p>“-p tester2”表示赋予 <em>tester2</em> 权限。</p>
<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><ul>
<li><a href="https://github.com/EOSIO/eos/wiki" target="_blank" rel="noopener">https://github.com/EOSIO/eos/wiki</a></li>
</ul>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
<tags>
<tag>Blockchain</tag>
</tags>
</entry>
<entry>
<title>EOSIO宪法草案</title>
<url>/2018/05/03/EOSIO%E5%AE%AA%E6%B3%95%E8%8D%89%E6%A1%88/</url>
<content><![CDATA[<ul>
<li><p><a href="https://forums.eosgo.io/discussion/424/design-principles-of-my-v0-1-draft-eos-io-constitution" target="_blank" rel="noopener">设计原则</a></p>
</li>
<li><p><a href="https://forums.eosgo.io/discussion/432/article-i-v0-1-draft-eos-io-constitution-no-lying" target="_blank" rel="noopener">第一条–不说谎</a>(EOS用户不得因为利益故意伪造或进行误导性陈述,这种行为将是违宪的并且将是可控告的)</p>
</li>
<li><p><a href="https://forums.eosgo.io/discussion/595/article-ii-v0-3-0-draft-eos-io-constitution-property-rights" target="_blank" rel="noopener">第二条–产权</a>(EOS将是一个有产权定义的区块链或社区,在链上或仲裁程序的范围内简历个人不可侵犯的财产权)</p>
</li>
<li><p><a href="https://forums.eosgo.io/discussion/630/article-iii-v0-3-0-draft-eos-io-constitution-arbitration" target="_blank" rel="noopener">第三条–仲裁</a>(EOS将提供一个“治理区块链”,通过有约束力的仲裁解决纠纷)</p>
</li>
</ul>
<a id="more"></a>
<ul>
<li><a href="https://forums.eosgo.io/discussion/646/article-iv-v0-3-0-draft-eos-io-constitution-no-buying-or-selling-of-votes" target="_blank" rel="noopener">第四条–选民独立</a>(令牌持有者可以选择块生产者,选民选举必须公平公正,不得因为某些利益而选择不恰当的块生产者)</li>
</ul>
<ul>
<li><p><a href="https://forums.eosgo.io/discussion/650/article-v-v0-3-0-draft-eos-io-constitution-no-owner-or-fiduciary" target="_blank" rel="noopener">第五条–无业主或受托人</a>(EOS不属于任何人或任何组织,EOS只忠于EOS宪法)</p>
</li>
<li><p><a href="https://forums.eosgo.io/discussion/651/article-vi-v0-3-0-draft-eos-io-constitution-10-ownership-cap" target="_blank" rel="noopener">第六条–10%所以权上限</a>(因为EOS不属于任何人或组织,也为了不被大户控制,任何成员或受益人权益均不得超过已发行代币的10%)</p>
</li>
<li><p><a href="https://forums.eosgo.io/discussion/718/article-vii-v0-3-0-draft-eos-io-constitution-agreement-to-penalties" target="_blank" rel="noopener">第七条–处罚协议</a>(会员应该同意对于违反宪法的处罚)</p>
</li>
<li><p><a href="https://forums.eosgo.io/discussion/719/article-viii-v0-3-0-draft-eos-io-constitution-block-producer-agreement" target="_blank" rel="noopener">第八条–区块生产协议</a>(将会被设计为一个要么同意它要么离开它的协议,记录着会员希望生产者做的事情和不应该做的事情,未同意该协议前不得作为生产者)</p>
</li>
<li><p><a href="https://forums.eosgo.io/discussion/745/article-ix-v0-3-0-draft-eos-io-constitution-establishes-arbitration-forums" target="_blank" rel="noopener">第九条–设立仲裁论坛</a>(仲裁员需要在非常严格的约束力下行使权力,并且描述了一个仲裁法庭的组成和被授权审理案件的过程)</p>
</li>
<li><p><a href="https://forums.eosgo.io/discussion/746/article-x-v0-3-0-draft-eos-io-constitution-arbitrator-standards" target="_blank" rel="noopener">第十条–仲裁员标准</a>(成为仲裁员的最低标准)</p>
</li>
<li><p><a href="https://forums.eosgo.io/discussion/747/article-xi-v0-3-0-draft-eos-io-constitution-developers-and-smart-contract-licenses" target="_blank" rel="noopener">第十一条–开发人员和智能合同许可</a>(提供智能合约的成员都是开发人员,应通过许可证来提供他们的合约,并且给合约指定一个仲裁法庭)</p>
</li>
<li><p><a href="https://forums.eosgo.io/discussion/748/article-xii-v0-3-0-draft-eos-io-constitution-multilingual-contracts" target="_blank" rel="noopener">第十二条–多种语言合同</a>(如有纠纷,多语言合约应指明主导语言,如果作者没有指定,则由处理纠纷的仲裁员选择)</p>
</li>
<li><p><a href="https://forums.eosgo.io/discussion/749/article-xiii-v0-3-0-draft-eos-io-constitution-developers-responsible-for-non-member-access" target="_blank" rel="noopener">第十三条–负责非会员访问的开发者</a>(由于开发者可以通过他的应用程序向非成员提供服务和链上互动,因此开发者应担全责保证非成员的互动是符合宪法的)</p>
</li>
<li><p><a href="https://forums.eosgo.io/discussion/750/article-xiv-v0-3-0-draft-eos-io-constitution-no-positive-rights" target="_blank" rel="noopener">第十四条–没有正面的权利</a>(宪法指出不会为任何成员或成员之间创建积极权利)</p>
</li>
<li><p><a href="https://forums.eosgo.io/discussion/751/article-xv-v0-3-0-draft-eos-io-constitution-default-arbitration-forum-named" target="_blank" rel="noopener">第十五条–默认仲裁论坛命名</a>(为没有明确指定仲裁法庭来处理纠纷的合约指定默认仲裁法庭)</p>
</li>
</ul>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
<tags>
<tag>Blockchain</tag>
</tags>
</entry>
<entry>
<title>Setting Up A Local Environment Of EOS.IO</title>
<url>/2018/05/03/Setting-Up-A-Local-Environment-Of-EOS-IO/</url>
<content><![CDATA[<p>eos.io的本地环境的搭建主要是参考eos.io在GitHub的<a href="https://github.com/EOSIO/eos/wiki/Local-Environment" target="_blank" rel="noopener">Wiki</a>,同时记录一下搭建过程中遇到的坑。<a id="more"></a></p>
<h2 id="Getting-the-Code"><a href="#Getting-the-Code" class="headerlink" title="Getting the Code"></a>Getting the Code</h2><p>首先需要获得eos.io的source code,通过以下的命令clone到本地:<br><code>git clone https://github.com/EOSIO/eos --recursive</code><br>命令行的<em>–recursive</em>这个flag确保克隆eos仓库和它的子模块,如果clone时没有使用这个flag,则通过下面的命令也可以取得子模块:<br><code>git submodule update --init --recursive</code></p>
<h2 id="Building-EOSIO"><a href="#Building-EOSIO" class="headerlink" title="Building EOSIO"></a>Building EOSIO</h2><h4 id="Automated-build-script"><a href="#Automated-build-script" class="headerlink" title="Automated build script"></a>Automated build script</h4><p>最简单的方法是使用eos的自动编译脚本,自动编译脚本支持以下的系统:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">1. Amazon 2017.09 and higher.</span><br><span class="line">2. Centos 7.</span><br><span class="line">3. Fedora 25 and higher (Fedora 27 recommended).</span><br><span class="line">4. Mint 18.</span><br><span class="line">5. Ubuntu 16.04 (Ubuntu 16.10 recommended).</span><br><span class="line">6. MacOS Darwin 10.12 and higher (MacOS 10.13.x recommended).</span><br></pre></td></tr></table></figure>
<h4 id="System-Requirements-all-platforms"><a href="#System-Requirements-all-platforms" class="headerlink" title="System Requirements (all platforms)"></a>System Requirements (all platforms)</h4><p>同时,所有的操作平台,需要满足:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">1. 8GB的可用内存</span><br><span class="line">2. 20GB的可用磁盘</span><br></pre></td></tr></table></figure>
<p>如果内存不满足,脚本将会给出提示并且终止当前编译。</p>
<h4 id="Run-the-build-script"><a href="#Run-the-build-script" class="headerlink" title="Run the build script"></a>Run the build script</h4><p><code>cd eos</code><br><code>./eosio_build.sh</code><br>脚本会check系统的环境,如果某些依赖不存在,脚本会提示进行安装。<br>如下是博主在macOS Darwin 10.13.4上运行脚本后出现的依赖不存在情况:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">Checking dependencies.</span><br><span class="line">Checking automake ... automake NOT found.</span><br><span class="line">Checking Libtool ... Libtool NOT found.</span><br><span class="line">Checking OpenSSL ... OpenSSL NOT found.</span><br><span class="line">Checking llvm ... llvm NOT found.</span><br><span class="line">Checking wget ... wget NOT found.</span><br><span class="line">Checking CMake ... CMake NOT found.</span><br><span class="line">Checking GMP ... GMP NOT found.</span><br><span class="line">Checking gettext ... gettext NOT found.</span><br><span class="line">Checking MongoDB ... MongoDB NOT found.</span><br><span class="line">Checking Doxygen ... Doxygen NOT found.</span><br><span class="line">Checking Graphviz ... Graphviz NOT found.</span><br><span class="line">Checking LCOV ... LCOV NOT found.</span><br><span class="line">Checking Python3 ... python3 NOT found.</span><br><span class="line"></span><br><span class="line">The following dependencies are required to install EOSIO.</span><br><span class="line"></span><br><span class="line">1. automake</span><br><span class="line">2. Libtool</span><br><span class="line">3. OpenSSL</span><br><span class="line">4. llvm</span><br><span class="line">5. wget</span><br><span class="line">6. CMake</span><br><span class="line">7. GMP</span><br><span class="line">8. gettext</span><br><span class="line">9. MongoDB</span><br><span class="line">10. Doxygen</span><br><span class="line">11. Graphviz</span><br><span class="line">12. LCOV</span><br><span class="line">13. Python 3</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Do you wish to install these packages?</span><br><span class="line">1) Yes</span><br><span class="line">2) No</span><br><span class="line">#?</span><br></pre></td></tr></table></figure>
<p>build seccess将会出现以下画面:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line"> _______ _______ _______ _________ _______</span><br><span class="line">( ____ \( ___ )( ____ \\__ __/( ___ )</span><br><span class="line">| ( \/| ( ) || ( \/ ) ( | ( ) |</span><br><span class="line">| (__ | | | || (_____ | | | | | |</span><br><span class="line">| __) | | | |(_____ ) | | | | | |</span><br><span class="line">| ( | | | | ) | | | | | | |</span><br><span class="line">| (____/\| (___) |/\____) |___) (___| (___) |</span><br><span class="line">(_______/(_______)\_______)\_______/(_______)</span><br><span class="line"></span><br><span class="line">EOS.IO has been successfully built. 1:6:15</span><br><span class="line"></span><br><span class="line">To verify your installation run the following commands:</span><br><span class="line"></span><br><span class="line">/usr/local/bin/mongod -f /usr/local/etc/mongod.conf &</span><br><span class="line">cd /Users/lauyearh/Blockchain/EOS/eos/build; make test</span><br><span class="line"></span><br><span class="line">For more information:</span><br><span class="line">EOS.IO website: https://eos.io</span><br><span class="line">EOS.IO Telegram channel @ https://t.me/EOSProject</span><br><span class="line">EOS.IO resources: https://eos.io/resources/</span><br><span class="line">EOS.IO wiki: https://github.com/EOSIO/eos/wiki</span><br></pre></td></tr></table></figure>
<h4 id="Build-validation"><a href="#Build-validation" class="headerlink" title="Build validation"></a>Build validation</h4><p>可以通过一组测试用例,验证你的编译。<br>如果是在Linux平台:<br><code>~/opt/mongodb/bin/mongod -f ~/opt/mongodb/mongod.conf &</code><br>如果是在macOS:<br><code>/usr/local/bin/mongod -f /usr/local/etc/mongod.conf &</code><br>然后执行以下命令,不管在Linux还是macOS:<br><code>cd build</code><br><code>make test</code><br>最后出现这样的提示说明测试通过:</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">100% tests passed, 0 tests failed out of 33</span><br><span class="line"></span><br><span class="line">Total Test time (real) = 806.70 sec</span><br></pre></td></tr></table></figure>
<h4 id="Install-the-executables"><a href="#Install-the-executables" class="headerlink" title="Install the executables"></a>Install the executables</h4><p>通过安装可执行档案,可以在系统中方便操作eos,相当于把cmd写到PATH变量中,但是其实是把内容安装到/usr/local:<br><code>cd build</code><br><code>sudo make install</code></p>
<h2 id="Creating-and-Launching-a-Single-Node-Testnet"><a href="#Creating-and-Launching-a-Single-Node-Testnet" class="headerlink" title="Creating and Launching a Single Node Testnet"></a>Creating and Launching a Single Node Testnet</h2><p>运行下面的命令,将启动一个简单的节点:<br><code>cd build/programs/nodeos</code><br><code>./nodeos -e -p eosio --plugin eosio::wallet_api_plugin --plugin eosio::chain_api_plugin --plugin eosio::account_history_api_plugin</code><br>这个时候,默认的config路径,在macOS上是<em>$HOME/Library/Application Support/eosio/nodeos/config</em>,里面包含<em>config.ini</em>和<em>genesis.json</em>两个文件。当然,你也可以在启动nodeos时通过<em>--config-dir</em>指定<em>config</em>路径,并且需要建立ini和json两个文件。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>博主build时用的是macOS,同时系统配置满足要求,使用自动编译脚本的时候没遇到什么坑,顺利build通过。Source code是<em>master</em> branch,commit id是<em>19227c9b0a095683c1df3668c110cef2d91cbb15</em>。</p>
<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><ul>
<li><a href="https://github.com/EOSIO/eos/wiki/Local-Environment" target="_blank" rel="noopener">https://github.com/EOSIO/eos/wiki/Local-Environment</a></li>
</ul>
]]></content>
<categories>
<category>攻城狮那些事儿</category>
</categories>
<tags>
<tag>Blockchain</tag>
</tags>
</entry>
<entry>
<title>说说以太猫</title>
<url>/2018/04/27/%E8%AF%B4%E8%AF%B4%E4%BB%A5%E5%A4%AA%E7%8C%AB/</url>
<content><![CDATA[<p>随着区块链的热门,基于以太坊的第一个有代表性的游戏项目出现了,这就是CryptoKitties,中文名谜恋猫。<a id="more"></a></p>
<p>类似于QQ宠物,谜恋猫需要喂养,可以进行交配并繁育处下一代猫咪。基于256色域作为基因图谱,部署到以太坊上,每一只猫都是独一无二的(颜色、形状等)。谜恋猫最初由背后团队发布500000只作为拍卖,称为初代(GEN 0)猫,然后这些猫交配繁育就会出现GEN 1,GEN 2等。如果你账户下有多只猫,那么可以让他们繁育,然后进行拍卖。当然GEN 0猫是最贵的,一些稀有属性的品种,比如繁育速度快、冷却时间(繁育后需要休息一段时间再进行下一次繁育)短、颜色或形状奇特的猫也是比较贵的,最贵的一只猫卖到了12万美金,真是名副其实的钻石猫。</p>
<p>最开始发布的时候,以太猫的火热程度甚至一度造成以太坊网络的拥堵,而最开始的撸猫党,也是发了一笔横财。天价的猫咪不确定是不是背后团队有炒作,但是不可否认人家没有ICO照样把钱给募了,也许这就是站在风口,猫也会飞。</p>