用Remix网页进行例子测试

例子来源:《基于区块链的疫苗溯源系统开发》https://github.com/adguh/Vaccine

一、用Remix在线测试

remix网址:http://remix.ethereum.org/(部分网络不能正常访问,我用电脑连接手机移动4G的热点)

1. 打开remix网址,点github,粘贴输入网址

用Remix网页进行例子测试

输入4个sol文件网址,导入4个.sol文件:

  • Vaccine.sol

  • Migrations.sol

  • MetaCoin.sol

  • ConvertLib.sol

2. 测试Vaccine.sol合约

pragma solidity ^0.5.0;

contract Vaccine{
    
    uint public Index;
    //address add1;
    mapping(address => mapping(uint => VaccineInfo)) store;//查找表
    mapping(uint => address) VaccineIdInstore;//反查表
    
    //疫苗基本信息
    struct VaccineInfo{
        uint Id;
        //address producer;//生产商
        uint traceNum;//追溯码
        string vaccineName;
        uint produceData;//生产日期
        uint endData;//失效日期
    }
     //VaccineInfo[] memory basicInfo;
     constructor() public{
         Index = 0;
     }
     //写入数据
     function addInfo(uint _traceNum, string memory _vaccineName, uint _produceData, 
     uint _endData)public returns(uint) {
         Index +=1;
         VaccineInfo memory vaccineInfo = VaccineInfo(Index, _traceNum, _vaccineName, _produceData
         , _endData);
         store[msg.sender][Index] = vaccineInfo;
         VaccineIdInstore[Index] = msg.sender;
        // return Index;
     }
     //提取数据
     function getInfo(uint _Index) view public returns(uint, uint, string memory, uint, uint){
         VaccineInfo memory vaccineInfo = store[VaccineIdInstore[_Index]][_Index];
         return(vaccineInfo.Id, vaccineInfo.traceNum, vaccineInfo.vaccineName, 
         vaccineInfo.produceData, vaccineInfo.endData);
     }

    
    
}

2.1 打开Vaccine.sol,网页自动选择相应版本,点击compile编译

用Remix网页进行例子测试

2.2 部署合约,选择账号,设置gas limit,点击Deploy,

用Remix网页进行例子测试

2.3 测试2个函数和1个状态变量

用Remix网页进行例子测试

2.3.1 点击index,显示0

用Remix网页进行例子测试

2.3.2 addinfo后输入

30001,“疫苗名称”,20110223,20150223

用Remix网页进行例子测试

显示成功

2.3.3 getinfo后输入 1

用Remix网页进行例子测试

得到刚才输入的信息

2.3.4 index

用Remix网页进行例子测试

成功注册1个

2.4 同上注册4个疫苗产品

用Remix网页进行例子测试

3. 测试migration.sol合约

pragma solidity >=0.4.21 <0.6.0;

contract Migrations {
    address public owner;
    uint public last_completed_migration;

    constructor() public {
        owner = msg.sender;
    }

    modifier restricted() {
        if (msg.sender == owner) _;
    }

    function setCompleted(uint completed) public restricted {
        last_completed_migration = completed;
    }

    function upgrade(address new_address) public restricted {
        Migrations upgraded = Migrations(new_address);
        upgraded.setCompleted(last_completed_migration);
    }
}

编译后部署完成,开始测试

3.1 先用地址1测试

用Remix网页进行例子测试

一切正常

3.2 不update地址的情况下更换为地址2测试

用Remix网页进行例子测试

变量不会更改,因为

 modifier restricted() {
        if (msg.sender == owner) _;
    }

限制了操作

4. 测试convertlib.sol合约

pragma solidity >=0.4.21 <0.6.0;

library ConvertLib {
    function convert(uint amount,uint conversionRate) public pure returns (uint convertedAmount) {
        return amount * conversionRate;
    }
}

区块链Solidity中library的使用和部署:https://www.jianshu.com/p/2a49b4e2ee58

 

上一篇:103. Binary Tree Zigzag Level Order Traversal


下一篇:算法:迷宫问题