在Solidity中初始化一个大的定长数组 [英] Initialize a big fixed length array in Solidity

查看:199
本文介绍了在Solidity中初始化一个大的定长数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个项目是在以太坊上开发游戏,我面临着存储和天然气限制.我想将存储智能合约存储在部署后要查询的区块链上.我确实需要使用我手动插入的常量值来初始化固定长度的数组.我的情况如下:

I'm building a game on ethereum as my first project and I'm facing with the storage and gas limits. I would like to store a storage smart contract on the blockchain to be queried after the deployment. I really need to initialize a fixed length array with constant values I insert manually. My situation is the following:

    contract A {

...some states variables/modifiers and events......

uint[] public vector = new uint[](162);

vector = [.......1, 2, 3,......];

function A () {

....some code....

ContractB contract = new ContractB(vector);

}

....functions....

}

此代码未部署.显然我超过了混音的气体限制.我尝试了以下方法:

This code doesn't deploy. Apparently I exceed gas limits on remix. I tried the following:

  • 我将向量分为10个不同的向量,然后仅将其中一个传递给构造函数.部署就可以了.

我真的只需要一个向量,因为它表示图的边集,其中ContractB是构建图的数据结构.向量元素的排序如下:

I really need to have just one single vector because it represents the edges set of a graph where ContractB is the data structure to build a graph. Vectors elements are ordered like this:

vector = [edge1From, edge1To, edge2From, edge2To,.......]

,我得到了81条边(矢量中有162条).

and I got 81 edges (162 entries in the vector).

我可以创建一个setData函数,该函数将在部署后一次调用向量中的值,然后将其推入向量中,但这不是我的情况,因为我需要在调用之前填充向量

I tought I can create a setData function that push the values in the vector one by one calling this function after the deployment but this is not my case because I need to have the vector filled before the call

ContractB contract = new ContractB(vector);

现在我可以看到我有两个疑问:

Now I can see I have two doubts:

1)我试图在A构造函数内部的函数调用中将向量作为参数传递给我吗?

1) Am I wrong trying to pass a vector as parameter in a function call inside the A constructor ?

2)我看到可以为边缘创建双重映射.像

2) I can see that I can create a double mapping for the edges. Something like

mapping (bool => mapping(uint => uint)) 

但是然后我将需要多键值映射(从同一点开始有更多边),并且像使用向量一样,我将面临一次初始化所有映射的问题?

but then I will need multi-key valued mappings (more edges starting from the same point) and I will have the problem to initialize all the mappings at once like I do with the vector?

推荐答案

为什么需要在施工时初始化合同?

Why does the contract need to be initialized at construction time?

这应该有效

pragma solidity ^0.4.2;

contract Graph {
    address owner;

    struct GraphEdge {
        uint128 from;
        uint128 to;
    }

    GraphEdge[] public graph;
    bool public initialized = false;

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

    function addEdge(uint128 edgeFrom, uint128 edgeTo) public {
        require(!initialized);
        graph.push(GraphEdge({
            from: edgeFrom,
            to: edgeTo
        }));
    }

    function finalize() public {
        require(msg.sender == owner);
        initialized = true;
    }
}

contract ContractB {
    Graph graph;

    constructor(address graphAddress) public {
        Graph _graph = Graph(graphAddress);
        require(_graph.initialized());
        graph = _graph;
    }
}

这篇关于在Solidity中初始化一个大的定长数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆