metaplex-foundation / mpl-token-metadata

Program to attach additional data to Fungible or Non-Fungible tokens on Solana.
https://developers.metaplex.com/token-metadata
Other
99 stars 42 forks source link

program: Update serialization to `borsh::to_writer` #32

Closed joncinque closed 10 months ago

joncinque commented 11 months ago

Problem

The token-metadata program uses the serialize function directly from Borsh, which has the unfortunate side-effect of consuming the underlying slice.

More information about the issue at https://github.com/near/borsh-rs/pull/34

Solution

Use the borsh::to_writer API which properly moves the Writer in order to serialize the data. You can see how this was done in SPL at https://github.com/solana-labs/solana-program-library/pull/4228.

This will avoid any gotchas already present in the code. In programs/token-metadata/program/src/state/mod.rs, there's this comment:

// passes a slice to borsh so the internal account data array does not get
// temporarily resized

Using the borsh::to_writer API, you never need to worry about that!

joncinque commented 11 months ago

Not sure why CI is failing, but the important bits of building and testing the program pass!

febo commented 10 months ago

Not sure why CI is failing, but the important bits of building and testing the program pass!

CI fails for the client part since it needs a token, which is not available on forks. But as you said, the important bits of building and testing are passing.

Thank you!