Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add proper support for borsh schema containers in ABI #872

Merged
merged 5 commits into from
Aug 1, 2022

Conversation

itegulov
Copy link
Contributor

@itegulov itegulov commented Jul 27, 2022

The main problem with this approach is that all schema containers are independent of each other. This can make the ABI files bloated when working with many complex types. Unfortunately, it seems like borsh-rs does not have a generator model like schemars does, so I can't generate one big schema like I do with JSON Schema. Maybe this is something that would make sense to add to borsh-rs in the future though?

Related to near/cargo-near#14

},
Struct {
#[serde(with = "FieldsDef")]
fields: Fields,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fields key feels redundant. Is there a reason this and others can't be inlined to avoid unnecessary data in abi?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am just following borsh-rs here: https://github.com/near/borsh-rs/blob/7325a0aab74049237b9f978e1e2c0fcf7bb799c2/borsh/src/schema.rs#L46

The structs need to be duplicated and match the other struct one for one so I can use serde's remote derivation (see https://serde.rs/remote-derive.html).

Comment on lines 87 to 94
"Struct": {
"fields": {
"UnnamedFields": [
"u32",
"u32"
]
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intuitively, feels like this should be

Suggested change
"Struct": {
"fields": {
"UnnamedFields": [
"u32",
"u32"
]
}
}
"Struct": [
"u32",
"u32"
]

And I can't see why you wouldn't be able to do this because the struct is either a JSON map, an array, or empty (empty map or null, idk what it's used for), am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yeah that's completely fair. I will see if I can use remote derivation with custom serde attributes to control the format a bit more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! This introduced even more complexity to the code, but at least it seems to work. This definitely needs some tests though, will add them next week.

Copy link
Contributor

@austinabell austinabell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The workarounds seem a bit unnecessary since we can modify borsh to include clone and update the schema format, but this seems fine for now to unlock the functionality.

Can you open an issue in borsh identifying how we want the serde serialization format to look, so we can get some input before making any changes to avoid the extra logic here? I opened near/borsh-rs#97 because it was unopinionated

@itegulov
Copy link
Contributor Author

itegulov commented Aug 1, 2022

I finished tests and this seems to behave as I expect it to, so I will merge this.

@austinabell created the serde derivation issue in borsh repo: near/borsh-rs#98

@itegulov itegulov merged commit d784831 into master Aug 1, 2022
@itegulov itegulov deleted the daniyar/borsh-support branch August 1, 2022 07:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants