Skip to content

Commit

Permalink
Compute packed size
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev committed Sep 27, 2023
1 parent 03c2f5b commit bb5814b
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 9 deletions.
1 change: 1 addition & 0 deletions crates/dojo-core/src/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ trait Model<T> {
fn keys(self: @T) -> Span<felt252>;
fn values(self: @T) -> Span<felt252>;
fn layout(self: @T) -> Span<u8>;
fn packed_size(self: @T) -> usize;
}

#[starknet::interface]
Expand Down
24 changes: 23 additions & 1 deletion crates/dojo-core/src/packing.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use array::{ArrayTrait, SpanTrait};
use traits::{Into, TryInto};
use integer::{U256BitAnd, U256BitOr, U256BitXor, upcast, downcast, BoundedInt};
use option::OptionTrait;
use debug::PrintTrait;

fn pack(ref packed: Array<felt252>, ref unpacked: Span<felt252>, ref layout: Span<u8>) {
assert(unpacked.len() == layout.len(), 'mismatched input lens');
Expand All @@ -22,6 +21,29 @@ fn pack(ref packed: Array<felt252>, ref unpacked: Span<felt252>, ref layout: Spa
packed.append(packing);
}

fn calculate_packed_size(ref layout: Span<u8>) -> usize {
let mut size = 1;
let mut partial = 0_usize;

loop {
match layout.pop_front() {
Option::Some(item) => {
let item_size: usize = (*item).into();
partial += item_size;
if (partial > 251) {
size += 1;
partial = item_size;
}
},
Option::None(_) => {
break;
}
};
};

size
}

fn unpack(ref unpacked: Array<felt252>, ref packed: Span<felt252>, ref layout: Span<u8>) {
let mut unpacking: felt252 = 0x0;
let mut offset: u8 = 251;
Expand Down
30 changes: 29 additions & 1 deletion crates/dojo-core/src/packing_test.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use array::{ArrayTrait, SpanTrait};
use starknet::{ClassHash, ContractAddress, Felt252TryIntoContractAddress, Felt252TryIntoClassHash};
use dojo::packing::{shl, shr, fpow, pack, unpack, pack_inner, unpack_inner};
use dojo::packing::{shl, shr, fpow, pack, unpack, pack_inner, unpack_inner, calculate_packed_size};
use integer::U256BitAnd;
use option::OptionTrait;
use debug::PrintTrait;
Expand Down Expand Up @@ -312,3 +312,31 @@ fn test_pack_unpack_felt252_single() {
let output = serde::Serde::<felt252>::deserialize(ref unpacked_span).unwrap();
assert(input == output, 'invalid output');
}

#[test]
#[available_gas(9000000)]
fn test_calculate_packed_size() {
let mut layout = array![128, 32].span();
let got = calculate_packed_size(ref layout);
assert(got == 1, 'invalid length for [128, 32]');

let mut layout = array![128, 128].span();
let got = calculate_packed_size(ref layout);
assert(got == 2, 'invalid length for [128, 128]');

let mut layout = array![251, 251].span();
let got = calculate_packed_size(ref layout);
assert(got == 2, 'invalid length for [251, 251]');

let mut layout = array![251].span();
let got = calculate_packed_size(ref layout);
assert(got == 1, 'invalid length for [251]');

let mut layout = array![32, 64, 128, 27].span();
let got = calculate_packed_size(ref layout);
assert(got == 1, 'invalid length');

let mut layout = array![32, 64, 128, 28].span();
let got = calculate_packed_size(ref layout);
assert(got == 2, 'invalid length');
}
4 changes: 2 additions & 2 deletions crates/dojo-erc/src/tests/erc721_tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ fn test__set_approval_for_all_owner_equal_operator_false() {
//

#[test]
#[available_gas(50000000)]
#[available_gas(60000000)]
fn test_transfer_from_owner() {
let mut state = setup();
let token_id = TOKEN_ID;
Expand Down Expand Up @@ -1359,7 +1359,7 @@ fn test__burn() {
}

#[test]
#[available_gas(20000000)]
#[available_gas(30000000)]
#[should_panic(expected: ('ERC721: invalid token ID',))]
fn test__burn_nonexistent() {
let (mut world, mut state) = STATE();
Expand Down
9 changes: 6 additions & 3 deletions crates/dojo-lang/src/inline_macros/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,14 @@ impl InlineMacroExprPlugin for GetMacro {
"\n let mut __{component}_layout__ = array::ArrayTrait::new();
dojo::database::schema::SchemaIntrospection::<{component}>::layout(ref \
__{component}_layout__);
let __{component}_layout_span__ = \
let mut __{component}_layout_clone__ = __{component}_layout__.clone();
let mut __{component}_layout_span__ = \
array::ArrayTrait::span(@__{component}_layout__);
let mut __{component}_layout_clone_span__ = \
array::ArrayTrait::span(@__{component}_layout_clone__);
let mut __{component}_values__ = {}.entity('{component}', __get_macro_keys__, \
0_u8, dojo::database::schema::SchemaIntrospection::<{component}>::size(), \
__{component}_layout_span__);
0_u8, dojo::packing::calculate_packed_size(ref \
__{component}_layout_clone_span__), __{component}_layout_span__);
let mut __{component}_component__ = array::ArrayTrait::new();
array::serialize_array_helper(__get_macro_keys__, ref __{component}_component__);
array::serialize_array_helper(__{component}_values__, ref \
Expand Down
4 changes: 2 additions & 2 deletions crates/dojo-lang/src/manifest_test_data/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ test_manifest_file
{
"name": "player_actions",
"address": null,
"class_hash": "0x48465b92d0b49ae15999132b88c619b84a0ac7877f92e134a2fef77d387035e",
"class_hash": "0x76e85c6abaa082687466201db4edc0427ca814c6a89d962b4c884ba365bee17",
"abi": [
{
"type": "impl",
Expand Down Expand Up @@ -675,7 +675,7 @@ test_manifest_file
{
"name": "player_actions_external",
"address": null,
"class_hash": "0x17a570b3ae75ace7bad6da4581f55356dc3325fa2b454913372f6976d8d5b40",
"class_hash": "0x66af9023e04e381743a83314fbe1bb7dc63c9aeebee6bbeb96578423fe53a9d",
"abi": [
{
"type": "impl",
Expand Down
8 changes: 8 additions & 0 deletions crates/dojo-lang/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,16 @@ pub fn handle_model_struct(
dojo::database::schema::SchemaIntrospection::<$type_name$>::layout(ref layout);
array::ArrayTrait::span(@layout)
}
#[inline(always)]
fn packed_size(self: @$type_name$) -> usize {
let mut layout = self.layout();
dojo::packing::calculate_packed_size(ref layout)
}
}
$schema_introspection$
#[starknet::interface]
trait I$type_name$<T> {
fn name(self: @T) -> felt252;
Expand Down
8 changes: 8 additions & 0 deletions crates/dojo-lang/src/plugin_test_data/introspect
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,15 @@ impl PositionModel of dojo::model::Model<Position> {
dojo::database::schema::SchemaIntrospection::<Position>::layout(ref layout);
array::ArrayTrait::span(@layout)
}

#[inline(always)]
fn packed_size(self: @Position) -> usize {
let mut layout = self.layout();
dojo::packing::calculate_packed_size(ref layout)
}
}


impl PositionSchemaIntrospection of dojo::database::schema::SchemaIntrospection<Position> {
#[inline(always)]
fn size() -> usize {
Expand Down Expand Up @@ -154,6 +161,7 @@ impl PositionSchemaIntrospection of dojo::database::schema::SchemaIntrospection<
}
}


#[starknet::interface]
trait IPosition<T> {
fn name(self: @T) -> felt252;
Expand Down
24 changes: 24 additions & 0 deletions crates/dojo-lang/src/plugin_test_data/model
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,15 @@ impl PositionModel of dojo::model::Model<Position> {
dojo::database::schema::SchemaIntrospection::<Position>::layout(ref layout);
array::ArrayTrait::span(@layout)
}

#[inline(always)]
fn packed_size(self: @Position) -> usize {
let mut layout = self.layout();
dojo::packing::calculate_packed_size(ref layout)
}
}


impl PositionSchemaIntrospection of dojo::database::schema::SchemaIntrospection<Position> {
#[inline(always)]
fn size() -> usize {
Expand Down Expand Up @@ -138,6 +145,7 @@ impl PositionSchemaIntrospection of dojo::database::schema::SchemaIntrospection<
}
}


#[starknet::interface]
trait IPosition<T> {
fn name(self: @T) -> felt252;
Expand Down Expand Up @@ -226,8 +234,15 @@ impl RolesModel of dojo::model::Model<Roles> {
dojo::database::schema::SchemaIntrospection::<Roles>::layout(ref layout);
array::ArrayTrait::span(@layout)
}

#[inline(always)]
fn packed_size(self: @Roles) -> usize {
let mut layout = self.layout();
dojo::packing::calculate_packed_size(ref layout)
}
}


impl RolesSchemaIntrospection of dojo::database::schema::SchemaIntrospection<Roles> {
#[inline(always)]
fn size() -> usize {
Expand Down Expand Up @@ -260,6 +275,7 @@ impl RolesSchemaIntrospection of dojo::database::schema::SchemaIntrospection<Rol
}
}


#[starknet::interface]
trait IRoles<T> {
fn name(self: @T) -> felt252;
Expand Down Expand Up @@ -336,8 +352,15 @@ impl PlayerModel of dojo::model::Model<Player> {
dojo::database::schema::SchemaIntrospection::<Player>::layout(ref layout);
array::ArrayTrait::span(@layout)
}

#[inline(always)]
fn packed_size(self: @Player) -> usize {
let mut layout = self.layout();
dojo::packing::calculate_packed_size(ref layout)
}
}


impl PlayerSchemaIntrospection of dojo::database::schema::SchemaIntrospection<Player> {
#[inline(always)]
fn size() -> usize {
Expand Down Expand Up @@ -384,6 +407,7 @@ impl PlayerSchemaIntrospection of dojo::database::schema::SchemaIntrospection<Pl
}
}


#[starknet::interface]
trait IPlayer<T> {
fn name(self: @T) -> felt252;
Expand Down
1 change: 1 addition & 0 deletions crates/torii/graphql/src/object/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl Default for ModelObject {
}
}
}

impl ModelObject {
pub fn value_mapping(model: Model) -> ValueMapping {
IndexMap::from([
Expand Down

0 comments on commit bb5814b

Please sign in to comment.