Lets consider a simple Contract that is to track a variable. A client App needs to access this variable's history to show a graph.
I can create an Array inside the Contract, and push() the value anytime, like the following (untested code):
contract Price {
uint[] public priceHistory;
function logPrice(uint price) public {
priceHistory.push(price);
}
}
Perhaps a cheaper and better option is to emit Events, similar to the following:
contract Price {
event PriceChanged(uint price);
function logPrice(uint price) public {
emit PriceChanged(price);
}
}
As per (3) a cheaper form of storage, the second approach is low cost. My question is, can we assume, reliably, that the logs will be written in time order (so that we can reconstruct a history from them).
The next question is, if this Contract calls other Contracts, which follow the same design pattern (all storage is in logs), how can we weave together a history with proper time-ordering? ie, establishing ClientApp -> ContractA - ContractB -> ContractC from reading Event logs for all the three Contracts.
One thought could be to pass-on transaction-hash from the first user -> contract call, and pass it all the way to subsequent contract -> contract calls.
If this were a monolithic system, timestamp could have helped. But since its a distributed system, I wonder how do we establish the notion of a sequence here?
Fire,Ready,Aim), how the reverse order would affect the logic of the dApp? Applying this "peculiar" order sounds more like it could make dApp code buggy or behave in unexpected ways and lead to web2 security issues. What are your insights on the potential security implications of this peculiar emitting order? – CJ42 Oct 13 '22 at 08:17