3

I wrote a solidity contract where one can share a file hash along with the accounts which can have access to the file hash. Sort of a file sharing thing. Just a basic contract, accounts having access control can get the file hash and the rest can't (they get an error message).

I test it on remix first and it is working absolutely fine. But once it is deployed on my ethereum private network, the blockCount variable is not getting incremented. So when I call function getFileHash, it returns "error".

Contract code:

pragma solidity ^0.4.0;
contract hcare

{
    struct block 
    {
            string fileHash;
            uint numRecv;
            address[] recvAddress;
    }

    uint i;
    uint recvCount;
    //uint8 public blockCount;
    address owner;
    block temp;
     //a single block
    block[] Blocks; //array of blocks

    event fileUploaded (address sender , uint cnt);

    function hcare()
    {
            owner = msg.sender;

    }

    function addBlock (string fHash, address[] recvAddr) public
    {


            //temp = block(fHash , recvAddr.length , recvAddr);

            Blocks.push(block(fHash , recvAddr.length , recvAddr));
            fileUploaded (msg.sender, Blocks.length);
    }

    function getBlockCount() public constant returns (uint)
    {
        return Blocks.length;
    }



    function getFileHash () public constant returns(string)
    {
            string tempFHash;  
            uint ctr=0;
            uint i;
            uint j;

            for (i=0; i < Blocks.length;i++)
            {
                    for ( j=0; j<Blocks[i].recvAddress.length;j++)
                    {
                            if (Blocks[i].recvAddress[j] == msg.sender)
                            {
                                    tempFHash = Blocks[i].fileHash;
                                    ctr = 1;

        //                             if (ctr == 1)
//                                    break;
                             }
                    }
            }        
            if(ctr == 1)
            return tempFHash;
            else
            return "error";
        }
    }

Edit1: Replaced the blockCount variable by returning Blocks.length , but still the block length returns zero. From what I can guess, nothing is being added to the structure array.

Edit2: Uploaded a refined version of the code at PasteBin. The main problem of block not adding to structure persists.

  • Can you check that addBlock has succeeded and didn't throw an out of gas exception? Operations with strings are expensive and can fail if they didn't have enough gas. – Ismael Jul 19 '17 at 6:04
  • The addBlock function, when executed, returned a transaction hash, so I'm kind of sure that there was no out of gas exception. Also, I tried replacing the default '3000000' gas limit in Remix with the gas limit of '0x8000000', and it is still working in Remix. – Arghya Dutta Jul 19 '17 at 6:29
  • 1
    Returning a tx hash does not imply that there was no out of gas exception. You can diagnose the issue by running eth.getTransactionReceipt, and checking if the gasUsed field is equal to the gas sent. Note that if it is, this implies some exception, not necessarily an OOG – Tjaden Hess Jul 19 '17 at 10:21
1

You are using blockCount for counting number of elements in array Blocks and both variables blockCount and Blocks are global. Instead of using blockCount for counting array size you must use a defined function length for obtaining size of array Blocks. So please remove blockCount and use Block.length on its place.

  • I have tried this alternative, and the result is still the same. Executing correctly on Remix, but not in the console. Is there any way to find out if anything's being actually added to the blocks, apart from trying the Blocks.length (which is not working)? – Arghya Dutta Jul 18 '17 at 11:37
  • @ArghyaDutta cool! so please update code in question. Don't forget to add update note. – furusiyya Jul 18 '17 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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