Skip to content

Commit

Permalink
fix: Asset struct serialization does not match Noir internal serializ…
Browse files Browse the repository at this point in the history
…ation (#6494)
  • Loading branch information
dbanks12 authored May 17, 2024
1 parent 690b2de commit 9e6a4c3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ struct Asset {
oracle: AztecAddress,
}

global SERIALIZED_LEN: Field = 4;
global SERIALIZED_LEN: Field = 6;

impl Serialize<SERIALIZED_LEN> for Asset {
fn serialize(Asset: Asset) -> [Field; SERIALIZED_LEN] {
[
Asset.interest_accumulator.to_integer(),
Asset.interest_accumulator.lo,
Asset.interest_accumulator.hi,
Asset.last_updated_ts as Field,
Asset.loan_to_value.to_integer(),
Asset.loan_to_value.lo,
Asset.loan_to_value.hi,
Asset.oracle.to_field()
]
}
Expand All @@ -30,10 +32,10 @@ impl Deserialize<SERIALIZED_LEN> for Asset {
// Right now we are wasting so many writes. If changing last_updated_ts
// we will end up rewriting all of them, wasting writes.
fn deserialize(fields: [Field; SERIALIZED_LEN]) -> Asset {
let interest_accumulator = U128::from_integer(fields[0]);
let last_updated_ts = fields[1] as u64;
let loan_to_value = U128::from_integer(fields[2]);
let oracle = AztecAddress::from_field(fields[3]);
let interest_accumulator = U128 { lo: fields[0], hi: fields[1] };
let last_updated_ts = fields[2] as u64;
let loan_to_value = U128 { lo: fields[3], hi: fields[4] };
let oracle = AztecAddress::from_field(fields[5]);

Asset {
interest_accumulator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ struct Asset {
price: U128,
}

global ASSET_SERIALIZED_LEN: Field = 1;
global ASSET_SERIALIZED_LEN: Field = 2;

impl Serialize<ASSET_SERIALIZED_LEN> for Asset {
fn serialize(asset: Asset) -> [Field; ASSET_SERIALIZED_LEN] {
[asset.price.to_integer()]
[asset.price.lo, asset.price.hi]
}
}

impl Deserialize<ASSET_SERIALIZED_LEN> for Asset {
fn deserialize(fields: [Field; ASSET_SERIALIZED_LEN]) -> Asset {
let price = U128::from_integer(fields[0]);
let price = U128 { lo: fields[0], hi: fields[1] };
Asset { price }
}
}

0 comments on commit 9e6a4c3

Please sign in to comment.