以太坊智能合约,功能成本过高:无限

时间:2018-06-27 08:25:01

标签: ethereum adjacency-list smartcontracts remix

我正在开发我的第一个智能合约,因此我在Remix IDE上遇到了问题。这标志着我的功能成本太高,我不明白问题可能在哪里。也许有人比我有更多的经验可以帮助我。

这是我的代码,应该可以建立邻接表。我使用地图来跟踪它,其中每个键代表图形上的每个节点,以及与它对应的所有节点(使用动态数组)对应的值。首先,在我的函数中,我检查A和B之间的连接是否已经存在,如果不是,则将其添加到A => B和B => A中。

pragma solidity ^0.4.21;

contract AdjacencyList {

mapping (address => address[]) public neighbours;



function comunication(address source, address destination) public {
    address[] storage d = neighbours[source];
    bool found = false;
    for (uint i = 0; i < d.length; i++) {
        if (d[i] == destination) {
            found = true;
            break;
        }            
    }
    if(!found) {
        d.push(destination);
        d = neighbours[destination];
        d.push(source);
    }   
}

}

0 个答案:

没有答案
相关问题