Skip to content

Commit

Permalink
rebuild artifacts + some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
remybar committed Jan 22, 2025
1 parent fb29638 commit a6870e8
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 3 deletions.
145 changes: 144 additions & 1 deletion crates/dojo/core-cairo-test/src/tests/meta/introspect.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -590,9 +590,152 @@ fn test_array_upgrade() {
}

#[test]
#[available_gas(259000)]
#[available_gas(300000)]
fn test_primitive_upgrade_performance() {
let gas = GasCounterTrait::start();
let _ = Ty::Primitive('ClassHash').is_an_upgrade_of(@Ty::Primitive('ContractAddress'));
gas.end("Upgrade from ContractAddress to ClassHash");
}

#[test]
fn test_key_member_upgrade() {
let s = Struct {
name: 's',
attrs: [].span(),
children: [
Member { name: 'x', attrs: ['key'].span(), ty: Ty::Primitive('u8') },
Member {
name: 'y',
attrs: ['key'].span(),
ty: Ty::Enum(
Enum {
name: 'e',
attrs: [].span(),
children: [('A', Ty::Primitive('u8')), ('B', Ty::Primitive('u16'))].span(),
},
),
},
]
.span(),
};

// primitive type
let mut upgraded = s;
upgraded
.children =
[Member { name: 'x', attrs: ['key'].span(), ty: Ty::Primitive('u128') }, *s.children[1]]
.span();

assert!(upgraded.is_an_upgrade_of(@s), "key primitive type upgrade");

// enum type
let mut upgraded = s;
upgraded
.children =
[
*s.children[0],
Member {
name: 'y',
attrs: ['key'].span(),
ty: Ty::Enum(
Enum {
name: 'e',
attrs: [].span(),
children: [
('A', Ty::Primitive('u8')), ('B', Ty::Primitive('u16')),
('C', Ty::Primitive('u32')),
]
.span(),
},
),
},
]
.span();

assert!(upgraded.is_an_upgrade_of(@s), "key enum type upgrade");

// struct type (not allowed)
let s = Struct {
name: 's',
attrs: [].span(),
children: [
Member {
name: 'x',
attrs: ['key'].span(),
ty: Ty::Struct(Struct { name: 'n', attrs: [].span(), children: [].span() }),
},
]
.span(),
};

let mut upgraded = s;
upgraded
.children =
[
Member {
name: 'x',
attrs: ['key'].span(),
ty: Ty::Struct(
Struct {
name: 'n',
attrs: [].span(),
children: [
Member { name: 'y', attrs: [].span(), ty: Ty::Primitive('u16') },
]
.span(),
},
),
},
]
.span();

assert!(!upgraded.is_an_upgrade_of(@s), "key struct type upgrade");

// array type (not allowed)
let s = Struct {
name: 's',
attrs: [].span(),
children: [
Member {
name: 'x', attrs: ['key'].span(), ty: Ty::Array([Ty::Primitive('u8')].span()),
},
]
.span(),
};

let mut upgraded = s;
upgraded
.children =
[
Member {
name: 'x', attrs: ['key'].span(), ty: Ty::Array([Ty::Primitive('u16')].span()),
},
]
.span();

assert!(!upgraded.is_an_upgrade_of(@s), "key array type upgrade");

// tuple type (not allowed)
let s = Struct {
name: 's',
attrs: [].span(),
children: [
Member {
name: 'x', attrs: ['key'].span(), ty: Ty::Tuple([Ty::Primitive('u8')].span()),
},
]
.span(),
};

let mut upgraded = s;
upgraded
.children =
[
Member {
name: 'x', attrs: ['key'].span(), ty: Ty::Tuple([Ty::Primitive('u16')].span()),
},
]
.span();

assert!(!upgraded.is_an_upgrade_of(@s), "key tuple type upgrade");
}
31 changes: 30 additions & 1 deletion crates/dojo/core/src/meta/introspect.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,36 @@ impl StructCompareImpl of TyCompareTrait<Struct> {

impl MemberCompareImpl of TyCompareTrait<Member> {
fn is_an_upgrade_of(self: @Member, old: @Member) -> bool {
self.name == old.name && self.attrs == old.attrs && self.ty.is_an_upgrade_of(old.ty)
if self.name != old.name || self.attrs != old.attrs {
return false;
}

let mut i = 0;
let is_key = loop {
if i >= (*self).attrs.len() {
break false;
}

if *self.attrs[i] == 'key' {
break true;
}

i += 1;
};

if is_key {
match (self.ty, old.ty) {
(Ty::Primitive(n), Ty::Primitive(o)) => n.is_an_upgrade_of(o),
(Ty::Enum(n), Ty::Enum(o)) => n.is_an_upgrade_of(o),
(Ty::Struct(n), Ty::Struct(o)) => n == o,
(Ty::Array(n), Ty::Array(o)) => n == o,
(Ty::Tuple(n), Ty::Tuple(o)) => n == o,
(Ty::ByteArray, Ty::ByteArray) => true,
_ => false,
}
} else {
self.ty.is_an_upgrade_of(old.ty)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/spawn-and-move/dojo_dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ rpc_url = "http://localhost:5050/"
# Default account for katana with seed = 0
account_address = "0x2af9427c5a277474c079a1283c880ee8a6f0f8fbf73ce969c08d88befec1bba"
private_key = "0x1800000000300000180000000000030000000000003006001800006600"
world_address = "0x266989f02ff93895d29cf079b4e59d8140d459b90fcc46fa71ffa1023bff5f3"
world_address = "0x7dab3fce8fed0cef35d203d8b613a48ddd7700ac7e7a1ba4d8abec4b7c5b14"
ipfs_config.url = "https://ipfs.infura.io:5001"
ipfs_config.username = "2EBrzr7ZASQZKH32sl2xWauXPSA"
ipfs_config.password = "12290b883db9138a8ae3363b6739d220"
Expand Down
Binary file modified spawn-and-move-db.tar.gz
Binary file not shown.
Binary file modified types-test-db.tar.gz
Binary file not shown.

0 comments on commit a6870e8

Please sign in to comment.