1

I'm testing to not use state variables and relay on events and an external service to know the latest state and history of, in the simple example above, a Document. You can think about Document as a struct with more members. To update the Document, you need to provide the right previous data, that is hashed and validated with _DocumentHash as a guard before making the changes.

My question is: what are the risks of this approach from the point of view of making the state of the contract inconsistent? I mean, making the contract vulnerable to transaction order dependency attacks, for example. Or any other trade-off?

contract DocumentRegistry {
    struct Document {
        uint32 var1;
        uint64 var2;
    }

    event DocUpdated(uint32 var1, uint64 var2);
    bytes32 public _DocumentHash;

    constructor() {
        Document memory doc = Document({var1: 0, var2: 0});
        _DocumentHash = keccak256(abi.encodePacked(doc.var1, doc.var2));
        emit DocUpdated(doc.var1, doc.var2);
    }

    function update(Document memory doc) external {
        bytes32 hashedDoc = keccak256(abi.encodePacked(doc.var1, doc.var2));
        require(hashedDoc == _DocumentHash, "Incorrect Document state");

        ++doc.var1;
        doc.var2 += 10;

        _DocumentHash = keccak256(abi.encodePacked(doc.var1, doc.var2));
        emit DocUpdated(doc.var1, doc.var2);
    }
}

Many thanks!

1 Answer 1

0

You will be dependent to projects that saves the events (TheGraph/Moralis/Covalent). For a huge amount of data this is the only way you can do it since interrogating function views will fail (dos based on gas). For example deb0x protocol which is an emailing/dropbox protocol for web3 doesn't save the emails on smart contract state because when you would like to see the received emails view functions will fail if you have a lot data stored. So by sending them to events will allow you to see the entire inbox. Note that you have to use "indexed" word for events. This will allow you to query for events on frontend/backend. In conclusion, based of how you do the arhitecture of storage you can take in consideration what is worth to be saved on smart contracts and what is worth to be saved on events.

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.