[Fix] Return proper JSON from REST APIs
Replaces PR #3606 and fixes issue #3361.
Reproducing the Bug
On staging, run test network using ./devnet.sh. When you query the bonded validator mapping, it should give you the incorrectly formatted output.
~> curl http://localhost:3030/testnet/program/credits.aleo/mapping/bonded/$VALIDATOR_ADDR
"{\n validator: $VALIDATOR_ADDR ,\n microcredits: 187500058346732u64\n}"
(here $VALIDATOR_ADDR is a bonded address, that can be retrieved from the logs)
Testing
On the PR branch (fix/value-json) run another test network and execute the above command again. Now you will get an output with the correct formatting.
~> curl http://localhost:3030/testnet/program/credits.aleo/mapping/bonded/$VALIDATOR_ADDR
{
"validator": "$VALIDATOR_ADDR",
"microcredits": "187500297811718u64"
}
Would the value_to_json and mapping_to_json functions be more useful in snarkVM?
Would the
value_to_jsonandmapping_to_jsonfunctions be more useful in snarkVM?
Yeah, that would make sense if you think there are other use cases for it. Maybe we should even have something like Value::into_json().
Initially, I was also wondering if it makes more sense to just fix the serialization of Value, but I wasn't sure if that would break something else, and the previous PR was already close to finished.
Thank you for picking this up. @kaimast which other endpoints exhibit this issue?
Based on Kai's answer, @HarukaMa @miazn @iamalwaysuncomfortable are you ok if we change the serialization in a backwards-incompatible way? Or will this break existing tooling to an unbearable degree?
I don't think I'm relying on this specific api behavior so I'm not affected.
However, I think the old format is potentially useful if people are manually executing transactions via snarkos developer execute, as the old output format is directly usable as inputs (especially records, not sure if it's applicable here). There would be no other issues if the execute cli can also consume valid json.
However, I think the old format is potentially useful if people are manually executing transactions via
snarkos developer execute, as the old output format is directly usable as inputs (especially records, not sure if it's applicable here). There would be no other issues if the execute cli can also consume valid json.
Thank you for pointing this out. I will test that developer execute still works as expected and fix it, if needed.
@vicsn this issue is tagged for Q4, but I am not sure what we decided on with regard to changing the API behavior. I think it should change, but we could add a flag to force the old behavior.
Also, we haven't decided if the value-to-JSON functionality should reside in snarkVM or remain in snarkOS (as implemented by this PR). The former would require more discussion, as we are trying to avoid API breakage in snarkVM.
This is not priority at the moment but adding this gem here for shared context: https://github.com/ProvableHQ/snarkVM/pull/2404