鍖哄潡閾炬櫤鑳藉悎绾﹁鏀诲嚮浜嗘€庝箞鍔?/a>

鏅鸿兘鍚堢害鏄姩鎬佺殑锛屽鏉傜殑浠ュ強闅句互缃俊鍦板己澶с€傝櫧鐒朵粬浠殑娼滃姏鏄緢闅炬兂璞★紝浣嗘槸涔熶笉鍙兘涓€澶滀箣闂村氨鎴愪负浜嗘敾鍑荤殑瀵硅薄銆?/p>

濡傛灉浣犲湪鏁板瓧璐у竵涓栫晫寰呰繃瓒冲鏃堕棿锛屼篃璁镐綘鍚杩?鎴?涓櫤鑳藉悎绾︽敾鍑绘椂闂达紝杩欎簺鏀诲嚮瀵艰嚧浜嗗嚑鍗冧竾缇庡厓鐨勭洍绐冩崯澶便€傛渶钁楀悕鐨勬敾鍑绘槸DAO浜嬩欢锛岃繖鏄暟瀛楄揣甯佷笘鐣屾渶鍙楁湡寰呯殑椤圭洰涔嬩竴锛屽悓鏃朵篃鏄櫤鑳藉悎绾︾殑鏀归潻銆傝櫧鐒跺緢澶氫汉鍚杩囪繖浜涙敾鍑伙紝浣嗘槸寰堝皯浜虹煡閬撳埌搴曞彂鐢熶簡浠€涔堬紝鏄€庝箞鍙戠敓鐨勶紝浠ュ強濡備綍閬垮厤杩欎簺閿欒銆?span style="display:none">aMG鏄惧崱涔嬪

鏅鸿兘鍚堢害鏄姩鎬佺殑锛屽鏉傜殑浠ュ強闅句互缃俊鍦板己澶с€傝櫧鐒朵粬浠殑娼滃姏鏄緢闅炬兂璞★紝浣嗘槸涔熶笉鍙兘涓€澶滀箣闂村氨鎴愪负浜嗘敾鍑荤殑瀵硅薄銆備篃灏辨槸璇达紝瀵逛簬寰€鍚庣殑鏁板瓧璐у竵锛屾垜浠彲浠ヤ粠涔嬪墠鐨勯敊璇腑瀛﹀埌缁忛獙锛岀劧鍚庝竴璧锋垚闀裤€傝櫧鐒禗AO鏄凡缁忓彂鐢熺殑浜嬫儏锛屼絾鏄繖瀵逛簬寮€鍙戣€咃紝鎶曡祫鑰咃紝浠ュ強绀惧尯鎴愬憳瀵逛簬鏅鸿兘鍚堢害鏀诲嚮鏉ヨ锛岄兘鏄竴涓緢濂界殑渚嬪瓙銆?span style="display:none">aMG鏄惧崱涔嬪

浠婂ぉ锛屾垜鎯冲拰澶у鑱婅亰浠嶥AO浜嬩欢涓紝鎴戜滑瀛﹀埌鐨?浠朵簨銆?span style="display:none">aMG鏄惧崱涔嬪

鏀诲嚮锛?锛氶噸鍏ユ敾鍑?span style="display:none">aMG鏄惧崱涔嬪

褰撴敾鍑昏€呴€氳繃瀵圭洰鏍囪皟鐢ㄦ彁娆炬搷浣滅殑鏃跺€欙紝閲嶅叆鏀诲嚮灏变細鍙戠敓锛屽氨濂藉儚DAO浜嬩欢涓€鏍枫€傚綋鍚堢害涓嶈兘鍦ㄥ彂鍑鸿祫閲戜箣鍓嶆洿鏂扮姸鎬侊紙鐢ㄦ埛浣欓锛夛紝鏀诲嚮鑰呭氨鍙互杩炵画杩涜鎻愬彇鍑芥暟璋冪敤锛屾潵鑾峰緱鍚堢害涓殑璧勯噾銆備换浣曟椂鍊欐敾鍑昏€呰幏寰椾互澶竵锛屼粬鐨勫悎绾﹂兘浼氳嚜鍔ㄥ湴璋冪敤鍙嶉鍑芥暟锛宖unction 锛堬級锛岃繖灏卞啀娆¤皟鐢ㄤ簡鎻愮幇鍚堢害銆傝繖鏃跺€欙紝鏀诲嚮灏变細杩涘叆閫掑綊鍥炶矾锛岃繖鏃跺€欒繖涓悎绾︿腑鐨勮祫閲戝氨浼氳浆鍏ユ敾鍑昏€呫€傚洜涓虹洰鏍囧悎绾﹂兘鍦ㄤ笉鍋滃湴璋冪敤鏀诲嚮鑰呯殑鍑芥暟锛岃繖涓悎绾︿篃涓嶄細鏇存柊鏀诲嚮鑰呯殑浣欓銆傚綋鍓嶇殑鍚堢害涓嶄細鍙戠幇鏈変换浣曢棶棰橈紝鏇存竻妤氬湴璇达紝鍚堢害鍑芥暟涓寘鍚弽棣堝嚱鏁帮紝褰撳悎绾︽敹鍒颁互澶竵鍜岄浂鏁版嵁鐨勬椂鍊欙紝鍚堢害鍑芥暟灏变細鑷姩鎵ц銆?span style="display:none">aMG鏄惧崱涔嬪

鏀诲嚮娴佺▼aMG鏄惧崱涔嬪

1锛庢敾鍑昏€呭皢浠ュお甯佸瓨鍏ョ洰鏍囧嚱鏁?span style="display:none">aMG鏄惧崱涔嬪

2锛庣洰鏍囧嚱鏁板氨浼氭牴鎹瓨鍏ョ殑浠ュお甯佽€屾洿鏂版敾鍑昏€呯殑绾?span style="display:none">aMG鏄惧崱涔嬪

3锛庢敾鍑昏€呰姹傛嬁鍥炶祫閲?span style="display:none">aMG鏄惧崱涔嬪

4锛庤祫閲戝氨浼氶€€鍥?span style="display:none">aMG鏄惧崱涔嬪

5锛庢敾鍑昏€呯殑鍙嶉鍑芥暟鐢熸晥锛岀劧鍚庤皟鐢ㄦ彁鐜板姛鑳?span style="display:none">aMG鏄惧崱涔嬪

6锛庢櫤鑳藉悎绾︾殑閫昏緫灏变細鏇存柊鏀诲嚮鑰呯殑浣欓锛屽洜涓烘彁鐜板張琚垚鍔熻皟鐢?span style="display:none">aMG鏄惧崱涔嬪

7锛庤祫閲戝彂閫佸埌鏀诲嚮鑰?span style="display:none">aMG鏄惧崱涔嬪

8锛庣5锛?姝ラ噸澶嶄娇鐢?span style="display:none">aMG鏄惧崱涔嬪

9锛庝竴鏃︽敾鍑荤粨鏉燂紝鏀诲嚮鑰呭氨浼氭妸璧勯噾浠庝粬浠嚜宸辩殑鍚堢害鍙戦€佸埌涓汉鍦板潃aMG鏄惧崱涔嬪

1锛奤eDgMZo2n0skHzgkl352zQaMG鏄惧崱涔嬪

閲嶅叆鏀诲嚮鐨勯€掑綊鍥炶矾aMG鏄惧崱涔嬪

寰堜笉骞稿湴鏄紝涓€鏃﹁繖涓敾鍑诲紑濮嬶紝鏃犳硶鍋滀笅銆傛敾鍑昏€呯殑鎻愮幇鍔熻兘浼氳涓€娆℃鍦拌皟鐢紝鐩村埌鍚堢害涓殑鐕冩枡璺戝畬锛屾垨鑰呰瀹宠€呯殑浠ュお甯佷綑棰濊娑堣€楀厜銆?span style="display:none">aMG鏄惧崱涔嬪

鍖哄潡閾炬櫤鑳藉悎绾﹁鏀诲嚮浜嗘€庝箞鍔? src=aMG鏄惧崱涔嬪

浠g爜aMG鏄惧崱涔嬪

涓嬮潰灏辨槸DAO鍚堢害鐨勭畝鍗曠増鏈紝鍏朵腑浼氬寘鎷竴浜涗粙缁嶆潵涓鸿繖浜涗笉鐔熸倝浠g爜锛?solidity璇█鏇村ソ鍦扮悊瑙e悎绾︺€?span style="display:none">aMG鏄惧崱涔嬪

contract babyDAO 锝?span style="display:none">aMG鏄惧崱涔嬪

锛忥紛 assign key锛弙alue pair so we can look upaMG鏄惧崱涔嬪

credit integers with an ETH address 锛婏紡aMG鏄惧崱涔嬪

mapping 锛坅ddress 锛濓紴 uint256锛?public credit锛?span style="display:none">aMG鏄惧崱涔嬪

锛忥紛 a function for funds to be added to the contract锛?span style="display:none">aMG鏄惧崱涔嬪

sender will be credited amount sent 锛婏紡aMG鏄惧崱涔嬪

function donate锛坅ddress to锛?payable 锝?span style="display:none">aMG鏄惧崱涔嬪

credit锛籱sg锛巗ender锛?锛嬶紳 msg锛巚alue锛?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

锛忥紛show ether credited to address锛婏紡aMG鏄惧崱涔嬪

function assignedCredit锛坅ddress锛?returns 锛坲int锛?锝?span style="display:none">aMG鏄惧崱涔嬪

return credit锛籱sg锛巗ender锛斤紱aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

锛忥紛withdrawal ether from contract锛婏紡aMG鏄惧崱涔嬪

function withdraw锛坲int amount锛?锝?span style="display:none">aMG鏄惧崱涔嬪

if 锛坈redit锛籱sg锛巗ender锛?锛烇紳 amount锛?锝?span style="display:none">aMG鏄惧崱涔嬪

msg锛巗ender锛巆all锛巚alue锛坅mount锛夛紙锛夛紱aMG鏄惧崱涔嬪

credit锛籱sg锛巗ender锛?锛嶏紳 amount锛?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

濡傛灉鎴戜滑鐪嬩笅鍑芥暟withdraw锛堬級锛屾垜浠彲浠ョ湅鍒癉AO鍚堢害浣跨敤address锛巆all锛巚alue锛堬級鏉ュ彂閫佽祫閲戝埌msg锛巗ender銆備笉浠呭姝わ紝鍦ㄨ祫閲戝彂鍑哄悗锛屽悎绾︿細鏇存柊credit锛籱sg锛巗ender锛界殑鐘舵€併€傛敾鍑昏€呭湪鍙戠幇浜嗗悎绾︿唬鐮佷腑鐨勯棶棰橈紝灏辫兘澶熶娇鐢ㄧ被浼间笅闈㈢殑ThisIsAHodlUp 锝涳綕鏉ュ皢璧勯噾杞叆contract babyDAO锝涳綕鍚堢害銆?span style="display:none">aMG鏄惧崱涔嬪

import ‘browser锛廱abyDAO锛巗ol’锛?span style="display:none">aMG鏄惧崱涔嬪

contract ThisIsAHodlUp 锝?span style="display:none">aMG鏄惧崱涔嬪

锛忥紛 assign babyDAO contract as 锛俤ao锛?锛婏紡aMG鏄惧崱涔嬪

babyDAO public dao 锛?babyDAO锛?x2ae锛庯紟锛庯級锛?span style="display:none">aMG鏄惧崱涔嬪

address owner锛?span style="display:none">aMG鏄惧崱涔嬪

锛忥紛assign contract creator as owner锛婏紡aMG鏄惧崱涔嬪

constructor锛圱hisIsAHodlUp锛?public 锝?span style="display:none">aMG鏄惧崱涔嬪

owner 锛?msg锛巗ender锛?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

锛忥紛fallback function锛?withdraws funds from babyDAO锛婏紡aMG鏄惧崱涔嬪

function锛堬級 public 锝?span style="display:none">aMG鏄惧崱涔嬪

dao锛巜ithdraw锛坉ao锛巃ssignedCredit锛坱his锛夛級锛?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

锛忥紛send drained funds to attacker’s address锛婏紡aMG鏄惧崱涔嬪

function drainFunds锛堬級 payable public锝?span style="display:none">aMG鏄惧崱涔嬪

owner锛巘ransfer锛坅ddress锛坱his锛夛紟balance锛夛紱aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

闇€瑕佹敞鎰忓湴鏄紝杩欎釜鍚庨€€鍑芥暟锛宖unction锛堬級锛屼細璋冪敤DAO鎴栬€卋abyDAO锝涳綕鐨勬彁鐜板嚱鏁帮紝鏉ヤ粠鍚堢害涓洍鍙栬祫閲戙€備粠鍙︿釜鏂归潰鏉ヨ锛屽綋鏀诲嚮鑰呮兂瑕佹妸鎵€鏈夊伔绐冩潵鐨勮祫閲戣禋鍒颁粬浠殑鍦板潃锛宒rainFunds锛堬級鍔熻兘浼氳璋冪敤銆?span style="display:none">aMG鏄惧崱涔嬪

瑙e喅鏂规aMG鏄惧崱涔嬪

鐜板湪锛屾垜浠簲璇ユ竻妤氶噸鏀炬敾鍑讳細鍒╃敤涓や釜鐗瑰埆鐨勬櫤鑳藉悎绾︽紡娲炪€傜涓€涓槸褰撳悎绾︾殑鐘舵€佸湪璧勯噾鍙戝嚭涔嬪悗锛岃€屼笉鏄箣鍓嶈繘琛屾洿鏂般€傜敱浜庡湪鍙戝嚭璧勯噾鍓嶆棤娉曟洿鏂板悎绾︾姸鎬侊紝鍑芥暟灏变細鍦ㄤ腑闂磋绠楃殑鏃跺€欒鎵撴柇锛屽悎绾︿篃璁や负璧勯噾鍏跺疄杩樻病鏈夊彂鍑恒€傜浜屼釜婕忔礊灏卞綋鍚堢害閿欒鍦颁娇鐢╝ddress锛巆all锛巚alue锛堬級鏉ュ彂鍑鸿祫閲戯紝鑰屼笉鏄痑ddress锛巘ransfer锛堬級 鎴栬€?address锛巗end锛堬級銆傝繖涓や釜閮藉彈闄愪簬2300gas锛屽彧璁板綍涓€涓簨浠惰€屼笉鏄涓閮ㄨ皟鐢ㄣ€?span style="display:none">aMG鏄惧崱涔嬪

contract babyDAO锝?span style="display:none">aMG鏄惧崱涔嬪

锛庯紟锛庯紟aMG鏄惧崱涔嬪

function withdraw锛坲int amount锛?锝?span style="display:none">aMG鏄惧崱涔嬪

if 锛坈redit锛籱sg锛巗ender锛?锛烇紳 amount锛?锝?span style="display:none">aMG鏄惧崱涔嬪

credit锛籱sg锛巗ender锛?锛嶏紳 amount锛?锛忥紛 updates balance first 锛婏紡aMG鏄惧崱涔嬪

msg锛巗ender锛巗end锛坅mount锛夛紙锛夛紱 锛忥紛 send funds properly 锛婏紡aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

鏀诲嚮2锛氫笅婧㈡敾鍑?span style="display:none">aMG鏄惧崱涔嬪

铏界劧DAO鍚堢害涓嶄細璁╁彈瀹宠€呮帀鍏ヤ笅婧㈡敾鍑伙紝鎴戜滑鑳藉閫氳繃鐜版湁鐨刡abyDAO contract锝涳綕鏉ユ洿濂藉湴鐞嗚В杩欎簺鏀诲嚮涓轰粈涔堜細鍙戠敓銆?span style="display:none">aMG鏄惧崱涔嬪

棣栧厛锛屾垜浠渶瑕佺悊瑙d粈涔堟槸256鍗曚綅鍒躲€備竴涓?56鍗曚綅鍒舵槸鐢?56涓瓧鑺傜粍鎴愩€備互澶潑鐨勮櫄鎷熸満鏄娇鐢?56瀛楄妭鏉ュ畬鎴愮殑銆傚洜涓轰互澶潑铏氭嫙鏈哄彈闄愪簬256瀛楄妭鐨勫ぇ灏忥紝鎵€浠ユ暟瀛楃殑鑼冨洿鏄?鍒?锛?94锛?67锛?95 锛?2锛燂紵锛夈€傚鏋滄垜浠秴杩囪繖涓寖鍥达紝閭d箞鏁板瓧灏变細閲嶇疆鍒拌寖鍥寸殑鏈€搴曢儴锛?2锛燂紵 锛?1 锛?0锛夈€傚鏋滄垜浠綆浜庤繖涓寖鍥达紝杩欎釜鏁板瓧灏变細閲嶇疆鍒拌繖涓寖鍥寸殑椤剁锛?–1锛?22锛燂紵锛夈€?span style="display:none">aMG鏄惧崱涔嬪

褰撴垜浠粠闆朵腑鍑忓幓澶т簬闆剁殑鏁帮紝灏变細鍙戠敓涓嬫孩鏀诲嚮锛屽鑷翠竴涓柊鐨?2锛燂紵鏁伴泦銆傜幇鍦紝濡傛灉鏀诲嚮鑰呯殑浣欓鍙戠敓浜嗕笅婧紝閭d箞杩欓儴鍒嗕綑棰濆氨浼氭洿鏂帮紝浠庤€屽鑷存暣涓祫閲戣鐩椼€?span style="display:none">aMG鏄惧崱涔嬪

鏀诲嚮娴佺▼aMG鏄惧崱涔嬪

鏀诲嚮鑰呴€氳繃鍙戝嚭1Wei鍒扮洰鏍囧悎绾︼紝鏉ュ惎鍔ㄦ敾鍑汇€?span style="display:none">aMG鏄惧崱涔嬪

鍚堢害璁よ瘉鍙戝嚭璧勯噾鐨勪汉aMG鏄惧崱涔嬪

闅忓悗璋冪敤1Wei鐨勬彁鐜板嚱鏁?span style="display:none">aMG鏄惧崱涔嬪

鍚堢害浼氫粠鍙戦€佽€呯殑璐︽埛鎵i櫎鐨?Wei锛岀幇鍦ㄨ处鎴蜂綑棰濆張鏄浂aMG鏄惧崱涔嬪

鍥犱负鐩爣鍚堢害灏嗕互澶竵鍙戠粰鏀诲嚮鑰咃紝鏀诲嚮鑰呯殑閫€鍥炲嚱鏁拌澶勭綒锛屾墍浠ユ彁鐜板嚱鏁板張琚皟鐢ㄣ€?span style="display:none">aMG鏄惧崱涔嬪

鎻愮幇1Wei鐨勪簨浠惰璁板綍aMG鏄惧崱涔嬪

鏀诲嚮鑰呭悎绾︾殑浣欓灏变細鏇存柊涓ゆ锛岀涓€娆℃槸鍒伴浂锛岀浜屾鏄埌锛?銆?span style="display:none">aMG鏄惧崱涔嬪

鏀诲嚮鑰呯殑浣欓鍥炵疆鍒?2锛燂紵aMG鏄惧崱涔嬪

鏀诲嚮鑰呴€氳繃鎻愮幇鐩爣鍚堢害鐨勬墍鏈夎祫閲戯紝浠庤€屽畬鎴愭暣涓敾鍑?span style="display:none">aMG鏄惧崱涔嬪

浠g爜aMG鏄惧崱涔嬪

锛忥紛donate 1 wei锛?withdraw 1 wei锛婏紡aMG鏄惧崱涔嬪

function attack锛堬級 锝?span style="display:none">aMG鏄惧崱涔嬪

dao锛巇onate锛巚alue锛?锛夛紙this锛夛紱aMG鏄惧崱涔嬪

dao锛巜ithdraw锛?锛夛紱aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

锛忥紛fallback function锛?results in 0–1 锛?2锛婏紛256 锛婏紡aMG鏄惧崱涔嬪

function锛堬級 锝?span style="display:none">aMG鏄惧崱涔嬪

if 锛坧erformAttack锛?锝?span style="display:none">aMG鏄惧崱涔嬪

performAttack 锛?false锛?span style="display:none">aMG鏄惧崱涔嬪

dao锛巜ithdraw锛?锛夛紱aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

锛忥紛extract balance from smart contract锛婏紡aMG鏄惧崱涔嬪

function getJackpot锛堬級 锝?span style="display:none">aMG鏄惧崱涔嬪

dao锛巜ithdraw锛坉ao锛巄alance锛夛紱aMG鏄惧崱涔嬪

owner锛巗end锛坱his锛巄alance锛夛紱aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

瑙e喅鏂规aMG鏄惧崱涔嬪

涓轰簡闃叉鍙楀浜洪櫡鍏ヤ笅婧㈡敾鍑伙紝鏈€濂界殑鏂规硶鏄湅鏇存柊鐨勭姸鎬佹槸鍚﹀湪瀛楄妭鑼冨洿鍐呫€傛垜浠彲浠ユ坊鍔犲弬鏁版潵妫€鏌ユ垜浠殑浠g爜锛屼綔涓烘渶鍚庝竴灞備繚鎶ゃ€傚嚱鏁皐ithdraw锛堬級鐨勯琛屼唬鐮佹槸涓轰簡妫€鏌ユ槸鍚︽湁瓒冲鐨勮祫閲戯紝绗簩琛屾槸涓轰簡妫€鏌ヨ秴婧紝绗笁涓槸妫€鏌ヤ笅婧€?span style="display:none">aMG鏄惧崱涔嬪

contract babysDAO锝?span style="display:none">aMG鏄惧崱涔嬪

锛庯紟锛庯紟aMG鏄惧崱涔嬪

锛忥紛withdrawal ether from contract锛婏紡aMG鏄惧崱涔嬪

function withdraw锛坲int amount锛?锝?span style="display:none">aMG鏄惧崱涔嬪

if 锛坈redit锛籱sg锛巗ender锛?锛烇紳 amountaMG鏄惧崱涔嬪

锛嗭紗 credit锛籱sg锛巗ender锛?锛?amount 锛烇紳 credit锛籱sg锛巗ender锛?span style="display:none">aMG鏄惧崱涔嬪

锛嗭紗 credit锛籱sg锛巗ender锛?锛?amount 锛滐紳 credit锛籱sg锛巗ender锛斤級 锝?span style="display:none">aMG鏄惧崱涔嬪

credit锛籱sg锛巗ender锛?锛嶏紳 amount锛?span style="display:none">aMG鏄惧崱涔嬪

msg锛巗ender锛巗end锛坅mount锛夛紙锛夛紱aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

闇€瑕佹敞鎰忥紝灏卞儚鎴戜滑涔嬪墠璁ㄨ锛屾垜浠笂闈㈢殑浠g爜鏄湪鍙戝嚭璧勯噾涔嬪墠鏇存柊鐢ㄦ埛鐨勪綑棰濄€?span style="display:none">aMG鏄惧崱涔嬪

鏀诲嚮锛?锛氳法鍑芥暟绔炰簤鏉′欢aMG鏄惧崱涔嬪

鏈€鍚庤璇寸殑锛屽氨鏄法鍑芥暟绔炰簤鏀诲嚮銆傚氨鍍忓湪閲嶆斁鏀诲嚮涓墍璇达紝DAO鍚堢害涓嶈兘姝g‘鐨勬洿鏂板悎绾︾姸鎬侊紝骞朵笖鍙互璁╄祫閲戣鐩楃獌銆侱AO闂鍜屽閮ㄨ皟鐢ㄤ腑鐨勯儴鍒嗗師鍥犳槸璺ㄥ嚱鏁扮珵浜夋潯浠舵敾鍑荤殑娼滃湪鍘熷洜銆傝櫧鐒朵互澶潑涓墍鏈夌殑杞处鏄嚎鎬у彂鐢燂紙涓€涓湪鍙︿竴涓悗闈級锛?澶栭儴璋冪敤锛堝彟涓€涓悎绾︽垨鑰呭湴鍧€鐨勮皟鐢級濡傛灉娌℃湁琚悎鐞嗙鐞嗭紝灏变細鎴愪负鐏鹃毦鐨勫鐏嚎銆傚湪鐜板疄涓栫晫涓紝浠栦滑鏄畬鍏ㄥ彲浠ラ伩鍏嶇殑銆傚綋涓や釜鍑芥暟琚皟鐢ㄥ苟涓斿垎浜悓涓姸鎬侊紝璺ㄥ嚱鏁扮珵浜夋潯浠舵敾鍑诲氨浼氬彂鐢熴€傝繖涓悎绾﹀氨浼氭兂鍒帮紝鐜板湪鏈変袱涓悎绾︾姸鎬佸瓨鍦紝浣嗘槸鐜板疄鏄彧鏈変竴涓湡姝g殑鍚堢害鐘舵€佸瓨鍦ㄣ€傛垜浠笉鑳藉悓鏃惰幏寰梄 锛?3鍜孹 锛?4杩欎袱绉嶇粨鏋溿€?璁╂垜浠敤涓€涓緥瀛愭潵璇存槑杩欎釜鍐呭銆?span style="display:none">aMG鏄惧崱涔嬪

鏀诲嚮鍜屼唬鐮?span style="display:none">aMG鏄惧崱涔嬪

contract crossFunctionRace锝?span style="display:none">aMG鏄惧崱涔嬪

mapping 锛坅ddress 锛濓紴 uint锛?private userBalances锛?span style="display:none">aMG鏄惧崱涔嬪

锛忥紛 uses userBalances to transfer funds 锛婏紡aMG鏄惧崱涔嬪

function transfer锛坅ddress to锛?uint amount锛?锝?span style="display:none">aMG鏄惧崱涔嬪

if 锛坲serBalances锛籱sg锛巗ender锛?锛烇紳 amount锛?锝?span style="display:none">aMG鏄惧崱涔嬪

userBalances锛籺o锛?锛嬶紳 amount锛?span style="display:none">aMG鏄惧崱涔嬪

userBalances锛籱sg锛巗ender锛?锛嶏紳 amount锛?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

锛忥紛 uses userBalances to withdraw funds 锛婏紡aMG鏄惧崱涔嬪

function withdrawalBalance锛堬級 public 锝?span style="display:none">aMG鏄惧崱涔嬪

uint amountToWithdraw 锛?userBalances锛籱sg锛巗ender锛斤紱aMG鏄惧崱涔嬪

require锛坢sg锛巗ender锛巗end锛坅mountToWithdraw锛夛紙锛夛級锛?span style="display:none">aMG鏄惧崱涔嬪

userBalances锛籱sg锛巗ender锛?锛?0锛?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

涓婇潰鐨勫悎绾︽湁2涓姛鑳?– 涓€涓槸鍙互杞Щ璧勯噾锛屽彟涓€涓槸鎻愮幇璧勯噾銆傛垜浠亣璁炬敾鍑昏€呰皟鐢ㄤ簡鍑芥暟transfer锛堬級锛岀劧鍚庡悓鏃朵娇鐢ㄥ閮ㄨ皟鐢ㄥ嚱鏁皐ithdrawalBalance锛堬級銆倁serBalance锛籱sg锛巗ender锛界殑鐘舵€侀€氳繃2涓笉鍚岀殑鏂瑰悜琚娊鍑恒€傜敤鎴风殑浣欓杩樻病鏈夎璁句负0锛屼絾鏄敖绠¤祫閲戝凡缁忚鎻愬彇锛屾敾鍑昏€呬篃鑳藉杞Щ璧勯噾銆傝繖鏍锋儏鍐典笅锛屽悎绾﹀彲浠ヨ鏀诲嚮鑰呬娇鐢ㄥ弻鑺憋紝杩欎篃鏄尯鍧楅摼鎶€鏈兂瑕佽В鍐崇殑闂涔嬩竴銆?span style="display:none">aMG鏄惧崱涔嬪

娉ㄦ剰锛氬鏋滄湁鍑芥暟鍒嗕韩鐘舵€侊紝璺ㄥ嚱鏁扮珵浜夋潯浠舵敾鍑诲氨浼氬湪澶氫釜鍚堢害涓彂鐢熴€?span style="display:none">aMG鏄惧崱涔嬪

锛嶅湪璋冪敤澶栭儴鍑芥暟涔嬪墠锛屽簲璇ュ畬鎴愭墍鏈夌殑鍐呴儴宸ヤ綔aMG鏄惧崱涔嬪

锛嶉伩鍏嶅彂鐢熷閮ㄨ皟鐢?span style="display:none">aMG鏄惧崱涔嬪

锛嶅湪涓嶅彲閬垮厤鍦版椂鍊欙紝浣跨敤澶栭儴鍑芥暟“涓嶅彲淇?rdquo;aMG鏄惧崱涔嬪

锛嶅湪澶栭儴璋冪敤涓嶅彲閬垮厤鐨勬儏鍐典笅锛屼娇鐢ㄤ簰鏂?span style="display:none">aMG鏄惧崱涔嬪

鏍规嵁涓嬮潰鐨勫悎绾︼紝鎴戜滑鍙互鐪嬪埌涓€涓緥瀛?锛?鍦ㄥ畬鎴愬閮ㄨ皟鐢ㄤ箣鍓嶏紝瀹屾垚鍐呴儴宸ヤ綔銆?锛夊皢鎵€鏈夊閮ㄨ皟鐢ㄩ兘璁句负“涓嶅彲淇?rdquo;銆傛垜浠殑鍚堢害浼氳璧勯噾鍙戦€佸埌涓€涓湴鍧€锛屽苟涓斿厑璁哥敤鎴蜂竴娆℃€у皢璧勯噾瀛樺叆鍚堝悓銆?span style="display:none">aMG鏄惧崱涔嬪

contract crossFunctionRace锝?span style="display:none">aMG鏄惧崱涔嬪

mapping 锛坅ddress 锛濓紴 uint锛?private userBalances锛?span style="display:none">aMG鏄惧崱涔嬪

mapping 锛坅ddress 锛濓紴 uint锛?private reward锛?span style="display:none">aMG鏄惧崱涔嬪

mapping 锛坅ddress 锛濓紴 bool锛?private claimedReward锛?span style="display:none">aMG鏄惧崱涔嬪

锛忥紡makes external call锛?need to mark as untrustedaMG鏄惧崱涔嬪

function untrustedWithdraw锛坅ddress recipient锛?public 锝?span style="display:none">aMG鏄惧崱涔嬪

uint amountWithdraw 锛?userBalances锛籸ecipient锛斤紱aMG鏄惧崱涔嬪

reward锛籸ecipient锛?锛?0锛?span style="display:none">aMG鏄惧崱涔嬪

require锛坮ecipient锛巆all锛巚alue锛坅mountWithdraw锛夛紙锛夛級锛?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

锛忥紡untrusted because withdraw is called锛?an external callaMG鏄惧崱涔嬪

function untrustedGetReward锛坅ddress recipient锛?public 锝?span style="display:none">aMG鏄惧崱涔嬪

锛忥紡check that reward hasn’t already been claimedaMG鏄惧崱涔嬪

require锛堬紒claimedReward锛籸ecipient锛斤級锛?span style="display:none">aMG鏄惧崱涔嬪

锛忥紡internal work first 锛坈laimedReward and assigning reward锛?span style="display:none">aMG鏄惧崱涔嬪

claimedReward 锛?true锛?span style="display:none">aMG鏄惧崱涔嬪

reward锛籸ecipient锛?锛嬶紳 100锛?span style="display:none">aMG鏄惧崱涔嬪

untrustedWithdraw锛坮ecipient锛夛紱aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

鎴戜滑鍙互鐪嬪嚭锛岃繖涓悎绾︾殑棣栦釜鍑芥暟鍦ㄥ彂閫佽祫閲戝埌鐢ㄦ埛鐨勫悎绾︼紡鍦板潃鐨勬椂鍊欙紝灏变細鍙戠敓澶栭儴璋冪敤銆傚悓鏍峰湴锛屽鍔卞嚱鏁板湪鍙戦€佷竴娆℃€у鍔辩殑鏃跺€欙紝涔熶細浣跨敤鎻愮幇鍑芥暟锛屽洜涓鸿繖涔熸槸涓嶅彲淇$殑銆傚悓鏍烽噸瑕佸湴鏄紝鍚堢害闇€瑕佹墽琛屾墍鏈夊唴閮ㄥ伐浣溿€傚氨濂藉儚閲嶅叆鏀诲嚮锛屽嚱鏁皍ntrustedGetReward锛堬級浼氬湪鍏佽鎻愮幇涔嬪墠锛岃鐢ㄦ埛鑾峰緱涓€娆℃€х殑濂栧姳锛屼粠鑰岄槻姝㈣法鍑芥暟绔炰簤鏉′欢鏀诲嚮銆?span style="display:none">aMG鏄惧崱涔嬪

鍦ㄧ湡瀹炰笘鐣岋紝鏅鸿兘鍚堢害涓嶉渶瑕佷緷璧栦簬澶栭儴璋冪敤銆備簨瀹炰笂锛屽閮ㄨ皟鐢ㄥ湪寰堝鎯呭喌涓嬶紝鍦ㄥ伐浣滅幆澧冧腑閮藉嚑涔庝笉鍙兘鍙戠敓鐨勩€傜敱浜庤繖涓師鍥狅紝浣跨敤浜掓枼浣撴潵“閿佸畾”涓€浜涚姸鎬侊紝骞朵笖璁╂嫢鏈夎€呮湁鑳藉姏鍘绘敼鍙樼姸鎬侊紝鍙互甯姪闃叉杩欑被鐏鹃毦銆傝櫧鐒朵簰鏂ヤ綋闈炲父鏈夋晥锛屼絾鏄綋鐢ㄤ簬澶氫釜鍚堢害鐨勬椂鍊欙紝閮戒細鍙樼殑寰堟鎵嬨€傚鏋滀綘浣跨敤浜掓枼浣撴潵闃叉杩欑被鏀诲嚮锛屼綘闇€瑕佸緢浠旂粏鍦扮‘淇濇病鏈夊叾浠栨柟娉曟潵閿佸畾锛屾垨鑰呮案杩滀笉浼氶噴鏀俱€傚鏋滀娇鐢ㄤ簰鏂ヤ綋鐨勬柟娉曪紝鍦ㄥ啓鍏ユ櫤鑳藉悎绾︾殑鏃跺€欙紝浣犻渶瑕佷繚璇佷綘瀹屽叏鐞嗚В娼滃湪鐨勫嵄闄┿€?span style="display:none">aMG鏄惧崱涔嬪

contract mutexExample锝?span style="display:none">aMG鏄惧崱涔嬪

mapping 锛坅ddress 锛濓紴 uint锛?private balances锛?span style="display:none">aMG鏄惧崱涔嬪

bool private lockBalances锛?span style="display:none">aMG鏄惧崱涔嬪

function deposit锛堬級 payable public returns 锛坆ool锛?锝?span style="display:none">aMG鏄惧崱涔嬪

锛忥紛check if lockBalances is unlocked before proceeding锛婏紡aMG鏄惧崱涔嬪

require锛堬紒lockBalances锛夛紱aMG鏄惧崱涔嬪

锛忥紛lock锛?execute锛?unlock 锛婏紡aMG鏄惧崱涔嬪

lockBalances 锛?true锛?span style="display:none">aMG鏄惧崱涔嬪

balances锛籱sg锛巗ender锛?锛嬶紳 msg锛巚alue锛?span style="display:none">aMG鏄惧崱涔嬪

lockBalances 锛?false锛?span style="display:none">aMG鏄惧崱涔嬪

return true锛?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

function withdraw锛坲int amount锛?payable public returns 锛坆ool锛?锝?span style="display:none">aMG鏄惧崱涔嬪

锛忥紛check if lockBalances is unlocked before proceeding锛婏紡aMG鏄惧崱涔嬪

require锛堬紒lockBalances 锛嗭紗 amount 锛?0 锛嗭紗 balances锛籱sg锛巗ender锛?span style="display:none">aMG鏄惧崱涔嬪

锛烇紳 amount锛夛紱aMG鏄惧崱涔嬪

锛忥紛lock锛?execute锛?unlock锛婏紡aMG鏄惧崱涔嬪

lockBalances 锛?true锛?span style="display:none">aMG鏄惧崱涔嬪

if 锛坢sg锛巗ender锛巆all锛坅mount锛夛紙锛夛級 锝?span style="display:none">aMG鏄惧崱涔嬪

balances锛籱sg锛巗ender锛?锛嶏紳 amount锛?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

lockBalances 锛?false锛?span style="display:none">aMG鏄惧崱涔嬪

return true锛?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

锝?span style="display:none">aMG鏄惧崱涔嬪

浠ヤ笂锛屾垜浠彲浠ョ湅鍒板悎绾utexExample锛堬級浼氭湁绉佷汉閿佸畾鐘舵€侊紝鏉ュ疄琛宒eposit锛堬級鍑芥暟鍔熻兘鍜寃ithdraw锛堬級鍑芥暟銆傞攣瀹氫細闃叉鐢ㄦ埛鑳藉鍦ㄦ墍鏈夌殑鍒濇璋冪敤瀹屾垚涔嬪墠锛屾垚鍔熷畬鎴恮ithdraw锛堬級璋冪敤锛屽彲浠ラ槻姝换浣曠绫荤殑璺ㄥ嚱鏁扮珵浜夋潯浠舵敾鍑汇€?span style="display:none">aMG鏄惧崱涔嬪

鏈€鍚庣殑缁撴灉aMG鏄惧崱涔嬪

鍔涢噺瓒婂ぇ锛岃矗浠昏秺澶с€傝櫧鐒跺尯鍧楅摼鍜屾櫤鑳藉悎绾︽妧鏈瘡澶╅兘鍦ㄩ潻鏂帮紝浣嗘槸椋庨櫓渚濈劧寰堥珮銆傛敾鍑昏€呬粠娌℃湁鏀惧純鍘诲鎵炬満浼氭潵鏀诲嚮杩欎簺鍚堢害銆傝繖鍙栧喅浜庢垜浠潵淇濊瘉锛屾垜浠彲浠ヤ粠涔嬪墠椤圭洰鐨勯棶棰樹腑瀛︿範缁忛獙锛屾潵璁╂垜浠幏寰楁垚闀裤€傚笇鏈涢€氳繃杩欑瘒鏂囩珷锛屼互鍙婂叾浠栫郴鍒楁枃绔狅紝浣犲彲浠ユ洿鏄庣櫧鏅鸿兘鍚堢害鏀诲嚮銆?span style="display:none">aMG鏄惧崱涔嬪

璐d换缂栬緫锛歝taMG鏄惧崱涔嬪

鐩稿叧鎺ㄨ崘