From f4fa34da18951087549670375af40b4177c34fb1 Mon Sep 17 00:00:00 2001 From: Arcticae Date: Tue, 22 Aug 2023 11:03:14 +0200 Subject: [PATCH 1/9] Use cheatable entrypoint in constructors --- .../account_cairo1.casm.json | 1 + crates/cheatnet/src/cheatcodes/deploy.rs | 85 ++++------ crates/cheatnet/src/constants.rs | 32 ++-- crates/cheatnet/src/conversions.rs | 21 +++ crates/cheatnet/src/rpc.rs | 151 ++++++++++++++++-- crates/forge/src/cheatcodes_hint_processor.rs | 11 +- crates/forge/tests/integration/roll.rs | 17 +- crates/forge/tests/integration/warp.rs | 14 +- 8 files changed, 232 insertions(+), 100 deletions(-) create mode 100644 crates/cheatnet/predeployed-contracts/account_cairo1.casm.json diff --git a/crates/cheatnet/predeployed-contracts/account_cairo1.casm.json b/crates/cheatnet/predeployed-contracts/account_cairo1.casm.json new file mode 100644 index 0000000000..eb834f483f --- /dev/null +++ b/crates/cheatnet/predeployed-contracts/account_cairo1.casm.json @@ -0,0 +1 @@ +{"prime":"0x800000000000011000000000000000000000000000000000000000000000001","compiler_version":"2.1.1","bytecode":["0xa0680017fff8000","0x7","0x482680017ffa8000","0x100000000000000000000000000000000","0x400280007ff97fff","0x10780017fff7fff","0x78","0x4825800180007ffa","0x0","0x400280007ff97fff","0x480a7ffc7fff8000","0x480a7ffd7fff8000","0x1104800180018000","0x31c","0x482680017ff98000","0x1","0x20680017fff7ffd","0x5f","0x48127ffb7fff8000","0x48127ffb7fff8000","0x1104800180018000","0x314","0x20680017fff7ffe","0x4b","0x48307ffc80007ffd","0x4824800180007fff","0x0","0x20680017fff7fff","0x4","0x10780017fff7fff","0x13","0x480a7ffb7fff8000","0x1104800180018000","0x32c","0x40780017fff7fff","0x1","0x480680017fff8000","0x496e70757420746f6f206c6f6e6720666f7220617267756d656e7473","0x400080007ffe7fff","0x48127fe87fff8000","0x48127fd77fff8000","0x48127ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x1104800180018000","0x539","0x482480017fff8000","0x538","0x480080007fff8000","0xa0680017fff8000","0x9","0x4824800180007fd9","0x0","0x482480017fff8000","0x100000000000000000000000000000000","0x400080007fe77fff","0x10780017fff7fff","0x16","0x4824800180007fd9","0x0","0x400080007fe87fff","0x40780017fff7fff","0x1","0x480680017fff8000","0x56414c4944","0x48127ffe7fff8000","0x48127ffd7fff8000","0x1104800180018000","0x307","0x482480017fe08000","0x1","0x48127ff67fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x0","0x48127ffa7fff8000","0x48127ffa7fff8000","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x4f7574206f6620676173","0x400080007ffe7fff","0x482480017fe58000","0x1","0x48127fd47fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x496e70757420746f6f2073686f727420666f7220617267756d656e7473","0x400080007ffe7fff","0x48127fee7fff8000","0x48127fdd7fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x496e70757420746f6f2073686f727420666f7220617267756d656e7473","0x400080007ffe7fff","0x48127ffd7fff8000","0x48127fec7fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x4f7574206f6620676173","0x400080007ffe7fff","0x482680017ff98000","0x1","0x480a7ffa7fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0xa0680017fff8000","0x7","0x482680017ffa8000","0x100000000000000000000000000000000","0x400280007ff97fff","0x10780017fff7fff","0x64","0x4825800180007ffa","0x0","0x400280007ff97fff","0x480a7ffc7fff8000","0x480a7ffd7fff8000","0x1104800180018000","0x290","0x482680017ff98000","0x1","0x20680017fff7ffd","0x4b","0x48307ffb80007ffc","0x4824800180007fff","0x0","0x20680017fff7fff","0x4","0x10780017fff7fff","0x13","0x480a7ffb7fff8000","0x1104800180018000","0x2a6","0x40780017fff7fff","0x1","0x480680017fff8000","0x496e70757420746f6f206c6f6e6720666f7220617267756d656e7473","0x400080007ffe7fff","0x48127ff77fff8000","0x48127fe67fff8000","0x48127ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x1104800180018000","0x4b3","0x482480017fff8000","0x4b2","0x480080007fff8000","0xa0680017fff8000","0x9","0x4824800180007fe8","0x0","0x482480017fff8000","0x100000000000000000000000000000000","0x400080007ff67fff","0x10780017fff7fff","0x16","0x4824800180007fe8","0x0","0x400080007ff77fff","0x40780017fff7fff","0x1","0x480680017fff8000","0x56414c4944","0x48127ffe7fff8000","0x48127ffd7fff8000","0x1104800180018000","0x281","0x482480017fef8000","0x1","0x48127ff67fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x0","0x48127ffa7fff8000","0x48127ffa7fff8000","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x4f7574206f6620676173","0x400080007ffe7fff","0x482480017ff48000","0x1","0x48127fe37fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x496e70757420746f6f2073686f727420666f7220617267756d656e7473","0x400080007ffe7fff","0x48127ffd7fff8000","0x48127fec7fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x4f7574206f6620676173","0x400080007ffe7fff","0x482680017ff98000","0x1","0x480a7ffa7fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0xa0680017fff8000","0x7","0x482680017ffa8000","0xffffffffffffffffffffffffffffed2c","0x400280007ff97fff","0x10780017fff7fff","0x98","0x4825800180007ffa","0x12d4","0x400280007ff97fff","0x482680017ff98000","0x1","0x480a7ffc7fff8000","0x480a7ffd7fff8000","0x1104800180018000","0x241","0x20680017fff7ffe","0x7f","0x48127ffc7fff8000","0x48127ffc7fff8000","0x1104800180018000","0x210","0x20680017fff7ffe","0x6b","0x48127fec7fff8000","0x48127fd07fff8000","0x48127ffa7fff8000","0x48127ffa7fff8000","0x1104800180018000","0x26c","0x20680017fff7ffa","0x5b","0x20680017fff7ffd","0x4b","0x48307ffb80007ffc","0x4824800180007fff","0x0","0x20680017fff7fff","0x4","0x10780017fff7fff","0x13","0x480a7ffb7fff8000","0x1104800180018000","0x21e","0x40780017fff7fff","0x1","0x480680017fff8000","0x496e70757420746f6f206c6f6e6720666f7220617267756d656e7473","0x400080007ffe7fff","0x48127ff07fff8000","0x48127ff07fff8000","0x48127ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x1104800180018000","0x42b","0x482480017fff8000","0x42a","0x480080007fff8000","0xa0680017fff8000","0x9","0x4824800180007ff2","0x0","0x482480017fff8000","0x100000000000000000000000000000000","0x400080007fef7fff","0x10780017fff7fff","0x16","0x4824800180007ff2","0x0","0x400080007ff07fff","0x40780017fff7fff","0x1","0x480680017fff8000","0x56414c4944","0x48127ffe7fff8000","0x48127ffd7fff8000","0x1104800180018000","0x1f9","0x482480017fe88000","0x1","0x48127ff67fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x0","0x48127ffa7fff8000","0x48127ffa7fff8000","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x4f7574206f6620676173","0x400080007ffe7fff","0x482480017fed8000","0x1","0x48127fed7fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x496e70757420746f6f2073686f727420666f7220617267756d656e7473","0x400080007ffe7fff","0x48127ff67fff8000","0x48127ff67fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x48127ff87fff8000","0x48127ff87fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x48127ffa7fff8000","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x496e70757420746f6f2073686f727420666f7220617267756d656e7473","0x400080007ffe7fff","0x48127fea7fff8000","0x48127fce7fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x496e70757420746f6f2073686f727420666f7220617267756d656e7473","0x400080007ffe7fff","0x48127ff97fff8000","0x48127fdd7fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x4f7574206f6620676173","0x400080007ffe7fff","0x482680017ff98000","0x1","0x480a7ffa7fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x40780017fff7fff","0x2","0xa0680017fff8000","0x7","0x482680017ffa8000","0xffffffffffffffffffffffffffff8288","0x400280007ff97fff","0x10780017fff7fff","0xa5","0x4825800180007ffa","0x7d78","0x400280007ff97fff","0x482680017ff98000","0x1","0x480a7ffc7fff8000","0x480a7ffd7fff8000","0x1104800180018000","0x193","0x20680017fff7ffe","0x8c","0x48127ffc7fff8000","0x48127ffc7fff8000","0x1104800180018000","0x162","0x40137ff07fff8000","0x20680017fff7ffe","0x77","0x48127fec7fff8000","0x48127fd07fff8000","0x48127ffa7fff8000","0x48127ffa7fff8000","0x40137ffb7fff8001","0x1104800180018000","0x1bc","0x20680017fff7ffa","0x66","0x20680017fff7ffd","0x56","0x48307ffb80007ffc","0x4824800180007fff","0x0","0x20680017fff7fff","0x4","0x10780017fff7fff","0x13","0x480a7ffb7fff8000","0x1104800180018000","0x16e","0x40780017fff7fff","0x1","0x480680017fff8000","0x496e70757420746f6f206c6f6e6720666f7220617267756d656e7473","0x400080007ffe7fff","0x48127ff07fff8000","0x48127ff07fff8000","0x48127ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x1104800180018000","0x37b","0x482480017fff8000","0x37a","0x480080007fff8000","0xa0680017fff8000","0x9","0x4824800180007ff2","0x0","0x482480017fff8000","0x100000000000000000000000000000000","0x400080007fef7fff","0x10780017fff7fff","0x21","0x4824800180007ff2","0x0","0x400080007ff07fff","0x48127fff7fff8000","0x480a7ffb7fff8000","0x480a80007fff8000","0x480a80017fff8000","0x48127ff27fff8000","0x48127ff27fff8000","0x1104800180018000","0x1cf","0x482480017fb68000","0x1","0x20680017fff7ffc","0xa","0x48127fff7fff8000","0x48127ff97fff8000","0x48127ff97fff8000","0x480680017fff8000","0x0","0x48127ff97fff8000","0x48127ff97fff8000","0x208b7fff7fff7ffe","0x48127fff7fff8000","0x48127ff97fff8000","0x48127ff97fff8000","0x480680017fff8000","0x1","0x48127ff97fff8000","0x48127ff97fff8000","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x4f7574206f6620676173","0x400080007ffe7fff","0x482480017fed8000","0x1","0x48127fed7fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x496e70757420746f6f2073686f727420666f7220617267756d656e7473","0x400080007ffe7fff","0x48127ff67fff8000","0x48127ff67fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x48127ff87fff8000","0x48127ff87fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x48127ffa7fff8000","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x496e70757420746f6f2073686f727420666f7220617267756d656e7473","0x400080007ffe7fff","0x48127fea7fff8000","0x48127fce7fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x496e70757420746f6f2073686f727420666f7220617267756d656e7473","0x400080007ffe7fff","0x48127ff97fff8000","0x48127fdd7fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x4f7574206f6620676173","0x400080007ffe7fff","0x482680017ff98000","0x1","0x480a7ffa7fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x40780017fff7fff","0x2","0xa0680017fff8000","0x7","0x482680017ffa8000","0xffffffffffffffffffffffffffffb744","0x400280007ff97fff","0x10780017fff7fff","0xa7","0x4825800180007ffa","0x48bc","0x400280007ff97fff","0x482680017ff98000","0x1","0x480a7ffc7fff8000","0x480a7ffd7fff8000","0x1104800180018000","0x1aa","0x20680017fff7ffe","0x8e","0x48127ffc7fff8000","0x48127ffc7fff8000","0x1104800180018000","0xa7","0x40137ff07fff8000","0x20680017fff7ffe","0x79","0x48127fec7fff8000","0x48127fd07fff8000","0x48127ffa7fff8000","0x48127ffa7fff8000","0x40137ffb7fff8001","0x1104800180018000","0x101","0x20680017fff7ffa","0x68","0x20680017fff7ffd","0x58","0x48307ffb80007ffc","0x4824800180007fff","0x0","0x20680017fff7fff","0x4","0x10780017fff7fff","0x13","0x480a7ffb7fff8000","0x1104800180018000","0xb3","0x40780017fff7fff","0x1","0x480680017fff8000","0x496e70757420746f6f206c6f6e6720666f7220617267756d656e7473","0x400080007ffe7fff","0x48127ff07fff8000","0x48127ff07fff8000","0x48127ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x1104800180018000","0x2c0","0x482480017fff8000","0x2bf","0x480080007fff8000","0xa0680017fff8000","0x9","0x4824800180007ff2","0x0","0x482480017fff8000","0x100000000000000000000000000000000","0x400080007fef7fff","0x10780017fff7fff","0x23","0x4824800180007ff2","0x0","0x400080007ff07fff","0x48127fff7fff8000","0x480a7ffb7fff8000","0x480a80007fff8000","0x480a80017fff8000","0x48127ff27fff8000","0x48127ff27fff8000","0x1104800180018000","0x19e","0x482480017fd28000","0x1","0x20680017fff7ffc","0xc","0x40780017fff7fff","0x1","0x48127ffe7fff8000","0x48127ff87fff8000","0x48127ff87fff8000","0x480680017fff8000","0x0","0x48127ffb7fff8000","0x48127ffa7fff8000","0x208b7fff7fff7ffe","0x48127fff7fff8000","0x48127ff97fff8000","0x48127ff97fff8000","0x480680017fff8000","0x1","0x48127ff97fff8000","0x48127ff97fff8000","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x4f7574206f6620676173","0x400080007ffe7fff","0x482480017fed8000","0x1","0x48127fed7fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x496e70757420746f6f2073686f727420666f7220617267756d656e7473","0x400080007ffe7fff","0x48127ff67fff8000","0x48127ff67fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x48127ff87fff8000","0x48127ff87fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x48127ffa7fff8000","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x496e70757420746f6f2073686f727420666f7220617267756d656e7473","0x400080007ffe7fff","0x48127fea7fff8000","0x48127fce7fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x496e70757420746f6f2073686f727420666f7220617267756d656e7473","0x400080007ffe7fff","0x48127ff97fff8000","0x48127fdd7fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x4f7574206f6620676173","0x400080007ffe7fff","0x482680017ff98000","0x1","0x480a7ffa7fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x1","0x48127ffa7fff8000","0x482480017ff98000","0x1","0x208b7fff7fff7ffe","0x48297ffc80007ffd","0x20680017fff7fff","0x4","0x10780017fff7fff","0xa","0x482680017ffc8000","0x1","0x480a7ffd7fff8000","0x480680017fff8000","0x0","0x480a7ffc7fff8000","0x10780017fff7fff","0x8","0x480a7ffc7fff8000","0x480a7ffd7fff8000","0x480680017fff8000","0x1","0x480680017fff8000","0x0","0x48127ffc7fff8000","0x48127ffc7fff8000","0x20680017fff7ffc","0x8","0x48127ffe7fff8000","0x48127ffe7fff8000","0x480680017fff8000","0x0","0x480080007ffa8000","0x208b7fff7fff7ffe","0x48127ffe7fff8000","0x48127ffe7fff8000","0x480680017fff8000","0x1","0x480680017fff8000","0x0","0x208b7fff7fff7ffe","0x480a7ffd7fff8000","0x208b7fff7fff7ffe","0x400380007ffd7ffb","0x480a7ffc7fff8000","0x482680017ffd8000","0x1","0x208b7fff7fff7ffe","0x480a7ffc7fff8000","0x480a7ffd7fff8000","0x1104800180018000","0x800000000000010ffffffffffffffffffffffffffffffffffffffffffffffd4","0x20680017fff7ffe","0x2b","0xa0680017fff8004","0xe","0x4824800180047ffe","0x800000000000000000000000000000000000000000000000000000000000000","0x484480017ffe8000","0x110000000000000000","0x48307ffe7fff8002","0x480280007ffb7ffc","0x480280017ffb7ffc","0x402480017ffb7ffd","0xffffffffffffffeeffffffffffffffff","0x400280027ffb7ffd","0x10780017fff7fff","0x14","0x484480017fff8001","0x8000000000000000000000000000000","0x48307fff80007ffd","0x480280007ffb7ffd","0x480280017ffb7ffd","0x402480017ffc7ffe","0xf8000000000000000000000000000000","0x400280027ffb7ffe","0x40780017fff7fff","0x1","0x482680017ffb8000","0x3","0x48127ff57fff8000","0x48127ff57fff8000","0x480680017fff8000","0x0","0x48127ff57fff8000","0x208b7fff7fff7ffe","0x482680017ffb8000","0x3","0x48127ff57fff8000","0x48127ff57fff8000","0x480680017fff8000","0x1","0x480680017fff8000","0x0","0x208b7fff7fff7ffe","0x40780017fff7fff","0x6","0x480a7ffb7fff8000","0x48127ff57fff8000","0x48127ff57fff8000","0x480680017fff8000","0x1","0x480680017fff8000","0x0","0x208b7fff7fff7ffe","0x48297ffc80007ffd","0x20680017fff7fff","0x4","0x10780017fff7fff","0xa","0x482680017ffc8000","0x1","0x480a7ffd7fff8000","0x480680017fff8000","0x0","0x480a7ffc7fff8000","0x10780017fff7fff","0x8","0x480a7ffc7fff8000","0x480a7ffd7fff8000","0x480680017fff8000","0x1","0x480680017fff8000","0x0","0x48127ffc7fff8000","0x48127ffc7fff8000","0x20680017fff7ffc","0x26","0x40780017fff7fff","0x1","0x480a7ffa7fff8000","0x480a7ffb7fff8000","0x48127ffb7fff8000","0x48127ffb7fff8000","0x48127ffb7fff8000","0x48127ffa7fff8000","0x480080007ff68000","0x1104800180018000","0xe9","0x20680017fff7ffa","0xc","0x48127ff87fff8000","0x48127ff87fff8000","0x480680017fff8000","0x0","0x48127ff87fff8000","0x48127ff87fff8000","0x48127ff87fff8000","0x48127ff87fff8000","0x48127ff87fff8000","0x208b7fff7fff7ffe","0x48127ff87fff8000","0x48127ff87fff8000","0x480680017fff8000","0x1","0x480680017fff8000","0x0","0x480680017fff8000","0x0","0x480680017fff8000","0x0","0x48127ff87fff8000","0x48127ff87fff8000","0x208b7fff7fff7ffe","0x480a7ffa7fff8000","0x480a7ffb7fff8000","0x480680017fff8000","0x0","0x48127ffb7fff8000","0x48127ffb7fff8000","0x480680017fff8000","0x1","0x480680017fff8000","0x0","0x480680017fff8000","0x0","0x208b7fff7fff7ffe","0x480a7ff87fff8000","0x480a7ff97fff8000","0x1104800180018000","0x12b","0x20680017fff7ffd","0x44","0x4824800180007fff","0x0","0x20680017fff7fff","0x31","0x480a7ffc7fff8000","0x480a7ffd7fff8000","0x480680017fff8000","0x43616c6c436f6e7472616374","0x400080007ff87fff","0x400080017ff87ff7","0x400180027ff87ffa","0x400180037ff87ffb","0x400080047ff87ffd","0x400080057ff87ffe","0x480080077ff88000","0x20680017fff7fff","0xb","0x480080067ff78000","0x482480017ff68000","0xa","0x480680017fff8000","0x0","0x480080087ff48000","0x480080097ff38000","0x10780017fff7fff","0x9","0x480080067ff78000","0x482480017ff68000","0xa","0x480680017fff8000","0x1","0x480080087ff48000","0x480080097ff38000","0x1104800180018000","0x11b","0x20680017fff7ffd","0x9","0x48127ff67fff8000","0x48127ff67fff8000","0x480680017fff8000","0x0","0x48127ffb7fff8000","0x48127ffb7fff8000","0x208b7fff7fff7ffe","0x48127ff67fff8000","0x48127ff67fff8000","0x480680017fff8000","0x1","0x48127ffb7fff8000","0x48127ffb7fff8000","0x208b7fff7fff7ffe","0x40780017fff7fff","0xc","0x40780017fff7fff","0x1","0x480680017fff8000","0x494e56414c49445f43414c4c4552","0x400080007ffe7fff","0x48127fec7fff8000","0x48127fec7fff8000","0x480680017fff8000","0x1","0x48127ffb7fff8000","0x482480017ffa8000","0x1","0x208b7fff7fff7ffe","0x40780017fff7fff","0xf","0x48127fec7fff8000","0x48127fec7fff8000","0x480680017fff8000","0x1","0x48127fec7fff8000","0x48127fec7fff8000","0x208b7fff7fff7ffe","0x480a7ffc7fff8000","0x480a7ffd7fff8000","0x1104800180018000","0x800000000000010ffffffffffffffffffffffffffffffffffffffffffffff02","0x20680017fff7ffe","0x2b","0xa0680017fff8004","0xe","0x4824800180047ffe","0x800000000000000000000000000000000000000000000000000000000000000","0x484480017ffe8000","0x110000000000000000","0x48307ffe7fff8002","0x480280007ffb7ffc","0x480280017ffb7ffc","0x402480017ffb7ffd","0xffffffffffffffeeffffffffffffffff","0x400280027ffb7ffd","0x10780017fff7fff","0x14","0x484480017fff8001","0x8000000000000000000000000000000","0x48307fff80007ffd","0x480280007ffb7ffd","0x480280017ffb7ffd","0x402480017ffc7ffe","0xf8000000000000000000000000000000","0x400280027ffb7ffe","0x40780017fff7fff","0x1","0x482680017ffb8000","0x3","0x48127ff57fff8000","0x48127ff57fff8000","0x480680017fff8000","0x0","0x48127ff57fff8000","0x208b7fff7fff7ffe","0x482680017ffb8000","0x3","0x48127ff57fff8000","0x48127ff57fff8000","0x480680017fff8000","0x1","0x480680017fff8000","0x0","0x208b7fff7fff7ffe","0x40780017fff7fff","0x6","0x480a7ffb7fff8000","0x48127ff57fff8000","0x48127ff57fff8000","0x480680017fff8000","0x1","0x480680017fff8000","0x0","0x208b7fff7fff7ffe","0x480a7ffc7fff8000","0x480a7ffd7fff8000","0x480680017fff8000","0x0","0x480680017fff8000","0x4465706c6f79","0x400280007ff97fff","0x400380017ff97ff8","0x400380027ff97ffa","0x400380037ff97ffb","0x400280047ff97ffc","0x400280057ff97ffd","0x400280067ff97ffe","0x480280087ff98000","0x20680017fff7fff","0xc","0x480280077ff98000","0x482680017ff98000","0xc","0x480680017fff8000","0x0","0x480280097ff98000","0x4802800a7ff98000","0x4802800b7ff98000","0x10780017fff7fff","0xb","0x480280077ff98000","0x482680017ff98000","0xb","0x480680017fff8000","0x1","0x480680017fff8000","0x0","0x480280097ff98000","0x4802800a7ff98000","0x1104800180018000","0xa1","0x20680017fff7ffc","0xb","0x48127ff47fff8000","0x48127ff47fff8000","0x480680017fff8000","0x0","0x480680017fff8000","0x0","0x480680017fff8000","0x0","0x208b7fff7fff7ffe","0x48127ff47fff8000","0x48127ff47fff8000","0x480680017fff8000","0x1","0x48127ffb7fff8000","0x48127ffb7fff8000","0x208b7fff7fff7ffe","0x1104800180018000","0xd4","0x482480017fff8000","0xd3","0x480080007fff8000","0xa0680017fff8000","0x9","0x4825800180007ff8","0x12a2","0x482480017fff8000","0x100000000000000000000000000000000","0x400280007ff77fff","0x10780017fff7fff","0x4c","0x4825800180007ff8","0x12a2","0x400280007ff77fff","0x482680017ff78000","0x1","0x20780017fff7ffd","0xd","0x48127fff7fff8000","0x48127ffd7fff8000","0x480680017fff8000","0x0","0x480a7ff97fff8000","0x480a7ffa7fff8000","0x480680017fff8000","0x0","0x480a7ffb7fff8000","0x480a7ffc7fff8000","0x208b7fff7fff7ffe","0x480a7ff97fff8000","0x480a7ffa7fff8000","0x1104800180018000","0x800000000000010fffffffffffffffffffffffffffffffffffffffffffffe72","0x20680017fff7ffe","0x27","0x400280007ffc7fff","0x48127ff07fff8000","0x48127fee7fff8000","0x48127ffa7fff8000","0x48127ffa7fff8000","0x480a7ffb7fff8000","0x482680017ffc8000","0x1","0x4825800180007ffd","0x1","0x1104800180018000","0x800000000000010ffffffffffffffffffffffffffffffffffffffffffffffd1","0x20680017fff7ffa","0xc","0x48127ff87fff8000","0x48127ff87fff8000","0x480680017fff8000","0x0","0x48127ff87fff8000","0x48127ff87fff8000","0x48127ff87fff8000","0x48127ff87fff8000","0x48127ff87fff8000","0x208b7fff7fff7ffe","0x48127ff87fff8000","0x48127ff87fff8000","0x480680017fff8000","0x1","0x480680017fff8000","0x0","0x480680017fff8000","0x0","0x480680017fff8000","0x0","0x48127ff87fff8000","0x48127ff87fff8000","0x208b7fff7fff7ffe","0x48127ff07fff8000","0x48127fee7fff8000","0x480680017fff8000","0x0","0x48127ff97fff8000","0x48127ff97fff8000","0x480680017fff8000","0x1","0x480680017fff8000","0x0","0x480680017fff8000","0x0","0x208b7fff7fff7ffe","0x40780017fff7fff","0x1","0x480680017fff8000","0x4f7574206f6620676173","0x400080007ffe7fff","0x482680017ff78000","0x1","0x480a7ff87fff8000","0x480680017fff8000","0x1","0x480680017fff8000","0x0","0x480680017fff8000","0x0","0x480680017fff8000","0x0","0x48127ff87fff8000","0x482480017ff78000","0x1","0x208b7fff7fff7ffe","0x480a7ffc7fff8000","0x480a7ffd7fff8000","0x1104800180018000","0x2e","0x20680017fff7ffd","0xa","0x48127ffb7fff8000","0x48127ffb7fff8000","0x480680017fff8000","0x0","0x480680017fff8000","0x0","0x480080027ffb8000","0x208b7fff7fff7ffe","0x48127ffb7fff8000","0x48127ffb7fff8000","0x480680017fff8000","0x1","0x48127ffb7fff8000","0x48127ffb7fff8000","0x208b7fff7fff7ffe","0x20780017fff7ffb","0x7","0x480680017fff8000","0x0","0x480a7ffc7fff8000","0x480a7ffd7fff8000","0x208b7fff7fff7ffe","0x480680017fff8000","0x1","0x480a7ffc7fff8000","0x480a7ffd7fff8000","0x208b7fff7fff7ffe","0x20780017fff7ffa","0x8","0x480680017fff8000","0x0","0x480a7ffb7fff8000","0x480a7ffc7fff8000","0x480a7ffd7fff8000","0x208b7fff7fff7ffe","0x480680017fff8000","0x1","0x480680017fff8000","0x0","0x480a7ffc7fff8000","0x480a7ffd7fff8000","0x208b7fff7fff7ffe","0x480680017fff8000","0x476574457865637574696f6e496e666f","0x400280007ffd7fff","0x400380017ffd7ffc","0x480280037ffd8000","0x20680017fff7fff","0xc","0x480280027ffd8000","0x482680017ffd8000","0x5","0x480680017fff8000","0x0","0x480680017fff8000","0x0","0x480280047ffd8000","0x10780017fff7fff","0x9","0x480280027ffd8000","0x482680017ffd8000","0x6","0x480680017fff8000","0x1","0x480280047ffd8000","0x480280057ffd8000","0x1104800180018000","0x13","0x20680017fff7ffd","0xa","0x48127ff67fff8000","0x48127ff67fff8000","0x480680017fff8000","0x0","0x480680017fff8000","0x0","0x48127ffb7fff8000","0x208b7fff7fff7ffe","0x48127ff67fff8000","0x48127ff67fff8000","0x480680017fff8000","0x1","0x48127ffb7fff8000","0x48127ffb7fff8000","0x208b7fff7fff7ffe","0x20780017fff7ffb","0x8","0x480680017fff8000","0x0","0x480680017fff8000","0x0","0x480a7ffd7fff8000","0x208b7fff7fff7ffe","0x480680017fff8000","0x1","0x480a7ffc7fff8000","0x480a7ffd7fff8000","0x208b7fff7fff7ffe"],"hints":[[0,[{"TestLessThanOrEqual":{"lhs":{"Immediate":"0x0"},"rhs":{"Deref":{"register":"FP","offset":-6}},"dst":{"register":"AP","offset":0}}}]],[34,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[53,[{"TestLessThanOrEqual":{"lhs":{"Immediate":"0x0"},"rhs":{"Deref":{"register":"AP","offset":-38}},"dst":{"register":"AP","offset":0}}}]],[65,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[82,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[97,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[111,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[125,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[140,[{"TestLessThanOrEqual":{"lhs":{"Immediate":"0x0"},"rhs":{"Deref":{"register":"FP","offset":-6}},"dst":{"register":"AP","offset":0}}}]],[168,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[187,[{"TestLessThanOrEqual":{"lhs":{"Immediate":"0x0"},"rhs":{"Deref":{"register":"AP","offset":-23}},"dst":{"register":"AP","offset":0}}}]],[199,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[216,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[231,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[245,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[260,[{"TestLessThanOrEqual":{"lhs":{"Immediate":"0x12d4"},"rhs":{"Deref":{"register":"FP","offset":-6}},"dst":{"register":"AP","offset":0}}}]],[304,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[323,[{"TestLessThanOrEqual":{"lhs":{"Immediate":"0x0"},"rhs":{"Deref":{"register":"AP","offset":-13}},"dst":{"register":"AP","offset":0}}}]],[335,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[352,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[367,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[389,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[403,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[417,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[434,[{"TestLessThanOrEqual":{"lhs":{"Immediate":"0x7d78"},"rhs":{"Deref":{"register":"FP","offset":-6}},"dst":{"register":"AP","offset":0}}}]],[480,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[499,[{"TestLessThanOrEqual":{"lhs":{"Immediate":"0x0"},"rhs":{"Deref":{"register":"AP","offset":-13}},"dst":{"register":"AP","offset":0}}}]],[539,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[554,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[576,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[590,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[604,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[621,[{"TestLessThanOrEqual":{"lhs":{"Immediate":"0x48bc"},"rhs":{"Deref":{"register":"FP","offset":-6}},"dst":{"register":"AP","offset":0}}}]],[667,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[686,[{"TestLessThanOrEqual":{"lhs":{"Immediate":"0x0"},"rhs":{"Deref":{"register":"AP","offset":-13}},"dst":{"register":"AP","offset":0}}}]],[710,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[728,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[743,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[765,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[779,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[793,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[857,[{"TestLessThan":{"lhs":{"Deref":{"register":"AP","offset":-1}},"rhs":{"Immediate":"0x800000000000000000000000000000000000000000000000000000000000000"},"dst":{"register":"AP","offset":4}}}]],[861,[{"LinearSplit":{"value":{"Deref":{"register":"AP","offset":3}},"scalar":{"Immediate":"0x110000000000000000"},"max_x":{"Immediate":"0xffffffffffffffffffffffffffffffff"},"x":{"register":"AP","offset":-2},"y":{"register":"AP","offset":-1}}}]],[871,[{"LinearSplit":{"value":{"Deref":{"register":"AP","offset":-2}},"scalar":{"Immediate":"0x8000000000000000000000000000000"},"max_x":{"Immediate":"0xffffffffffffffffffffffffffffffff"},"x":{"register":"AP","offset":-1},"y":{"register":"AP","offset":0}}}]],[931,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[1000,[{"SystemCall":{"system":{"Deref":{"register":"AP","offset":-8}}}}]],[1039,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[1067,[{"TestLessThan":{"lhs":{"Deref":{"register":"AP","offset":-1}},"rhs":{"Immediate":"0x800000000000000000000000000000000000000000000000000000000000000"},"dst":{"register":"AP","offset":4}}}]],[1071,[{"LinearSplit":{"value":{"Deref":{"register":"AP","offset":3}},"scalar":{"Immediate":"0x110000000000000000"},"max_x":{"Immediate":"0xffffffffffffffffffffffffffffffff"},"x":{"register":"AP","offset":-2},"y":{"register":"AP","offset":-1}}}]],[1081,[{"LinearSplit":{"value":{"Deref":{"register":"AP","offset":-2}},"scalar":{"Immediate":"0x8000000000000000000000000000000"},"max_x":{"Immediate":"0xffffffffffffffffffffffffffffffff"},"x":{"register":"AP","offset":-1},"y":{"register":"AP","offset":0}}}]],[1131,[{"SystemCall":{"system":{"Deref":{"register":"FP","offset":-7}}}}]],[1178,[{"TestLessThanOrEqual":{"lhs":{"Immediate":"0x12a2"},"rhs":{"Deref":{"register":"FP","offset":-8}},"dst":{"register":"AP","offset":0}}}]],[1261,[{"AllocSegment":{"dst":{"register":"AP","offset":0}}}]],[1333,[{"SystemCall":{"system":{"Deref":{"register":"FP","offset":-3}}}}]]],"entry_points_by_type":{"EXTERNAL":[{"selector":"0x15d40a3d6ca2ac30f4031e42be28da9b056fef9bb7357ac5e85627ee876e5ad","offset":432,"builtins":["range_check"]},{"selector":"0x162da33a4585851fe8d3af3c2a9c60b557814e221e0d4f30ff0b2189d9c7775","offset":260,"builtins":["range_check"]},{"selector":"0x2730079d734ee55315f4f141eaed376bddd8c2133523d223a344c5604e0f7f8","offset":619,"builtins":["range_check"]},{"selector":"0x289da278a8dc833409cabfdad1581e8e7d40e42dcaed693fa4008dcdb4963b3","offset":140,"builtins":["range_check"]},{"selector":"0x36fcbf06cd96843058359e1a75928beacfac10727dab22a3972f0af8aa92895","offset":0,"builtins":["range_check"]}],"L1_HANDLER":[],"CONSTRUCTOR":[]}} \ No newline at end of file diff --git a/crates/cheatnet/src/cheatcodes/deploy.rs b/crates/cheatnet/src/cheatcodes/deploy.rs index 74ad33170b..0083d2d6fe 100644 --- a/crates/cheatnet/src/cheatcodes/deploy.rs +++ b/crates/cheatnet/src/cheatcodes/deploy.rs @@ -1,29 +1,23 @@ -use crate::constants::{ - build_block_context, build_invoke_transaction, TEST_ACCOUNT_CONTRACT_ADDRESS, -}; +use crate::constants::TEST_ACCOUNT_CONTRACT_ADDRESS; use crate::state::DictStateReader; use crate::{cheatcodes::EnhancedHintError, CheatnetState}; -use anyhow::{Context, Result}; +use anyhow::Result; use blockifier::abi::abi_utils::selector_from_name; -use blockifier::execution::execution_utils::felt_to_stark_felt; +use blockifier::execution::execution_utils::{felt_to_stark_felt, stark_felt_to_felt}; -use blockifier::execution::entry_point::CallInfo; use blockifier::state::cached_state::CachedState; use blockifier::state::state_api::StateReader; -use blockifier::transaction::account_transaction::AccountTransaction; -use blockifier::transaction::transactions::{ExecutableTransaction, InvokeTransaction}; use cairo_felt::Felt252; +use starknet::core::utils::get_selector_from_name; use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector, PatriciaKey}; use starknet_api::hash::{StarkFelt, StarkHash}; -use starknet_api::transaction::{ - Calldata, ContractAddressSalt, InvokeTransactionV1, TransactionHash, -}; +use starknet_api::transaction::ContractAddressSalt; use starknet_api::{patricia_key, stark_felt}; use super::CheatcodeError; -use crate::conversions::felt_from_short_string; -use crate::panic_data::try_extract_panic_data; +use crate::conversions::{felt_from_short_string, field_element_to_felt252}; +use crate::rpc::{call_contract, CallContractOutput}; impl CheatnetState { pub fn deploy( @@ -33,9 +27,9 @@ impl CheatnetState { ) -> Result { // Deploy a contract using syscall deploy. let account_address = ContractAddress(patricia_key!(TEST_ACCOUNT_CONTRACT_ADDRESS)); - let block_context = build_block_context(); let entry_point_selector = selector_from_name("deploy_contract"); let salt = self.get_salt(); + let contract_address = self.precalculate_address(&class_hash, calldata); let class_hash = ClassHash(StarkFelt::new(class_hash.to_be_bytes()).unwrap()); self.increment_deploy_salt_base(); @@ -58,41 +52,20 @@ impl CheatnetState { &salt, ); - let nonce = blockifier_state - .get_nonce_at(account_address) - .context("Failed to get nonce") - .map_err::(From::from)?; - let tx = build_invoke_transaction(execute_calldata, account_address); - let tx = InvokeTransactionV1 { nonce, ..tx }; - let account_tx = AccountTransaction::Invoke(InvokeTransaction { - tx: starknet_api::transaction::InvokeTransaction::V1(tx), - tx_hash: TransactionHash::default(), // TODO(#358): Check if this is legit - }); - - let tx_info = account_tx - .execute(blockifier_state, &block_context, true, true) - .unwrap_or_else(|e| panic!("Unparseable transaction error: {e:?}")); - - if let Some(CallInfo { execution, .. }) = tx_info.execute_call_info { - let contract_address = execution - .retdata - .0 - .get(0) - .expect("Failed to get contract_address from return_data"); - - let contract_address = ContractAddress::try_from(*contract_address) - .expect("Failed to cast contract address into the right struct"); - - return Ok(contract_address); + let call_result = call_contract( + &account_address, + &field_element_to_felt252(&get_selector_from_name("__execute__").unwrap()), + execute_calldata.as_slice(), + self, + ) + .unwrap_or_else(|err| panic!("Deploy txn failed: {err}")); + + match call_result { + CallContractOutput::Success { .. } => Ok(contract_address), + CallContractOutput::Panic { panic_data } => { + Err(CheatcodeError::Recoverable(panic_data)) + } } - - let revert_error = tx_info - .revert_error - .expect("Unparseable tx info, {tx_info:?}"); - let extracted_panic_data = try_extract_panic_data(&revert_error) - .expect("Unparseable error message, {revert_error}"); - - Err(CheatcodeError::Recoverable(extracted_panic_data)) } } @@ -102,7 +75,7 @@ fn create_execute_calldata( account_address: &ContractAddress, entry_point_selector: &EntryPointSelector, salt: &ContractAddressSalt, -) -> Calldata { +) -> Vec { let calldata_len = u128::try_from(calldata.len()).unwrap(); let mut execute_calldata = vec![ *account_address.0.key(), // Contract address. @@ -114,13 +87,15 @@ fn create_execute_calldata( ]; let mut calldata: Vec = calldata.iter().map(felt_to_stark_felt).collect(); execute_calldata.append(&mut calldata); - Calldata(execute_calldata.into()) + return execute_calldata + .iter() + .map(|sf| stark_felt_to_felt(*sf)) + .collect(); } #[cfg(test)] mod test { use super::*; - use std::sync::Arc; #[test] fn execute_calldata() { @@ -133,7 +108,7 @@ mod test { ); assert_eq!( calldata, - Calldata(Arc::new(vec![ + vec![ StarkFelt::from(111_u32), StarkFelt::from(222_u32), StarkFelt::from(5_u32), @@ -142,7 +117,7 @@ mod test { StarkFelt::from(2_u32), StarkFelt::from(100_u32), StarkFelt::from(200_u32), - ])) + ] ); } @@ -157,14 +132,14 @@ mod test { ); assert_eq!( calldata, - Calldata(Arc::new(vec![ + vec![ StarkFelt::from(111_u32), StarkFelt::from(222_u32), StarkFelt::from(3_u32), StarkFelt::from(123_u32), StarkFelt::from(333_u32), StarkFelt::from(0_u32), - ])) + ] ); } } diff --git a/crates/cheatnet/src/constants.rs b/crates/cheatnet/src/constants.rs index 7056470cc6..d8366b1feb 100644 --- a/crates/cheatnet/src/constants.rs +++ b/crates/cheatnet/src/constants.rs @@ -1,6 +1,7 @@ use std::sync::Arc; use std::{collections::HashMap, fs, path::PathBuf}; +use blockifier::execution::contract_class::ContractClassV1; use blockifier::state::cached_state::GlobalContractCache; use blockifier::{ abi::{abi_utils::get_storage_var_address, constants}, @@ -112,16 +113,30 @@ pub fn build_invoke_transaction( } } -fn load_contract_class( +fn read_predeployed_contract_file( predeployed_contracts: &Utf8PathBuf, contract_path: &str, -) -> ContractClassV0 { +) -> String { let full_contract_path: PathBuf = predeployed_contracts.join(contract_path).into(); - let raw_contract_class = - fs::read_to_string(full_contract_path).expect("Failed to read predeployed contracts"); + fs::read_to_string(full_contract_path).expect("Failed to read predeployed contracts") +} + +fn load_v0_contract_class( + predeployed_contracts: &Utf8PathBuf, + contract_path: &str, +) -> ContractClassV0 { + let raw_contract_class = read_predeployed_contract_file(predeployed_contracts, contract_path); ContractClassV0::try_from_json_string(&raw_contract_class).unwrap() } +fn load_v1_contract_class( + predeployed_contracts: &Utf8PathBuf, + contract_path: &str, +) -> ContractClassV1 { + let raw_contract_class = read_predeployed_contract_file(predeployed_contracts, contract_path); + ContractClassV1::try_from_json_string(&raw_contract_class).unwrap() +} + fn erc20_account_balance_key() -> StorageKey { get_storage_var_address( "ERC20_balances", @@ -135,11 +150,8 @@ fn erc20_account_balance_key() -> StorageKey { // Account does not include validations #[must_use] pub fn build_testing_state(predeployed_contracts: &Utf8PathBuf) -> CachedState { - let account_class = load_contract_class( - predeployed_contracts, - "account_no_validations_contract.casm.json", - ); - let erc20_class = load_contract_class( + let account_class = load_v1_contract_class(predeployed_contracts, "account_cairo1.casm.json"); + let erc20_class = load_v0_contract_class( predeployed_contracts, "erc20_contract_without_some_syscalls_compiled.json", ); @@ -148,7 +160,7 @@ pub fn build_testing_state(predeployed_contracts: &Utf8PathBuf) -> CachedState Felt252 { return Felt252::from_bytes_be(short_str.as_bytes()); } +#[must_use] +pub fn class_hash_from_felt(felt: &Felt252) -> ClassHash { + return ClassHash(felt_to_stark_felt(felt)); +} + +#[must_use] +pub fn contract_address_from_felt(felt: &Felt252) -> ContractAddress { + ContractAddress( + PatriciaKey::try_from(felt_to_stark_felt(felt)) + .expect("StarkFelt to PatriciaKey conversion failed"), + ) +} + +#[must_use] +pub fn field_element_to_felt252(field_element: &FieldElement) -> Felt252 { + Felt252::from_bytes_be(&field_element.to_bytes_be()) +} + #[cfg(test)] mod test { use super::*; diff --git a/crates/cheatnet/src/rpc.rs b/crates/cheatnet/src/rpc.rs index 455f669393..589c8a1054 100644 --- a/crates/cheatnet/src/rpc.rs +++ b/crates/cheatnet/src/rpc.rs @@ -16,11 +16,11 @@ use std::collections::HashSet; use std::{any::Any, collections::HashMap, sync::Arc}; use crate::{ - constants::{build_block_context, build_transaction_context, TEST_ACCOUNT_CONTRACT_ADDRESS}, + constants::{build_block_context, build_transaction_context}, CheatnetState, }; -use blockifier::execution::entry_point::CallExecution; -use blockifier::execution::entry_point::Retdata; +use blockifier::execution::entry_point::{handle_empty_constructor, Retdata}; +use blockifier::execution::entry_point::{CallExecution, ConstructorContext}; use blockifier::{ abi::constants, execution::{ @@ -61,19 +61,20 @@ use cairo_lang_casm::{ }; use cairo_vm::vm::runners::cairo_runner::ExecutionResources as VmExecutionResources; use starknet_api::{ - core::{ClassHash, ContractAddress, EntryPointSelector, PatriciaKey}, + core::{ClassHash, ContractAddress, EntryPointSelector}, deprecated_contract_class::EntryPointType, hash::{StarkFelt, StarkHash}, - patricia_key, transaction::{Calldata, TransactionVersion}, }; use blockifier::execution::syscalls::{ - LibraryCallRequest, SyscallRequest, SyscallRequestWrapper, SyscallResponse, - SyscallResponseWrapper, SyscallResult, + DeployRequest, DeployResponse, LibraryCallRequest, SyscallRequest, SyscallRequestWrapper, + SyscallResponse, SyscallResponseWrapper, SyscallResult, }; +use blockifier::state::errors::StateError; use cairo_vm::hint_processor::hint_processor_definition::HintProcessorLogic; use cairo_vm::vm::runners::cairo_runner::ResourceTracker; +use starknet_api::core::calculate_contract_address; use crate::panic_data::try_extract_panic_data; use crate::state::CheatcodeState; @@ -88,7 +89,7 @@ pub enum CallContractOutput { // This does contract call without the transaction layer. This way `call_contract` can return data and modify state. // `call` and `invoke` on the transactional layer use such method under the hood. pub fn call_contract( - contract_address: &Felt252, + contract_address: &ContractAddress, entry_point_selector: &Felt252, calldata: &[Felt252], cheatnet_state: &mut CheatnetState, @@ -96,13 +97,9 @@ pub fn call_contract( let blockifier_state = &mut cheatnet_state.blockifier_state; let cheatcode_state = &mut cheatnet_state.cheatcode_state; - let contract_address = ContractAddress(PatriciaKey::try_from(StarkFelt::new( - contract_address.to_be_bytes(), - )?)?); let entry_point_selector = EntryPointSelector(StarkHash::new(entry_point_selector.to_be_bytes())?); - let account_address = ContractAddress(patricia_key!(TEST_ACCOUNT_CONTRACT_ADDRESS)); let calldata = Calldata(Arc::new( calldata .iter() @@ -111,12 +108,12 @@ pub fn call_contract( )); let mut entry_point = CallEntryPoint { class_hash: None, - code_address: Some(contract_address), + code_address: Some(*contract_address), entry_point_type: EntryPointType::External, entry_point_selector, calldata, - storage_address: contract_address, - caller_address: account_address, + storage_address: *contract_address, + caller_address: ContractAddress::default(), call_type: CallType::Call, initial_gas: u64::MAX, }; @@ -516,6 +513,10 @@ impl CheatableSyscallHandler<'_> { library_call_syscall, constants::CALL_CONTRACT_GAS_COST, ); + } else if SyscallSelector::Deploy == selector { + // Increment, since the selector was peeked into before + self.syscall_handler.syscall_ptr += 1; + return self.execute_syscall(vm, deploy_syscall, constants::DEPLOY_GAS_COST); } self.syscall_handler.execute_next_syscall(vm, hint) @@ -633,6 +634,126 @@ pub fn call_contract_syscall( }) } +// blockifier/src/execution/syscalls/mod.rs:222 (deploy_syscall) +fn deploy_syscall( + request: DeployRequest, + vm: &mut VirtualMachine, + syscall_handler: &mut CheatableSyscallHandler<'_>, + remaining_gas: &mut u64, +) -> SyscallResult { + let deployer_address = syscall_handler.syscall_handler.storage_address(); + let deployer_address_for_calculation = match request.deploy_from_zero { + true => ContractAddress::default(), + false => deployer_address, + }; + let deployed_contract_address = calculate_contract_address( + request.contract_address_salt, + request.class_hash, + &request.constructor_calldata, + deployer_address_for_calculation, + )?; + + let ctor_context = ConstructorContext { + class_hash: request.class_hash, + code_address: Some(deployed_contract_address), + storage_address: deployed_contract_address, + caller_address: deployer_address, + }; + let call_info = execute_deployment( + syscall_handler.syscall_handler.state, + syscall_handler.syscall_handler.resources, + syscall_handler.syscall_handler.context, + ctor_context, + request.constructor_calldata, + *remaining_gas, + syscall_handler.cheatcode_state, + )?; + + let constructor_retdata = create_retdata_segment( + vm, + &mut syscall_handler.syscall_handler, + &call_info.execution.retdata.0, + )?; + update_remaining_gas(remaining_gas, &call_info); + + syscall_handler.syscall_handler.inner_calls.push(call_info); + + Ok(DeployResponse { + contract_address: deployed_contract_address, + constructor_retdata, + }) +} + +// blockifier/src/execution/execution_utils.rs:217 (execute_deployment) +pub fn execute_deployment( + state: &mut dyn State, + resources: &mut ExecutionResources, + context: &mut EntryPointExecutionContext, + ctor_context: ConstructorContext, + constructor_calldata: Calldata, + remaining_gas: u64, + cheatcode_state: &CheatcodeState, +) -> EntryPointExecutionResult { + // Address allocation in the state is done before calling the constructor, so that it is + // visible from it. + let deployed_contract_address = ctor_context.storage_address; + let current_class_hash = state.get_class_hash_at(deployed_contract_address)?; + if current_class_hash != ClassHash::default() { + return Err(StateError::UnavailableContractAddress(deployed_contract_address).into()); + } + + state.set_class_hash_at(deployed_contract_address, ctor_context.class_hash)?; + + let call_info = execute_constructor_entry_point( + state, + resources, + context, + ctor_context, + constructor_calldata, + remaining_gas, + cheatcode_state, + )?; + + Ok(call_info) +} +// blockifier/src/execution/entry_point.rs:366 (execute_constructor_entry_point) +pub fn execute_constructor_entry_point( + state: &mut dyn State, + resources: &mut ExecutionResources, + context: &mut EntryPointExecutionContext, + ctor_context: ConstructorContext, + calldata: Calldata, + remaining_gas: u64, + cheatcode_state: &CheatcodeState, +) -> EntryPointExecutionResult { + // Ensure the class is declared (by reading it). + let contract_class = state.get_compiled_contract_class(&ctor_context.class_hash)?; + let Some(constructor_selector) = contract_class.constructor_selector() else { + // Contract has no constructor. + return handle_empty_constructor(ctor_context, calldata, remaining_gas.clone()); + }; + + let mut constructor_call = CallEntryPoint { + class_hash: None, + code_address: ctor_context.code_address, + entry_point_type: EntryPointType::Constructor, + entry_point_selector: constructor_selector, + calldata, + storage_address: ctor_context.storage_address, + caller_address: ctor_context.caller_address, + call_type: CallType::Call, + initial_gas: remaining_gas.clone(), + }; + + execute_call_entry_point( + &mut constructor_call, + state, + cheatcode_state, + resources, + context, + ) +} + // Inspired by blockifier::execution::syscalls::library_call // Calls a library using our implementation with modified logic pub fn library_call_syscall( diff --git a/crates/forge/src/cheatcodes_hint_processor.rs b/crates/forge/src/cheatcodes_hint_processor.rs index f5f0535d23..c663e5a2dc 100644 --- a/crates/forge/src/cheatcodes_hint_processor.rs +++ b/crates/forge/src/cheatcodes_hint_processor.rs @@ -34,6 +34,7 @@ use cairo_lang_runner::{ }; use cairo_lang_utils::bigint::BigIntAsHex; use cairo_vm::vm::runners::cairo_runner::{ResourceTracker, RunResources}; +use cheatnet::conversions::contract_address_from_felt; mod file_operations; @@ -338,7 +339,7 @@ impl CairoHintProcessor<'_> { Ok(()) } "get_class_hash" => { - let contract_address = contract_address_from_felt252(&inputs[0])?; + let contract_address = contract_address_from_felt(&inputs[0]); match self.cheatnet_state.get_class_hash(contract_address) { Ok(class_hash) => { @@ -421,7 +422,7 @@ fn execute_syscall( let calldata = buffer.next_arr().unwrap(); let call_result = call_contract( - &contract_address, + &contract_address_from_felt(&contract_address), &entry_point_selector, &calldata, cheatnet_state, @@ -460,9 +461,3 @@ fn write_cheatcode_panic(buffer: &mut MemBuffer, panic_data: &[Felt252]) { .write_data(panic_data.iter()) .expect("Failed to insert error in memory"); } - -fn contract_address_from_felt252(felt: &Felt252) -> Result { - Ok(ContractAddress(PatriciaKey::try_from(felt_to_stark_felt( - felt, - ))?)) -} diff --git a/crates/forge/tests/integration/roll.rs b/crates/forge/tests/integration/roll.rs index 18151ab67e..0728d71a08 100644 --- a/crates/forge/tests/integration/roll.rs +++ b/crates/forge/tests/integration/roll.rs @@ -90,9 +90,7 @@ fn start_roll_with_other_syscall() { assert_passed!(result); } -// TODO (#254): Make it pass #[test] -#[ignore] fn start_roll_in_constructor_test() { let test = test_case!( indoc!( @@ -101,6 +99,7 @@ fn start_roll_in_constructor_test() { use array::ArrayTrait; use option::OptionTrait; use traits::TryInto; + use traits::Into; use starknet::ContractAddress; use starknet::Felt252TryIntoContractAddress; @@ -114,12 +113,16 @@ fn start_roll_in_constructor_test() { #[test] fn test_roll_constructor_simple() { let contract = declare('ConstructorRollChecker'); - let contract_address: ContractAddress = 2598896470772924212281968896271340780432065735045468431712403008297614014532.try_into().unwrap(); - start_roll(contract_address, 234); - let contract_address = contract.deploy(@ArrayTrait::new()).unwrap(); - + let constructor_calldata = array![]; + let precalculated_address = contract.precalculate_address(@constructor_calldata); + + start_roll(precalculated_address, 234); + let contract_address = contract.deploy(@constructor_calldata).unwrap(); + + assert(contract_address == precalculated_address, 'Addresses dont match'); + let dispatcher = IConstructorRollCheckerDispatcher { contract_address }; - assert(dispatcher.get_stored_block_number() == 234, 'Wrong stored blk_nb'); + assert(dispatcher.get_stored_block_number() == 234, 'Wrong stored block nb'); } "# ), diff --git a/crates/forge/tests/integration/warp.rs b/crates/forge/tests/integration/warp.rs index ac95edf207..346f0251e8 100644 --- a/crates/forge/tests/integration/warp.rs +++ b/crates/forge/tests/integration/warp.rs @@ -108,9 +108,7 @@ fn warp() { assert_passed!(result); } -// TODO (#254): Make it pass #[test] -#[ignore] fn start_warp_in_constructor_test() { let test = test_case!( indoc!( @@ -119,9 +117,11 @@ fn start_warp_in_constructor_test() { use array::ArrayTrait; use option::OptionTrait; use traits::TryInto; + use traits::Into; use starknet::ContractAddress; use starknet::Felt252TryIntoContractAddress; - use snforge_std::{ declare, deploy, start_warp }; + + use snforge_std::{ declare, ContractClassTrait, start_warp }; #[starknet::interface] trait IConstructorWarpChecker { @@ -131,9 +131,13 @@ fn start_warp_in_constructor_test() { #[test] fn test_warp_constructor_simple() { let contract = declare('ConstructorWarpChecker'); - let contract_address: ContractAddress = 3536868843103376321721783970179672615412806578951102081876401371045020950704.try_into().unwrap(); - start_warp(contract_address, 234); + let constructor_calldata = array![]; + let precalculated_address = contract.precalculate_address(@constructor_calldata); + + start_warp(precalculated_address, 234); let contract_address = contract.deploy(@ArrayTrait::new()).unwrap(); + + assert(contract_address == precalculated_address, 'Addresses dont match'); let dispatcher = IConstructorWarpCheckerDispatcher { contract_address }; assert(dispatcher.get_stored_block_timestamp() == 234, 'Wrong stored timestamp'); From 12656521ba5a818aafcea4dc52337cb43fdeca70 Mon Sep 17 00:00:00 2001 From: Arcticae Date: Tue, 22 Aug 2023 11:35:24 +0200 Subject: [PATCH 2/9] Fix type --- crates/cheatnet/src/cheatcodes/deploy.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/cheatnet/src/cheatcodes/deploy.rs b/crates/cheatnet/src/cheatcodes/deploy.rs index 32dc9a8ccd..df9eba038d 100644 --- a/crates/cheatnet/src/cheatcodes/deploy.rs +++ b/crates/cheatnet/src/cheatcodes/deploy.rs @@ -29,7 +29,8 @@ impl CheatnetState { let account_address = ContractAddress(patricia_key!(TEST_ACCOUNT_CONTRACT_ADDRESS)); let entry_point_selector = selector_from_name("deploy_contract"); let salt = self.get_salt(); - let contract_address = self.precalculate_address(class_hash_to_felt(class_hash), calldata); + let contract_address = + self.precalculate_address(class_hash_to_felt(*class_hash.clone()), calldata); let class_hash = ClassHash(StarkFelt::new(class_hash.to_be_bytes()).unwrap()); self.increment_deploy_salt_base(); From 91c3af83a4070d7d8a5768d2a65a1aa90ad174a8 Mon Sep 17 00:00:00 2001 From: Arcticae Date: Tue, 22 Aug 2023 11:50:02 +0200 Subject: [PATCH 3/9] Unignore tests. Fix after merge --- crates/cheatnet/src/cheatcodes/deploy.rs | 6 ++---- crates/cheatnet/tests/cheatcodes/roll.rs | 1 - crates/cheatnet/tests/cheatcodes/warp.rs | 1 - 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/crates/cheatnet/src/cheatcodes/deploy.rs b/crates/cheatnet/src/cheatcodes/deploy.rs index df9eba038d..cce7b83582 100644 --- a/crates/cheatnet/src/cheatcodes/deploy.rs +++ b/crates/cheatnet/src/cheatcodes/deploy.rs @@ -16,7 +16,7 @@ use starknet_api::transaction::ContractAddressSalt; use starknet_api::{patricia_key, stark_felt}; use super::CheatcodeError; -use crate::conversions::{class_hash_to_felt, felt_from_short_string, field_element_to_felt252}; +use crate::conversions::{felt_from_short_string, field_element_to_felt252}; use crate::rpc::{call_contract, CallContractOutput}; impl CheatnetState { @@ -29,9 +29,7 @@ impl CheatnetState { let account_address = ContractAddress(patricia_key!(TEST_ACCOUNT_CONTRACT_ADDRESS)); let entry_point_selector = selector_from_name("deploy_contract"); let salt = self.get_salt(); - let contract_address = - self.precalculate_address(class_hash_to_felt(*class_hash.clone()), calldata); - let class_hash = ClassHash(StarkFelt::new(class_hash.to_be_bytes()).unwrap()); + let contract_address = self.precalculate_address(class_hash, calldata); self.increment_deploy_salt_base(); let blockifier_state: &mut CachedState = &mut self.blockifier_state; diff --git a/crates/cheatnet/tests/cheatcodes/roll.rs b/crates/cheatnet/tests/cheatcodes/roll.rs index 27a2e53754..ef44985872 100644 --- a/crates/cheatnet/tests/cheatcodes/roll.rs +++ b/crates/cheatnet/tests/cheatcodes/roll.rs @@ -44,7 +44,6 @@ fn roll_with_other_syscall() { } #[test] -#[ignore = "TODO(#254)"] fn roll_in_constructor() { let mut state = create_cheatnet_state(); diff --git a/crates/cheatnet/tests/cheatcodes/warp.rs b/crates/cheatnet/tests/cheatcodes/warp.rs index b93d4a9f30..26aa849b54 100644 --- a/crates/cheatnet/tests/cheatcodes/warp.rs +++ b/crates/cheatnet/tests/cheatcodes/warp.rs @@ -44,7 +44,6 @@ fn warp_with_other_syscall() { } #[test] -#[ignore = "TODO(#254)"] fn warp_in_constructor() { let mut state = create_cheatnet_state(); From f3c72ada4ea076da557ed7b7686902bea8e836ec Mon Sep 17 00:00:00 2001 From: Arcticae Date: Tue, 22 Aug 2023 12:24:20 +0200 Subject: [PATCH 4/9] Clippy, tests fix --- crates/cheatnet/src/cheatcodes/deploy.rs | 32 +++++++++---------- crates/cheatnet/src/conversions.rs | 2 +- crates/cheatnet/src/rpc.rs | 12 ++++--- crates/forge/src/cheatcodes_hint_processor.rs | 2 +- 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/crates/cheatnet/src/cheatcodes/deploy.rs b/crates/cheatnet/src/cheatcodes/deploy.rs index cce7b83582..59607d1fe0 100644 --- a/crates/cheatnet/src/cheatcodes/deploy.rs +++ b/crates/cheatnet/src/cheatcodes/deploy.rs @@ -35,7 +35,7 @@ impl CheatnetState { let blockifier_state: &mut CachedState = &mut self.blockifier_state; let contract_class = blockifier_state - .get_compiled_contract_class(&class_hash) + .get_compiled_contract_class(class_hash) .map_err::(From::from)?; if contract_class.constructor_selector().is_none() && !calldata.is_empty() { return Err(CheatcodeError::Recoverable(vec![felt_from_short_string( @@ -45,7 +45,7 @@ impl CheatnetState { let execute_calldata = create_execute_calldata( calldata, - &class_hash, + class_hash, &account_address, &entry_point_selector, &salt, @@ -108,14 +108,14 @@ mod test { assert_eq!( calldata, vec![ - StarkFelt::from(111_u32), - StarkFelt::from(222_u32), - StarkFelt::from(5_u32), - StarkFelt::from(123_u32), - StarkFelt::from(333_u32), - StarkFelt::from(2_u32), - StarkFelt::from(100_u32), - StarkFelt::from(200_u32), + Felt252::from(111_u32), + Felt252::from(222_u32), + Felt252::from(5_u32), + Felt252::from(123_u32), + Felt252::from(333_u32), + Felt252::from(2_u32), + Felt252::from(100_u32), + Felt252::from(200_u32), ] ); } @@ -132,12 +132,12 @@ mod test { assert_eq!( calldata, vec![ - StarkFelt::from(111_u32), - StarkFelt::from(222_u32), - StarkFelt::from(3_u32), - StarkFelt::from(123_u32), - StarkFelt::from(333_u32), - StarkFelt::from(0_u32), + Felt252::from(111_u32), + Felt252::from(222_u32), + Felt252::from(3_u32), + Felt252::from(123_u32), + Felt252::from(333_u32), + Felt252::from(0_u32), ] ); } diff --git a/crates/cheatnet/src/conversions.rs b/crates/cheatnet/src/conversions.rs index 0f7be20490..b91bb97467 100644 --- a/crates/cheatnet/src/conversions.rs +++ b/crates/cheatnet/src/conversions.rs @@ -28,7 +28,7 @@ pub fn class_hash_to_felt(class_hash: ClassHash) -> Felt252 { #[must_use] pub fn class_hash_from_felt(felt: &Felt252) -> ClassHash { - return ClassHash(felt_to_stark_felt(felt)); + ClassHash(felt_to_stark_felt(felt)) } #[must_use] diff --git a/crates/cheatnet/src/rpc.rs b/crates/cheatnet/src/rpc.rs index 589c8a1054..48f529ce5d 100644 --- a/crates/cheatnet/src/rpc.rs +++ b/crates/cheatnet/src/rpc.rs @@ -642,10 +642,12 @@ fn deploy_syscall( remaining_gas: &mut u64, ) -> SyscallResult { let deployer_address = syscall_handler.syscall_handler.storage_address(); - let deployer_address_for_calculation = match request.deploy_from_zero { - true => ContractAddress::default(), - false => deployer_address, + let deployer_address_for_calculation = if request.deploy_from_zero { + ContractAddress::default() + } else { + deployer_address }; + let deployed_contract_address = calculate_contract_address( request.contract_address_salt, request.class_hash, @@ -730,7 +732,7 @@ pub fn execute_constructor_entry_point( let contract_class = state.get_compiled_contract_class(&ctor_context.class_hash)?; let Some(constructor_selector) = contract_class.constructor_selector() else { // Contract has no constructor. - return handle_empty_constructor(ctor_context, calldata, remaining_gas.clone()); + return handle_empty_constructor(ctor_context, calldata, remaining_gas); }; let mut constructor_call = CallEntryPoint { @@ -742,7 +744,7 @@ pub fn execute_constructor_entry_point( storage_address: ctor_context.storage_address, caller_address: ctor_context.caller_address, call_type: CallType::Call, - initial_gas: remaining_gas.clone(), + initial_gas: remaining_gas, }; execute_call_entry_point( diff --git a/crates/forge/src/cheatcodes_hint_processor.rs b/crates/forge/src/cheatcodes_hint_processor.rs index 98eadadef6..4ccada2e6c 100644 --- a/crates/forge/src/cheatcodes_hint_processor.rs +++ b/crates/forge/src/cheatcodes_hint_processor.rs @@ -457,7 +457,7 @@ fn execute_call_contract(mut buffer: MemBuffer, cheatnet_state: &mut CheatnetSta let calldata = buffer.next_arr().unwrap(); let call_result = call_contract( - &contract_address_from_felt(&contract_address), + &contract_address, &entry_point_selector, &calldata, cheatnet_state, From 5a936f61e3ee5ecfbef95d5c55363844645fd8a6 Mon Sep 17 00:00:00 2001 From: Arcticae Date: Tue, 22 Aug 2023 13:20:39 +0200 Subject: [PATCH 5/9] Uncomment prank test, update changelog --- CHANGELOG.md | 1 + crates/cheatnet/tests/cheatcodes/prank.rs | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e8500a931..1a159f79a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 #### Added - support for `keccak_syscall` syscall. It can be used directly in cairo tests +- support for `roll`ing/`warp`ing/`prank`ing the constructor logic (precalculate address, prank, assert pranked state in constructor) ### Cast diff --git a/crates/cheatnet/tests/cheatcodes/prank.rs b/crates/cheatnet/tests/cheatcodes/prank.rs index e3ccaee1f0..fa9e1cd121 100644 --- a/crates/cheatnet/tests/cheatcodes/prank.rs +++ b/crates/cheatnet/tests/cheatcodes/prank.rs @@ -45,7 +45,6 @@ fn prank_with_other_syscall() { } #[test] -#[ignore = "TODO(#254)"] fn prank_in_constructor() { let mut state = create_cheatnet_state(); From d67f4a25344ffe9123c6a8324c430a661372ae28 Mon Sep 17 00:00:00 2001 From: Arcticae Date: Wed, 23 Aug 2023 17:30:35 +0200 Subject: [PATCH 6/9] Docs --- docs/src/testing/using-cheatcodes.md | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/docs/src/testing/using-cheatcodes.md b/docs/src/testing/using-cheatcodes.md index d3744b6da4..f6c87ca05d 100644 --- a/docs/src/testing/using-cheatcodes.md +++ b/docs/src/testing/using-cheatcodes.md @@ -167,3 +167,33 @@ Failure data: Tests: 0 passed, 1 failed, 0 skipped ``` + +### Pranking the constructor + +Most of the cheatcodes like `prank`, `mock_call`, `warp`, `roll` do work in the constructor of the contracts. + +Let's say, that you have a contract that saves the caller address (deployer) in the constructor, and you want it to be pre-set to a certain value. + +To `prank` the constructor, you need to `start_prank` before it is invoked, with the right address. +To achieve this, you need to precalculate address of the contract using `precalculate_address` of `ContractClassTrait` on declared contract, +and then use it in `start_prank` as an argument: + + +```rust +use snforge_std::{ declare, PreparedContract, ContractClassTrait, deploy, start_prank }; + +#[test] +fn prank_the_constructor() { + let contract = declare('HelloStarknet'); + let constructor_arguments = @ArrayTrait::new(); + + // Precalculate the address to obtain the contract address before the constructor call (deploy) itself + let contract_address = contract.precalculate_address(constructor_arguments); + + // Change the caller address to 123 before the call to contract.deploy + start_prank(contract_address, 123.try_into().unwrap()); + + // The constructor will have 123 set as the caller address + contract.deploy(constructor_arguments).unwrap(); +} +``` \ No newline at end of file From 358475105bd56986f5f491b2f08a2726e1472c69 Mon Sep 17 00:00:00 2001 From: Arcticae Date: Thu, 24 Aug 2023 10:40:25 +0200 Subject: [PATCH 7/9] Fix, review --- crates/cheatnet/src/cheatcodes/deploy.rs | 4 ++++ crates/cheatnet/tests/cheatcodes/mock_call.rs | 1 - crates/forge/src/cheatcodes_hint_processor.rs | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/cheatnet/src/cheatcodes/deploy.rs b/crates/cheatnet/src/cheatcodes/deploy.rs index 59607d1fe0..388499b5cf 100644 --- a/crates/cheatnet/src/cheatcodes/deploy.rs +++ b/crates/cheatnet/src/cheatcodes/deploy.rs @@ -8,6 +8,7 @@ use blockifier::execution::execution_utils::{felt_to_stark_felt, stark_felt_to_f use blockifier::state::cached_state::CachedState; use blockifier::state::state_api::StateReader; use cairo_felt::Felt252; +use cairo_vm::vm::errors::hint_errors::HintError::CustomHint; use starknet::core::utils::get_selector_from_name; use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector, PatriciaKey}; @@ -64,6 +65,9 @@ impl CheatnetState { CallContractOutput::Panic { panic_data } => { Err(CheatcodeError::Recoverable(panic_data)) } + CallContractOutput::Error { msg } => Err(CheatcodeError::Unrecoverable( + EnhancedHintError::from(CustomHint(Box::from(msg))), + )), } } } diff --git a/crates/cheatnet/tests/cheatcodes/mock_call.rs b/crates/cheatnet/tests/cheatcodes/mock_call.rs index b0a6937956..2c4c460f75 100644 --- a/crates/cheatnet/tests/cheatcodes/mock_call.rs +++ b/crates/cheatnet/tests/cheatcodes/mock_call.rs @@ -296,7 +296,6 @@ fn mock_call_not_implemented() { } #[test] -#[ignore = "TODO(#254)"] fn mock_call_in_constructor() { let mut state = create_cheatnet_state(); diff --git a/crates/forge/src/cheatcodes_hint_processor.rs b/crates/forge/src/cheatcodes_hint_processor.rs index 64c71a5967..29fc919d4c 100644 --- a/crates/forge/src/cheatcodes_hint_processor.rs +++ b/crates/forge/src/cheatcodes_hint_processor.rs @@ -360,7 +360,7 @@ impl CairoHintProcessor<'_> { } } "l1_handler_execute" => { - let contract_address = contract_address_from_felt252(&inputs[0])?; + let contract_address = contract_address_from_felt(&inputs[0]); let function_name = inputs[1].clone(); let from_address = inputs[2].clone(); let fee = inputs[3].clone(); From 755b6ef502d0a95c65bcf98dc9ac10219f7c6750 Mon Sep 17 00:00:00 2001 From: Arcticae Date: Thu, 24 Aug 2023 12:47:59 +0200 Subject: [PATCH 8/9] Fix mock call in constructor test --- crates/cheatnet/tests/cheatcodes/mock_call.rs | 30 +++++++++++-------- .../src/constructor_mock_checker.cairo | 26 ++++++++-------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/crates/cheatnet/tests/cheatcodes/mock_call.rs b/crates/cheatnet/tests/cheatcodes/mock_call.rs index 2c4c460f75..beb873576f 100644 --- a/crates/cheatnet/tests/cheatcodes/mock_call.rs +++ b/crates/cheatnet/tests/cheatcodes/mock_call.rs @@ -1,3 +1,4 @@ +use crate::common::recover_data; use crate::{ assert_success, common::{deploy_contract, get_contracts, state::create_cheatnet_state}, @@ -301,26 +302,31 @@ fn mock_call_in_constructor() { let contracts = get_contracts(); - let contract_name = felt_from_short_string("ConstructorMockChecker"); + let contract_name = felt_from_short_string("HelloStarknet"); let class_hash = state.declare(&contract_name, &contracts).unwrap(); - let precalculated_address = state.precalculate_address(&class_hash, &[]); - - let ret_data = vec![Felt252::from(123)]; + let balance_contract_address = state.deploy(&class_hash, &[]).unwrap(); + let ret_data = vec![Felt252::from(223)]; state.start_mock_call( - precalculated_address, - &felt_from_short_string("get_constant_thing"), + balance_contract_address, + &felt_from_short_string("get_balance"), &ret_data, ); - let contract_address = state.deploy(&class_hash, &[]).unwrap(); - - assert_eq!(precalculated_address, contract_address); + let contract_name = felt_from_short_string("ConstructorMockChecker"); + let class_hash = state.declare(&contract_name, &contracts).unwrap(); + let contract_address = state + .deploy( + &class_hash, + &[contract_address_to_felt(balance_contract_address)], + ) + .unwrap(); - let selector = felt_selector_from_name("get_stored_thing"); + let selector = felt_selector_from_name("get_constructor_balance"); let output = call_contract(&contract_address, &selector, &[], &mut state).unwrap(); - - assert_success!(output, ret_data); + let output_data = recover_data(output); + assert_eq!(output_data.len(), 1); + assert_eq!(output_data.get(0).unwrap().clone(), Felt252::from(223)); } #[test] diff --git a/crates/cheatnet/tests/contracts/src/constructor_mock_checker.cairo b/crates/cheatnet/tests/contracts/src/constructor_mock_checker.cairo index b9fa0b57c6..5cd6a9e58a 100644 --- a/crates/cheatnet/tests/contracts/src/constructor_mock_checker.cairo +++ b/crates/cheatnet/tests/contracts/src/constructor_mock_checker.cairo @@ -2,33 +2,33 @@ use starknet::ContractAddress; #[starknet::interface] trait IConstructorMockChecker { - fn get_stored_thing(ref self: TContractState) -> felt252; - fn get_constant_thing(ref self: TContractState) -> felt252; + fn get_constructor_balance(ref self: TContractState) -> felt252; } #[starknet::contract] mod ConstructorMockChecker { - use super::IConstructorMockChecker; + use starknet::ContractAddress; + + #[starknet::interface] + trait IHelloStarknet { + fn get_balance(self: @TContractState) -> felt252; + } #[storage] struct Storage { - stored_thing: felt252, + constructor_balance: felt252 } #[constructor] - fn constructor(ref self: ContractState) { - let const_thing = self.get_constant_thing(); - self.stored_thing.write(const_thing); + fn constructor(ref self: ContractState, balance_contract_address: ContractAddress) { + let hello_starknet = IHelloStarknetDispatcher { contract_address: balance_contract_address }; + self.constructor_balance.write(hello_starknet.get_balance()); } #[external(v0)] impl IConstructorMockCheckerImpl of super::IConstructorMockChecker { - fn get_constant_thing(ref self: ContractState) -> felt252 { - 13 - } - - fn get_stored_thing(ref self: ContractState) -> felt252 { - self.stored_thing.read() + fn get_constructor_balance(ref self: ContractState) -> felt252 { + self.constructor_balance.read() } } } From d35aa372f6abeebf96f69384781a41004908bf23 Mon Sep 17 00:00:00 2001 From: Arcticae Date: Thu, 24 Aug 2023 12:55:05 +0200 Subject: [PATCH 9/9] Mark modifed region --- crates/cheatnet/src/rpc.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/cheatnet/src/rpc.rs b/crates/cheatnet/src/rpc.rs index 56f74f3d2b..c9b8acfcd1 100644 --- a/crates/cheatnet/src/rpc.rs +++ b/crates/cheatnet/src/rpc.rs @@ -765,7 +765,7 @@ pub fn execute_constructor_entry_point( call_type: CallType::Call, initial_gas: remaining_gas, }; - + // region: Modified code execute_call_entry_point( &mut constructor_call, state, @@ -773,6 +773,7 @@ pub fn execute_constructor_entry_point( resources, context, ) + // endregion } // Inspired by blockifier::execution::syscalls::library_call