From ebb2edfae6b3b8fb5396d452e30594d0931bb7f0 Mon Sep 17 00:00:00 2001 From: Michael Birch Date: Tue, 1 Jun 2021 12:04:18 -0400 Subject: [PATCH 01/37] Remove the dependency on Lunarity. (#115) --- Cargo.lock | 304 ++++++++++++---------------- Cargo.toml | 4 +- etc/state-migration-test/Cargo.lock | 135 +++++------- src/meta_parsing.rs | 272 +++++++++++++++++++------ 4 files changed, 389 insertions(+), 326 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9b207e93e..bb365821e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -86,9 +86,9 @@ version = "0.1.42" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d3a45e77e34375a7923b1e8febb049bb011f064714a8e17a1a616fef01da13d" dependencies = [ - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -129,7 +129,7 @@ dependencies = [ "git2", "hex", "libsecp256k1", - "lunarity-lexer", + "logos", "near-crypto 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "near-primitives-core 0.1.0 (git+https://github.com/near/nearcore?rev=3744f07e13bf43a9522fb39fa8f6f128396d0e1f)", "near-sdk", @@ -180,6 +180,12 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" +[[package]] +name = "beef" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6736e2428df2ca2848d846c43e88745121a6654696e349ce0054a420815a7409" + [[package]] name = "bincode" version = "1.3.3" @@ -201,8 +207,8 @@ dependencies = [ "lazy_static", "lazycell", "peeking_take_while", - "proc-macro2 1.0.26", - "quote 1.0.9", + "proc-macro2", + "quote", "regex", "rustc-hash", "shlex", @@ -318,8 +324,8 @@ dependencies = [ "borsh-derive-internal", "borsh-schema-derive-internal", "proc-macro-crate", - "proc-macro2 1.0.26", - "syn 1.0.57", + "proc-macro2", + "syn", ] [[package]] @@ -328,9 +334,9 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2104c73179359431cc98e016998f2f23bc7a05bc53e79741bcba705f30047bc" dependencies = [ - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -339,9 +345,9 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae29eb8418fcd46f723f8691a2ac06857d31179d33d2f2d91eb13967de97c728" dependencies = [ - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -426,8 +432,8 @@ dependencies = [ "async-mutex", "cached_proc_macro_types", "darling 0.10.2", - "quote 1.0.9", - "syn 1.0.57", + "quote", + "syn", ] [[package]] @@ -992,10 +998,10 @@ checksum = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b" dependencies = [ "fnv", "ident_case", - "proc-macro2 1.0.26", - "quote 1.0.9", + "proc-macro2", + "quote", "strsim 0.9.3", - "syn 1.0.57", + "syn", ] [[package]] @@ -1006,10 +1012,10 @@ checksum = "8e91455b86830a1c21799d94524df0845183fa55bafd9aa137b01c7d1065fa36" dependencies = [ "fnv", "ident_case", - "proc-macro2 1.0.26", - "quote 1.0.9", + "proc-macro2", + "quote", "strsim 0.10.0", - "syn 1.0.57", + "syn", ] [[package]] @@ -1019,8 +1025,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72" dependencies = [ "darling_core 0.10.2", - "quote 1.0.9", - "syn 1.0.57", + "quote", + "syn", ] [[package]] @@ -1030,8 +1036,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29b5acf0dea37a7f66f7b25d2c5e93fd46f8f6968b1a5d7a3e02e97768afc95a" dependencies = [ "darling_core 0.12.4", - "quote 1.0.9", - "syn 1.0.57", + "quote", + "syn", ] [[package]] @@ -1041,9 +1047,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f82b1b72f1263f214c0f823371768776c4f5841b942c9883aa8e5ec584fd0ba6" dependencies = [ "convert_case", - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -1074,9 +1080,9 @@ dependencies = [ "byteorder", "lazy_static", "proc-macro-error", - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -1096,8 +1102,8 @@ version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62473f23f4a15690bd64ea37269266ba7b273e7c26b302aef68d3968ae9262a0" dependencies = [ - "quote 1.0.9", - "syn 1.0.57", + "quote", + "syn", ] [[package]] @@ -1154,9 +1160,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e19c52f9ec503c8a68dc04daf71a04b07e690c32ab1a8b68e33897f255269d47" dependencies = [ "darling 0.12.4", - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -1406,9 +1412,9 @@ checksum = "a4c40298486cdf52cc00cd6d6987892ba502c7656a16a4192a9992b1ccedd121" dependencies = [ "autocfg", "proc-macro-hack", - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -1444,15 +1450,6 @@ dependencies = [ "slab", ] -[[package]] -name = "fxhash" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" -dependencies = [ - "byteorder", -] - [[package]] name = "gcc" version = "0.3.55" @@ -1787,7 +1784,7 @@ checksum = "6002fe04202bdaf9e8d82929a7c9ebfcf47d027d87f671818e8cf9ccb4029908" dependencies = [ "lazy_static", "manifest-dir-macros", - "syn 1.0.57", + "syn", ] [[package]] @@ -1931,35 +1928,28 @@ dependencies = [ [[package]] name = "logos" -version = "0.7.7" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60ca690691528b32832c7e8aaae8ae1edcdee4e9ffde55b2d31a4795bc7a12d0" +checksum = "427e2abca5be13136da9afdbf874e6b34ad9001dd70f2b103b083a85daa7b345" dependencies = [ "logos-derive", - "toolshed", ] [[package]] name = "logos-derive" -version = "0.7.7" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "917dccdd529d5681f3d28b26bcfdafd2ed67fe4f26d15b5ac679f67b55279f3d" +checksum = "56a7d287fd2ac3f75b11f19a1c8a874a7d55744bd91f7a1b3e7cf87d4343c36d" dependencies = [ - "proc-macro2 0.4.30", - "quote 0.6.13", + "beef", + "fnv", + "proc-macro2", + "quote", "regex-syntax", - "syn 0.15.44", + "syn", "utf8-ranges", ] -[[package]] -name = "lunarity-lexer" -version = "0.2.1" -source = "git+https://github.com/ilblackdragon/lunarity?rev=5201d9a76f7e491082b7f74af7e64049271e387f#5201d9a76f7e491082b7f74af7e64049271e387f" -dependencies = [ - "logos", -] - [[package]] name = "mach" version = "0.3.2" @@ -1976,9 +1966,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca7bbc41d799583acd24ed05a9c3db3c9275c93491b4e7cde0e609bb9598f2f0" dependencies = [ "once_cell", - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -2245,10 +2235,10 @@ name = "near-rpc-error-core" version = "0.1.0" source = "git+https://github.com/near/nearcore?rev=3744f07e13bf43a9522fb39fa8f6f128396d0e1f#3744f07e13bf43a9522fb39fa8f6f128396d0e1f" dependencies = [ - "proc-macro2 1.0.26", - "quote 1.0.9", + "proc-macro2", + "quote", "serde", - "syn 1.0.57", + "syn", ] [[package]] @@ -2257,11 +2247,11 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffa8dbf8437a28ac40fcb85859ab0d0b8385013935b000c7a51ae79631dd74d9" dependencies = [ - "proc-macro2 1.0.26", - "quote 1.0.9", + "proc-macro2", + "quote", "serde", "serde_json", - "syn 1.0.57", + "syn", ] [[package]] @@ -2270,11 +2260,11 @@ version = "0.1.0" source = "git+https://github.com/near/nearcore?rev=3744f07e13bf43a9522fb39fa8f6f128396d0e1f#3744f07e13bf43a9522fb39fa8f6f128396d0e1f" dependencies = [ "near-rpc-error-core 0.1.0 (git+https://github.com/near/nearcore?rev=3744f07e13bf43a9522fb39fa8f6f128396d0e1f)", - "proc-macro2 1.0.26", - "quote 1.0.9", + "proc-macro2", + "quote", "serde", "serde_json", - "syn 1.0.57", + "syn", ] [[package]] @@ -2284,11 +2274,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c6111d713e90c7c551dee937f4a06cb9ea2672243455a4454cc7566387ba2d9" dependencies = [ "near-rpc-error-core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "proc-macro2 1.0.26", - "quote 1.0.9", + "proc-macro2", + "quote", "serde", "serde_json", - "syn 1.0.57", + "syn", ] [[package]] @@ -2370,9 +2360,9 @@ version = "3.0.0-pre.3" source = "git+https://github.com/near/near-sdk-rs?rev=9d99077c6acfde68c06845f2a1eb2b5ed7983401#9d99077c6acfde68c06845f2a1eb2b5ed7983401" dependencies = [ "Inflector", - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -2381,9 +2371,9 @@ version = "3.0.0-pre.3" source = "git+https://github.com/near/near-sdk-rs?rev=9d99077c6acfde68c06845f2a1eb2b5ed7983401#9d99077c6acfde68c06845f2a1eb2b5ed7983401" dependencies = [ "near-sdk-core", - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -2806,9 +2796,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f44c5f94427bd0b5076e8f7e15ca3f60a4d8ac0077e4793884e6fdfd8915344e" dependencies = [ "proc-macro-crate", - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -2977,9 +2967,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" dependencies = [ "proc-macro-error-attr", - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", "version_check", ] @@ -2989,8 +2979,8 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" dependencies = [ - "proc-macro2 1.0.26", - "quote 1.0.9", + "proc-macro2", + "quote", "version_check", ] @@ -3006,22 +2996,13 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc881b2c22681370c6a780e47af9840ef841837bc98118431d4e1868bd0c1086" -[[package]] -name = "proc-macro2" -version = "0.4.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" -dependencies = [ - "unicode-xid 0.1.0", -] - [[package]] name = "proc-macro2" version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec" dependencies = [ - "unicode-xid 0.2.2", + "unicode-xid", ] [[package]] @@ -3065,22 +3046,13 @@ dependencies = [ "parity-wasm", ] -[[package]] -name = "quote" -version = "0.6.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1" -dependencies = [ - "proc-macro2 0.4.30", -] - [[package]] name = "quote" version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" dependencies = [ - "proc-macro2 1.0.26", + "proc-macro2", ] [[package]] @@ -3333,9 +3305,9 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e33d7b2abe0c340d8797fe2907d3f20d3b5ea5908683618bfe80df7f621f672a" dependencies = [ - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -3455,9 +3427,9 @@ version = "1.0.118" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c84d3526699cd55261af4b941e4e725444df67aa4f9e6a3564f18030d12672df" dependencies = [ - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -3552,9 +3524,9 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "133659a15339456eeeb07572eb02a91c91e9815e9cbc89566944d2c8d3efdbf6" dependencies = [ - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -3597,9 +3569,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ee8bc6b87a5112aeeab1f4a9f7ab634fe6cbefc4850006df31267f4cfb9e3149" dependencies = [ "heck", - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -3614,26 +3586,15 @@ version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e81da0851ada1f3e9d4312c704aa4f8806f0f9d69faaf8df2f3464b4a9437c2" -[[package]] -name = "syn" -version = "0.15.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" -dependencies = [ - "proc-macro2 0.4.30", - "quote 0.6.13", - "unicode-xid 0.1.0", -] - [[package]] name = "syn" version = "1.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4211ce9909eb971f111059df92c45640aad50a619cf55cd76476be803c4c68e6" dependencies = [ - "proc-macro2 1.0.26", - "quote 1.0.9", - "unicode-xid 0.2.2", + "proc-macro2", + "quote", + "unicode-xid", ] [[package]] @@ -3642,10 +3603,10 @@ version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" dependencies = [ - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", - "unicode-xid 0.2.2", + "proc-macro2", + "quote", + "syn", + "unicode-xid", ] [[package]] @@ -3704,9 +3665,9 @@ version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7765189610d8241a44529806d6fd1f2e0a08734313a35d5b3a556f92b381f3c0" dependencies = [ - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -3771,15 +3732,6 @@ dependencies = [ "serde", ] -[[package]] -name = "toolshed" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54a272adbf14cfbb486774d09ee3e00c38d488cd390084a528f70e10e3a184a8" -dependencies = [ - "fxhash", -] - [[package]] name = "tracing" version = "0.1.26" @@ -3798,9 +3750,9 @@ version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c42e6fa53307c8a17e4ccd4dc81cf5ec38db9209f59b222210375b54ee40d1e2" dependencies = [ - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -3870,12 +3822,6 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" -[[package]] -name = "unicode-xid" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" - [[package]] name = "unicode-xid" version = "0.2.2" @@ -3982,9 +3928,9 @@ dependencies = [ "bumpalo", "lazy_static", "log", - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", "wasm-bindgen-shared", ] @@ -3994,7 +3940,7 @@ version = "0.2.72" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ef9aa01d36cda046f797c57959ff5f3c615c9cc63997a8d545831ec7976819b" dependencies = [ - "quote 1.0.9", + "quote", "wasm-bindgen-macro-support", ] @@ -4004,9 +3950,9 @@ version = "0.2.72" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96eb45c1b2ee33545a813a92dbb53856418bf7eb54ab34f7f7ff1448a5b3735d" dependencies = [ - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -4102,9 +4048,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8b86dcd2c3efdb8390728a2b56f762db07789aaa5aa872a9dc776ba3a7912ed" dependencies = [ "proc-macro-error", - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -4726,8 +4672,8 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2c1e130bebaeab2f23886bf9acbaca14b092408c452543c857f66399cd6dab1" dependencies = [ - "proc-macro2 1.0.26", - "quote 1.0.9", - "syn 1.0.57", + "proc-macro2", + "quote", + "syn", "synstructure", ] diff --git a/Cargo.toml b/Cargo.toml index 0d91f3bd6..750a3ab1c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,7 +51,7 @@ rlp = { version = "0.5.0", default-features = false } sha2 = { version = "0.9.3", default-features = false, optional = true } sha3 = { version = "0.9.1", default-features = false } wee_alloc = { version = "0.4.5", default-features = false } -lunarity-lexer = { git = "https://github.com/ilblackdragon/lunarity", rev = "5201d9a76f7e491082b7f74af7e64049271e387f", default-features = false } +logos = { version = "0.12", default-features = false, features = ["export_derive"] } ethabi = { git = "https://github.com/darwinia-network/ethabi", branch = "xavier-no-std", default-features = false } hex = { version = "0.4", default-features = false, features = ["alloc"] } byte-slice-cast = { version = "1.0", default-features = false } @@ -73,7 +73,7 @@ git2 = "0.13" [features] default = ["sha2", "std"] -std = ["borsh/std", "evm/std", "primitive-types/std", "rlp/std", "sha3/std", "ethabi/std", "lunarity-lexer/std", "bn/std"] +std = ["borsh/std", "evm/std", "primitive-types/std", "rlp/std", "sha3/std", "ethabi/std", "logos/std", "bn/std"] testnet = [] engine = [] contract = ["engine"] diff --git a/etc/state-migration-test/Cargo.lock b/etc/state-migration-test/Cargo.lock index 41d496c5b..0e3b2666e 100644 --- a/etc/state-migration-test/Cargo.lock +++ b/etc/state-migration-test/Cargo.lock @@ -42,7 +42,7 @@ dependencies = [ "evm-core", "hex", "libsecp256k1", - "lunarity-lexer", + "logos", "num", "primitive-types", "ripemd160", @@ -67,6 +67,12 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +[[package]] +name = "beef" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6736e2428df2ca2848d846c43e88745121a6654696e349ce0054a420815a7409" + [[package]] name = "blake2" version = "0.9.1" @@ -133,8 +139,8 @@ dependencies = [ "borsh-derive-internal", "borsh-schema-derive-internal", "proc-macro-crate", - "proc-macro2 1.0.27", - "syn 1.0.72", + "proc-macro2", + "syn", ] [[package]] @@ -143,9 +149,9 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2104c73179359431cc98e016998f2f23bc7a05bc53e79741bcba705f30047bc" dependencies = [ - "proc-macro2 1.0.27", - "quote 1.0.9", - "syn 1.0.72", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -154,16 +160,16 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae29eb8418fcd46f723f8691a2ac06857d31179d33d2f2d91eb13967de97c728" dependencies = [ - "proc-macro2 1.0.27", - "quote 1.0.9", - "syn 1.0.72", + "proc-macro2", + "quote", + "syn", ] [[package]] name = "bumpalo" -version = "3.7.0" +version = "3.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c59e7af012c713f529e7a3ee57ce9b31ddd858d4b512923602f74608b009631" +checksum = "63396b8a4b9de3f4fdfb320ab6080762242f66a8ef174c49d8e19b674db4cdbe" [[package]] name = "byte-slice-cast" @@ -356,6 +362,12 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "funty" version = "1.1.0" @@ -383,9 +395,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.3" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" dependencies = [ "cfg-if 1.0.0", "js-sys", @@ -456,9 +468,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.95" +version = "0.2.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "789da6d93f1b866ffe175afc5322a4d76c038605a1c3319bb57b06967ca98a36" +checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e" [[package]] name = "libsecp256k1" @@ -484,34 +496,28 @@ dependencies = [ [[package]] name = "logos" -version = "0.7.7" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60ca690691528b32832c7e8aaae8ae1edcdee4e9ffde55b2d31a4795bc7a12d0" +checksum = "427e2abca5be13136da9afdbf874e6b34ad9001dd70f2b103b083a85daa7b345" dependencies = [ "logos-derive", ] [[package]] name = "logos-derive" -version = "0.7.7" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "917dccdd529d5681f3d28b26bcfdafd2ed67fe4f26d15b5ac679f67b55279f3d" +checksum = "56a7d287fd2ac3f75b11f19a1c8a874a7d55744bd91f7a1b3e7cf87d4343c36d" dependencies = [ - "proc-macro2 0.4.30", - "quote 0.6.13", + "beef", + "fnv", + "proc-macro2", + "quote", "regex-syntax", - "syn 0.15.44", + "syn", "utf8-ranges", ] -[[package]] -name = "lunarity-lexer" -version = "0.2.1" -source = "git+https://github.com/ilblackdragon/lunarity?rev=5201d9a76f7e491082b7f74af7e64049271e387f#5201d9a76f7e491082b7f74af7e64049271e387f" -dependencies = [ - "logos", -] - [[package]] name = "memory_units" version = "0.4.0" @@ -634,29 +640,11 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "0.4.30" +version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" +checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec" dependencies = [ - "unicode-xid 0.1.0", -] - -[[package]] -name = "proc-macro2" -version = "1.0.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038" -dependencies = [ - "unicode-xid 0.2.2", -] - -[[package]] -name = "quote" -version = "0.6.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1" -dependencies = [ - "proc-macro2 0.4.30", + "unicode-xid", ] [[package]] @@ -665,7 +653,7 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" dependencies = [ - "proc-macro2 1.0.27", + "proc-macro2", ] [[package]] @@ -743,9 +731,9 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e33d7b2abe0c340d8797fe2907d3f20d3b5ea5908683618bfe80df7f621f672a" dependencies = [ - "proc-macro2 1.0.27", - "quote 1.0.9", - "syn 1.0.72", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -810,26 +798,15 @@ version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e81da0851ada1f3e9d4312c704aa4f8806f0f9d69faaf8df2f3464b4a9437c2" -[[package]] -name = "syn" -version = "0.15.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" -dependencies = [ - "proc-macro2 0.4.30", - "quote 0.6.13", - "unicode-xid 0.1.0", -] - [[package]] name = "syn" version = "1.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1e8cdbefb79a9a5a65e0db8b47b723ee907b7c7f8496c76a1770b5c310bab82" dependencies = [ - "proc-macro2 1.0.27", - "quote 1.0.9", - "unicode-xid 0.2.2", + "proc-macro2", + "quote", + "unicode-xid", ] [[package]] @@ -878,12 +855,6 @@ dependencies = [ "static_assertions", ] -[[package]] -name = "unicode-xid" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" - [[package]] name = "unicode-xid" version = "0.2.2" @@ -927,9 +898,9 @@ dependencies = [ "bumpalo", "lazy_static", "log", - "proc-macro2 1.0.27", - "quote 1.0.9", - "syn 1.0.72", + "proc-macro2", + "quote", + "syn", "wasm-bindgen-shared", ] @@ -939,7 +910,7 @@ version = "0.2.74" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "088169ca61430fe1e58b8096c24975251700e7b1f6fd91cc9d59b04fb9b18bd4" dependencies = [ - "quote 1.0.9", + "quote", "wasm-bindgen-macro-support", ] @@ -949,9 +920,9 @@ version = "0.2.74" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be2241542ff3d9f241f5e2cb6dd09b37efe786df8851c54957683a49f0987a97" dependencies = [ - "proc-macro2 1.0.27", - "quote 1.0.9", - "syn 1.0.72", + "proc-macro2", + "quote", + "syn", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/src/meta_parsing.rs b/src/meta_parsing.rs index 6b5dfcbfa..a227e0a10 100644 --- a/src/meta_parsing.rs +++ b/src/meta_parsing.rs @@ -1,6 +1,6 @@ use borsh::BorshDeserialize; use ethabi::{encode, Token as ABIToken}; -use lunarity_lexer::{Lexer, Token}; +use logos::Logos; use rlp::{Decodable, DecoderError, Rlp}; use crate::parameters::MetaCallArgs; @@ -18,6 +18,70 @@ pub enum ParsingError { pub type ParsingResult = core::result::Result; +mod type_lexer { + use logos::{Lexer, Logos}; + + #[derive(Logos, Debug, PartialEq)] + pub(super) enum Token { + #[regex("byte|bytes[1-2][0-9]?|bytes3[0-2]?|bytes[4-9]", fixed_bytes_size)] + FixedBytes(u8), + #[regex("uint(8|16|24|32|40|48|56|64|72|80|88|96|104|112|120|128|136|144|152|160|168|176|184|192|200|208|216|224|232|240|248|256)?", |lex| fixed_int_size(lex, "uint"))] + Uint(usize), + #[regex("int(8|16|24|32|40|48|56|64|72|80|88|96|104|112|120|128|136|144|152|160|168|176|184|192|200|208|216|224|232|240|248|256)?", |lex| fixed_int_size(lex, "int"))] + Int(usize), + #[regex("bool")] + Bool, + #[regex("address")] + Address, + #[regex("bytes")] + Bytes, + #[regex("string")] + String, + #[regex("\\[[0-9]*\\]", reference_type_size)] + ReferenceType(Option), + #[regex("[a-zA-Z_$][a-zA-Z0-9_$]*")] + Identifier, + + #[error] + Error, + } + + fn fixed_bytes_size(lex: &mut Lexer) -> u8 { + let slice = lex.slice(); + + if slice == "byte" { + return 1; + } + + let n = slice["bytes".len()..].parse(); + n.ok().unwrap_or(1) + } + + fn fixed_int_size(lex: &mut Lexer, prefix: &str) -> usize { + let slice = lex.slice(); + + if slice == prefix { + // the default int size is 32 + return 32; + } + + let n = slice[prefix.len()..].parse(); + n.unwrap_or(32) + } + + fn reference_type_size(lex: &mut Lexer) -> Option { + let slice = lex.slice(); + + if slice == "[]" { + return None; + } + + let end_index = slice.len() - 1; + let n = slice[1..end_index].parse(); + n.ok() + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub enum ArgType { Address, @@ -38,71 +102,35 @@ pub enum ArgType { /// field_type: A single evm function arg type in string, without the argument name /// e.g. "bytes" "uint256[][3]" "CustomStructName" pub fn parse_type(field_type: &str) -> ParsingResult { - #[derive(PartialEq)] - enum State { - Open, - Close, - } - - let mut lexer = Lexer::new(field_type); - let mut token = None; - let mut state = State::Close; - let mut array_depth = 0; - let mut current_array_length: Option = None; - - while lexer.token != Token::EndOfProgram { - let type_ = match lexer.token { - Token::Identifier => ArgType::Custom(lexer.slice().to_owned()), - Token::TypeByte => ArgType::Byte(lexer.extras.0), - Token::TypeBytes => ArgType::Bytes, - Token::TypeBool => ArgType::Bool, - Token::TypeUint => ArgType::Uint, - Token::TypeInt => ArgType::Int, - Token::TypeString => ArgType::String, - Token::TypeAddress => ArgType::Address, - Token::LiteralInteger => { - let length = lexer.slice(); - current_array_length = Some( - length - .parse() - .map_err(|_| ParsingError::InvalidMetaTransactionMethodName)?, - ); - lexer.advance(); - continue; - } - Token::BracketOpen if token.is_some() && state == State::Close => { - state = State::Open; - lexer.advance(); - continue; - } - Token::BracketClose if array_depth < 10 => { - if state == State::Open && token.is_some() { - let length = current_array_length.take(); - state = State::Close; - token = Some(ArgType::Array { - inner: Box::new(token.expect("if statement checks for some; qed")), - length, - }); - lexer.advance(); - array_depth += 1; - continue; - } else { - return Err(ParsingError::InvalidMetaTransactionMethodName); - } - } - Token::BracketClose if array_depth == 10 => { - return Err(ParsingError::InvalidMetaTransactionMethodName); - } - _ => { - return Err(ParsingError::InvalidMetaTransactionMethodName); - } + let mut lexer = type_lexer::Token::lexer(field_type); + let mut current_token = lexer.next(); + let mut inner_type: Option = None; + + loop { + let typ = match current_token { + None => break, + Some(type_lexer::Token::Address) => ArgType::Address, + Some(type_lexer::Token::Bool) => ArgType::Bool, + Some(type_lexer::Token::String) => ArgType::String, + Some(type_lexer::Token::Bytes) => ArgType::Bytes, + Some(type_lexer::Token::Identifier) => ArgType::Custom(lexer.slice().to_owned()), + Some(type_lexer::Token::FixedBytes(size)) => ArgType::Byte(size), + Some(type_lexer::Token::Int(_)) => ArgType::Int, + Some(type_lexer::Token::Uint(_)) => ArgType::Uint, + Some(type_lexer::Token::ReferenceType(length)) => match inner_type { + None => return Err(ParsingError::ArgumentParseError), + Some(t) => ArgType::Array { + length, + inner: Box::new(t), + }, + }, + Some(type_lexer::Token::Error) => return Err(ParsingError::ArgumentParseError), }; - - token = Some(type_); - lexer.advance(); + inner_type = Some(typ); + current_token = lexer.next(); } - token.ok_or(ParsingError::InvalidMetaTransactionMethodName) + inner_type.ok_or(ParsingError::ArgumentParseError) } /// NEAR's domainSeparator @@ -575,3 +603,121 @@ pub fn parse_meta_call( Err(_) => Err(ParsingError::InvalidEcRecoverSignature), } } + +#[cfg(test)] +mod tests { + use super::ArgType; + use rand::Rng; + + #[test] + fn test_parse_type() { + // # atomic types + + // ## bytesN + for n in 1..=32 { + let s = format!("bytes{}", n); + assert_arg_type(&s, ArgType::Byte(n)); + } + assert_arg_type("byte", ArgType::Byte(1)); + + // ## uintN + for n in 1..=32 { + let s = format!("uint{}", 8 * n); + assert_arg_type(&s, ArgType::Uint); + } + assert_arg_type("uint", ArgType::Uint); + + // ## intN + for n in 1..=32 { + let s = format!("int{}", 8 * n); + assert_arg_type(&s, ArgType::Int); + } + assert_arg_type("int", ArgType::Int); + + // ## bool + assert_arg_type("bool", ArgType::Bool); + + // ## address + assert_arg_type("address", ArgType::Address); + + // ## custom + let mut rng = rand::thread_rng(); + for _ in 0..u8::MAX { + let name = rand_identifier(&mut rng); + assert_arg_type(&name, ArgType::Custom(name.clone())); + } + + // # dynamic types + + // ## bytes + assert_arg_type("bytes", ArgType::Bytes); + + // ## string + assert_arg_type("string", ArgType::String); + + // # arrays + let inner_types: Vec = (1..=32) + .map(|n| format!("bytes{}", n)) + .chain((1..=32).map(|n| format!("uint{}", 8 * n))) + .chain((1..=32).map(|n| format!("int{}", 8 * n))) + .chain(std::iter::once("bool".to_string())) + .chain(std::iter::once("address".to_string())) + .chain(std::iter::once(rand_identifier(&mut rng))) + .chain(std::iter::once("bytes".to_string())) + .chain(std::iter::once("string".to_string())) + .collect(); + for t in inner_types { + let inner_type = super::parse_type(&t).ok().unwrap(); + let size: Option = rng.gen(); + + // single array + let single_array_string = create_array_type_string(&t, size); + let expected = ArgType::Array { + length: size.map(|x| x as u64), + inner: Box::new(inner_type), + }; + assert_arg_type(&single_array_string, expected.clone()); + + // nested array + let inner_type = expected; + let size: Option = rng.gen(); + let nested_array_string = create_array_type_string(&single_array_string, size); + let expected = ArgType::Array { + length: size.map(|x| x as u64), + inner: Box::new(inner_type), + }; + assert_arg_type(&nested_array_string, expected); + } + + // # errors + // ## only numbers + super::parse_type("27182818").unwrap_err(); + // ## invalid characters + super::parse_type("Some.InvalidType").unwrap_err(); + super::parse_type("Some::NotType").unwrap_err(); + super::parse_type("*AThing*").unwrap_err(); + } + + fn create_array_type_string(inner_type: &str, size: Option) -> String { + format!( + "{}[{}]", + inner_type, + size.map(|x| x.to_string()).unwrap_or(String::new()) + ) + } + + fn assert_arg_type(s: &str, expected: ArgType) { + assert_eq!(super::parse_type(s).ok().unwrap(), expected); + } + + fn rand_identifier(rng: &mut T) -> String { + use rand::distributions::Alphanumeric; + use rand::seq::IteratorRandom; + + // The first character must be a letter, so we sample that separately. + let first_char = ('a'..='z').chain('A'..='Z').choose(rng).unwrap(); + let other_letters = (0..7).map(|_| char::from(rng.sample(Alphanumeric))); + + std::iter::once(first_char).chain(other_letters).collect() + } +} From 418a47348a44ee490e561f7ecb98630991e3bb1b Mon Sep 17 00:00:00 2001 From: Michael Birch Date: Tue, 1 Jun 2021 15:07:14 -0400 Subject: [PATCH 02/37] Fix Clippy complaint with +nightly. (#117) --- src/types.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.rs b/src/types.rs index b31f7c0f5..1137c6b0f 100644 --- a/src/types.rs +++ b/src/types.rs @@ -45,7 +45,7 @@ impl Wei { amount.checked_mul(Self::ETH_TO_WEI).map(Self) } - pub fn to_bytes(&self) -> [u8; 32] { + pub fn to_bytes(self) -> [u8; 32] { u256_to_arr(&self.0) } From 0f11b379605e8fde9f54e01f096c5b985df3c262 Mon Sep 17 00:00:00 2001 From: Frank Braun Date: Fri, 4 Jun 2021 08:55:50 +0000 Subject: [PATCH 03/37] Add an `evm-bully=yes` build to CI. (#121) --- .github/workflows/tests.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 5b1048175..9c2b0364d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -28,6 +28,20 @@ jobs: with: command: test args: --locked --verbose + bully-build: + name: Bully build + runs-on: ubuntu-latest + steps: + - name: Clone the repository + uses: actions/checkout@v2 + - name: Install the toolchain + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: nightly-2021-03-25 + override: true + - run: make evm-bully=yes + - run: ls -lH release.wasm env: CARGO_TERM_COLOR: always CARGO_INCREMENTAL: 0 From 544ad9273e1185ed6ac4042d6b37f13e5c6b291a Mon Sep 17 00:00:00 2001 From: Michael Birch Date: Fri, 4 Jun 2021 09:29:19 -0400 Subject: [PATCH 04/37] Fix evm-bully builds. (#130) --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index eb4cb5364..293ad0658 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -353,7 +353,7 @@ mod contract { ) } // return new chain ID - sdk::return_output(&Engine::get_state().chain_id) + sdk::return_output(&Engine::get_state().sdk_unwrap().chain_id) } #[cfg(feature = "evm_bully")] From aaeec7695760e0bab78679099180c897ead4ac72 Mon Sep 17 00:00:00 2001 From: Michael Birch Date: Fri, 4 Jun 2021 18:48:24 -0400 Subject: [PATCH 05/37] Handle transaction gas limit properly. (#123) --- src/engine.rs | 32 ++++++---- src/lib.rs | 23 ++++++- src/test_utils/erc20.rs | 8 +-- src/test_utils/mod.rs | 2 +- src/test_utils/standard_precompiles.rs | 4 +- src/tests/erc20.rs | 85 ++++++++++++++++++++++++++ src/tests/sanity.rs | 41 +++++++++++++ src/transaction.rs | 32 +++++++++- 8 files changed, 204 insertions(+), 23 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 3391feb9b..3218caf1c 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -145,7 +145,7 @@ pub struct Engine { } // TODO: upgrade to Berlin HF -const CONFIG: &Config = &Config::istanbul(); +pub(crate) const CONFIG: &Config = &Config::istanbul(); /// Key for storing the state of the engine. const STATE_KEY: &[u8; 5] = b"STATE"; @@ -294,7 +294,7 @@ impl Engine { pub fn deploy_code_with_input(&mut self, input: Vec) -> EngineResult { let origin = self.origin(); let value = Wei::zero(); - self.deploy_code(origin, value, input) + self.deploy_code(origin, value, input, u64::MAX) } pub fn deploy_code( @@ -302,15 +302,19 @@ impl Engine { origin: Address, value: Wei, input: Vec, + gas_limit: u64, ) -> EngineResult { - let mut executor = self.make_executor(); + let mut executor = self.make_executor(gas_limit); let address = executor.create_address(CreateScheme::Legacy { caller: origin }); let (status, result) = ( - executor.transact_create(origin, value.raw(), input, u64::MAX), + executor.transact_create(origin, value.raw(), input, gas_limit), address, ); let is_succeed = status.is_succeed(); - status.into_result()?; + if let Err(e) = status.into_result() { + Engine::increment_nonce(&origin); + return Err(e); + } let used_gas = executor.used_gas(); let (values, logs) = executor.into_state().deconstruct(); self.apply(values, Vec::::new(), true); @@ -327,7 +331,7 @@ impl Engine { let origin = self.origin(); let contract = Address(args.contract); let value = Wei::zero(); - self.call(origin, contract, value, args.input) + self.call(origin, contract, value, args.input, u64::MAX) } pub fn call( @@ -336,10 +340,11 @@ impl Engine { contract: Address, value: Wei, input: Vec, + gas_limit: u64, ) -> EngineResult { - let mut executor = self.make_executor(); + let mut executor = self.make_executor(gas_limit); let (status, result) = - executor.transact_call(origin, contract, value.raw(), input, u64::MAX); + executor.transact_call(origin, contract, value.raw(), input, gas_limit); let used_gas = executor.used_gas(); let (values, logs) = executor.into_state().deconstruct(); @@ -372,7 +377,7 @@ impl Engine { let origin = Address::from_slice(&args.sender); let contract = Address::from_slice(&args.address); let value = U256::from_big_endian(&args.amount); - self.view(origin, contract, Wei::new(value), args.input) + self.view(origin, contract, Wei::new(value), args.input, u64::MAX) } pub fn view( @@ -381,16 +386,17 @@ impl Engine { contract: Address, value: Wei, input: Vec, + gas_limit: u64, ) -> EngineResult> { - let mut executor = self.make_executor(); + let mut executor = self.make_executor(gas_limit); let (status, result) = - executor.transact_call(origin, contract, value.raw(), input, u64::MAX); + executor.transact_call(origin, contract, value.raw(), input, gas_limit); status.into_result()?; Ok(result) } - fn make_executor(&self) -> StackExecutor> { - let metadata = StackSubstateMetadata::new(u64::MAX, &CONFIG); + fn make_executor(&self, gas_limit: u64) -> StackExecutor> { + let metadata = StackSubstateMetadata::new(gas_limit, &CONFIG); let state = MemoryStackState::new(metadata, self); StackExecutor::new_with_precompile(state, &CONFIG, precompiles::istanbul_precompiles) } diff --git a/src/lib.rs b/src/lib.rs index 293ad0658..4fec40c07 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -96,6 +96,7 @@ mod contract { const CODE_KEY: &[u8; 4] = b"CODE"; const CODE_STAGE_KEY: &[u8; 10] = b"CODE_STAGE"; + const GAS_OVERFLOW: &str = "ERR_GAS_OVERFLOW"; /// /// ADMINISTRATIVE METHODS @@ -239,16 +240,33 @@ mod contract { Engine::check_nonce(&sender, &signed_transaction.transaction.nonce).sdk_unwrap(); + // Check intrinsic gas is covered by transaction gas limit + match signed_transaction + .transaction + .intrinsic_gas(&crate::engine::CONFIG) + { + None => sdk::panic_utf8(GAS_OVERFLOW.as_bytes()), + Some(intrinsic_gas) => { + if signed_transaction.transaction.gas < intrinsic_gas.into() { + sdk::panic_utf8(b"ERR_INTRINSIC_GAS") + } + } + } + // Figure out what kind of a transaction this is, and execute it: let mut engine = Engine::new_with_state(state, sender); let value = signed_transaction.transaction.value; + let gas_limit = signed_transaction + .transaction + .get_gas_limit() + .sdk_expect(GAS_OVERFLOW); let data = signed_transaction.transaction.data; let result = if let Some(receiver) = signed_transaction.transaction.to { - Engine::call(&mut engine, sender, receiver, value, data) + Engine::call(&mut engine, sender, receiver, value, data, gas_limit) // TODO: charge for storage } else { // Execute a contract deployment: - Engine::deploy_code(&mut engine, sender, value, data) + Engine::deploy_code(&mut engine, sender, value, data, gas_limit) // TODO: charge for storage }; result @@ -276,6 +294,7 @@ mod contract { meta_call_args.contract_address, meta_call_args.value, meta_call_args.input, + u64::MAX, // TODO: is there a gas limit with meta calls? ); result .map(|res| res.try_to_vec().sdk_expect("ERR_SERIALIZE")) diff --git a/src/test_utils/erc20.rs b/src/test_utils/erc20.rs index e9ec02f14..16404cd28 100644 --- a/src/test_utils/erc20.rs +++ b/src/test_utils/erc20.rs @@ -43,7 +43,7 @@ impl ERC20Constructor { EthTransaction { nonce, gas_price: Default::default(), - gas: Default::default(), + gas: u64::MAX.into(), to: None, value: Default::default(), data, @@ -86,7 +86,7 @@ impl ERC20 { EthTransaction { nonce, gas_price: Default::default(), - gas: Default::default(), + gas: u64::MAX.into(), to: Some(self.0.address), value: Default::default(), data, @@ -107,7 +107,7 @@ impl ERC20 { EthTransaction { nonce, gas_price: Default::default(), - gas: Default::default(), + gas: u64::MAX.into(), to: Some(self.0.address), value: Default::default(), data, @@ -125,7 +125,7 @@ impl ERC20 { EthTransaction { nonce, gas_price: Default::default(), - gas: Default::default(), + gas: u64::MAX.into(), to: Some(self.0.address), value: Default::default(), data, diff --git a/src/test_utils/mod.rs b/src/test_utils/mod.rs index 75bd49a95..0bf61ce7e 100644 --- a/src/test_utils/mod.rs +++ b/src/test_utils/mod.rs @@ -299,7 +299,7 @@ pub(crate) fn create_eth_transaction( let tx = EthTransaction { nonce: Default::default(), gas_price: Default::default(), - gas: Default::default(), + gas: u64::MAX.into(), to, value, data, diff --git a/src/test_utils/standard_precompiles.rs b/src/test_utils/standard_precompiles.rs index 681580c37..f76e7d8d4 100644 --- a/src/test_utils/standard_precompiles.rs +++ b/src/test_utils/standard_precompiles.rs @@ -34,7 +34,7 @@ impl PrecompilesConstructor { EthTransaction { nonce, gas_price: Default::default(), - gas: Default::default(), + gas: u64::MAX.into(), to: None, value: Default::default(), data, @@ -62,7 +62,7 @@ impl PrecompilesContract { EthTransaction { nonce, gas_price: Default::default(), - gas: Default::default(), + gas: u64::MAX.into(), to: Some(self.0.address), value: Default::default(), data, diff --git a/src/tests/erc20.rs b/src/tests/erc20.rs index 54769bde8..8f4f506cb 100644 --- a/src/tests/erc20.rs +++ b/src/tests/erc20.rs @@ -46,6 +46,49 @@ fn erc20_mint() { ); } +#[test] +fn erc20_mint_out_of_gas() { + let (mut runner, source_account, dest_address, contract) = initialize_erc20(); + + // Validate pre-state + assert_eq!( + U256::zero(), + get_address_erc20_balance( + &mut runner, + &source_account, + (INITIAL_NONCE + 1).into(), + dest_address, + &contract + ) + ); + + // Try mint transaction + let mint_amount: u64 = rand::random(); + let mut mint_tx = contract.mint(dest_address, mint_amount.into(), (INITIAL_NONCE + 2).into()); + + // not enough gas to cover intrinsic cost + mint_tx.gas = (mint_tx.intrinsic_gas(&evm::Config::istanbul()).unwrap() - 1).into(); + let outcome = runner.submit_transaction(&source_account, mint_tx.clone()); + let error = outcome.unwrap_err(); + let error_message = format!("{:?}", error); + assert!(error_message.contains("ERR_INTRINSIC_GAS")); + + // not enough gas to complete transaction + mint_tx.gas = U256::from(67_000); + let outcome = runner.submit_transaction(&source_account, mint_tx); + let error = outcome.unwrap_err(); + let error_message = format!("{:?}", error); + assert!(error_message.contains("ERR_OUT_OF_GAS")); + + // Validate post-state + test_utils::validate_address_balance_and_nonce( + &runner, + test_utils::address_from_secret_key(&source_account), + Wei::new_u64(INITIAL_BALANCE), + (INITIAL_NONCE + 3).into(), + ); +} + #[test] fn erc20_transfer_success() { let (mut runner, source_account, dest_address, contract) = initialize_erc20(); @@ -186,6 +229,48 @@ fn erc20_transfer_insufficient_balance() { ); } +#[test] +fn deploy_erc_20_out_of_gas() { + let mut runner = test_utils::deploy_evm(); + let mut rng = rand::thread_rng(); + let source_account = SecretKey::random(&mut rng); + let source_address = test_utils::address_from_secret_key(&source_account); + runner.create_address( + source_address, + Wei::new_u64(INITIAL_BALANCE), + INITIAL_NONCE.into(), + ); + + let constructor = ERC20Constructor::load(); + let mut deploy_transaction = constructor.deploy("OutOfGas", "OOG", INITIAL_NONCE.into()); + + // not enough gas to cover intrinsic cost + deploy_transaction.gas = (deploy_transaction + .intrinsic_gas(&evm::Config::istanbul()) + .unwrap() + - 1) + .into(); + let outcome = runner.submit_transaction(&source_account, deploy_transaction.clone()); + let error = outcome.unwrap_err(); + let error_message = format!("{:?}", error); + assert!(error_message.contains("ERR_INTRINSIC_GAS")); + + // not enough gas to complete transaction + deploy_transaction.gas = U256::from(3_200_000); + let outcome = runner.submit_transaction(&source_account, deploy_transaction); + let error = outcome.unwrap_err(); + let error_message = format!("{:?}", error); + assert!(error_message.contains("ERR_OUT_OF_GAS")); + + // Validate post-state + test_utils::validate_address_balance_and_nonce( + &runner, + test_utils::address_from_secret_key(&source_account), + Wei::new_u64(INITIAL_BALANCE), + (INITIAL_NONCE + 1).into(), + ); +} + fn get_address_erc20_balance( runner: &mut test_utils::AuroraRunner, signing_account: &SecretKey, diff --git a/src/tests/sanity.rs b/src/tests/sanity.rs index b89a81887..9a2d756f6 100644 --- a/src/tests/sanity.rs +++ b/src/tests/sanity.rs @@ -135,6 +135,47 @@ fn test_eth_transfer_incorrect_nonce() { test_utils::validate_address_balance_and_nonce(&runner, dest_address, Wei::zero(), 0.into()); } +#[test] +fn test_eth_transfer_not_enough_gas() { + let (mut runner, source_account, dest_address) = initialize_transfer(); + let source_address = test_utils::address_from_secret_key(&source_account); + let transaction = EthTransaction { + nonce: INITIAL_NONCE.into(), + gas_price: Default::default(), + gas: 10_000.into(), // this is not enough gas + to: Some(dest_address), + value: TRANSFER_AMOUNT.into(), + data: vec![], + }; + let transaction = + test_utils::sign_transaction(transaction, Some(runner.chain_id), &source_account); + let input = rlp::encode(&transaction).to_vec(); + let calling_account_id = "some-account.near".to_string(); + + // validate pre-state + test_utils::validate_address_balance_and_nonce( + &runner, + source_address, + INITIAL_BALANCE, + INITIAL_NONCE.into(), + ); + test_utils::validate_address_balance_and_nonce(&runner, dest_address, Wei::zero(), 0.into()); + + // attempt transfer + let (_, maybe_err) = runner.call(test_utils::SUBMIT, calling_account_id, input); + let error_message = format!("{:?}", maybe_err); + assert!(error_message.contains("ERR_INTRINSIC_GAS")); + + // validate post-state (which is the same as pre-state in this case) + test_utils::validate_address_balance_and_nonce( + &runner, + source_address, + INITIAL_BALANCE, + INITIAL_NONCE.into(), + ); + test_utils::validate_address_balance_and_nonce(&runner, dest_address, Wei::zero(), 0.into()); +} + fn initialize_transfer() -> (test_utils::AuroraRunner, SecretKey, Address) { // set up Aurora runner and accounts let mut runner = test_utils::deploy_evm(); diff --git a/src/transaction.rs b/src/transaction.rs index fe1be41ba..adad635cd 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -2,7 +2,7 @@ use crate::prelude::{Address, Vec, U256}; use crate::types::Wei; use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream}; -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, Clone)] pub struct EthTransaction { /// A monotonically increasing transaction counter for this sender pub nonce: U256, @@ -36,6 +36,36 @@ impl EthTransaction { s.append(&0u8); } } + + pub fn intrinsic_gas(&self, config: &evm::Config) -> Option { + let is_contract_creation = self.to.is_none(); + + let base_gas = if is_contract_creation { + config.gas_transaction_create + } else { + config.gas_transaction_call + }; + + let num_zero_bytes = self.data.iter().filter(|b| **b == 0).count(); + let num_non_zero_bytes = self.data.len() - num_zero_bytes; + + let gas_zero_bytes = config + .gas_transaction_zero_data + .checked_mul(num_zero_bytes as u64)?; + let gas_non_zero_bytes = config + .gas_transaction_non_zero_data + .checked_mul(num_non_zero_bytes as u64)?; + + base_gas + .checked_add(gas_zero_bytes) + .and_then(|gas| gas.checked_add(gas_non_zero_bytes)) + } + + /// Returns self.gas as a u64, or None if self.gas > u64::MAX + pub fn get_gas_limit(&self) -> Option { + use crate::prelude::TryInto; + self.gas.try_into().ok() + } } #[derive(Debug, Eq, PartialEq)] From d8f042d68cb1a197e3014f786041eeaacff19772 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sat, 5 Jun 2021 03:05:58 +0300 Subject: [PATCH 06/37] Add an `is_used_proof` interface. (#120) --- src/connector.rs | 8 ++- src/lib.rs | 14 +++++- src/parameters.rs | 8 +++ tests/test_connector.rs | 105 +++++++++++++++++++++++++++++++--------- 4 files changed, 108 insertions(+), 27 deletions(-) diff --git a/src/connector.rs b/src/connector.rs index fd4df0110..6db5a125a 100644 --- a/src/connector.rs +++ b/src/connector.rs @@ -8,7 +8,7 @@ use crate::deposit_event::*; use crate::engine::Engine; use crate::json::parse_json; use crate::prelude::*; -use crate::prover::validate_eth_address; +use crate::prover::{validate_eth_address, Proof}; use crate::storage::{self, EthConnectorStorageId, KeyPrefix}; #[cfg(feature = "log")] use alloc::format; @@ -181,7 +181,6 @@ impl EthConnectorContract { pub fn deposit(&self) { self.assert_not_paused(PAUSE_DEPOSIT); - use crate::prover::Proof; #[cfg(feature = "log")] sdk::log("[Deposit tokens]"); @@ -643,6 +642,11 @@ impl EthConnectorContract { sdk::storage_has_key(&self.used_event_key(key)) } + /// Checks whether the provided proof was already used + pub fn is_used_proof(&self, proof: Proof) -> bool { + self.check_used_event(&proof.get_key()) + } + /// Get Eth connector paused flags pub fn get_paused_flags(&self) -> PausedMask { self.get_paused() diff --git a/src/lib.rs b/src/lib.rs index 4fec40c07..54e648720 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -86,8 +86,9 @@ mod contract { #[cfg(feature = "evm_bully")] use crate::parameters::{BeginBlockArgs, BeginChainArgs}; use crate::parameters::{ - ExpectUtf8, FunctionCallArgs, GetStorageAtArgs, InitCallArgs, NewCallArgs, - PauseEthConnectorCallArgs, SetContractDataCallArgs, TransferCallCallArgs, ViewCallArgs, + ExpectUtf8, FunctionCallArgs, GetStorageAtArgs, InitCallArgs, IsUsedProofCallArgs, + NewCallArgs, PauseEthConnectorCallArgs, SetContractDataCallArgs, TransferCallCallArgs, + ViewCallArgs, }; use crate::prelude::{Address, H256, U256}; use crate::sdk; @@ -420,6 +421,15 @@ mod contract { EthConnectorContract::get_instance().finish_deposit_near(); } + #[no_mangle] + pub extern "C" fn is_used_proof() { + let args = IsUsedProofCallArgs::try_from_slice(&sdk::read_input()).expect(ERR_FAILED_PARSE); + + let is_used_proof = EthConnectorContract::get_instance().is_used_proof(args.proof); + let res = is_used_proof.try_to_vec().unwrap(); + sdk::return_output(&res[..]); + } + #[no_mangle] pub extern "C" fn ft_total_supply() { EthConnectorContract::get_instance().ft_total_supply(); diff --git a/src/parameters.rs b/src/parameters.rs index 7b1831427..fc2d4c5f9 100644 --- a/src/parameters.rs +++ b/src/parameters.rs @@ -145,6 +145,14 @@ pub struct DepositCallArgs { pub relayer_eth_account: Option, } +/// Eth-connector isUsedProof arguments +#[cfg(feature = "engine")] +#[derive(BorshSerialize, BorshDeserialize)] +pub struct IsUsedProofCallArgs { + /// Proof data + pub proof: Proof, +} + /// withdraw result for eth-connector #[cfg(feature = "engine")] #[derive(BorshSerialize)] diff --git a/tests/test_connector.rs b/tests/test_connector.rs index d0e4bda26..9dcb8c255 100644 --- a/tests/test_connector.rs +++ b/tests/test_connector.rs @@ -68,6 +68,11 @@ pub struct WithdrawResult { pub eth_custodian_address: EthAddress, } +#[derive(BorshDeserialize, Debug)] +pub struct IsUsedProofResult { + pub is_used_proof: bool, +} + fn init(custodian_address: &str) -> (UserAccount, UserAccount) { let master_account = near_sdk_sim::init_simulator(None); let contract = init_contract(&master_account, CONTRACT_ACC, custodian_address); @@ -144,6 +149,35 @@ fn call_deposit_near(master_account: &UserAccount, contract: &str) -> Vec