Arrays 坚固性:学生认证体系结构

Arrays 坚固性:学生认证体系结构,arrays,mapping,ethereum,solidity,smartcontracts,Arrays,Mapping,Ethereum,Solidity,Smartcontracts,我最近做了一个关于uDemy的课程,获得了第一个概念。我开始为一个培训中心开发一个真实的应用程序。我想在区块链中注册Jhon Doe,ID(文档)xxxx,在特定日期批准了“区块链硕士”课程,并注册认证/许可的“到期日” 在PHP中,我将创建如下数组: $certifications[123456] = [ [ "name" => "Jhon", "lastName" => "Doe&quo

我最近做了一个关于uDemy的课程,获得了第一个概念。我开始为一个培训中心开发一个真实的应用程序。我想在区块链中注册Jhon Doe,ID(文档)xxxx,在特定日期批准了“区块链硕士”课程,并注册认证/许可的“到期日”

在PHP中,我将创建如下数组:

$certifications[123456] = [
    [
    "name" => "Jhon",
    "lastName" => "Doe",
    "courseName" => "Blockchain master",
    "Expiration date" => "2022-01-01"
    ],
    [
    "name" => "Jhon",
    "lastName" => "Doe",
    "courseName" => "Just another course",
    "Expiration date" => "2021-01-01"
    ]
];
对于输出:

array (size=1)
  123456 => 
    array (size=2)
      0 => 
        array (size=4)
          'name' => string 'Jhon' (length=4)
          'lastName' => string 'Doe' (length=3)
          'courseName' => string 'Blockchain master' (length=17)
          'Expiration date' => string '2022-01-01' (length=10)
      1 => 
        array (size=4)
          'name' => string 'Jhon' (length=4)
          'lastName' => string 'Doe' (length=3)
          'courseName' => string 'Just another course' (length=19)
          'Expiration date' => string '2021-01-01' (length=10)
此外,我想创建一个函数,通过文档ID查找他/她所做的认证

不用说,我希望以持久的方式将其保存在内存中,必要时使用gas

我想在映射和数组之间创建一个混合体,也许结构。。。但我觉得我走错了路

所以。。。任何人都可以指导我或给我一个类似的例子来检查如何接近

提前谢谢

第1版:

我按照一些提示编写了这段代码,并使其正常工作,除了两件事:

pragma solidity 0.6.6;
pragma experimental ABIEncoderV2;
// SPDX-License-Identifier: MIT

import 'https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/math/SafeMath.sol';

contract Certification {
    using SafeMath for uint256;
    
    address private owner;
    
    struct Certificate {
        string name;
        string lastname;
        string certificationName;
        string instructorName;
        uint256 dueDate;
        uint256 expirationDate;
    }
    
    
    Certificate[] public certifications;

    mapping(uint => uint) public dniToCertification;
    
    event certificateSubscribed(string name, string lastname, uint dni, string certification, string instructor, uint256 date, uint256 untilDate);
    
    constructor() public {
        owner = msg.sender;
    }
    
    modifier isOwner() {
        require(owner == msg.sender);
        _;
    }
    
    function subscribeCertificate(
        string memory name,
        string memory lastname,
        uint dni,
        string memory certificationName,
        string memory instructorName,
        uint256 dueDate,
        uint256 expirationDate) public isOwner {
            
        certifications.push(Certificate(
           name,
           lastname,
           certificationName,
           instructorName,
           dueDate,
           expirationDate
        ));
            
        Certificate storage certification; //certification will be an instance of Struct Certificate
        
        certification.name = name;
        certification.lastname = lastname;
        certification.certificationName = certificationName;
        certification.instructorName = instructorName;
        certification.dueDate = dueDate;
        certification.expirationDate = expirationDate;
        
        uint id = certifications.length - 1;
    
        dniToCertification[dni] = id;
        
        
        emit certificateSubscribed(name, lastname, dni, certificationName, instructorName, dueDate, expirationDate);
    }
    
    function checkCertificateByDni(uint id) public view returns (Certificate memory) {
        return (certifications[dniToCertification[id]]);
        
    }
    
}
  • 映射是不可编辑的,因此我必须使用
    experimental ABIEncoderV2
  • 我在第二次调用subscribeCertificate时出错,没有详细信息

  • 我建议将证书数组保存在Structs中,并为用户ID的地址和文档列表创建映射

    //certificate object
    struct Certificate {
        string name;
        string lastName;
        string courseName;
        uint256 expirationDate;
    }
    
    // you can save all certifications on this array.
    Certificate[] public certifications;
    
    //save every address certifications and address
    mapping(address => uint256[]) public userCertifications;
    
    用法示例:

    // get certificate  by id
    certifications[id]
    
    // get user certifications-> return array of user certifications
    userCertifications[msg.sender] or userCertifications[address]
    
    第1版答案 我把你的代码改成这个,我想它可以为你工作

    pragma solidity 0.6.6;
    // SPDX-License-Identifier: MIT
    
    import 'https://github.com/OpenZeppelin/openzeppelin- 
    solidity/contracts/math/SafeMath.sol';
    
    contract Certification {
    using SafeMath for uint256;
    
    address private owner;
    
    struct Certificate {
        string name;
        string lastName;
        string certificationName;
        string instructorName;
        uint256 dueDate;
        uint256 expirationDate;
    }
    
    
    Certificate[] public certifications;
    
    mapping(uint => uint) public dniToCertification;
    
    
    
    
    event certificateSubscribed(string name, string lastname, uint dni, string certification, string instructor, uint256 date, uint256 untilDate);
    
    constructor() public {
        owner = msg.sender;
    }
    
    modifier isOwner() {
        require(owner == msg.sender);
        _;
    }
    
    function subscribeCertificate(
        string memory name,
        string memory lastname,
        uint dni,
        string memory certificationName,
        string memory instructorName,
        uint256 dueDate,
        uint256 expirationDate) public isOwner {
            
            
            certifications.push(Certificate(
               name,
               lastname,
               certificationName,
               instructorName,
               dueDate,
               expirationDate));
    
            uint id = certifications.length - 1;
            
            dniToCertification[dni] = id;
            
            
            emit certificateSubscribed(name, lastname, dni, certificationName, instructorName, dueDate, expirationDate);
    }
    
    }
    
    复制并粘贴它,您将看到有两种方法 一个通过dni获得证书,另一个通过dni获得证书。
    此外,您无法获得稳定的结构数组,必须在客户端使用循环获取整个证书。

    谢谢。学生们没有地址,唯一的地址是中心地址。我可以使用文档编号代替地址作为映射索引吗?是的,您可以使用id代替地址=>mapping(uint256=>uint256[])公共用户证书;我不明白你是在什么时候把结构推到映射中的…@JuliSmz我更改了你的代码,我认为它适合你,请查看上面的答案版本部分。另外,我建议这本坚固性教程谢谢@Armir我已经测试过了,问题仍然存在。。。和以前一样,它只在第一次工作。我在第1版中更新了代码。谢谢!