0

I want to store cells of an Excel file with 94 rows and 104 columns (it means 9776 cells!) in a byte array as follows :

I define my array as follows :

struct StateStruct {
        bytes32 description;
        mapping(bytes32 => bytes32) sub_state;
    }

    struct ObjectStruct {
        StateStruct state;
        address owner; 
        bool isObject;

        bytes32 review;
    }

    mapping(bytes32 => ObjectStruct) objectStructs;
    bytes32[] public objectList;

bytes32[9776] ExcelFields = [bytes32("cell_1"),bytes32("cell_2"),bytes32("cell_3"), ... , bytes32("9776")]; // !!! How can I do this ? and How long does it take ? 

function setExcelCell(bytes32 _id, bytes32[9776] cell_values, address _owner) public returns(bool success) {
        require(!isObject(_id));

        uint256 counter=0;
        for(counter; counter < 9776; counter++) {

            objectStructs[_id].state.sub_state[ExcelFields[counter]] = cell_values[counter]; // cell_values[counter] may be "true", "false" or other value.

        }

        return true;
    }

According to answer and comment of User "AnAllergyToAnalogy" I defined also following "alternative function" :

mapping(uint => mapping(uint => bool)) myNestedMapping;

setMyNestedMapping(bytes32 column,bytes32 row,bool[9776] value) public returns(bool success) {

uint256 counter=0;
uint256 i=0;
uint256 j=0;
        for(i; i< 9776; i++) 
            for(j; j< 9776; j++){

            myNestedMapping[i][j] = true;

        }

return true;
}

You can see a screen shot of this Excel file here : Excel File Screen Shot

You also can download this file from here : http://www.filedropper.com/usecase5heatpumpomplan

And in parallel, I'm going to save the hash of this file in a byte array or by an event.

However, in general, I am not sure my strategy for designing and developing such a Smart Contract is correct and logic ? or I'm going totally wrong ?

**

Important Note : Is it possible to use IPFS for this purpose ? and can it help us ?

**

7
  • You can do the initialization part from the off-chain, after you add an only-owner permission, and even a one-time locking mechanism for transparency (i.e. intention declaration). May 17, 2018 at 16:31
  • If you store 9776 cells and each cell is 32 bytes you will be using 320kb. From ethereum.stackexchange.com/questions/872/… it cost around 0.035 eth per kb, at usd$ 700 per ether it will cost usd$ 7800 to store. If you only store 1byte per cell it will be more reasonable us$ 245, but your code to read from the storage will be more complex and will cost more to deploy.
    – Ismael
    May 17, 2018 at 16:37
  • @Ismael Thank you, What do you propose to manage this problem ? For example using event is OK ? or it cost as well too much ? Thanks
    – Questioner
    May 17, 2018 at 16:49
  • @Ismael And can IPFS help me in this case ? Thanks
    – Questioner
    May 17, 2018 at 17:33
  • It is hard to recommend something without knowing the details of your problem. Events are cheaper than storage but a contract cannot access not modify them. I'd not recommend to store something in the blockchain unless you really need it. You can store IPFS hashes on a contract and access data off-chain with IPFS. But it depends on your use case.
    – Ismael
    May 17, 2018 at 17:34

1 Answer 1

2

Without knowing exactly the purpose of your smart contract, it seems like all the cells in your Excel document essentially have a boolean value.

I would suggest, rather than using a giant, costly and slow-to-process byte array, you should use a nested array or nested mapping, depending on how you'll be accessing the data.

ie,

mapping(uint => mapping(uint => bool)) myNestedMapping;

and then you can store an "X" (assuming it is equivalent to "true") as:

myNestedMapping[column][row] = true;

Update: Rather than using the comments I'm updating my answer,

You never need the byte array, forget them. You don't need anything except a couple of uints, and you don't need to do any iteration on-chain.

Your function should be something like:

function setMyNestedMapping(uint row,uint column, bool cellValue) public{
    myNestedMapping[row][column] = cellValue;
}

Then off chain, (ie, in Javascript if you're using Web3.js) you iterate through your Excel file and only call this contract function if the cellValue = true, because all cells on chain will be set to false by default.

If you don't know about how mappings work, I suggest you check them out in the docs because they are an essential part of programming in solidity.

7
  • Thank you. In fact, I defined a nested array in my code and I added it to my question. If you think something is defined incorrectly, please let me know. Thanks
    – Questioner
    May 17, 2018 at 14:33
  • I think you're approaching the whole problem incorrectly. When dealing with Solidity smart contracts, you should keep the data stored on chain, and the processing done on chain, to an absolute minimum. Given that you know the structure of the excel sheet (ie, what x and y columns there are), and given that the cells can only take true or false values, i wouldnt even be touching bytes. You can do any extra work to interpret the data off-chain May 17, 2018 at 14:39
  • Also, my problem is that I cannot use two for loop and then put a digit for [column][row] (for example : myNestedMapping[1][1] = true; or myNestedMapping[1][2] = false;) but also I need to init array like this :myNestedMapping[action_name_1][player_1] = true; Thank again.
    – Questioner
    May 17, 2018 at 14:44
  • If you're iterating through the excel sheet to read it and write it to your on chain array, you should do this offchain. make a function in your smart contract like setMyNestedMapping(column,row,value) and just call that externally as needed. but note, mapping values default to 0 (false) so you dont need to init the false values. May 17, 2018 at 14:53
  • Thank you very much. It seems a very interesting approach. Could you please explain a bit more in your answer ? For example: at time of implementing function setMyNestedMapping(column,row,value) in solidity, how it is coded in solidity to read excel sheet and write it to chain array ? Thank you very much again.
    – Questioner
    May 17, 2018 at 15:07

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.