0

I have to store the file hash of all the files stored on our server. Can I use single contract with fields like:

contract FileDetail {
    string fileName;
    string fileHash;

.................
}

Whenever I will call set method will it overwrite the field values of fileName and fileHash or it will create new values/copies of fileName or fileHash ? Or do I need to use a list/map if I use single contract approach.

Is it feasible to create new contract for each file. ? Can creation of a contract get automated ?

2 Answers 2

1

Just to add to Shawn's answer, here's a bit more complete example:

pragma solidity ^0.4.24;

contract Test {
    struct File {
        string fileName;
        string fileHash;
    }

    File[] files;

    function addFile(string fileName, string fileHash) public {
        File memory f = File(fileName, fileHash);
        files.push(f);
    }

    // a test function for getting data at certain index
    function getFiles(uint256 index) public view returns (string, string) {
        return (files[index].fileName, files[index].fileHash);
    }
}
4
  • One more thing, is there a better way to fetch the File record ? I mean using start/end timestamp and using a query like thing ?
    – Vivek Sadh
    Jul 20, 2018 at 6:58
  • 1
    Sure you can build whatever query engines you want. Everything is doable. Just remember that many operations are expensive in Ethereum - you can try different things and see what works for you. Jul 20, 2018 at 7:09
  • Any link/example of using query in solidity ? I am currently going through Solidity docs and hope I can find everything there.
    – Vivek Sadh
    Jul 20, 2018 at 8:00
  • 1
    Um. I don't know what all sorts of libraries there are. You can either try googling or just create your own. Just don't try to make anything complicated or too fancy. Jul 20, 2018 at 8:03
1

It is possible to automate contract creation, however, it is not likely that creating a new contract for every file would be correct here. It would be extremely expensive and likely not very scalable or manageable.

It seems like you want to use a struct here to define a File object which contains the properties fileName and fileHash. And then you would make an array or mapping of these objects so that you can manage and track multiple files.

struct File
{
    string fileName;
    string fileHash;
}

File[] files;
files.push(File(fileName, fileHash));
0

Your Answer

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

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