0

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:

I split the vector in 10 different vectors and then pass just one of them to the constructor. With this the deploy works. 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,.......]

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

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) Am I wrong trying to pass a vector as parameter in a function call inside the A constructor ?

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?

1 Answer 1

1

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.

You have exceeded maximum permissible contract size. Also as far as I know you can't and shouldn't hard code big data structures.

Blockchain is much different from traditional databases/programming.

You need to spend gas to write data on the blockchain.

If you want the data (vector here) to be predefined and immutable, you can do so by deploying the contract and writing on the data structure.

Also, you don't want anyone to mess around with your data so you can put conditional modifiers around your function or use require inside your setter function.

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

Once deployed simply spend some gas and update your structure. Then deploy the second contract and pass the vector (hint: same can be done in deployment/migrate scripts!) . Alternatively you might want to access such data in run time and just pass address of contract A in the constructor.

I didn't write any code because its fun to do some research on ether and write code (even I am new and learning everyday).

3
  • Hi ! Thanks for the fast reply. Why you say "can't" and "shouldn't" about hardcoding data structure ? The only way I can think of a graph is specifying nodes (how many) and edges (hardcoded since they are unique for a specific graph). Regarding the hint you gave me: Do you mean passing an hardcoded vector from the console directly to the constructor parameters of my ContractB ? I will create a separate contractC to initializate a contractB with a set function. Then I will use this specific address of contractB inside my contractA constructor. Is this right ?
    – Xaler
    Commented Apr 17, 2018 at 10:59
  • Values once pushed to blockchain will remain there forever , and won't change. If you have an exclusive owner editable only function which pushes data from console/js/python, you will achieve what you want. Don't think of it as passing values while deploying the contract. think of it passing value just after deploying the contracts.
    – Karan
    Commented Apr 17, 2018 at 11:40
  • You don't need C. First deploy B. Set values in B. Deploy A with address of B in constructor. call getVector from A, on B's instance. obviously, there are many other way to achieve the same depending on your dapp requirements.
    – Karan
    Commented Apr 17, 2018 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.