Skip to content

Commit

Permalink
[cherry-pick][sui-json] bcs serde fallback (#10995)
Browse files Browse the repository at this point in the history
## Description 

try to (de)serialize, and if fails, try to return some semblance of
`input` by falling back to `JsonValue::Array`.


## Test Plan 

Verify that making the call below yields response with `transaction`
when `showInput` is true:
```
curl --location --request POST 'http://ewr-tnt-rpc-12.testnet.sui.io:9000' \
--header 'Content-Type: application/json' \
--data-raw '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "sui_getTransactionBlock", "params": ["JBBRdsd4UnrQ8haRYS2RoCkEJFFoUduSATZ78oBSdkhC",     {
      "showInput": true,
      "showRawInput": false,
      "showEffects": true,
      "showEvents": false,
      "showObjectChanges": false,
      "showBalanceChanges": false
    }]
}'
```
Result:


`{"jsonrpc":"2.0","result":{"digest":"JBBRdsd4UnrQ8haRYS2RoCkEJFFoUduSATZ78oBSdkhC","transaction":{"data":{"messageVersion":"v1","transaction":{"kind":"ProgrammableTransaction","inputs":[{"type":"object","objectType":"immOrOwnedObject","objectId":"0x0c6a196a08a410d540f32574e023e38e3083bcb4d21efc9629991c89c4db38e8","version":"3076698","digest":"CvRsjGRqkvGFeQtygL9Ctpw8DV9zkuvQP39jnN4JCzkh"},{"type":"object","objectType":"sharedObject","objectId":"0xe33f687cb3bfcfc9cf7977e44a17d0f52149135749707796817ec50693f76722","initialSharedVersion":"3076696","mutable":true},{"type":"pure","valueType":"vector<address>","value":[2,148,239,44,104,164,158,158,39,94,14,74,123,139,61,135,32,85,210,90,215,100,149,83,174,187,53,228,252,246,120,160,242,10,0]}],"transactions":[{"MoveCall":{"package":"0xa6631e8990a6b0225486ea4774fe5f95a2ceef1d58185d8d04404df3960d2ab0","module":"belaunch","function":"update_whitelist","type_arguments":["0xb2ec21c5d4eb1e8c6d915c4a3ec6c5eaada8d51dc0f756d48dea7007f1a301c6::spike::SPIKE","0x2::sui::SUI"],"arguments":[{"Input":0},{"Input":1},{"Input":2}]}}]},"sender":"0x94ef2c68a49e9e275e0e4a7b8b3d872055d25ad7649553aebb35e4fcf678a0f2","gasData":{"payment":[{"objectId":"0x00464f409ff3724c0b81aeeba88b34341ebfd307b6727ef3863417900edbbb56","version":3076699,"digest":"6pVp5oqf9593MVhERsZXFfNBwChoXWhZLHTWGtrdDLnV"}],"owner":"0x94ef2c68a49e9e275e0e4a7b8b3d872055d25ad7649553aebb35e4fcf678a0f2","price":"1000","budget":"144064000"}},"txSignatures":["AIcbke/3Ch15N8avO8sgiBMvTnJy1p2joXq/EfOmJybakv5O3BgVdZPVUHMCXG5oM4P50TlBel2/MrMUPp536wpRgjTy77TpQBYfOmtlc0Snm9kgoerRf8He8UjP48R/dA=="]},"effects":{"messageVersion":"v1","status":{"status":"failure","error":"CommandArgumentError
{ arg_idx: 2, kind: InvalidBCSBytes } in command

0"},"executedEpoch":"755","gasUsed":{"computationCost":"1000000","storageCost":"7197200","storageRebate":"7125228","nonRefundableStorageFee":"71972"},"modifiedAtVersions":[{"objectId":"0x00464f409ff3724c0b81aeeba88b34341ebfd307b6727ef3863417900edbbb56","sequenceNumber":"3076699"},{"objectId":"0x0c6a196a08a410d540f32574e023e38e3083bcb4d21efc9629991c89c4db38e8","sequenceNumber":"3076698"},{"objectId":"0xe33f687cb3bfcfc9cf7977e44a17d0f52149135749707796817ec50693f76722","sequenceNumber":"3076698"}],"sharedObjects":[{"objectId":"0xe33f687cb3bfcfc9cf7977e44a17d0f52149135749707796817ec50693f76722","version":3076698,"digest":"66aEDuuZ1pdUCJUPptEtrVxout4aG6kHbVxHEvgGGNao"}],"transactionDigest":"JBBRdsd4UnrQ8haRYS2RoCkEJFFoUduSATZ78oBSdkhC","mutated":[{"owner":{"AddressOwner":"0x94ef2c68a49e9e275e0e4a7b8b3d872055d25ad7649553aebb35e4fcf678a0f2"},"reference":{"objectId":"0x00464f409ff3724c0b81aeeba88b34341ebfd307b6727ef3863417900edbbb56","version":3076700,"digest":"DqgEASmjad6jiSgrasNqmQFsFYzkC7UpPCgmpLGJnPdZ"}},{"owner":{"AddressOwner":"0x94ef2c68a49e9e275e0e4a7b8b3d872055d25ad7649553aebb35e4fcf678a0f2"},"reference":{"objectId":"0x0c6a196a08a410d540f32574e023e38e3083bcb4d21efc9629991c89c4db38e8","version":3076700,"digest":"CbvPi1jjpwGT8eXyHvxGjJPXkt18CUHyEtnqqLJhaW2Y"}},{"owner":{"Shared":{"initial_shared_version":3076696}},"reference":{"objectId":"0xe33f687cb3bfcfc9cf7977e44a17d0f52149135749707796817ec50693f76722","version":3076700,"digest":"F2wPHvqs2jjWsiA8JrR7BZXajirpRFSssChFNRDEADBv"}}],"gasObject":{"owner":{"AddressOwner":"0x94ef2c68a49e9e275e0e4a7b8b3d872055d25ad7649553aebb35e4fcf678a0f2"},"reference":{"objectId":"0x00464f409ff3724c0b81aeeba88b34341ebfd307b6727ef3863417900edbbb56","version":3076700,"digest":"DqgEASmjad6jiSgrasNqmQFsFYzkC7UpPCgmpLGJnPdZ"}},"dependencies":["4GKRjuiNwC31Kw2sNuV3RX964X8hH2E5sNDbd7pAGoem","6VxtoY5d85csiV5ic41tEkqV3PDGUD1fCsQG6FGAJtWo","BXubZDY4FnqK1qU78H8zNjEHZ8wtweQPFEHxvirhfJtQ","C2zZu2dpX2sLQy2234yt6ecRiNTVgQTXeQpgw9GhxGgo","C5a23ccFjTACtCnVXtwjasXhdc4jVAVDLSWbwwzm1oYp"]},"timestampMs":"1681265989581","checkpoint":"1064672"},"id":1}%`

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes

## Description 

Describe the changes or additions included in this PR.

## Test Plan 

How did you test the new or updated feature?

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
wlmyng authored Apr 17, 2023
1 parent 3aa6dc4 commit cab7e97
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions crates/sui-json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,29 @@ impl SuiJsonValue {
if let Some(s) = try_parse_string(layout, bytes) {
json!(s)
} else {
let move_value = bcs::from_bytes_seed(layout, bytes)?;
move_value_to_json(&move_value).unwrap_or_else(|| {
// fallback to array[u8] if fail to convert to json.
JsonValue::Array(
bytes
.iter()
.map(|b| JsonValue::Number(Number::from(*b)))
.collect(),
)
})
let result = bcs::from_bytes_seed(layout, bytes).map_or_else(
|_| {
// fallback to array[u8] if fail to convert to json.
JsonValue::Array(
bytes
.iter()
.map(|b| JsonValue::Number(Number::from(*b)))
.collect(),
)
},
|move_value| {
move_value_to_json(&move_value).unwrap_or_else(|| {
// fallback to array[u8] if fail to convert to json.
JsonValue::Array(
bytes
.iter()
.map(|b| JsonValue::Number(Number::from(*b)))
.collect(),
)
})
},
);
result
}
} else {
json!(bytes)
Expand Down

0 comments on commit cab7e97

Please sign in to comment.