From 978d89d02579f65c61eacd9780460fc63f6786fc Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 5 Jun 2023 11:04:48 +0300 Subject: [PATCH 01/17] multisig interactor nft scenario test --- .../examples/multisig/interact-rs/.gitignore | 2 +- .../scenarios/interactor_nft.scen.json | 517 ++++++++++++++++++ ...e.scen.json => interactor_wegld.scen.json} | 0 .../tests/multisig_scenario_go_test.rs | 10 +- .../tests/multisig_scenario_rs_test.rs | 10 +- 5 files changed, 534 insertions(+), 5 deletions(-) create mode 100644 contracts/examples/multisig/scenarios/interactor_nft.scen.json rename contracts/examples/multisig/scenarios/{interactor_trace.scen.json => interactor_wegld.scen.json} (100%) diff --git a/contracts/examples/multisig/interact-rs/.gitignore b/contracts/examples/multisig/interact-rs/.gitignore index ea1b920080..0d3b55f264 100644 --- a/contracts/examples/multisig/interact-rs/.gitignore +++ b/contracts/examples/multisig/interact-rs/.gitignore @@ -5,4 +5,4 @@ state.toml # Trace file of interactor tooling -interactor_trace.scen.json +interactor*.scen.json diff --git a/contracts/examples/multisig/scenarios/interactor_nft.scen.json b/contracts/examples/multisig/scenarios/interactor_nft.scen.json new file mode 100644 index 0000000000..2f091e6549 --- /dev/null +++ b/contracts/examples/multisig/scenarios/interactor_nft.scen.json @@ -0,0 +1,517 @@ +{ + "steps": [ + { + "step": "setState", + "accounts": { + "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60": { + "nonce": "501", + "balance": "106019095933290000003", + "esdt": { + "str:CAN-14dc0a": "1000", + "str:CAN-2abf4b": "1000", + "str:CAN-6d39e6": "1000", + "str:CAN-ac1592": "1000" + }, + "username": "" + } + } + }, + { + "step": "setState", + "accounts": { + "0xb2a11555ce521e4944e09ab17549d85b487dcd26c84b5017a39e31a3670889ba": { + "nonce": "190", + "balance": "3715097382200000000", + "esdt": { + "str:TEST-d2b50f": { + "instances": [ + { + "nonce": "1", + "balance": "2" + } + ] + } + }, + "username": "" + } + } + }, + { + "step": "setState", + "accounts": { + "0xb13a017423c366caff8cecfb77a12610a130f4888134122c7937feae0d6d7d17": { + "nonce": "181", + "balance": "7776837070240000000", + "username": "" + } + } + }, + { + "step": "setState", + "accounts": { + "0x3af8d9c9423b2577c6252722c1d90212a4111f7203f9744f76fcfa1d0a310033": { + "nonce": "396", + "balance": "62236676937310000000", + "esdt": { + "str:USDC-091bd3": "9997000000000", + "str:XRF-079f0d": "999999805000000000000000000", + "str:XUSDC-929b9b": "1000000000000000000000000" + }, + "username": "" + } + } + }, + { + "step": "setState", + "accounts": { + "0x00000000000000000500c114ee7698050a4d40c092add8c31d25e99a6e1ec2ee": { + "nonce": "0", + "balance": "54064792195269569513", + "esdt": { + "str:WEGLD-6cf38e": { + "instances": [], + "roles": [ + "ESDTRoleLocalBurn", + "ESDTRoleLocalMint" + ] + } + }, + "username": "", + "storage": { + "0x7772617070656445676c64546f6b656e4964": "0x5745474c442d366366333865" + }, + "code": "0x0061736d0100000001661160000060017f0060027f7f017f60027f7f006000017f60017f017f60037f7f7f0060037f7f7f017f60057f7f7e7f7f017f60047f7f7f7f0060067e7f7f7f7f7f017f60047f7f7f7f017f60027f7e0060017e006000017e60057f7f7f7f7f0060037e7f7f0002cc062303656e760b7369676e616c4572726f72000303656e7618626967496e7447657445787465726e616c42616c616e6365000303656e760a6d4275666665724e6577000403656e760d6d427566666572417070656e64000203656e76096d4275666665724571000203656e76106d616e61676564534341646472657373000103656e761b6d616e61676564457865637574654f6e44657374436f6e74657874000a03656e760f636c65616e52657475726e44617461000003656e760d6d616e6167656443616c6c6572000103656e76136d616e616765644f776e657241646472657373000103656e76126d427566666572476574417267756d656e74000203656e760f6765744e756d417267756d656e7473000403656e76126d427566666572417070656e644279746573000703656e760f6d4275666665725365744279746573000703656e76106d4275666665724765744c656e677468000503656e76196d42756666657246726f6d426967496e74556e7369676e6564000203656e76136d42756666657247657442797465536c696365000b03656e76126d42756666657253746f726167654c6f6164000203656e76126d616e616765645369676e616c4572726f72000103656e760e626967496e74536574496e743634000c03656e760a626967496e745369676e000503656e76136d42756666657253746f7261676553746f7265000203656e760e636865636b4e6f5061796d656e74000003656e7614626967496e7446696e697368556e7369676e6564000103656e760d6d42756666657246696e697368000503656e7614736d616c6c496e7446696e6973685369676e6564000d03656e761c6d616e616765644765744d756c74694553445443616c6c56616c7565000103656e7609626967496e74436d70000203656e760a6765744761734c656674000e03656e761b6d616e616765645472616e7366657256616c756545786563757465000803656e76136765744e756d455344545472616e7366657273000403656e7612626967496e7447657443616c6c56616c7565000103656e7609626967496e74416464000603656e76226d616e616765644d756c74695472616e73666572455344544e465445786563757465000803656e760f6d42756666657247657442797465730002032c2b0f00040405020310010400010303040503020301050509030306060900040501000000000000000000000005030100110619037f01418080c0000b7f0041e5d1c0000b7f0041f0d1c0000b079b010c066d656d6f7279020004696e69740044146765744c6f636b656445676c6442616c616e63650045156765745772617070656445676c64546f6b656e496400460869735061757365640047057061757365004807756e706175736500490a756e7772617045676c64004a087772617045676c64004b0863616c6c4261636b004c0a5f5f646174615f656e6403010b5f5f686561705f6261736503020abc162b2e000240200120024d0440200220044d0d011024000b1024000b2000200220016b3602042000200120036a3602000b05001043000b2301027f10262200100520001026210041c5d1c00010221a41c5d1c0002000100120000b1b01017f41a483c00041a483c00028020041016b220036020020000b0f01017f10022201200010031a20010b0b0020002001100441004a0b0900200020011000000b1f01017f4167100510262203102b20004167200320012002102610061a10070b08002000420010130b0c01017f10262200100820000b1e01017f1026220010092000102c102804400f0b41f082c00041241000000b1500100b20004604400f0b41cb81c00041191000000b4701017f230041106b2202240020022001410874418080fc077120014118747220014108764180fe03712001411876727236020c20002002410c6a4104100c1a200241106a24000b0d0010311a200020011032102f0b1401017f1026220041d482c0004100100d1a20000b0f01017f102622012000100f1a20010b0d0010311a200020011027102f0b1101017f1026220220002001100d1a20020b09002000200110031a0b5501017f20002d00042101200041003a000402402001410171044041a883c00028020022014191ce004f0d01200028020041ac83c0002001100c1a41a883c000410036020041bcd1c00041003a00000b0f0b1024000bae0102027f017e230041106b2202240020024200370308200010382200100e220141094904402002200241086a41082001103920004100200228020422002002280200220110101a027f41002000450d001a034020000440200041016b210020013100002003420886842103200141016a21010c010b0b02402003420158044041002003a741016b0d021a0c010b41f281c0004112103a000b41010b200241106a24000f0b41e481c000410e103a000b0d0020001026220010111a20000b3b01017f230041106b22042400200441086a41002003200120021023200428020c21012000200428020836020020002001360204200441106a24000b1b01017f418482c00041161034220220002001100c1a20021012000bf20101047f230041106b2203240020032000100e22024118742002410874418080fc07717220024108764180fe03712002411876727236020c20012003410c6a4104103c20012d00042102200141003a00040240024002402002410171220204402000100e22054190ce0041a883c00028020022046b4b0d0220032004200420056a2204103d200041002003280204200328020010101a41a883c00020043602000c010b2001280200200010350b200120023a00040c010b2001103620012802002000103520012d0004200120023a0004410171450d0041a883c000410036020041bcd1c00041003a00000b200341106a24000b800101027f230041106b220324000240024020002d000404404190ce0041a883c00028020022046b2002490d01200341086a2004200220046a2200103d2003280208200328020c20012002103e41a883c00020003602000c020b200028020020012002100c1a0c010b20001036200028020020012002100c1a0b200341106a24000b4001017f230041106b22032400200341086a2001200241ac83c0004190ce001023200328020c21012000200328020836020020002001360204200341106a24000bb00201067f2001200346044020012203410f4b04402000410020006b41037122056a210420050440200221010340200020012d00003a0000200141016a2101200041016a22002004490d000b0b2004200320056b2203417c7122066a21000240200220056a220541037122010440200641004c0d012005417c71220741046a21024100200141037422086b4118712109200728020021010340200420012008762002280200220120097472360200200241046a2102200441046a22042000490d000b0c010b200641004c0d0020052102034020042002280200360200200241046a2102200441046a22042000490d000b0b20034103712103200520066a21020b20030440200020036a21010340200020022d00003a0000200241016a2102200041016a22002001490d000b0b0f0b1043000b1c0041be82c0004113103410374504400f0b419a82c00041121029000b0b0041ac82c000411210340b09002000101441004a0ba60101037f230041106b2201240041be82c00041131034200142808080808080808001420020001b3703080240200045044041d482c00021000c010b4100210003400240024020004108470440200141086a20006a2d00002202450d022002411874411f7520006a220041094f0d01410820006b21022000200141086a6a21000c040b1043000b104d000b200041016a21000c000b000b20002002103410151a200141106a24000b0500104d000b1a01017f10164101102e410010262200100a1a1040200010151a0b0c0010164100102e102510170b0f0010164100102e1040103810181a0b160010164100102e41be82c000411310341037ad10190b0e001016102d4100102e410110420b0e001016102d4100102e410010420bed0302097f017e230041106b220124004100102e103f416b2102024041c4d1c0002d000022000440416b41ffffffff0720001b21020c010b41c4d1c00041013a0000416b101a0b02402002100e4170714110460440410021002002100e2106200141086a2107410121050340200041106a220420064b0d022007420037030020014200370300200220004110200110101a2005044020012902042209423886200942288642808080808080c0ff0083842009421886428080808080e03f8320094208864280808080f01f838484200942088842808080f80f832009421888428080fc07838420094228884280fe038320094238888484842109200128020022004118742000410874418080fc07717220004108764180fe0371200041187672722108200128020c22004118742000410874418080fc07717220004108764180fe037120004118767272210341002105200421000c010b0b1043000b418481c00041221000000b024002400240200950044020081040103822041028450d0120031041450d0220031025101b41004a0d031031220020041033200020031030101c41ea80c000410d10342000102a102c2003420010311031101d1a200141106a24000f0b41d482c000411c1000000b418080c00041101029000b419080c000411c1029000b41ac80c00041231029000beb0301097f230041206b220124000240101e4504404100102e103f41752103024041c0d1c0002d000022000440417541ffffffff0720001b21030c010b41c0d1c00041013a00004175101f0b20031041450d011040103821041031220020041033200020031030101c41f780c000410d10342000102a102c10312107103121081031210520041027210210262200102b20002000200310202001420037020c20012002410874418080fc077120024118747220024108764180fe03712002411876727236020820012000410874418080fc077120004118747220004108764180fe0371200041187672723602142005200141086a4110100c1a200542002007200810211a2001027f41bcd1c0002d0000220045044041bcd1c00041013a000041a883c0004100360200200141ac83c0004190ce00410010392001280200200128020441d482c0004100103e10310c010b41d482c000410010340b360218200120004101733a001c2004200141186a2200103b200142003703082000200141086a22024108103c200310322000103b20012802182100200120012d001c3a000c2001200036020820021036200128020820012d000c044041a883c000410036020041bcd1c00041003a00000b10181a200141206a24000f0b41a681c00041251000000b41cf80c000411b1029000b0300010b0c00419483c000410e1000000b0bb8030200418080c0000ba20357726f6e67206573647420746f6b656e4d75737420706179206d6f7265207468616e203020746f6b656e7321436f6e747261637420646f6573206e6f74206861766520656e6f7567682066756e64735061796d656e74206d757374206265206d6f7265207468616e2030455344544c6f63616c4275726e455344544c6f63616c4d696e74696e636f7272656374206e756d626572206f662045534454207472616e736665727366756e6374696f6e20646f6573206e6f74206163636570742045534454207061796d656e7477726f6e67206e756d626572206f6620617267756d656e7473696e70757420746f6f206c6f6e67696e707574206f7574206f662072616e676573746f72616765206465636f6465206572726f723a20436f6e7472616374206973207061757365647772617070656445676c64546f6b656e496470617573655f6d6f64756c653a70617573656400000066756e6769626c65204553445420746f6b656e206578706563746564456e64706f696e742063616e206f6e6c792062652063616c6c6564206279206f776e657270616e6963206f636375727265640041a483c0000b049cffffff" + } + } + }, + { + "step": "setState", + "newAddresses": [ + { + "creatorAddress": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "creatorNonce": "501", + "newAddress": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60" + } + ] + }, + { + "step": "scDeploy", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "contractCode": "file:../output/multisig.wasm", + "arguments": [ + "0x02", + "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "0xb2a11555ce521e4944e09ab17549d85b487dcd26c84b5017a39e31a3670889ba", + "0xb13a017423c366caff8cecfb77a12610a130f4888134122c7937feae0d6d7d17", + "0x3af8d9c9423b2577c6252722c1d90212a4111f7203f9744f76fcfa1d0a310033" + ], + "gasLimit": "70,000,000", + "gasPrice": "" + }, + "expect": { + "status": "0" + } + }, + { + "step": "transfer", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "egldValue": "0,050000000000000000", + "gasLimit": "50,000" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "proposeAsyncCall", + "arguments": [ + "0x000000000000000000010000000000000000000000000000000000000002ffff", + "0xb1a2bc2ec50000", + "0x69737375654e6f6e46756e6769626c65", + "0x54657374436f6c6c656374696f6e31", + "0x54455354434f4c4c31" + ], + "gasLimit": "10,000,000", + "gasPrice": "" + }, + "expect": { + "status": "0" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xb2a11555ce521e4944e09ab17549d85b487dcd26c84b5017a39e31a3670889ba", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "sign", + "arguments": [ + "0x01" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xb13a017423c366caff8cecfb77a12610a130f4888134122c7937feae0d6d7d17", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "sign", + "arguments": [ + "0x01" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0x3af8d9c9423b2577c6252722c1d90212a4111f7203f9744f76fcfa1d0a310033", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "sign", + "arguments": [ + "0x01" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "performAction", + "arguments": [ + "0x01" + ], + "gasLimit": "80,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "proposeAsyncCall", + "arguments": [ + "0x000000000000000000010000000000000000000000000000000000000002ffff", + "0x", + "0x7365745370656369616c526f6c65", + "0x54455354434f4c4c312d313431366462", + "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "0x45534454526f6c654e4654437265617465" + ], + "gasLimit": "10,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xb2a11555ce521e4944e09ab17549d85b487dcd26c84b5017a39e31a3670889ba", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "sign", + "arguments": [ + "0x02" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xb13a017423c366caff8cecfb77a12610a130f4888134122c7937feae0d6d7d17", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "sign", + "arguments": [ + "0x02" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0x3af8d9c9423b2577c6252722c1d90212a4111f7203f9744f76fcfa1d0a310033", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "sign", + "arguments": [ + "0x02" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "performAction", + "arguments": [ + "0x02" + ], + "gasLimit": "80,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "proposeAsyncCall", + "arguments": [ + "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "0x", + "0x455344544e4654437265617465", + "0x54455354434f4c4c312d313431366462", + "0x01", + "0x5465737420636f6c6c656374696f6e206974656d202330", + "0x0bb8", + "0x", + "0x746167733a746573742c727573742d696e7465726163746f72", + "0x68747470733a2f2f697066732e696f2f697066732f516d5979416145663170684a53356d4e3677666f7535646535476270556464427854593156656b4b636a643550432f6e667430302e706e67" + ], + "gasLimit": "10,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "proposeAsyncCall", + "arguments": [ + "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "0x", + "0x455344544e4654437265617465", + "0x54455354434f4c4c312d313431366462", + "0x01", + "0x5465737420636f6c6c656374696f6e206974656d202331", + "0x0bb8", + "0x", + "0x746167733a746573742c727573742d696e7465726163746f72", + "0x68747470733a2f2f697066732e696f2f697066732f516d5979416145663170684a53356d4e3677666f7535646535476270556464427854593156656b4b636a643550432f6e667430312e706e67" + ], + "gasLimit": "10,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "proposeAsyncCall", + "arguments": [ + "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "0x", + "0x455344544e4654437265617465", + "0x54455354434f4c4c312d313431366462", + "0x01", + "0x5465737420636f6c6c656374696f6e206974656d202332", + "0x0bb8", + "0x", + "0x746167733a746573742c727573742d696e7465726163746f72", + "0x68747470733a2f2f697066732e696f2f697066732f516d5979416145663170684a53356d4e3677666f7535646535476270556464427854593156656b4b636a643550432f6e667430322e706e67" + ], + "gasLimit": "10,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xb2a11555ce521e4944e09ab17549d85b487dcd26c84b5017a39e31a3670889ba", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "sign", + "arguments": [ + "0x03" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xb13a017423c366caff8cecfb77a12610a130f4888134122c7937feae0d6d7d17", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "sign", + "arguments": [ + "0x03" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0x3af8d9c9423b2577c6252722c1d90212a4111f7203f9744f76fcfa1d0a310033", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "sign", + "arguments": [ + "0x03" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xb2a11555ce521e4944e09ab17549d85b487dcd26c84b5017a39e31a3670889ba", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "sign", + "arguments": [ + "0x04" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xb13a017423c366caff8cecfb77a12610a130f4888134122c7937feae0d6d7d17", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "sign", + "arguments": [ + "0x04" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0x3af8d9c9423b2577c6252722c1d90212a4111f7203f9744f76fcfa1d0a310033", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "sign", + "arguments": [ + "0x04" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xb2a11555ce521e4944e09ab17549d85b487dcd26c84b5017a39e31a3670889ba", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "sign", + "arguments": [ + "0x05" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xb13a017423c366caff8cecfb77a12610a130f4888134122c7937feae0d6d7d17", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "sign", + "arguments": [ + "0x05" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0x3af8d9c9423b2577c6252722c1d90212a4111f7203f9744f76fcfa1d0a310033", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "sign", + "arguments": [ + "0x05" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "performAction", + "arguments": [ + "0x03" + ], + "gasLimit": "30,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "performAction", + "arguments": [ + "0x04" + ], + "gasLimit": "30,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", + "function": "performAction", + "arguments": [ + "0x05" + ], + "gasLimit": "30,000,000", + "gasPrice": "" + } + } + ] +} diff --git a/contracts/examples/multisig/scenarios/interactor_trace.scen.json b/contracts/examples/multisig/scenarios/interactor_wegld.scen.json similarity index 100% rename from contracts/examples/multisig/scenarios/interactor_trace.scen.json rename to contracts/examples/multisig/scenarios/interactor_wegld.scen.json diff --git a/contracts/examples/multisig/tests/multisig_scenario_go_test.rs b/contracts/examples/multisig/tests/multisig_scenario_go_test.rs index 5335581336..3983c7a619 100644 --- a/contracts/examples/multisig/tests/multisig_scenario_go_test.rs +++ b/contracts/examples/multisig/tests/multisig_scenario_go_test.rs @@ -55,8 +55,14 @@ fn deploy_duplicate_bm_go() { } #[test] -fn interactor_trace_go() { - world().run("scenarios/interactor_trace.scen.json"); +#[ignore = "system SC not yet implemented"] +fn interactor_nft_go() { + world().run("scenarios/interactor_nft.scen.json"); +} + +#[test] +fn interactor_wegld_go() { + world().run("scenarios/interactor_wegld.scen.json"); } #[test] diff --git a/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs b/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs index 1b78711bb0..a418d8e7a5 100644 --- a/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs +++ b/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs @@ -82,8 +82,14 @@ fn deploy_duplicate_bm_rs() { } #[test] -fn interactor_trace_rs() { - world().run("scenarios/interactor_trace.scen.json"); +#[ignore = "system SC not yet implemented"] +fn interactor_nft_rs() { + world().run("scenarios/interactor_nft.scen.json"); +} + +#[test] +fn interactor_wegld_rs() { + world().run("scenarios/interactor_wegld.scen.json"); } #[test] From f62dc6a153d5fb00de755c1d2a176461b7ac2062 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 5 Jun 2023 13:40:16 +0300 Subject: [PATCH 02/17] system sc stub --- .../tests/multisig_scenario_rs_test.rs | 1 - vm/Cargo.toml | 1 + .../mod.rs => builtin_function_mocks.rs} | 0 vm/src/tx_execution/exec_call.rs | 10 +++- vm/src/tx_execution/exec_general_tx.rs | 20 ++++--- vm/src/tx_execution/mod.rs | 1 + vm/src/tx_execution/system_sc.rs | 54 +++++++++++++++++++ .../tx_execution/system_sc/system_sc_issue.rs | 13 +++++ 8 files changed, 91 insertions(+), 9 deletions(-) rename vm/src/tx_execution/{builtin_function_mocks/mod.rs => builtin_function_mocks.rs} (100%) create mode 100644 vm/src/tx_execution/system_sc.rs create mode 100644 vm/src/tx_execution/system_sc/system_sc_issue.rs diff --git a/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs b/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs index a418d8e7a5..3124ac2e89 100644 --- a/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs +++ b/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs @@ -82,7 +82,6 @@ fn deploy_duplicate_bm_rs() { } #[test] -#[ignore = "system SC not yet implemented"] fn interactor_nft_rs() { world().run("scenarios/interactor_nft.scen.json"); } diff --git a/vm/Cargo.toml b/vm/Cargo.toml index f8a0730d7d..e54a19c82b 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -27,6 +27,7 @@ rand_seeder = "0.2.2" ed25519-dalek = "1.0.1" itertools = "0.10.3" bech32 = "0.9" +hex-literal = "0.3.1" [dependencies.multiversx-sc] version = "=0.41.0" diff --git a/vm/src/tx_execution/builtin_function_mocks/mod.rs b/vm/src/tx_execution/builtin_function_mocks.rs similarity index 100% rename from vm/src/tx_execution/builtin_function_mocks/mod.rs rename to vm/src/tx_execution/builtin_function_mocks.rs diff --git a/vm/src/tx_execution/exec_call.rs b/vm/src/tx_execution/exec_call.rs index 07bede4188..041f9f612c 100644 --- a/vm/src/tx_execution/exec_call.rs +++ b/vm/src/tx_execution/exec_call.rs @@ -12,7 +12,9 @@ use crate::{ world_mock::{AccountData, AccountEsdt, BlockchainMock}, }; -use super::{execute_builtin_function_or_default, execute_tx_context}; +use super::{ + execute_builtin_function_or_default, execute_tx_context, system_sc::is_system_sc_address, +}; pub fn execute_sc_query(tx_input: TxInput, state: BlockchainMock) -> (TxResult, BlockchainMock) { let state_rc = Rc::new(state); @@ -37,11 +39,15 @@ pub fn execute_sc_call(tx_input: TxInput, mut state: BlockchainMock) -> (TxResul (tx_result, state) } +fn existing_account(state: &BlockchainMock, address: &Address) -> bool { + state.accounts.contains_key(address) || is_system_sc_address(address) +} + pub fn execute_async_call_and_callback( async_data: AsyncCallTxData, state: BlockchainMock, ) -> (TxResult, TxResult, BlockchainMock) { - if state.accounts.contains_key(&async_data.to) { + if existing_account(&state, &async_data.to) { let async_input = async_call_tx_input(&async_data); let (async_result, state) = sc_call_with_async_and_callback(async_input, state); diff --git a/vm/src/tx_execution/exec_general_tx.rs b/vm/src/tx_execution/exec_general_tx.rs index d33651ecf7..ff8692ca2c 100644 --- a/vm/src/tx_execution/exec_general_tx.rs +++ b/vm/src/tx_execution/exec_general_tx.rs @@ -5,7 +5,7 @@ use crate::tx_mock::{ BlockchainUpdate, TxCache, TxContext, TxFunctionName, TxInput, TxLog, TxResult, }; -use super::execute_tx_context; +use super::{execute_tx_context, system_sc}; pub fn default_execution(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { let mut tx_context = TxContext::new(tx_input, tx_cache); @@ -14,10 +14,13 @@ pub fn default_execution(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, Blo &tx_context.tx_input_box.from, &tx_context.tx_input_box.egld_value, ); - tx_context.tx_cache.increase_egld_balance( - &tx_context.tx_input_box.to, - &tx_context.tx_input_box.egld_value, - ); + + if !system_sc::is_system_sc_address(&tx_context.tx_input_box.to) { + tx_context.tx_cache.increase_egld_balance( + &tx_context.tx_input_box.to, + &tx_context.tx_input_box.egld_value, + ); + } // skip for transactions coming directly from scenario json, which should all be coming from user wallets // TODO: reorg context logic @@ -49,11 +52,16 @@ pub fn default_execution(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, Blo ); } - let mut tx_result = if !tx_context.tx_input_box.to.is_smart_contract_address() + let recipient_address = &tx_context.tx_input_box.to; + let mut tx_result = if !recipient_address.is_smart_contract_address() || tx_context.tx_input_box.func_name.is_empty() { // direct EGLD transfer TxResult::empty() + } else if system_sc::is_system_sc_address(recipient_address) { + let (tx_context_modified, tx_result) = system_sc::execute_system_sc(tx_context); + tx_context = tx_context_modified; + tx_result } else { let (tx_context_modified, tx_result) = execute_tx_context(tx_context); tx_context = tx_context_modified; diff --git a/vm/src/tx_execution/mod.rs b/vm/src/tx_execution/mod.rs index 01541d526e..991df07ee6 100644 --- a/vm/src/tx_execution/mod.rs +++ b/vm/src/tx_execution/mod.rs @@ -3,6 +3,7 @@ mod exec_call; mod exec_contract_endpoint; mod exec_create; mod exec_general_tx; +mod system_sc; pub use builtin_function_mocks::*; pub use exec_call::*; diff --git a/vm/src/tx_execution/system_sc.rs b/vm/src/tx_execution/system_sc.rs new file mode 100644 index 0000000000..afa94c8c30 --- /dev/null +++ b/vm/src/tx_execution/system_sc.rs @@ -0,0 +1,54 @@ +mod system_sc_issue; + +use crate::tx_mock::{TxContext, TxResult}; +use hex_literal::hex; +use multiversx_sc::types::Address; +use system_sc_issue::*; + +/// Address of the system smart contract that manages ESDT. +/// Bech32: erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u +pub const ESDT_SYSTEM_SC_ADDRESS_ARRAY: [u8; 32] = + hex!("000000000000000000010000000000000000000000000000000000000002ffff"); + +pub fn is_system_sc_address(address: &Address) -> bool { + address.as_array() == &ESDT_SYSTEM_SC_ADDRESS_ARRAY +} + +pub fn execute_system_sc(tx_context: TxContext) -> (TxContext, TxResult) { + let func_name = tx_context.tx_input_box.func_name.clone(); + match func_name.as_str() { + "issue" => issue(tx_context), + "issueSemiFungible" => issue_semi_fungible(tx_context), + "issueNonFungible" => issue_non_fungible(tx_context), + "registerMetaESDT" => todo!(), + "changeSFTToMetaESDT" => todo!(), + "registerAndSetAllRoles" => todo!(), + "ESDTBurn" => todo!(), + "mint" => todo!(), + "freeze" => todo!(), + "unFreeze" => todo!(), + "wipe" => todo!(), + "pause" => todo!(), + "unPause" => todo!(), + "freezeSingleNFT" => todo!(), + "unFreezeSingleNFT" => todo!(), + "wipeSingleNFT" => todo!(), + "claim" => todo!(), + "configChange" => todo!(), + "controlChanges" => todo!(), + "transferOwnership" => todo!(), + "getTokenProperties" => todo!(), + "getSpecialRoles" => todo!(), + "setSpecialRole" => todo!(), + "unSetSpecialRole" => todo!(), + "transferNFTCreateRole" => todo!(), + "stopNFTCreate" => todo!(), + "getAllAddressesAndRoles" => todo!(), + "getContractConfig" => todo!(), + "changeToMultiShardCreate" => todo!(), + "setBurnRoleGlobally" => todo!(), + "unsetBurnRoleGlobally" => todo!(), + "sendAllTransferRoleAddresses" => todo!(), + s => panic!("invalid system SC function: {s}"), + } +} diff --git a/vm/src/tx_execution/system_sc/system_sc_issue.rs b/vm/src/tx_execution/system_sc/system_sc_issue.rs new file mode 100644 index 0000000000..ce70c36097 --- /dev/null +++ b/vm/src/tx_execution/system_sc/system_sc_issue.rs @@ -0,0 +1,13 @@ +use crate::tx_mock::{TxContext, TxResult}; + +pub fn issue(_tx_context: TxContext) -> (TxContext, TxResult) { + panic!("System SC issue not implemented") +} + +pub fn issue_semi_fungible(_tx_context: TxContext) -> (TxContext, TxResult) { + panic!("System SC issue_semi_fungible not implemented") +} + +pub fn issue_non_fungible(_tx_context: TxContext) -> (TxContext, TxResult) { + panic!("System SC issue_non_fungible not implemented") +} From b3e3107fa7613e023907d857548774afd19b9b18 Mon Sep 17 00:00:00 2001 From: Ovidiu Stinga Date: Wed, 28 Jun 2023 11:18:27 +0300 Subject: [PATCH 03/17] handle token issue and set special role --- .../scenarios/interactor_nft.scen.json | 10 +-- vm/src/tx_execution/exec_call.rs | 8 +- vm/src/tx_execution/exec_general_tx.rs | 18 ++-- vm/src/tx_execution/mod.rs | 1 + vm/src/tx_execution/system_sc.rs | 4 +- .../tx_execution/system_sc/system_sc_issue.rs | 89 +++++++++++++++++-- .../system_sc/system_sc_special_roles.rs | 23 +++++ vm/src/world_mock/esdt_data.rs | 30 +++++++ 8 files changed, 158 insertions(+), 25 deletions(-) create mode 100644 vm/src/tx_execution/system_sc/system_sc_special_roles.rs diff --git a/contracts/examples/multisig/scenarios/interactor_nft.scen.json b/contracts/examples/multisig/scenarios/interactor_nft.scen.json index 2f091e6549..be0f1ad0ef 100644 --- a/contracts/examples/multisig/scenarios/interactor_nft.scen.json +++ b/contracts/examples/multisig/scenarios/interactor_nft.scen.json @@ -212,7 +212,7 @@ "0x000000000000000000010000000000000000000000000000000000000002ffff", "0x", "0x7365745370656369616c526f6c65", - "0x54455354434f4c4c312d313431366462", + "0x54455354434f4c4c312d343039366266", "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", "0x45534454526f6c654e4654437265617465" ], @@ -287,7 +287,7 @@ "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", "0x", "0x455344544e4654437265617465", - "0x54455354434f4c4c312d313431366462", + "0x54455354434f4c4c312d343039366266", "0x01", "0x5465737420636f6c6c656374696f6e206974656d202330", "0x0bb8", @@ -310,7 +310,7 @@ "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", "0x", "0x455344544e4654437265617465", - "0x54455354434f4c4c312d313431366462", + "0x54455354434f4c4c312d343039366266", "0x01", "0x5465737420636f6c6c656374696f6e206974656d202331", "0x0bb8", @@ -333,7 +333,7 @@ "0x00000000000000000500a89d3e8de032c63fc1328a12d0267f409845bd4fed60", "0x", "0x455344544e4654437265617465", - "0x54455354434f4c4c312d313431366462", + "0x54455354434f4c4c312d343039366266", "0x01", "0x5465737420636f6c6c656374696f6e206974656d202332", "0x0bb8", @@ -514,4 +514,4 @@ } } ] -} +} \ No newline at end of file diff --git a/vm/src/tx_execution/exec_call.rs b/vm/src/tx_execution/exec_call.rs index 041f9f612c..efda838281 100644 --- a/vm/src/tx_execution/exec_call.rs +++ b/vm/src/tx_execution/exec_call.rs @@ -12,9 +12,7 @@ use crate::{ world_mock::{AccountData, AccountEsdt, BlockchainMock}, }; -use super::{ - execute_builtin_function_or_default, execute_tx_context, system_sc::is_system_sc_address, -}; +use super::{execute_builtin_function_or_default, execute_tx_context, is_system_sc_address}; pub fn execute_sc_query(tx_input: TxInput, state: BlockchainMock) -> (TxResult, BlockchainMock) { let state_rc = Rc::new(state); @@ -25,7 +23,9 @@ pub fn execute_sc_query(tx_input: TxInput, state: BlockchainMock) -> (TxResult, } pub fn execute_sc_call(tx_input: TxInput, mut state: BlockchainMock) -> (TxResult, BlockchainMock) { - state.subtract_tx_gas(&tx_input.from, tx_input.gas_limit, tx_input.gas_price); + if !is_system_sc_address(&tx_input.from) { + state.subtract_tx_gas(&tx_input.from, tx_input.gas_limit, tx_input.gas_price); + } let state_rc = Rc::new(state); let tx_cache = TxCache::new(state_rc.clone()); diff --git a/vm/src/tx_execution/exec_general_tx.rs b/vm/src/tx_execution/exec_general_tx.rs index ff8692ca2c..adaf16a011 100644 --- a/vm/src/tx_execution/exec_general_tx.rs +++ b/vm/src/tx_execution/exec_general_tx.rs @@ -5,17 +5,19 @@ use crate::tx_mock::{ BlockchainUpdate, TxCache, TxContext, TxFunctionName, TxInput, TxLog, TxResult, }; -use super::{execute_tx_context, system_sc}; +use super::{execute_system_sc, execute_tx_context, is_system_sc_address}; pub fn default_execution(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { let mut tx_context = TxContext::new(tx_input, tx_cache); - tx_context.tx_cache.subtract_egld_balance( - &tx_context.tx_input_box.from, - &tx_context.tx_input_box.egld_value, - ); + if !is_system_sc_address(&tx_context.tx_input_box.from) { + tx_context.tx_cache.subtract_egld_balance( + &tx_context.tx_input_box.from, + &tx_context.tx_input_box.egld_value, + ); + } - if !system_sc::is_system_sc_address(&tx_context.tx_input_box.to) { + if !is_system_sc_address(&tx_context.tx_input_box.to) { tx_context.tx_cache.increase_egld_balance( &tx_context.tx_input_box.to, &tx_context.tx_input_box.egld_value, @@ -58,8 +60,8 @@ pub fn default_execution(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, Blo { // direct EGLD transfer TxResult::empty() - } else if system_sc::is_system_sc_address(recipient_address) { - let (tx_context_modified, tx_result) = system_sc::execute_system_sc(tx_context); + } else if is_system_sc_address(recipient_address) { + let (tx_context_modified, tx_result) = execute_system_sc(tx_context); tx_context = tx_context_modified; tx_result } else { diff --git a/vm/src/tx_execution/mod.rs b/vm/src/tx_execution/mod.rs index 991df07ee6..cdf3d577a9 100644 --- a/vm/src/tx_execution/mod.rs +++ b/vm/src/tx_execution/mod.rs @@ -10,3 +10,4 @@ pub use exec_call::*; pub use exec_contract_endpoint::*; pub use exec_create::*; pub use exec_general_tx::*; +pub use system_sc::*; diff --git a/vm/src/tx_execution/system_sc.rs b/vm/src/tx_execution/system_sc.rs index afa94c8c30..1189cc17e4 100644 --- a/vm/src/tx_execution/system_sc.rs +++ b/vm/src/tx_execution/system_sc.rs @@ -1,9 +1,11 @@ mod system_sc_issue; +mod system_sc_special_roles; use crate::tx_mock::{TxContext, TxResult}; use hex_literal::hex; use multiversx_sc::types::Address; use system_sc_issue::*; +use system_sc_special_roles::*; /// Address of the system smart contract that manages ESDT. /// Bech32: erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u @@ -39,7 +41,7 @@ pub fn execute_system_sc(tx_context: TxContext) -> (TxContext, TxResult) { "transferOwnership" => todo!(), "getTokenProperties" => todo!(), "getSpecialRoles" => todo!(), - "setSpecialRole" => todo!(), + "setSpecialRole" => set_special_role(tx_context), "unSetSpecialRole" => todo!(), "transferNFTCreateRole" => todo!(), "stopNFTCreate" => todo!(), diff --git a/vm/src/tx_execution/system_sc/system_sc_issue.rs b/vm/src/tx_execution/system_sc/system_sc_issue.rs index ce70c36097..cf6f61cb8d 100644 --- a/vm/src/tx_execution/system_sc/system_sc_issue.rs +++ b/vm/src/tx_execution/system_sc/system_sc_issue.rs @@ -1,13 +1,88 @@ -use crate::tx_mock::{TxContext, TxResult}; +use crate::{ + num_bigint::BigUint, + tx_mock::{TxContext, TxResult}, +}; +use multiversx_sc::codec::TopDecode; -pub fn issue(_tx_context: TxContext) -> (TxContext, TxResult) { - panic!("System SC issue not implemented") +pub fn issue(tx_context: TxContext) -> (TxContext, TxResult) { + let tx_input = tx_context.input_ref(); + let tx_cache = tx_context.blockchain_cache(); + let tx_result: TxResult; + + if tx_input.args.len() < 4 { + tx_result = TxResult::from_vm_error("not enough arguments"); + return (tx_context, tx_result); + } + let _name = tx_input.args[0].clone(); + let _ticker = tx_input.args[1].clone(); + let _total_supply = BigUint::from_bytes_be(tx_input.args[2].clone().as_ref()); + let _decimals = u32::top_decode(tx_input.args[3].clone().as_ref()).unwrap(); + + let token_identifier = b"TESTCOLL1-4096af".to_vec(); + + tx_cache.with_account_mut(&tx_input.from, |account| { + account.esdt.issue_token(&token_identifier); + }); + + tx_result = TxResult { + result_values: vec![token_identifier], + ..Default::default() + }; + + (tx_context, tx_result) } -pub fn issue_semi_fungible(_tx_context: TxContext) -> (TxContext, TxResult) { - panic!("System SC issue_semi_fungible not implemented") +pub fn issue_semi_fungible(tx_context: TxContext) -> (TxContext, TxResult) { + issue_non_fungible(tx_context) } -pub fn issue_non_fungible(_tx_context: TxContext) -> (TxContext, TxResult) { - panic!("System SC issue_non_fungible not implemented") +pub fn issue_non_fungible(tx_context: TxContext) -> (TxContext, TxResult) { + let tx_input = tx_context.input_ref(); + let tx_cache = tx_context.blockchain_cache(); + let tx_result: TxResult; + + if tx_input.args.len() < 2 { + tx_result = TxResult::from_vm_error("not enough arguments"); + return (tx_context, tx_result); + } + let _name = tx_input.args[0].clone(); + let _ticker = tx_input.args[1].clone(); + + let token_identifier = b"TESTCOLL1-4096bf".to_vec(); + + tx_cache.with_account_mut(&tx_input.from, |account| { + account.esdt.issue_token(&token_identifier); + }); + + tx_result = TxResult { + result_values: vec![token_identifier], + ..Default::default() + }; + + (tx_context, tx_result) } + +// todo#1: generate token identifier randomly +// - - - - - - - - - - - - - - - - - - - - - - - - - +// code from mx-chain-go +// - - - - - - - - - - - - - - - - - - - - - - - - - + +// newRandomBase := append(caller, e.eei.BlockChainHook().CurrentRandomSeed()...) +// newRandom := e.hasher.Compute(string(newRandomBase)) +// newRandomForTicker := newRandom[:tickerRandomSequenceLength] + +// tickerPrefix := append(ticker, []byte(tickerSeparator)...) +// newRandomAsBigInt := big.NewInt(0).SetBytes(newRandomForTicker) + +// one := big.NewInt(1) +// for i := 0; i < numOfRetriesForIdentifier; i++ { +// encoded := fmt.Sprintf("%06x", newRandomAsBigInt) +// newIdentifier := append(tickerPrefix, encoded...) +// buff := e.eei.GetStorage(newIdentifier) +// if len(buff) == 0 { +// return newIdentifier, nil +// } +// newRandomAsBigInt.Add(newRandomAsBigInt, one) +// } + +// return nil, vm.ErrCouldNotCreateNewTokenIdentifier diff --git a/vm/src/tx_execution/system_sc/system_sc_special_roles.rs b/vm/src/tx_execution/system_sc/system_sc_special_roles.rs new file mode 100644 index 0000000000..fcf7b26de6 --- /dev/null +++ b/vm/src/tx_execution/system_sc/system_sc_special_roles.rs @@ -0,0 +1,23 @@ +use crate::tx_mock::{TxContext, TxResult}; + +use multiversx_sc::{codec::TopDecode, types::heap::Address}; + +pub fn set_special_role(tx_context: TxContext) -> (TxContext, TxResult) { + let tx_input = tx_context.input_ref(); + let tx_cache = tx_context.blockchain_cache(); + let mut tx_result = TxResult::empty(); + + if tx_input.args.len() < 3 { + tx_result = TxResult::from_vm_error("setSpecialRole too few arguments"); + return (tx_context, tx_result); + } + let token_identifier = tx_input.args[0].clone(); + let address = Address::top_decode(tx_input.args[1].as_slice()).unwrap(); + let role = tx_input.args[2].clone(); + + tx_cache.with_account_mut(&address, |account| { + account.esdt.set_special_role(&token_identifier, &role); + }); + + (tx_context, tx_result) +} diff --git a/vm/src/world_mock/esdt_data.rs b/vm/src/world_mock/esdt_data.rs index 54d932ec49..e03e822c90 100644 --- a/vm/src/world_mock/esdt_data.rs +++ b/vm/src/world_mock/esdt_data.rs @@ -147,6 +147,36 @@ impl AccountEsdt { pub fn iter(&self) -> Iter, EsdtData> { self.0.iter() } + + pub fn issue_token(&mut self, token_identifier: &[u8]) { + let roles = vec![ + "ESDTLocalMint".as_bytes().to_vec(), + "ESDTLocalBurn".as_bytes().to_vec(), + ]; + + self.0.insert( + token_identifier.to_vec(), + EsdtData { + instances: EsdtInstances::new(), + last_nonce: 0, + roles: EsdtRoles::new(roles), + frozen: false, + }, + ); + } + + pub fn set_special_role(&mut self, token_identifier: &[u8], role: &[u8]) { + if let Some(esdt_data) = self.get_mut_by_identifier(token_identifier) { + let roles = esdt_data.roles.get(); + if roles.contains(role.to_vec().as_ref()) { + return; + } else { + let mut new_roles = roles.clone(); + new_roles.push(role.to_vec()); + esdt_data.roles = EsdtRoles::new(new_roles); + } + } + } } impl fmt::Display for EsdtData { From 4e4b28b50b71cecddebe9f6ff564088e152e0c64 Mon Sep 17 00:00:00 2001 From: Ovidiu Stinga Date: Wed, 28 Jun 2023 11:30:11 +0300 Subject: [PATCH 04/17] clippy fixes --- .../tx_execution/system_sc/system_sc_issue.rs | 25 ------------------- vm/src/world_mock/esdt_data.rs | 6 ++--- 2 files changed, 2 insertions(+), 29 deletions(-) diff --git a/vm/src/tx_execution/system_sc/system_sc_issue.rs b/vm/src/tx_execution/system_sc/system_sc_issue.rs index cf6f61cb8d..2cb190d95a 100644 --- a/vm/src/tx_execution/system_sc/system_sc_issue.rs +++ b/vm/src/tx_execution/system_sc/system_sc_issue.rs @@ -61,28 +61,3 @@ pub fn issue_non_fungible(tx_context: TxContext) -> (TxContext, TxResult) { (tx_context, tx_result) } - -// todo#1: generate token identifier randomly -// - - - - - - - - - - - - - - - - - - - - - - - - - -// code from mx-chain-go -// - - - - - - - - - - - - - - - - - - - - - - - - - - -// newRandomBase := append(caller, e.eei.BlockChainHook().CurrentRandomSeed()...) -// newRandom := e.hasher.Compute(string(newRandomBase)) -// newRandomForTicker := newRandom[:tickerRandomSequenceLength] - -// tickerPrefix := append(ticker, []byte(tickerSeparator)...) -// newRandomAsBigInt := big.NewInt(0).SetBytes(newRandomForTicker) - -// one := big.NewInt(1) -// for i := 0; i < numOfRetriesForIdentifier; i++ { -// encoded := fmt.Sprintf("%06x", newRandomAsBigInt) -// newIdentifier := append(tickerPrefix, encoded...) -// buff := e.eei.GetStorage(newIdentifier) -// if len(buff) == 0 { -// return newIdentifier, nil -// } -// newRandomAsBigInt.Add(newRandomAsBigInt, one) -// } - -// return nil, vm.ErrCouldNotCreateNewTokenIdentifier diff --git a/vm/src/world_mock/esdt_data.rs b/vm/src/world_mock/esdt_data.rs index e03e822c90..f4eeb29d50 100644 --- a/vm/src/world_mock/esdt_data.rs +++ b/vm/src/world_mock/esdt_data.rs @@ -168,10 +168,8 @@ impl AccountEsdt { pub fn set_special_role(&mut self, token_identifier: &[u8], role: &[u8]) { if let Some(esdt_data) = self.get_mut_by_identifier(token_identifier) { let roles = esdt_data.roles.get(); - if roles.contains(role.to_vec().as_ref()) { - return; - } else { - let mut new_roles = roles.clone(); + if !roles.contains(role.to_vec().as_ref()) { + let mut new_roles = roles; new_roles.push(role.to_vec()); esdt_data.roles = EsdtRoles::new(new_roles); } From 9b4b2d0a997ba69fbeaa9394651ca3f017da7977 Mon Sep 17 00:00:00 2001 From: Ovidiu Stinga Date: Fri, 30 Jun 2023 10:09:31 +0300 Subject: [PATCH 05/17] fixes after merge, random token identifier generation method --- vm/src/tx_execution/exec_call.rs | 2 +- vm/src/tx_execution/system_sc.rs | 5 +- .../tx_execution/system_sc/system_sc_issue.rs | 95 +++++++++++++++++-- .../system_sc/system_sc_special_roles.rs | 6 +- 4 files changed, 92 insertions(+), 16 deletions(-) diff --git a/vm/src/tx_execution/exec_call.rs b/vm/src/tx_execution/exec_call.rs index 48bb8f3f14..476924560a 100644 --- a/vm/src/tx_execution/exec_call.rs +++ b/vm/src/tx_execution/exec_call.rs @@ -37,7 +37,7 @@ pub fn execute_sc_call(tx_input: TxInput, mut state: BlockchainMock) -> (TxResul (tx_result, state) } -fn existing_account(state: &BlockchainMock, address: &Address) -> bool { +fn existing_account(state: &BlockchainMock, address: &VMAddress) -> bool { state.accounts.contains_key(address) || is_system_sc_address(address) } diff --git a/vm/src/tx_execution/system_sc.rs b/vm/src/tx_execution/system_sc.rs index 1189cc17e4..ab99120c32 100644 --- a/vm/src/tx_execution/system_sc.rs +++ b/vm/src/tx_execution/system_sc.rs @@ -1,9 +1,8 @@ mod system_sc_issue; mod system_sc_special_roles; -use crate::tx_mock::{TxContext, TxResult}; +use crate::{tx_mock::{TxContext, TxResult}, types::VMAddress}; use hex_literal::hex; -use multiversx_sc::types::Address; use system_sc_issue::*; use system_sc_special_roles::*; @@ -12,7 +11,7 @@ use system_sc_special_roles::*; pub const ESDT_SYSTEM_SC_ADDRESS_ARRAY: [u8; 32] = hex!("000000000000000000010000000000000000000000000000000000000002ffff"); -pub fn is_system_sc_address(address: &Address) -> bool { +pub fn is_system_sc_address(address: &VMAddress) -> bool { address.as_array() == &ESDT_SYSTEM_SC_ADDRESS_ARRAY } diff --git a/vm/src/tx_execution/system_sc/system_sc_issue.rs b/vm/src/tx_execution/system_sc/system_sc_issue.rs index 2cb190d95a..48b1bfef87 100644 --- a/vm/src/tx_execution/system_sc/system_sc_issue.rs +++ b/vm/src/tx_execution/system_sc/system_sc_issue.rs @@ -1,8 +1,10 @@ +use num_bigint::BigUint; + use crate::{ - num_bigint::BigUint, + crypto_functions::keccak256, tx_mock::{TxContext, TxResult}, + types::top_decode_u64, }; -use multiversx_sc::codec::TopDecode; pub fn issue(tx_context: TxContext) -> (TxContext, TxResult) { let tx_input = tx_context.input_ref(); @@ -14,11 +16,28 @@ pub fn issue(tx_context: TxContext) -> (TxContext, TxResult) { return (tx_context, tx_result); } let _name = tx_input.args[0].clone(); - let _ticker = tx_input.args[1].clone(); + let ticker = tx_input.args[1].clone(); let _total_supply = BigUint::from_bytes_be(tx_input.args[2].clone().as_ref()); - let _decimals = u32::top_decode(tx_input.args[3].clone().as_ref()).unwrap(); + let _decimals = top_decode_u64(tx_input.args[3].clone().as_ref()) as u32; + + let new_random_base = [ + tx_input.from.as_bytes(), + tx_cache + .blockchain_ref() + .current_block_info + .block_random_seed + .as_slice(), + ] + .concat(); + let new_random = keccak256(&new_random_base); + let new_random_for_ticker = &new_random[..3]; - let token_identifier = b"TESTCOLL1-4096af".to_vec(); + let token_identifier = [ + &ticker, + "-".as_bytes(), + hex::encode(new_random_for_ticker).as_bytes(), + ] + .concat(); tx_cache.with_account_mut(&tx_input.from, |account| { account.esdt.issue_token(&token_identifier); @@ -33,7 +52,46 @@ pub fn issue(tx_context: TxContext) -> (TxContext, TxResult) { } pub fn issue_semi_fungible(tx_context: TxContext) -> (TxContext, TxResult) { - issue_non_fungible(tx_context) + let tx_input = tx_context.input_ref(); + let tx_cache = tx_context.blockchain_cache(); + let tx_result: TxResult; + + if tx_input.args.len() < 2 { + tx_result = TxResult::from_vm_error("not enough arguments"); + return (tx_context, tx_result); + } + let _name = tx_input.args[0].clone(); + let ticker = tx_input.args[1].clone(); + + let new_random_base = [ + tx_input.from.as_bytes(), + tx_cache + .blockchain_ref() + .current_block_info + .block_random_seed + .as_slice(), + ] + .concat(); + let new_random = keccak256(&new_random_base); + let new_random_for_ticker = &new_random[..3]; + + let token_identifier = [ + &ticker, + "-".as_bytes(), + hex::encode(new_random_for_ticker).as_bytes(), + ] + .concat(); + + tx_cache.with_account_mut(&tx_input.from, |account| { + account.esdt.issue_token(&token_identifier); + }); + + tx_result = TxResult { + result_values: vec![token_identifier], + ..Default::default() + }; + + (tx_context, tx_result) } pub fn issue_non_fungible(tx_context: TxContext) -> (TxContext, TxResult) { @@ -46,9 +104,30 @@ pub fn issue_non_fungible(tx_context: TxContext) -> (TxContext, TxResult) { return (tx_context, tx_result); } let _name = tx_input.args[0].clone(); - let _ticker = tx_input.args[1].clone(); + let ticker = tx_input.args[1].clone(); + + let new_random_base = [ + tx_input.from.as_bytes(), + tx_cache + .blockchain_ref() + .current_block_info + .block_random_seed + .as_slice(), + ] + .concat(); + let new_random = keccak256(&new_random_base); + let new_random_for_ticker = &new_random[..3]; - let token_identifier = b"TESTCOLL1-4096bf".to_vec(); + let token_identifier = [ + &ticker, + "-".as_bytes(), + hex::encode(new_random_for_ticker).as_bytes(), + ] + .concat(); + println!( + "\n\ntoken_identifier: {}\n\n", + std::str::from_utf8(&token_identifier).unwrap() + ); tx_cache.with_account_mut(&tx_input.from, |account| { account.esdt.issue_token(&token_identifier); diff --git a/vm/src/tx_execution/system_sc/system_sc_special_roles.rs b/vm/src/tx_execution/system_sc/system_sc_special_roles.rs index fcf7b26de6..f335c7a3e7 100644 --- a/vm/src/tx_execution/system_sc/system_sc_special_roles.rs +++ b/vm/src/tx_execution/system_sc/system_sc_special_roles.rs @@ -1,6 +1,4 @@ -use crate::tx_mock::{TxContext, TxResult}; - -use multiversx_sc::{codec::TopDecode, types::heap::Address}; +use crate::{tx_mock::{TxContext, TxResult}, types::VMAddress}; pub fn set_special_role(tx_context: TxContext) -> (TxContext, TxResult) { let tx_input = tx_context.input_ref(); @@ -12,7 +10,7 @@ pub fn set_special_role(tx_context: TxContext) -> (TxContext, TxResult) { return (tx_context, tx_result); } let token_identifier = tx_input.args[0].clone(); - let address = Address::top_decode(tx_input.args[1].as_slice()).unwrap(); + let address = VMAddress::from_slice(tx_input.args[1].as_slice()); let role = tx_input.args[2].clone(); tx_cache.with_account_mut(&address, |account| { From 21dc513c25e44a578a1fd94ab59fdd27634afc4b Mon Sep 17 00:00:00 2001 From: Ovidiu Stinga Date: Fri, 30 Jun 2023 13:39:44 +0300 Subject: [PATCH 06/17] handle issued token identifiers for blockchain update part 1 --- .../scenario/src/scenario/run_vm/set_state.rs | 3 ++ vm/src/tx_mock/tx_cache.rs | 6 +++- vm/src/world_mock/blockchain_mock.rs | 30 ++++++++++++++----- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/framework/scenario/src/scenario/run_vm/set_state.rs b/framework/scenario/src/scenario/run_vm/set_state.rs index 2f8239793c..cd69dc2893 100644 --- a/framework/scenario/src/scenario/run_vm/set_state.rs +++ b/framework/scenario/src/scenario/run_vm/set_state.rs @@ -76,6 +76,9 @@ fn execute(state: &mut BlockchainMock, set_state_step: &SetStateStep) { new_address.new_address.to_vm_address(), ) } + for new_token_identifier in set_state_step.new_token_identifiers.iter().cloned() { + state.put_new_token_identifier(new_token_identifier) + } if let Some(block_info_obj) = &*set_state_step.previous_block_info { update_block_info(&mut state.previous_block_info, block_info_obj); } diff --git a/vm/src/tx_mock/tx_cache.rs b/vm/src/tx_mock/tx_cache.rs index f32aa7677f..45addf8c8e 100644 --- a/vm/src/tx_mock/tx_cache.rs +++ b/vm/src/tx_mock/tx_cache.rs @@ -1,6 +1,6 @@ use std::{ cell::{Ref, RefCell}, - collections::HashMap, + collections::{HashMap, HashSet}, fmt, rc::Rc, }; @@ -100,6 +100,7 @@ impl TxCache { pub fn into_blockchain_updates(self) -> BlockchainUpdate { BlockchainUpdate { accounts: self.accounts.into_inner(), + issued_token_identifiers: HashSet::new(), } } @@ -112,16 +113,19 @@ impl TxCache { pub struct BlockchainUpdate { accounts: HashMap, + issued_token_identifiers: HashSet, // todo: new issued token hashset } impl BlockchainUpdate { pub fn empty() -> Self { BlockchainUpdate { accounts: HashMap::new(), + issued_token_identifiers: HashSet::new(), } } pub fn apply(self, blockchain: &mut BlockchainMock) { blockchain.update_accounts(self.accounts); + blockchain.clear_issued_token_identifiers(self.issued_token_identifiers); } } diff --git a/vm/src/world_mock/blockchain_mock.rs b/vm/src/world_mock/blockchain_mock.rs index cd2549e288..5a0093893a 100644 --- a/vm/src/world_mock/blockchain_mock.rs +++ b/vm/src/world_mock/blockchain_mock.rs @@ -6,7 +6,11 @@ use crate::{ use multiversx_chain_vm_executor::Executor; use num_bigint::BigUint; use num_traits::Zero; -use std::{collections::HashMap, fmt::Debug, rc::Rc}; +use std::{ + collections::{HashMap, HashSet}, + fmt::Debug, + rc::Rc, +}; use super::{AccountData, BlockInfo, FailingExecutor}; @@ -16,6 +20,7 @@ pub struct BlockchainMock { pub accounts: HashMap, pub builtin_functions: Rc, pub new_addresses: HashMap<(VMAddress, u64), VMAddress>, + pub new_token_identifiers: HashSet, pub previous_block_info: BlockInfo, pub current_block_info: BlockInfo, pub executor: Box, @@ -27,20 +32,25 @@ impl BlockchainMock { accounts: HashMap::new(), builtin_functions: Rc::new(init_builtin_functions()), new_addresses: HashMap::new(), + new_token_identifiers: HashSet::new(), previous_block_info: BlockInfo::new(), current_block_info: BlockInfo::new(), executor, } } -} -impl Default for BlockchainMock { - fn default() -> Self { - Self::new(Box::new(FailingExecutor)) + pub fn put_new_token_identifier(&mut self, token_identifier: String) { + self.new_token_identifiers.insert(token_identifier); + } + + pub fn clear_issued_token_identifiers(&mut self, issued_token_identifiers: HashSet) { + self.new_token_identifiers = self + .new_token_identifiers + .difference(&issued_token_identifiers) + .cloned() + .collect(); } -} -impl BlockchainMock { pub fn account_exists(&self, address: &VMAddress) -> bool { self.accounts.contains_key(address) } @@ -105,6 +115,12 @@ impl BlockchainMock { } } +impl Default for BlockchainMock { + fn default() -> Self { + Self::new(Box::new(FailingExecutor)) + } +} + impl Debug for BlockchainMock { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { f.debug_struct("BlockchainMock") From 9b29ada982e36b8196d98054b381f6ed3cc8cd83 Mon Sep 17 00:00:00 2001 From: Ovidiu Stinga Date: Mon, 3 Jul 2023 18:10:02 +0300 Subject: [PATCH 07/17] handle issued token identifiers for blockchain update part 2 --- .../tx_execution/system_sc/system_sc_issue.rs | 91 +++++++------------ vm/src/tx_mock/tx_cache.rs | 23 ++++- vm/src/world_mock/blockchain_mock.rs | 20 ++-- 3 files changed, 59 insertions(+), 75 deletions(-) diff --git a/vm/src/tx_execution/system_sc/system_sc_issue.rs b/vm/src/tx_execution/system_sc/system_sc_issue.rs index 48b1bfef87..553b4c1054 100644 --- a/vm/src/tx_execution/system_sc/system_sc_issue.rs +++ b/vm/src/tx_execution/system_sc/system_sc_issue.rs @@ -6,6 +6,7 @@ use crate::{ types::top_decode_u64, }; +/// Issues a new token. pub fn issue(tx_context: TxContext) -> (TxContext, TxResult) { let tx_input = tx_context.input_ref(); let tx_cache = tx_context.blockchain_cache(); @@ -20,28 +21,18 @@ pub fn issue(tx_context: TxContext) -> (TxContext, TxResult) { let _total_supply = BigUint::from_bytes_be(tx_input.args[2].clone().as_ref()); let _decimals = top_decode_u64(tx_input.args[3].clone().as_ref()) as u32; - let new_random_base = [ - tx_input.from.as_bytes(), - tx_cache - .blockchain_ref() - .current_block_info - .block_random_seed - .as_slice(), - ] - .concat(); - let new_random = keccak256(&new_random_base); - let new_random_for_ticker = &new_random[..3]; - - let token_identifier = [ - &ticker, - "-".as_bytes(), - hex::encode(new_random_for_ticker).as_bytes(), - ] - .concat(); + let mut new_token_identifiers = tx_cache.get_new_token_identifiers(); + let token_identifier = if let Some(ti) = new_token_identifiers.pop_front() { + println!("\n\ntoken_identifier: {}\n\n", ti); + ti.into_bytes() + } else { + generate_token_identifier_from_ticker(tx_input, tx_cache, &ticker) + }; tx_cache.with_account_mut(&tx_input.from, |account| { account.esdt.issue_token(&token_identifier); }); + tx_cache.set_new_token_identifiers(new_token_identifiers); tx_result = TxResult { result_values: vec![token_identifier], @@ -51,7 +42,13 @@ pub fn issue(tx_context: TxContext) -> (TxContext, TxResult) { (tx_context, tx_result) } +/// Issues a semi-fungible token. pub fn issue_semi_fungible(tx_context: TxContext) -> (TxContext, TxResult) { + issue_non_fungible(tx_context) +} + +/// Issues a non-fungible token. +pub fn issue_non_fungible(tx_context: TxContext) -> (TxContext, TxResult) { let tx_input = tx_context.input_ref(); let tx_cache = tx_context.blockchain_cache(); let tx_result: TxResult; @@ -63,28 +60,18 @@ pub fn issue_semi_fungible(tx_context: TxContext) -> (TxContext, TxResult) { let _name = tx_input.args[0].clone(); let ticker = tx_input.args[1].clone(); - let new_random_base = [ - tx_input.from.as_bytes(), - tx_cache - .blockchain_ref() - .current_block_info - .block_random_seed - .as_slice(), - ] - .concat(); - let new_random = keccak256(&new_random_base); - let new_random_for_ticker = &new_random[..3]; - - let token_identifier = [ - &ticker, - "-".as_bytes(), - hex::encode(new_random_for_ticker).as_bytes(), - ] - .concat(); + let mut new_token_identifiers = tx_cache.get_new_token_identifiers(); + let token_identifier = if let Some(ti) = new_token_identifiers.pop_front() { + println!("\n\ntoken_identifier: {}\n\n", ti); + ti.into_bytes() + } else { + generate_token_identifier_from_ticker(tx_input, tx_cache, &ticker) + }; tx_cache.with_account_mut(&tx_input.from, |account| { account.esdt.issue_token(&token_identifier); }); + tx_cache.set_new_token_identifiers(new_token_identifiers); tx_result = TxResult { result_values: vec![token_identifier], @@ -94,18 +81,11 @@ pub fn issue_semi_fungible(tx_context: TxContext) -> (TxContext, TxResult) { (tx_context, tx_result) } -pub fn issue_non_fungible(tx_context: TxContext) -> (TxContext, TxResult) { - let tx_input = tx_context.input_ref(); - let tx_cache = tx_context.blockchain_cache(); - let tx_result: TxResult; - - if tx_input.args.len() < 2 { - tx_result = TxResult::from_vm_error("not enough arguments"); - return (tx_context, tx_result); - } - let _name = tx_input.args[0].clone(); - let ticker = tx_input.args[1].clone(); - +fn generate_token_identifier_from_ticker( + tx_input: &crate::tx_mock::TxInput, + tx_cache: &crate::tx_mock::TxCache, + ticker: &[u8], +) -> Vec { let new_random_base = [ tx_input.from.as_bytes(), tx_cache @@ -119,24 +99,15 @@ pub fn issue_non_fungible(tx_context: TxContext) -> (TxContext, TxResult) { let new_random_for_ticker = &new_random[..3]; let token_identifier = [ - &ticker, + ticker, "-".as_bytes(), hex::encode(new_random_for_ticker).as_bytes(), ] .concat(); println!( - "\n\ntoken_identifier: {}\n\n", + "\n\ngenerated new token_identifier: {}\n\n", std::str::from_utf8(&token_identifier).unwrap() ); - tx_cache.with_account_mut(&tx_input.from, |account| { - account.esdt.issue_token(&token_identifier); - }); - - tx_result = TxResult { - result_values: vec![token_identifier], - ..Default::default() - }; - - (tx_context, tx_result) + token_identifier } diff --git a/vm/src/tx_mock/tx_cache.rs b/vm/src/tx_mock/tx_cache.rs index 45addf8c8e..c32bfbe106 100644 --- a/vm/src/tx_mock/tx_cache.rs +++ b/vm/src/tx_mock/tx_cache.rs @@ -1,6 +1,6 @@ use std::{ cell::{Ref, RefCell}, - collections::{HashMap, HashSet}, + collections::{HashMap, VecDeque}, fmt, rc::Rc, }; @@ -16,6 +16,7 @@ use super::TxCacheSource; pub struct TxCache { source_ref: Rc, pub(super) accounts: RefCell>, + pub(super) new_token_identifiers: RefCell>>, } impl fmt::Debug for TxCache { @@ -31,6 +32,7 @@ impl TxCache { TxCache { source_ref, accounts: RefCell::new(HashMap::new()), + new_token_identifiers: RefCell::new(None), } } @@ -97,10 +99,18 @@ impl TxCache { }) } + pub fn get_new_token_identifiers(&self) -> VecDeque { + self.blockchain_ref().get_new_token_identifiers() + } + + pub fn set_new_token_identifiers(&self, token_identifiers: VecDeque) { + *self.new_token_identifiers.borrow_mut() = Some(token_identifiers); + } + pub fn into_blockchain_updates(self) -> BlockchainUpdate { BlockchainUpdate { accounts: self.accounts.into_inner(), - issued_token_identifiers: HashSet::new(), + new_token_identifiers: self.new_token_identifiers.into_inner(), } } @@ -113,19 +123,22 @@ impl TxCache { pub struct BlockchainUpdate { accounts: HashMap, - issued_token_identifiers: HashSet, // todo: new issued token hashset + new_token_identifiers: Option>, } impl BlockchainUpdate { pub fn empty() -> Self { BlockchainUpdate { accounts: HashMap::new(), - issued_token_identifiers: HashSet::new(), + new_token_identifiers: None, } } pub fn apply(self, blockchain: &mut BlockchainMock) { blockchain.update_accounts(self.accounts); - blockchain.clear_issued_token_identifiers(self.issued_token_identifiers); + + if let Some(token_identifiers) = self.new_token_identifiers { + blockchain.update_new_token_identifiers(token_identifiers); + } } } diff --git a/vm/src/world_mock/blockchain_mock.rs b/vm/src/world_mock/blockchain_mock.rs index 5a0093893a..d2d6f589f5 100644 --- a/vm/src/world_mock/blockchain_mock.rs +++ b/vm/src/world_mock/blockchain_mock.rs @@ -7,7 +7,7 @@ use multiversx_chain_vm_executor::Executor; use num_bigint::BigUint; use num_traits::Zero; use std::{ - collections::{HashMap, HashSet}, + collections::{HashMap, VecDeque}, fmt::Debug, rc::Rc, }; @@ -20,7 +20,7 @@ pub struct BlockchainMock { pub accounts: HashMap, pub builtin_functions: Rc, pub new_addresses: HashMap<(VMAddress, u64), VMAddress>, - pub new_token_identifiers: HashSet, + pub new_token_identifiers: VecDeque, pub previous_block_info: BlockInfo, pub current_block_info: BlockInfo, pub executor: Box, @@ -32,7 +32,7 @@ impl BlockchainMock { accounts: HashMap::new(), builtin_functions: Rc::new(init_builtin_functions()), new_addresses: HashMap::new(), - new_token_identifiers: HashSet::new(), + new_token_identifiers: VecDeque::new(), previous_block_info: BlockInfo::new(), current_block_info: BlockInfo::new(), executor, @@ -40,15 +40,15 @@ impl BlockchainMock { } pub fn put_new_token_identifier(&mut self, token_identifier: String) { - self.new_token_identifiers.insert(token_identifier); + self.new_token_identifiers.push_back(token_identifier) } - pub fn clear_issued_token_identifiers(&mut self, issued_token_identifiers: HashSet) { - self.new_token_identifiers = self - .new_token_identifiers - .difference(&issued_token_identifiers) - .cloned() - .collect(); + pub fn get_new_token_identifiers(&self) -> VecDeque { + self.new_token_identifiers.clone() + } + + pub fn update_new_token_identifiers(&mut self, token_identifiers: VecDeque) { + self.new_token_identifiers = token_identifiers; } pub fn account_exists(&self, address: &VMAddress) -> bool { From 0fc5ee8783391ff04929dcbc07ec2a9f6e824782 Mon Sep 17 00:00:00 2001 From: Ovidiu Stinga Date: Tue, 4 Jul 2023 14:42:43 +0300 Subject: [PATCH 08/17] remove bech32 crate from vm and group unimplemented fns from system_sc --- vm/Cargo.toml | 1 - vm/src/tx_execution/system_sc.rs | 65 ++++---- .../system_sc/system_sc_unimplemented.rs | 144 ++++++++++++++++++ 3 files changed, 179 insertions(+), 31 deletions(-) create mode 100644 vm/src/tx_execution/system_sc/system_sc_unimplemented.rs diff --git a/vm/Cargo.toml b/vm/Cargo.toml index e53ec121d5..0790b01343 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -26,7 +26,6 @@ rand = "0.8.5" rand_seeder = "0.2.2" ed25519-dalek = "1.0.1" itertools = "0.10.3" -bech32 = "0.9" hex-literal = "0.3.1" bitflags = "1.3.2" diff --git a/vm/src/tx_execution/system_sc.rs b/vm/src/tx_execution/system_sc.rs index ab99120c32..b8e7a4b380 100644 --- a/vm/src/tx_execution/system_sc.rs +++ b/vm/src/tx_execution/system_sc.rs @@ -1,10 +1,15 @@ mod system_sc_issue; mod system_sc_special_roles; +mod system_sc_unimplemented; -use crate::{tx_mock::{TxContext, TxResult}, types::VMAddress}; +use crate::{ + tx_mock::{TxContext, TxResult}, + types::VMAddress, +}; use hex_literal::hex; use system_sc_issue::*; use system_sc_special_roles::*; +use system_sc_unimplemented::*; /// Address of the system smart contract that manages ESDT. /// Bech32: erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqzllls8a5w6u @@ -21,35 +26,35 @@ pub fn execute_system_sc(tx_context: TxContext) -> (TxContext, TxResult) { "issue" => issue(tx_context), "issueSemiFungible" => issue_semi_fungible(tx_context), "issueNonFungible" => issue_non_fungible(tx_context), - "registerMetaESDT" => todo!(), - "changeSFTToMetaESDT" => todo!(), - "registerAndSetAllRoles" => todo!(), - "ESDTBurn" => todo!(), - "mint" => todo!(), - "freeze" => todo!(), - "unFreeze" => todo!(), - "wipe" => todo!(), - "pause" => todo!(), - "unPause" => todo!(), - "freezeSingleNFT" => todo!(), - "unFreezeSingleNFT" => todo!(), - "wipeSingleNFT" => todo!(), - "claim" => todo!(), - "configChange" => todo!(), - "controlChanges" => todo!(), - "transferOwnership" => todo!(), - "getTokenProperties" => todo!(), - "getSpecialRoles" => todo!(), + "registerMetaESDT" => register_meta_esdt(tx_context), + "changeSFTToMetaESDT" => change_sft_to_meta_esdt(tx_context), + "registerAndSetAllRoles" => register_and_set_all_roles(), + "ESDTBurn" => esdt_burn(tx_context), + "mint" => mint(tx_context), + "freeze" => freeze(tx_context), + "unFreeze" => unfreeze(tx_context), + "wipe" => wipe(tx_context), + "pause" => pause(tx_context), + "unPause" => unpause(tx_context), + "freezeSingleNFT" => freeze_single_nft(tx_context), + "unFreezeSingleNFT" => unfreeze_single_nft(tx_context), + "wipeSingleNFT" => wipe_single_nft(tx_context), + "claim" => claim(tx_context), + "configChange" => config_change(tx_context), + "controlChanges" => control_changes(tx_context), + "transferOwnership" => transfer_ownership(tx_context), + "getTokenProperties" => get_token_properties(tx_context), + "getSpecialRoles" => get_special_roles(tx_context), "setSpecialRole" => set_special_role(tx_context), - "unSetSpecialRole" => todo!(), - "transferNFTCreateRole" => todo!(), - "stopNFTCreate" => todo!(), - "getAllAddressesAndRoles" => todo!(), - "getContractConfig" => todo!(), - "changeToMultiShardCreate" => todo!(), - "setBurnRoleGlobally" => todo!(), - "unsetBurnRoleGlobally" => todo!(), - "sendAllTransferRoleAddresses" => todo!(), - s => panic!("invalid system SC function: {s}"), + "unSetSpecialRole" => unset_special_role(tx_context), + "transferNFTCreateRole" => transfer_nft_create_role(tx_context), + "stopNFTCreate" => stop_nft_create(tx_context), + "getAllAddressesAndRoles" => get_all_addresses_and_roles(tx_context), + "getContractConfig" => get_contract_config(tx_context), + "changeToMultiShardCreate" => change_to_multi_shard_create(tx_context), + "setBurnRoleGlobally" => set_burn_role_globally(tx_context), + "unsetBurnRoleGlobally" => unset_burn_role_globally(tx_context), + "sendAllTransferRoleAddresses" => send_all_transfer_role_addresses(tx_context), + invalid_func_name => panic!("invalid system SC function: {invalid_func_name}"), } } diff --git a/vm/src/tx_execution/system_sc/system_sc_unimplemented.rs b/vm/src/tx_execution/system_sc/system_sc_unimplemented.rs new file mode 100644 index 0000000000..9d4d07ce96 --- /dev/null +++ b/vm/src/tx_execution/system_sc/system_sc_unimplemented.rs @@ -0,0 +1,144 @@ +use crate::tx_mock::{TxContext, TxResult}; + +/// Every unimplemented fn will be implemented and moved to its corresponding file. +/// This file will be deleted. + +#[allow(unused_variables)] +pub fn register_meta_esdt(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn change_sft_to_meta_esdt(_tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn register_and_set_all_roles() -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn esdt_burn(_tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn mint(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn freeze(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn unfreeze(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn wipe(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn pause(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn unpause(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn freeze_single_nft(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn unfreeze_single_nft(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn wipe_single_nft(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn claim(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn config_change(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn control_changes(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn transfer_ownership(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn get_token_properties(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn get_special_roles(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn unset_special_role(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn transfer_nft_create_role(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn stop_nft_create(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn get_all_addresses_and_roles(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn get_contract_config(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn change_to_multi_shard_create(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn set_burn_role_globally(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn unset_burn_role_globally(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} + +#[allow(unused_variables)] +pub fn send_all_transfer_role_addresses(tx_context: TxContext) -> (TxContext, TxResult) { + unimplemented!() +} From a30201df5b5ee2e193345e574eed8030c5760936 Mon Sep 17 00:00:00 2001 From: Ovidiu Stinga Date: Tue, 4 Jul 2023 17:29:58 +0300 Subject: [PATCH 09/17] find first token identifier matching ticker and add some tests --- .../tx_execution/system_sc/system_sc_issue.rs | 81 ++++++++++++++++--- vm/src/tx_mock/tx_cache.rs | 10 +-- vm/src/world_mock/blockchain_mock.rs | 16 ++-- 3 files changed, 79 insertions(+), 28 deletions(-) diff --git a/vm/src/tx_execution/system_sc/system_sc_issue.rs b/vm/src/tx_execution/system_sc/system_sc_issue.rs index 553b4c1054..666d44678f 100644 --- a/vm/src/tx_execution/system_sc/system_sc_issue.rs +++ b/vm/src/tx_execution/system_sc/system_sc_issue.rs @@ -2,7 +2,7 @@ use num_bigint::BigUint; use crate::{ crypto_functions::keccak256, - tx_mock::{TxContext, TxResult}, + tx_mock::{TxCache, TxContext, TxInput, TxResult}, types::top_decode_u64, }; @@ -22,13 +22,21 @@ pub fn issue(tx_context: TxContext) -> (TxContext, TxResult) { let _decimals = top_decode_u64(tx_input.args[3].clone().as_ref()) as u32; let mut new_token_identifiers = tx_cache.get_new_token_identifiers(); - let token_identifier = if let Some(ti) = new_token_identifiers.pop_front() { - println!("\n\ntoken_identifier: {}\n\n", ti); + + let token_identifier = if let Some((i, ti)) = + first_token_identifier_with_ticker(&new_token_identifiers, &ticker) + { + new_token_identifiers.remove(i); ti.into_bytes() } else { generate_token_identifier_from_ticker(tx_input, tx_cache, &ticker) }; + println!( + "\n\ngenerated new token_identifier: {}\n\n", + std::str::from_utf8(&token_identifier).unwrap() + ); + tx_cache.with_account_mut(&tx_input.from, |account| { account.esdt.issue_token(&token_identifier); }); @@ -42,12 +50,12 @@ pub fn issue(tx_context: TxContext) -> (TxContext, TxResult) { (tx_context, tx_result) } -/// Issues a semi-fungible token. +/// Issues a new semi-fungible token. pub fn issue_semi_fungible(tx_context: TxContext) -> (TxContext, TxResult) { issue_non_fungible(tx_context) } -/// Issues a non-fungible token. +/// Issues a new non-fungible token. pub fn issue_non_fungible(tx_context: TxContext) -> (TxContext, TxResult) { let tx_input = tx_context.input_ref(); let tx_cache = tx_context.blockchain_cache(); @@ -61,8 +69,11 @@ pub fn issue_non_fungible(tx_context: TxContext) -> (TxContext, TxResult) { let ticker = tx_input.args[1].clone(); let mut new_token_identifiers = tx_cache.get_new_token_identifiers(); - let token_identifier = if let Some(ti) = new_token_identifiers.pop_front() { - println!("\n\ntoken_identifier: {}\n\n", ti); + + let token_identifier = if let Some((i, ti)) = + first_token_identifier_with_ticker(&new_token_identifiers, &ticker) + { + new_token_identifiers.remove(i); ti.into_bytes() } else { generate_token_identifier_from_ticker(tx_input, tx_cache, &ticker) @@ -81,9 +92,27 @@ pub fn issue_non_fungible(tx_context: TxContext) -> (TxContext, TxResult) { (tx_context, tx_result) } +fn first_token_identifier_with_ticker( + token_identifiers: &Vec, + ticker: &[u8], +) -> Option<(usize, String)> { + let extract_ticker = + |ti: &String| -> String { ti.split("-").map(|x| x.to_string()).next().unwrap() }; + + let position = token_identifiers + .iter() + .position(|x| extract_ticker(x).as_bytes() == ticker); + + if let Some(i) = position { + Some((i, token_identifiers[i].clone())) + } else { + None + } +} + fn generate_token_identifier_from_ticker( - tx_input: &crate::tx_mock::TxInput, - tx_cache: &crate::tx_mock::TxCache, + tx_input: &TxInput, + tx_cache: &TxCache, ticker: &[u8], ) -> Vec { let new_random_base = [ @@ -104,10 +133,36 @@ fn generate_token_identifier_from_ticker( hex::encode(new_random_for_ticker).as_bytes(), ] .concat(); - println!( - "\n\ngenerated new token_identifier: {}\n\n", - std::str::from_utf8(&token_identifier).unwrap() - ); token_identifier } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_first_token_identifier_with_ticker_ok() { + let ticker = String::from("BBBB").into_bytes(); + let new_token_indetifiers = vec![ + "AAAA-0123".to_string(), + "BBBB-4567".to_string(), + "BBBB-0123".to_string(), + "CCCC-4567".to_string(), + ]; + + let ti = first_token_identifier_with_ticker(&new_token_indetifiers, &ticker); + let expected = b"BBBB-4567".as_slice(); + assert_eq!(expected, ti.unwrap().1.into_bytes()); + } + + #[test] + fn test_first_token_identifier_with_ticker_is_none() { + let ticker = String::from("BBBB").into_bytes(); + let new_token_indetifiers = vec!["AAAA-0123".to_string()]; + + let i = first_token_identifier_with_ticker(&new_token_indetifiers, &ticker); + let expected = None; + assert_eq!(expected, i); + } +} diff --git a/vm/src/tx_mock/tx_cache.rs b/vm/src/tx_mock/tx_cache.rs index c32bfbe106..c188a78e33 100644 --- a/vm/src/tx_mock/tx_cache.rs +++ b/vm/src/tx_mock/tx_cache.rs @@ -1,6 +1,6 @@ use std::{ cell::{Ref, RefCell}, - collections::{HashMap, VecDeque}, + collections::HashMap, fmt, rc::Rc, }; @@ -16,7 +16,7 @@ use super::TxCacheSource; pub struct TxCache { source_ref: Rc, pub(super) accounts: RefCell>, - pub(super) new_token_identifiers: RefCell>>, + pub(super) new_token_identifiers: RefCell>>, } impl fmt::Debug for TxCache { @@ -99,11 +99,11 @@ impl TxCache { }) } - pub fn get_new_token_identifiers(&self) -> VecDeque { + pub fn get_new_token_identifiers(&self) -> Vec { self.blockchain_ref().get_new_token_identifiers() } - pub fn set_new_token_identifiers(&self, token_identifiers: VecDeque) { + pub fn set_new_token_identifiers(&self, token_identifiers: Vec) { *self.new_token_identifiers.borrow_mut() = Some(token_identifiers); } @@ -123,7 +123,7 @@ impl TxCache { pub struct BlockchainUpdate { accounts: HashMap, - new_token_identifiers: Option>, + new_token_identifiers: Option>, } impl BlockchainUpdate { diff --git a/vm/src/world_mock/blockchain_mock.rs b/vm/src/world_mock/blockchain_mock.rs index d2d6f589f5..b6288953ac 100644 --- a/vm/src/world_mock/blockchain_mock.rs +++ b/vm/src/world_mock/blockchain_mock.rs @@ -6,11 +6,7 @@ use crate::{ use multiversx_chain_vm_executor::Executor; use num_bigint::BigUint; use num_traits::Zero; -use std::{ - collections::{HashMap, VecDeque}, - fmt::Debug, - rc::Rc, -}; +use std::{collections::HashMap, fmt::Debug, rc::Rc}; use super::{AccountData, BlockInfo, FailingExecutor}; @@ -20,7 +16,7 @@ pub struct BlockchainMock { pub accounts: HashMap, pub builtin_functions: Rc, pub new_addresses: HashMap<(VMAddress, u64), VMAddress>, - pub new_token_identifiers: VecDeque, + pub new_token_identifiers: Vec, pub previous_block_info: BlockInfo, pub current_block_info: BlockInfo, pub executor: Box, @@ -32,7 +28,7 @@ impl BlockchainMock { accounts: HashMap::new(), builtin_functions: Rc::new(init_builtin_functions()), new_addresses: HashMap::new(), - new_token_identifiers: VecDeque::new(), + new_token_identifiers: Vec::new(), previous_block_info: BlockInfo::new(), current_block_info: BlockInfo::new(), executor, @@ -40,14 +36,14 @@ impl BlockchainMock { } pub fn put_new_token_identifier(&mut self, token_identifier: String) { - self.new_token_identifiers.push_back(token_identifier) + self.new_token_identifiers.push(token_identifier) } - pub fn get_new_token_identifiers(&self) -> VecDeque { + pub fn get_new_token_identifiers(&self) -> Vec { self.new_token_identifiers.clone() } - pub fn update_new_token_identifiers(&mut self, token_identifiers: VecDeque) { + pub fn update_new_token_identifiers(&mut self, token_identifiers: Vec) { self.new_token_identifiers = token_identifiers; } From 27e617c68ec7bc3ac258008ffcbeadd46e467f5b Mon Sep 17 00:00:00 2001 From: Ovidiu Stinga Date: Tue, 4 Jul 2023 17:36:00 +0300 Subject: [PATCH 10/17] fix clippy issues --- vm/src/tx_execution/system_sc/system_sc_issue.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/vm/src/tx_execution/system_sc/system_sc_issue.rs b/vm/src/tx_execution/system_sc/system_sc_issue.rs index 666d44678f..cb9c0fcea9 100644 --- a/vm/src/tx_execution/system_sc/system_sc_issue.rs +++ b/vm/src/tx_execution/system_sc/system_sc_issue.rs @@ -93,21 +93,16 @@ pub fn issue_non_fungible(tx_context: TxContext) -> (TxContext, TxResult) { } fn first_token_identifier_with_ticker( - token_identifiers: &Vec, + token_identifiers: &[String], ticker: &[u8], ) -> Option<(usize, String)> { let extract_ticker = - |ti: &String| -> String { ti.split("-").map(|x| x.to_string()).next().unwrap() }; + |ti: &String| -> String { ti.split('-').map(|x| x.to_string()).next().unwrap() }; - let position = token_identifiers + token_identifiers .iter() - .position(|x| extract_ticker(x).as_bytes() == ticker); - - if let Some(i) = position { - Some((i, token_identifiers[i].clone())) - } else { - None - } + .position(|x| extract_ticker(x).as_bytes() == ticker) + .map(|i| (i, token_identifiers[i].clone())) } fn generate_token_identifier_from_ticker( From 2e95bd35d3f97acdfba7e43035494db19f1c1549 Mon Sep 17 00:00:00 2001 From: Ovidiu Stinga Date: Wed, 5 Jul 2023 13:06:31 +0300 Subject: [PATCH 11/17] add cli for issue nft with all roles and new trace for scenarios --- .../interact-rs/src/multisig_interact.rs | 8 + .../interact-rs/src/multisig_interact_cli.rs | 10 + .../interact-rs/src/multisig_interact_nfts.rs | 89 +++ .../interactor_nft_all_roles.scen.json | 526 ++++++++++++++++++ .../tests/multisig_scenario_go_test.rs | 37 +- .../tests/multisig_scenario_rs_test.rs | 37 +- 6 files changed, 675 insertions(+), 32 deletions(-) create mode 100644 contracts/examples/multisig/scenarios/interactor_nft_all_roles.scen.json diff --git a/contracts/examples/multisig/interact-rs/src/multisig_interact.rs b/contracts/examples/multisig/interact-rs/src/multisig_interact.rs index 289ac3ade2..f5e79795f0 100644 --- a/contracts/examples/multisig/interact-rs/src/multisig_interact.rs +++ b/contracts/examples/multisig/interact-rs/src/multisig_interact.rs @@ -58,9 +58,17 @@ async fn main() { Some(multisig_interact_cli::InteractCliCommand::MultiDeploy(args)) => { multisig_interact.multi_deploy(&args.count).await; }, + Some(multisig_interact_cli::InteractCliCommand::NftFullAllRoles) => { + multisig_interact + .issue_multisig_and_collection_with_all_roles_full() + .await; + }, Some(multisig_interact_cli::InteractCliCommand::NftFull) => { multisig_interact.issue_multisig_and_collection_full().await; }, + Some(multisig_interact_cli::InteractCliCommand::NftIssueAllRoles) => { + multisig_interact.issue_collection_with_all_roles().await; + }, Some(multisig_interact_cli::InteractCliCommand::NftIssue) => { multisig_interact.issue_collection().await; }, diff --git a/contracts/examples/multisig/interact-rs/src/multisig_interact_cli.rs b/contracts/examples/multisig/interact-rs/src/multisig_interact_cli.rs index e2a67a199a..49c7f21d82 100644 --- a/contracts/examples/multisig/interact-rs/src/multisig_interact_cli.rs +++ b/contracts/examples/multisig/interact-rs/src/multisig_interact_cli.rs @@ -22,8 +22,18 @@ pub enum InteractCliCommand { Feed, #[command(name = "multi-deploy", about = "Multiple deploy contracts")] MultiDeploy(MultiDeployArgs), + #[command( + name = "nft-full-all-roles", + about = "Issue multisig and collection with all roles" + )] + NftFullAllRoles, #[command(name = "nft-full", about = "Issue multisig and collection")] NftFull, + #[command( + name = "nft-issue-all-roles", + about = "Issue collection with all roles" + )] + NftIssueAllRoles, #[command(name = "nft-issue", about = "Issue collection")] NftIssue, #[command(name = "nft-items", about = "Create items")] diff --git a/contracts/examples/multisig/interact-rs/src/multisig_interact_nfts.rs b/contracts/examples/multisig/interact-rs/src/multisig_interact_nfts.rs index df7ab0c1ad..ebb6c4f85f 100644 --- a/contracts/examples/multisig/interact-rs/src/multisig_interact_nfts.rs +++ b/contracts/examples/multisig/interact-rs/src/multisig_interact_nfts.rs @@ -8,6 +8,7 @@ const ISSUE_COST: u64 = 50000000000000000; // 0.05 EGLD const COLLECTION_NAME: &str = "TestCollection1"; const COLLECTION_TICKER: &str = "TESTCOLL1"; +const TOKEN_TYPE: &str = "NFT"; const NUM_ITEMS: usize = 3; const ROYALTIES: usize = 3000; @@ -23,6 +24,94 @@ impl MultisigInteract { self.create_items().await; } + pub async fn issue_multisig_and_collection_with_all_roles_full(&mut self) { + self.deploy().await; + self.feed_contract_egld().await; + self.issue_collection_with_all_roles().await; + self.interactor.sleep(Duration::from_secs(15)).await; + self.create_items().await; + } + + pub async fn propose_issue_collection_with_all_roles(&mut self) -> Option { + let system_sc_address = bech32::decode(SYSTEM_SC_BECH32); + let mut typed_sc_call = self + .state + .multisig() + .propose_async_call( + system_sc_address, + ISSUE_COST, + "registerAndSetAllRoles".to_string(), + MultiValueVec::from([ + COLLECTION_NAME.as_bytes(), + COLLECTION_TICKER.as_bytes(), + TOKEN_TYPE.as_bytes(), + top_encode_to_vec_u8_or_panic(&0u32).as_slice(), + ]), + ) + .into_blockchain_call() + .from(&self.wallet_address) + .gas_limit("10,000,000") + .expect(TxExpect::ok()); + + self.interactor.sc_call(&mut typed_sc_call).await; + + let result = typed_sc_call.result(); + if result.is_err() { + println!( + "propose issue collection with roles failed with: {}", + result.err().unwrap() + ); + return None; + } + + let action_id = result.unwrap(); + println!("successfully proposed issue colllection with roles action `{action_id}`"); + Some(action_id) + } + + pub async fn issue_collection_with_all_roles(&mut self) { + println!("proposing issue collection with all roles..."); + let action_id = self.propose_issue_collection_with_all_roles().await; + if action_id.is_none() { + return; + } + + let action_id = action_id.unwrap(); + println!("perfoming issue collection with all roles action `{action_id}`..."); + + if !self.quorum_reached(action_id).await && !self.sign(action_id).await { + return; + } + println!("quorum reached for action `{action_id}`"); + + let mut typed_sc_call = self + .state + .multisig() + .perform_action_endpoint(action_id) + .into_blockchain_call() + .from(&self.wallet_address) + .gas_limit("80,000,000"); + + self.interactor.sc_call(&mut typed_sc_call).await; + + let result = typed_sc_call + .response() + .issue_non_fungible_new_token_identifier(); + if result.is_err() { + println!( + "perform issue collection with all roles failed with: {}", + result.err().unwrap() + ); + return; + } + + self.collection_token_identifier = result.unwrap(); + println!( + "collection token identifier: {}", + self.collection_token_identifier + ); + } + pub async fn propose_issue_collection(&mut self) -> Option { let system_sc_address = bech32::decode(SYSTEM_SC_BECH32); let mut typed_sc_call = self diff --git a/contracts/examples/multisig/scenarios/interactor_nft_all_roles.scen.json b/contracts/examples/multisig/scenarios/interactor_nft_all_roles.scen.json new file mode 100644 index 0000000000..8746f7fba9 --- /dev/null +++ b/contracts/examples/multisig/scenarios/interactor_nft_all_roles.scen.json @@ -0,0 +1,526 @@ +{ + "steps": [ + { + "step": "setState", + "accounts": { + "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60": { + "nonce": "619", + "balance": "101110120254850000003", + "esdt": { + "str:CAN-14dc0a": "1000", + "str:CAN-2abf4b": "1000", + "str:CAN-6d39e6": "1000", + "str:CAN-ac1592": "1000" + }, + "username": "" + } + } + }, + { + "step": "setState", + "accounts": { + "0xb2a11555ce521e4944e09ab17549d85b487dcd26c84b5017a39e31a3670889ba": { + "nonce": "251", + "balance": "101558004165010000000", + "esdt": { + "str:CAMP-3bd1c7": { + "instances": [ + { + "nonce": "26", + "balance": "1" + }, + { + "nonce": "23", + "balance": "1" + }, + { + "nonce": "21", + "balance": "1" + }, + { + "nonce": "25", + "balance": "1" + }, + { + "nonce": "24", + "balance": "1" + }, + { + "nonce": "22", + "balance": "1" + } + ] + }, + "str:TEST-0f54a2": { + "instances": [ + { + "nonce": "9", + "balance": "1" + }, + { + "nonce": "1", + "balance": "1" + }, + { + "nonce": "2", + "balance": "1" + }, + { + "nonce": "6", + "balance": "1" + } + ] + }, + "str:TEST-48ef66": { + "instances": [ + { + "nonce": "5", + "balance": "1" + }, + { + "nonce": "4", + "balance": "1" + } + ] + }, + "str:TEST-d2b50f": { + "instances": [ + { + "nonce": "1", + "balance": "2" + } + ] + }, + "str:TEST-fb415a": { + "instances": [ + { + "nonce": "3", + "balance": "1" + }, + { + "nonce": "2", + "balance": "1" + }, + { + "nonce": "4", + "balance": "1" + } + ] + } + }, + "username": "" + } + } + }, + { + "step": "setState", + "accounts": { + "0xb13a017423c366caff8cecfb77a12610a130f4888134122c7937feae0d6d7d17": { + "nonce": "222", + "balance": "1767500200900000000", + "username": "" + } + } + }, + { + "step": "setState", + "accounts": { + "0x3af8d9c9423b2577c6252722c1d90212a4111f7203f9744f76fcfa1d0a310033": { + "nonce": "434", + "balance": "62227641093810000000", + "esdt": { + "str:USDC-091bd3": "9997000000000", + "str:XRF-079f0d": "999999805000000000000000000", + "str:XUSDC-929b9b": "1000000000000000000000000" + }, + "username": "" + } + } + }, + { + "step": "setState", + "accounts": { + "0x00000000000000000500c114ee7698050a4d40c092add8c31d25e99a6e1ec2ee": { + "nonce": "0", + "balance": "175064792195269569513", + "esdt": { + "str:WEGLD-6cf38e": { + "instances": [], + "roles": [ + "ESDTRoleLocalBurn", + "ESDTRoleLocalMint" + ] + } + }, + "username": "", + "storage": { + "0x7772617070656445676c64546f6b656e4964": "0x5745474c442d366366333865" + }, + "code": "0x0061736d0100000001661160000060017f0060027f7f017f60027f7f006000017f60017f017f60037f7f7f0060037f7f7f017f60057f7f7e7f7f017f60047f7f7f7f0060067e7f7f7f7f7f017f60047f7f7f7f017f60027f7e0060017e006000017e60057f7f7f7f7f0060037e7f7f0002cc062303656e760b7369676e616c4572726f72000303656e7618626967496e7447657445787465726e616c42616c616e6365000303656e760a6d4275666665724e6577000403656e760d6d427566666572417070656e64000203656e76096d4275666665724571000203656e76106d616e61676564534341646472657373000103656e761b6d616e61676564457865637574654f6e44657374436f6e74657874000a03656e760f636c65616e52657475726e44617461000003656e760d6d616e6167656443616c6c6572000103656e76136d616e616765644f776e657241646472657373000103656e76126d427566666572476574417267756d656e74000203656e760f6765744e756d417267756d656e7473000403656e76126d427566666572417070656e644279746573000703656e760f6d4275666665725365744279746573000703656e76106d4275666665724765744c656e677468000503656e76196d42756666657246726f6d426967496e74556e7369676e6564000203656e76136d42756666657247657442797465536c696365000b03656e76126d42756666657253746f726167654c6f6164000203656e76126d616e616765645369676e616c4572726f72000103656e760e626967496e74536574496e743634000c03656e760a626967496e745369676e000503656e76136d42756666657253746f7261676553746f7265000203656e760e636865636b4e6f5061796d656e74000003656e7614626967496e7446696e697368556e7369676e6564000103656e760d6d42756666657246696e697368000503656e7614736d616c6c496e7446696e6973685369676e6564000d03656e761c6d616e616765644765744d756c74694553445443616c6c56616c7565000103656e7609626967496e74436d70000203656e760a6765744761734c656674000e03656e761b6d616e616765645472616e7366657256616c756545786563757465000803656e76136765744e756d455344545472616e7366657273000403656e7612626967496e7447657443616c6c56616c7565000103656e7609626967496e74416464000603656e76226d616e616765644d756c74695472616e73666572455344544e465445786563757465000803656e760f6d42756666657247657442797465730002032c2b0f00040405020310010400010303040503020301050509030306060900040501000000000000000000000005030100110619037f01418080c0000b7f0041e5d1c0000b7f0041f0d1c0000b079b010c066d656d6f7279020004696e69740044146765744c6f636b656445676c6442616c616e63650045156765745772617070656445676c64546f6b656e496400460869735061757365640047057061757365004807756e706175736500490a756e7772617045676c64004a087772617045676c64004b0863616c6c4261636b004c0a5f5f646174615f656e6403010b5f5f686561705f6261736503020abc162b2e000240200120024d0440200220044d0d011024000b1024000b2000200220016b3602042000200120036a3602000b05001043000b2301027f10262200100520001026210041c5d1c00010221a41c5d1c0002000100120000b1b01017f41a483c00041a483c00028020041016b220036020020000b0f01017f10022201200010031a20010b0b0020002001100441004a0b0900200020011000000b1f01017f4167100510262203102b20004167200320012002102610061a10070b08002000420010130b0c01017f10262200100820000b1e01017f1026220010092000102c102804400f0b41f082c00041241000000b1500100b20004604400f0b41cb81c00041191000000b4701017f230041106b2202240020022001410874418080fc077120014118747220014108764180fe03712001411876727236020c20002002410c6a4104100c1a200241106a24000b0d0010311a200020011032102f0b1401017f1026220041d482c0004100100d1a20000b0f01017f102622012000100f1a20010b0d0010311a200020011027102f0b1101017f1026220220002001100d1a20020b09002000200110031a0b5501017f20002d00042101200041003a000402402001410171044041a883c00028020022014191ce004f0d01200028020041ac83c0002001100c1a41a883c000410036020041bcd1c00041003a00000b0f0b1024000bae0102027f017e230041106b2202240020024200370308200010382200100e220141094904402002200241086a41082001103920004100200228020422002002280200220110101a027f41002000450d001a034020000440200041016b210020013100002003420886842103200141016a21010c010b0b02402003420158044041002003a741016b0d021a0c010b41f281c0004112103a000b41010b200241106a24000f0b41e481c000410e103a000b0d0020001026220010111a20000b3b01017f230041106b22042400200441086a41002003200120021023200428020c21012000200428020836020020002001360204200441106a24000b1b01017f418482c00041161034220220002001100c1a20021012000bf20101047f230041106b2203240020032000100e22024118742002410874418080fc07717220024108764180fe03712002411876727236020c20012003410c6a4104103c20012d00042102200141003a00040240024002402002410171220204402000100e22054190ce0041a883c00028020022046b4b0d0220032004200420056a2204103d200041002003280204200328020010101a41a883c00020043602000c010b2001280200200010350b200120023a00040c010b2001103620012802002000103520012d0004200120023a0004410171450d0041a883c000410036020041bcd1c00041003a00000b200341106a24000b800101027f230041106b220324000240024020002d000404404190ce0041a883c00028020022046b2002490d01200341086a2004200220046a2200103d2003280208200328020c20012002103e41a883c00020003602000c020b200028020020012002100c1a0c010b20001036200028020020012002100c1a0b200341106a24000b4001017f230041106b22032400200341086a2001200241ac83c0004190ce001023200328020c21012000200328020836020020002001360204200341106a24000bb00201067f2001200346044020012203410f4b04402000410020006b41037122056a210420050440200221010340200020012d00003a0000200141016a2101200041016a22002004490d000b0b2004200320056b2203417c7122066a21000240200220056a220541037122010440200641004c0d012005417c71220741046a21024100200141037422086b4118712109200728020021010340200420012008762002280200220120097472360200200241046a2102200441046a22042000490d000b0c010b200641004c0d0020052102034020042002280200360200200241046a2102200441046a22042000490d000b0b20034103712103200520066a21020b20030440200020036a21010340200020022d00003a0000200241016a2102200041016a22002001490d000b0b0f0b1043000b1c0041be82c0004113103410374504400f0b419a82c00041121029000b0b0041ac82c000411210340b09002000101441004a0ba60101037f230041106b2201240041be82c00041131034200142808080808080808001420020001b3703080240200045044041d482c00021000c010b4100210003400240024020004108470440200141086a20006a2d00002202450d022002411874411f7520006a220041094f0d01410820006b21022000200141086a6a21000c040b1043000b104d000b200041016a21000c000b000b20002002103410151a200141106a24000b0500104d000b1a01017f10164101102e410010262200100a1a1040200010151a0b0c0010164100102e102510170b0f0010164100102e1040103810181a0b160010164100102e41be82c000411310341037ad10190b0e001016102d4100102e410110420b0e001016102d4100102e410010420bed0302097f017e230041106b220124004100102e103f416b2102024041c4d1c0002d000022000440416b41ffffffff0720001b21020c010b41c4d1c00041013a0000416b101a0b02402002100e4170714110460440410021002002100e2106200141086a2107410121050340200041106a220420064b0d022007420037030020014200370300200220004110200110101a2005044020012902042209423886200942288642808080808080c0ff0083842009421886428080808080e03f8320094208864280808080f01f838484200942088842808080f80f832009421888428080fc07838420094228884280fe038320094238888484842109200128020022004118742000410874418080fc07717220004108764180fe0371200041187672722108200128020c22004118742000410874418080fc07717220004108764180fe037120004118767272210341002105200421000c010b0b1043000b418481c00041221000000b024002400240200950044020081040103822041028450d0120031041450d0220031025101b41004a0d031031220020041033200020031030101c41ea80c000410d10342000102a102c2003420010311031101d1a200141106a24000f0b41d482c000411c1000000b418080c00041101029000b419080c000411c1029000b41ac80c00041231029000beb0301097f230041206b220124000240101e4504404100102e103f41752103024041c0d1c0002d000022000440417541ffffffff0720001b21030c010b41c0d1c00041013a00004175101f0b20031041450d011040103821041031220020041033200020031030101c41f780c000410d10342000102a102c10312107103121081031210520041027210210262200102b20002000200310202001420037020c20012002410874418080fc077120024118747220024108764180fe03712002411876727236020820012000410874418080fc077120004118747220004108764180fe0371200041187672723602142005200141086a4110100c1a200542002007200810211a2001027f41bcd1c0002d0000220045044041bcd1c00041013a000041a883c0004100360200200141ac83c0004190ce00410010392001280200200128020441d482c0004100103e10310c010b41d482c000410010340b360218200120004101733a001c2004200141186a2200103b200142003703082000200141086a22024108103c200310322000103b20012802182100200120012d001c3a000c2001200036020820021036200128020820012d000c044041a883c000410036020041bcd1c00041003a00000b10181a200141206a24000f0b41a681c00041251000000b41cf80c000411b1029000b0300010b0c00419483c000410e1000000b0bb8030200418080c0000ba20357726f6e67206573647420746f6b656e4d75737420706179206d6f7265207468616e203020746f6b656e7321436f6e747261637420646f6573206e6f74206861766520656e6f7567682066756e64735061796d656e74206d757374206265206d6f7265207468616e2030455344544c6f63616c4275726e455344544c6f63616c4d696e74696e636f7272656374206e756d626572206f662045534454207472616e736665727366756e6374696f6e20646f6573206e6f74206163636570742045534454207061796d656e7477726f6e67206e756d626572206f6620617267756d656e7473696e70757420746f6f206c6f6e67696e707574206f7574206f662072616e676573746f72616765206465636f6465206572726f723a20436f6e7472616374206973207061757365647772617070656445676c64546f6b656e496470617573655f6d6f64756c653a70617573656400000066756e6769626c65204553445420746f6b656e206578706563746564456e64706f696e742063616e206f6e6c792062652063616c6c6564206279206f776e657270616e6963206f636375727265640041a483c0000b049cffffff" + } + } + }, + { + "step": "setState", + "newAddresses": [ + { + "creatorAddress": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "creatorNonce": "619", + "newAddress": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60" + } + ] + }, + { + "step": "scDeploy", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "contractCode": "file:../output/multisig.wasm", + "arguments": [ + "0x02", + "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "0xb2a11555ce521e4944e09ab17549d85b487dcd26c84b5017a39e31a3670889ba", + "0xb13a017423c366caff8cecfb77a12610a130f4888134122c7937feae0d6d7d17", + "0x3af8d9c9423b2577c6252722c1d90212a4111f7203f9744f76fcfa1d0a310033" + ], + "gasLimit": "70,000,000", + "gasPrice": "" + }, + "expect": { + "status": "0" + } + }, + { + "step": "transfer", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "egldValue": "0,050000000000000000", + "gasLimit": "50,000" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "proposeAsyncCall", + "arguments": [ + "0x000000000000000000010000000000000000000000000000000000000002ffff", + "0xb1a2bc2ec50000", + "0x7265676973746572416e64536574416c6c526f6c6573", + "0x54657374436f6c6c656374696f6e31", + "0x54455354434f4c4c31", + "0x4e4654", + "0x" + ], + "gasLimit": "10,000,000", + "gasPrice": "" + }, + "expect": { + "status": "0" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xb2a11555ce521e4944e09ab17549d85b487dcd26c84b5017a39e31a3670889ba", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "sign", + "arguments": [ + "0x01" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xb13a017423c366caff8cecfb77a12610a130f4888134122c7937feae0d6d7d17", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "sign", + "arguments": [ + "0x01" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0x3af8d9c9423b2577c6252722c1d90212a4111f7203f9744f76fcfa1d0a310033", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "sign", + "arguments": [ + "0x01" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "setState", + "newTokenIdentifiers": [ + "TESTCOLL1-636884" + ] + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "performAction", + "arguments": [ + "0x01" + ], + "gasLimit": "80,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "proposeAsyncCall", + "arguments": [ + "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "0x", + "0x455344544e4654437265617465", + "0x54455354434f4c4c312d363336383834", + "0x01", + "0x5465737420636f6c6c656374696f6e206974656d202330", + "0x0bb8", + "0x", + "0x746167733a746573742c727573742d696e7465726163746f72", + "0x68747470733a2f2f697066732e696f2f697066732f516d5979416145663170684a53356d4e3677666f7535646535476270556464427854593156656b4b636a643550432f6e667430302e706e67" + ], + "gasLimit": "10,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "proposeAsyncCall", + "arguments": [ + "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "0x", + "0x455344544e4654437265617465", + "0x54455354434f4c4c312d363336383834", + "0x01", + "0x5465737420636f6c6c656374696f6e206974656d202331", + "0x0bb8", + "0x", + "0x746167733a746573742c727573742d696e7465726163746f72", + "0x68747470733a2f2f697066732e696f2f697066732f516d5979416145663170684a53356d4e3677666f7535646535476270556464427854593156656b4b636a643550432f6e667430312e706e67" + ], + "gasLimit": "10,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "proposeAsyncCall", + "arguments": [ + "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "0x", + "0x455344544e4654437265617465", + "0x54455354434f4c4c312d363336383834", + "0x01", + "0x5465737420636f6c6c656374696f6e206974656d202332", + "0x0bb8", + "0x", + "0x746167733a746573742c727573742d696e7465726163746f72", + "0x68747470733a2f2f697066732e696f2f697066732f516d5979416145663170684a53356d4e3677666f7535646535476270556464427854593156656b4b636a643550432f6e667430322e706e67" + ], + "gasLimit": "10,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xb2a11555ce521e4944e09ab17549d85b487dcd26c84b5017a39e31a3670889ba", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "sign", + "arguments": [ + "0x02" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xb13a017423c366caff8cecfb77a12610a130f4888134122c7937feae0d6d7d17", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "sign", + "arguments": [ + "0x02" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0x3af8d9c9423b2577c6252722c1d90212a4111f7203f9744f76fcfa1d0a310033", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "sign", + "arguments": [ + "0x02" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xb2a11555ce521e4944e09ab17549d85b487dcd26c84b5017a39e31a3670889ba", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "sign", + "arguments": [ + "0x03" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xb13a017423c366caff8cecfb77a12610a130f4888134122c7937feae0d6d7d17", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "sign", + "arguments": [ + "0x03" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0x3af8d9c9423b2577c6252722c1d90212a4111f7203f9744f76fcfa1d0a310033", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "sign", + "arguments": [ + "0x03" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xb2a11555ce521e4944e09ab17549d85b487dcd26c84b5017a39e31a3670889ba", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "sign", + "arguments": [ + "0x04" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xb13a017423c366caff8cecfb77a12610a130f4888134122c7937feae0d6d7d17", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "sign", + "arguments": [ + "0x04" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0x3af8d9c9423b2577c6252722c1d90212a4111f7203f9744f76fcfa1d0a310033", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "sign", + "arguments": [ + "0x04" + ], + "gasLimit": "15,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "performAction", + "arguments": [ + "0x02" + ], + "gasLimit": "30,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "performAction", + "arguments": [ + "0x03" + ], + "gasLimit": "30,000,000", + "gasPrice": "" + } + }, + { + "step": "scCall", + "id": "", + "tx": { + "from": "0xe32afedc904fe1939746ad973beb383563cf63642ba669b3040f9b9428a5ed60", + "to": "0x000000000000000005008a3621a73196c9a9539f05be7d3c277346cdc989ed60", + "function": "performAction", + "arguments": [ + "0x04" + ], + "gasLimit": "30,000,000", + "gasPrice": "" + } + } + ] +} \ No newline at end of file diff --git a/contracts/examples/multisig/tests/multisig_scenario_go_test.rs b/contracts/examples/multisig/tests/multisig_scenario_go_test.rs index 3983c7a619..22c7328e6c 100644 --- a/contracts/examples/multisig/tests/multisig_scenario_go_test.rs +++ b/contracts/examples/multisig/tests/multisig_scenario_go_test.rs @@ -6,83 +6,88 @@ fn world() -> ScenarioWorld { #[test] fn call_other_shard_1_go() { - world().run("scenarios/call_other_shard-1.scen.json"); + multiversx_sc_scenario::run_go("scenarios/call_other_shard-1.scen.json"); } #[test] fn call_other_shard_2_go() { - world().run("scenarios/call_other_shard-2.scen.json"); + multiversx_sc_scenario::run_go("scenarios/call_other_shard-2.scen.json"); } #[test] fn change_board_go() { - world().run("scenarios/changeBoard.scen.json"); + multiversx_sc_scenario::run_go("scenarios/changeBoard.scen.json"); } #[test] fn change_quorum_go() { - world().run("scenarios/changeQuorum.scen.json"); + multiversx_sc_scenario::run_go("scenarios/changeQuorum.scen.json"); } #[test] fn change_quorum_too_big_go() { - world().run("scenarios/changeQuorum_tooBig.scen.json"); + multiversx_sc_scenario::run_go("scenarios/changeQuorum_tooBig.scen.json"); } #[test] fn deploy_adder_err_go() { - world().run("scenarios/deployAdder_err.scen.json"); + multiversx_sc_scenario::run_go("scenarios/deployAdder_err.scen.json"); } #[test] fn deploy_adder_then_call_go() { - world().run("scenarios/deployAdder_then_call.scen.json"); + multiversx_sc_scenario::run_go("scenarios/deployAdder_then_call.scen.json"); } #[test] fn deploy_factorial_go() { - world().run("scenarios/deployFactorial.scen.json"); + multiversx_sc_scenario::run_go("scenarios/deployFactorial.scen.json"); } #[test] fn deploy_other_multisig_go() { - world().run("scenarios/deployOtherMultisig.scen.json"); + multiversx_sc_scenario::run_go("scenarios/deployOtherMultisig.scen.json"); } #[test] fn deploy_duplicate_bm_go() { - world().run("scenarios/deploy_duplicate_bm.scen.json"); + multiversx_sc_scenario::run_go("scenarios/deploy_duplicate_bm.scen.json"); } #[test] #[ignore = "system SC not yet implemented"] fn interactor_nft_go() { - world().run("scenarios/interactor_nft.scen.json"); + multiversx_sc_scenario::run_go("scenarios/interactor_nft.scen.json"); +} + +#[test] +fn interactor_nft_all_roles_go() { + multiversx_sc_scenario::run_go("scenarios/interactor_nft_all_roles.scen.json"); } #[test] fn interactor_wegld_go() { - world().run("scenarios/interactor_wegld.scen.json"); + multiversx_sc_scenario::run_go("scenarios/interactor_wegld.scen.json"); } #[test] fn remove_everyone_go() { - world().run("scenarios/remove_everyone.scen.json"); + multiversx_sc_scenario::run_go("scenarios/remove_everyone.scen.json"); } // TODO: investigate gas issue #[test] #[ignore] fn send_esdt_go() { - world().run("scenarios/sendEsdt.scen.json"); + multiversx_sc_scenario::run_go("scenarios/sendEsdt.scen.json"); } #[test] fn upgrade_go() { - world().run("scenarios/upgrade.scen.json"); + multiversx_sc_scenario::run_go("scenarios/upgrade.scen.json"); } #[test] fn upgrade_from_source_go() { - world().run("scenarios/upgrade_from_source.scen.json"); + multiversx_sc_scenario::run_go("scenarios/upgrade_from_source.scen.json"); } diff --git a/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs b/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs index 3124ac2e89..c6dc14c495 100644 --- a/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs +++ b/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs @@ -32,81 +32,86 @@ fn world() -> ScenarioWorld { #[test] #[ignore] fn call_other_shard_1_rs() { - world().run("scenarios/call_other_shard-1.scen.json"); + multiversx_sc_scenario::run_rs("scenarios/call_other_shard-1.scen.json", world()); } #[test] #[ignore] fn call_other_shard_2_rs() { - world().run("scenarios/call_other_shard-2.scen.json"); + multiversx_sc_scenario::run_rs("scenarios/call_other_shard-2.scen.json", world()); } #[test] fn change_board_rs() { - world().run("scenarios/changeBoard.scen.json"); + multiversx_sc_scenario::run_rs("scenarios/changeBoard.scen.json", world()); } #[test] fn change_quorum_rs() { - world().run("scenarios/changeQuorum.scen.json"); + multiversx_sc_scenario::run_rs("scenarios/changeQuorum.scen.json", world()); } #[test] fn change_quorum_too_big_rs() { - world().run("scenarios/changeQuorum_tooBig.scen.json"); + multiversx_sc_scenario::run_rs("scenarios/changeQuorum_tooBig.scen.json", world()); } #[test] fn deploy_adder_err_rs() { - world().run("scenarios/deployAdder_err.scen.json"); + multiversx_sc_scenario::run_rs("scenarios/deployAdder_err.scen.json", world()); } #[test] fn deploy_adder_then_call_rs() { - world().run("scenarios/deployAdder_then_call.scen.json"); + multiversx_sc_scenario::run_rs("scenarios/deployAdder_then_call.scen.json", world()); } #[test] fn deploy_factorial_rs() { - world().run("scenarios/deployFactorial.scen.json"); + multiversx_sc_scenario::run_rs("scenarios/deployFactorial.scen.json", world()); } #[test] fn deploy_other_multisig_rs() { - world().run("scenarios/deployOtherMultisig.scen.json"); + multiversx_sc_scenario::run_rs("scenarios/deployOtherMultisig.scen.json", world()); } #[test] fn deploy_duplicate_bm_rs() { - world().run("scenarios/deploy_duplicate_bm.scen.json"); + multiversx_sc_scenario::run_rs("scenarios/deploy_duplicate_bm.scen.json", world()); } #[test] fn interactor_nft_rs() { - world().run("scenarios/interactor_nft.scen.json"); + multiversx_sc_scenario::run_rs("scenarios/interactor_nft.scen.json", world()); +} + +#[test] +fn interactor_nft_all_roles_rs() { + multiversx_sc_scenario::run_rs("scenarios/interactor_nft_all_roles.scen.json", world()); } #[test] fn interactor_wegld_rs() { - world().run("scenarios/interactor_wegld.scen.json"); + multiversx_sc_scenario::run_rs("scenarios/interactor_wegld.scen.json", world()); } #[test] fn remove_everyone_rs() { - world().run("scenarios/remove_everyone.scen.json"); + multiversx_sc_scenario::run_rs("scenarios/remove_everyone.scen.json", world()); } #[test] fn send_esdt_rs() { - world().run("scenarios/sendEsdt.scen.json"); + multiversx_sc_scenario::run_rs("scenarios/sendEsdt.scen.json", world()); } #[test] fn upgrade_rs() { - world().run("scenarios/upgrade.scen.json"); + multiversx_sc_scenario::run_rs("scenarios/upgrade.scen.json", world()); } #[test] fn upgrade_from_source_rs() { - world().run("scenarios/upgrade_from_source.scen.json"); + multiversx_sc_scenario::run_rs("scenarios/upgrade_from_source.scen.json", world()); } From 3671cd3213f9e177a7240bcf38e4fe8a8c808390 Mon Sep 17 00:00:00 2001 From: Ovidiu Stinga Date: Wed, 5 Jul 2023 13:18:59 +0300 Subject: [PATCH 12/17] test-gen with new version --- .../tests/multisig_scenario_go_test.rs | 35 ++++++++++--------- .../tests/multisig_scenario_rs_test.rs | 35 ++++++++++--------- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/contracts/examples/multisig/tests/multisig_scenario_go_test.rs b/contracts/examples/multisig/tests/multisig_scenario_go_test.rs index 22c7328e6c..6e7fc7464e 100644 --- a/contracts/examples/multisig/tests/multisig_scenario_go_test.rs +++ b/contracts/examples/multisig/tests/multisig_scenario_go_test.rs @@ -6,88 +6,89 @@ fn world() -> ScenarioWorld { #[test] fn call_other_shard_1_go() { - multiversx_sc_scenario::run_go("scenarios/call_other_shard-1.scen.json"); + world().run("scenarios/call_other_shard-1.scen.json"); } #[test] fn call_other_shard_2_go() { - multiversx_sc_scenario::run_go("scenarios/call_other_shard-2.scen.json"); + world().run("scenarios/call_other_shard-2.scen.json"); } #[test] fn change_board_go() { - multiversx_sc_scenario::run_go("scenarios/changeBoard.scen.json"); + world().run("scenarios/changeBoard.scen.json"); } #[test] fn change_quorum_go() { - multiversx_sc_scenario::run_go("scenarios/changeQuorum.scen.json"); + world().run("scenarios/changeQuorum.scen.json"); } #[test] fn change_quorum_too_big_go() { - multiversx_sc_scenario::run_go("scenarios/changeQuorum_tooBig.scen.json"); + world().run("scenarios/changeQuorum_tooBig.scen.json"); } #[test] fn deploy_adder_err_go() { - multiversx_sc_scenario::run_go("scenarios/deployAdder_err.scen.json"); + world().run("scenarios/deployAdder_err.scen.json"); } #[test] fn deploy_adder_then_call_go() { - multiversx_sc_scenario::run_go("scenarios/deployAdder_then_call.scen.json"); + world().run("scenarios/deployAdder_then_call.scen.json"); } #[test] fn deploy_factorial_go() { - multiversx_sc_scenario::run_go("scenarios/deployFactorial.scen.json"); + world().run("scenarios/deployFactorial.scen.json"); } #[test] fn deploy_other_multisig_go() { - multiversx_sc_scenario::run_go("scenarios/deployOtherMultisig.scen.json"); + world().run("scenarios/deployOtherMultisig.scen.json"); } #[test] fn deploy_duplicate_bm_go() { - multiversx_sc_scenario::run_go("scenarios/deploy_duplicate_bm.scen.json"); + world().run("scenarios/deploy_duplicate_bm.scen.json"); } #[test] #[ignore = "system SC not yet implemented"] fn interactor_nft_go() { - multiversx_sc_scenario::run_go("scenarios/interactor_nft.scen.json"); + world().run("scenarios/interactor_nft.scen.json"); } #[test] +#[ignore = "system SC not yet implemented"] fn interactor_nft_all_roles_go() { - multiversx_sc_scenario::run_go("scenarios/interactor_nft_all_roles.scen.json"); + world().run("scenarios/interactor_nft_all_roles.scen.json"); } #[test] fn interactor_wegld_go() { - multiversx_sc_scenario::run_go("scenarios/interactor_wegld.scen.json"); + world().run("scenarios/interactor_wegld.scen.json"); } #[test] fn remove_everyone_go() { - multiversx_sc_scenario::run_go("scenarios/remove_everyone.scen.json"); + world().run("scenarios/remove_everyone.scen.json"); } // TODO: investigate gas issue #[test] #[ignore] fn send_esdt_go() { - multiversx_sc_scenario::run_go("scenarios/sendEsdt.scen.json"); + world().run("scenarios/sendEsdt.scen.json"); } #[test] fn upgrade_go() { - multiversx_sc_scenario::run_go("scenarios/upgrade.scen.json"); + world().run("scenarios/upgrade.scen.json"); } #[test] fn upgrade_from_source_go() { - multiversx_sc_scenario::run_go("scenarios/upgrade_from_source.scen.json"); + world().run("scenarios/upgrade_from_source.scen.json"); } diff --git a/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs b/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs index c6dc14c495..3220babdc1 100644 --- a/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs +++ b/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs @@ -32,86 +32,87 @@ fn world() -> ScenarioWorld { #[test] #[ignore] fn call_other_shard_1_rs() { - multiversx_sc_scenario::run_rs("scenarios/call_other_shard-1.scen.json", world()); + world().run("scenarios/call_other_shard-1.scen.json"); } #[test] #[ignore] fn call_other_shard_2_rs() { - multiversx_sc_scenario::run_rs("scenarios/call_other_shard-2.scen.json", world()); + world().run("scenarios/call_other_shard-2.scen.json"); } #[test] fn change_board_rs() { - multiversx_sc_scenario::run_rs("scenarios/changeBoard.scen.json", world()); + world().run("scenarios/changeBoard.scen.json"); } #[test] fn change_quorum_rs() { - multiversx_sc_scenario::run_rs("scenarios/changeQuorum.scen.json", world()); + world().run("scenarios/changeQuorum.scen.json"); } #[test] fn change_quorum_too_big_rs() { - multiversx_sc_scenario::run_rs("scenarios/changeQuorum_tooBig.scen.json", world()); + world().run("scenarios/changeQuorum_tooBig.scen.json"); } #[test] fn deploy_adder_err_rs() { - multiversx_sc_scenario::run_rs("scenarios/deployAdder_err.scen.json", world()); + world().run("scenarios/deployAdder_err.scen.json"); } #[test] fn deploy_adder_then_call_rs() { - multiversx_sc_scenario::run_rs("scenarios/deployAdder_then_call.scen.json", world()); + world().run("scenarios/deployAdder_then_call.scen.json"); } #[test] fn deploy_factorial_rs() { - multiversx_sc_scenario::run_rs("scenarios/deployFactorial.scen.json", world()); + world().run("scenarios/deployFactorial.scen.json"); } #[test] fn deploy_other_multisig_rs() { - multiversx_sc_scenario::run_rs("scenarios/deployOtherMultisig.scen.json", world()); + world().run("scenarios/deployOtherMultisig.scen.json"); } #[test] fn deploy_duplicate_bm_rs() { - multiversx_sc_scenario::run_rs("scenarios/deploy_duplicate_bm.scen.json", world()); + world().run("scenarios/deploy_duplicate_bm.scen.json"); } #[test] fn interactor_nft_rs() { - multiversx_sc_scenario::run_rs("scenarios/interactor_nft.scen.json", world()); + world().run("scenarios/interactor_nft.scen.json"); } #[test] +#[should_panic(expected = "not implemented")] fn interactor_nft_all_roles_rs() { - multiversx_sc_scenario::run_rs("scenarios/interactor_nft_all_roles.scen.json", world()); + world().run("scenarios/interactor_nft_all_roles.scen.json"); } #[test] fn interactor_wegld_rs() { - multiversx_sc_scenario::run_rs("scenarios/interactor_wegld.scen.json", world()); + world().run("scenarios/interactor_wegld.scen.json"); } #[test] fn remove_everyone_rs() { - multiversx_sc_scenario::run_rs("scenarios/remove_everyone.scen.json", world()); + world().run("scenarios/remove_everyone.scen.json"); } #[test] fn send_esdt_rs() { - multiversx_sc_scenario::run_rs("scenarios/sendEsdt.scen.json", world()); + world().run("scenarios/sendEsdt.scen.json"); } #[test] fn upgrade_rs() { - multiversx_sc_scenario::run_rs("scenarios/upgrade.scen.json", world()); + world().run("scenarios/upgrade.scen.json"); } #[test] fn upgrade_from_source_rs() { - multiversx_sc_scenario::run_rs("scenarios/upgrade_from_source.scen.json", world()); + world().run("scenarios/upgrade_from_source.scen.json"); } From 1b786f7b51059433a799063f09165b7e8cb97ca1 Mon Sep 17 00:00:00 2001 From: Ovidiu Stinga Date: Wed, 5 Jul 2023 18:39:27 +0300 Subject: [PATCH 13/17] system_mock register and set all roles and refactor --- .../tests/multisig_scenario_rs_test.rs | 1 - vm/src/tx_execution/system_sc.rs | 2 +- .../tx_execution/system_sc/system_sc_issue.rs | 86 +++++++++++-------- .../system_sc/system_sc_unimplemented.rs | 9 +- vm/src/world_mock/esdt_data.rs | 53 +++++++++--- 5 files changed, 93 insertions(+), 58 deletions(-) diff --git a/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs b/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs index 3220babdc1..27d286e1d9 100644 --- a/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs +++ b/contracts/examples/multisig/tests/multisig_scenario_rs_test.rs @@ -87,7 +87,6 @@ fn interactor_nft_rs() { } #[test] -#[should_panic(expected = "not implemented")] fn interactor_nft_all_roles_rs() { world().run("scenarios/interactor_nft_all_roles.scen.json"); } diff --git a/vm/src/tx_execution/system_sc.rs b/vm/src/tx_execution/system_sc.rs index b8e7a4b380..d3103e0239 100644 --- a/vm/src/tx_execution/system_sc.rs +++ b/vm/src/tx_execution/system_sc.rs @@ -28,7 +28,7 @@ pub fn execute_system_sc(tx_context: TxContext) -> (TxContext, TxResult) { "issueNonFungible" => issue_non_fungible(tx_context), "registerMetaESDT" => register_meta_esdt(tx_context), "changeSFTToMetaESDT" => change_sft_to_meta_esdt(tx_context), - "registerAndSetAllRoles" => register_and_set_all_roles(), + "registerAndSetAllRoles" => register_and_set_all_roles(tx_context), "ESDTBurn" => esdt_burn(tx_context), "mint" => mint(tx_context), "freeze" => freeze(tx_context), diff --git a/vm/src/tx_execution/system_sc/system_sc_issue.rs b/vm/src/tx_execution/system_sc/system_sc_issue.rs index cb9c0fcea9..00f946e67e 100644 --- a/vm/src/tx_execution/system_sc/system_sc_issue.rs +++ b/vm/src/tx_execution/system_sc/system_sc_issue.rs @@ -6,7 +6,8 @@ use crate::{ types::top_decode_u64, }; -/// Issues a new token. +/// Issues a new fungible token. +#[allow(unused_variables)] pub fn issue(tx_context: TxContext) -> (TxContext, TxResult) { let tx_input = tx_context.input_ref(); let tx_cache = tx_context.blockchain_cache(); @@ -16,46 +17,33 @@ pub fn issue(tx_context: TxContext) -> (TxContext, TxResult) { tx_result = TxResult::from_vm_error("not enough arguments"); return (tx_context, tx_result); } - let _name = tx_input.args[0].clone(); + let name = tx_input.args[0].clone(); let ticker = tx_input.args[1].clone(); - let _total_supply = BigUint::from_bytes_be(tx_input.args[2].clone().as_ref()); - let _decimals = top_decode_u64(tx_input.args[3].clone().as_ref()) as u32; + let total_supply = BigUint::from_bytes_be(tx_input.args[2].clone().as_ref()); + let decimals = top_decode_u64(tx_input.args[3].clone().as_ref()) as u32; - let mut new_token_identifiers = tx_cache.get_new_token_identifiers(); - - let token_identifier = if let Some((i, ti)) = - first_token_identifier_with_ticker(&new_token_identifiers, &ticker) - { - new_token_identifiers.remove(i); - ti.into_bytes() - } else { - generate_token_identifier_from_ticker(tx_input, tx_cache, &ticker) - }; - - println!( - "\n\ngenerated new token_identifier: {}\n\n", - std::str::from_utf8(&token_identifier).unwrap() - ); - - tx_cache.with_account_mut(&tx_input.from, |account| { - account.esdt.issue_token(&token_identifier); - }); - tx_cache.set_new_token_identifiers(new_token_identifiers); - - tx_result = TxResult { - result_values: vec![token_identifier], - ..Default::default() - }; - - (tx_context, tx_result) + register_and_set_roles(tx_context, ticker, "FT".to_string()) } /// Issues a new semi-fungible token. +#[allow(unused_variables)] pub fn issue_semi_fungible(tx_context: TxContext) -> (TxContext, TxResult) { - issue_non_fungible(tx_context) + let tx_input = tx_context.input_ref(); + let tx_cache = tx_context.blockchain_cache(); + let tx_result: TxResult; + + if tx_input.args.len() < 2 { + tx_result = TxResult::from_vm_error("not enough arguments"); + return (tx_context, tx_result); + } + let name = tx_input.args[0].clone(); + let ticker = tx_input.args[1].clone(); + + register_and_set_roles(tx_context, ticker, "SFT".to_string()) } /// Issues a new non-fungible token. +#[allow(unused_variables)] pub fn issue_non_fungible(tx_context: TxContext) -> (TxContext, TxResult) { let tx_input = tx_context.input_ref(); let tx_cache = tx_context.blockchain_cache(); @@ -65,9 +53,35 @@ pub fn issue_non_fungible(tx_context: TxContext) -> (TxContext, TxResult) { tx_result = TxResult::from_vm_error("not enough arguments"); return (tx_context, tx_result); } - let _name = tx_input.args[0].clone(); + let name = tx_input.args[0].clone(); let ticker = tx_input.args[1].clone(); + register_and_set_roles(tx_context, ticker, "NFT".to_string()) +} + +// Issues a new token and sets all roles for its type. +#[allow(unused_variables)] +pub fn register_and_set_all_roles(tx_context: TxContext) -> (TxContext, TxResult) { + let tx_input = tx_context.input_ref(); + let tx_cache = tx_context.blockchain_cache(); + + if tx_input.args.len() < 4 { + let tx_result = TxResult::from_vm_error("not enough arguments"); + return (tx_context, tx_result); + } + + let name = tx_input.args[0].clone(); + let ticker = tx_input.args[1].clone(); + let token_type = String::from_utf8(tx_input.args[2].clone()).unwrap(); + let decimals = top_decode_u64(tx_input.args[3].clone().as_ref()) as u32; + + register_and_set_roles(tx_context, ticker, token_type) +} + +fn register_and_set_roles(tx_context: TxContext, ticker: Vec, token_type: String) -> (TxContext, TxResult) { + let tx_input = tx_context.input_ref(); + let tx_cache = tx_context.blockchain_cache(); + let mut new_token_identifiers = tx_cache.get_new_token_identifiers(); let token_identifier = if let Some((i, ti)) = @@ -80,11 +94,13 @@ pub fn issue_non_fungible(tx_context: TxContext) -> (TxContext, TxResult) { }; tx_cache.with_account_mut(&tx_input.from, |account| { - account.esdt.issue_token(&token_identifier); + account + .esdt + .register_and_set_roles(&token_identifier, &token_type); }); tx_cache.set_new_token_identifiers(new_token_identifiers); - tx_result = TxResult { + let tx_result = TxResult { result_values: vec![token_identifier], ..Default::default() }; diff --git a/vm/src/tx_execution/system_sc/system_sc_unimplemented.rs b/vm/src/tx_execution/system_sc/system_sc_unimplemented.rs index 9d4d07ce96..3a25157294 100644 --- a/vm/src/tx_execution/system_sc/system_sc_unimplemented.rs +++ b/vm/src/tx_execution/system_sc/system_sc_unimplemented.rs @@ -9,17 +9,12 @@ pub fn register_meta_esdt(tx_context: TxContext) -> (TxContext, TxResult) { } #[allow(unused_variables)] -pub fn change_sft_to_meta_esdt(_tx_context: TxContext) -> (TxContext, TxResult) { +pub fn change_sft_to_meta_esdt(tx_context: TxContext) -> (TxContext, TxResult) { unimplemented!() } #[allow(unused_variables)] -pub fn register_and_set_all_roles() -> (TxContext, TxResult) { - unimplemented!() -} - -#[allow(unused_variables)] -pub fn esdt_burn(_tx_context: TxContext) -> (TxContext, TxResult) { +pub fn esdt_burn(tx_context: TxContext) -> (TxContext, TxResult) { unimplemented!() } diff --git a/vm/src/world_mock/esdt_data.rs b/vm/src/world_mock/esdt_data.rs index d44098bb4f..6e44782af9 100644 --- a/vm/src/world_mock/esdt_data.rs +++ b/vm/src/world_mock/esdt_data.rs @@ -150,31 +150,56 @@ impl AccountEsdt { self.0.iter() } - pub fn issue_token(&mut self, token_identifier: &[u8]) { - let roles = vec![ - "ESDTLocalMint".as_bytes().to_vec(), - "ESDTLocalBurn".as_bytes().to_vec(), - ]; + pub fn set_special_role(&mut self, token_identifier: &[u8], role: &[u8]) { + if let Some(esdt_data) = self.get_mut_by_identifier(token_identifier) { + let roles = esdt_data.roles.get(); + if !roles.contains(role.to_vec().as_ref()) { + let mut new_roles = roles; + new_roles.push(role.to_vec()); + esdt_data.roles = EsdtRoles::new(new_roles); + } + } + } + + pub fn register_and_set_roles(&mut self, token_identifier: &[u8], token_type: &str) { + self.issue_token(token_identifier); + self.set_roles( + token_identifier.to_vec(), + Self::get_all_roles_for_token_type(token_type), + ); + } + fn issue_token(&mut self, token_identifier: &[u8]) { self.0.insert( token_identifier.to_vec(), EsdtData { instances: EsdtInstances::new(), last_nonce: 0, - roles: EsdtRoles::new(roles), + roles: EsdtRoles::default(), frozen: false, }, ); } - pub fn set_special_role(&mut self, token_identifier: &[u8], role: &[u8]) { - if let Some(esdt_data) = self.get_mut_by_identifier(token_identifier) { - let roles = esdt_data.roles.get(); - if !roles.contains(role.to_vec().as_ref()) { - let mut new_roles = roles; - new_roles.push(role.to_vec()); - esdt_data.roles = EsdtRoles::new(new_roles); - } + fn get_all_roles_for_token_type(token_type: &str) -> Vec> { + match token_type { + "NFT" => vec![ + "ESDTRoleNFTCreate".as_bytes().to_vec(), + "ESDTRoleNFTBurn".as_bytes().to_vec(), + "ESDTRoleNFTUpdateAttributes".as_bytes().to_vec(), + "ESDTRoleNFTAddURI".as_bytes().to_vec(), + ], + "SFT" | "META" => vec![ + "ESDTRoleNFTCreate".as_bytes().to_vec(), + "ESDTRoleNFTBurn".as_bytes().to_vec(), + "ESDTRoleNFTAddQuantity".as_bytes().to_vec(), + ], + "FNG" => vec![ + "ESDTRoleLocalMint".as_bytes().to_vec(), + "ESDTRoleLocalBurn".as_bytes().to_vec(), + "ESDTRoleLocalTransfer".as_bytes().to_vec(), + ], + _ => panic!("invalid token type"), } } } From 3022b0690e06231378581d98083cdb2b324c1b4c Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 10 Jul 2023 16:51:14 +0300 Subject: [PATCH 14/17] Merge branch 'rc/v0.42' into feat/whitebox (system-sc) --- vm/src/tx_execution/exec_general_tx.rs | 2 +- vm/src/tx_execution/system_sc.rs | 75 ++++++------- .../tx_execution/system_sc/system_sc_issue.rs | 62 ++++------ .../system_sc/system_sc_special_roles.rs | 17 ++- .../system_sc/system_sc_unimplemented.rs | 106 +++++++++--------- vm/src/world_mock/blockchain_mock.rs | 5 +- 6 files changed, 122 insertions(+), 145 deletions(-) diff --git a/vm/src/tx_execution/exec_general_tx.rs b/vm/src/tx_execution/exec_general_tx.rs index ef4fdf81a6..3fd572abd4 100644 --- a/vm/src/tx_execution/exec_general_tx.rs +++ b/vm/src/tx_execution/exec_general_tx.rs @@ -32,7 +32,7 @@ impl BlockchainVMRef { pub fn default_execution( &self, tx_input: TxInput, - mut tx_cache: TxCache, + tx_cache: TxCache, f: F, ) -> (TxResult, BlockchainUpdate) where diff --git a/vm/src/tx_execution/system_sc.rs b/vm/src/tx_execution/system_sc.rs index bbfa8d5c95..ed28fecd8f 100644 --- a/vm/src/tx_execution/system_sc.rs +++ b/vm/src/tx_execution/system_sc.rs @@ -3,7 +3,7 @@ mod system_sc_special_roles; mod system_sc_unimplemented; use crate::{ - tx_mock::{BlockchainUpdate, TxCache, TxContext, TxInput, TxResult}, + tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult}, types::VMAddress, }; use hex_literal::hex; @@ -21,41 +21,40 @@ pub fn is_system_sc_address(address: &VMAddress) -> bool { } pub fn execute_system_sc(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { - todo!() - // let func_name = tx_context.tx_input_box.func_name.clone(); - // match func_name.as_str() { - // "issue" => issue(tx_context), - // "issueSemiFungible" => issue_semi_fungible(tx_context), - // "issueNonFungible" => issue_non_fungible(tx_context), - // "registerMetaESDT" => register_meta_esdt(tx_context), - // "changeSFTToMetaESDT" => change_sft_to_meta_esdt(tx_context), - // "registerAndSetAllRoles" => register_and_set_all_roles(tx_context), - // "ESDTBurn" => esdt_burn(tx_context), - // "mint" => mint(tx_context), - // "freeze" => freeze(tx_context), - // "unFreeze" => unfreeze(tx_context), - // "wipe" => wipe(tx_context), - // "pause" => pause(tx_context), - // "unPause" => unpause(tx_context), - // "freezeSingleNFT" => freeze_single_nft(tx_context), - // "unFreezeSingleNFT" => unfreeze_single_nft(tx_context), - // "wipeSingleNFT" => wipe_single_nft(tx_context), - // "claim" => claim(tx_context), - // "configChange" => config_change(tx_context), - // "controlChanges" => control_changes(tx_context), - // "transferOwnership" => transfer_ownership(tx_context), - // "getTokenProperties" => get_token_properties(tx_context), - // "getSpecialRoles" => get_special_roles(tx_context), - // "setSpecialRole" => set_special_role(tx_context), - // "unSetSpecialRole" => unset_special_role(tx_context), - // "transferNFTCreateRole" => transfer_nft_create_role(tx_context), - // "stopNFTCreate" => stop_nft_create(tx_context), - // "getAllAddressesAndRoles" => get_all_addresses_and_roles(tx_context), - // "getContractConfig" => get_contract_config(tx_context), - // "changeToMultiShardCreate" => change_to_multi_shard_create(tx_context), - // "setBurnRoleGlobally" => set_burn_role_globally(tx_context), - // "unsetBurnRoleGlobally" => unset_burn_role_globally(tx_context), - // "sendAllTransferRoleAddresses" => send_all_transfer_role_addresses(tx_context), - // invalid_func_name => panic!("invalid system SC function: {invalid_func_name}"), - // } + let func_name = &tx_input.func_name; + match func_name.as_str() { + "issue" => issue(tx_input, tx_cache), + "issueSemiFungible" => issue_semi_fungible(tx_input, tx_cache), + "issueNonFungible" => issue_non_fungible(tx_input, tx_cache), + "registerMetaESDT" => register_meta_esdt(tx_input, tx_cache), + "changeSFTToMetaESDT" => change_sft_to_meta_esdt(tx_input, tx_cache), + "registerAndSetAllRoles" => register_and_set_all_roles(tx_input, tx_cache), + "ESDTBurn" => esdt_burn(tx_input, tx_cache), + "mint" => mint(tx_input, tx_cache), + "freeze" => freeze(tx_input, tx_cache), + "unFreeze" => unfreeze(tx_input, tx_cache), + "wipe" => wipe(tx_input, tx_cache), + "pause" => pause(tx_input, tx_cache), + "unPause" => unpause(tx_input, tx_cache), + "freezeSingleNFT" => freeze_single_nft(tx_input, tx_cache), + "unFreezeSingleNFT" => unfreeze_single_nft(tx_input, tx_cache), + "wipeSingleNFT" => wipe_single_nft(tx_input, tx_cache), + "claim" => claim(tx_input, tx_cache), + "configChange" => config_change(tx_input, tx_cache), + "controlChanges" => control_changes(tx_input, tx_cache), + "transferOwnership" => transfer_ownership(tx_input, tx_cache), + "getTokenProperties" => get_token_properties(tx_input, tx_cache), + "getSpecialRoles" => get_special_roles(tx_input, tx_cache), + "setSpecialRole" => set_special_role(tx_input, tx_cache), + "unSetSpecialRole" => unset_special_role(tx_input, tx_cache), + "transferNFTCreateRole" => transfer_nft_create_role(tx_input, tx_cache), + "stopNFTCreate" => stop_nft_create(tx_input, tx_cache), + "getAllAddressesAndRoles" => get_all_addresses_and_roles(tx_input, tx_cache), + "getContractConfig" => get_contract_config(tx_input, tx_cache), + "changeToMultiShardCreate" => change_to_multi_shard_create(tx_input, tx_cache), + "setBurnRoleGlobally" => set_burn_role_globally(tx_input, tx_cache), + "unsetBurnRoleGlobally" => unset_burn_role_globally(tx_input, tx_cache), + "sendAllTransferRoleAddresses" => send_all_transfer_role_addresses(tx_input, tx_cache), + invalid_func_name => panic!("invalid system SC function: {invalid_func_name}"), + } } diff --git a/vm/src/tx_execution/system_sc/system_sc_issue.rs b/vm/src/tx_execution/system_sc/system_sc_issue.rs index 93e970dcbe..a359eff353 100644 --- a/vm/src/tx_execution/system_sc/system_sc_issue.rs +++ b/vm/src/tx_execution/system_sc/system_sc_issue.rs @@ -2,72 +2,60 @@ use num_bigint::BigUint; use crate::{ crypto_functions::keccak256, - tx_mock::{TxCache, TxContext, TxInput, TxResult}, + tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult}, types::top_decode_u64, }; /// Issues a new fungible token. #[allow(unused_variables)] -pub fn issue(tx_context: TxContext) -> (TxContext, TxResult) { - let tx_input = tx_context.input_ref(); - let tx_cache = tx_context.blockchain_cache(); - let tx_result: TxResult; - +pub fn issue(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { if tx_input.args.len() < 4 { - tx_result = TxResult::from_vm_error("not enough arguments"); - return (tx_context, tx_result); + let tx_result = TxResult::from_vm_error("not enough arguments"); + return (tx_result, BlockchainUpdate::empty()); } let name = tx_input.args[0].clone(); let ticker = tx_input.args[1].clone(); let total_supply = BigUint::from_bytes_be(tx_input.args[2].clone().as_ref()); let decimals = top_decode_u64(tx_input.args[3].clone().as_ref()) as u32; - register_and_set_roles(tx_context, ticker, "FT".to_string()) + register_and_set_roles(tx_input, tx_cache, ticker, "FT".to_string()) } /// Issues a new semi-fungible token. #[allow(unused_variables)] -pub fn issue_semi_fungible(tx_context: TxContext) -> (TxContext, TxResult) { - let tx_input = tx_context.input_ref(); - let tx_cache = tx_context.blockchain_cache(); - let tx_result: TxResult; - +pub fn issue_semi_fungible(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { if tx_input.args.len() < 2 { - tx_result = TxResult::from_vm_error("not enough arguments"); - return (tx_context, tx_result); + let tx_result = TxResult::from_vm_error("not enough arguments"); + return (tx_result, BlockchainUpdate::empty()); } let name = tx_input.args[0].clone(); let ticker = tx_input.args[1].clone(); - register_and_set_roles(tx_context, ticker, "SFT".to_string()) + register_and_set_roles(tx_input, tx_cache, ticker, "SFT".to_string()) } /// Issues a new non-fungible token. #[allow(unused_variables)] -pub fn issue_non_fungible(tx_context: TxContext) -> (TxContext, TxResult) { - let tx_input = tx_context.input_ref(); - let tx_cache = tx_context.blockchain_cache(); - let tx_result: TxResult; - +pub fn issue_non_fungible(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { if tx_input.args.len() < 2 { - tx_result = TxResult::from_vm_error("not enough arguments"); - return (tx_context, tx_result); + let tx_result = TxResult::from_vm_error("not enough arguments"); + return (tx_result, BlockchainUpdate::empty()); } let name = tx_input.args[0].clone(); let ticker = tx_input.args[1].clone(); - register_and_set_roles(tx_context, ticker, "NFT".to_string()) + register_and_set_roles(tx_input, tx_cache, ticker, "NFT".to_string()) } // Issues a new token and sets all roles for its type. #[allow(unused_variables)] -pub fn register_and_set_all_roles(tx_context: TxContext) -> (TxContext, TxResult) { - let tx_input = tx_context.input_ref(); - let tx_cache = tx_context.blockchain_cache(); - +pub fn register_and_set_all_roles( + tx_input: TxInput, + tx_cache: TxCache, +) -> (TxResult, BlockchainUpdate) { if tx_input.args.len() < 4 { let tx_result = TxResult::from_vm_error("not enough arguments"); - return (tx_context, tx_result); + return (tx_result, BlockchainUpdate::empty()); } let name = tx_input.args[0].clone(); @@ -75,17 +63,15 @@ pub fn register_and_set_all_roles(tx_context: TxContext) -> (TxContext, TxResult let token_type = String::from_utf8(tx_input.args[2].clone()).unwrap(); let decimals = top_decode_u64(tx_input.args[3].clone().as_ref()) as u32; - register_and_set_roles(tx_context, ticker, token_type) + register_and_set_roles(tx_input, tx_cache, ticker, token_type) } fn register_and_set_roles( - tx_context: TxContext, + tx_input: TxInput, + tx_cache: TxCache, ticker: Vec, token_type: String, -) -> (TxContext, TxResult) { - let tx_input = tx_context.input_ref(); - let tx_cache = tx_context.blockchain_cache(); - +) -> (TxResult, BlockchainUpdate) { let mut new_token_identifiers = tx_cache.get_new_token_identifiers(); let token_identifier = if let Some((i, ti)) = @@ -94,7 +80,7 @@ fn register_and_set_roles( new_token_identifiers.remove(i); ti.into_bytes() } else { - generate_token_identifier_from_ticker(tx_input, tx_cache, &ticker) + generate_token_identifier_from_ticker(&tx_input, &tx_cache, &ticker) }; tx_cache.with_account_mut(&tx_input.from, |account| { @@ -109,7 +95,7 @@ fn register_and_set_roles( ..Default::default() }; - (tx_context, tx_result) + (tx_result, tx_cache.into_blockchain_updates()) } fn first_token_identifier_with_ticker( diff --git a/vm/src/tx_execution/system_sc/system_sc_special_roles.rs b/vm/src/tx_execution/system_sc/system_sc_special_roles.rs index fd9a2f3148..17beef2266 100644 --- a/vm/src/tx_execution/system_sc/system_sc_special_roles.rs +++ b/vm/src/tx_execution/system_sc/system_sc_special_roles.rs @@ -1,17 +1,16 @@ use crate::{ - tx_mock::{TxContext, TxResult}, + tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult}, types::VMAddress, }; -pub fn set_special_role(tx_context: TxContext) -> (TxContext, TxResult) { - let tx_input = tx_context.input_ref(); - let tx_cache = tx_context.blockchain_cache(); - let mut tx_result = TxResult::empty(); - +pub fn set_special_role(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { if tx_input.args.len() < 3 { - tx_result = TxResult::from_vm_error("setSpecialRole too few arguments"); - return (tx_context, tx_result); + return ( + TxResult::from_vm_error("setSpecialRole too few arguments"), + BlockchainUpdate::empty(), + ); } + let token_identifier = tx_input.args[0].clone(); let address = VMAddress::from_slice(tx_input.args[1].as_slice()); let role = tx_input.args[2].clone(); @@ -20,5 +19,5 @@ pub fn set_special_role(tx_context: TxContext) -> (TxContext, TxResult) { account.esdt.set_special_role(&token_identifier, &role); }); - (tx_context, tx_result) + (TxResult::empty(), tx_cache.into_blockchain_updates()) } diff --git a/vm/src/tx_execution/system_sc/system_sc_unimplemented.rs b/vm/src/tx_execution/system_sc/system_sc_unimplemented.rs index 3a25157294..cf93be7727 100644 --- a/vm/src/tx_execution/system_sc/system_sc_unimplemented.rs +++ b/vm/src/tx_execution/system_sc/system_sc_unimplemented.rs @@ -1,139 +1,135 @@ -use crate::tx_mock::{TxContext, TxResult}; +#![allow(unused_variables)] + +use crate::tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult}; /// Every unimplemented fn will be implemented and moved to its corresponding file. /// This file will be deleted. -#[allow(unused_variables)] -pub fn register_meta_esdt(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn register_meta_esdt(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn change_sft_to_meta_esdt(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn change_sft_to_meta_esdt( + tx_input: TxInput, + tx_cache: TxCache, +) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn esdt_burn(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn esdt_burn(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn mint(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn mint(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn freeze(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn freeze(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn unfreeze(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn unfreeze(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn wipe(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn wipe(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn pause(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn pause(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn unpause(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn unpause(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn freeze_single_nft(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn freeze_single_nft(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn unfreeze_single_nft(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn unfreeze_single_nft(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn wipe_single_nft(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn wipe_single_nft(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn claim(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn claim(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn config_change(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn config_change(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn control_changes(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn control_changes(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn transfer_ownership(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn transfer_ownership(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn get_token_properties(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn get_token_properties(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn get_special_roles(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn get_special_roles(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn unset_special_role(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn unset_special_role(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn transfer_nft_create_role(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn transfer_nft_create_role( + tx_input: TxInput, + tx_cache: TxCache, +) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn stop_nft_create(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn stop_nft_create(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn get_all_addresses_and_roles(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn get_all_addresses_and_roles( + tx_input: TxInput, + tx_cache: TxCache, +) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn get_contract_config(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn get_contract_config(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn change_to_multi_shard_create(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn change_to_multi_shard_create( + tx_input: TxInput, + tx_cache: TxCache, +) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn set_burn_role_globally(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn set_burn_role_globally( + tx_input: TxInput, + tx_cache: TxCache, +) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn unset_burn_role_globally(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn unset_burn_role_globally( + tx_input: TxInput, + tx_cache: TxCache, +) -> (TxResult, BlockchainUpdate) { unimplemented!() } -#[allow(unused_variables)] -pub fn send_all_transfer_role_addresses(tx_context: TxContext) -> (TxContext, TxResult) { +pub fn send_all_transfer_role_addresses( + tx_input: TxInput, + tx_cache: TxCache, +) -> (TxResult, BlockchainUpdate) { unimplemented!() } diff --git a/vm/src/world_mock/blockchain_mock.rs b/vm/src/world_mock/blockchain_mock.rs index 925d0a166f..0113ea3520 100644 --- a/vm/src/world_mock/blockchain_mock.rs +++ b/vm/src/world_mock/blockchain_mock.rs @@ -1,9 +1,6 @@ use crate::{tx_execution::BlockchainVMRef, with_shared::Shareable}; use multiversx_chain_vm_executor::Executor; -use std::{ - fmt::Debug, - ops::{Deref, DerefMut}, -}; +use std::{fmt::Debug, ops::Deref}; use super::{BlockchainState, FailingExecutor}; From def10e58f90fefd06f35b03401ce4e1d66178c6b Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 10 Jul 2023 17:09:33 +0300 Subject: [PATCH 15/17] vm refactor - VMTokenType enum --- .../tx_execution/system_sc/system_sc_issue.rs | 14 +++++++------- vm/src/types.rs | 2 ++ vm/src/types/vm_token_type.rs | 19 +++++++++++++++++++ vm/src/world_mock/esdt_data.rs | 13 ++++++------- 4 files changed, 34 insertions(+), 14 deletions(-) create mode 100644 vm/src/types/vm_token_type.rs diff --git a/vm/src/tx_execution/system_sc/system_sc_issue.rs b/vm/src/tx_execution/system_sc/system_sc_issue.rs index a359eff353..dd787c61e2 100644 --- a/vm/src/tx_execution/system_sc/system_sc_issue.rs +++ b/vm/src/tx_execution/system_sc/system_sc_issue.rs @@ -3,7 +3,7 @@ use num_bigint::BigUint; use crate::{ crypto_functions::keccak256, tx_mock::{BlockchainUpdate, TxCache, TxInput, TxResult}, - types::top_decode_u64, + types::{top_decode_u64, VMTokenType}, }; /// Issues a new fungible token. @@ -18,7 +18,7 @@ pub fn issue(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdat let total_supply = BigUint::from_bytes_be(tx_input.args[2].clone().as_ref()); let decimals = top_decode_u64(tx_input.args[3].clone().as_ref()) as u32; - register_and_set_roles(tx_input, tx_cache, ticker, "FT".to_string()) + register_and_set_roles(tx_input, tx_cache, ticker, VMTokenType::Fungible) } /// Issues a new semi-fungible token. @@ -31,7 +31,7 @@ pub fn issue_semi_fungible(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, B let name = tx_input.args[0].clone(); let ticker = tx_input.args[1].clone(); - register_and_set_roles(tx_input, tx_cache, ticker, "SFT".to_string()) + register_and_set_roles(tx_input, tx_cache, ticker, VMTokenType::SemiFungible) } /// Issues a new non-fungible token. @@ -44,7 +44,7 @@ pub fn issue_non_fungible(tx_input: TxInput, tx_cache: TxCache) -> (TxResult, Bl let name = tx_input.args[0].clone(); let ticker = tx_input.args[1].clone(); - register_and_set_roles(tx_input, tx_cache, ticker, "NFT".to_string()) + register_and_set_roles(tx_input, tx_cache, ticker, VMTokenType::NonFungible) } // Issues a new token and sets all roles for its type. @@ -60,7 +60,7 @@ pub fn register_and_set_all_roles( let name = tx_input.args[0].clone(); let ticker = tx_input.args[1].clone(); - let token_type = String::from_utf8(tx_input.args[2].clone()).unwrap(); + let token_type = VMTokenType::from_system_sc_arg(&tx_input.args[2]); let decimals = top_decode_u64(tx_input.args[3].clone().as_ref()) as u32; register_and_set_roles(tx_input, tx_cache, ticker, token_type) @@ -70,7 +70,7 @@ fn register_and_set_roles( tx_input: TxInput, tx_cache: TxCache, ticker: Vec, - token_type: String, + token_type: VMTokenType, ) -> (TxResult, BlockchainUpdate) { let mut new_token_identifiers = tx_cache.get_new_token_identifiers(); @@ -86,7 +86,7 @@ fn register_and_set_roles( tx_cache.with_account_mut(&tx_input.from, |account| { account .esdt - .register_and_set_roles(&token_identifier, &token_type); + .register_and_set_roles(&token_identifier, token_type); }); tx_cache.set_new_token_identifiers(new_token_identifiers); diff --git a/vm/src/types.rs b/vm/src/types.rs index c242d61cd2..d1ada0c0f6 100644 --- a/vm/src/types.rs +++ b/vm/src/types.rs @@ -3,6 +3,7 @@ mod vm_code_metadata; mod vm_esdt_local_role; mod vm_esdt_local_role_flags; mod vm_h256; +mod vm_token_type; use num_bigint::BigUint; use num_traits::Zero; @@ -11,6 +12,7 @@ pub use vm_code_metadata::VMCodeMetadata; pub use vm_esdt_local_role::EsdtLocalRole; pub use vm_esdt_local_role_flags::EsdtLocalRoleFlags; pub use vm_h256::H256; +pub use vm_token_type::VMTokenType; pub type RawHandle = i32; diff --git a/vm/src/types/vm_token_type.rs b/vm/src/types/vm_token_type.rs new file mode 100644 index 0000000000..18670a90dc --- /dev/null +++ b/vm/src/types/vm_token_type.rs @@ -0,0 +1,19 @@ +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum VMTokenType { + Fungible, + SemiFungible, + Meta, + NonFungible, +} + +impl VMTokenType { + pub fn from_system_sc_arg(raw: &[u8]) -> Self { + match raw { + b"FNG" => VMTokenType::Fungible, + b"SFT" => VMTokenType::SemiFungible, + b"META" => VMTokenType::Meta, + b"NFT" => VMTokenType::NonFungible, + _ => panic!("invalid token type"), + } + } +} diff --git a/vm/src/world_mock/esdt_data.rs b/vm/src/world_mock/esdt_data.rs index 6e44782af9..4d7093aa2e 100644 --- a/vm/src/world_mock/esdt_data.rs +++ b/vm/src/world_mock/esdt_data.rs @@ -1,7 +1,7 @@ use num_bigint::BigUint; use num_traits::Zero; -use crate::display_util::key_hex; +use crate::{display_util::key_hex, types::VMTokenType}; use std::{ collections::{hash_map::Iter, HashMap}, fmt::{self, Write}, @@ -161,7 +161,7 @@ impl AccountEsdt { } } - pub fn register_and_set_roles(&mut self, token_identifier: &[u8], token_type: &str) { + pub fn register_and_set_roles(&mut self, token_identifier: &[u8], token_type: VMTokenType) { self.issue_token(token_identifier); self.set_roles( token_identifier.to_vec(), @@ -181,25 +181,24 @@ impl AccountEsdt { ); } - fn get_all_roles_for_token_type(token_type: &str) -> Vec> { + fn get_all_roles_for_token_type(token_type: VMTokenType) -> Vec> { match token_type { - "NFT" => vec![ + VMTokenType::NonFungible => vec![ "ESDTRoleNFTCreate".as_bytes().to_vec(), "ESDTRoleNFTBurn".as_bytes().to_vec(), "ESDTRoleNFTUpdateAttributes".as_bytes().to_vec(), "ESDTRoleNFTAddURI".as_bytes().to_vec(), ], - "SFT" | "META" => vec![ + VMTokenType::SemiFungible | VMTokenType::Meta => vec![ "ESDTRoleNFTCreate".as_bytes().to_vec(), "ESDTRoleNFTBurn".as_bytes().to_vec(), "ESDTRoleNFTAddQuantity".as_bytes().to_vec(), ], - "FNG" => vec![ + VMTokenType::Fungible => vec![ "ESDTRoleLocalMint".as_bytes().to_vec(), "ESDTRoleLocalBurn".as_bytes().to_vec(), "ESDTRoleLocalTransfer".as_bytes().to_vec(), ], - _ => panic!("invalid token type"), } } } From 6fa2d6752a1ccdf3b86e1db36701670b5496fb61 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 10 Jul 2023 17:11:59 +0300 Subject: [PATCH 16/17] clippy fix --- vm/src/tx_mock/tx_cache_balance_util.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vm/src/tx_mock/tx_cache_balance_util.rs b/vm/src/tx_mock/tx_cache_balance_util.rs index 692a24f158..97eb5fb1a5 100644 --- a/vm/src/tx_mock/tx_cache_balance_util.rs +++ b/vm/src/tx_mock/tx_cache_balance_util.rs @@ -92,10 +92,10 @@ impl TxCache { to: &VMAddress, value: &BigUint, ) -> Result<(), TxPanic> { - if !is_system_sc_address(&from) { + if !is_system_sc_address(from) { self.subtract_egld_balance(from, value)?; } - if !is_system_sc_address(&to) { + if !is_system_sc_address(to) { self.increase_egld_balance(to, value); } Ok(()) @@ -109,7 +109,7 @@ impl TxCache { nonce: u64, value: &BigUint, ) -> Result<(), TxPanic> { - if !is_system_sc_address(&from) && !is_system_sc_address(&to) { + if !is_system_sc_address(from) && !is_system_sc_address(to) { let metadata = self.subtract_esdt_balance(from, esdt_token_identifier, nonce, value)?; self.increase_esdt_balance(to, esdt_token_identifier, nonce, value, metadata); } From 715b0a93fb36cd751c725a692534de6fd4edf0ee Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Mon, 10 Jul 2023 17:37:54 +0300 Subject: [PATCH 17/17] fix after merge - BlockchainUpdate apply --- vm/src/tx_mock/blockchain_update.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/vm/src/tx_mock/blockchain_update.rs b/vm/src/tx_mock/blockchain_update.rs index 46ffb7c880..0c78626a93 100644 --- a/vm/src/tx_mock/blockchain_update.rs +++ b/vm/src/tx_mock/blockchain_update.rs @@ -18,5 +18,9 @@ impl BlockchainUpdate { pub fn apply(self, blockchain: &mut BlockchainState) { blockchain.update_accounts(self.accounts); + + if let Some(token_identifiers) = self.new_token_identifiers { + blockchain.update_new_token_identifiers(token_identifiers); + } } }