Solidity

Solidity 兼容性配置

  • May 21, 2019

我是一名網路安全分析師,剛接觸乙太坊,正在從事一個研究加密安全的項目。這是你應該利用的一系列 22 個易受攻擊的智能合約。對於任何感興趣的人,這是挑戰:

https://ethernaut.zeppelin.solutions/

我正在嘗試使用本指南為挑戰配置一個松露項目:https ://www.notonlyowner.com/ethereum/solving-zeppelin-ethernaut-ctf-fallback/在我執行安裝命令後:

npm i truffle@4 ganache-cli@6 openzeppelin-solidity@2.0.0

我會嘗試部署契約,但我得到了一堆錯誤。我開始嘗試修復它們並淘汰了一些,但我覺得必須有更好的解決方案。如果有經驗的區塊鏈開發人員能幫助我部署這個,我會非常感激,因為我整天都在工作中把頭撞在我的辦公桌上。我在下面發布了原始碼和我的錯誤:

pragma solidity ^0.4.18;

import 'zeppelin-solidity/contracts/ownership/Ownable.sol';

contract Fallback is Ownable {

 mapping(address => uint) public contributions;

 function Fallback() public {
   contributions[msg.sender] = 1000 * (1 ether);
 }

 function contribute() public payable {
   require(msg.value < 0.001 ether);
   contributions[msg.sender] += msg.value;
   if(contributions[msg.sender] > contributions[owner]) {
     owner = msg.sender;
   }
 }

 function getContribution() public view returns (uint) {
   return contributions[msg.sender];
 }

 function withdraw() public onlyOwner {
   owner.transfer(this.balance);
 }

 function() payable public {
   require(msg.value > 0 && contributions[msg.sender] > 0);
   owner = msg.sender;
 }
}

錯誤:

Compiling ./contracts/Fallback.sol...
Compiling ./contracts/Migrations.sol...
Compiling openzeppelin-solidity/contracts/ownership/Ownable.sol...

/home/awile/smart_contracts/ethernaut/contracts/Fallback.sol:16:48: TypeError: Type function () view returns (address) is not implicitly convertible to expected type address.
       if(contributions[msg.sender] > contributions[owner]) {
                                                    ^---^
,/home/awile/smart_contracts/ethernaut/contracts/Fallback.sol:17:4: TypeError: Expression has to be an lvalue.
           owner = msg.sender;
           ^---^
,/home/awile/smart_contracts/ethernaut/contracts/Fallback.sol:17:12: TypeError: Type address is not implicitly convertible to expected type function () view returns (address).
           owner = msg.sender;
                   ^--------^
,/home/awile/smart_contracts/ethernaut/contracts/Fallback.sol:31:3: TypeError: Expression has to be an lvalue.
       owner = msg.sender;
       ^---^
,/home/awile/smart_contracts/ethernaut/contracts/Fallback.sol:31:11: TypeError: Type address is not implicitly convertible to expected type function () view returns (address).
       owner = msg.sender;
               ^--------^
Compilation failed. See above.

編輯:通過將 openzeppelin-solidity 降級到 1.11.0 來修復

npm uninstall openzeppelin-solidity
npm i openzeppelin-solidity@1.11.0

很高興聽到您正在使用Ethernaut來了解有關智能合約安全性的更多資訊。

我會建議你只使用控制台作為備份級別。然後我可以幫助進行環境設置。

關於備份級別的一些提示。

Ethernaut 關卡有許多社區解決方案,您甚至可以在處理它們時添加自己的解決方案。https://forum.zeppelin.solutions/t/ethernaut-community-solutions/561

如果您對 Ethernaut 有任何回饋,請在社區論壇發帖: https ://forum.zeppelin.solutions

社區目前正在更新 Ethernaut,歡迎您加入。

引用自:https://ethereum.stackexchange.com/questions/70913