provenance-io / explorer-service

The server side of the blockchain Explorer app. Allows for UI browsing of the blockchain.
Apache License 2.0
6 stars 3 forks source link

Validator Priority displayed as 32-bit int but is a 64-bit int from the chain. #530

Closed SpicyLemon closed 2 months ago

SpicyLemon commented 2 months ago

Summary of Bug

On the block page, (e.g. 18318664), the "Proposer Priority" values do not match what the provenanced q comet-validator-set has for each validator.

Steps to Reproduce

$ provenanced q comet-validator-set 18318664 | jq -r '.validators|sort_by(.proposer_priority|tonumber)|.[]|.address + " " + .proposer_priority' | grep 89r
pbvalcons14z3qgnjh3d2gexl33gzrcrk70y9cscllksn89r -4976622813127

That pbvalcons14z3qgnjh3d2gexl33gzrcrk70y9cscllksn89r is TreeStaker.

If you look at the block page, it'll show that TreeStaker's "Proposer Priority" for that block was 1244282937 (instead of -4976622813127).

$ printf '%s\n' $(( -4976622813127 & 4294967295 ))
1244282937

Then, since 1244282937 is still within the bounds of 32-bit int, that's the result of casting -4976622813127 into a 32-bit integer. That 1244282937 is what explorer lists as Treestakers proposer priority for that block.

If it were larger (it'll never be smaller because the sign-bit is now zero), we would want to xor it with 4294967295, add one, then put a negative sign on it.

E.g.

$ printf '%s\n' "-$(( ( ( ( 5719164737899 ) & 4294967295 ) ^ 4294967295 ) + 1 ))"
-1731700373

Those 5719164737899 and -1731700373 are Treestaker's proposer priority for block 18318663 (actual and display respectively).


For Admin Use

SpicyLemon commented 2 months ago

Here's a shell function for converting a number to a 32-bit int:

$ to_int32 () { if [[ "$1" -le '2147483647' && "$1" -ge '-2147483648' ]]; then printf '%s\n' "$1"; return 0; fi; local v; v=$(( $1 & 4294967295 )); if [[ "$v" -gt '2147483647' ]]; then v="-$(( ( v ^ 4294967295 ) + 1 ))"; fi; printf '%s\n' "$v"; }

Example uses:

$ to_int32 -4976622813127
1244282937
$ to_int32 5719164737899
-1731700373

I'm not sure how much I trust it since I'm pretty rusty with bit math, but it's correct for all the ones I've tried so far. I wouldn't be surprised if I made it harder than it needed to be though.