-
Notifications
You must be signed in to change notification settings - Fork 4
Introduced two new traits for spec's serialization and deserialization #190
base: master
Are you sure you want to change the base?
Conversation
Implemented it for all primitives and used types. Added derive macro to simplify implementation.
@@ -488,6 +482,7 @@ impl Input { | |||
.chain(recipient) | |||
.chain(nonce.to_be_bytes()) | |||
.chain(amount.to_be_bytes()) | |||
// TODO: Seems it is a bug and we need to pad `data` with zeros |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the spec, it says "and all other value types are serialized according to the standard transaction serialization".
And seems we need to pad it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, no, because here we do it without any padding=) https://github.com/FuelLabs/fuel-v2-contracts/blob/master/contracts/sidechain/FuelMessagePortal.sol#L130
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/io.rs
Outdated
} | ||
} | ||
|
||
// TODO: Move trait definition to `fuel-types` and derive this implementation for `fuel-asm`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mentioned it in the PR's description=)
Added more comments. Implemented traits for `[T; N]` arrays. It has many usage of `unsafe`, but I did the same change in this PR: paritytech/parity-scale-codec#299
src/io.rs
Outdated
} | ||
|
||
impl<const N: usize, T: Deserialize> Deserialize for [T; N] { | ||
fn decode_static<I: Input + ?Sized>(buffer: &mut I) -> Result<Self, Error> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It contains a lot of unsafe
, but I did the same change here: paritytech/parity-scale-codec#299
So it should be safe=)
…sed in the offset calculation. Added unit tests for fuel types, primitives, vector encode and decode. Fixed typo in the error name.
It is breaking change and we froze the code for testnet. So this change is paused until the code unfreezes. |
Overview
Introduced two new traits -
Serialize
andDeserialize
to implement spec's rules.Each primitive type is padded to have 64 bits alignment.
Vec<u8>
is padded with zeroes.Also implemented derive procedure macro with following rules:
Struct:
encode_static
/decode_static
. It encodes all information required to create an instance of the type into bytes.encode_dynamic
/decode_dynamic
. It encodes the dynamic part of the type. Those methods are implemented only forVec<T>
and[T; N]
.So each not-dynamic type is simply encoded/decoded into/from bytes. For all dynamic types(aka
Vec<T>
), the first iteration leaves sizes, and the second iteration leaves the bytes of inner types.The same rule is applied for each variant of the enum. The discriminant of the enum is
u64
.Reason
This PR aims to automate the implementation of serialization/deserialization instead of manual implementation.
It reduces the chance of an error(if we test auto-generation properly) in the layout and reduces the amount of code.
Another reason for this PR is to create explicit traits/rules of spec's serialization and deserialization(to avoid FuelLabs/fuel-types#51 (comment)). We have several serialization formats for each project area, and the
fuel-core
database format is mixed withfuel-tx
. For example, all types in thefuel-types
haveconst LEN
, which describes the number of bytes. In thefuel-tx
,const LEN
describes the aligned number of bytes.State of the change
The PR entirely replaced
io::Read
andio::Write
implementations. But it doesn't have new tests. All old tests are passed, so serialization is the same as before. I will fill it with tests if we are okay with the change.I cleaned up
io::Read
andio::Write
, but notbytes::SizedBytes
. It can be done in follow-up PR.Also, if we believe in auto-generation(I believe with proper testing), we can remove all manual
consts
files and replace them with auto-calculated sizes.Also, maybe we want to move new traits into
fuel-types
. Because it already containsbytes
module with padding.Adaption of
fuel-vm
for this change, all tests are passed: FuelLabs/fuel-vm#223