kleros/kleros-interaction

When initialising a DisputeSctruct avoid using ambiguous values such as 0 (refuse to arbitrate)

Closed this issue · 3 comments

Here:

function createDispute(uint _choices, bytes _extraData) public payable returns(uint disputeID) {
super.createDispute(_choices, _extraData);
disputeID = disputes.push(DisputeStruct({
arbitrated: Arbitrable(msg.sender),
choices: _choices,
fee: msg.value,
ruling: 0,
status: DisputeStatus.Waiting
})) - 1; // Create the dispute and return its number.
emit DisputeCreation(disputeID, Arbitrable(msg.sender));
}

ruling: 0 is a valid ruling (refuse to arbitrate)

Initialise it to MAX_UINT instead

Unless storing 0 has some gas saving implications. I still prefer clarity, readability, simplicity.

Storing 0 is cheaper.

Yes and no. I agree to a degree.

At some point in the future, in most of the cases (unless really 0, “refuse to arbitrate”) someone else will have to pay the cost of storing one integer.

As always, I opt for simplicity and adoption.

If we are so much into gas savings, read this: https://medium.com/@novablitz/storing-structs-is-costing-you-gas-774da988895e

Then, we changed creationTime from a uint64 to a uint48. This lets the top five fields all pack together into 32 bytes. Timestamps don’t need to be more than uint48, and Solidity (unlike most other languages) allows a uint to be any multiple of 8, not just 8/16/32/64 etc. (if you’re reeeally crunched for space, you can use a uint32 for timestamp — you’ll likely be dead before it causes a problem in 2106).

struct DisputeStruct {
Arbitrable arbitrated;
uint choices;
uint fee;
uint ruling;
DisputeStatus status;
}

(use proper types and align fields)

I agree with you generally. But having 0 by default in this instance seems OK because inactive arbitrator means no arbitration.

And yeah, we could use smaller types. CentralizedArbitrator is not a contract we use it for our products right now. It's more like an example contract. So I guess that's why we didn't care to optimize gas costs.