Open AuHau opened 1 year ago
Thanks for the report. Probably it's this arithmetic that is causing this. I think we should: use datatypes of the same capacity (length) both in contract and in the client and use safe arithmetic (one that doesn't crashes the node when there's overflow).
Blockchain contract uses uint256
for ttl?
Thanks for the report. Probably it's this arithmetic that is causing this.
I don't think that is it. I have my eyes on: https://github.com/codex-storage/nim-codex/blob/8681a40ee79b260923d49f30dced865172c53b60/codex/clock.nim#L44
Do you know if truncate()
call could lead to this? Underneath, it calls cast[]()
which I am now not sure if it would do something?
I think we should: use datatypes of the same capacity (length) both in contract and in the client and use safe arithmetic (one that doesn't crashes the node when there's overflow). Blockchain contract uses uint256 for ttl?
Yeaaah, it is bit "complicated". I mean expiry is meant to be a timestamp and it is also often compared to "now" which is from the definition in Nim int64
and because of these comparisons, we are doing these conversions. So if we want to "align the datatypes to Uint256" it would make it quite cumbersome.
Yes, in blockchain contract we use uint256 because it is exposed by the solidity as such (the block.timestamp
property). It does not really make sense to have it uint256
but unfortunately it is the "native datatype" for blockchain which costs the least to use. Any addition "trimming" actually leads to more gas used :-(
Honestly, I would prefer more "align the datatypes to int64" and only do the conversion when data are coming from/to the blockchain (I don't think we will change the contract type for this though). I have started the work on it, but paused it as it was taking me too much time š It is here, but it is nowhere usable yet: https://github.com/codex-storage/nim-codex/pull/638
Weird the value for expiry
that crashes the node is actually fitting very easily even into int64
: 1700813939965
I originally thought that it is "too big for int64" but that is not the case š³
It seems that problem is somewhere around https://github.com/codex-storage/nim-codex/blob/8681a40ee79b260923d49f30dced865172c53b60/codex/contracts/clock.nim#L47-L46
Thanks for more context on this! If it makes most sense to use uint256
in blockchain, I think we should stay with it and try to change the client - so either change client to the also use uint256
or continue with uint64/int64
but handle values out of range gracefully.
Weird the value for expiry that crashes the node is actually fitting very easily even into int64: 1700813939965 I originally thought that it is "too big for int64" but that is not the case š³
That's a bit suspicious š¤ I guess this issue requires a bit more investigation.
If the times module returns an int64
is should be safe to convert to a uint, but not the other way around? A cast
isn't doing any sort of conversion, it's simply doing a blunt bit copy and in general it isn't safe, we should probably simply not use truncate
and rely on explicit conversions, if there are overflows, they should be caught way before we get to calling the contracts... I would start by removing that and see where things are getting messed up.
Err, bit copy
is wrong, it's simply forcing the bits to be interpreted differently, without doing checks or any actual conversions.
If way big expiry timestamp is provided (I managed that because of mistake), then it crashes the nodes in the network and this is a persistent during restarts as the big expiry timestamp is persisted in blockchain.
Logs: