功能高的气体要求:无限

bak*_*bak 6 ethereum solidity web3js web3-donotuse

以下是我的智能合约.当我把它放入混音时,我会收到以下每个功能的警告.

函数GasRecord.addNote(bytes32,bytes32)的气体要求高:无限.

函数GasRecord.getDoctorsNames()的气体要求高:无限.

函数GasRecord.getNotes()的气体要求高:无限.

函数GasRecord.giveDoctorAccess(address,bytes32)的气体要求高:无限.

pragma solidity ^0.4.17;


contract MedicalRecord {
struct Doctor {
    bytes32 name;
    uint id;
}

struct Note {
    bytes32 title;
    bytes32 note;
}

address public patient;
uint private doctorId;
bytes32[] public doctorsNames;
Note[] notes;
mapping (address => Doctor) private doctors;

modifier onlypatient {
    require(msg.sender == patient);
    _;
}

modifier isCurrentDoctor {
    require(!(doctors[msg.sender].id < doctorId));
    _;
}

function MedicalRecord() public {
    patient = msg.sender;
    doctorId = 0;
}

function giveDoctorAccess(address drAddress, bytes32 name)
public
onlypatient
returns (bytes32)
{
    doctors[drAddress] = Doctor (name, doctorId);
    doctorId++;
    doctorsNames.push(name);
    return (name);
}

function getNotes()
    view
    public
    isCurrentDoctor
    returns (bytes32[], bytes32[])
{
    bytes32[] memory titles = new bytes32[](notes.length);
    bytes32[] memory noteTexts = new bytes32[](notes.length);

    for (uint i = 0; i < notes.length; i++) {
        Note storage snote = notes[i];
        titles[i] = snote.title;
        noteTexts[i] = snote.note;
    }

    return (titles, noteTexts);
}

function getDoctorsNames() view public returns (bytes32[]) {
    return doctorsNames;
}

function addNote(bytes32 title, bytes32 note) public isCurrentDoctor 
{
    notes.push(Note({title: title, note:note}));
}
}
Run Code Online (Sandbox Code Playgroud)

谁能告诉我如何改进这个?

Ada*_*nis 7

对动态阵列的任何引用都将导致无限的气体警告.这并不一定意味着你做错了什么.

从优化尺寸来看,您可以做几件小事,但大多数都不会大幅改变您的燃气消耗量.例如,我认为不需要存储单独的数组,doctorNames因为您已经在Doctor结构中保存了这些数组.另外,一般来说,在你做的时候迭代一个可能无界的项目数组并不是一个好主意getNotes().通常,最好有一个返回数组长度的函数,然后调用一个单独的函数来获取在索引中传递的注释(换句话说,在客户端中执行循环).但即使在这里,你的用例似乎也不会产生大量的笔记.

您的最大成本将是您与单个患者签订的合同.合同部署非常昂贵.在这里,您将为您系统中的每位患者部署合同(可能是每位患者签订多份合同,因为这是一份病历?).我希望将所有内容都归结为结构,并为所有患者/医疗记录签订1份合同.根据整个商业案例,每个患者可能会签一份合同(即使考虑到这一点,我想我会先探讨每位医生签一份合同).但是,肯定不是每份医疗记录的合同.